Files
t3code/apps/web/src/components/pullRequest/pullRequestDetail.logic.test.ts
T
2026-09-17 15:38:11 -07:00

1756 lines
60 KiB
TypeScript

import { resolvePlanFollowUpSubmission } from "../../proposedPlan";
import { serializeLegacyContextMessage } from "@t3tools/shared/composerContextLegacySend";
import {
ProjectId,
PullRequestAction,
type PullRequestCheck,
type PullRequestComment,
type PullRequestDetail,
type PullRequestDetailView,
type PullRequestReviewThread,
type ThreadPullRequestLink,
} from "@t3tools/contracts";
import { describe, expect, it } from "vite-plus/test";
import { formatInlineContextReference } from "~/lib/composerContextReferences";
import { buildMessageContext, reviewCommentContextReference } from "~/lib/composerContextRecords";
import {
buildAddSelectionToAgentHandoff,
buildAskAboutPullRequestHandoff,
buildExplainPullRequestHandoff,
buildPullRequestReferenceContext,
buildFixFindingHandoff,
buildFixFindingsHandoff,
groupPullRequestTimelineConversations,
handoffPrompt,
handoffReviewComments,
stripPullRequestHandoffReferences,
isPullRequestVerdictStale,
isStackedPullRequestBase,
pullRequestPanelContext,
latestPullRequestReviewOutcomes,
newestPullRequestCommitAt,
mergePullRequestThreadComments,
orderPullRequestComments,
pullRequestActionMenuHasGroup,
pullRequestActionNeedsHostRefresh,
pullRequestCheckoutCommand,
pullRequestFindingKey,
pullRequestReviewOutcome,
readableFailure,
readPullRequestDetailSnapshot,
resolveDisplayedPullRequestDetail,
resolvePullRequestReferenceHost,
resolvePullRequestPrimaryControl,
allowsSinglePullRequestMerge,
shouldRefreshPullRequestActivity,
resolveBaseFreshness,
resolvePullRequestMergeMethod,
buildPullRequestTimeline,
editPullRequestThreadComment,
writePullRequestDetailSnapshot,
} from "./pullRequestDetail.logic";
import type { ReviewCommentContext } from "~/reviewCommentContext";
describe("pull request checkout commands", () => {
it.each([
["github", "feature", null, "gh pr checkout 42"],
["gitlab", "feature", null, "glab mr checkout 42"],
["forgejo", "feature", null, null],
["azure-devops", "feature", null, "az repos pr checkout --id 42"],
[
"bitbucket",
"feature/checkout",
"maria/t3code",
"git clone --single-branch --branch feature/checkout https://bitbucket.org/maria/t3code.git t3code-pr-42",
],
["unknown", "feature", null, null],
] as const)("builds the %s command", (provider, branch, repository, expected) => {
expect(pullRequestCheckoutCommand(provider, 42, branch, repository)).toBe(expected);
});
it("fetches Forgejo pull refs from the actual repository, including a mounted host and port", () => {
expect(
pullRequestCheckoutCommand(
"forgejo",
42,
"feature",
null,
"https://forgejo.local:3000/git/maria/repo",
),
).toBe(
"git fetch 'https://forgejo.local:3000/git/maria/repo' refs/pull/42/head && git checkout -B pulls/42 FETCH_HEAD",
);
});
it("quotes shell metacharacters in Forgejo repository URLs", () => {
expect(
pullRequestCheckoutCommand(
"forgejo",
42,
"feature",
null,
"https://forgejo.local/maria/repo'$(echo nope)",
),
).toBe(
"git fetch 'https://forgejo.local/maria/repo'\\''$(echo nope)' refs/pull/42/head && git checkout -B pulls/42 FETCH_HEAD",
);
});
});
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 merge method", () => {
it("uses the current choice, then the project default, then the last choice", () => {
expect(
resolvePullRequestMergeMethod(["merge", "squash", "rebase"], null, "squash", "rebase"),
).toBe("squash");
expect(
resolvePullRequestMergeMethod(["merge", "squash", "rebase"], "rebase", "squash", "merge"),
).toBe("rebase");
expect(resolvePullRequestMergeMethod(["merge", "rebase"], null, "squash", "rebase")).toBe(
"rebase",
);
expect(resolvePullRequestMergeMethod(["squash"], null, "merge", "rebase")).toBe("squash");
});
});
describe("pull request activity refresh", () => {
const first = {
key: "project:acme/web#7",
updatedAt: "2026-08-13T13:00:00Z",
};
it("refreshes activity only after the same pull request changes", () => {
expect(
shouldRefreshPullRequestActivity(first, {
...first,
updatedAt: "2026-08-13T13:01:00Z",
}),
).toBe(true);
});
it("does not duplicate the first activity read or carry a revision across pull requests", () => {
expect(shouldRefreshPullRequestActivity(null, first)).toBe(false);
expect(shouldRefreshPullRequestActivity(first, first)).toBe(false);
expect(
shouldRefreshPullRequestActivity(first, {
key: "project:acme/web#8",
updatedAt: "2026-08-13T13:01:00Z",
}),
).toBe(false);
});
});
describe("review thread comment pages", () => {
it("appends new comments once and keeps refreshed base comments", () => {
expect(
mergePullRequestThreadComments(
[
{ id: "c1", body: "refreshed" },
{ id: "c2", body: "already in base" },
],
[
{ id: "c2", body: "stale page copy" },
{ id: "c3", body: "next page" },
],
),
).toEqual([
{ id: "c1", body: "refreshed" },
{ id: "c2", body: "already in base" },
{ id: "c3", body: "next page" },
]);
});
it("keeps a loaded comment after its body is edited", () => {
const loaded = [
{ id: "c2", body: "old body" },
{ id: "c3", body: "another loaded comment" },
];
expect(editPullRequestThreadComment(loaded, "c2", "saved body")).toEqual([
{ id: "c2", body: "saved body" },
{ id: "c3", body: "another loaded comment" },
]);
});
});
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 primary control", () => {
const open = {
state: "open" as const,
isDraft: false,
mergeability: "mergeable" as const,
checksState: "passing" as const,
autoMergeEnabled: false,
hasMergeMethod: true,
canMerge: true,
canMarkReady: true,
canEnableAutoMerge: true,
};
it("moves pending and failing checks to auto-merge", () => {
expect(resolvePullRequestPrimaryControl({ ...open, checksState: "pending" })).toBe(
"enable-auto-merge",
);
expect(resolvePullRequestPrimaryControl({ ...open, checksState: "failing" })).toBe(
"enable-auto-merge",
);
});
it("does not offer auto-merge while the host state is unknown", () => {
expect(
resolvePullRequestPrimaryControl({
...open,
checksState: "pending",
autoMergeEnabled: undefined,
}),
).toBe("merge");
});
it("keeps armed and terminal states in the merge button slot", () => {
expect(resolvePullRequestPrimaryControl({ ...open, autoMergeEnabled: true })).toBe(
"auto-merge-armed",
);
expect(resolvePullRequestPrimaryControl({ ...open, state: "merged" })).toBe("merged");
expect(resolvePullRequestPrimaryControl({ ...open, state: "closed" })).toBe("closed");
});
it("keeps conflicts and drafts actionable before merge", () => {
expect(resolvePullRequestPrimaryControl({ ...open, mergeability: "conflicting" })).toBe(
"resolve",
);
expect(resolvePullRequestPrimaryControl({ ...open, isDraft: true })).toBe("ready");
});
});
describe("stacked pull request classification", () => {
it("requires a known default branch", () => {
expect(isStackedPullRequestBase("main", [{ name: "main", isDefault: false }])).toBe(false);
});
it("recognizes local and remote forms of the default branch", () => {
expect(
isStackedPullRequestBase("main", [{ name: "main", isDefault: true, isRemote: false }]),
).toBe(false);
expect(
isStackedPullRequestBase("main", [
{ name: "origin/main", isDefault: true, isRemote: true, remoteName: "origin" },
]),
).toBe(false);
});
it("classifies a non-default base as stacked once the default is known", () => {
expect(
isStackedPullRequestBase("feature-base", [
{ name: "origin/main", isDefault: true, isRemote: true, remoteName: "origin" },
]),
).toBe(true);
});
it("does not mistake a nested branch suffix for the default branch", () => {
expect(
isStackedPullRequestBase("main", [
{
name: "origin/feature/main",
isDefault: true,
isRemote: true,
remoteName: "origin",
},
]),
).toBe(true);
expect(
isStackedPullRequestBase("1.0", [{ name: "release/1.0", isDefault: true, isRemote: false }]),
).toBe(true);
});
});
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("review verdicts", () => {
it("reads the same three verdicts however a host spells them", () => {
expect(pullRequestReviewOutcome("APPROVED")).toBe("approved");
expect(pullRequestReviewOutcome("approved")).toBe("approved");
expect(pullRequestReviewOutcome("CHANGES_REQUESTED")).toBe("changes-requested");
expect(pullRequestReviewOutcome("changes_requested")).toBe("changes-requested");
expect(pullRequestReviewOutcome("DISMISSED")).toBe("dismissed");
});
it("is not a verdict where the review only carried remarks", () => {
expect(pullRequestReviewOutcome("COMMENTED")).toBeNull();
expect(pullRequestReviewOutcome("PENDING")).toBeNull();
expect(pullRequestReviewOutcome(null)).toBeNull();
});
it("keeps each reviewer's last word, whatever order the host returned them in", () => {
const review = (
id: string,
login: string,
reviewState: string,
createdAt: string,
): PullRequestComment => ({
id,
kind: "review",
author: { login, name: null, avatarUrl: null },
body: "",
createdAt,
url: null,
path: null,
reviewState,
});
expect(
latestPullRequestReviewOutcomes([
review("r3", "bilal", "APPROVED", "2026-07-03T00:00:00Z"),
review("r1", "bilal", "CHANGES_REQUESTED", "2026-07-01T00:00:00Z"),
review("r2", "octocat", "CHANGES_REQUESTED", "2026-07-02T00:00:00Z"),
// Not a verdict, so it neither adds a reviewer nor overwrites one.
review("r4", "octocat", "COMMENTED", "2026-07-04T00:00:00Z"),
]).map((entry) => [entry.actor?.login, entry.outcome]),
).toEqual([
["bilal", "approved"],
["octocat", "changes-requested"],
]);
});
it("keeps two deleted accounts apart rather than counting them as one reviewer", () => {
expect(
latestPullRequestReviewOutcomes([
{
...TIMELINE_SOURCE.comments[0]!,
id: "r1",
kind: "review",
author: null,
reviewState: "APPROVED",
createdAt: "2026-07-01T00:00:00Z",
},
{
...TIMELINE_SOURCE.comments[0]!,
id: "r2",
kind: "review",
author: null,
reviewState: "APPROVED",
createdAt: "2026-07-02T00:00:00Z",
},
]),
).toHaveLength(2);
});
it("gives every entry a key that separates the reviewers it kept apart", () => {
const entries = latestPullRequestReviewOutcomes([
{
...TIMELINE_SOURCE.comments[0]!,
id: "r1",
kind: "review",
author: null,
reviewState: "APPROVED",
createdAt: "2026-07-01T00:00:00Z",
},
{
...TIMELINE_SOURCE.comments[0]!,
id: "r2",
kind: "review",
author: null,
reviewState: "APPROVED",
createdAt: "2026-07-01T00:00:00Z",
},
]);
// Same author (none) and the same instant, so only the review's own id tells them apart.
expect(new Set(entries.map((entry) => entry.key)).size).toBe(2);
});
it("calls a verdict stale once commits land after it, and current before that", () => {
const commits = [
{ oid: "c0ffee", messageHeadline: "later work", committedDate: "2026-07-05T00:00:00Z" },
];
const review = (createdAt: string): PullRequestComment => ({
...TIMELINE_SOURCE.comments[0]!,
kind: "review",
reviewState: "APPROVED",
createdAt,
});
expect(
latestPullRequestReviewOutcomes([review("2026-07-01T00:00:00Z")], commits)[0]?.stale,
).toBe(true);
expect(
latestPullRequestReviewOutcomes([review("2026-07-06T00:00:00Z")], commits)[0]?.stale,
).toBe(false);
// Nothing to be overtaken by, so nothing is stale.
expect(latestPullRequestReviewOutcomes([review("2026-07-01T00:00:00Z")], [])[0]?.stale).toBe(
false,
);
});
it("measures staleness against the newest commit, not the last one listed", () => {
expect(
newestPullRequestCommitAt([
{ oid: "a", messageHeadline: "", committedDate: "2026-07-09T00:00:00Z" },
{ oid: "b", messageHeadline: "", committedDate: "2026-07-02T00:00:00Z" },
]),
).toBe("2026-07-09T00:00:00Z");
expect(newestPullRequestCommitAt([])).toBeNull();
});
it("orders instants rather than their text, so a UTC offset cannot invert them", () => {
// 01:00+02:00 is 23:00 the previous day, so as text it sorts after the Z stamp and in time
// it falls well before it.
expect(
newestPullRequestCommitAt([
{ oid: "a", messageHeadline: "", committedDate: "2026-07-05T00:30:00Z" },
{ oid: "b", messageHeadline: "", committedDate: "2026-07-05T01:00:00+02:00" },
]),
).toBe("2026-07-05T00:30:00Z");
expect(isPullRequestVerdictStale("2026-07-05T00:30:00Z", "2026-07-05T01:00:00+02:00")).toBe(
false,
);
// A timestamp nothing can parse is not a position, so it settles nothing either way.
expect(isPullRequestVerdictStale("2026-07-01T00:00:00Z", "not a date")).toBe(false);
expect(
newestPullRequestCommitAt([{ oid: "a", messageHeadline: "", committedDate: "not a date" }]),
).toBeNull();
});
it("shows nothing for a reviewer whose verdict was dismissed", () => {
expect(
latestPullRequestReviewOutcomes([
{
...TIMELINE_SOURCE.comments[0]!,
kind: "review",
reviewState: "APPROVED",
createdAt: "2026-07-01T00:00:00Z",
},
{
...TIMELINE_SOURCE.comments[0]!,
id: "c2",
kind: "review",
reviewState: "DISMISSED",
createdAt: "2026-07-02T00:00:00Z",
},
]),
).toEqual([]);
});
});
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"],
]);
});
it("keeps a verdict out of the collapsed conversation it was submitted in", () => {
const events = buildPullRequestTimeline({
...TIMELINE_SOURCE,
comments: [
{ ...TIMELINE_SOURCE.comments[0]!, id: "chatter-1", createdAt: "2026-07-05T00:00:00Z" },
{
...TIMELINE_SOURCE.comments[0]!,
id: "approval",
kind: "review",
body: "",
reviewState: "APPROVED",
createdAt: "2026-07-04T00:00:00Z",
},
{ ...TIMELINE_SOURCE.comments[0]!, id: "chatter-2", createdAt: "2026-07-03T00:00:00Z" },
// A review without a verdict is ordinary conversation and still groups.
{
...TIMELINE_SOURCE.comments[0]!,
id: "remark",
kind: "review",
reviewState: "COMMENTED",
createdAt: "2026-07-02T12: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([
["comments", "chatter-1"],
["event", "approval"],
["comments", "chatter-2", "remark"],
["event", "1baf7bdcafe"],
["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",
state: "open" as const,
isDraft: false,
};
it.each(["", "Please consider "])("preserves PR plan feedback with prose %j", (prose) => {
const comment = buildPullRequestReferenceContext(base);
const draftText = prose + formatInlineContextReference(reviewCommentContextReference(comment));
const submission = resolvePlanFollowUpSubmission({ draftText, planMarkdown: "# Plan" });
const context = buildMessageContext({
terminalContexts: [],
previewAnnotations: [],
reviewComments: [comment],
});
expect(submission).toEqual({ text: draftText, interactionMode: "plan" });
expect(context?.records[0]).toMatchObject({ pullRequest: base });
const legacyText = serializeLegacyContextMessage({
text: submission.text,
records: context!.records,
});
expect(legacyText).toContain(base.url);
expect(legacyText).toContain(prose);
expect(legacyText).not.toContain("PLEASE IMPLEMENT THIS PLAN");
expect(legacyText).not.toContain("t3-context://");
});
it("builds a neutral composer reference without prescribing an action", () => {
const context = buildPullRequestReferenceContext(base);
expect(context.pullRequest).toEqual(expect.objectContaining({ number: 42, state: "open" }));
expect(context.text).toContain("https://github.com/pingdotgg/t3code/pull/42");
expect(context.text).not.toContain("Do not change any code");
expect(context.text).not.toContain("Walk through this pull request");
});
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",
pullRequest: {
number: 42,
title: "Add the pull requests page",
url: "https://github.com/pingdotgg/t3code/pull/42",
headBranch: "feat/page",
baseBranch: "main",
state: "open",
isDraft: false,
},
}),
]);
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("keeps a reader's own pull request reference when a later handoff lands", () => {
const own = buildPullRequestReferenceContext({
number: 42,
title: "Add the pull requests page",
url: "https://github.com/pingdotgg/t3code/pull/42",
headBranch: "feature",
baseBranch: "main",
state: "open" as const,
isDraft: false,
});
const prompt = `Look at this. ${formatInlineContextReference(reviewCommentContextReference(own))} `;
expect(stripPullRequestHandoffReferences(prompt, [own])).toBe(prompt);
expect(
handoffReviewComments([own], [chip("pull-request-context:42")]).map((comment) => comment.id),
).toEqual([own.id, "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("removes the previous handoff chip before replacing its prompt", () => {
const previous = chip("pull-request-context:42");
const prompt = `Explain this pull request. ${formatInlineContextReference(
reviewCommentContextReference(previous),
)} `;
expect(stripPullRequestHandoffReferences(prompt, [previous])).toBe(
"Explain this pull request.",
);
});
it("keeps a handoff reference when the next action deliberately repeats it", () => {
const previous = chip("pull-request-context:42");
const prompt = formatInlineContextReference(reviewCommentContextReference(previous));
expect(stripPullRequestHandoffReferences(prompt, [previous], new Set([previous.id]))).toBe(
prompt,
);
});
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("pull request panel context beside a thread", () => {
// Shapes copied from real threads: a thread that opened a stack holds the top layer as a
// manual link and every lower layer as a "stack" link, with the legacy field pointing at
// whichever one the server chose. Snapshots are null until the sync reactor's first pass.
const link = (
number: number,
overrides: Partial<ThreadPullRequestLink> = {},
): ThreadPullRequestLink => ({
host: "github.com",
repository: "pingdotgg/t3code",
number,
url: `https://github.com/pingdotgg/t3code/pull/${number}`,
source: "manual",
linkedAt: "2026-09-09T00:00:00Z",
snapshot: null,
stack: null,
...overrides,
});
const surface = (
number: number,
overrides: Partial<Parameters<typeof pullRequestPanelContext>[1]> = {},
) => ({
projectId: "proj-a",
host: "github.com",
repository: "pingdotgg/t3code",
number,
...overrides,
});
const stackThread = {
projectId: "proj-a",
pullRequests: [
link(10856),
link(10832, { source: "stack" }),
link(10677, { source: "stack" }),
link(10854, { source: "stack" }),
link(10855, { source: "stack" }),
],
linkedPullRequest: {
projectId: "proj-a",
repository: "pingdotgg/t3code",
number: 10856,
url: "https://github.com/pingdotgg/t3code/pull/10856",
},
};
it("treats every layer of the thread's stack as its own, not only the one the legacy field names", () => {
for (const number of [10856, 10832, 10677, 10854, 10855]) {
expect(pullRequestPanelContext(stackThread, surface(number)), `#${number}`).toBe("thread");
}
});
it("does not let the legacy field decide when the thread holds a link list", () => {
// Every prior regression flipped here: a server-side change to which link the legacy field
// resolves to must not turn the thread's own second link into a checkout-able stranger.
const thread = {
projectId: "proj-a",
pullRequests: [link(11101, { source: "created" }), link(11105, { source: "stack" })],
linkedPullRequest: {
projectId: "proj-a",
repository: "pingdotgg/t3code",
number: 11105,
url: "https://github.com/pingdotgg/t3code/pull/11105",
},
};
expect(pullRequestPanelContext(thread, surface(11101))).toBe("thread");
expect(pullRequestPanelContext(thread, surface(11105))).toBe("thread");
expect(pullRequestPanelContext({ ...thread, linkedPullRequest: null }, surface(11101))).toBe(
"thread",
);
});
it("is the page for a pull request the thread is not linked to", () => {
expect(pullRequestPanelContext(stackThread, surface(12320))).toBe("page");
expect(pullRequestPanelContext(stackThread, surface(10856, { repository: "acme/web" }))).toBe(
"page",
);
});
it("is the page under another project's checkout of the same repository", () => {
expect(pullRequestPanelContext(stackThread, surface(10856, { projectId: "proj-b" }))).toBe(
"page",
);
});
it("recognizes an unsynced manual link, and matches host and repository case-insensitively", () => {
const thread = { projectId: "proj-a", pullRequests: [link(7, { host: "GitHub.com" })] };
expect(pullRequestPanelContext(thread, surface(7, { repository: "PingDotGG/T3Code" }))).toBe(
"thread",
);
expect(pullRequestPanelContext(thread, surface(7, { host: undefined }))).toBe("thread");
expect(pullRequestPanelContext(thread, surface(7, { host: "gitlab.com" }))).toBe("page");
});
it("ignores a dismissed stack member the reader chose not to see", () => {
const thread = {
projectId: "proj-a",
pullRequests: [link(1), link(2, { source: "stack-dismissed" })],
};
expect(pullRequestPanelContext(thread, surface(2))).toBe("page");
});
it("falls back to the legacy fields only for a thread with no link list", () => {
const legacy = {
projectId: "proj-a",
repository: "pingdotgg/t3code",
number: 3,
url: "https://github.com/pingdotgg/t3code/pull/3",
};
expect(
pullRequestPanelContext({ projectId: "proj-a", linkedPullRequest: legacy }, surface(3)),
).toBe("thread");
expect(
pullRequestPanelContext({ projectId: "proj-a", branchPullRequest: legacy }, surface(3)),
).toBe("thread");
expect(pullRequestPanelContext({ projectId: "proj-a", pullRequests: [] }, surface(3))).toBe(
"page",
);
expect(pullRequestPanelContext({ projectId: null }, surface(3))).toBe("page");
});
});
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" || action === "approve-workflows",
),
);
});
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",
"revert",
] as const) {
expect(pullRequestActionNeedsHostRefresh(action)).toBe(false);
}
});
});
describe("cached pull request detail", () => {
const reference = { projectId: ProjectId.make("project-1"), repository: "acme/web", number: 7 };
const detail = (overrides: Partial<PullRequestDetail> = {}): PullRequestDetail =>
({
provider: "github",
capabilities: {
diff: true,
comment: true,
actions: ["merge"],
mergeMethods: ["merge"],
search: true,
review: {
inlineComment: true,
reply: true,
resolve: true,
verdicts: ["comment", "approve", "request-changes"],
},
reviewers: { request: true, listCandidates: true },
},
viewerPermissions: {
actions: ["merge"],
comment: true,
resolve: true,
verdicts: ["comment", "approve", "request-changes"],
requestReviewers: true,
},
projectId: "project-1",
projectTitle: "web",
workspaceRoot: "/repo",
repository: "acme/web",
number: 7,
title: "Cache the title",
body: "who made it",
url: "https://github.com/acme/web/pull/7",
author: { login: "octocat", name: null, avatarUrl: "https://avatars.example/octocat" },
state: "open",
isDraft: false,
mergeability: "mergeable",
additions: 12,
deletions: 3,
changedFiles: 2,
headBranch: "feat/cache",
baseBranch: "main",
createdAt: "2026-07-01T00:00:00.000Z",
updatedAt: "2026-07-02T00:00:00.000Z",
mergedAt: null,
closedAt: null,
reviewers: [],
labels: [],
checks: [],
mergeCapabilities: { merge: true, squash: true, rebase: true },
...overrides,
}) as PullRequestDetail;
const makeStorage = () => {
const held = new Map<string, string>();
return {
getItem: (key: string) => held.get(key) ?? null,
setItem: (key: string, value: string) => void held.set(key, value),
};
};
it("hydrates the last title, author, and counts so a reopen does not ghost the tab", () => {
const storage = makeStorage();
writePullRequestDetailSnapshot(storage, "env-1", reference, detail());
const snapshot = readPullRequestDetailSnapshot(storage, "env-1", reference);
expect(snapshot?.title).toBe("Cache the title");
expect(snapshot?.author?.login).toBe("octocat");
expect(snapshot?.additions).toBe(12);
expect(snapshot?.deletions).toBe(3);
});
it("reuses a host-qualified snapshot when reopening a thread link without a host", () => {
const storage = makeStorage();
writePullRequestDetailSnapshot(
storage,
"env-1",
{ ...reference, host: "github.com" },
detail(),
);
const resolved = resolvePullRequestReferenceHost(reference, {
canonicalKey: "github.com/acme/web",
locator: {
source: "git-remote",
remoteName: "origin",
remoteUrl: "https://github.com/acme/web.git",
},
provider: "github",
});
expect(readPullRequestDetailSnapshot(storage, "env-1", resolved)?.title).toBe(
"Cache the title",
);
const explicit = { ...reference, host: "github.example.com" };
expect(
resolvePullRequestReferenceHost(explicit, {
canonicalKey: "github.com/acme/web",
locator: {
source: "git-remote",
remoteName: "origin",
remoteUrl: "https://github.com/acme/web.git",
},
}),
).toBe(explicit);
});
it("leaves server-resolved Azure SSH references unchanged", () => {
expect(
resolvePullRequestReferenceHost(reference, {
canonicalKey: "ssh.dev.azure.com/v3/org/project/web",
locator: {
source: "git-remote",
remoteName: "origin",
remoteUrl: "[email protected]:v3/org/project/web",
},
provider: "azure-devops",
}),
).toBe(reference);
expect(resolvePullRequestReferenceHost(reference, undefined)).toBe(reference);
});
it("hydrates legacy hostless snapshots only for the matching host", () => {
const storage = makeStorage();
writePullRequestDetailSnapshot(storage, "env-1", reference, detail());
expect(
readPullRequestDetailSnapshot(storage, "env-1", { ...reference, host: "github.com" })?.title,
).toBe("Cache the title");
expect(
readPullRequestDetailSnapshot(storage, "env-1", {
...reference,
host: "github.example.com",
}),
).toBeNull();
});
it("keeps Forgejo ports isolated when recovering legacy snapshots", () => {
const storage = makeStorage();
const cached = detail({
provider: "forgejo",
url: "https://forge.example:8443/acme/web/pulls/7",
});
writePullRequestDetailSnapshot(storage, "env-1", reference, cached);
const resolved = { ...reference, host: "forge.example:8443" };
expect(readPullRequestDetailSnapshot(storage, "env-1", resolved)?.title).toBe(cached.title);
expect(
readPullRequestDetailSnapshot(storage, "env-1", {
...reference,
host: "forge.example:9443",
}),
).toBeNull();
expect(
readPullRequestDetailSnapshot(storage, "env-1", {
...reference,
host: "forge.example",
}),
).toBeNull();
});
it.each(["github", "gitlab"] as const)(
"retains portless %s snapshot identities for custom web ports",
(provider) => {
const storage = makeStorage();
const host = `${provider}.example.com`;
const hosted = { ...reference, host };
const cached = detail({
provider,
url: `https://${host}:8443/acme/web/${provider === "github" ? "pull" : "-/merge_requests"}/7`,
});
writePullRequestDetailSnapshot(storage, "env-1", hosted, cached);
expect(readPullRequestDetailSnapshot(storage, "env-1", hosted)?.title).toBe(cached.title);
},
);
it("keeps a cached tab painted while the live read replaces the counts", () => {
const cached = detail();
const live = detail({ additions: 40, deletions: 9, title: "Cache the title" });
expect(resolveDisplayedPullRequestDetail({ live, cached, reference })?.additions).toBe(40);
expect(resolveDisplayedPullRequestDetail({ live: null, cached, reference })?.additions).toBe(
12,
);
});
it("does not paint another change request's snapshot", () => {
expect(
resolveDisplayedPullRequestDetail({
live: null,
cached: detail({ number: 8 }),
reference,
}),
).toBeNull();
expect(readPullRequestDetailSnapshot(makeStorage(), "env-2", reference)).toBeNull();
});
it("isolates stored and displayed details between hosts with the same repository and number", () => {
const storage = makeStorage();
const publicRef = { ...reference, host: "github.com" };
const enterpriseRef = { ...reference, host: "ghe.example.com" };
const publicDetail = detail();
const enterpriseDetail = detail({
title: "Enterprise change",
url: "https://ghe.example.com/acme/web/pull/7",
});
writePullRequestDetailSnapshot(storage, "env-1", publicRef, publicDetail);
expect(readPullRequestDetailSnapshot(storage, "env-1", enterpriseRef)).toBeNull();
writePullRequestDetailSnapshot(storage, "env-1", enterpriseRef, enterpriseDetail);
expect(readPullRequestDetailSnapshot(storage, "env-1", publicRef)?.title).toBe(
publicDetail.title,
);
expect(readPullRequestDetailSnapshot(storage, "env-1", enterpriseRef)?.title).toBe(
enterpriseDetail.title,
);
expect(
resolveDisplayedPullRequestDetail({
live: null,
cached: publicDetail,
reference: enterpriseRef,
}),
).toBeNull();
expect(
resolveDisplayedPullRequestDetail({
live: null,
cached: enterpriseDetail,
reference: enterpriseRef,
}),
).toBe(enterpriseDetail);
writePullRequestDetailSnapshot(storage, "env-1", enterpriseRef, publicDetail);
expect(readPullRequestDetailSnapshot(storage, "env-1", enterpriseRef)).toBeNull();
});
it("shrugs off corrupt storage and no storage at all", () => {
const storage = makeStorage();
storage.setItem("t3.pullRequests.detail:env-1:project-1:acme/web#7", "{not json");
expect(readPullRequestDetailSnapshot(storage, "env-1", reference)).toBeNull();
expect(readPullRequestDetailSnapshot(undefined, "env-1", reference)).toBeNull();
const hosted = { ...reference, host: "github.com" };
writePullRequestDetailSnapshot(storage, "env-1", hosted, detail({ url: "invalid url" }));
expect(readPullRequestDetailSnapshot(storage, "env-1", hosted)).toBeNull();
});
});
describe("single-PR merge compatibility during stack discovery", () => {
it.each([
[false, true, false, null, true],
[false, false, true, null, true],
[true, false, true, null, false],
[true, false, false, "Lookup failed", false],
[true, true, false, null, false],
[true, false, false, null, true],
] as const)(
"capability=%s stack=%s pending=%s error=%s permits=%s",
(supportsStackActions, hasStack, stackPending, stackError, allowed) => {
expect(
allowsSinglePullRequestMerge({ supportsStackActions, hasStack, stackPending, stackError }),
).toBe(allowed);
},
);
});