Skip to content

Commit 35351e6

Browse files
committed
test(server): cover more squash cleanup cases that must keep the worktree
Folds in the cases from #14849: a closed-unmerged pull request, a stale merged snapshot on the thread, a stack-parent base, and HEAD moving during cleanup. Also asserts the head SHA through the GraphQL read, its gh fallback, the provider mapping, and a refreshed branch lookup.
1 parent 5c3c75a commit 35351e6

4 files changed

Lines changed: 76 additions & 27 deletions

File tree

‎apps/server/src/git/GitManager.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,6 +1479,7 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
14791479
url: "https://github.com/pingdotgg/codething-mvp/pull/220",
14801480
baseRefName: "main",
14811481
headRefName: "feature/shared-pr-cache",
1482+
headRefOid: "a".repeat(40),
14821483
state: "MERGED",
14831484
updatedAt: "2026-04-07T15:00:00Z",
14841485
},
@@ -1490,6 +1491,7 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
14901491
url: "https://github.com/pingdotgg/codething-mvp/pull/221",
14911492
baseRefName: "main",
14921493
headRefName: "feature/shared-pr-cache",
1494+
headRefOid: "b".repeat(40),
14931495
state: "OPEN",
14941496
updatedAt: "2026-04-08T15:00:00Z",
14951497
},
@@ -1506,6 +1508,7 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
15061508

