import { describe, expect, test } from "bun:test" import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs" import { tmpdir } from "node:os" import { dirname, join } from "node:path" import type { PluginInput } from "@opencode-ai/plugin" import { unsafeTestValue } from "../../../../../test-support/unsafe-test-value" import { createPlanFormatValidatorHook } from "./hook" type PlanFormatValidatorHook = ReturnType type ToolExecuteAfter = NonNullable type HookInput = Parameters[0] type HookOutput = Parameters[1] type FixtureOptions = { readonly filePath?: string readonly initialOutput?: string readonly tool?: string } function createFixture(content: string, options: FixtureOptions = {}) { const directory = mkdtempSync(join(tmpdir(), "plan-format-validator-")) const filePath = options.filePath ?? ".omo/plans/plan.md" const resolvedPath = join(directory, filePath) mkdirSync(dirname(resolvedPath), { recursive: true }) writeFileSync(resolvedPath, content, "utf-8") const hook = createPlanFormatValidatorHook(unsafeTestValue({ directory })) const input: HookInput = { tool: options.tool ?? "Write", sessionID: "ses_plan-format-validator", callID: "call_plan-format-validator", args: { filePath }, } const output: HookOutput = { title: "plan.md", output: options.initialOutput ?? "write complete", metadata: {}, } return { output, resolvedPath, async run(): Promise { await hook["tool.execute.after"](input, output) }, cleanup(): void { rmSync(directory, { recursive: true, force: true }) }, } } describe("plan-format-validator", () => { test("appends a warning when a recognized Todos section contains only prose", async () => { // given const fixture = createFixture(`# Plan\n\n## Todos\nDescribe the implementation work here.`) try { // when await fixture.run() // then expect(fixture.output.output).toContain("") } finally { fixture.cleanup() } }) test("appends a warning when a recognized Final Verification Wave contains only prose", async () => { // given const fixture = createFixture(`# Plan\n\n## Final Verification Wave\nDescribe the final checks here.`) try { // when await fixture.run() // then expect(fixture.output.output).toContain("") } finally { fixture.cleanup() } }) test("warns when a valid Todos section masks a prose-only final wave", async () => { // given const fixture = createFixture(`# Plan ## Todos - [ ] 1. Implement the change ## Final Verification Wave Describe the final checks here. `) try { // when await fixture.run() // then expect(fixture.output.output).toContain("") } finally { fixture.cleanup() } }) test("warns when a later valid Todos section masks an earlier prose-only Todos section", async () => { // given const fixture = createFixture(`# Plan ## TODOs Describe the first implementation section here. ## TODOs - [ ] 1. Implement the change `) try { // when await fixture.run() // then expect(fixture.output.output).toContain("") } finally { fixture.cleanup() } }) test("preserves a valid structured plan", async () => { // given const fixture = createFixture(`# Plan ## Todos - [ ] 1. Implement the change - [x] 2. Review the change ## Final Verification Wave - [ ] F1. Run the focused tests `) const originalOutput = fixture.output.output try { // when await fixture.run() // then expect(fixture.output.output).toBe(originalOutput) } finally { fixture.cleanup() } }) test("normalizes an Effort line that carries an hour count to a band", async () => { // given const fixture = createFixture(`# Plan ## TL;DR (For humans) **What you'll get:** A settings page. **Effort:** 200 hours **Risk:** Low - isolated change ## Todos - [ ] 1. Implement the change ## Final Verification Wave - [ ] F1. Run the focused tests `) try { // when await fixture.run() // then const written = readFileSync(fixture.resolvedPath, "utf-8") expect(written).not.toContain("200 hours") expect(written).toContain("**Effort:** XL") expect(fixture.output.output).toContain("") expect(fixture.output.output).toContain("Effort") } finally { fixture.cleanup() } }) test("normalizes an Effort line that carries a day count to the matching band", async () => { // given const fixture = createFixture(`# Plan ## TL;DR (For humans) **Effort:** 3 days ## Todos - [ ] 1. Implement the change `) try { // when await fixture.run() // then const written = readFileSync(fixture.resolvedPath, "utf-8") expect(written).not.toContain("3 days") expect(written).toContain("**Effort:** Large") } finally { fixture.cleanup() } }) test("leaves a band-valued Effort line untouched", async () => { // given const content = `# Plan ## TL;DR (For humans) **Effort:** Medium ## Todos - [ ] 1. Implement the change ` const fixture = createFixture(content) const originalOutput = fixture.output.output try { // when await fixture.run() // then expect(readFileSync(fixture.resolvedPath, "utf-8")).toBe(content) expect(fixture.output.output).toBe(originalOutput) } finally { fixture.cleanup() } }) test("preserves task-ID rows and blocked rows the progress counter counts", async () => { // given const fixture = createFixture( [ "# Plan", "## TODOs", "- [x] T1.1 Build the parser", "- [~] T1.2 \u2014 Push, blocked on the user", "## Final Verification Wave", "- [ ] F1 \u2014 Plan compliance audit", "- [ ] H1 \u2014 User approval", ].join("\n"), ) try { // when await fixture.run() // then expect(fixture.output.output).toBe("write complete") } finally { fixture.cleanup() } }) test("warns when a structured plan has malformed checkboxes", async () => { // given const fixture = createFixture(`# Plan\n\n## Todos\n- [ ] missing a numeric task label`) try { // when await fixture.run() // then expect(fixture.output.output).toContain("") } finally { fixture.cleanup() } }) test("does not append a duplicate warning", async () => { // given const initialOutput = "write complete\nalready emitted" const fixture = createFixture(`# Plan\n\n## Todos\n- [ ] missing a numeric task label`, { initialOutput }) try { // when await fixture.run() // then expect(fixture.output.output).toBe(initialOutput) } finally { fixture.cleanup() } }) test("bypasses non-plan files", async () => { // given const fixture = createFixture(`# Notes\n- [ ] 1. This is not a plan`, { filePath: "notes.md" }) const originalOutput = fixture.output.output try { // when await fixture.run() // then expect(fixture.output.output).toBe(originalOutput) } finally { fixture.cleanup() } }) test("preserves legacy plans with no recognized structured headings", async () => { // given const fixture = createFixture(`# Legacy Plan\n- [ ] 1. Legacy task`) const originalOutput = fixture.output.output try { // when await fixture.run() // then expect(fixture.output.output).toBe(originalOutput) } finally { fixture.cleanup() } }) test("warns when a four-backtick fence contains the only apparent task row", async () => { // given const fixture = createFixture(`# Plan ## Todos \`\`\`\`md \`\`\`ts - [ ] 1. Fenced example \`\`\` \`\`\`\` `) try { // when await fixture.run() // then expect(fixture.output.output).toContain("") } finally { fixture.cleanup() } }) test("warns for prose-only sections under headings with ATX closing markers", async () => { // given const fixture = createFixture(`# Plan ## TODOs ## Describe implementation tasks here. ## Final Verification Wave ### Describe final checks here. `) try { // when await fixture.run() // then expect(fixture.output.output).toContain("") } finally { fixture.cleanup() } }) test("preserves a canonical task after inline backtick code", async () => { // given const fixture = createFixture(`# Plan ## Todos \`\`\`example\`\`\` - [ ] 1. Implement `) try { // when await fixture.run() // then expect(fixture.output.output).not.toContain("") } finally { fixture.cleanup() } }) test("preserves canonical tasks after a child heading inside Todos", async () => { // given const fixture = createFixture(`# Plan ## TODOs - [x] 1. First task ### Notes - [ ] 2. Second task `) const originalOutput = fixture.output.output try { // when await fixture.run() // then expect(fixture.output.output).toBe(originalOutput) } finally { fixture.cleanup() } }) })