|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { createMockRequest } from '@sim/testing' |
| 5 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 6 | + |
| 7 | +const { mockGetInvitationJoinPreview, mockGetSession, mockListPendingInvitationsForEmail } = |
| 8 | + vi.hoisted(() => ({ |
| 9 | + mockGetInvitationJoinPreview: vi.fn(), |
| 10 | + mockGetSession: vi.fn(), |
| 11 | + mockListPendingInvitationsForEmail: vi.fn(), |
| 12 | + })) |
| 13 | + |
| 14 | +vi.mock('@/lib/auth', () => ({ |
| 15 | + auth: { api: { getSession: vi.fn() } }, |
| 16 | + getSession: mockGetSession, |
| 17 | +})) |
| 18 | + |
| 19 | +vi.mock('@/lib/invitations/core', () => ({ |
| 20 | + getInvitationJoinPreview: mockGetInvitationJoinPreview, |
| 21 | + listPendingInvitationsForEmail: mockListPendingInvitationsForEmail, |
| 22 | +})) |
| 23 | + |
| 24 | +import { GET } from '@/app/api/invitations/route' |
| 25 | + |
| 26 | +function invitation(id: string) { |
| 27 | + return { |
| 28 | + id, |
| 29 | + kind: 'organization', |
| 30 | + email: 'invitee@example.com', |
| 31 | + organizationId: 'org-1', |
| 32 | + organizationName: 'Org', |
| 33 | + membershipIntent: 'member', |
| 34 | + role: 'member', |
| 35 | + status: 'pending', |
| 36 | + expiresAt: new Date('2026-02-01T00:00:00.000Z'), |
| 37 | + createdAt: new Date('2026-01-01T00:00:00.000Z'), |
| 38 | + inviterName: 'Ada', |
| 39 | + inviterEmail: 'ada@example.com', |
| 40 | + grants: [{ workspaceId: 'ws-1', workspaceName: 'WS', permission: 'read' }], |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +describe('GET /api/invitations', () => { |
| 45 | + beforeEach(() => { |
| 46 | + vi.clearAllMocks() |
| 47 | + mockGetSession.mockResolvedValue({ |
| 48 | + user: { id: 'user-1', email: 'invitee@example.com' }, |
| 49 | + }) |
| 50 | + }) |
| 51 | + |
| 52 | + it('pairs each row with its own preview, in order', async () => { |
| 53 | + mockListPendingInvitationsForEmail.mockResolvedValue(['a', 'b', 'c'].map(invitation)) |
| 54 | + mockGetInvitationJoinPreview.mockImplementation(async (_userId, inv) => ({ for: inv.id })) |
| 55 | + |
| 56 | + const { invitations } = await (await GET(createMockRequest('GET'))).json() |
| 57 | + |
| 58 | + expect(invitations.map((i: { id: string }) => i.id)).toEqual(['a', 'b', 'c']) |
| 59 | + expect(invitations.map((i: { joinPreview: unknown }) => i.joinPreview)).toEqual([ |
| 60 | + { for: 'a' }, |
| 61 | + { for: 'b' }, |
| 62 | + { for: 'c' }, |
| 63 | + ]) |
| 64 | + }) |
| 65 | + |
| 66 | + /** |
| 67 | + * The preview is disclosure-only, so one failing row degrades to `null` rather than hiding |
| 68 | + * the invitation — which is what makes the bounded mapper safe, since it fails |
| 69 | + * all-or-nothing on a throwing mapper. |
| 70 | + */ |
| 71 | + it('degrades a failing preview to null without dropping the invitation', async () => { |
| 72 | + mockListPendingInvitationsForEmail.mockResolvedValue(['a', 'b'].map(invitation)) |
| 73 | + mockGetInvitationJoinPreview.mockImplementation(async (_userId, inv) => { |
| 74 | + if (inv.id === 'a') throw new Error('preview blew up') |
| 75 | + return { for: inv.id } |
| 76 | + }) |
| 77 | + |
| 78 | + const { invitations } = await (await GET(createMockRequest('GET'))).json() |
| 79 | + |
| 80 | + expect(invitations).toHaveLength(2) |
| 81 | + expect(invitations[0].joinPreview).toBeNull() |
| 82 | + expect(invitations[1].joinPreview).toEqual({ for: 'b' }) |
| 83 | + }) |
| 84 | + |
| 85 | + /** |
| 86 | + * Each preview issues several queries of its own, so the fan-out stays bounded rather than |
| 87 | + * holding one pooled connection per pending invitation. |
| 88 | + */ |
| 89 | + it('runs previews concurrently, up to a bound', async () => { |
| 90 | + mockListPendingInvitationsForEmail.mockResolvedValue( |
| 91 | + Array.from({ length: 12 }, (_, i) => invitation(`inv-${i}`)) |
| 92 | + ) |
| 93 | + let inFlight = 0 |
| 94 | + let peak = 0 |
| 95 | + mockGetInvitationJoinPreview.mockImplementation(async () => { |
| 96 | + inFlight++ |
| 97 | + peak = Math.max(peak, inFlight) |
| 98 | + await Promise.resolve() |
| 99 | + inFlight-- |
| 100 | + return null |
| 101 | + }) |
| 102 | + |
| 103 | + await GET(createMockRequest('GET')) |
| 104 | + |
| 105 | + expect(mockGetInvitationJoinPreview).toHaveBeenCalledTimes(12) |
| 106 | + expect(peak).toBe(4) |
| 107 | + }) |
| 108 | +}) |
0 commit comments