|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +const path = require("path"); |
| 4 | +const fs = require("fs"); |
| 5 | +const { |
| 6 | + assemble, |
| 7 | + loadComponent, |
| 8 | + stripFrontmatter, |
| 9 | +} = require("../lib/assemble"); |
| 10 | +const { loadManifest, getTemplates } = require("../lib/manifest"); |
| 11 | + |
| 12 | +const fixtureDir = path.resolve(__dirname, "fixtures"); |
| 13 | + |
| 14 | +describe("assemble", () => { |
| 15 | + describe("stripFrontmatter", () => { |
| 16 | + test("removes YAML frontmatter delimited by ---", () => { |
| 17 | + const input = "---\nname: test\ntype: foo\n---\n# Body\n\nContent here."; |
| 18 | + expect(stripFrontmatter(input)).toBe("# Body\n\nContent here."); |
| 19 | + }); |
| 20 | + |
| 21 | + test("returns content unchanged when no frontmatter present", () => { |
| 22 | + const input = "# Just a heading\n\nSome content."; |
| 23 | + expect(stripFrontmatter(input)).toBe(input); |
| 24 | + }); |
| 25 | + |
| 26 | + test("handles frontmatter with Windows-style line endings", () => { |
| 27 | + const input = "---\r\nname: test\r\n---\r\n# Body"; |
| 28 | + expect(stripFrontmatter(input)).toBe("# Body"); |
| 29 | + }); |
| 30 | + |
| 31 | + test("trims leading/trailing whitespace after stripping", () => { |
| 32 | + const input = "---\nname: test\n---\n\n # Body \n\n"; |
| 33 | + expect(stripFrontmatter(input)).toBe("# Body"); |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + describe("loadComponent", () => { |
| 38 | + test("loads a persona and strips HTML comments and frontmatter", () => { |
| 39 | + const body = loadComponent(fixtureDir, "personas/test-engineer.md"); |
| 40 | + expect(body).not.toContain("SPDX-License-Identifier"); |
| 41 | + expect(body).not.toContain("---"); |
| 42 | + expect(body).toContain("# Persona: Test Engineer"); |
| 43 | + expect(body).toContain("You are a test engineer"); |
| 44 | + }); |
| 45 | + |
| 46 | + test("loads a protocol and strips HTML comments and frontmatter", () => { |
| 47 | + const body = loadComponent( |
| 48 | + fixtureDir, |
| 49 | + "protocols/guardrails/anti-hallucination.md" |
| 50 | + ); |
| 51 | + expect(body).not.toContain("SPDX-License-Identifier"); |
| 52 | + expect(body).toContain("# Protocol: Anti-Hallucination"); |
| 53 | + expect(body).toContain("KNOWN, INFERRED, or ASSUMED"); |
| 54 | + }); |
| 55 | + |
| 56 | + test("returns null for missing component and warns", () => { |
| 57 | + const warnSpy = jest.spyOn(console, "warn").mockImplementation(); |
| 58 | + const result = loadComponent(fixtureDir, "nonexistent.md"); |
| 59 | + |
| 60 | + expect(result).toBeNull(); |
| 61 | + expect(warnSpy).toHaveBeenCalledWith( |
| 62 | + expect.stringContaining("nonexistent.md") |
| 63 | + ); |
| 64 | + warnSpy.mockRestore(); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + describe("assemble (full)", () => { |
| 69 | + let manifest; |
| 70 | + |
| 71 | + beforeAll(() => { |
| 72 | + manifest = loadManifest(fixtureDir); |
| 73 | + }); |
| 74 | + |
| 75 | + test("assembles a complete prompt with all sections", () => { |
| 76 | + const templates = getTemplates(manifest); |
| 77 | + const bug = templates.find((t) => t.name === "investigate-bug"); |
| 78 | + const result = assemble(fixtureDir, manifest, bug); |
| 79 | + |
| 80 | + // Check section headers are present in order |
| 81 | + expect(result).toContain("# Identity"); |
| 82 | + expect(result).toContain("# Reasoning Protocols"); |
| 83 | + expect(result).toContain("# Output Format"); |
| 84 | + expect(result).toContain("# Task"); |
| 85 | + |
| 86 | + // Verify section order |
| 87 | + const identityPos = result.indexOf("# Identity"); |
| 88 | + const protocolsPos = result.indexOf("# Reasoning Protocols"); |
| 89 | + const formatPos = result.indexOf("# Output Format"); |
| 90 | + const taskPos = result.indexOf("# Task"); |
| 91 | + expect(identityPos).toBeLessThan(protocolsPos); |
| 92 | + expect(protocolsPos).toBeLessThan(formatPos); |
| 93 | + expect(formatPos).toBeLessThan(taskPos); |
| 94 | + }); |
| 95 | + |
| 96 | + test("includes persona body text verbatim", () => { |
| 97 | + const templates = getTemplates(manifest); |
| 98 | + const bug = templates.find((t) => t.name === "investigate-bug"); |
| 99 | + const result = assemble(fixtureDir, manifest, bug); |
| 100 | + |
| 101 | + expect(result).toContain("# Persona: Test Engineer"); |
| 102 | + expect(result).toContain("You are a test engineer who writes thorough tests."); |
| 103 | + }); |
| 104 | + |
| 105 | + test("includes all protocol bodies", () => { |
| 106 | + const templates = getTemplates(manifest); |
| 107 | + const bug = templates.find((t) => t.name === "investigate-bug"); |
| 108 | + const result = assemble(fixtureDir, manifest, bug); |
| 109 | + |
| 110 | + expect(result).toContain("# Protocol: Anti-Hallucination"); |
| 111 | + expect(result).toContain("# Protocol: Root Cause Analysis"); |
| 112 | + }); |
| 113 | + |
| 114 | + test("includes taxonomy when referenced", () => { |
| 115 | + const templates = getTemplates(manifest); |
| 116 | + const review = templates.find((t) => t.name === "review-code"); |
| 117 | + const result = assemble(fixtureDir, manifest, review); |
| 118 | + |
| 119 | + expect(result).toContain("# Classification Taxonomy"); |
| 120 | + expect(result).toContain("# Taxonomy: Stack Lifetime Hazards"); |
| 121 | + }); |
| 122 | + |
| 123 | + test("omits taxonomy section when not referenced", () => { |
| 124 | + const templates = getTemplates(manifest); |
| 125 | + const bug = templates.find((t) => t.name === "investigate-bug"); |
| 126 | + const result = assemble(fixtureDir, manifest, bug); |
| 127 | + |
| 128 | + expect(result).not.toContain("# Classification Taxonomy"); |
| 129 | + }); |
| 130 | + |
| 131 | + test("includes format body text", () => { |
| 132 | + const templates = getTemplates(manifest); |
| 133 | + const bug = templates.find((t) => t.name === "investigate-bug"); |
| 134 | + const result = assemble(fixtureDir, manifest, bug); |
| 135 | + |
| 136 | + expect(result).toContain("# Format: Investigation Report"); |
| 137 | + expect(result).toContain("## Findings"); |
| 138 | + }); |
| 139 | + |
| 140 | + test("includes template task body", () => { |
| 141 | + const templates = getTemplates(manifest); |
| 142 | + const bug = templates.find((t) => t.name === "investigate-bug"); |
| 143 | + const result = assemble(fixtureDir, manifest, bug); |
| 144 | + |
| 145 | + expect(result).toContain("# Task: Investigate Bug"); |
| 146 | + expect(result).toContain("{{bug_description}}"); |
| 147 | + }); |
| 148 | + |
| 149 | + test("substitutes parameters when provided", () => { |
| 150 | + const templates = getTemplates(manifest); |
| 151 | + const bug = templates.find((t) => t.name === "investigate-bug"); |
| 152 | + const result = assemble(fixtureDir, manifest, bug, { |
| 153 | + bug_description: "Use-after-free in socket handler", |
| 154 | + context: "See socket.c line 42", |
| 155 | + }); |
| 156 | + |
| 157 | + expect(result).toContain("Use-after-free in socket handler"); |
| 158 | + expect(result).toContain("See socket.c line 42"); |
| 159 | + expect(result).not.toContain("{{bug_description}}"); |
| 160 | + expect(result).not.toContain("{{context}}"); |
| 161 | + }); |
| 162 | + |
| 163 | + test("leaves unfilled params as placeholders", () => { |
| 164 | + const templates = getTemplates(manifest); |
| 165 | + const bug = templates.find((t) => t.name === "investigate-bug"); |
| 166 | + const result = assemble(fixtureDir, manifest, bug, { |
| 167 | + bug_description: "memory leak", |
| 168 | + }); |
| 169 | + |
| 170 | + expect(result).toContain("memory leak"); |
| 171 | + expect(result).toContain("{{context}}"); |
| 172 | + }); |
| 173 | + |
| 174 | + test("does not contain YAML frontmatter or SPDX headers", () => { |
| 175 | + const templates = getTemplates(manifest); |
| 176 | + const bug = templates.find((t) => t.name === "investigate-bug"); |
| 177 | + const result = assemble(fixtureDir, manifest, bug); |
| 178 | + |
| 179 | + expect(result).not.toContain("SPDX-License-Identifier"); |
| 180 | + // Section dividers (---) are expected; YAML frontmatter (---\nkey: val\n---) is not |
| 181 | + expect(result).not.toMatch(/^---\r?\n\w+:/m); |
| 182 | + }); |
| 183 | + |
| 184 | + test("separates sections with --- dividers", () => { |
| 185 | + const templates = getTemplates(manifest); |
| 186 | + const bug = templates.find((t) => t.name === "investigate-bug"); |
| 187 | + const result = assemble(fixtureDir, manifest, bug); |
| 188 | + |
| 189 | + // Sections are joined with \n\n---\n\n |
| 190 | + const sections = result.split("\n\n---\n\n"); |
| 191 | + expect(sections.length).toBeGreaterThanOrEqual(4); |
| 192 | + }); |
| 193 | + }); |
| 194 | +}); |
0 commit comments