OpenTalon / app / native / runtime / task-activity.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 | const { Connector } = require('./mcp.cjs') |
| 8 | const { forward } = require('./mcp-bridge.cjs') |
| 9 | function fixture(t) { |
| 10 | const root = process.env.OPENTALON_TEST_ROOT || '/Volumes/Expand/Projects/OpenTalon/native-app/tests' |
| 11 | fs.mkdirSync(root, { recursive: true }) |
| 12 | const folder = fs.mkdtempSync(path.join(root, 'activity-')) |
| 13 | let store = new Store(folder) |
| 14 | const cleanup = [] |
| 15 | t.after(async () => { |
| 16 | for (const close of cleanup) await close() |
| 17 | store.close() |
| 18 | fs.rmSync(folder, { recursive: true, force: true }) |
| 19 | }) |
| 20 | const write = (name, input = {}) => |
| 21 | store.dispatch(name, { requestKey: randomUUID(), ...input }, 'You · test') |
| 22 | return { |
| 23 | folder, |
| 24 | cleanup, |
| 25 | get store() { |
| 26 | return store |
| 27 | }, |
| 28 | write, |
| 29 | reopen() { |
| 30 | store.close() |
| 31 | store = new Store(folder) |
| 32 | } |
| 33 | } |
| 34 | } |
| 35 | const png = { |
| 36 | name: 'proof.png', |
| 37 | mimeType: 'image/png', |
| 38 | data: fs |
| 39 | .readFileSync(path.join(__dirname, '../assets/opentalon-logo-1024.png')) |
| 40 | .toString('base64') |
| 41 | } |
| 42 | const activity = (f, id, extra = {}) => |
| 43 | f.store.dispatch('list_task_activity', { taskId: id, limit: 50, ...extra }) |
| 44 | async function connect(f) { |
| 45 | const connector = new Connector({ |
| 46 | folder: f.folder, |
| 47 | call: (name, args) => f.store.dispatch(name, args) |
| 48 | }) |
| 49 | await connector.enable(true) |
| 50 | f.cleanup.push(() => connector.stop()) |
| 51 | return async (name, args) => (await forward({ |
| 52 | jsonrpc: '2.0', id: 1, method: 'tools/call', params: { name, arguments: args } |
| 53 | }, connector.file)).result |
| 54 | } |
| 55 | test('public MCP reports and legacy comment calls write Activity; only the private UI writes Comments', async (t) => { |
| 56 | const f = fixture(t) |
| 57 | const { task } = await f.write('create_task', { |
| 58 | boardId: f.store.boards()[0].id, |
| 59 | title: 'Verify Activity' |
| 60 | }) |
| 61 | const rpc = await connect(f) |
| 62 | const runId = randomUUID() |
| 63 | f.store.assignAgent(task.id, runId) |
| 64 | const cases = [ |
| 65 | ['add_task_comment', 'task key', task.taskKey], |
| 66 | ['add_task_comment', 'UUID', task.id], |
| 67 | ['add_task_activity', 'task key', task.taskKey.toLowerCase()], |
| 68 | ['add_task_activity', 'UUID', task.id] |
| 69 | ] |
| 70 | for (const [name, form, taskId] of cases) { |
| 71 | await t.test(`${name} with ${form}`, async () => { |
| 72 | const input = { taskId, text: 'Agent verification passed', requestKey: randomUUID() } |
| 73 | if (name === 'add_task_comment') input.notifyAgent = true |
| 74 | const result = await rpc(name, input) |
| 75 | assert.equal(result.isError, undefined, JSON.stringify(result.structuredContent)) |
| 76 | assert.equal(result.structuredContent.activity.taskId, task.id) |
| 77 | assert.equal(result.structuredContent.activity.runId, runId) |
| 78 | assert.deepEqual(await rpc(name, input), result) |
| 79 | if (name === 'add_task_comment') { |
| 80 | assert.equal(result.structuredContent.redirectedToActivity, true) |
| 81 | assert.deepEqual(result.structuredContent.comment, result.structuredContent.activity) |
| 82 | } |
| 83 | }) |
| 84 | } |
| 85 | const input = { |
| 86 | taskId: task.id, |
| 87 | text: 'Agent verification passed', |
| 88 | images: [png], |
| 89 | requestKey: randomUUID() |
| 90 | } |
| 91 | const result = await rpc('add_task_activity', input) |
| 92 | assert.notEqual(result.isError, true) |
| 93 | assert.deepEqual(await rpc('add_task_activity', input), result) |
| 94 | const hidden = await rpc('add_user_comment', { |
| 95 | taskId: task.id, |
| 96 | text: 'Should be rejected', |
| 97 | requestKey: randomUUID() |
| 98 | }) |
| 99 | assert.equal(hidden.isError, true) |
| 100 | assert.equal(f.store.comments(task.id).length, 0) |
| 101 | assert.equal(f.store.task(task.id).commentCount, 0) |
| 102 | assert.equal(f.store.agentJobs().length, 0) |
| 103 | const image = result.structuredContent.activity.images[0] |
| 104 | assert.equal((await f.store.dispatch('read_comment_image', { imageId: image.id })).data, png.data) |
| 105 | await f.write('add_user_comment', { |
| 106 | taskId: task.id, |
| 107 | text: 'Please check the Mac build too.', |
| 108 | notifyAgent: false |
| 109 | }) |
| 110 | assert.deepEqual( |
| 111 | f.store.comments(task.id).map((c) => c.text), |
| 112 | ['Please check the Mac build too.'] |
| 113 | ) |
| 114 | const entries = (await activity(f, task.id)).entries |
| 115 | assert.equal(entries.filter((e) => e.kind === 'agent').length, 5) |
| 116 | assert.equal(entries[0].summary, 'Saved a revision request') |
| 117 | assert.equal(entries[0].detail, '') |
| 118 | assert.equal(entries[0].actor, 'You · test') |
| 119 | await assert.rejects( |
| 120 | f.store.dispatch('move_task', { |
| 121 | id: task.id, |
| 122 | expectedRevision: f.store.task(task.id).revision, |
| 123 | status: 'Sent to agent', |
| 124 | dispatchComment: 'Pretend user comment', |
| 125 | requestKey: randomUUID() |
| 126 | }), |
| 127 | { code: 'INVALID_ARGUMENT' } |
| 128 | ) |
| 129 | const trashed = await rpc('trash_task', { id: task.id, expectedRevision: task.revision, requestKey: randomUUID() }) |
| 130 | assert.equal(trashed.structuredContent.task.deleted, true) |
| 131 | }) |
| 132 | test('MCP identifies a foreign-key failure and a retry with the same requestKey saves exactly once', async (t) => { |
| 133 | const f = fixture(t) |
| 134 | const { task } = await f.write('create_task', { boardId: f.store.boards()[0].id, title: 'Verify rollback' }) |
| 135 | const rpc = await connect(f) |
| 136 | const input = { taskId: task.taskKey, text: 'Recovered Activity write', requestKey: randomUUID() } |
| 137 | const before = (await rpc('list_task_activity', { taskId: task.taskKey })).structuredContent |
| 138 | // Fail after the report, history and revision have been written, but before commit. |
| 139 | f.store.db.exec(` |
| 140 | CREATE TABLE invalid_receipt (task_id TEXT REFERENCES tasks(id)); |
| 141 | CREATE TEMP TRIGGER reject_receipt BEFORE INSERT ON operation_receipts BEGIN |
| 142 | INSERT INTO invalid_receipt VALUES ('missing-task'); |
| 143 | END; |
| 144 | `) |
| 145 | const failed = await rpc('add_task_activity', input) |
| 146 | assert.equal(failed.isError, true) |
| 147 | assert.equal(failed.structuredContent.code, 'ERR_SQLITE_ERROR') |
| 148 | assert.match(failed.structuredContent.message, /SQLITE_CONSTRAINT_FOREIGNKEY \(787\).*same requestKey/) |
| 149 | assert.deepEqual(failed.structuredContent.details, { |
| 150 | sqliteCode: 'SQLITE_CONSTRAINT_FOREIGNKEY', sqliteResultCode: 787, sameRequestKeySafe: true |
| 151 | }) |
| 152 | assert.deepEqual(JSON.parse(failed.content[0].text), failed.structuredContent) |
| 153 | assert.deepEqual((await rpc('list_task_activity', { taskId: task.taskKey })).structuredContent, before) |
| 154 | |
| 155 | f.store.db.exec('DROP TRIGGER reject_receipt') |
| 156 | const recovered = await rpc('add_task_activity', input) |
| 157 | assert.equal(recovered.structuredContent.revision, before.revision + 1) |
| 158 | assert.deepEqual(await rpc('add_task_activity', input), recovered) |
| 159 | const after = (await rpc('list_task_activity', { taskId: task.taskKey })).structuredContent |
| 160 | assert.equal(after.revision, before.revision + 1) |
| 161 | assert.equal(after.entries.filter(({ text }) => text === input.text).length, 1) |
| 162 | assert.equal((await rpc('trash_task', { |
| 163 | id: task.id, expectedRevision: task.revision, requestKey: randomUUID() |
| 164 | })).structuredContent.task.deleted, true) |
| 165 | }) |
| 166 | test('MCP reports an unavailable backup destination and the next Activity write succeeds', async (t) => { |
| 167 | const f = fixture(t) |
| 168 | const { task } = await f.write('create_task', { boardId: f.store.boards()[0].id, title: 'Verify backup isolation' }) |
| 169 | const rpc = await connect(f) |
| 170 | const [backup, report] = await Promise.all([ |
| 171 | rpc('create_backup', { destination: path.join(f.folder, 'missing-folder', 'snapshot.json'), |
| 172 | format: 'json', requestKey: randomUUID() }), |
| 173 | rpc('add_task_activity', { taskId: task.taskKey, text: 'Activity during backup', requestKey: randomUUID() }) |
| 174 | ]) |
| 175 | assert.equal(backup.isError, true) |
| 176 | assert.equal(backup.structuredContent.code, 'ENOENT') |
| 177 | assert.match(backup.structuredContent.message, /backup destination is unavailable/) |
| 178 | assert.equal(report.structuredContent.activity.taskId, task.id) |
| 179 | assert.equal((await rpc('trash_task', { |
| 180 | id: task.id, expectedRevision: task.revision, requestKey: randomUUID() |
| 181 | })).structuredContent.task.deleted, true) |
| 182 | }) |
| 183 | test('edits, moves, order, agent pickup and lifecycle changes persist in paginated activity and both backups', async (t) => { |
| 184 | const f = fixture(t), |
| 185 | { task } = await f.write('create_task', { boardId: f.store.boards()[0].id, title: 'Original' }) |
| 186 | for (let n = 0; n < 24; n++) |
| 187 | await f.write('update_task', { |
| 188 | id: task.id, |
| 189 | expectedRevision: f.store.task(task.id).revision, |
| 190 | title: 'Edit ' + n |
| 191 | }) |
| 192 | const first = await activity(f, task.id, { limit: 10 }), |
| 193 | older = await activity(f, task.id, { limit: 50, beforeSequence: first.nextBefore }) |
| 194 | assert.equal(first.entries.length, 10) |
| 195 | assert.equal(older.entries.length, 15) |
| 196 | assert.equal(older.nextBefore, null) |
| 197 | assert.equal(new Set([...first.entries, ...older.entries].map((e) => e.id)).size, 25) |
| 198 | const { board } = await f.write('create_board', { name: 'Destination' }) |
| 199 | await f.write('move_task', { |
| 200 | id: task.id, |
| 201 | boardId: board.id, |
| 202 | status: 'In progress', |
| 203 | expectedRevision: f.store.task(task.id).revision |
| 204 | }) |
| 205 | await f.write('rename_board', { |
| 206 | id: board.id, |
| 207 | expectedRevision: f.store.board(board.id).revision, |
| 208 | name: 'Renamed destination' |
| 209 | }) |
| 210 | assert.ok( |
| 211 | (await activity(f, task.id)).entries.some((entry) => entry.summary === 'Project renamed') |
| 212 | ) |
| 213 | await f.write('set_task_plan', { |
| 214 | id: task.id, |
| 215 | expectedMarkdown: '', |
| 216 | markdown: '# Check everything' |
| 217 | }) |
| 218 | f.store.saveAgentJob({ |
| 219 | id: 'activity-run', |
| 220 | taskId: task.id, |
| 221 | agent: 'codex', |
| 222 | state: 'queued', |
| 223 | workingFolder: f.folder, |
| 224 | threadId: null, |
| 225 | createdAt: new Date().toISOString(), |
| 226 | summary: 'Waiting for Mac' |
| 227 | }) |
| 228 | f.store.saveAgentJob({ |
| 229 | id: 'activity-run', |
| 230 | taskId: task.id, |
| 231 | agent: 'codex', |
| 232 | state: 'working', |
| 233 | workingFolder: f.folder, |
| 234 | threadId: null, |
| 235 | createdAt: new Date().toISOString(), |
| 236 | summary: 'Checking the build', |
| 237 | pickedUpAt: new Date().toISOString() |
| 238 | }) |
| 239 | await f.write('companion_organize_task', { id: task.id, destination: 'archive' }) |
| 240 | await f.write('companion_organize_task', { id: task.id, destination: 'active' }) |
| 241 | await f.write('trash_task', { id: task.id, expectedRevision: f.store.task(task.id).revision }) |
| 242 | await f.write('restore_task', { id: task.id, expectedRevision: f.store.task(task.id).revision }) |
| 243 | const entries = (await activity(f, task.id)).entries |
| 244 | for (const phrase of [ |
| 245 | 'Moved from', |
| 246 | 'Plan updated', |
| 247 | 'Codex picked up', |
| 248 | 'Archived task', |
| 249 | 'Restored task from Archive', |
| 250 | 'Moved task to Trash', |
| 251 | 'Restored task from Trash' |
| 252 | ]) |
| 253 | assert.ok( |
| 254 | entries.some((e) => e.summary.includes(phrase)), |
| 255 | phrase |
| 256 | ) |
| 257 | f.reopen() |
| 258 | assert.deepEqual((await activity(f, task.id)).entries, entries) |
| 259 | for (const format of ['json', 'sqlite']) { |
| 260 | const backup = await f.write('create_backup', { format }), |
| 261 | preview = await f.store.dispatch('preview_destructive_action', { |
| 262 | action: 'restore_backup', |
| 263 | path: backup.path |
| 264 | }) |
| 265 | await f.write('confirm_destructive_action', { token: preview.token }) |
| 266 | assert.ok((await activity(f, task.id)).entries.some((e) => e.id === entries[0].id)) |
| 267 | assert.equal((await activity(f, task.id)).entries[0].kind, 'restore') |
| 268 | } |
| 269 | }) |
| 270 | test('version ten upgrades preserve old reports, user feedback and screenshot IDs without inventing authorship', async (t) => { |
| 271 | const f = fixture(t), |
| 272 | { task } = await f.write('create_task', { |
| 273 | boardId: f.store.boards()[0].id, |
| 274 | title: 'Earlier task' |
| 275 | }) |
| 276 | await f.write('add_user_comment', { |
| 277 | taskId: task.id, |
| 278 | text: 'Earlier unknown message', |
| 279 | images: [png], |
| 280 | notifyAgent: true |
| 281 | }) |
| 282 | const comment = f.store.comments(task.id)[0], |
| 283 | imageID = comment.images[0].id |
| 284 | const original = { ...comment } |
| 285 | delete original.source |
| 286 | f.store.db |
| 287 | .prepare('UPDATE task_comments SET data=? WHERE id=?') |
| 288 | .run(JSON.stringify(original), comment.id) |
| 289 | const agentID = randomUUID(), |
| 290 | createdAt = new Date().toISOString() |
| 291 | f.store.db.prepare('INSERT INTO task_comments VALUES (?,?,?)').run( |
| 292 | agentID, |
| 293 | task.id, |
| 294 | JSON.stringify({ |
| 295 | id: agentID, |
| 296 | taskId: task.id, |
| 297 | text: 'Earlier agent report', |
| 298 | createdAt, |
| 299 | agent: 'codex', |
| 300 | runId: 'old-run' |
| 301 | }) |
| 302 | ) |
| 303 | f.store.db.exec( |
| 304 | 'DROP TABLE activity_images; DROP TABLE task_activity; DROP TABLE task_files; DELETE FROM schema_migrations WHERE version>=11; PRAGMA user_version=10' |
| 305 | ) |
| 306 | f.reopen() |
| 307 | assert.equal(f.store.task(task.id).commentCount, 0) |
| 308 | assert.equal(f.store.comments(task.id).length, 1) |
| 309 | assert.equal(f.store.comments(task.id)[0].source, 'legacy') |
| 310 | assert.equal(f.store.comments(task.id)[0].delivery, 'local') |
| 311 | let entries = (await activity(f, task.id)).entries |
| 312 | assert.ok(entries.some((e) => e.actor === 'Codex' && e.detail === 'Earlier agent report')) |
| 313 | assert.ok(entries.some((e) => e.actor === 'Earlier message' && e.images[0]?.id === imageID)) |
| 314 | assert.equal((await f.store.dispatch('read_comment_image', { imageId: imageID })).data, png.data) |
| 315 | f.reopen() |
| 316 | assert.deepEqual((await activity(f, task.id)).entries, entries) |
| 317 | }) |
| 318 | test('invalid Activity backup is rejected before replacing the current workspace', async (t) => { |
| 319 | const f = fixture(t), |
| 320 | { task } = await f.write('create_task', { |
| 321 | boardId: f.store.boards()[0].id, |
| 322 | title: 'Keep current task' |
| 323 | }) |
| 324 | await f.store.dispatch('add_task_activity', { |
| 325 | taskId: task.id, |
| 326 | text: 'Saved report', |
| 327 | images: [png], |
| 328 | requestKey: randomUUID() |
| 329 | }) |
| 330 | const backup = await f.write('create_backup', { format: 'json' }), |
| 331 | payload = JSON.parse(fs.readFileSync(backup.path)) |
| 332 | assert.equal(payload.activityImages.length, 1) |
| 333 | const bad = structuredClone(payload) |
| 334 | bad.activity[0].task_id = 'missing' |
| 335 | fs.writeFileSync(backup.path, JSON.stringify(bad)) |
| 336 | await assert.rejects(f.store.dispatch('inspect_backup', { path: backup.path }), { |
| 337 | code: 'INVALID_BACKUP' |
| 338 | }) |
| 339 | assert.equal(f.store.task(task.id).title, 'Keep current task') |
| 340 | fs.writeFileSync(backup.path, JSON.stringify(payload)) |
| 341 | const preview = await f.store.dispatch('preview_destructive_action', { |
| 342 | action: 'restore_backup', |
| 343 | path: backup.path |
| 344 | }) |
| 345 | await f.write('confirm_destructive_action', { token: preview.token }) |
| 346 | const report = (await activity(f, task.id)).entries.find((e) => e.detail === 'Saved report') |
| 347 | assert.equal( |
| 348 | (await f.store.dispatch('read_comment_image', { imageId: report.images[0].id })).data, |
| 349 | png.data |
| 350 | ) |
| 351 | }) |