Files
t3code/apps/web/src/components/pullRequest/pullRequestDetail.logic.test.ts
T

940 lines
31 KiB
TypeScript

import {
PullRequestAction,
type PullRequestCheck,
type PullRequestComment,
type PullRequestDetailView,
type PullRequestReviewThread,
} from "@t3tools/contracts";
import { describe, expect, it } from "vite-plus/test";
import {
buildAddSelectionToAgentHandoff,
buildAskAboutPullRequestHandoff,
buildExplainPullRequestHandoff,
buildFixFindingHandoff,
buildFixFindingsHandoff,
groupPullRequestTimelineConversations,
handoffPrompt,
handoffReviewComments,
isThreadOwnPullRequest,
orderPullRequestComments,
pullRequestActionMenuHasGroup,
pullRequestActionNeedsHostRefresh,
pullRequestComposerTarget,
pullRequestFindingKey,
pullRequestHandoffLabels,
readableFailure,
resolveBaseFreshness,
buildPullRequestTimeline,
describePullRequestState,
} from "./pullRequestDetail.logic";
import type { ReviewCommentContext } from "~/reviewCommentContext";
const TIMELINE_SOURCE: Pick<
PullRequestDetailView,
"createdAt" | "author" | "commits" | "comments" | "mergedAt" | "closedAt"
> = {
createdAt: "2026-07-01T00:00:00Z",
author: { login: "octocat", name: null, avatarUrl: null },
commits: [
{ oid: "1baf7bdcafe", messageHeadline: "add the page", committedDate: "2026-07-02T00:00:00Z" },
],
comments: [
{
id: "c1",
kind: "issue-comment",
author: { login: "bilal", name: null, avatarUrl: null },
body: "looks good",
createdAt: "2026-07-03T00:00:00Z",
url: null,
path: null,
reviewState: null,
},
],
mergedAt: null,
closedAt: null,
};
describe("pull request action menu", () => {
it("keeps the group divider when auto-merge is the only action", () => {
expect(pullRequestActionMenuHasGroup(false, true, false)).toBe(true);
});
});
describe("pull request state description", () => {
it("keeps draft and conflicts orthogonal to the terminal states", () => {
expect(describePullRequestState("open", true)).toBe("Draft");
expect(describePullRequestState("open", false)).toBe("Ready for review");
expect(describePullRequestState("merged", true)).toBe("Merged");
expect(describePullRequestState("closed", false)).toBe("Closed");
});
});
describe("pull request handoff labels", () => {
it("names the open thread when actions write to its composer", () => {
expect(pullRequestHandoffLabels(true)).toEqual({
fixFinding: "Fix in this thread",
fixCheck: "Fix in this thread",
fixFindings: "Fix findings in this thread",
resolve: "Resolve in this thread",
resolveConflicts: "Resolve conflicts in this thread",
});
});
it("keeps the standalone pull request page labels", () => {
expect(pullRequestHandoffLabels(false)).toEqual({
fixFinding: "Fix in a thread",
fixCheck: "Fix",
fixFindings: "Fix findings in a thread",
resolve: "Resolve in a new thread",
resolveConflicts: "Resolve conflicts in a thread",
});
});
});
describe("pull request composer target", () => {
it("rejects a page composer so agent comments cannot open another thread", () => {
const target = { environmentId: "env-1", threadId: "thread-1" };
expect(pullRequestComposerTarget("page", target)).toBeNull();
expect(pullRequestComposerTarget("thread", target)).toBe(target);
});
});
describe("ordering comments", () => {
it("reverses the chronological list for newest first, and leaves oldest first alone", () => {
const comments = [{ createdAt: "a" }, { createdAt: "b" }, { createdAt: "c" }];
expect(orderPullRequestComments(comments, "newest")).toEqual([
{ createdAt: "c" },
{ createdAt: "b" },
{ createdAt: "a" },
]);
expect(orderPullRequestComments(comments, "oldest")).toEqual(comments);
// The source array is chronological input, not a mutation target.
expect(comments).toEqual([{ createdAt: "a" }, { createdAt: "b" }, { createdAt: "c" }]);
});
});
describe("pull request timeline", () => {
it("orders creation, commits and comments newest first", () => {
// What happened last is what the reader opening the tab is asking about.
expect(buildPullRequestTimeline(TIMELINE_SOURCE).map((event) => event.id)).toEqual([
"c1",
"1baf7bdcafe",
"created",
]);
});
it("carries the comment url, and leaves the events the host cannot address without one", () => {
const events = buildPullRequestTimeline({
...TIMELINE_SOURCE,
comments: [{ ...TIMELINE_SOURCE.comments[0]!, url: "https://example.test/pull/1#c1" }],
});
expect(events.map((event) => event.url)).toEqual([
"https://example.test/pull/1#c1",
null,
null,
]);
});
it("carries actors and review context into the presentation model", () => {
const commitAuthors = [
{ login: "octocat", name: null, avatarUrl: "https://example.test/octocat.png" },
{ login: "pair", name: "Pair Author", avatarUrl: null },
];
const events = buildPullRequestTimeline({
...TIMELINE_SOURCE,
commits: [{ ...TIMELINE_SOURCE.commits[0]!, authors: commitAuthors }],
comments: [
{
...TIMELINE_SOURCE.comments[0]!,
kind: "review",
path: "src/app.ts",
reviewState: "APPROVED",
},
],
});
expect(events.find((event) => event.kind === "commit")?.commitAuthors).toEqual(commitAuthors);
expect(events.find((event) => event.kind === "review")).toMatchObject({
actor: TIMELINE_SOURCE.comments[0]?.author,
path: "src/app.ts",
reviewState: "APPROVED",
});
});
it("carries each commit's line counts into its timeline event", () => {
const events = buildPullRequestTimeline({
...TIMELINE_SOURCE,
commits: [{ ...TIMELINE_SOURCE.commits[0]!, additions: 12, deletions: 4 }],
});
expect(events.find((event) => event.kind === "commit")).toMatchObject({
additions: 12,
deletions: 4,
});
});
it("drops a body that is nothing but a bot's HTML comment, and keeps one that says more", () => {
const events = buildPullRequestTimeline({
...TIMELINE_SOURCE,
comments: [
{ ...TIMELINE_SOURCE.comments[0]!, body: "<!-- MURMUR_IGNORE -->" },
{
...TIMELINE_SOURCE.comments[0]!,
id: "c2",
body: "<!-- summarize by coderabbit.ai -->\nNeeds a test.",
createdAt: "2026-07-04T00:00:00Z",
},
],
});
expect(events.find((event) => event.id === "c1")?.body).toBeNull();
// Kept whole: the renderer drops the marker itself, and stripping it here would also
// strip an HTML comment a reviewer quoted inside a code fence.
expect(events.find((event) => event.id === "c2")?.body).toBe(
"<!-- summarize by coderabbit.ai -->\nNeeds a test.",
);
});
it("calls a comment markdown and a commit headline plain text", () => {
const events = buildPullRequestTimeline(TIMELINE_SOURCE);
// A headline reading `fix: drop *legacy* path` is not asking for emphasis.
expect(events.map((event) => [event.title.startsWith("Commit"), event.markdown])).toEqual(
expect.arrayContaining([[true, false]]),
);
expect(events.find((event) => event.id === "c1")?.markdown).toBe(true);
});
it("reports a merge rather than the close GitHub records alongside it", () => {
const events = buildPullRequestTimeline({
...TIMELINE_SOURCE,
mergedAt: "2026-07-04T00:00:00Z",
closedAt: "2026-07-04T00:00:00Z",
});
// Newest first, so the terminal event opens the list rather than ending it.
expect(events[0]?.id).toBe("merged");
expect(events.some((event) => event.id === "closed")).toBe(false);
});
it("groups conversation sections without crossing commits or PR updates", () => {
const events = buildPullRequestTimeline({
...TIMELINE_SOURCE,
comments: [
{
...TIMELINE_SOURCE.comments[0]!,
id: "new-comment-1",
createdAt: "2026-07-04T00:00:00Z",
},
{
...TIMELINE_SOURCE.comments[0]!,
id: "new-comment-2",
createdAt: "2026-07-03T00:00:00Z",
},
{
...TIMELINE_SOURCE.comments[0]!,
id: "old-comment-1",
createdAt: "2026-07-01T12:00:00Z",
},
{
...TIMELINE_SOURCE.comments[0]!,
id: "old-comment-2",
createdAt: "2026-07-01T06:00:00Z",
},
],
mergedAt: "2026-07-05T00:00:00Z",
});
const rows = groupPullRequestTimelineConversations(events);
expect(
rows.map((row) =>
row.kind === "comments"
? [row.kind, ...row.events.map((event) => event.id)]
: [row.kind, row.event.id],
),
).toEqual([
["event", "merged"],
["comments", "new-comment-1", "new-comment-2"],
["event", "1baf7bdcafe"],
["comments", "old-comment-1", "old-comment-2"],
["event", "created"],
]);
});
});
describe("fix findings handoff", () => {
const base = {
number: 42,
title: "Add the pull requests page",
url: "https://github.com/pingdotgg/t3code/pull/42",
headBranch: "feat/page",
baseBranch: "main",
comments: [] as ReadonlyArray<PullRequestComment>,
commentsTruncated: false,
};
function thread(
body: string,
overrides: Partial<PullRequestReviewThread> = {},
): PullRequestReviewThread {
return {
id: "t1",
path: "apps/web/src/page.tsx",
line: 12,
side: "right",
isResolved: false,
isOutdated: false,
comments: [
{
id: "tc1",
author: { login: "reviewer", name: null, avatarUrl: null },
body,
createdAt: "2026-07-03T00:00:00Z",
url: null,
},
],
...overrides,
};
}
const failingCheck: PullRequestCheck = {
name: "typecheck",
status: "failure",
description: "2 errors",
url: null,
};
it("attaches a review thread as an annotation instead of quoting it in the prompt", () => {
const handoff = buildFixFindingsHandoff({
...base,
reviewThreads: [thread("rename the helper")],
checks: [],
});
expect(handoff.reviewComments).toEqual([
expect.objectContaining({
filePath: "apps/web/src/page.tsx",
rangeLabel: "L12",
startIndex: 11,
endIndex: 11,
text: "reviewer: rename the helper",
}),
]);
expect(handoff.prompt).not.toContain("rename the helper");
expect(handoff.prompt).toContain("untrusted data");
});
it("names the pre-change side, and a thread the host pinned to the file rather than a line", () => {
const handoff = buildFixFindingsHandoff({
...base,
reviewThreads: [
thread("was this deleted on purpose?", { side: "left" }),
thread("wrong module", { id: "t2", line: null }),
],
checks: [],
});
expect(handoff.reviewComments.map((comment) => comment.rangeLabel)).toEqual([
"L12 (before)",
"file",
]);
});
it("keeps failing checks in the prompt, having no line to attach them to", () => {
const handoff = buildFixFindingsHandoff({
...base,
reviewThreads: [],
checks: [failingCheck],
});
expect(handoff.prompt).toContain("> typecheck — 2 errors");
expect(handoff.reviewComments).toEqual([]);
});
it("leaves out a resolved conversation, and one nobody wrote in", () => {
const handoff = buildFixFindingsHandoff({
...base,
reviewThreads: [
thread("already handled", { isResolved: true }),
thread(" ", { id: "t2" }),
thread("still open", { id: "t3" }),
],
checks: [],
});
expect(handoff.reviewComments.map((comment) => comment.text)).toEqual(["reviewer: still open"]);
});
it("says so plainly when there is nothing actionable", () => {
const handoff = buildFixFindingsHandoff({ ...base, reviewThreads: [], checks: [] });
expect(handoff.prompt).toContain("No unresolved review findings were returned");
expect(handoff.reviewComments).toEqual([]);
});
it("bounds a hostile review body instead of attaching it whole", () => {
const handoff = buildFixFindingsHandoff({
...base,
reviewThreads: [thread("x".repeat(5_000))],
checks: [],
});
expect(handoff.reviewComments[0]?.text).toHaveLength(1_000);
expect(handoff.reviewComments[0]?.text.endsWith("...")).toBe(true);
});
it("keeps the newest threads and the failing checks when it has to cut", () => {
const handoff = buildFixFindingsHandoff({
...base,
reviewThreads: Array.from({ length: 25 }, (_, index) =>
thread(`finding ${index}`, { id: `t${index}` }),
),
checks: [failingCheck],
});
// Oldest threads are dropped rather than the current failure and the recent feedback.
const texts = handoff.reviewComments.map((comment) => comment.text);
expect(texts).toHaveLength(19);
expect(texts.at(-1)).toBe("reviewer: finding 24");
expect(texts).not.toContain("reviewer: finding 0");
expect(handoff.prompt).toContain("typecheck");
expect(handoff.prompt).toContain("6 further findings were omitted");
});
});
describe("findings that cannot be attached", () => {
const base = {
number: 42,
title: "Add the pull requests page",
url: "https://github.com/pingdotgg/t3code/pull/42",
headBranch: "feat/page",
baseBranch: "main",
reviewThreads: [] as ReadonlyArray<PullRequestReviewThread>,
checks: [] as ReadonlyArray<PullRequestCheck>,
commentsTruncated: false,
};
const review: PullRequestComment = {
id: "r1",
kind: "review",
author: { login: "julius", name: null, avatarUrl: null },
body: "This breaks SSO auth, revert the middleware change.",
createdAt: "2026-07-01T00:00:00Z",
url: null,
path: null,
reviewState: "CHANGES_REQUESTED",
};
it("carries a review submitted with words but no line, which has nothing to attach to", () => {
const handoff = buildFixFindingsHandoff({ ...base, comments: [review] });
// It has no file and no line, so it travels the way a failing check does rather than
// being dropped for lacking somewhere to point.
expect(handoff.reviewComments).toEqual([]);
expect(handoff.prompt).toContain("revert the middleware change");
expect(handoff.prompt).not.toContain("No unresolved review findings");
});
it("carries a host's line comments when it reports no threads at all", () => {
// Azure DevOps has no diff to pin a conversation to, so every remark arrives this way.
const handoff = buildFixFindingsHandoff({
...base,
comments: [{ ...review, id: "a1", kind: "review-comment", path: "src/app.ts" }],
});
expect(handoff.prompt).toContain("src/app.ts");
expect(handoff.prompt).toContain("revert the middleware change");
});
it("does not repeat a remark that was already attached as a thread", () => {
const attachedId = "t1c1";
const handoff = buildFixFindingsHandoff({
...base,
reviewThreads: [
{
id: "t1",
path: "src/app.ts",
line: 12,
side: "right",
isResolved: false,
isOutdated: false,
comments: [
{
id: attachedId,
author: { login: "julius", name: null, avatarUrl: null },
body: "rename the helper",
createdAt: "2026-07-01T00:00:00Z",
url: null,
},
],
},
],
comments: [{ ...review, id: attachedId, kind: "review-comment", body: "rename the helper" }],
});
expect(handoff.reviewComments).toHaveLength(1);
expect(handoff.prompt).not.toContain("rename the helper");
});
});
describe("one finding handed over on its own", () => {
const base = {
number: 42,
title: "Add the pull requests page",
url: "https://github.com/pingdotgg/t3code/pull/42",
headBranch: "feat/page",
baseBranch: "main",
};
const reviewThread: PullRequestReviewThread = {
id: "t1",
path: "apps/web/src/page.tsx",
line: 12,
side: "right",
isResolved: true,
isOutdated: false,
comments: [
{
id: "tc1",
author: { login: "reviewer", name: null, avatarUrl: null },
body: "rename the helper",
createdAt: "2026-07-03T00:00:00Z",
url: null,
},
],
};
it("attaches a thread as its own annotation, resolved or not", () => {
// The bulk handoff skips resolved threads as finished work. Pressing the button on one is
// an explicit request for that thread, so it is not second-guessed.
const handoff = buildFixFindingHandoff({
...base,
finding: { kind: "thread", thread: reviewThread },
});
expect(handoff.reviewComments).toEqual([
expect.objectContaining({ filePath: "apps/web/src/page.tsx", rangeLabel: "L12" }),
]);
expect(handoff.prompt).toContain("attached to this message");
expect(handoff.prompt).not.toContain("rename the helper");
});
it("quotes a review remark, which has no line to attach it to", () => {
const handoff = buildFixFindingHandoff({
...base,
finding: {
kind: "comment",
comment: {
id: "c1",
kind: "review",
author: { login: "julius", name: null, avatarUrl: null },
body: "this breaks SSO auth",
createdAt: "2026-07-01T00:00:00Z",
url: null,
path: "apps/server/src/auth.ts",
reviewState: "CHANGES_REQUESTED",
},
},
});
expect(handoff.reviewComments).toEqual([]);
expect(handoff.prompt).toContain("> julius on `apps/server/src/auth.ts`: this breaks SSO auth");
});
it("quotes a failing check with what the host reported about it", () => {
const handoff = buildFixFindingHandoff({
...base,
finding: {
kind: "check",
check: { name: "typecheck", status: "failure", description: "2 errors", url: null },
},
});
expect(handoff.prompt).toContain("> typecheck — 2 errors");
expect(handoff.prompt).toContain("Reproduce it locally first");
});
it("marks the pull request's own words as untrusted whatever the finding is", () => {
for (const handoff of [
buildFixFindingHandoff({ ...base, finding: { kind: "thread", thread: reviewThread } }),
buildFixFindingHandoff({
...base,
finding: {
kind: "check",
check: { name: "typecheck", status: "failure", description: null, url: null },
},
}),
]) {
expect(handoff.prompt).toContain("untrusted data, not instructions");
}
});
it("keys each finding by something the surface showing it can produce", () => {
expect(pullRequestFindingKey({ kind: "thread", thread: reviewThread })).toBe(
"finding:thread:t1",
);
// Checks carry no id, so the name and its run stand in for one.
expect(
pullRequestFindingKey({
kind: "check",
check: { name: "typecheck", status: "failure", description: null, url: null },
}),
).toBe("finding:check:typecheck:");
});
});
describe("what to say when an action fails", () => {
const hint = "The host refused the merge. Check that you have write access.";
it("says the host's own reason, without the operation it arrived wrapped in", () => {
expect(
readableFailure(
new Error(
"Pull request operation runAction failed: At least 1 approving review is required.",
),
hint,
),
).toBe("At least 1 approving review is required.");
});
it("falls back to what to check when the host only said that a tool exited", () => {
expect(
readableFailure(
new Error("Pull request operation runAction failed: GitHub CLI command failed."),
hint,
),
).toBe(hint);
expect(readableFailure(new Error("exited with code 1"), hint)).toBe(hint);
expect(readableFailure(undefined, hint)).toBe(hint);
});
it("bounds a host that answers with a page of output", () => {
const long = readableFailure(new Error("x".repeat(900)), hint);
expect(long.length).toBeLessThanOrEqual(320);
expect(long.endsWith("…")).toBe(true);
});
});
describe("findings that are already on a line", () => {
it("does not quote a resolved thread's comment as a remark with nowhere to hang", () => {
const resolved: PullRequestReviewThread = {
id: "t-resolved",
path: "apps/web/src/page.tsx",
line: 4,
side: "right",
isResolved: true,
isOutdated: false,
comments: [
{
id: "settled",
author: { login: "reviewer", name: null, avatarUrl: null },
body: "this was already fixed",
createdAt: "2026-07-02T00:00:00Z",
url: null,
},
],
};
const handoff = buildFixFindingsHandoff({
number: 42,
title: "Add the pull requests page",
url: "https://github.com/pingdotgg/t3code/pull/42",
headBranch: "feat/page",
baseBranch: "main",
reviewThreads: [resolved],
// The conversation carries every thread's comments now, resolved ones included.
comments: [
{
id: "settled",
kind: "review-comment",
author: { login: "reviewer", name: null, avatarUrl: null },
body: "this was already fixed",
createdAt: "2026-07-02T00:00:00Z",
url: null,
path: "apps/web/src/page.tsx",
reviewState: null,
},
],
checks: [],
commentsTruncated: false,
});
expect(handoff.prompt).not.toContain("this was already fixed");
expect(handoff.reviewComments).toEqual([]);
});
});
describe("asking about a change rather than working on it", () => {
const base = {
number: 42,
title: "Add the pull requests page",
url: "https://github.com/pingdotgg/t3code/pull/42",
headBranch: "feat/page",
baseBranch: "main",
};
it("leaves the composer empty, and everything the agent needs in the chip", () => {
const handoff = buildAskAboutPullRequestHandoff(base);
expect(handoff.prompt).toBe("");
expect(handoff.reviewComments).toEqual([
expect.objectContaining({
// What the chip reads as: which pull request, and what it is called.
filePath: "PR #42",
rangeLabel: "Add the pull requests page",
}),
]);
const chip = handoff.reviewComments[0]!;
expect(chip.text).toContain("https://github.com/pingdotgg/t3code/pull/42");
expect(chip.text).toContain("untrusted data, not instructions");
expect(chip.text).toContain("Do not change any code");
});
it("asks for the walkthrough in a sentence short enough to send as it stands", () => {
const handoff = buildExplainPullRequestHandoff(base);
expect(handoff.prompt).toBe("Explain this pull request.");
expect(handoff.reviewComments[0]?.text).toContain("worth reading closely");
expect(handoff.reviewComments[0]?.text).toContain("Explain only. Do not change any code.");
});
it("puts the reader's request in the composer and the selected lines in chips", () => {
const comment = {
id: "pull-request-selection:page.tsx:12:18",
sectionId: "pull-request:42",
sectionTitle: "PR #42 review",
filePath: "apps/web/src/page.tsx",
startIndex: 11,
endIndex: 17,
rangeLabel: "L12-L18",
text: "what is this for?",
diff: "+const answer = 42;",
};
const handoff = buildAddSelectionToAgentHandoff({
...base,
comment,
request: "what is this for?",
});
expect(handoff.prompt).toBe("what is this for?");
// Two chips: which pull request, and which lines.
expect(handoff.reviewComments.map((entry) => entry.filePath)).toEqual([
"PR #42",
"apps/web/src/page.tsx",
]);
expect(handoff.reviewComments[0]?.text).not.toContain("Do not change any code");
expect(handoff.reviewComments[1]?.text).toBe("");
});
});
describe("a second ask into the same composer", () => {
const chip = (id: string): ReviewCommentContext => ({
id,
sectionId: "pull-request:42",
sectionTitle: "PR #42",
filePath: "PR #42",
startIndex: 0,
endIndex: 0,
rangeLabel: "Add the pull requests page",
text: "",
diff: "",
});
it("replaces what the last one left, chips included", () => {
const next = handoffReviewComments(
[chip("pull-request-context:41"), chip("pull-request-selection:page.tsx:1:2")],
[chip("pull-request-context:42")],
);
expect(next.map((comment) => comment.id)).toEqual(["pull-request-context:42"]);
});
it("empties what the last ask left, so the two are never sent as one question", () => {
const handed = "Explain this pull request.";
expect(handoffPrompt({ prompt: handed, lastHandoffPrompt: handed }, "")).toBe("");
});
it("replaces the last ask's prompt with this one's", () => {
const handed = "Explain this pull request.";
expect(handoffPrompt({ prompt: handed, lastHandoffPrompt: handed }, "Why the cache key?")).toBe(
"Why the cache key?",
);
});
it("leaves a sentence the reader typed themselves where it is", () => {
expect(
handoffPrompt({ prompt: "check the migration first", lastHandoffPrompt: undefined }, ""),
).toBe("check the migration first");
});
it("keeps what the reader wrote next to the chip an earlier ask left", () => {
// The chip is still there, but these words are not the ones the hand-off wrote.
expect(
handoffPrompt({ prompt: "why is the cache keyed on the branch?", lastHandoffPrompt: "" }, ""),
).toBe("why is the cache keyed on the branch?");
});
it("keeps an edit the reader made to the sentence they were handed", () => {
expect(
handoffPrompt(
{
prompt: "Explain this pull request, the caching especially.",
lastHandoffPrompt: "Explain this pull request.",
},
"",
),
).toBe("Explain this pull request, the caching especially.");
});
it("puts an ask under what the reader typed rather than over it", () => {
expect(
handoffPrompt(
{ prompt: "check the migration first", lastHandoffPrompt: undefined },
"Explain this pull request.",
),
).toBe("check the migration first\n\nExplain this pull request.");
});
it("replaces only its own sentence under text the reader typed", () => {
expect(
handoffPrompt(
{
prompt: "check the migration first\n\nExplain this pull request.",
lastHandoffPrompt: "Explain this pull request.",
},
"Why the cache key?",
),
).toBe("check the migration first\n\nWhy the cache key?");
});
it("takes back only its own sentence when the next ask is empty", () => {
expect(
handoffPrompt(
{
prompt: "check the migration first\n\nExplain this pull request.",
lastHandoffPrompt: "Explain this pull request.",
},
"",
),
).toBe("check the migration first");
});
it("keeps the lines the reader marked up in the thread themselves", () => {
const own = chip("file-comment:3");
const next = handoffReviewComments(
[own, chip("pull-request-finding:t1")],
[chip("pull-request-context:42")],
);
expect(next.map((comment) => comment.id)).toEqual([
"file-comment:3",
"pull-request-context:42",
]);
});
});
describe("how the branch stands against its base", () => {
const detail = (overrides: Record<string, unknown> = {}) =>
({
state: "open",
mergeability: "mergeable",
baseComparison: "behind",
behindBy: 12,
capabilities: { updateMethods: ["merge", "rebase"] },
viewerPermissions: { updateMethods: ["merge", "rebase"] },
...overrides,
}) as Parameters<typeof resolveBaseFreshness>[0];
it("offers both ways where the host and the reader both allow them", () => {
expect(resolveBaseFreshness(detail())).toEqual({ behindBy: 12, methods: ["merge", "rebase"] });
});
it("says nothing about a branch that is already current", () => {
expect(resolveBaseFreshness(detail({ baseComparison: "up-to-date" }))).toBeNull();
});
it("says nothing where the host could not compare, rather than claiming it is current", () => {
expect(resolveBaseFreshness(detail({ baseComparison: "unknown" }))).toBeNull();
expect(resolveBaseFreshness(detail({ baseComparison: undefined }))).toBeNull();
});
it("leaves a conflicting branch to the conflicts row", () => {
expect(resolveBaseFreshness(detail({ mergeability: "conflicting" }))).toBeNull();
});
it("says nothing where the host has no merge verdict yet", () => {
expect(resolveBaseFreshness(detail({ mergeability: "unknown" }))).toBeNull();
});
it("says nothing about a merged or closed pull request", () => {
expect(resolveBaseFreshness(detail({ state: "merged" }))).toBeNull();
expect(resolveBaseFreshness(detail({ state: "closed" }))).toBeNull();
});
it("narrows to what this reader may actually take", () => {
expect(
resolveBaseFreshness(detail({ viewerPermissions: { updateMethods: ["merge"] } }))?.methods,
).toEqual(["merge"]);
});
it("still reports the news where the reader may take none of it", () => {
// Somebody reading another account's pull request is told why it is blocked without being
// offered a button the host would refuse.
expect(resolveBaseFreshness(detail({ viewerPermissions: {} }))).toEqual({
behindBy: 12,
methods: [],
});
});
it("reports a count only where the host counted", () => {
expect(resolveBaseFreshness(detail({ behindBy: undefined }))?.behindBy).toBeNull();
});
});
describe("whether the panel is showing the thread's own pull request", () => {
const surface = { projectId: "proj-a", repository: "acme/app", number: 7 };
it("matches on project, repository and number together", () => {
expect(
isThreadOwnPullRequest({ projectId: "proj-a", repository: "acme/app", number: 7 }, surface),
).toBe(true);
});
it("rejects a second checkout of the same repository under another project", () => {
expect(
isThreadOwnPullRequest({ projectId: "proj-b", repository: "acme/app", number: 7 }, surface),
).toBe(false);
});
it("rejects another repository or another number", () => {
expect(
isThreadOwnPullRequest({ projectId: "proj-a", repository: "acme/web", number: 7 }, surface),
).toBe(false);
expect(
isThreadOwnPullRequest({ projectId: "proj-a", repository: "acme/app", number: 8 }, surface),
).toBe(false);
});
it("rejects a thread with no project or no pull request of its own", () => {
expect(
isThreadOwnPullRequest({ projectId: null, repository: "acme/app", number: 7 }, surface),
).toBe(false);
expect(
isThreadOwnPullRequest(
{ projectId: "proj-a", repository: "acme/app", number: null },
surface,
),
).toBe(false);
});
});
describe("which actions need the host read again after they run", () => {
it("classifies every action the contract knows about", () => {
// Imported from the contract rather than hand-listed, so a new PullRequestAction fails this
// test until somebody decides which side of the diff it belongs on.
expect(PullRequestAction.literals.map(pullRequestActionNeedsHostRefresh)).toEqual(
PullRequestAction.literals.map((action) => action === "update-branch"),
);
});
it("sends update-branch back to the host, having moved the head commit", () => {
expect(pullRequestActionNeedsHostRefresh("update-branch")).toBe(true);
});
it("leaves every action that only changes metadata to the cheaper detail refresh", () => {
for (const action of [
"ready",
"draft",
"close",
"reopen",
"enable-auto-merge",
"disable-auto-merge",
"merge",
] as const) {
expect(pullRequestActionNeedsHostRefresh(action)).toBe(false);
}
});
});