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