Files
anonymous_github/test/review-pages.test.js
T
Thomas DurieuxandClaude Opus 5.5 023e3f9a51 fix(viewers): keep authors out of PR commits, branches and gist binaries
- Commit messages lose sign-off and co-author trailers, e-mail addresses
  and the fork owner in merge headlines; without the origin option merge
  headlines lose branch names too.
- Branch names are shown only with the origin option.
- Gists keep only images (hidden unless images are displayed); other
  binaries are never stored or served. Images share an 8 MB budget so the
  gist document stays under MongoDB's limit, and an unreadable image no
  longer fails the whole gist.
- Review threads match the diff side, and outdated comments go to the end
  of their file instead of an unrelated line.
- The diff parser no longer reads a removed '-- ' line as a file header.
- Each diff file renders as its own component from escaped HTML, so large
  diffs don't re-render on scroll or collapse.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-29 12:33:11 +00:00

167 lines
9.9 KiB
JavaScript

const { expect } = require("chai");
const { JSDOM } = require("jsdom");
const path = require("path");
const { pathToFileURL } = require("url");
const load = (name) => import(pathToFileURL(path.join(__dirname, "../public/script", name)).href);
describe("Reviewer page outlines and diffs", function () {
let outline, formatters, pages, dom, vue;
// The pages register their scroll listeners in the controller's effect scope.
const inScope = (fn) => { const scope = vue.effectScope(); scope.run(fn); return scope; };
before(async () => {
dom = new JSDOM("<!doctype html><body></body>", { pretendToBeVisual: true });
global.window = dom.window;
outline = await load("outline.js");
formatters = await load("formatters.js");
pages = await load("review-pages.js");
vue = await import("vue");
});
after(() => { delete global.window; dom.window.close(); });
it("splits redaction masks into slot parts", () => {
expect(outline.redactParts("Results from XXXX-2 cluster")).to.deep.equal([{ t: "Results from " }, { n: "2" }, { t: " cluster" }]);
expect(outline.redactParts("")).to.deep.equal([]);
});
it("outlines h1-h3 with redaction slots read back from the chips", () => {
const html = '<h1 id="t">Guide</h1><h2 id="a">Setup</h2><h3 id="b">Data</h3><h4 id="c">Skip</h4>'
+ '<h2 id="r">Results from <mark class="redact-chip" data-term="XXXX-2"><i>2</i></mark> cluster</h2>';
const list = outline.headingOutline(html);
expect(list.map((h) => [h.id, h.level, h.depth])).to.deep.equal([["t", 1, 0], ["a", 2, 1], ["b", 3, 2], ["r", 2, 1]]);
expect(list[3].parts).to.deep.equal([{ t: "Results from " }, { n: "2" }, { t: " cluster" }]);
});
it("starts depth at the shallowest heading", () => {
const list = outline.headingOutline('<h2 id="a">A</h2><h3 id="b">B</h3>');
expect(list.map((h) => h.depth)).to.deep.equal([0, 1]);
});
it("highlights the last section above the reading line", () => {
const doc = dom.window.document;
doc.body.innerHTML = '<h2 id="one">1</h2><h2 id="two">2</h2><h2 id="three">3</h2>';
const tops = { one: -400, two: 120, three: 700 };
for (const id of Object.keys(tops)) doc.getElementById(id).getBoundingClientRect = () => ({ top: tops[id] });
const view = { innerHeight: 900, scrollY: 400 };
expect(outline.activeSection(["one", "two", "three"], { doc, view })).to.equal("two");
tops.two = 400;
expect(outline.activeSection(["one", "two", "three"], { doc, view })).to.equal("one");
tops.one = 50;
tops.two = 300; tops.three = 600;
expect(outline.activeSection(["one", "two", "three"], { doc, view: { innerHeight: 900, scrollY: 0 } })).to.equal("one");
});
it("parses a diff into files with status and counts, without a phantom last line", () => {
const files = formatters.parseDiff([
"diff --git a/a.py b/a.py", "--- a/a.py", "+++ b/a.py", "@@ -1,2 +1,2 @@", " keep", "-old", "+new",
"diff --git a/gone.sh b/gone.sh", "deleted file mode 100644", "--- a/gone.sh", "+++ /dev/null", "@@ -1,2 +0,0 @@", "-x", "-y",
"diff --git a/new.md b/new.md", "new file mode 100644", "--- /dev/null", "+++ b/new.md", "@@ -0,0 +1 @@", "+hi",
"\\ No newline at end of file", "",
].join("\n"));
expect(files.map((f) => [f.path, f.status, f.additions, f.deletions, f.lines.length])).to.deep.equal([
["a.py", "modified", 1, 1, 4], ["gone.sh", "deleted", 0, 2, 3], ["new.md", "added", 1, 0, 2],
]);
expect(files[0].lines[3]).to.include({ kind: "add", newNo: 2 });
});
it("keeps removed '-- ' and added '++ ' lines inside a hunk as content", () => {
const files = formatters.parseDiff([
"diff --git a/q.sql b/q.sql", "--- a/q.sql", "+++ b/q.sql", "@@ -1,3 +1,3 @@", " select 1;", "--- old comment", "+++ new counter", " end;",
"diff --git a/b.txt b/b.txt", "--- a/b.txt", "+++ b/b.txt", "@@ -1 +1 @@", "-x", "+y",
].join("\n"));
expect(files.map((f) => [f.path, f.additions, f.deletions])).to.deep.equal([["q.sql", 1, 1], ["b.txt", 1, 1]]);
expect(files[0].lines.map((l) => l.text)).to.deep.equal(["@@ -1,3 +1,3 @@", "select 1;", "-- old comment", "++ new counter", "end;"]);
});
it("places a LEFT thread on the removed line and a RIGHT one on the new line with the same number", () => {
const state = { details: {
diff: "--- a/a.py\n+++ b/a.py\n@@ -1,2 +1,2 @@\n-gone\n+added\n keep\n",
reviewComments: [{ path: "a.py", line: 1, side: "LEFT", comments: [{ body: "old" }] }, { path: "a.py", line: 1, side: "RIGHT", comments: [{ body: "new" }] }],
}, fmt: { date: (d) => d } };
const scope = inScope(() => pages.setupPullRequestPage(state));
const [file] = state.prFiles();
expect(file.lines.filter((l) => l.threads).map((l) => [l.kind, l.threads[0].comments[0].body])).to.deep.equal([["remove", "old"], ["add", "new"]]);
scope.stop();
});
it("renders the legacy diff HTML from the same parser", () => {
const html = formatters.diff("--- a/x\n+++ b/x\n@@ -1 +1 @@\n-a<b\n+b\n");
expect(html).to.include('diff-file-status-modified').and.include("a&lt;b");
expect((html.match(/<tr/g) || []).length).to.equal(3);
});
it("places review threads on their diff line and keeps the rest at the end of the file", () => {
const state = { details: {
diff: "--- a/src/a.py\n+++ b/src/a.py\n@@ -1,2 +1,3 @@\n one\n+two\n three\n",
reviewComments: [{ path: "src/a.py", line: 2, comments: [{ body: "here" }] }, { path: "src/a.py", line: 40, comments: [{ body: "elsewhere" }] }, { path: "other", line: 1, comments: [] }],
comments: [{ body: "c", creationDate: "2026-03-02" }],
commits: [{ message: "First\n\nBody", date: "2026-03-01" }, { message: "Second", date: "2026-03-01" }],
}, fmt: { date: (d) => d.slice(0, 10) } };
const scope = inScope(() => pages.setupPullRequestPage(state));
const [file] = state.prFiles();
expect(file).to.include({ path: "src/a.py", dir: "src/", name: "a.py", threads: 2, id: "diff-1" });
expect(file.lines.find((l) => l.threads)).to.include({ newNo: 2, text: "two" });
expect(file.looseThreads.map((t) => t.line)).to.deep.equal([40]);
expect(state.prStats()).to.deep.equal({ files: 1, additions: 1, deletions: 0 });
expect(state.diffBar(file)).to.deep.equal(["add", "add", "add", "add", "add"]);
expect(state.commitGroups()).to.deep.equal([{ day: "2026-03-01", commits: [
{ message: "First\n\nBody", date: "2026-03-01", index: 1, headline: "First", detail: "Body" },
{ message: "Second", date: "2026-03-01", index: 2, headline: "Second", detail: "" },
] }]);
expect(state.prTimeline().map((i) => i.kind)).to.deep.equal(["comment", "review", "review", "review"]);
state.toggleAllFiles();
expect(state.allCollapsed()).to.equal(true);
scope.stop();
});
it("opens a pull request on its conversation, or on the files without one", () => {
expect(pages.defaultPullRequestTab({ body: "x", diff: "d" })).to.equal("conversation");
expect(pages.defaultPullRequestTab({ diff: "d", comments: [] })).to.equal("files");
expect(pages.defaultPullRequestTab({ commits: [{}] })).to.equal("commits");
});
it("lists gist files with language, lines and nested Markdown headings", () => {
const state = { tabState: { active: "files" }, details: { files: [
{ filename: "NOTES-XXXX-1.md", language: "Markdown", content: "# A\n\n## B\n" },
{ filename: "run.sh", language: "Shell", content: "a\nb\nc\n" },
] } };
const scope = inScope(() => pages.setupGistPage(state));
state.setFileOutline(0, [{ id: "file-1-a", level: 1, depth: 0, parts: [{ t: "A" }] }]);
const toc = state.gistToc();
expect(toc.map((f) => [f.id, f.language, f.lines, f.headings.length])).to.deep.equal([["file-1", "Markdown", 3, 1], ["file-2", "Shell", 3, 0]]);
expect(toc[0].parts).to.deep.equal([{ t: "NOTES-" }, { n: "1" }, { t: ".md" }]);
expect(state.showGistToc()).to.equal(true);
state.activeAnchor = "file-1-a";
expect(state.activeFileIndex()).to.equal(0);
state.details.files.pop();
expect(state.showGistToc()).to.equal(false);
scope.stop();
});
it("chooses the explorer viewer for each gist file and a source language", () => {
const kind = (file) => pages.gistFileKind(file);
expect(kind({ filename: "a.md", content: "# x" })).to.equal("markdown");
expect(kind({ filename: "notes", language: "Markdown", content: "x" })).to.equal("markdown");
expect(kind({ filename: "a.ipynb", content: "{}" })).to.equal("notebook");
expect(kind({ filename: "a.tsv", content: "a\tb" })).to.equal("csv");
expect(kind({ filename: "a.csv", content: "x".repeat(1024 * 1024 + 1) })).to.equal("code");
expect(kind({ filename: "a.svg", content: "<svg/>" })).to.equal("svg");
expect(kind({ filename: "a.py", content: "x" })).to.equal("code");
expect(kind({ filename: "a.png", encoding: "base64", content: "AA==" })).to.equal("image");
expect(kind({ filename: "a.pdf", encoding: "base64", content: "AA==" })).to.equal("pdf");
expect(kind({ filename: "a.webm", encoding: "base64", content: "AA==" })).to.equal("video");
expect(kind({ filename: "a.mp3", encoding: "base64", content: "AA==" })).to.equal("audio");
expect(kind({ filename: "a.bin", encoding: "base64", content: "AA==" })).to.equal("binary");
expect(kind({ filename: "a.png", encoding: "base64", hidden: true })).to.equal("hidden");
expect(kind({ filename: "a.bin", encoding: "base64", tooLarge: true })).to.equal("too-large");
expect(kind({ filename: "a.txt", content: "" })).to.equal("empty");
expect(["markdown", "notebook", "csv", "svg", "code", "image"].map(pages.hasRenderedView)).to.deep.equal([true, true, true, true, false, false]);
expect(pages.gistSourceLanguage({ filename: "a.ipynb" }, "notebook")).to.equal("json");
expect(pages.gistSourceLanguage({ filename: "a.py", language: "Python" }, "code")).to.equal("Python");
});
it("parses quoted CSV cells with the explorer's parser", () => {
expect(formatters.parseDelimited('a,"b ""q"", c"\r\n1,2\n', ",")).to.deep.equal([["a", 'b "q", c'], ["1", "2"]]);
});
});