Something went wrong. Try again.
Monorepo for Tangled forked from tangled.org/core
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181import { beforeEach, describe, expect, it, vi } from "vitest";import { getUnreadCount, hydrateNotifications, listNotifications, updateNotificationSeen} from "#lib/api/notifications.ts";import type { Auth } from "#lib/auth.svelte.ts";import { createFixtureNotifications, createNotifications, retainRows} from "#lib/notifications.svelte.ts";import { notifications } from "#lib/components/notifications/mock.ts";import type { NotificationSummary } from "#lib/components/notifications/types.ts";
vi.mock("#lib/api/notifications.ts", () => ({ getUnreadCount: vi.fn(), hydrateNotifications: vi.fn(async () => []), listNotifications: vi.fn(async () => ({ notifications: [] })), markAllNotificationsRead: vi.fn(), markEntityNotificationsRead: vi.fn(), updateNotificationSeen: vi.fn()}));
const rows: NotificationSummary[] = notifications.map((notification, index) => ({ ...notification, uri: `at://mock/notif/${index}`, read: index % 2 === 1}));
const unreadRow = rows.find((row) => !row.read)!;const readRow = rows.find((row) => row.read)!;const baseline = rows.filter((row) => !row.read).length;
const readOf = ( service: { overlay(rows: NotificationSummary[]): NotificationSummary[] }, uri: string) => service.overlay(rows).find((row) => row.uri === uri)!.read;
describe("notification read state", () => { it("moves the count with the row it marked", async () => { const service = createFixtureNotifications(rows); expect(service.unread).toBe(baseline);
await service.setRead(unreadRow.uri, true); expect(service.unread).toBe(baseline - 1); expect(readOf(service, unreadRow.uri)).toBe(true);
await service.setRead(unreadRow.uri, false); expect(service.unread).toBe(baseline); expect(readOf(service, unreadRow.uri)).toBe(false); });
it("ignores a mark that changes nothing", async () => { const service = createFixtureNotifications(rows);
await service.setRead(unreadRow.uri, true); await service.setRead(unreadRow.uri, true); await service.setRead(readRow.uri, true);
expect(service.unread).toBe(baseline - 1); });
it("empties the count and every known row on mark all read", async () => { const service = createFixtureNotifications(rows);
await service.markAllRead();
expect(service.unread).toBe(0); expect(service.overlay(rows).every((row) => row.read)).toBe(true); });});
describe("list", () => { const alice = "did:plc:alice"; const auth = { currentDid: alice, agent: { sub: alice } } as unknown as Auth; const mockedCount = vi.mocked(getUnreadCount); const create = () => createNotifications(auth, "https://notifs.example", "https://bobbin.example");
beforeEach(() => { vi.clearAllMocks(); mockedCount.mockResolvedValue({ count: 3 }); });
it("asks for the unread count once when two lists load together", async () => { const service = create();
await Promise.all([service.list({ read: "unread", limit: 5 }), service.list({ limit: 5 })]);
expect(mockedCount).toHaveBeenCalledTimes(1); expect(listNotifications).toHaveBeenCalledTimes(2); expect(hydrateNotifications).toHaveBeenCalledTimes(2); expect(service.unread).toBe(3); });
it("asks again once the earlier count has landed", async () => { const service = create();
await service.list(); mockedCount.mockResolvedValue({ count: 1 }); await service.list();
expect(mockedCount).toHaveBeenCalledTimes(2); expect(service.unread).toBe(1); });
it("keeps the read state it just changed while the write is in flight", async () => { const service = create(); vi.mocked(hydrateNotifications).mockResolvedValueOnce(rows); await service.list(); expect(service.unread).toBe(3);
let release!: () => void; vi.mocked(updateNotificationSeen).mockReturnValueOnce( new Promise<void>((resolve) => (release = resolve)) );
const marking = service.setRead(unreadRow.uri, true); await vi.waitFor(() => expect(updateNotificationSeen).toHaveBeenCalled()); expect(service.unread).toBe(2);
vi.mocked(hydrateNotifications).mockResolvedValueOnce(rows); await service.list();
expect(service.unread).toBe(2); expect(readOf(service, unreadRow.uri)).toBe(true);
release(); await marking; });
it("ignores a snapshot that was already in flight when a write landed", async () => { const service = create(); let resolveHydration!: (rows: NotificationSummary[]) => void; vi.mocked(hydrateNotifications).mockReturnValueOnce( new Promise<NotificationSummary[]>((resolve) => (resolveHydration = resolve)) );
const listing = service.list(); await vi.waitFor(() => expect(hydrateNotifications).toHaveBeenCalled());
vi.mocked(updateNotificationSeen).mockResolvedValueOnce(undefined); await service.setRead(unreadRow.uri, true);
resolveHydration(rows); await listing;
expect(service.unread).toBe(0); expect(readOf(service, unreadRow.uri)).toBe(true); });});
describe("retainRows", () => { const row = (uri: string, createdAt: string, read = false): NotificationSummary => ({ uri, type: "followed", read, createdAt, actor: { did: "did:plc:actor", handle: "actor.test" } });
it("keeps the rows a newer snapshot no longer carries, newest first", () => { const current = [ row("b", "2026-01-01T02:00:00.000Z"), row("a", "2026-01-01T01:00:00.000Z") ]; const incoming = [row("c", "2026-01-01T03:00:00.000Z")];
expect(retainRows(current, incoming).map((n) => n.uri)).toEqual(["c", "b", "a"]); });
it("takes the incoming read state for a uri both snapshots carry", () => { const current = [row("a", "2026-01-01T01:00:00.000Z")]; const incoming = [row("a", "2026-01-01T01:00:00.000Z", true)];
expect(retainRows(current, incoming)[0].read).toBe(true); });});