const { expect } = require("chai"); require("ts-node/register/transpile-only"); const { default: PullRequest, reviewThreads, commitMessage } = 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 shows branch names only with the origin", function () { const out = makePullRequest(all).content(); expect(out.author).to.equal("XXXX-1"); expect(out).to.not.have.property("baseRef"); expect(out).to.not.have.property("headRef"); const withOrigin = makePullRequest({ ...all, origin: true }).content(); expect(withOrigin.baseRef).to.equal("main"); expect(withOrigin.headRef).to.equal("XXXX-1/fix-XXXX-2"); }); it("strips people from commit messages even without matching terms", function () { const message = "Merge pull request #3 from asmith/fix-x\n\nFix x \n\nSigned-off-by: Ann Smith \nCo-authored-by: Bo \n"; expect(commitMessage(message, { origin: false })).to.equal("Merge pull request #3\n\nFix x"); expect(commitMessage(message, { origin: true })).to.equal("Merge pull request #3 from fix-x\n\nFix x"); expect(commitMessage("Merge branch 'main' of github.com:asmith/repo into dev", { origin: true })).to.equal("Merge branch 'main' into dev"); expect(commitMessage("Merge branch 'asmith/x'", { origin: false })).to.equal("Merge branch"); expect(commitMessage("Plain message", { origin: false })).to.equal("Plain message"); const pr = makePullRequest(all); pr.model.pullRequest.commits = [{ message: "Merge pull request #3 from asmith/fix\n\nSigned-off-by: Ann Smith ", author: "asmith", date: at(3) }]; expect(pr.content().commits[0].message).to.equal("Merge pull request #3"); }); 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 }); }); });