// 3.6.0: the nightly people check against Accounts (contracts 1.14.0 people.check). type U = { id: string; accountId: string; name: string; email: string; phoneNumber: string | null; newsEmail: boolean; newsWhatsApp: boolean }; let mockUsers: U[] = []; const mockUpdated: string[] = []; const mockCalls: { type: string; sub: string }[] = []; const mockErrors: string[] = []; let mockMode = true; let mockLinked = true; let mockDeferred = new Set(); jest.mock('../src/config/db', () => ({ user: { findMany: async ({ take, cursor }: { take: number; cursor?: { id: string } }) => { const sorted = [...mockUsers].sort((a, b) => a.id.localeCompare(b.id)); const start = cursor ? sorted.findIndex((u) => u.id === cursor.id) + 1 : 0; return sorted.slice(start, start + take); }, }, })); jest.mock('../src/accounts/mode', () => ({ viaAccounts: async () => mockMode })); jest.mock('../src/accounts/client', () => ({ accountsLink: async () => (mockLinked ? { installId: 'acc-1' } : null), accounts: async () => { throw new Error('not in tests'); } })); jest.mock('../src/accounts/signin', () => ({ updateLocalCopy: async (id: string) => { mockUpdated.push(id); } })); jest.mock('../src/accounts/calls', () => ({ handleAccountsCall: async (c: { type: string; sub: string }) => { mockCalls.push(c); return { status: mockDeferred.has(c.sub) ? 'deferred' : 'deleted' }; }, })); jest.mock('../src/accounts/status', () => ({ recordSignInError: async (_c: string, m: string) => { mockErrors.push(m); } })); import type { PeopleCheckReply, Profile } from '@crosscode/contracts'; import { runPeopleCheck, sameAsLocal } from '../src/accounts/peopleCheck'; const sub = (n: number) => `00000000-0000-4000-8000-${String(n).padStart(12, '0')}`; const user = (n: number, over: Partial = {}): U => ({ id: `u${String(n).padStart(3, '0')}`, accountId: sub(n), name: `Person ${n}`, email: `p${n}@hope.test`, phoneNumber: null, newsEmail: false, newsWhatsApp: false, ...over }); const profile = (u: U, over: Partial = {}): Profile => ({ name: u.name, given_name: null, family_name: null, email: u.email, email_verified: true, phone_number: u.phoneNumber, phone_number_verified: false, news_consent: { email: u.newsEmail, whatsapp: u.newsWhatsApp }, ...over }); beforeEach(() => { mockUsers = []; mockUpdated.length = 0; mockCalls.length = 0; mockErrors.length = 0; mockMode = true; mockLinked = true; mockDeferred = new Set(); }); test('updates only copies that differ, handles deleted accounts as user.deleted does, counts the rest', async () => { mockUsers = [user(1), user(2), user(3), user(4), user(5)]; mockDeferred = new Set([sub(4)]); const ask = async (subs: string[]): Promise => ({ schema: 'crosscode.identity-request/1', people: subs.map((s) => { const u = mockUsers.find((x) => x.accountId === s)!; if (s === sub(1)) return { sub: s, status: 'active' as const, profile: profile(u) }; if (s === sub(2)) return { sub: s, status: 'active' as const, profile: profile(u, { name: 'New Name', news_consent: { email: true, whatsapp: false } }) }; if (s === sub(3) || s === sub(4)) return { sub: s, status: 'deleted' as const, profile: null }; return { sub: s, status: 'unknown' as const, profile: null }; }), }); expect(await runPeopleCheck({ ask, pageSize: 2 })).toEqual({ checked: 5, updated: 1, deleted: 1, deferred: 1, deactivated: 0, unknown: 1 }); expect(mockUpdated).toEqual(['u002']); expect(mockCalls.map((c) => [c.type, c.sub])).toEqual([['user.deleted', sub(3)], ['user.deleted', sub(4)]]); }); test('stops, changing nothing, when Accounts knows hardly anyone (linked to the wrong Accounts?)', async () => { mockUsers = Array.from({ length: 12 }, (_, i) => user(i + 1)); const ask = async (subs: string[]): Promise => ({ schema: 'crosscode.identity-request/1', people: subs.map((s) => ({ sub: s, status: 'unknown' as const, profile: null })) }); await expect(runPeopleCheck({ ask })).rejects.toThrow(/doesn't know 12 of 12/); expect(mockCalls).toEqual([]); expect(mockErrors).toHaveLength(1); }); test('skipped on its own sign-in, or without a link', async () => { mockMode = false; expect(await runPeopleCheck({ ask: async () => { throw new Error('asked'); } })).toEqual({ skipped: expect.any(String) }); mockMode = true; mockLinked = false; expect(await runPeopleCheck({ ask: async () => { throw new Error('asked'); } })).toEqual({ skipped: expect.any(String) }); }); test('sameAsLocal ignores email case and what Events doesn\'t keep', () => { const u = user(1, { email: 'Jane@Hope.test' }); expect(sameAsLocal(u, profile(u, { email: 'jane@hope.test', email_verified: false, given_name: 'Jane' }))).toBe(true); expect(sameAsLocal(u, profile(u, { phone_number: '+27821234567' }))).toBe(false); }); test('an Accounts before 1.5.0 (people.check unknown there) skips the check instead of failing every night', async () => { mockUsers = [user(1)]; const ask = async () => { throw Object.assign(new Error('Not a crosscode.identity-request/1 request: type Invalid input'), { code: 'INVALID_REQUEST' }); }; expect(await runPeopleCheck({ ask })).toEqual({ skipped: expect.stringContaining('1.5.0') }); });