OpenTalon / app · opentalon.ai / native / runtime / agent-readiness.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 { assertAgentReady } = require('./agent-readiness.cjs') |
| 6 | const base = process.env.OPENTALON_TEST_ROOT || '/Volumes/Expand/Projects/OpenTalon/native-app/tests' |
| 7 | |
| 8 | test('dispatch preflight identifies missing folder, unavailable launcher and disabled MCP before queuing', () => { |
| 9 | const runtimes = { 'claude-code': { installed: true }, codex: { installed: true } } |
| 10 | const connection = { running: true } |
| 11 | const settings = { defaultAgent: 'claude-code', workingFolder: '' } |
| 12 | assert.throws(() => assertAgentReady(settings, runtimes, connection), { |
| 13 | code: 'AGENT_SETUP_REQUIRED', |
| 14 | }) |
| 15 | settings.workingFolder = '/nonexistent/opentalon-test-folder' |
| 16 | assert.throws(() => assertAgentReady(settings, runtimes, connection), { |
| 17 | code: 'AGENT_SETUP_REQUIRED', |
| 18 | }) |
| 19 | settings.workingFolder = path.dirname(__filename) |
| 20 | assert.doesNotThrow(() => assertAgentReady(settings, runtimes, connection)) |
| 21 | settings.defaultAgent = 'codex' |
| 22 | assert.doesNotThrow(() => assertAgentReady(settings, runtimes, connection)) |
| 23 | assert.throws(() => assertAgentReady(settings, { codex: { installed: false } }, connection), { |
| 24 | code: 'AGENT_NOT_INSTALLED', |
| 25 | }) |
| 26 | assert.throws(() => assertAgentReady(settings, runtimes, { running: false }), { |
| 27 | code: 'MCP_DISABLED', |
| 28 | }) |
| 29 | assert.equal(fs.existsSync(settings.workingFolder), true) |
| 30 | }) |
| 31 | |
| 32 | test('failed dispatch leaves task and receipts unchanged; successful retry is durable after readiness changes', async (t) => { |
| 33 | const { Store } = require('./store.cjs') |
| 34 | const { randomUUID } = require('node:crypto') |
| 35 | fs.mkdirSync(base, { recursive: true }) |
| 36 | const folder = fs.mkdtempSync(path.join(base, 'readiness-')) |
| 37 | let running = false |
| 38 | const store = new Store(folder, { |
| 39 | agentReadiness: (settings) => |
| 40 | assertAgentReady(settings, { codex: { installed: true } }, { running }), |
| 41 | }) |
| 42 | t.after(() => { |
| 43 | store.close() |
| 44 | fs.rmSync(folder, { recursive: true, force: true }) |
| 45 | }) |
| 46 | const write = (name, args) => store.dispatch(name, { requestKey: randomUUID(), ...args }) |
| 47 | await write('set_agent_settings', { defaultAgent: 'codex', workingFolder: folder }) |
| 48 | const task = ( |
| 49 | await write('create_task', { boardId: store.boards()[0].id, title: 'Only launch after setup' }) |
| 50 | ).task |
| 51 | const input = { |
| 52 | id: task.id, |
| 53 | status: 'Sent to agent', |
| 54 | expectedRevision: task.revision, |
| 55 | requestKey: randomUUID(), |
| 56 | } |
| 57 | const revision = store.revision() |
| 58 | await assert.rejects(store.dispatch('move_task', input), { code: 'MCP_DISABLED' }) |
| 59 | assert.equal(store.revision(), revision) |
| 60 | assert.equal(store.task(task.id).status, 'To do') |
| 61 | assert.equal(store.agentJobs().length, 0) |
| 62 | running = true |
| 63 | const sent = await store.dispatch('move_task', input) |
| 64 | assert.equal(store.agentJobs().length, 1) |
| 65 | running = false |
| 66 | assert.deepEqual(await store.dispatch('move_task', input), sent) |
| 67 | assert.equal(store.agentJobs().length, 1) |
| 68 | await assert.rejects( |
| 69 | write('create_task', { |
| 70 | boardId: task.boardId, |
| 71 | title: 'Not created on failed setup', |
| 72 | status: 'Sent to agent', |
| 73 | }), |
| 74 | { code: 'MCP_DISABLED' } |
| 75 | ) |
| 76 | assert.equal(store.list({}).tasks.length, 1) |
| 77 | }) |