|
| 1 | +/** |
| 2 | + * Regression test for bugfix #676: `porch approve plan-approval` fails with |
| 3 | + * "Plan not found" when the plan lives in a builder worktree. |
| 4 | + * |
| 5 | + * Reproduction: |
| 6 | + * 1. Architect runs `porch approve NNN plan-approval ...` from the main |
| 7 | + * workspace root (cwd = repo root). |
| 8 | + * 2. `findStatusPath` (fixed in PR #674) correctly locates the project's |
| 9 | + * status.yaml inside `.builders/<slug>/codev/projects/...`. |
| 10 | + * 3. BUT the artifact resolver and the cwd passed to `runPhaseChecks` are |
| 11 | + * still scoped to the main workspace, so the `plan_exists` check looks |
| 12 | + * at `<main>/codev/plans/` — where the plan does not exist yet — |
| 13 | + * and fails. |
| 14 | + * |
| 15 | + * Fix: `check`, `done`, and `approve` derive the artifact root from the |
| 16 | + * resolved status path (via `getArtifactRoot`) and rebuild the resolver + |
| 17 | + * check cwd to point at that worktree. |
| 18 | + */ |
| 19 | + |
| 20 | +import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; |
| 21 | +import * as fs from 'node:fs'; |
| 22 | +import * as path from 'node:path'; |
| 23 | +import { tmpdir } from 'node:os'; |
| 24 | +import { approve, check, done } from '../index.js'; |
| 25 | +import { writeState, readState, getStatusPath } from '../state.js'; |
| 26 | +import type { ProjectState } from '../types.js'; |
| 27 | + |
| 28 | +// --------------------------------------------------------------------------- |
| 29 | +// Helpers |
| 30 | +// --------------------------------------------------------------------------- |
| 31 | + |
| 32 | +/** Mimic the on-disk layout: `<mainRoot>/.builders/<slug>/...` with its own codev/. */ |
| 33 | +function makeWorkspace(suffix: string): { mainRoot: string; worktreeRoot: string; worktreeSlug: string } { |
| 34 | + const mainRoot = path.join(tmpdir(), `porch-676-${suffix}-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`); |
| 35 | + fs.mkdirSync(mainRoot, { recursive: true }); |
| 36 | + const worktreeSlug = 'spir-42-test-feature'; |
| 37 | + const worktreeRoot = path.join(mainRoot, '.builders', worktreeSlug); |
| 38 | + fs.mkdirSync(worktreeRoot, { recursive: true }); |
| 39 | + return { mainRoot, worktreeRoot, worktreeSlug }; |
| 40 | +} |
| 41 | + |
| 42 | +function writeProtocol(root: string, protocol: { name: string; [k: string]: unknown }): void { |
| 43 | + const dir = path.join(root, 'codev', 'protocols', protocol.name); |
| 44 | + fs.mkdirSync(dir, { recursive: true }); |
| 45 | + fs.writeFileSync(path.join(dir, 'protocol.json'), JSON.stringify(protocol, null, 2)); |
| 46 | +} |
| 47 | + |
| 48 | +function writeWorktreeStatus(worktreeRoot: string, state: ProjectState): string { |
| 49 | + const statusPath = getStatusPath(worktreeRoot, state.id, state.title); |
| 50 | + fs.mkdirSync(path.dirname(statusPath), { recursive: true }); |
| 51 | + writeState(statusPath, state); |
| 52 | + return statusPath; |
| 53 | +} |
| 54 | + |
| 55 | +function makeState(overrides: Partial<ProjectState> = {}): ProjectState { |
| 56 | + return { |
| 57 | + id: '0042', |
| 58 | + title: 'test-feature', |
| 59 | + protocol: 'spir-676-test', |
| 60 | + phase: 'plan', |
| 61 | + plan_phases: [], |
| 62 | + current_plan_phase: null, |
| 63 | + gates: { |
| 64 | + 'plan-approval': { status: 'pending', requested_at: new Date().toISOString() }, |
| 65 | + }, |
| 66 | + iteration: 1, |
| 67 | + build_complete: false, |
| 68 | + history: [], |
| 69 | + started_at: new Date().toISOString(), |
| 70 | + updated_at: new Date().toISOString(), |
| 71 | + ...overrides, |
| 72 | + }; |
| 73 | +} |
| 74 | + |
| 75 | +// Protocol is saved to disk as JSON and normalized by `loadProtocol`. |
| 76 | +// Phase `checks` is an object of name → command (not an array). |
| 77 | +const testProtocolJson = { |
| 78 | + name: 'spir-676-test', |
| 79 | + version: '1.0.0', |
| 80 | + phases: [ |
| 81 | + { |
| 82 | + id: 'plan', |
| 83 | + name: 'Plan', |
| 84 | + gate: 'plan-approval', |
| 85 | + checks: { |
| 86 | + plan_exists: 'test -f codev/plans/${PROJECT_TITLE}.md', |
| 87 | + }, |
| 88 | + next: 'implement', |
| 89 | + }, |
| 90 | + { id: 'implement', name: 'Implement', next: null }, |
| 91 | + ], |
| 92 | +}; |
| 93 | + |
| 94 | +// Suppress noisy CLI output; keep process.exit swallowable so we can inspect state. |
| 95 | +let logSpy: ReturnType<typeof vi.spyOn>; |
| 96 | +let exitSpy: ReturnType<typeof vi.spyOn>; |
| 97 | + |
| 98 | +beforeEach(() => { |
| 99 | + logSpy = vi.spyOn(console, 'log').mockImplementation(() => {}); |
| 100 | + exitSpy = vi.spyOn(process, 'exit').mockImplementation(((code?: number) => { |
| 101 | + throw new Error(`process.exit(${code ?? 0})`); |
| 102 | + }) as never); |
| 103 | +}); |
| 104 | + |
| 105 | +afterEach(() => { |
| 106 | + logSpy.mockRestore(); |
| 107 | + exitSpy.mockRestore(); |
| 108 | +}); |
| 109 | + |
| 110 | +// --------------------------------------------------------------------------- |
| 111 | +// Tests |
| 112 | +// --------------------------------------------------------------------------- |
| 113 | + |
| 114 | +describe('bugfix #676 — artifact resolver follows worktree status path', () => { |
| 115 | + it('approve resolves plan_exists against the worktree, not the main workspace', async () => { |
| 116 | + const { mainRoot, worktreeRoot } = makeWorkspace('approve'); |
| 117 | + writeProtocol(mainRoot, testProtocolJson); |
| 118 | + // Protocol files are read from the main workspace — but artifacts live in the worktree. |
| 119 | + |
| 120 | + const statusPath = writeWorktreeStatus(worktreeRoot, makeState()); |
| 121 | + |
| 122 | + // Plan exists ONLY in the worktree; the main workspace has NO codev/plans/ directory. |
| 123 | + const worktreePlansDir = path.join(worktreeRoot, 'codev', 'plans'); |
| 124 | + fs.mkdirSync(worktreePlansDir, { recursive: true }); |
| 125 | + fs.writeFileSync(path.join(worktreePlansDir, '0042-test-feature.md'), '# Plan\n'); |
| 126 | + |
| 127 | + expect(fs.existsSync(path.join(mainRoot, 'codev', 'plans'))).toBe(false); |
| 128 | + |
| 129 | + // Architect runs approve from main workspace root |
| 130 | + await approve(mainRoot, '0042', 'plan-approval', true); |
| 131 | + |
| 132 | + // Gate should be approved (checks passed because resolver found the plan in the worktree) |
| 133 | + const updated = readState(statusPath); |
| 134 | + expect(updated.gates['plan-approval'].status).toBe('approved'); |
| 135 | + expect(updated.gates['plan-approval'].approved_at).toBeDefined(); |
| 136 | + }); |
| 137 | + |
| 138 | + it('approve fails cleanly when the plan is missing from the worktree', async () => { |
| 139 | + const { mainRoot, worktreeRoot } = makeWorkspace('approve-missing'); |
| 140 | + writeProtocol(mainRoot, testProtocolJson); |
| 141 | + const statusPath = writeWorktreeStatus(worktreeRoot, makeState()); |
| 142 | + |
| 143 | + // Writing a plan in the MAIN workspace must NOT trick the check into |
| 144 | + // approving the gate — only the worktree is authoritative. |
| 145 | + const mainPlansDir = path.join(mainRoot, 'codev', 'plans'); |
| 146 | + fs.mkdirSync(mainPlansDir, { recursive: true }); |
| 147 | + fs.writeFileSync(path.join(mainPlansDir, '0042-test-feature.md'), '# Decoy\n'); |
| 148 | + |
| 149 | + await expect(approve(mainRoot, '0042', 'plan-approval', true)).rejects.toThrow(/process\.exit/); |
| 150 | + |
| 151 | + // Gate must NOT be approved when the worktree lacks the plan |
| 152 | + const updated = readState(statusPath); |
| 153 | + expect(updated.gates['plan-approval'].status).toBe('pending'); |
| 154 | + expect(updated.gates['plan-approval'].approved_at).toBeUndefined(); |
| 155 | + }); |
| 156 | + |
| 157 | + it('check resolves plan_exists against the worktree', async () => { |
| 158 | + const { mainRoot, worktreeRoot } = makeWorkspace('check'); |
| 159 | + writeProtocol(mainRoot, testProtocolJson); |
| 160 | + writeWorktreeStatus(worktreeRoot, makeState()); |
| 161 | + |
| 162 | + const worktreePlansDir = path.join(worktreeRoot, 'codev', 'plans'); |
| 163 | + fs.mkdirSync(worktreePlansDir, { recursive: true }); |
| 164 | + fs.writeFileSync(path.join(worktreePlansDir, '0042-test-feature.md'), '# Plan\n'); |
| 165 | + |
| 166 | + // check() prints results but must not throw when all checks pass |
| 167 | + await expect(check(mainRoot, '0042')).resolves.toBeUndefined(); |
| 168 | + }); |
| 169 | + |
| 170 | + it('done resolves plan_exists against the worktree', async () => { |
| 171 | + const { mainRoot, worktreeRoot } = makeWorkspace('done'); |
| 172 | + // Protocol where the plan phase has no gate, so done() will attempt to advance |
| 173 | + const protocolNoGate = { |
| 174 | + name: 'spir-676-test-done', |
| 175 | + version: '1.0.0', |
| 176 | + phases: [ |
| 177 | + { |
| 178 | + id: 'plan', |
| 179 | + name: 'Plan', |
| 180 | + checks: { plan_exists: 'test -f codev/plans/${PROJECT_TITLE}.md' }, |
| 181 | + next: 'review', |
| 182 | + }, |
| 183 | + { id: 'review', name: 'Review', next: null }, |
| 184 | + ], |
| 185 | + }; |
| 186 | + writeProtocol(mainRoot, protocolNoGate); |
| 187 | + |
| 188 | + const state = makeState({ protocol: 'spir-676-test-done', gates: {} }); |
| 189 | + const statusPath = writeWorktreeStatus(worktreeRoot, state); |
| 190 | + |
| 191 | + const worktreePlansDir = path.join(worktreeRoot, 'codev', 'plans'); |
| 192 | + fs.mkdirSync(worktreePlansDir, { recursive: true }); |
| 193 | + fs.writeFileSync(path.join(worktreePlansDir, '0042-test-feature.md'), '# Plan\n'); |
| 194 | + |
| 195 | + await done(mainRoot, '0042'); |
| 196 | + |
| 197 | + const updated = readState(statusPath); |
| 198 | + expect(updated.phase).toBe('review'); |
| 199 | + }); |
| 200 | +}); |
0 commit comments