|
| 1 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 2 | +import { canViewSpace } from '../../../community/blacksky/tenant-gate.js' |
| 3 | +import { runNotificationCount } from './getUnreadCount.js' |
| 4 | + |
| 5 | +vi.mock('../../../community/blacksky/tenant-gate.js', () => ({ |
| 6 | + canViewSpace: vi.fn(), |
| 7 | +})) |
| 8 | + |
| 9 | +const firstSpace = 'at://did:plc:tenant/space/community.blacksky.feed/first' |
| 10 | +const secondSpace = 'at://did:plc:tenant/space/community.blacksky.feed/second' |
| 11 | +const viewer = 'did:plc:viewer' |
| 12 | + |
| 13 | +const context = () => { |
| 14 | + const getUnreadNotificationSpaces = vi.fn(async () => ({ |
| 15 | + spaces: [ |
| 16 | + { postUri: `${firstSpace}/post/one`, spaceUri: firstSpace }, |
| 17 | + { postUri: `${firstSpace}/post/two`, spaceUri: firstSpace }, |
| 18 | + { postUri: `${secondSpace}/post/three`, spaceUri: secondSpace }, |
| 19 | + ], |
| 20 | + })) |
| 21 | + const getUnreadNotificationCount = vi.fn( |
| 22 | + async ({ allowedSpaceUris }: { allowedSpaceUris: string[] }) => ({ |
| 23 | + count: 2 + allowedSpaceUris.length, |
| 24 | + }), |
| 25 | + ) |
| 26 | + return { |
| 27 | + ctx: { |
| 28 | + hydrator: { |
| 29 | + dataplane: { |
| 30 | + getUnreadNotificationSpaces, |
| 31 | + getUnreadNotificationCount, |
| 32 | + }, |
| 33 | + }, |
| 34 | + } as any, |
| 35 | + getUnreadNotificationSpaces, |
| 36 | + getUnreadNotificationCount, |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +describe(runNotificationCount, () => { |
| 41 | + beforeEach(() => { |
| 42 | + vi.mocked(canViewSpace).mockReset() |
| 43 | + }) |
| 44 | + |
| 45 | + it('deduplicates candidates and counts public plus authorized spaces', async () => { |
| 46 | + const { ctx, getUnreadNotificationCount } = context() |
| 47 | + vi.mocked(canViewSpace).mockImplementation(async (_ctx, spaceUri) => |
| 48 | + Promise.resolve(spaceUri === firstSpace), |
| 49 | + ) |
| 50 | + |
| 51 | + await expect( |
| 52 | + runNotificationCount({ viewer }, ctx, 'authorized-union'), |
| 53 | + ).resolves.toEqual({ count: 3 }) |
| 54 | + expect(canViewSpace).toHaveBeenCalledTimes(2) |
| 55 | + expect(canViewSpace).toHaveBeenCalledWith(ctx, firstSpace, viewer) |
| 56 | + expect(canViewSpace).toHaveBeenCalledWith(ctx, secondSpace, viewer) |
| 57 | + expect(getUnreadNotificationCount).toHaveBeenCalledWith({ |
| 58 | + actorDid: viewer, |
| 59 | + priority: false, |
| 60 | + allowedSpaceUris: [firstSpace], |
| 61 | + }) |
| 62 | + }) |
| 63 | + |
| 64 | + it('fails closed when one space authorization throws', async () => { |
| 65 | + const { ctx, getUnreadNotificationCount } = context() |
| 66 | + vi.mocked(canViewSpace).mockImplementation(async (_ctx, spaceUri) => { |
| 67 | + if (spaceUri === firstSpace) throw new Error('authorization unavailable') |
| 68 | + return true |
| 69 | + }) |
| 70 | + |
| 71 | + await expect( |
| 72 | + runNotificationCount({ viewer }, ctx, 'authorized-union'), |
| 73 | + ).resolves.toEqual({ count: 3 }) |
| 74 | + expect(getUnreadNotificationCount).toHaveBeenCalledWith({ |
| 75 | + actorDid: viewer, |
| 76 | + priority: false, |
| 77 | + allowedSpaceUris: [secondSpace], |
| 78 | + }) |
| 79 | + }) |
| 80 | + |
| 81 | + it('makes no candidate or authorization calls in public-only mode', async () => { |
| 82 | + const { ctx, getUnreadNotificationSpaces, getUnreadNotificationCount } = |
| 83 | + context() |
| 84 | + |
| 85 | + await expect( |
| 86 | + runNotificationCount({ viewer, priority: true }, ctx, 'public-only'), |
| 87 | + ).resolves.toEqual({ count: 2 }) |
| 88 | + expect(getUnreadNotificationSpaces).not.toHaveBeenCalled() |
| 89 | + expect(canViewSpace).not.toHaveBeenCalled() |
| 90 | + expect(getUnreadNotificationCount).toHaveBeenCalledWith({ |
| 91 | + actorDid: viewer, |
| 92 | + priority: true, |
| 93 | + allowedSpaceUris: [], |
| 94 | + }) |
| 95 | + }) |
| 96 | +}) |
0 commit comments