// 3.5.0: how people sign in, for the Hub (heartbeat stats.signIn, contracts 1.13.0). import { hub } from '@crosscode/contracts'; const mockSettings = new Map(); let mockMode = 'accounts'; let mockLink: object | null = null; jest.mock('../src/accounts/client', () => ({ accountsLink: async () => mockLink })); jest.mock('../src/accounts/mode', () => ({ authMode: async () => mockMode, 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 { clearSignInError, recordSignInError, signInStatus } from '../src/accounts/status'; beforeEach(() => { mockSettings.clear(); mockMode = 'accounts'; mockLink = null; }); test('Accounts mode without a link, and why the last sign-in failed', async () => { await recordSignInError('ACCOUNTS_UNAVAILABLE', 'Accounts isn\'t connected.\nConnect it under Settings → Linked products.', new Date('2026-11-20T10:00:00.000Z')); const s = await signInStatus(); expect(s).toEqual({ mode: 'accounts', accounts: null, lastError: { at: '2026-11-20T10:00:00.000Z', code: 'ACCOUNTS_UNAVAILABLE', message: 'Accounts isn\'t connected. Connect it under Settings → Linked products.' } }); // It fits the heartbeat's contract. expect(hub.eventsStatsSchema.shape.signIn.safeParse(s).success).toBe(true); await clearSignInError(); expect((await signInStatus()).lastError).toBeNull(); }); test('linked: the Accounts install and its address', async () => { mockLink = { installId: 'acc-1', url: 'https://accounts.hope.test', secret: 'x', updatedAt: new Date() }; expect((await signInStatus()).accounts).toEqual({ installId: 'acc-1', url: 'https://accounts.hope.test' }); });