// 3.0.0 (docs/EVENTS-3.0-PLAN.md ยง9): connecting to the church's Accounts once the Hub lists it. const mockSettings = new Map(); const mockConnects: unknown[][] = []; let mockListed: { installId: string; product: string }[] = []; let mockLink: object | null = null; jest.mock('../src/links/connect', () => ({ connectTo: async (...args: unknown[]) => { mockConnects.push(args); mockLink = { installId: args[0] }; } })); jest.mock('../src/links/store', () => ({ linkedProducts: async () => mockListed })); jest.mock('../src/accounts/client', () => ({ accountsLink: async () => mockLink })); let mockMode = 'local'; jest.mock('../src/accounts/mode', () => ({ viaAccounts: async () => mockMode === 'accounts', getSetting: async (k: string) => mockSettings.get(k) ?? null, setSetting: async (k: string, v: string | null) => { if (v === null) mockSettings.delete(k); else mockSettings.set(k, v); }, })); import { AUTO_KEY, connectAccountsIfListed } from '../src/accounts/autoConnect'; const ACCOUNTS = '9a4c2f1e-7b3d-4e5a-8c6f-0d1e2f3a4b5c'; beforeEach(() => { mockSettings.clear(); mockConnects.length = 0; mockListed = []; mockLink = null; mockMode = 'local'; }); test('connects to the Accounts the Hub lists, as the system, once', async () => { mockListed = [{ installId: 'w1', product: 'website' }, { installId: ACCOUNTS, product: 'accounts' }]; expect(await connectAccountsIfListed()).toBe('connected'); expect(mockConnects).toEqual([[ACCOUNTS, { id: null, role: 'system' }, null, expect.any(Date)]]); expect(mockSettings.has(AUTO_KEY)).toBe(true); // An admin disconnects later: it isn't connected again. mockLink = null; expect(await connectAccountsIfListed()).toBe('skipped'); expect(mockConnects).toHaveLength(1); }); test('nothing listed yet: tries again at the next heartbeat', async () => { expect(await connectAccountsIfListed()).toBe('skipped'); expect(mockSettings.has(AUTO_KEY)).toBe(false); }); test('already connected by an admin: just remembers it', async () => { mockLink = { installId: ACCOUNTS }; mockListed = [{ installId: ACCOUNTS, product: 'accounts' }]; expect(await connectAccountsIfListed()).toBe('skipped'); expect(mockConnects).toEqual([]); expect(mockSettings.has(AUTO_KEY)).toBe(true); }); test('3.5.0: a site in Accounts mode that lost its link (a demo load before 3.5.0) connects again', async () => { mockSettings.set(AUTO_KEY, '2026-11-19T10:00:00.000Z'); mockListed = [{ installId: ACCOUNTS, product: 'accounts' }]; // On its own sign-in, an earlier Disconnect stays. expect(await connectAccountsIfListed()).toBe('skipped'); mockMode = 'accounts'; expect(await connectAccountsIfListed()).toBe('connected'); expect(mockConnects).toEqual([[ACCOUNTS, { id: null, role: 'system' }, null, expect.any(Date)]]); });