/** * Account registry: pure state transitions. These invariants keep multi-account * isolation honest — removing the active account always leaves a deterministic * successor, and removing an instance removes every account it owns. */ import { addAccount, getActive, parseAccountState, removeAccount, removeInstance, setActiveAccount, updateAccountProfile, upsertInstance, } from './registry'; import { EMPTY_ACCOUNT_STATE, type AccountState } from './types'; function state(partial: Partial): AccountState { return { ...EMPTY_ACCOUNT_STATE, ...partial }; } const instanceA = { id: 'inst-a', baseUrl: 'https://a.example', label: 'a.example', addedAt: 1 }; const instanceB = { id: 'inst-b', baseUrl: 'https://b.example', label: 'b.example', addedAt: 2 }; const accountA1 = { id: 'a1', instanceId: 'inst-a', login: 'alice', addedAt: 10, lastUsedAt: 100, }; const accountA2 = { id: 'a2', instanceId: 'inst-a', login: 'anna', addedAt: 20, lastUsedAt: 200, }; const accountB1 = { id: 'b1', instanceId: 'inst-b', login: 'bob', addedAt: 30, lastUsedAt: 300, }; describe('parseAccountState', () => { it('returns empty state for null / malformed storage', () => { expect(parseAccountState(null)).toEqual(EMPTY_ACCOUNT_STATE); expect(parseAccountState('not json')).toEqual(EMPTY_ACCOUNT_STATE); expect(parseAccountState('42')).toEqual(EMPTY_ACCOUNT_STATE); }); it('drops records that miss required fields', () => { const raw = JSON.stringify({ instances: [instanceA, { id: 'broken' }], accounts: [accountA1, { login: 'no-id' }], activeAccountId: 'a1', }); const parsed = parseAccountState(raw); expect(parsed.instances).toHaveLength(1); expect(parsed.accounts).toHaveLength(1); expect(parsed.activeAccountId).toBe('a1'); }); it('falls back to the first account when the active id is stale', () => { const raw = JSON.stringify({ instances: [instanceA], accounts: [accountA2, accountA1], activeAccountId: 'ghost' }); expect(parseAccountState(raw).activeAccountId).toBe('a2'); }); }); describe('upsertInstance', () => { it('creates a new instance the first time', () => { const { state: next, instance } = upsertInstance(EMPTY_ACCOUNT_STATE, { baseUrl: instanceA.baseUrl, label: instanceA.label, }); expect(next.instances).toHaveLength(1); expect(instance.baseUrl).toBe(instanceA.baseUrl); }); it('reuses the existing instance for the same base URL', () => { const initial = state({ instances: [instanceA] }); const { state: next, instance } = upsertInstance(initial, { baseUrl: instanceA.baseUrl, label: 'ignored', version: '1.21.0', }); expect(next.instances).toHaveLength(1); expect(instance.id).toBe('inst-a'); expect(instance.version).toBe('1.21.0'); }); }); describe('addAccount', () => { it('adds a new account and activates it', () => { const { state: next, account } = addAccount(state({ instances: [instanceA] }), { instanceId: 'inst-a', login: 'alice', userId: 1, }); expect(next.accounts).toHaveLength(1); expect(next.activeAccountId).toBe(account.id); }); it('refreshes an existing (instance, login) instead of duplicating it', () => { const initial = state({ instances: [instanceA], accounts: [accountA1], activeAccountId: 'a2' }); const { state: next, account } = addAccount(initial, { instanceId: 'inst-a', login: 'ALICE', fullName: 'Alice', }); expect(next.accounts).toHaveLength(1); expect(account.id).toBe('a1'); expect(account.fullName).toBe('Alice'); expect(next.activeAccountId).toBe('a1'); }); }); describe('removeAccount', () => { it('reassigns the active account to the most recently used survivor', () => { const initial = state({ instances: [instanceA], accounts: [accountA1, accountA2], activeAccountId: 'a2', }); const { state: next, removed } = removeAccount(initial, 'a2'); expect(removed?.id).toBe('a2'); expect(next.accounts.map((a) => a.id)).toEqual(['a1']); expect(next.activeAccountId).toBe('a1'); }); it('clears the active account when the last one is removed', () => { const initial = state({ instances: [instanceA], accounts: [accountA1], activeAccountId: 'a1' }); expect(removeAccount(initial, 'a1').state.activeAccountId).toBeNull(); }); it('leaves the active account alone when removing a different one', () => { const initial = state({ instances: [instanceA], accounts: [accountA1, accountA2], activeAccountId: 'a1', }); expect(removeAccount(initial, 'a2').state.activeAccountId).toBe('a1'); }); }); describe('removeInstance', () => { it('removes the instance and all of its accounts', () => { const initial = state({ instances: [instanceA, instanceB], accounts: [accountA1, accountA2, accountB1], activeAccountId: 'a1', }); const { state: next, removedAccounts } = removeInstance(initial, 'inst-a'); expect(removedAccounts.map((a) => a.id).sort()).toEqual(['a1', 'a2']); expect(next.instances.map((i) => i.id)).toEqual(['inst-b']); expect(next.accounts.map((a) => a.id)).toEqual(['b1']); expect(next.activeAccountId).toBe('b1'); }); }); describe('setActiveAccount + getActive', () => { it('activates a known account and resolves its instance', () => { const initial = state({ instances: [instanceA], accounts: [accountA1, accountA2], activeAccountId: null }); const { state: next } = setActiveAccount(initial, 'a2'); expect(next.activeAccountId).toBe('a2'); const active = getActive(next); expect(active?.account.id).toBe('a2'); expect(active?.instance.id).toBe('inst-a'); }); it('ignores unknown account ids', () => { const initial = state({ instances: [instanceA], accounts: [accountA1], activeAccountId: 'a1' }); const { account } = setActiveAccount(initial, 'ghost'); expect(account).toBeNull(); }); }); describe('updateAccountProfile', () => { it('merges profile fields', () => { const initial = state({ instances: [instanceA], accounts: [accountA1] }); const { account } = updateAccountProfile(initial, 'a1', { fullName: 'Alice', avatarUrl: 'https://x/y.png' }); expect(account?.fullName).toBe('Alice'); expect(account?.avatarUrl).toBe('https://x/y.png'); expect(account?.login).toBe('alice'); }); });