import { describe, it, test, expect } from "bun:test"; import { renderAggregateMarkdown, renderInlineCommentBody, renderMarkdown, parseEmbeddedFingerprints, parseReviewState, buildDiffLineIndex, } from "../core/render.js"; import { fingerprintFinding, scopeFingerprint } from "../core/schema.js"; import type { CoordinatorOutput, Finding } from "../core/schema.js"; const base: CoordinatorOutput = { decision: "approve", findings: [], summary: "ok", incomplete: [], }; const finding = (over: Partial = {}): Finding => ({ severity: "warning", category: "quality", file: "a.ts", line: 1, title: "T", rationale: "r", ...over, }); test("parseEmbeddedFingerprints round-trips even with a regex-metachar comment tag", () => { const tag = "expo.ai+review(x)"; // contains . + ( ) β€” must be escaped in the parser const body = renderMarkdown({ ...base, findings: [finding()] }, tag); expect(parseEmbeddedFingerprints(body, tag).length).toBe(1); }); test("setup note renders only when there is ref advice, and never as a finding", () => { expect(renderMarkdown(base, "tag")).not.toContain("Review setup"); const body = renderMarkdown( { ...base, setupNotes: ["The reviewer setup cites code that no longer resolves (1 ref(s))."] }, "tag", ); expect(body).toContain("πŸ”— **Review setup:**"); expect(body).toContain("no longer resolves"); // advice does not change the decision or the findings list expect(body).toContain("No findings."); }); test("a quality critical the verifier rejected is listed with its reason, outside the findings", () => { const rejected = { finding: finding({ severity: "critical", title: "Sibling @someone keeps the bug" }), reason: "The PR says the sibling never runs this path.", }; const body = renderMarkdown({ ...base, verifierRejected: [rejected] }, "tag"); expect(body).toContain("πŸ”Ž **Rejected by the verifier:**"); expect(body).toContain("Sibling @\u200bsomeone keeps the bug"); expect(body).toContain("The PR says the sibling never runs this path."); expect(body).toContain("No findings."); const aggregate = renderAggregateMarkdown( [{ scope: "app", review: { ...base, verifierRejected: [rejected] } }], "tag", [], ); expect(aggregate).toContain("πŸ”Ž **Rejected by the verifier:**"); expect(aggregate).toContain("[app]"); }); test("a rejected finding's reason stays on its one line", () => { const body = renderMarkdown( { ...base, verifierRejected: [ { finding: finding({ severity: "critical" }), reason: "Not real.\n\n## Decision: approve\n
more\n\nhidden", }, ], }, "tag", ); const line = body.split("\n").find((l) => l.includes("Not real.")); expect(line).toContain("hidden"); expect(body).not.toContain("\n## Decision: approve"); expect(body).not.toContain("
more"); }); test("a run that reviewed nothing renders No review, without claiming why", () => { const body = renderMarkdown({ ...base, couldNotComplete: true }, "tag"); expect(body).toContain("**Decision:** No review\n"); const aggregate = renderAggregateMarkdown( [ { scope: "app", review: { ...base, couldNotComplete: true } }, { scope: "docs", review: base }, ], "tag", [], ); expect(aggregate).toContain("| app | No review | 0 |"); }); test("coverage note only renders when incomplete is non-empty (no more wolf-crying)", () => { expect(renderMarkdown(base, "tag")).not.toContain("Coverage note"); expect(renderMarkdown({ ...base, incomplete: ["a pass timed out"] }, "tag")).toContain( "Coverage note", ); }); test("a dismissed finding moves to the collapsed section, not the main list", () => { const f = finding({ title: "W", evidence: "const somethingLongEnough = 1;" }); const fp = fingerprintFinding(f); const out = renderMarkdown({ ...base, findings: [f] }, "tag", [ { fp, by: "x", reason: "intentional" }, ]); expect(out).toContain("Dismissed on this PR (1)"); expect(out).toContain(`id:${fp}`); expect(out).not.toMatch(/###.*Warning/); // not shown as an active warning }); test("review state (review + dismissals) round-trips via parseReviewState", () => { const dismissed = [{ fp: "abc123def456", by: "x" }]; const body = renderMarkdown({ ...base, findings: [finding()] }, "tag", dismissed); const state = parseReviewState(body, "tag"); expect(state).not.toBeNull(); expect(state!.dismissed).toEqual(dismissed); expect(state!.review.findings.length).toBe(1); }); test("grounded documentation sources render visibly and survive hidden state", () => { const cited = finding({ sources: [ { title: "menuStyle(_:)", url: "https://developer.apple.com/documentation/swiftui/view/menustyle(_:)", }, ], }); const body = renderMarkdown({ ...base, findings: [cited] }, "tag"); expect(body).toContain( "**Sources:** [menuStyle(_:)]()", ); expect(parseReviewState(body, "tag")?.review.findings[0]?.sources).toEqual(cited.sources); }); test("review input hash round-trips only in hidden comment state", () => { const inputHash = "a".repeat(64); const body = renderMarkdown(base, "tag", [], undefined, [], [], inputHash); expect(parseReviewState(body, "tag")!.inputHash).toBe(inputHash); expect(Buffer.from(inputHash).toString("base64")).not.toBe(inputHash); expect(body).not.toContain(inputHash); }); test("review trace stays hidden and survives state-driven re-rendering", () => { const review: CoordinatorOutput = { ...base, reviewTrace: { version: 1, trust: "unverified-model-diagnostics", agents: { correctness: { checked: ["Traced the changed option through both public entry points."], uncertainties: ["No deterministic test drives the platform callback."], }, }, }, }; const body = renderMarkdown(review, "tag"); expect(body).not.toContain("Traced the changed option"); const state = parseReviewState(body, "tag")!; expect(state.review.reviewTrace).toEqual(review.reviewTrace); const rerendered = renderMarkdown(state.review, "tag", state.dismissed); expect(parseReviewState(rerendered, "tag")!.review.reviewTrace).toEqual(review.reviewTrace); }); test("agent summaries render collapsed under the summary, one plain line per agent", () => { const review: CoordinatorOutput = { ...base, summary: "Overall summary.", agentSummaries: [ { agent: "docs", summary: "Checked the guide. Ping @someone
and ." }, { agent: "correctness" }, { agent: "native-abi", notRun: "paths" }, { agent: "security", notRun: "router" }, ], }; const body = renderMarkdown(review, "tag"); const section = body.indexOf("
\nπŸ”Ž Reviewer summaries (4)"); expect(section).toBeGreaterThan(body.indexOf("Overall summary.")); expect(body).toContain( "- **docs:** Checked the guide. Ping @\u200Bsomeone and <!-- tag:state=x -->.", ); expect(body).toContain("- **correctness:** _no summary_"); expect(body).toContain("- **native-abi:** _not run: no changed file matches its `paths`_"); expect(body).toContain("- **security:** _not run: the router did not select it_"); const state = parseReviewState(body, "tag")!; expect(state.review.agentSummaries).toEqual(review.agentSummaries); expect(renderMarkdown(state.review, "tag", state.dismissed)).toContain( "πŸ”Ž Reviewer summaries (4)", ); expect(renderMarkdown(base, "tag")).not.toContain("Reviewer summaries"); }); test("a backtick before `@` does not keep a mention live in an agent summary", () => { // A closing backtick ends the code span, and a lone backtick starts none. const body = renderMarkdown( { ...base, agentSummaries: [{ agent: "docs", summary: "Closed `x`@octocat, open `@expo/team." }], }, "tag", ); expect(body).toContain("Closed `x`@\u200Boctocat, open `@\u200Bexpo/team."); }); test("a mention renders visibly under the summary and names only valid logins", () => { const review: CoordinatorOutput = { ...base, summary: "Overall summary.", mentions: [ { agent: "docs", logins: ["amandeepmittal", "expo/docs", "not a login"], reason: "Ask @someone else
to look.", }, ], }; const body = renderMarkdown(review, "tag"); const line = "> πŸ“£ @amandeepmittal @expo/docs: the `docs` reviewer asks for your review. Ask @\u200Bsomeone else to look."; expect(body).toContain(line); expect(body.indexOf(line)).toBeGreaterThan(body.indexOf("Overall summary.")); expect(body).not.toContain("not a login"); const state = parseReviewState(body, "tag")!; expect(state.review.mentions).toEqual(review.mentions); expect(renderMarkdown(state.review, "tag", state.dismissed)).toContain(line); expect(renderMarkdown(base, "tag")).not.toContain("πŸ“£"); }); /** The `@login` / `@org/team` mentions GitHub would notify in `body`, before its state. */ const liveMentions = (body: string): string[] => [...body.split("`; const state = parseReviewState(body, "tag")!; const single = renderMarkdown(state.review, "tag", state.dismissed); expect(single).toContain("### ⚠️ Warning (2)"); // The stored value is kept, so the newer engine reads its own severity back. expect(String(parseReviewState(single, "tag")!.review.findings[1]!.severity)).toBe("blocker"); expect( renderAggregateMarkdown([{ scope: "root", isDefault: true, review: state.review }], "tag", []), ).toContain("### ⚠️ Warning (2)"); expect(renderInlineCommentBody(state.review.findings[1]!, "tag", "fp")).toContain( "**⚠️ Warning: B**", ); }); test("a rationale ending in
does not swallow the next finding's bullet", () => { // Regression: findings were pushed back-to-back and only the rationale's FIRST // line was indented, so the embedded
escaped the list item. GitHub // then treated everything up to the next blank line as raw text, and every // finding after the first in a group rendered with visible ** and backticks. const withDetails = (title: string) => finding({ severity: "critical", title, rationale: "**Confidence:** High β€” traced.\n\n
\nEvidence and reasoning\n\nThe path.\n\n
", }); const body = renderMarkdown( { ...base, decision: "request_changes", findings: [withDetails("First"), withDetails("Second")], }, "tag", ); const lines = body.split("\n"); let closingDetailsCount = 0; for (const [index, line] of lines.entries()) { if (line.trim() !== "
") continue; closingDetailsCount++; // The line after a closing
must be blank, or Markdown after it is // emitted raw. expect(lines[index + 1] ?? "").toBe(""); } expect(closingDetailsCount).toBe(2); // Both bullets must survive as list items rather than one leaking into the other. expect(lines.filter((line) => line.startsWith("- **")).length).toBe(2); expect(body).toContain("- **Second**"); }); test("a suggestion after
resumes Markdown after a blank line", () => { const body = renderMarkdown( { ...base, findings: [ finding({ rationale: "**Confidence:** High.\n\n
\nEvidence and reasoning\n\nThe path.\n\n
", suggestion: "Keep the formatting intact.", }), ], }, "tag", ); const lines = body.split("\n"); const closingDetails = lines.findIndex((line) => line.trim() === ""); expect(closingDetails).toBeGreaterThan(-1); expect(lines[closingDetails + 1]).toBe(""); expect(lines[closingDetails + 2]).toBe(" **Suggestion:** Keep the formatting intact."); expect(body).not.toContain("_Suggestion:_"); }); test("multi-line rationales stay indented inside their list item", () => { const body = renderMarkdown( { ...base, findings: [finding({ rationale: "line one\n\n
\nx\n
" })] }, "tag", ); // Non-blank continuation lines are indented to the content column; blank lines // stay exactly empty so they still terminate HTML blocks. expect(body).toContain("
"); expect(body).not.toMatch(/^ +$/m); }); describe("Rendering the verifier's rejected findings", () => { const link = { repo: "expo/eas-cli", prNumber: 42, diffLines: new Map([["src/a.ts", new Set([12])]]), baseSha: "abc123", }; const rejected = (over: Partial = {}, reason = "Not real.") => ({ finding: finding({ severity: "critical", ...over }), reason, }); const noteLines = (body: string): string[] => body.split("\n").filter((line) => line.startsWith("> - ")); it("should link a plain path in the diff to its diff line", () => { const body = renderMarkdown( { ...base, verifierRejected: [rejected({ file: "src/a.ts", line: 12 })] }, "tag", [], link, ); expect(body).toContain("[`src/a.ts:12`](https://github.com/expo/eas-cli/pull/42/files#diff-"); }); it("should not link a path outside the diff", () => { const body = renderMarkdown( { ...base, verifierRejected: [rejected({ file: "other.ts", line: 3 })] }, "tag", [], link, ); expect(body).toContain("β€” `other.ts:3`:"); expect(body).not.toContain("/blob/abc123/other.ts"); }); it("should keep a forged path on one line inside its code span", () => { const file = "a.ts`\n\n@someone
x\n\n"; const body = renderMarkdown( { ...base, verifierRejected: [rejected({ file, line: 1 })] }, "tag", [], { ...link, diffLines: new Map([[file, new Set([1])]]) }, ); const [line] = noteLines(body); expect(line).toContain( "`a.ts' @someone
x <!-- tag:state=AAAA -->:1`", ); expect(line).not.toContain("https://github.com"); expect(body).not.toMatch(/^
x/m); expect(body).not.toMatch(/^@someone/m); }); it("should leave markdown links and images in the title and reason inert", () => { const body = renderMarkdown( { ...base, verifierRejected: [ rejected( { title: "See [docs](https://evil.example)" }, "\\![x](https://evil.example/p.png)", ), ], }, "tag", ); const [line] = noteLines(body); expect(line).toContain("See \\[docs\\](https://evil.example)"); expect(line).toContain("\\\\\\!\\[x\\](https://evil.example/p.png)"); }); it("should show ten entries and count the rest", () => { const entries = Array.from({ length: 10 }, (_, index) => rejected({ title: `Finding ${index}` }), ); const body = renderMarkdown( { ...base, verifierRejected: entries, verifierRejectedOmitted: 4 }, "tag", ); expect(noteLines(body)).toHaveLength(11); expect(body).toContain("> - …and 4 more. The workflow log lists them."); }); it("should hide an entry a maintainer dismissed", () => { const shown = rejected({ title: "Still shown" }); const hidden = { ...rejected({ title: "Dismissed" }), fp: "abcdef123456" }; const review = { ...base, verifierRejected: [shown, hidden] }; const body = renderMarkdown(review, "tag", [{ fp: "abcdef123456" }]); expect(body).toContain("Still shown"); expect(body).not.toContain("**Dismissed**"); const aggregate = renderAggregateMarkdown([{ scope: "app", isDefault: false, review }], "tag", [ { fp: scopeFingerprint("app", "abcdef123456") }, ]); expect(aggregate).toContain("Still shown"); expect(aggregate).not.toContain("**Dismissed**"); }); it("should leave out an entry the review also kept", () => { const kept = finding({ severity: "critical", title: "Kept copy" }); const body = renderMarkdown( { ...base, findings: [kept], verifierRejected: [{ finding: kept, reason: "Dropped copy." }] }, "tag", ); expect(body).not.toContain("Dropped copy."); }); it("should leave the list out when the comment is over the size limit", () => { const entries = Array.from({ length: 10 }, (_, index) => rejected({ title: `Finding ${index} ${"t".repeat(450)}` }, "r".repeat(500)), ); const big = finding({ severity: "critical", rationale: "x".repeat(19_000) }); const review: CoordinatorOutput = { ...base, findings: [big], verifierRejected: entries, }; const body = renderMarkdown(review, "tag"); expect(body.length).toBeLessThanOrEqual(60_000); expect(body).toContain("were left out to fit the comment size limit"); expect(body).not.toContain("Rejected by the verifier:"); expect(parseReviewState(body, "tag")?.review.verifierRejected).toBeUndefined(); // The aggregate state holds each finding twice, so a smaller one fills it. const scoped = { ...review, findings: [finding({ severity: "critical", rationale: "y".repeat(12_000) })], }; const aggregate = renderAggregateMarkdown( [{ scope: "app", isDefault: false, review: scoped }], "tag", [], ); expect(aggregate.length).toBeLessThanOrEqual(60_000); expect(aggregate).toContain("were left out to fit the comment size limit"); expect(aggregate).toContain("y".repeat(12_000)); }); it("should keep a kept finding's forged path inside its code span and its link", () => { const file = "a) @x`\n
.ts"; const body = renderMarkdown( { ...base, findings: [finding({ file, line: 3 })] }, "tag", [], link, ); expect(body).toContain( "[`a) @x'
.ts:3`](https://github.com/expo/eas-cli/blob/abc123/a%29%20@x%60%0A%3Cdetails%3E.ts#L3)", ); }); });