// How the accounts page reads a service's rotation off its profiles and // writes it back (the accounts spec's mapping). A daemon E2E sees only the // saved result; these check the choices: which profile leads, what order a // list with stale ids shows, which profiles a write leaves alone. import assert from 'node:assert/strict'; import { readFile } from 'node:fs/promises'; import { stripTypeScriptTypes } from 'node:module'; import test from 'node:test'; const moduleURL = (code) => `data:text/javascript;base64,${Buffer.from(code).toString('base64')}`; const typeless = async (path) => stripTypeScriptTypes( (await readFile(new URL(path, import.meta.url), 'utf8')).replace( /^import type .*;\n/gm, '', ), { mode: 'transform' }, ); const providersURL = moduleURL( await typeless('../src/lib/settings/providers.ts'), ); const accounts = await import( moduleURL( (await typeless('../src/lib/settings/accounts.ts')).replace( /from ['"]\.\/providers['"]/, `from ${JSON.stringify(providersURL)}`, ), ) ); const account = (id, extra = {}) => ({ id, provider: 'claude', label: id, detail: '', kind: 'model', source: 'saved', endpoint: null, limited_until: null, email: null, plan: null, needs_sign_in: false, selected_by_profiles: [], rotated_by_profiles: [], ...extra, }); const profile = (extra = {}) => ({ extension: 'claude', endpoint: null, protocol: 'chat_completions', model: 'm', effort: null, image_edge: null, account_id: null, account_ids: null, rotation: 'failover', project: null, location: null, has_key: false, ...extra, }); const auth = [ account('a'), account('b'), account('c'), account('g', { provider: 'codex' }), account('k', { kind: 'key', provider: 'openai' }), ]; test('the default profile leads, else the first that rotates', () => { const settings = { default_profile: 'zed', profiles: { alpha: profile(), beta: profile({ account_ids: ['b'] }), zed: profile({ account_ids: ['c'], rotation: 'spread' }), gpt: profile({ extension: 'codex' }), }, }; assert.equal(accounts.leadProfile('claude', settings), 'zed'); assert.equal( accounts.leadProfile('claude', { ...settings, default_profile: 'gpt' }), 'beta', ); assert.equal(accounts.leadProfile('antigravity', settings), null); }); test('order is the lead list (existing ids only), then the rest in auth order', () => { const settings = { default_profile: null, profiles: { claude: profile({ account_ids: ['c', 'gone', 'a'], rotation: 'spread' }), }, }; const { order, mode } = accounts.serviceRotation('claude', settings, auth); assert.deepEqual( order.map((item) => item.id), ['c', 'a', 'b'], ); assert.equal(mode, 'spread'); }); test('an automatic lead with two accounts shows as Only #1', () => { const settings = { default_profile: null, profiles: { claude: profile() } }; assert.equal( accounts.serviceRotation('claude', settings, auth).mode, 'first', ); assert.equal( accounts.serviceRotation('codex', settings, auth).mode, 'failover', ); }); test('a write sets every following profile and leaves pins and own keys alone', () => { const settings = { default_profile: null, profiles: { auto: profile(), list: profile({ account_ids: ['a'] }), pinned: profile({ account_id: 'b' }), keyed: profile({ has_key: true }), gpt: profile({ extension: 'codex' }), }, }; const change = { account_ids: ['b', 'a'], rotation: 'spread', account_id: null, }; assert.deepEqual( accounts.rotationPatch('claude', settings, ['b', 'a'], 'spread'), { profiles: { auto: change, list: change } }, ); assert.deepEqual( accounts.rotationPatch('antigravity', settings, ['x'], 'first'), {}, ); }); test('moving clamps at the ends', () => { assert.deepEqual(accounts.moved(['a', 'b', 'c'], 'c', -1), ['a', 'c', 'b']); assert.deepEqual(accounts.moved(['a', 'b'], 'a', -1), ['a', 'b']); }); test('status, and when every account is limited', () => { const now = Date.parse('2026-11-17T10:00:00Z'); const soon = account('s', { limited_until: '2026-11-17T11:00:00Z' }); const later = account('l', { limited_until: '2026-11-17T12:00:00Z' }); const past = account('p', { limited_until: '2026-11-17T09:00:00Z' }); const expired = account('e', { needs_sign_in: true }); assert.equal(accounts.status(past, now).kind, 'ready'); assert.equal(accounts.status(expired, now).text, 'needs sign-in'); assert.match(accounts.status(soon, now).text, /^limited until /); assert.equal( accounts.allLimited([later, soon, expired], now)?.toISOString(), '2026-11-17T11:00:00.000Z', ); assert.equal(accounts.allLimited([later, past], now), null); assert.equal(accounts.allLimited([expired], now), null); }); test('sign-out says what happens next', () => { assert.equal( accounts.signOutConsequence(auth[0], auth), 'Claude keeps running on b, c.', ); assert.equal( accounts.signOutConsequence(auth[3], auth), 'ChatGPT models stop working until you sign in again. Sessions keep their history.', ); const key = account('or', { kind: 'key', provider: 'openai', endpoint: 'https://openrouter.ai/api/v1', }); assert.equal( accounts.signOutConsequence(key, [key]), 'Models from OpenRouter stop until you add another OpenRouter key.', ); });