|
| 1 | +/** |
| 2 | + * Tests for improvement hook reader. |
| 3 | + * |
| 4 | + * Run: bun test packages/shared/improvement-hooks.test.ts |
| 5 | + */ |
| 6 | + |
| 7 | +import { describe, expect, test, beforeEach, afterEach } from "bun:test"; |
| 8 | +import { mkdirSync, writeFileSync, rmSync, existsSync } from "fs"; |
| 9 | +import { join } from "path"; |
| 10 | +import { tmpdir } from "os"; |
| 11 | + |
| 12 | +// We need to override the base dirs used by readImprovementHook. |
| 13 | +// Since the module uses homedir() at import time, we mock it via |
| 14 | +// a test harness that sets HOME to a temp directory. |
| 15 | + |
| 16 | +const TEST_HOME = join(tmpdir(), `improvement-hooks-test-${Date.now()}`); |
| 17 | +const NEW_BASE = join(TEST_HOME, ".plannotator", "hooks"); |
| 18 | +const LEGACY_BASE = join(TEST_HOME, ".plannotator"); |
| 19 | +const HOOK_RELATIVE = "compound/enterplanmode-improve-hook.txt"; |
| 20 | + |
| 21 | +function setupTestHome() { |
| 22 | + mkdirSync(join(NEW_BASE, "compound"), { recursive: true }); |
| 23 | + mkdirSync(join(LEGACY_BASE, "compound"), { recursive: true }); |
| 24 | +} |
| 25 | + |
| 26 | +function cleanTestHome() { |
| 27 | + if (existsSync(TEST_HOME)) { |
| 28 | + rmSync(TEST_HOME, { recursive: true, force: true }); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +// Since the module reads homedir() at import time, we need to |
| 33 | +// re-import with HOME overridden. Use a helper that spawns a |
| 34 | +// small inline script to test each scenario in isolation. |
| 35 | +async function runScenario(setup: { |
| 36 | + newPathContent?: string | null; |
| 37 | + legacyPathContent?: string | null; |
| 38 | +}): Promise<{ content: string; filePath: string } | null> { |
| 39 | + setupTestHome(); |
| 40 | + |
| 41 | + const newPath = join(NEW_BASE, HOOK_RELATIVE); |
| 42 | + const legacyPath = join(LEGACY_BASE, HOOK_RELATIVE); |
| 43 | + |
| 44 | + if (setup.newPathContent !== undefined && setup.newPathContent !== null) { |
| 45 | + writeFileSync(newPath, setup.newPathContent); |
| 46 | + } |
| 47 | + if (setup.legacyPathContent !== undefined && setup.legacyPathContent !== null) { |
| 48 | + writeFileSync(legacyPath, setup.legacyPathContent); |
| 49 | + } |
| 50 | + |
| 51 | + // Run in a subprocess with HOME overridden so homedir() returns TEST_HOME |
| 52 | + const proc = Bun.spawn( |
| 53 | + [ |
| 54 | + "bun", |
| 55 | + "-e", |
| 56 | + ` |
| 57 | + import { readImprovementHook } from "./packages/shared/improvement-hooks"; |
| 58 | + const result = readImprovementHook("enterplanmode-improve"); |
| 59 | + console.log(JSON.stringify(result)); |
| 60 | + `, |
| 61 | + ], |
| 62 | + { |
| 63 | + env: { ...process.env, HOME: TEST_HOME }, |
| 64 | + cwd: join(import.meta.dir, "../.."), |
| 65 | + stdout: "pipe", |
| 66 | + stderr: "pipe", |
| 67 | + }, |
| 68 | + ); |
| 69 | + |
| 70 | + const stdout = await new Response(proc.stdout).text(); |
| 71 | + const exitCode = await proc.exited; |
| 72 | + |
| 73 | + if (exitCode !== 0) { |
| 74 | + const stderr = await new Response(proc.stderr).text(); |
| 75 | + throw new Error(`Subprocess failed (exit ${exitCode}): ${stderr}`); |
| 76 | + } |
| 77 | + |
| 78 | + const parsed = JSON.parse(stdout.trim()); |
| 79 | + return parsed; |
| 80 | +} |
| 81 | + |
| 82 | +describe("readImprovementHook", () => { |
| 83 | + beforeEach(setupTestHome); |
| 84 | + afterEach(cleanTestHome); |
| 85 | + |
| 86 | + test("returns content from new path when file exists", async () => { |
| 87 | + const result = await runScenario({ |
| 88 | + newPathContent: "Focus on error handling", |
| 89 | + }); |
| 90 | + expect(result).not.toBeNull(); |
| 91 | + expect(result!.content).toBe("Focus on error handling"); |
| 92 | + expect(result!.filePath).toContain(".plannotator/hooks/compound/"); |
| 93 | + }); |
| 94 | + |
| 95 | + test("new path wins over legacy path", async () => { |
| 96 | + const result = await runScenario({ |
| 97 | + newPathContent: "New instructions", |
| 98 | + legacyPathContent: "Old instructions", |
| 99 | + }); |
| 100 | + expect(result).not.toBeNull(); |
| 101 | + expect(result!.content).toBe("New instructions"); |
| 102 | + expect(result!.filePath).toContain(".plannotator/hooks/compound/"); |
| 103 | + }); |
| 104 | + |
| 105 | + test("falls back to legacy path when new path is absent", async () => { |
| 106 | + const result = await runScenario({ |
| 107 | + legacyPathContent: "Legacy instructions", |
| 108 | + }); |
| 109 | + expect(result).not.toBeNull(); |
| 110 | + expect(result!.content).toBe("Legacy instructions"); |
| 111 | + expect(result!.filePath).toContain(".plannotator/compound/"); |
| 112 | + expect(result!.filePath).not.toContain(".plannotator/hooks/"); |
| 113 | + }); |
| 114 | + |
| 115 | + test("returns null when new path exists but is empty (no legacy fallback)", async () => { |
| 116 | + const result = await runScenario({ |
| 117 | + newPathContent: "", |
| 118 | + legacyPathContent: "Legacy instructions", |
| 119 | + }); |
| 120 | + expect(result).toBeNull(); |
| 121 | + }); |
| 122 | + |
| 123 | + test("returns null when no files exist", async () => { |
| 124 | + const result = await runScenario({}); |
| 125 | + expect(result).toBeNull(); |
| 126 | + }); |
| 127 | + |
| 128 | + test("returns null when new path is whitespace-only (no legacy fallback)", async () => { |
| 129 | + const result = await runScenario({ |
| 130 | + newPathContent: " \n \n ", |
| 131 | + legacyPathContent: "Legacy instructions", |
| 132 | + }); |
| 133 | + expect(result).toBeNull(); |
| 134 | + }); |
| 135 | +}); |
0 commit comments