|
| 1 | +import { describe, test, expect, beforeEach, afterEach } from 'bun:test' |
| 2 | +import { existsSync, mkdirSync, rmSync, writeFileSync } from 'fs' |
| 3 | +import { join } from 'path' |
| 4 | +import { savePlanFromDialog } from '../../src/utils/plan-save-target' |
| 5 | +import { resolvePlanArchiveDir } from '../../src/utils/plan-archive' |
| 6 | + |
| 7 | +const TEST_DIR = '/tmp/opencode-forge-plan-save-target-test-' + Date.now() |
| 8 | + |
| 9 | +describe('savePlanFromDialog', () => { |
| 10 | + let testDataDir: string |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + testDataDir = TEST_DIR + '-data-' + Math.random().toString(36).slice(2) |
| 14 | + process.env['XDG_DATA_HOME'] = testDataDir |
| 15 | + }) |
| 16 | + |
| 17 | + afterEach(() => { |
| 18 | + delete process.env['XDG_DATA_HOME'] |
| 19 | + if (existsSync(testDataDir)) { |
| 20 | + rmSync(testDataDir, { recursive: true, force: true }) |
| 21 | + } |
| 22 | + }) |
| 23 | + |
| 24 | + test('sessionId present writes to session and does not touch archive', async () => { |
| 25 | + const calls: Array<[string, string]> = [] |
| 26 | + const outcome = await savePlanFromDialog({ |
| 27 | + sessionId: 'session-1', |
| 28 | + projectId: 'project-1', |
| 29 | + text: '# Session Plan', |
| 30 | + writeSession: async (sessionId, text) => { |
| 31 | + calls.push([sessionId, text]) |
| 32 | + return true |
| 33 | + }, |
| 34 | + }) |
| 35 | + |
| 36 | + expect(calls).toEqual([['session-1', '# Session Plan']]) |
| 37 | + expect(existsSync(resolvePlanArchiveDir('project-1'))).toBe(false) |
| 38 | + expect(outcome).toEqual({ kind: 'session', ok: true }) |
| 39 | + }) |
| 40 | + |
| 41 | + test('sessionId present returns false when session write fails', async () => { |
| 42 | + const outcome = await savePlanFromDialog({ |
| 43 | + sessionId: 'session-1', |
| 44 | + projectId: 'project-1', |
| 45 | + text: '# Session Plan', |
| 46 | + writeSession: async () => false, |
| 47 | + }) |
| 48 | + |
| 49 | + expect(outcome).toEqual({ kind: 'session', ok: false }) |
| 50 | + }) |
| 51 | + |
| 52 | + test('sessionId empty archives and dedupes identical content', async () => { |
| 53 | + let sessionWrites = 0 |
| 54 | + const args = { |
| 55 | + sessionId: undefined, |
| 56 | + projectId: 'project-archive', |
| 57 | + text: '# Archived Plan', |
| 58 | + writeSession: async () => { |
| 59 | + sessionWrites += 1 |
| 60 | + return true |
| 61 | + }, |
| 62 | + now: new Date('2026-05-04T12:00:00.000Z'), |
| 63 | + } |
| 64 | + |
| 65 | + const first = await savePlanFromDialog(args) |
| 66 | + const second = await savePlanFromDialog(args) |
| 67 | + |
| 68 | + expect(sessionWrites).toBe(0) |
| 69 | + if (first.kind === 'archive' && first.ok) { |
| 70 | + expect(first.deduped).toBe(false) |
| 71 | + expect(first.filename).toMatch(/^[0-9a-f]{64}\.md$/) |
| 72 | + expect(existsSync(first.filepath)).toBe(true) |
| 73 | + } else { |
| 74 | + throw new Error('expected archive success') |
| 75 | + } |
| 76 | + if (second.kind === 'archive' && second.ok) { |
| 77 | + expect(second.deduped).toBe(true) |
| 78 | + expect(existsSync(second.filepath)).toBe(true) |
| 79 | + } else { |
| 80 | + throw new Error('expected archive success') |
| 81 | + } |
| 82 | + }) |
| 83 | + |
| 84 | + test('sessionId empty and projectId missing returns noop and writes nothing', async () => { |
| 85 | + let sessionWrites = 0 |
| 86 | + const outcome = await savePlanFromDialog({ |
| 87 | + sessionId: '', |
| 88 | + projectId: undefined, |
| 89 | + text: '# No Context', |
| 90 | + writeSession: async () => { |
| 91 | + sessionWrites += 1 |
| 92 | + return true |
| 93 | + }, |
| 94 | + }) |
| 95 | + |
| 96 | + expect(sessionWrites).toBe(0) |
| 97 | + expect(outcome).toEqual({ kind: 'noop', reason: 'missing-project' }) |
| 98 | + expect(existsSync(join(testDataDir, 'opencode', 'forge', 'plans'))).toBe(false) |
| 99 | + }) |
| 100 | + |
| 101 | + test('archive write errors are returned as archive failures', async () => { |
| 102 | + const sentinel = join(testDataDir, 'opencode') |
| 103 | + mkdirSync(testDataDir, { recursive: true }) |
| 104 | + writeFileSync(sentinel, 'not a directory') |
| 105 | + |
| 106 | + const outcome = await savePlanFromDialog({ |
| 107 | + sessionId: undefined, |
| 108 | + projectId: 'project-error', |
| 109 | + text: '# Error Plan', |
| 110 | + writeSession: async () => true, |
| 111 | + }) |
| 112 | + |
| 113 | + if (outcome.kind === 'archive' && !outcome.ok) { |
| 114 | + expect(outcome.error).toBeInstanceOf(Error) |
| 115 | + } else { |
| 116 | + throw new Error('expected archive failure') |
| 117 | + } |
| 118 | + }) |
| 119 | +}) |
0 commit comments