OpenTalon / app · opentalon.ai / native / runtime / task-history.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 } = require('./store.cjs') |
| 7 | |
| 8 | function fixture(t) { |
| 9 | const root = process.env.OPENTALON_TEST_ROOT || '/Volumes/Expand/Projects/OpenTalon/native-app/tests' |
| 10 | fs.mkdirSync(root, { recursive: true }) |
| 11 | const folder = fs.mkdtempSync(path.join(root, 'history-')) |
| 12 | const store = new Store(folder) |
| 13 | t.after(() => { store.close(); fs.rmSync(folder, { recursive: true, force: true }) }) |
| 14 | const write = (name, input = {}) => store.dispatch(name, { ...input, requestKey: randomUUID() }) |
| 15 | return { store, folder, write } |
| 16 | } |
| 17 | |
| 18 | test('native completion dates survive edits and track reopen and re-complete without changing lane positions', async t => { |
| 19 | const { store, write } = fixture(t) |
| 20 | const { beginIfMissing } = require('./task-history.cjs') |
| 21 | t.mock.timers.enable({ apis: ['Date'], now: new Date('2026-10-20T12:00:00.000Z') }) |
| 22 | const boardId = store.boards()[0].id |
| 23 | const create = (title, status) => write('create_task', { boardId, title, status }).then(result => result.task) |
| 24 | const move = (task, status) => write('move_task', { id: task.id, status, expectedRevision: store.task(task.id).revision }) |
| 25 | const snapshot = () => store.snapshot({ includeRecordedCompletions: true }).tasks |
| 26 | const completion = task => snapshot().find(item => item.id === task.id).recordedCompletionAt |
| 27 | const activeOrder = () => snapshot().filter(item => item.status === 'To do').map(item => [item.id, item.position]) |
| 28 | |
| 29 | const legacy = await create('Migrated Done', 'Done') |
| 30 | store.db.prepare('DELETE FROM task_history WHERE task_id=?').run(legacy.id) |
| 31 | beginIfMissing(store, legacy) |
| 32 | assert.ok(store.task(legacy.id).completedAt) |
| 33 | assert.equal(completion(legacy), null) |
| 34 | |
| 35 | const older = await create('Older') |
| 36 | const recent = await create('Recent') |
| 37 | const firstActive = await create('First active') |
| 38 | const secondActive = await create('Second active') |
| 39 | // Older backups can have an active task with no journal entries at all. |
| 40 | store.db.prepare('DELETE FROM task_history WHERE task_id=?').run(older.id) |
| 41 | t.mock.timers.tick(1000) |
| 42 | await move(older, 'Done') |
| 43 | const originalCompletion = completion(older) |
| 44 | assert.equal(originalCompletion, '2026-10-20T12:00:01.000Z') |
| 45 | t.mock.timers.tick(1000) |
| 46 | await move(recent, 'Done') |
| 47 | assert.ok(completion(recent) > originalCompletion) |
| 48 | |
| 49 | const originalActiveOrder = activeOrder() |
| 50 | assert.deepEqual(originalActiveOrder.map(([id]) => id), [firstActive.id, secondActive.id]) |
| 51 | t.mock.timers.tick(1000) |
| 52 | await write('update_task', { id: older.id, expectedRevision: store.task(older.id).revision, notes: 'Review added after completion' }) |
| 53 | assert.ok(store.task(older.id).updatedAt > completion(recent)) |
| 54 | assert.equal(completion(older), originalCompletion) |
| 55 | assert.deepEqual(activeOrder(), originalActiveOrder) |
| 56 | |
| 57 | await move(older, 'To do') |
| 58 | assert.equal(completion(older), null) |
| 59 | t.mock.timers.tick(1000) |
| 60 | await move(older, 'Done') |
| 61 | assert.ok(completion(older) > completion(recent)) |
| 62 | assert.deepEqual(activeOrder().map(([id]) => id), originalActiveOrder.map(([id]) => id)) |
| 63 | }) |
| 64 | |
| 65 | test('Issue/Bug defaults, field edits, filters and history survive restarts and backups', async t => { |
| 66 | const { store, folder, write } = fixture(t) |
| 67 | const boardId = store.boards()[0].id |
| 68 | const { task } = await write('create_task', { boardId, title: 'Original issue', notes: 'Original notes', planMarkdown: '# Original plan' }) |
| 69 | assert.equal(task.issueType, 'Issue') |
| 70 | const first = (await store.dispatch('get_task_history', { id: task.id })).entries[0] |
| 71 | assert.equal(first.state.notes, 'Original notes') |
| 72 | await write('companion_set_task_field', { id: task.id, field: 'issueType', expectedValue: 'Issue', value: 'Bug' }) |
| 73 | assert.equal(store.task(task.id).issueType, 'Bug') |
| 74 | assert.equal((await store.dispatch('list_tasks', { issueType: 'Bug' })).tasks.length, 1) |
| 75 | assert.equal((await store.dispatch('list_tasks', { issueType: 'Issue' })).tasks.length, 0) |
| 76 | assert.equal(store.agentJobs().length, 0) |
| 77 | await assert.rejects(write('create_task', { boardId, title: 'Invalid', issueType: 'Feature' }), { code: 'INVALID_ARGUMENT' }) |
| 78 | await write('update_task', { id: task.id, expectedRevision: store.task(task.id).revision, title: 'Revised', notes: 'New notes', priority: 'High', dueDate: '2026-12-03' }) |
| 79 | await write('set_task_plan', { id: task.id, expectedMarkdown: '# Original plan', markdown: '# New plan' }) |
| 80 | const before = await store.dispatch('get_task_history', { id: task.id }) |
| 81 | assert.equal(before.entries.length, 4) |
| 82 | assert.ok(before.entries[0].changedFields.includes('planMarkdown')) |
| 83 | for (const format of ['json', 'sqlite']) { |
| 84 | const backup = await write('create_backup', { format }) |
| 85 | const preview = await store.dispatch('preview_destructive_action', { action: 'restore_backup', path: backup.path }) |
| 86 | await write('confirm_destructive_action', { token: preview.token }) |
| 87 | assert.equal(store.task(task.id).issueType, 'Bug') |
| 88 | assert.equal(store.task(task.id).planMarkdown, '# New plan') |
| 89 | assert.ok((await store.dispatch('get_task_history', { id: task.id })).entries.some(entry => entry.id === first.id)) |
| 90 | } |
| 91 | const input = { id: task.id, historyID: first.id, expectedRevision: store.task(task.id).revision, requestKey: randomUUID() } |
| 92 | const restored = await store.dispatch('restore_task_revision', input) |
| 93 | assert.deepEqual(await store.dispatch('restore_task_revision', input), restored) |
| 94 | assert.equal(restored.task.title, 'Original issue') |
| 95 | assert.equal(restored.task.notes, 'Original notes') |
| 96 | assert.equal(restored.task.planMarkdown, '# Original plan') |
| 97 | assert.equal(restored.task.issueType, 'Issue') |
| 98 | assert.equal(restored.task.dueDate, null) |
| 99 | const history = await store.dispatch('get_task_history', { id: task.id }) |
| 100 | assert.equal(history.entries[0].action, 'restore_task_revision') |
| 101 | assert.ok(history.entries.some(entry => entry.state.planMarkdown === '# New plan')) |
| 102 | const other = new Store(folder) |
| 103 | assert.deepEqual(await other.dispatch('get_task_history', { id: task.id }), history) |
| 104 | other.close() |
| 105 | }) |
| 106 | |
| 107 | test('history pagination is stable, excludes unchanged saves and rejects stale or foreign restores', async t => { |
| 108 | const { store, write } = fixture(t) |
| 109 | const boardId = store.boards()[0].id |
| 110 | const { task } = await write('create_task', { boardId, title: 'Revision 1' }) |
| 111 | for (let index = 2; index <= 26; index++) await write('update_task', { id: task.id, expectedRevision: store.task(task.id).revision, title: `Revision ${index}` }) |
| 112 | const first = await store.dispatch('get_task_history', { id: task.id, limit: 10 }) |
| 113 | assert.deepEqual(first.entries.map(entry => entry.version), [26,25,24,23,22,21,20,19,18,17]) |
| 114 | const next = await store.dispatch('get_task_history', { id: task.id, limit: 20, beforeVersion: first.nextBefore }) |
| 115 | assert.equal(next.entries.length, 16) |
| 116 | assert.equal(next.entries.at(-1).state.title, 'Revision 1') |
| 117 | assert.equal(next.nextBefore, null) |
| 118 | await write('update_task', { id: task.id, expectedRevision: store.task(task.id).revision, title: 'Revision 26' }) |
| 119 | assert.equal((await store.dispatch('get_task_history', { id: task.id })).entries.length, 26) |
| 120 | await assert.rejects(write('restore_task_revision', { id: task.id, historyID: next.entries[0].id, expectedRevision: task.revision }), { code: 'CONFLICT' }) |
| 121 | const { task: other } = await write('create_task', { boardId, title: 'Other task' }) |
| 122 | await assert.rejects(write('restore_task_revision', { id: other.id, historyID: first.entries[0].id, expectedRevision: other.revision }), { code: 'NOT_FOUND' }) |
| 123 | assert.equal(store.task(task.id).title, 'Revision 26') |
| 124 | }) |
| 125 | |
| 126 | test('restoring an agent revision stops dispatch, retains comments, reopens Archive and respects project task keys', async t => { |
| 127 | const { store, write } = fixture(t) |
| 128 | const boardId = store.boards()[0].id |
| 129 | const { task } = await write('create_task', { boardId, title: 'Recover me', issueType: 'Bug' }) |
| 130 | const original = (await store.dispatch('get_task_history', { id: task.id })).entries[0] |
| 131 | const { board } = await write('create_board', { name: 'Destination' }) |
| 132 | await write('move_task', { id: task.id, boardId: board.id, expectedRevision: task.revision, status: 'In progress' }) |
| 133 | await write('add_user_comment', { taskId: task.id, text: 'Keep this feedback' }) |
| 134 | await write('companion_organize_task', { id: task.id, destination: 'archive' }) |
| 135 | await write('restore_task_revision', { id: task.id, historyID: original.id, expectedRevision: store.task(task.id).revision }) |
| 136 | assert.equal(store.task(task.id).boardId, boardId) |
| 137 | assert.equal(store.task(task.id).status, 'To do') |
| 138 | assert.equal(store.task(task.id).archivedAt, null) |
| 139 | assert.match(store.task(task.id).taskKey, /^[A-Z]{3}-\d+$/) |
| 140 | assert.equal((await store.dispatch('list_task_comments', { taskId: task.id })).comments[0].text, 'Keep this feedback') |
| 141 | // Simulate a persisted queued dispatch without launching an external agent. |
| 142 | const history = require('./task-history.cjs') |
| 143 | store.db.prepare("UPDATE tasks SET status='Sent to agent' WHERE id=?").run(task.id) |
| 144 | history.record(store, store.task(task.id), 'move_task') |
| 145 | store.saveAgentJob({ id: 'history-test-run', taskId: task.id, state: 'queued', agent: 'codex' }) |
| 146 | store.assignAgent(task.id, 'history-test-run') |
| 147 | const sent = (await store.dispatch('get_task_history', { id: task.id })).entries[0] |
| 148 | await write('restore_task_revision', { id: task.id, historyID: sent.id, expectedRevision: store.task(task.id).revision }) |
| 149 | assert.equal(store.task(task.id).status, 'To do') |
| 150 | assert.equal(store.agentJobs()[0].state, 'canceled') |
| 151 | assert.equal(store.assignedRun(task.id), null) |
| 152 | await write('trash_task', { id: task.id, expectedRevision: store.task(task.id).revision }) |
| 153 | await assert.rejects(write('restore_task_revision', { id: task.id, historyID: original.id, expectedRevision: store.task(task.id).revision }), { code: 'IN_TRASH' }) |
| 154 | }) |
| 155 | |
| 156 | test('corrupt revision history cannot replace a healthy workspace', async t => { |
| 157 | const { store, write } = fixture(t) |
| 158 | const { task } = await write('create_task', { boardId: store.boards()[0].id, title: 'Keep my current data' }) |
| 159 | const backup = await write('create_backup', { format: 'json' }) |
| 160 | const payload = JSON.parse(fs.readFileSync(backup.path)) |
| 161 | payload.taskHistory[0].state_json = JSON.stringify({ ...JSON.parse(payload.taskHistory[0].state_json), issueType: 'Invalid' }) |
| 162 | fs.writeFileSync(backup.path, JSON.stringify(payload)) |
| 163 | await assert.rejects(store.dispatch('inspect_backup', { path: backup.path }), { code: 'INVALID_BACKUP' }) |
| 164 | assert.equal(store.task(task.id).title, 'Keep my current data') |
| 165 | }) |