Files
anonymous_github/test/pull-request-review-content.test.js
T
Thomas DurieuxandClaude Opus 5.5 176bc857a1 feat(viewers): outlines for Markdown and gists, refined pull request page
Markdown: the "On this page" outline covers h1-h3, draws redaction slots
like the body, treats a lone opening h1 as the title, highlights the
section past the reading line, and shows its menu from 768 to 1199px too.

Gists: one card per file and an outline of the files (name, language,
lines) with Markdown headings nested under each file; sticky right rail
on wide screens, an "In this gist" menu on narrow ones.

Pull requests: header with state, anonymized author, branches and
commit count; Conversation, Commits and Files changed tabs; a file list
with +/- counts, per-file collapse, inline review threads, and redaction
slots in the title, body and diff. The server now stores the author,
branch names, commits (no SHAs) and review comments, grouped into
threads; older pull requests render without them.

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

108 lines
5.0 KiB
JavaScript

const { expect } = require("chai");
require("ts-node/register/transpile-only");
const { default: PullRequest, reviewThreads } = require("../src/core/PullRequest");
const at = (day) => new Date(`2026-03-${String(day).padStart(2, "0")}T10:00:00Z`);
function makePullRequest(options) {
return new PullRequest({
pullRequestId: "pr-test",
owner: "000000000000000000000001",
anonymizeDate: at(20),
source: { pullRequestId: 12, repositoryFullName: "alice/secret-project" },
options: { terms: ["Alice", "secret-project"], image: true, link: true, ...options },
pullRequest: {
title: "Fix by Alice",
body: "Body",
diff: "--- a/x\n+++ b/x\n@@ -1 +1 @@\n-Alice\n+Bob\n",
state: "open",
author: "Alice",
baseRef: "main",
headRef: "Alice/fix-secret-project",
creationDate: at(1),
updatedDate: at(2),
comments: [],
commits: [
{ message: "First commit by Alice\n\nDetails for secret-project", author: "Alice", date: at(3) },
{ message: "Second", author: "bob", date: at(4) },
],
reviewComments: [
{ commentId: 11, path: "src/Alice.py", line: 4, body: "Why Alice?", author: "bob", creationDate: at(5), updatedDate: at(5) },
{ commentId: 13, inReplyTo: 11, path: "src/Alice.py", line: 4, body: "Because", author: "Alice", creationDate: at(7), updatedDate: at(7) },
{ commentId: 12, path: "README.md", line: 1, body: "Typo", author: "carol", creationDate: at(6), updatedDate: at(6) },
],
},
});
}
const all = { title: true, body: true, comments: true, diff: true, username: true, date: true, origin: false };
describe("PullRequest.content() commits, branches and review threads", function () {
it("anonymizes the author and branch names", function () {
const out = makePullRequest(all).content();
expect(out.author).to.equal("XXXX-1");
expect(out.baseRef).to.equal("main");
expect(out.headRef).to.equal("XXXX-1/fix-XXXX-2");
});
it("hides the author without the username option", function () {
const out = makePullRequest({ ...all, username: false }).content();
expect(out.author).to.be.undefined;
expect(out.commits[0]).to.not.have.property("author");
expect(out.reviewComments[0].comments[0]).to.not.have.property("author");
});
it("lists commits with anonymized messages and no SHA", function () {
const out = makePullRequest(all).content();
expect(out.commits).to.have.length(2);
expect(out.commits[0].message).to.equal("First commit by XXXX-1\n\nDetails for XXXX-2");
expect(out.commits[0].author).to.equal("XXXX-1");
expect(out.commits[0].date).to.deep.equal(at(3));
expect(Object.keys(out.commits[0])).to.have.members(["message", "author", "date"]);
});
it("drops commits when the diff is hidden, and dates when dates are hidden", function () {
expect(makePullRequest({ ...all, diff: false }).content().commits).to.be.undefined;
expect(makePullRequest({ ...all, date: false }).content().commits[0]).to.not.have.property("date");
});
it("groups review comments into threads without GitHub ids", function () {
const out = makePullRequest(all).content();
expect(out.reviewComments).to.have.length(2);
const [first, second] = out.reviewComments;
expect(first.path).to.equal("src/XXXX-1.py");
expect(first.line).to.equal(4);
expect(first.comments.map((c) => c.body)).to.deep.equal(["Why XXXX-1?", "Because"]);
expect(first.comments[1].author).to.equal("XXXX-1");
expect(second.path).to.equal("README.md");
expect(JSON.stringify(out.reviewComments)).to.not.match(/commentId|inReplyTo|"1[123]"/);
});
it("omits review threads without the comments option", function () {
expect(makePullRequest({ ...all, comments: false }).content().reviewComments).to.be.undefined;
});
it("keeps pull requests stored before commits and review comments readable", function () {
const pr = makePullRequest(all);
delete pr.model.pullRequest.commits;
delete pr.model.pullRequest.reviewComments;
const out = pr.content();
expect(out.commits).to.be.undefined;
expect(out.reviewComments).to.be.undefined;
expect(out.diff).to.include("XXXX-1");
});
});
describe("reviewThreads()", function () {
it("attaches replies to their thread even when they arrive out of order", function () {
const threads = reviewThreads([
{ commentId: 3, inReplyTo: 1, path: "a", line: 2, body: "reply 2", author: "x", creationDate: at(9), updatedDate: at(9) },
{ commentId: 1, path: "a", line: 2, body: "root", author: "x", creationDate: at(1), updatedDate: at(1) },
{ commentId: 2, inReplyTo: 1, path: "a", line: 2, body: "reply 1", author: "y", creationDate: at(2), updatedDate: at(2) },
{ commentId: 4, inReplyTo: 99, path: "b", line: 5, body: "orphan", author: "z", creationDate: at(3), updatedDate: at(3) },
]);
expect(threads.map((t) => t.comments.map((c) => c.body))).to.deep.equal([["root", "reply 1", "reply 2"], ["orphan"]]);
expect(threads[1]).to.include({ path: "b", line: 5 });
});
});