|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + renderPlanAsJson, |
| 4 | + renderPlanAsMarkdown, |
| 5 | + type PlanDag, |
| 6 | + type PlanStep, |
| 7 | +} from "../../packages/gittensory-engine/src/plan-export"; |
| 8 | + |
| 9 | +function step(over: Partial<PlanStep> & { id: string; title: string }): PlanStep { |
| 10 | + return { |
| 11 | + actionClass: undefined, |
| 12 | + dependsOn: [], |
| 13 | + status: "pending", |
| 14 | + attempts: 0, |
| 15 | + maxAttempts: 3, |
| 16 | + lastError: null, |
| 17 | + ...over, |
| 18 | + }; |
| 19 | +} |
| 20 | + |
| 21 | +describe("renderPlanAsMarkdown", () => { |
| 22 | + it("renders an empty plan with a placeholder", () => { |
| 23 | + expect(renderPlanAsMarkdown({ steps: [] })).toBe("_No steps in this plan._"); |
| 24 | + }); |
| 25 | + |
| 26 | + it("checks completed steps, leaves others unchecked, and shows status", () => { |
| 27 | + const md = renderPlanAsMarkdown({ |
| 28 | + steps: [ |
| 29 | + step({ id: "a", title: "Build", status: "completed" }), |
| 30 | + step({ id: "b", title: "Test", status: "pending", dependsOn: ["a"] }), |
| 31 | + ], |
| 32 | + }); |
| 33 | + expect(md).toBe("- [x] Build — completed\n- [ ] Test — pending"); |
| 34 | + }); |
| 35 | + |
| 36 | + it("annotates attempts only after a step has run and appends the last error", () => { |
| 37 | + const md = renderPlanAsMarkdown({ |
| 38 | + steps: [ |
| 39 | + step({ id: "a", title: "Deploy", status: "failed", attempts: 2, maxAttempts: 2, lastError: "boom" }), |
| 40 | + step({ id: "b", title: "Wait", status: "pending", attempts: 0 }), |
| 41 | + ], |
| 42 | + }); |
| 43 | + expect(md).toBe("- [ ] Deploy — failed (attempt 2/2): boom\n- [ ] Wait — pending"); |
| 44 | + }); |
| 45 | + |
| 46 | + it("orders steps by dependency so a dependency precedes its dependents", () => { |
| 47 | + const md = renderPlanAsMarkdown({ |
| 48 | + steps: [ |
| 49 | + step({ id: "b", title: "B", dependsOn: ["a"] }), |
| 50 | + step({ id: "a", title: "A" }), |
| 51 | + ], |
| 52 | + }); |
| 53 | + expect(md).toBe("- [ ] A — pending\n- [ ] B — pending"); |
| 54 | + }); |
| 55 | + |
| 56 | + it("treats an unknown dependency id as already satisfied", () => { |
| 57 | + const md = renderPlanAsMarkdown({ steps: [step({ id: "a", title: "A", dependsOn: ["ghost"] })] }); |
| 58 | + expect(md).toBe("- [ ] A — pending"); |
| 59 | + }); |
| 60 | + |
| 61 | + it("appends steps caught in a dependency cycle in original order instead of dropping or looping", () => { |
| 62 | + const md = renderPlanAsMarkdown({ |
| 63 | + steps: [ |
| 64 | + step({ id: "a", title: "A", dependsOn: ["b"] }), |
| 65 | + step({ id: "b", title: "B", dependsOn: ["a"] }), |
| 66 | + ], |
| 67 | + }); |
| 68 | + expect(md.split("\n")).toEqual(["- [ ] A — pending", "- [ ] B — pending"]); |
| 69 | + }); |
| 70 | + |
| 71 | + it("keeps each step on one line when a title or lastError contains newlines", () => { |
| 72 | + const md = renderPlanAsMarkdown({ |
| 73 | + steps: [step({ id: "a", title: "Line one\nLine two", status: "failed", attempts: 1, maxAttempts: 3, lastError: "err one\r\nerr two" })], |
| 74 | + }); |
| 75 | + expect(md.split("\n")).toHaveLength(1); |
| 76 | + expect(md).toBe("- [ ] Line one Line two — failed (attempt 1/3): err one err two"); |
| 77 | + }); |
| 78 | + |
| 79 | + it("escapes Markdown control characters in a title or lastError so they cannot re-style the line", () => { |
| 80 | + const md = renderPlanAsMarkdown({ |
| 81 | + steps: [step({ id: "a", title: "drop `table` [x] *now*", status: "failed", attempts: 1, maxAttempts: 2, lastError: "path a\\b <tag> | ~x~" })], |
| 82 | + }); |
| 83 | + expect(md.split("\n")).toHaveLength(1); |
| 84 | + expect(md).toBe("- [ ] drop \\`table\\` \\[x\\] \\*now\\* — failed (attempt 1/2): path a\\\\b \\<tag\\> \\| \\~x\\~"); |
| 85 | + }); |
| 86 | +}); |
| 87 | + |
| 88 | +describe("renderPlanAsJson", () => { |
| 89 | + it("produces stable, key-sorted JSON that is byte-identical across renders of the same plan", () => { |
| 90 | + const plan: PlanDag = { steps: [step({ id: "z", title: "Z", status: "running", attempts: 1 })] }; |
| 91 | + const first = renderPlanAsJson(plan); |
| 92 | + expect(renderPlanAsJson(plan)).toBe(first); |
| 93 | + const parsedStep = JSON.parse(first).steps[0]; |
| 94 | + expect(Object.keys(parsedStep)).toEqual([...Object.keys(parsedStep)].sort()); |
| 95 | + }); |
| 96 | + |
| 97 | + it("sorts keys regardless of input insertion order but preserves the step array order", () => { |
| 98 | + const forward = renderPlanAsJson({ |
| 99 | + steps: [step({ id: "1", title: "One" }), step({ id: "2", title: "Two" })], |
| 100 | + }); |
| 101 | + const reorderedKeys = { |
| 102 | + title: "One", |
| 103 | + id: "1", |
| 104 | + maxAttempts: 3, |
| 105 | + attempts: 0, |
| 106 | + status: "pending", |
| 107 | + dependsOn: [], |
| 108 | + actionClass: undefined, |
| 109 | + lastError: null, |
| 110 | + } as PlanStep; |
| 111 | + const reordered = renderPlanAsJson({ steps: [reorderedKeys, step({ id: "2", title: "Two" })] }); |
| 112 | + expect(reordered).toBe(forward); |
| 113 | + expect(forward.indexOf('"id": "1"')).toBeLessThan(forward.indexOf('"id": "2"')); |
| 114 | + }); |
| 115 | +}); |
0 commit comments