Branchwell

OpenTalon / app · opentalon.ai / native / runtime / task-organization.test.cjs

Raw 2.9 KB on d82caf36fa99426fea138f030e7704d2b3cf17b3
1const test = require('node:test')
2const assert = require('node:assert/strict')
3const fs = require('node:fs')
4const os = require('node:os')
5const path = require('node:path')
6const { randomUUID } = require('node:crypto')
7const { Store } = require('./store.cjs')
8
9test('companion Archive and Trash preserve task data, cancel dispatch and restore without duplicates', async (t) => {
10 const directory = fs.mkdtempSync(path.join(os.tmpdir(), 'opentalon-organization-'))
11 const store = new Store(directory, { agentReadiness: () => 'codex' })
12 t.after(() => { store.close(); fs.rmSync(directory, { recursive: true, force: true }) })
13 const call = (name, input) => store.dispatch(name, { requestKey: randomUUID(), ...input })
14 const boardId = store.snapshot().boards[0].id
15 store.set('agentSettings', { defaultAgent: 'codex', workingFolder: directory })
16 for (const status of ['To do', 'In progress', 'Done', 'Sent to agent']) {
17 let { task } = await call('create_task', { boardId, title: `Keep ${status}`, notes: 'Preserve these notes', status })
18 await call('add_user_comment', { taskId: task.id, text: 'Keep this comment' })
19 // A queued edit before Archive must not invalidate the user's later organization action.
20 task = store.task(task.id)
21 await call('update_task', { id: task.id, expectedRevision: task.revision, priority: 'High' })
22 const archive = { id: task.id, destination: 'archive', requestKey: randomUUID() }
23 const first = await store.dispatch('companion_organize_task', archive)
24 const replay = await store.dispatch('companion_organize_task', archive)
25 assert.deepEqual(replay, first)
26 task = store.task(task.id)
27 assert.ok(task.archivedAt)
28 assert.equal(task.deleted, false)
29 assert.equal(task.status, status)
30 assert.equal(task.notes, 'Preserve these notes')
31 assert.equal(task.priority, 'High')
32 assert.equal(store.comments(task.id).length, 1)
33 for (const format of ['json', 'sqlite']) {
34 const backup = await call('create_backup', { format })
35 const restored = await store.inspect(backup.path)
36 assert.ok(restored.tasks.find((item) => item.id === task.id).archived_at)
37 }
38 assert.ok(!store.assignedRun(task.id))
39 assert.ok(!store.agentJobs().some((job) => job.taskId === task.id && job.state === 'queued'))
40 await call('companion_organize_task', { id: task.id, destination: 'active' })
41 task = store.task(task.id)
42 assert.equal(task.archivedAt, null)
43 assert.equal(task.status, 'To do')
44 await call('companion_organize_task', { id: task.id, destination: 'trash' })
45 task = store.task(task.id)
46 assert.equal(task.deleted, true)
47 await call('restore_task', { id: task.id, expectedRevision: task.revision })
48 assert.equal(store.task(task.id).deleted, false)
49 assert.equal(store.comments(task.id).length, 1)
50 }
51 const live = store.snapshot().tasks.filter((task) => !task.deleted && !task.archivedAt)
52 assert.equal(new Set(live.map((task) => task.position)).size, live.length)
53})