|
| 1 | +/* @vitest-environment node */ |
| 2 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 3 | + |
| 4 | +vi.mock("./functions", () => ({ |
| 5 | + mutation: (def: { handler: unknown }) => ({ _handler: def.handler }), |
| 6 | + query: (def: { handler: unknown }) => ({ _handler: def.handler }), |
| 7 | +})); |
| 8 | +vi.mock("./lib/access", () => ({ requireUser: vi.fn() })); |
| 9 | + |
| 10 | +const { requireUser } = await import("./lib/access"); |
| 11 | +const { getMine, markSeenThrough } = await import("./publisherActivityInbox"); |
| 12 | + |
| 13 | +type WrappedHandler<TArgs, TResult> = { |
| 14 | + _handler: (ctx: unknown, args: TArgs) => Promise<TResult>; |
| 15 | +}; |
| 16 | + |
| 17 | +const getMineHandler = (getMine as unknown as WrappedHandler<Record<string, never>, unknown>) |
| 18 | + ._handler; |
| 19 | +const markSeenThroughHandler = ( |
| 20 | + markSeenThrough as unknown as WrappedHandler<{ groupId: string }, unknown> |
| 21 | +)._handler; |
| 22 | + |
| 23 | +function makeCtx({ |
| 24 | + group = { _id: "publisherActivityGroups:1", publisherId: "publishers:1", sortKey: "200:b" }, |
| 25 | + follow = { _id: "publisherFollows:1" } as { _id: string } | null, |
| 26 | + state = null as null | { |
| 27 | + _id: string; |
| 28 | + userId: string; |
| 29 | + seenThroughSortKey: string; |
| 30 | + updatedAt: number; |
| 31 | + }, |
| 32 | +} = {}) { |
| 33 | + const insert = vi.fn(async () => "publisherActivityInboxState:1"); |
| 34 | + const patch = vi.fn(); |
| 35 | + const query = vi.fn((table: string) => ({ |
| 36 | + withIndex: vi.fn((_index: string, build: (q: unknown) => unknown) => { |
| 37 | + const q = { eq: vi.fn().mockReturnThis() }; |
| 38 | + build(q); |
| 39 | + return { unique: vi.fn(async () => (table === "publisherFollows" ? follow : state)) }; |
| 40 | + }), |
| 41 | + })); |
| 42 | + return { |
| 43 | + db: { get: vi.fn(async () => group), insert, patch, query }, |
| 44 | + insert, |
| 45 | + patch, |
| 46 | + }; |
| 47 | +} |
| 48 | + |
| 49 | +afterEach(() => vi.restoreAllMocks()); |
| 50 | + |
| 51 | +describe("publisher activity inbox state", () => { |
| 52 | + it("returns an empty read frontier for a new inbox", async () => { |
| 53 | + vi.mocked(requireUser).mockResolvedValue({ userId: "users:1" } as never); |
| 54 | + const result = await getMineHandler(makeCtx(), {}); |
| 55 | + expect(result).toEqual({ ok: true, seenThroughSortKey: null, updatedAt: null }); |
| 56 | + }); |
| 57 | + |
| 58 | + it("creates a seen-through frontier for a followed activity group", async () => { |
| 59 | + vi.mocked(requireUser).mockResolvedValue({ userId: "users:1" } as never); |
| 60 | + const ctx = makeCtx(); |
| 61 | + const result = await markSeenThroughHandler(ctx, { groupId: "publisherActivityGroups:1" }); |
| 62 | + expect(ctx.insert).toHaveBeenCalledWith("publisherActivityInboxState", { |
| 63 | + userId: "users:1", |
| 64 | + seenThroughSortKey: "200:b", |
| 65 | + updatedAt: expect.any(Number), |
| 66 | + }); |
| 67 | + expect(result).toEqual({ ok: true, seenThroughSortKey: "200:b" }); |
| 68 | + }); |
| 69 | + |
| 70 | + it("never moves the frontier backwards", async () => { |
| 71 | + vi.mocked(requireUser).mockResolvedValue({ userId: "users:1" } as never); |
| 72 | + const ctx = makeCtx({ |
| 73 | + state: { |
| 74 | + _id: "publisherActivityInboxState:1", |
| 75 | + userId: "users:1", |
| 76 | + seenThroughSortKey: "300:c", |
| 77 | + updatedAt: 1, |
| 78 | + }, |
| 79 | + }); |
| 80 | + const result = await markSeenThroughHandler(ctx, { groupId: "publisherActivityGroups:1" }); |
| 81 | + expect(ctx.patch).not.toHaveBeenCalled(); |
| 82 | + expect(result).toEqual({ ok: true, seenThroughSortKey: "300:c" }); |
| 83 | + }); |
| 84 | + |
| 85 | + it("does not reveal groups from publishers the user no longer follows", async () => { |
| 86 | + vi.mocked(requireUser).mockResolvedValue({ userId: "users:1" } as never); |
| 87 | + const ctx = makeCtx({ follow: null }); |
| 88 | + await expect( |
| 89 | + markSeenThroughHandler(ctx, { groupId: "publisherActivityGroups:1" }), |
| 90 | + ).rejects.toThrow("Publisher activity group not found"); |
| 91 | + expect(ctx.insert).not.toHaveBeenCalled(); |
| 92 | + }); |
| 93 | +}); |
0 commit comments