15071509
expect(status.pr?.state).toBe("merged");
15081510
expect(pullRequest?.state).toBe("merged");
1511+
expect(pullRequest?.headSha).toBe("a".repeat(40));
15091512
expect(ghCalls.filter((call) => call.startsWith("pr list "))).toHaveLength(1);
15101513
const refreshed = yield* manager.branchPullRequest(
15111514
{ cwd: repoDir, branch: "feature/shared-pr-cache" },
@@ -1514,6 +1517,7 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {
15141517
expect(refreshed).toMatchObject({
15151518
number: 221,
15161519
state: "open",
1520+
headSha: "b".repeat(40),
15171521
repositoryKey: "github.com/pingdotgg/codething-mvp",
15181522
});
15191523
expect(ghCalls.filter((call) => call.startsWith("pr list "))).toHaveLength(2);

‎apps/server/src/orchestration/ThreadSettlementReactor.test.ts‎

Lines changed: 66 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {
22
DEFAULT_SERVER_SETTINGS,
33
EventId,
4-
GitManagerError,
4+
GitCommandError,
55
ProjectId,
66
ProviderInstanceId,
77
ProviderDriverKind,
@@ -1570,13 +1570,17 @@ describe("storage cleanup", () => {
15701570
"recent",
15711571
"merged",
15721572
"unmerged",
1573-
"squash-merged",
1573+
"squash-exact-head",
15741574
"squash-later-commit",
1575-
"squash-other-head",
1576-
"squash-other-base",
1577-
"squash-no-head-sha",
1575+
"squash-release-base",
1576+
"squash-stack-base",
1577+
"squash-missing-head",
15781578
"squash-lookup-failed",
1579-
"squash-unchanged-only",
1579+
"squash-different-branch",
1580+
"squash-stale-merged",
1581+
"squash-closed",
1582+
"squash-head-moved",
1583+
"unchanged-squash",
15801584
"unchanged",
15811585
"unchanged-two-worktrees",
15821586
"diverged",
@@ -1642,6 +1646,31 @@ describe("storage cleanup", () => {
16421646
const thread = makeThread("storage-thread", {
16431647
branch: "feature",
16441648
worktreePath,
1649+
pullRequests:
1650+
protection === "squash-stale-merged"
1651+
? [
1652+
{
1653+
host: "example.test",
1654+
repository: "owner/repository",
1655+
number: 42,
1656+
url: "https://example.test/owner/repository/pull/42",
1657+
source: "manual",
1658+
linkedAt: NOW,
1659+
stack: null,
1660+
snapshot: {
1661+
state: "merged",
1662+
title: "Previously merged",
1663+
headBranch: "feature",
1664+
baseBranch: "main",
1665+
isDraft: false,
1666+
updatedAt: NOW,
1667+
syncedAt: NOW,
1668+
mergedAt: NOW,
1669+
closedAt: null,
1670+
},
1671+
},
1672+
]
1673+
: [],
16451674
latestUserMessageAt:
16461675
protection === "recent" ? "2026-08-26T00:00:00.000Z" : "2026-08-01T00:00:00.000Z",
16471676
...(protection === "session"
@@ -1666,15 +1695,14 @@ describe("storage cleanup", () => {
16661695
const deleteRule = protection.startsWith("deleted");
16671696
let tombstoned = deleteRule && protection !== "deleted-event";
16681697
const removals: string[] = [];
1669-
// A squash merge leaves the checkout's head outside the default branch.
1670-
const squash = protection.startsWith("squash-");
1698+
const squash = protection.startsWith("squash-") || protection === "unchanged-squash";
16711699
const mergeRule =
16721700
protection === "merged" ||
16731701
protection === "unmerged" ||
1674-
(squash && protection !== "squash-unchanged-only");
1702+
protection.startsWith("squash-");
16751703
const unchangedRule =
1676-
protection === "squash-unchanged-only" ||
16771704
protection === "unchanged" ||
1705+
protection === "unchanged-squash" ||
16781706
protection === "unchanged-two-worktrees" ||
16791707
protection === "diverged" ||
16801708
protection === "head-moved";
@@ -1828,27 +1856,37 @@ describe("storage cleanup", () => {
18281856
invalidateStatus: () => Effect.void,
18291857
branchPullRequest: (_input, options) => {
18301858
assert.strictEqual(options?.refresh, true);
1831-
assert.notStrictEqual(protection, "squash-unchanged-only");
1832-
if (protection === "squash-lookup-failed")
1859+
assert.notStrictEqual(protection, "unchanged-squash");
1860+
if (protection === "squash-lookup-failed") {
18331861
return Effect.fail(
1834-
new GitManagerError({
1862+
new GitCommandError({
18351863
operation: "branchPullRequest",
1864+
command: "gh",
18361865
cwd: worktreePath,
1837-
detail: "host unavailable",
1866+
detail: "Host unavailable",
18381867
}),
18391868
);
1840-
const pullRequest = makeBranchPullRequest(
1841-
protection === "unmerged" ? "open" : "merged",
1842-
);
1843-
if (!squash) return Effect.succeed(pullRequest);
1869+
}
18441870
return Effect.succeed({
1845-
...pullRequest,
1846-
headRef: protection === "squash-other-head" ? "saved-feature" : "feature",
1847-
baseRef: protection === "squash-other-base" ? "release" : "main",
1848-
headSha:
1849-
protection === "squash-no-head-sha"
1850-
? null
1851-
: (protection === "squash-later-commit" ? "e" : "a").repeat(40),
1871+
...makeBranchPullRequest(
1872+
protection === "unmerged" || protection === "squash-stale-merged"
1873+
? "open"
1874+
: protection === "squash-closed"
1875+
? "closed"
1876+
: "merged",
1877+
),
1878+
headRef: protection === "squash-different-branch" ? "other" : "feature",
1879+
baseRef:
1880+
protection === "squash-release-base"
1881+
? "release"
1882+
: protection === "squash-stack-base"
1883+
? "stack-parent"
1884+
: "main",
1885+
...(!squash || protection === "squash-missing-head"
1886+
? {}
1887+
: {
1888+
headSha: (protection === "squash-later-commit" ? "c" : "a").repeat(40),
1889+
}),
18521890
});
18531891
},
18541892
}),
@@ -1903,7 +1941,8 @@ describe("storage cleanup", () => {
19031941
headReads++;
19041942
return {
19051943
commitSha:
1906-
protection === "head-moved" && headReads > 1
1944+
(protection === "head-moved" || protection === "squash-head-moved") &&
1945+
headReads > 1
19071946
? "c".repeat(40)
19081947
: "a".repeat(40),
19091948
};
@@ -2042,7 +2081,7 @@ describe("storage cleanup", () => {
20422081
protection === "files-disabled" ||
20432082
protection === "files-extended" ||
20442083
protection === "merged" ||
2045-
protection === "squash-merged" ||
2084+
protection === "squash-exact-head" ||
20462085
protection === "unchanged" ||
20472086
protection === "unchanged-two-worktrees";
20482087
assert.strictEqual(yield* fs.exists(worktreePath), !removed);

‎apps/server/src/sourceControl/GitHubCli.test.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ describe("GitHubCli.listPullRequestsByHead", () => {
232232
url: `https://github.com/acme/web/pull/${number}`,
233233
baseRefName: "main",
234234
headRefName,
235+
headRefOid: "a".repeat(40),
235236
state: "MERGED",
236237
mergedAt: "2026-01-01T00:00:00Z",
237238
updatedAt: "2026-01-02T00:00:00Z",
@@ -286,9 +287,11 @@ describe("GitHubCli.listPullRequestsByHead", () => {
286287
first?.map((pr) => [pr.number, pr.state, pr.headRepositoryNameWithOwner]),
287288
[[7, "merged", "acme/web"]],
288289
);
290+
assert.strictEqual(first?.[0]?.headSha, "a".repeat(40));
289291
assert.deepStrictEqual(second, []);
290292
assert.strictEqual(documents.length, 1);
291293
assert.include(documents[0]!.query, "rateLimit");
294+
assert.include(documents[0]!.query, "headRefOid");
292295
assert.deepStrictEqual(documents[0]!.variables, {
293296
owner: "acme",
294297
name: "web",
@@ -330,6 +333,7 @@ describe("GitHubCli.listPullRequestsByHead", () => {
330333
pullRequests.map((pr) => pr.number),
331334
[8],
332335
);
336+
assert.strictEqual(pullRequests[0]?.headSha, "a".repeat(40));
333337
assert.deepStrictEqual(commands.at(-1), [
334338
"gh",
335339
"pr",

‎apps/server/src/sourceControl/GitHubSourceControlProvider.test.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ it.effect("lists change request history through the batched head lookup", () =>
152152
url: "https://enterprise.test/acme/web/pull/7",
153153
baseRefName: "main",
154154
headRefName: "feature/merged",
155+
headSha: "a".repeat(40),
155156
state: "merged",
156157
mergedAt: "2026-01-01T00:00:00Z",
157158
updatedAt: Option.some(DateTime.makeUnsafe("2026-01-02T00:00:00.000Z")),
@@ -181,6 +182,7 @@ it.effect("lists change request history through the batched head lookup", () =>
181182
});
182183
assert.strictEqual(changeRequests[0]?.provider, "github");
183184
assert.strictEqual(changeRequests[0]?.state, "merged");
185+
assert.strictEqual(changeRequests[0]?.headSha, "a".repeat(40));
184186
assert.strictEqual(changeRequests[0]?.mergedAt, "2026-01-01T00:00:00Z");
185187
assert.deepStrictEqual(
186188
changeRequests[0]?.updatedAt,

0 commit comments

Comments
 (0)