// 3.0.0 (docs/EVENTS-3.0-PLAN.md ยง7): Accounts' user.export and user.deleted calls, answered with // Events' existing export and deletion rules. The sign-in itself, the move and the other calls run // against the real Accounts in tests/accounts-integration (npm run test:accounts). const mockUsers = new Map(); const mockAnonymised: string[] = []; let mockBlockers: { eventTitle: string; endDate: Date }[] = []; jest.mock('../src/config/db', () => ({ user: { findUnique: async ({ where }: { where: { accountId?: string; id?: string } }) => [...mockUsers.values()].find((u) => (where.accountId ? u.accountId === where.accountId : u.id === where.id)) ?? null, update: async ({ where, data }: { where: { id: string }; data: Record }) => { const u = mockUsers.get(where.id)!; if (data.tokenVersion && typeof data.tokenVersion === 'object') u.tokenVersion += 1; if ('accountId' in data) u.accountId = data.accountId as string | null; return u; }, }, })); jest.mock('../src/utils/privacy', () => ({ exportUserData: async (id: string) => ({ account: { id, name: 'Thandi' }, registrations: [{ event: 'Youth Camp' }], payments: [], seasonPasses: [] }), deletionBlockers: async () => mockBlockers, anonymiseUser: async (id: string) => { mockAnonymised.push(id); }, })); import { IDENTITY_SCHEMA, userDeletedReplySchema, userExportReplySchema } from '@crosscode/contracts'; import { exportSections, handleAccountsCall } from '../src/accounts/calls'; const SUB = '5f0c7d2a-3b1e-4c8f-9a6d-2e4b1c7f8a90'; const base = { schema: IDENTITY_SCHEMA, callId: '0b6e1f2a-3c4d-4e5f-8a9b-0c1d2e3f4a5b', occurredAt: '2026-11-20T10:00:00.000Z', sub: SUB } as const; beforeEach(() => { mockUsers.clear(); mockUsers.set('u1', { id: 'u1', accountId: SUB, tokenVersion: 0 }); mockAnonymised.length = 0; mockBlockers = []; }); test('user.export answers with Events\' export as sections, empty ones left out', async () => { const reply = await handleAccountsCall({ ...base, type: 'user.export', exportId: '1c7f2a3b-4d5e-4f6a-9b0c-1d2e3f4a5b6c' }); expect(userExportReplySchema.safeParse(reply).success).toBe(true); const sections = (reply as { sections: { key: string; title: string }[] }).sections; expect(sections.map((s) => s.key)).toEqual(['account', 'registrations']); expect(sections[1]!.title).toBe('Registrations'); }); test('section keys fit the contract', () => { expect(exportSections({ seasonPasses: [{}], kidsFamily: { a: 1 } }).map((s) => s.key)).toEqual(['season_passes', 'kids_family']); }); test('user.deleted waits while there is a registration for an event that has not ended', async () => { mockBlockers = [{ eventTitle: 'Youth Camp', endDate: new Date('2027-03-14T12:00:00Z') }]; const reply = await handleAccountsCall({ ...base, type: 'user.deleted', approvedAt: base.occurredAt }); expect(userDeletedReplySchema.parse(reply)).toMatchObject({ status: 'deferred', reason: 'Has a registration for Youth Camp (2027-03-14)' }); expect(new Date((reply as { retryAfter: string }).retryAfter).getTime()).toBeGreaterThan(new Date('2027-03-14T12:00:00Z').getTime()); expect(mockAnonymised).toEqual([]); }); test('user.deleted anonymises, signs them out here and unlinks the account', async () => { const reply = await handleAccountsCall({ ...base, type: 'user.deleted', approvedAt: base.occurredAt }); expect(reply).toEqual({ schema: IDENTITY_SCHEMA, status: 'deleted' }); expect(mockAnonymised).toEqual(['u1']); expect(mockUsers.get('u1')).toMatchObject({ accountId: null, tokenVersion: 1 }); }); test('a call for someone Events doesn\'t know is answered as done', async () => { const other = { ...base, sub: '8d2e4f6a-1b3c-4d5e-8f7a-9b0c1d2e3f4a' }; expect(await handleAccountsCall({ ...other, type: 'user.deleted', approvedAt: base.occurredAt })).toEqual({ schema: IDENTITY_SCHEMA, status: 'deleted' }); const exp = await handleAccountsCall({ ...other, type: 'user.export', exportId: '1c7f2a3b-4d5e-4f6a-9b0c-1d2e3f4a5b6c' }); expect((exp as { sections: unknown[] }).sections).toEqual([]); });