OpenTalon / app · opentalon.ai / native / runtime / agent-models.test.cjs
| 1 | const test = require('node:test') |
| 2 | const assert = require('node:assert/strict') |
| 3 | const fs = require('node:fs') |
| 4 | const path = require('node:path') |
| 5 | const { randomUUID } = require('node:crypto') |
| 6 | const { Store, FORMAT } = require('./store.cjs') |
| 7 | const { agentModels, mergeAgentModels, validateModelOverrides } = require('./agent-models.cjs') |
| 8 | const { tools } = require('./contracts.cjs') |
| 9 | const base = process.env.OPENTALON_TEST_ROOT || '/Volumes/Expand/Projects/OpenTalon/native-app/tests' |
| 10 | fs.mkdirSync(base, { recursive: true }) |
| 11 | const write = (store, name, args = {}) => store.dispatch(name, { ...args, requestKey: randomUUID() }) |
| 12 | |
| 13 | function fixture(t) { |
| 14 | const folder = fs.mkdtempSync(path.join(base, 'models-')) |
| 15 | const store = new Store(folder) |
| 16 | t.after(() => { store.close(); fs.rmSync(folder, { recursive: true, force: true }) }) |
| 17 | return { store, folder } |
| 18 | } |
| 19 | |
| 20 | test('current shipped models include verified current provider IDs and omit superseded Sol/Astra', () => { |
| 21 | assert.ok(agentModels.some(x => x.agent === 'codex' && x.id === 'gpt-6.2-sol')) |
| 22 | assert.ok(agentModels.some(x => x.agent === 'codex' && x.id === 'gpt-6.2-astra')) |
| 23 | assert.ok(agentModels.some(x => x.agent === 'claude-code' && x.id === 'claude-fable-5-2')) |
| 24 | assert.ok(agentModels.some(x => x.agent === 'claude-code' && x.id === 'claude-haiku-5-5')) |
| 25 | assert.ok(!agentModels.some(x => ['gpt-6-astra', 'gpt-6.1-sol'].includes(x.id))) |
| 26 | assert.ok(tools.some(x => x.name === 'get_agent_models' && !x.mutates)) |
| 27 | assert.ok(tools.some(x => x.name === 'set_agent_models' && x.mutates)) |
| 28 | }) |
| 29 | |
| 30 | test('a later app default catalog adds new models while preserving custom labels, hidden choices and custom models', () => { |
| 31 | const old = [ |
| 32 | { agent: 'codex', id: 'old-default', title: 'Old default' }, |
| 33 | { agent: 'codex', id: 'kept', title: 'Original name' }, |
| 34 | { agent: 'claude-code', id: 'hidden', title: 'Hidden choice' }, |
| 35 | ] |
| 36 | const overrides = validateModelOverrides([ |
| 37 | { agent: 'codex', id: 'kept', title: 'My name' }, |
| 38 | { agent: 'claude-code', id: 'hidden', title: 'Hidden choice', hidden: true }, |
| 39 | { agent: 'codex', id: 'custom-next', title: 'My custom model' }, |
| 40 | ]) |
| 41 | const next = [old[1], old[2], { agent: 'codex', id: 'new-default', title: 'New default' }] |
| 42 | assert.deepEqual(mergeAgentModels(next, overrides), [ |
| 43 | { agent: 'codex', id: 'kept', title: 'My name' }, |
| 44 | { agent: 'codex', id: 'new-default', title: 'New default' }, |
| 45 | { agent: 'codex', id: 'custom-next', title: 'My custom model' }, |
| 46 | ]) |
| 47 | assert.equal(old[1].title, 'Original name', 'Merging must not mutate shipped defaults') |
| 48 | }) |
| 49 | |
| 50 | test('catalog edits are independently versioned, replay-safe and never dispatch or rewrite tasks', async t => { |
| 51 | const { store } = fixture(t) |
| 52 | const first = await store.dispatch('get_agent_models') |
| 53 | const task = (await write(store, 'create_task', { boardId: store.boards()[0].id, title: 'Keep this unchanged' })).task |
| 54 | const overrides = [{ agent: 'codex', id: 'provider-new-model', title: ' My model ' }] |
| 55 | const args = { overrides, expectedCatalogRevision: first.catalogRevision, requestKey: randomUUID() } |
| 56 | const saved = await store.dispatch('set_agent_models', args) |
| 57 | assert.equal(saved.catalogRevision, first.catalogRevision + 1) |
| 58 | assert.equal(saved.models.at(-1).title, 'My model') |
| 59 | assert.deepEqual(await store.dispatch('set_agent_models', args), saved) |
| 60 | await assert.rejects(write(store, 'set_agent_models', { overrides: [], expectedCatalogRevision: first.catalogRevision }), { code: 'CONFLICT' }) |
| 61 | assert.equal(store.task(task.id).revision, task.revision) |
| 62 | assert.equal(store.task(task.id).status, 'To do') |
| 63 | assert.deepEqual(store.agentJobs(), []) |
| 64 | const reset = await write(store, 'set_agent_models', { overrides: [], expectedCatalogRevision: saved.catalogRevision }) |
| 65 | assert.deepEqual(reset.models, agentModels) |
| 66 | }) |
| 67 | |
| 68 | test('invalid catalog writes are atomic and cannot smuggle flags or duplicate models', async t => { |
| 69 | const { store } = fixture(t) |
| 70 | const before = await store.dispatch('get_agent_models') |
| 71 | for (const overrides of [ |
| 72 | [{ agent: 'codex', id: '--config', title: 'Bad flag' }], |
| 73 | [{ agent: 'codex', id: 'good', title: ' ' }], |
| 74 | [{ agent: 'codex', id: 'good', title: 'Bad\nname' }], |
| 75 | [{ agent: 'another-provider', id: 'good', title: 'Bad provider' }], |
| 76 | [{ agent: 'codex', id: 'good', title: 'One' }, { agent: 'codex', id: 'good', title: 'Two' }], |
| 77 | ]) { |
| 78 | await assert.rejects(write(store, 'set_agent_models', { overrides, expectedCatalogRevision: before.catalogRevision }), { code: 'INVALID_ARGUMENT' }) |
| 79 | } |
| 80 | assert.deepEqual(await store.dispatch('get_agent_models'), before) |
| 81 | }) |
| 82 | |
| 83 | test('custom models are validated for the chosen provider and stored unchanged on the dispatched run', async t => { |
| 84 | const { store, folder } = fixture(t) |
| 85 | await write(store, 'set_agent_settings', { defaultAgent: 'codex', workingFolder: folder }) |
| 86 | const current = await store.dispatch('get_agent_models') |
| 87 | const overrides = [{ agent: 'codex', id: 'my-custom-model', title: 'Custom' }] |
| 88 | await write(store, 'set_agent_models', { overrides, expectedCatalogRevision: current.catalogRevision }) |
| 89 | const task = (await write(store, 'create_task', { boardId: store.boards()[0].id, title: 'Use my model', status: 'Sent to agent', agent: 'codex', model: 'my-custom-model' })).task |
| 90 | const run = store.agentJobs()[0] |
| 91 | assert.equal(run.model, 'my-custom-model') |
| 92 | const catalog = await store.dispatch('get_agent_models') |
| 93 | await write(store, 'set_agent_models', { overrides: [], expectedCatalogRevision: catalog.catalogRevision }) |
| 94 | assert.equal(store.agentJobs()[0].model, run.model) |
| 95 | assert.equal(store.task(task.id).status, 'Sent to agent') |
| 96 | await assert.rejects(write(store, 'create_task', { boardId: store.boards()[0].id, title: 'Wrong provider', status: 'Sent to agent', agent: 'claude-code', model: 'my-custom-model' }), { code: 'INVALID_ARGUMENT' }) |
| 97 | }) |
| 98 | |
| 99 | test('model overrides survive restart and both backup formats; old backups adopt the current defaults', async t => { |
| 100 | const { store, folder } = fixture(t) |
| 101 | const overrides = [ |
| 102 | { agent: 'codex', id: 'gpt-6.2-sol', title: 'My Sol' }, |
| 103 | { agent: 'claude-code', id: 'claude-haiku-5-5', title: 'Haiku', hidden: true }, |
| 104 | { agent: 'codex', id: 'next-custom', title: 'Next custom' }, |
| 105 | ] |
| 106 | await write(store, 'set_agent_models', { overrides, expectedCatalogRevision: 1 }) |
| 107 | const expected = (await store.dispatch('get_agent_models')).models |
| 108 | await write(store, 'set_agent_settings', { defaultAgent: 'codex', workingFolder: folder }) |
| 109 | await write(store, 'create_task', { boardId: store.boards()[0].id, title: 'History keeps my selected model', |
| 110 | status: 'Sent to agent', agent: 'codex', model: 'next-custom' }) |
| 111 | const reopened = new Store(folder) |
| 112 | assert.deepEqual(reopened.agentModelCatalog().models, expected) |
| 113 | reopened.close() |
| 114 | for (const format of ['json', 'sqlite']) { |
| 115 | const backup = await write(store, 'create_backup', { format }) |
| 116 | const target = new Store(path.join(folder, 'restore-' + format)) |
| 117 | try { |
| 118 | const preview = await target.dispatch('preview_destructive_action', { action: 'restore_backup', path: backup.path }) |
| 119 | await write(target, 'confirm_destructive_action', { token: preview.token }) |
| 120 | assert.deepEqual(target.agentModelCatalog().models, expected) |
| 121 | assert.equal(target.agentJobs()[0].model, 'next-custom') |
| 122 | } finally { target.close() } |
| 123 | } |
| 124 | const exported = await write(store, 'create_backup', { format: 'json' }) |
| 125 | const payload = JSON.parse(fs.readFileSync(exported.path)) |
| 126 | payload.formatVersion = 12 |
| 127 | delete payload.agentModelOverrides |
| 128 | const old = path.join(folder, 'old-backup.json') |
| 129 | fs.writeFileSync(old, JSON.stringify(payload)) |
| 130 | const preview = await store.dispatch('preview_destructive_action', { action: 'restore_backup', path: old }) |
| 131 | await write(store, 'confirm_destructive_action', { token: preview.token }) |
| 132 | assert.deepEqual(store.agentModelCatalog().models, agentModels) |
| 133 | assert.equal(store.db.prepare('PRAGMA user_version').get().user_version, FORMAT) |
| 134 | }) |