OpenTalon / app / native / runtime / task-plans.test.cjs
| 1 | const test = require('node:test') |
| 2 | const assert = require('node:assert/strict') |
| 3 | const fs = require('node:fs') |
| 4 | const os = require('node:os') |
| 5 | const path = require('node:path') |
| 6 | const { randomUUID } = require('node:crypto') |
| 7 | const { Store, FORMAT } = require('./store.cjs') |
| 8 | |
| 9 | test('a version-seven workspace upgrades without changing existing tasks', async (t) => { |
| 10 | const folder = fs.mkdtempSync(path.join(os.tmpdir(), 'opentalon-plan-migration-')) |
| 11 | let store = new Store(folder) |
| 12 | t.after(() => { store.close(); fs.rmSync(folder, { recursive: true, force: true }) }) |
| 13 | const { task } = await store.dispatch('create_task', { |
| 14 | requestKey: randomUUID(), boardId: store.boards()[0].id, title: 'Existing task', notes: 'Keep these notes', priority: 'High', |
| 15 | }) |
| 16 | store.db.exec('DROP TABLE activity_images; DROP TABLE task_activity; DROP TABLE task_history; DROP TABLE task_files; ALTER TABLE tasks DROP COLUMN issue_type; ALTER TABLE tasks DROP COLUMN plan_markdown; DELETE FROM schema_migrations WHERE version>=8; PRAGMA user_version=7;') |
| 17 | store.close() |
| 18 | store = new Store(folder) |
| 19 | assert.equal(store.task(task.id).planMarkdown, '') |
| 20 | assert.equal(store.task(task.id).notes, 'Keep these notes') |
| 21 | assert.equal(store.task(task.id).taskKey, task.taskKey) |
| 22 | assert.equal(store.task(task.id).priority, 'High') |
| 23 | assert.equal(store.db.prepare('PRAGMA user_version').get().user_version, FORMAT) |
| 24 | }) |
| 25 | |
| 26 | test('Markdown plans persist, detect document conflicts, follow project moves and survive both backup formats', async (t) => { |
| 27 | const folder = fs.mkdtempSync(path.join(os.tmpdir(), 'opentalon-plans-')) |
| 28 | const store = new Store(folder) |
| 29 | t.after(() => { store.close(); fs.rmSync(folder, { recursive: true, force: true }) }) |
| 30 | const write = (name, args) => store.dispatch(name, { requestKey: randomUUID(), ...args }) |
| 31 | const boardId = store.boards()[0].id |
| 32 | const original = '# Launch\n\n- [ ] Verify the installer\n\n```swift\nprint("hello")\n```\n' |
| 33 | let { task } = await write('create_task', { boardId, title: 'Review launch', planMarkdown: original }) |
| 34 | assert.equal(task.planMarkdown, original) |
| 35 | const id = task.id |
| 36 | const next = original + '\n- [ ] Check iCloud sync\n' |
| 37 | const edit = { id, expectedMarkdown: original, markdown: next, requestKey: randomUUID() } |
| 38 | // Editing unrelated metadata must not lock or invalidate an open plan editor. |
| 39 | await write('update_task', { id, expectedRevision: task.revision, priority: 'High' }) |
| 40 | const applied = await store.dispatch('set_task_plan', edit) |
| 41 | assert.deepEqual(await store.dispatch('set_task_plan', edit), applied) |
| 42 | await assert.rejects(write('set_task_plan', { id, expectedMarkdown: original, markdown: 'Stale plan' }), { code: 'CONFLICT' }) |
| 43 | assert.equal(store.task(id).planMarkdown, next) |
| 44 | const { board } = await write('create_board', { name: 'New project' }) |
| 45 | await write('move_task', { id, expectedRevision: store.task(id).revision, boardId: board.id }) |
| 46 | assert.equal(store.task(id).planMarkdown, next) |
| 47 | await write('companion_organize_task', { id, destination: 'archive' }) |
| 48 | for (const format of ['json', 'sqlite']) { |
| 49 | const backup = await write('create_backup', { format }) |
| 50 | const preview = await store.dispatch('preview_destructive_action', { action: 'restore_backup', path: backup.path }) |
| 51 | await write('confirm_destructive_action', { token: preview.token }) |
| 52 | assert.equal(store.task(id).planMarkdown, next) |
| 53 | assert.ok(store.task(id).archivedAt) |
| 54 | } |
| 55 | await write('companion_organize_task', { id, destination: 'active' }) |
| 56 | await write('companion_organize_task', { id, destination: 'trash' }) |
| 57 | await write('restore_task', { id, expectedRevision: store.task(id).revision }) |
| 58 | assert.equal(store.task(id).planMarkdown, next) |
| 59 | assert.equal(store.task(id).deleted, false) |
| 60 | await assert.rejects(write('set_task_plan', { id, expectedMarkdown: next, markdown: 'x'.repeat(100001) }), { code: 'INVALID_ARGUMENT' }) |
| 61 | await assert.rejects(write('set_task_plan', { id, expectedMarkdown: next, markdown: 'Plan\0content' }), { code: 'INVALID_ARGUMENT' }) |
| 62 | assert.equal(store.task(id).planMarkdown, next) |
| 63 | }) |
| 64 | |
| 65 | test('assistant text edits compare only their own fields and validate all values', async (t) => { |
| 66 | const folder = fs.mkdtempSync(path.join(os.tmpdir(), 'opentalon-assistant-')) |
| 67 | const store = new Store(folder) |
| 68 | t.after(() => { store.close(); fs.rmSync(folder, { recursive: true, force: true }) }) |
| 69 | const write = (name, args) => store.dispatch(name, { requestKey: randomUUID(), ...args }) |
| 70 | const { task } = await write('create_task', { boardId: store.boards()[0].id, title: 'Original' }) |
| 71 | await write('companion_set_task_field', { id: task.id, field: 'notes', expectedValue: '', value: 'New notes' }) |
| 72 | await write('companion_set_task_field', { id: task.id, field: 'title', expectedValue: 'Original', value: 'Renamed' }) |
| 73 | await write('companion_set_task_field', { id: task.id, field: 'definitionOfDone', expectedValue: '', value: 'Verified' }) |
| 74 | assert.equal(store.task(task.id).notes, 'New notes') |
| 75 | assert.equal(store.task(task.id).title, 'Renamed') |
| 76 | assert.equal(store.task(task.id).definitionOfDone, 'Verified') |
| 77 | await assert.rejects(write('companion_set_task_field', { id: task.id, field: 'title', expectedValue: 'Original', value: 'Stale' }), { code: 'CONFLICT' }) |
| 78 | await assert.rejects(write('companion_set_task_field', { id: task.id, field: 'title', expectedValue: 'Renamed', value: '' }), { code: 'INVALID_ARGUMENT' }) |
| 79 | await assert.rejects(write('companion_set_task_field', { id: task.id, field: 'deleted_at', expectedValue: null, value: 'now' }), { code: 'INVALID_ARGUMENT' }) |
| 80 | }) |
| 81 | |
| 82 | test('legacy Markdown attachments recover on upgrade without replacing documents or losing notes and history', async (t) => { |
| 83 | const folder = fs.mkdtempSync(path.join(os.tmpdir(), 'opentalon-attached-plan-')) |
| 84 | let store = new Store(folder) |
| 85 | t.after(() => { store.close(); fs.rmSync(folder, { recursive: true, force: true }) }) |
| 86 | const boardId = store.boards()[0].id |
| 87 | const block = markdown => `--- Plan: /not-a-real-file/project.md (copied earlier) ---\n${markdown}--- End plan ---\n` |
| 88 | const latest = '# Current plan\n\n- [ ] Verify the release.\n\n```text\n--- End plan ---\n```\n' |
| 89 | const notes = 'Keep this context.\n' + block('# Previous plan\n') + '\n' + block(latest) + '\nKeep this follow-up.' |
| 90 | const create = async (title, markdown, body = notes) => (await store.dispatch('create_task', { |
| 91 | requestKey: randomUUID(), boardId, title, notes: body, planMarkdown: markdown, |
| 92 | })).task |
| 93 | // Explicit empty plans recreate pre-upgrade records without using the new compatibility write path. |
| 94 | const recovered = await create('Recover the latest complete plan', '') |
| 95 | const existing = await create('Keep my edited document', '# Independently edited\n') |
| 96 | const incomplete = await create('Do not invent a missing attachment', '', '--- Plan: /missing.md ---\n# Incomplete') |
| 97 | const fenced = await create('Ignore a quoted example', '', '```text\n' + block('# Example only\n') + '```\n') |
| 98 | const previousHistory = store.dispatch('get_task_history', { id: recovered.id }) |
| 99 | const history = await previousHistory |
| 100 | store.db.exec('DROP TABLE task_files; DELETE FROM schema_migrations WHERE version>=12; PRAGMA user_version=11;') |
| 101 | store.close(); store = new Store(folder) |
| 102 | assert.equal(store.task(recovered.id).planMarkdown, latest) |
| 103 | assert.equal(store.task(recovered.id).notes, notes) |
| 104 | assert.equal(store.task(recovered.id).taskKey, recovered.taskKey) |
| 105 | assert.equal(store.task(recovered.id).status, recovered.status) |
| 106 | assert.equal(store.task(existing.id).planMarkdown, '# Independently edited\n') |
| 107 | assert.equal(store.task(incomplete.id).planMarkdown, '') |
| 108 | assert.equal(store.task(fenced.id).planMarkdown, '') |
| 109 | const updatedHistory = await store.dispatch('get_task_history', { id: recovered.id }) |
| 110 | assert.equal(updatedHistory.entries.length, history.entries.length + 1) |
| 111 | assert.equal(updatedHistory.entries[0].action, 'recover_task_plan') |
| 112 | assert.deepEqual(updatedHistory.entries.slice(1), history.entries) |
| 113 | const revision = store.revision() |
| 114 | store.close(); store = new Store(folder) |
| 115 | assert.equal(store.revision(), revision, 'recovery runs once') |
| 116 | assert.equal(store.task(recovered.id).planMarkdown, latest) |
| 117 | }) |
| 118 | |
| 119 | test('old connector plan blocks and version-eleven backups remain accessible as real plans', async (t) => { |
| 120 | const folder = fs.mkdtempSync(path.join(os.tmpdir(), 'opentalon-plan-compatibility-')) |
| 121 | const store = new Store(folder) |
| 122 | t.after(() => { store.close(); fs.rmSync(folder, { recursive: true, force: true }) }) |
| 123 | const write = (name, args) => store.dispatch(name, { requestKey: randomUUID(), ...args }) |
| 124 | const markdown = '# Attached plan\n\n- [ ] Check preview and editing.\n' |
| 125 | const notes = `Context\n--- Plan: /missing/plan.md ---\n${markdown}--- End plan ---\n` |
| 126 | const { task } = await write('create_task', { boardId: store.boards()[0].id, title: 'Legacy connector', notes }) |
| 127 | assert.equal(task.planMarkdown, markdown) |
| 128 | await write('set_task_plan', { id: task.id, expectedMarkdown: markdown, markdown: '# User changed the plan\n' }) |
| 129 | await write('update_task', { id: task.id, expectedRevision: store.task(task.id).revision, notes: notes + 'More context' }) |
| 130 | assert.equal(store.task(task.id).planMarkdown, '# User changed the plan\n') |
| 131 | const backup = await write('create_backup', { format: 'json' }) |
| 132 | const payload = JSON.parse(fs.readFileSync(backup.path)) |
| 133 | payload.formatVersion = 11 |
| 134 | payload.tasks.find(row => row.id === task.id).plan_markdown = '' |
| 135 | fs.writeFileSync(backup.path, JSON.stringify(payload)) |
| 136 | const preview = await store.dispatch('preview_destructive_action', { action: 'restore_backup', path: backup.path }) |
| 137 | await write('confirm_destructive_action', { token: preview.token }) |
| 138 | assert.equal(store.task(task.id).planMarkdown, markdown) |
| 139 | assert.equal(store.task(task.id).notes, notes + 'More context') |
| 140 | }) |