|
| 1 | +import { |
| 2 | + chmodSync, |
| 3 | + mkdirSync, |
| 4 | + mkdtempSync, |
| 5 | + rmSync, |
| 6 | + symlinkSync, |
| 7 | + writeFileSync, |
| 8 | +} from 'node:fs' |
| 9 | +import { tmpdir } from 'node:os' |
| 10 | +import { delimiter, join } from 'node:path' |
| 11 | +import { afterEach, beforeEach, describe, expect, it } from 'vitest' |
| 12 | +import { messages } from '../../src/messages.js' |
| 13 | +import { runPiped } from '../helpers/spawn-piped.js' |
| 14 | + |
| 15 | +/** |
| 16 | + * #738: `stash plan` printed `Plan drafted at .cipherstash/plan.md` and exited |
| 17 | + * 0 unconditionally. The plan file is written by the handed-off agent, not by |
| 18 | + * the CLI, so a failed or deferred handoff produced a false success — and sent |
| 19 | + * `stash impl` (and any automation reading the outro) after a file that never |
| 20 | + * existed. The command now verifies the file on disk after the handoff and |
| 21 | + * reports the outcome that actually occurred. |
| 22 | + * |
| 23 | + * A fake `claude` binary prepended to PATH drives the "agent launched" paths |
| 24 | + * without the real agent — no DB, no network. (The e2e suite runs on POSIX |
| 25 | + * only, so a /bin/sh script is fine.) |
| 26 | + */ |
| 27 | +describe('stash plan — outcome reflects the plan file on disk', () => { |
| 28 | + let dir: string |
| 29 | + |
| 30 | + beforeEach(() => { |
| 31 | + dir = mkdtempSync(join(tmpdir(), 'plan-outcome-e2e-')) |
| 32 | + mkdirSync(join(dir, '.cipherstash'), { recursive: true }) |
| 33 | + writeFileSync( |
| 34 | + join(dir, '.cipherstash', 'context.json'), |
| 35 | + JSON.stringify({ |
| 36 | + integration: 'postgresql', |
| 37 | + packageManager: 'npm', |
| 38 | + schemas: [], |
| 39 | + }), |
| 40 | + ) |
| 41 | + }) |
| 42 | + afterEach(() => { |
| 43 | + rmSync(dir, { recursive: true, force: true }) |
| 44 | + }) |
| 45 | + |
| 46 | + /** |
| 47 | + * Write an executable fake `claude` into a bin dir under the fixture and |
| 48 | + * return a PATH that resolves it first. `spawnAgent` inherits the CLI's |
| 49 | + * cwd, so the script's relative paths land inside the fixture project. |
| 50 | + */ |
| 51 | + function fakeClaudePath(script: string): string { |
| 52 | + const bin = join(dir, 'fake-bin') |
| 53 | + mkdirSync(bin, { recursive: true }) |
| 54 | + const file = join(bin, 'claude') |
| 55 | + writeFileSync(file, `#!/bin/sh\n${script}\n`) |
| 56 | + chmodSync(file, 0o755) |
| 57 | + return `${bin}${delimiter}${process.env.PATH ?? ''}` |
| 58 | + } |
| 59 | + |
| 60 | + it('exits 1 when the launched agent writes no plan (no false success)', async () => { |
| 61 | + const r = await runPiped(['plan', '--target', 'claude-code'], { |
| 62 | + cwd: dir, |
| 63 | + env: { PATH: fakeClaudePath('exit 0') }, |
| 64 | + timeoutMs: 20000, |
| 65 | + }) |
| 66 | + expect(r.timedOut).toBe(false) |
| 67 | + expect(r.exitCode).toBe(1) |
| 68 | + const out = r.stdout + r.stderr |
| 69 | + expect(out).toContain(messages.plan.notWritten) |
| 70 | + expect(out).not.toContain(messages.plan.drafted) |
| 71 | + }) |
| 72 | + |
| 73 | + it('reports "Plan drafted" and exits 0 when the agent wrote the plan', async () => { |
| 74 | + const r = await runPiped(['plan', '--target', 'claude-code'], { |
| 75 | + cwd: dir, |
| 76 | + env: { |
| 77 | + PATH: fakeClaudePath('echo "# Plan" > .cipherstash/plan.md'), |
| 78 | + }, |
| 79 | + timeoutMs: 20000, |
| 80 | + }) |
| 81 | + expect(r.timedOut).toBe(false) |
| 82 | + expect(r.exitCode).toBe(0) |
| 83 | + const out = r.stdout + r.stderr |
| 84 | + expect(out).toContain(`${messages.plan.drafted} \`.cipherstash/plan.md\``) |
| 85 | + }) |
| 86 | + |
| 87 | + it('reports a pre-existing plan as unchanged, not drafted', async () => { |
| 88 | + writeFileSync(join(dir, '.cipherstash', 'plan.md'), '# old plan\n') |
| 89 | + const r = await runPiped(['plan', '--target', 'claude-code'], { |
| 90 | + cwd: dir, |
| 91 | + env: { PATH: fakeClaudePath('exit 0') }, |
| 92 | + timeoutMs: 20000, |
| 93 | + }) |
| 94 | + expect(r.timedOut).toBe(false) |
| 95 | + // A usable plan exists on disk, so this is not a failure — but the run |
| 96 | + // must not claim it drafted anything. |
| 97 | + expect(r.exitCode).toBe(0) |
| 98 | + const out = r.stdout + r.stderr |
| 99 | + expect(out).toContain(messages.plan.unchanged) |
| 100 | + expect(out).not.toContain(messages.plan.drafted) |
| 101 | + }) |
| 102 | + |
| 103 | + it('reports "drafted" when the agent revises a pre-existing plan', async () => { |
| 104 | + writeFileSync(join(dir, '.cipherstash', 'plan.md'), '# old plan\n') |
| 105 | + const r = await runPiped(['plan', '--target', 'claude-code'], { |
| 106 | + cwd: dir, |
| 107 | + // The agent appends — a real revision that changes size (and mtime), so |
| 108 | + // the change-detection limb reports "drafted", not "unchanged". |
| 109 | + env: { PATH: fakeClaudePath('echo "# revised" >> .cipherstash/plan.md') }, |
| 110 | + timeoutMs: 20000, |
| 111 | + }) |
| 112 | + expect(r.timedOut).toBe(false) |
| 113 | + expect(r.exitCode).toBe(0) |
| 114 | + const out = r.stdout + r.stderr |
| 115 | + expect(out).toContain(`${messages.plan.drafted} \`.cipherstash/plan.md\``) |
| 116 | + expect(out).not.toContain(messages.plan.unchanged) |
| 117 | + }) |
| 118 | + |
| 119 | + it('treats a plan.md directory as no plan, not a false "unchanged"', async () => { |
| 120 | + // `statSync` succeeds for a directory, but no agent can write a plan there. |
| 121 | + // Without the isFile() gate this would warn "already exists" and then, with |
| 122 | + // an agent that writes nothing, report a false "unchanged" exit 0. |
| 123 | + mkdirSync(join(dir, '.cipherstash', 'plan.md')) |
| 124 | + const r = await runPiped(['plan', '--target', 'claude-code'], { |
| 125 | + cwd: dir, |
| 126 | + env: { PATH: fakeClaudePath('exit 0') }, |
| 127 | + timeoutMs: 20000, |
| 128 | + }) |
| 129 | + expect(r.timedOut).toBe(false) |
| 130 | + expect(r.exitCode).toBe(1) |
| 131 | + const out = r.stdout + r.stderr |
| 132 | + expect(out).toContain(messages.plan.notWritten) |
| 133 | + expect(out).not.toContain(messages.plan.unchanged) |
| 134 | + expect(out).not.toContain(messages.plan.drafted) |
| 135 | + }) |
| 136 | + |
| 137 | + it('agents-md handoff says "No plan drafted yet" instead of claiming success', async () => { |
| 138 | + const r = await runPiped(['plan', '--target', 'agents-md'], { |
| 139 | + cwd: dir, |
| 140 | + timeoutMs: 20000, |
| 141 | + }) |
| 142 | + expect(r.timedOut).toBe(false) |
| 143 | + // The deferred handoff delivered its files-and-instructions contract, so |
| 144 | + // exit 0 — but the plan is written later, by the user's editor agent. |
| 145 | + expect(r.exitCode).toBe(0) |
| 146 | + const out = r.stdout + r.stderr |
| 147 | + expect(out).toContain(messages.plan.noPlanYet) |
| 148 | + expect(out).not.toContain(messages.plan.drafted) |
| 149 | + }) |
| 150 | + |
| 151 | + it('claude-code target without claude on PATH defers honestly', async () => { |
| 152 | + const r = await runPiped(['plan', '--target', 'claude-code'], { |
| 153 | + cwd: dir, |
| 154 | + // A PATH with no `claude` anywhere: the handoff writes files and |
| 155 | + // prints install instructions instead of spawning. |
| 156 | + env: { PATH: '/usr/bin:/bin' }, |
| 157 | + timeoutMs: 20000, |
| 158 | + }) |
| 159 | + expect(r.timedOut).toBe(false) |
| 160 | + expect(r.exitCode).toBe(0) |
| 161 | + const out = r.stdout + r.stderr |
| 162 | + expect(out).toContain(messages.plan.noPlanYet) |
| 163 | + expect(out).not.toContain(messages.plan.drafted) |
| 164 | + }) |
| 165 | + |
| 166 | + it('exits 1 with a clear message when the plan path cannot be statted', async () => { |
| 167 | + // A self-referential symlink at plan.md makes `statSync` throw ELOOP, not |
| 168 | + // ENOENT — a non-ENOENT fs error that must surface as a controlled exit |
| 169 | + // (clear message + non-zero), never an opaque "Fatal error" crash. |
| 170 | + symlinkSync('plan.md', join(dir, '.cipherstash', 'plan.md')) |
| 171 | + const r = await runPiped(['plan', '--target', 'agents-md'], { |
| 172 | + cwd: dir, |
| 173 | + timeoutMs: 20000, |
| 174 | + }) |
| 175 | + expect(r.timedOut).toBe(false) |
| 176 | + expect(r.exitCode).toBe(1) |
| 177 | + const out = r.stdout + r.stderr |
| 178 | + expect(out).toContain('Could not read') |
| 179 | + expect(out).not.toContain(messages.plan.drafted) |
| 180 | + }) |
| 181 | +}) |
0 commit comments