Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

storageCleanup.test.ts

198 lines6.3 KB

1import { describe, expect, it } from "vite-plus/test";
2import {
3 ProjectId,
4 ProviderInstanceId,
5 RunId,
6 RuntimeRequestId,
7 ThreadId,
8 type OrchestrationV2ThreadShell,
9} from "@t3tools/contracts";
10import * as DateTime from "effect/DateTime";
11import {
12 storageCleanupActivityAt,
13 storageCleanupPullRequestMerged,
14 storageCleanupThreadIdle,
15} from "./storageCleanup.ts";
16​
17const NOW_MS = Date.parse("2026-06-10T12:00:00.000Z");
18const DAY_MS = 24 * 60 * 60 * 1_000;
19​
20function at(offsetMs: number): DateTime.Utc {
21 return DateTime.makeUnsafe(NOW_MS + offsetMs);
23​
24function shell(overrides: Partial<OrchestrationV2ThreadShell> = {}): OrchestrationV2ThreadShell {
25 return {
26 id: ThreadId.make("thread-1"),
27 projectId: ProjectId.make("project-1"),
28 title: "Thread",
29 providerInstanceId: ProviderInstanceId.make("codex"),
30 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
31 runtimeMode: "full-access",
32 interactionMode: "default",
33 worktreePath: null,
34 activeProviderThreadId: null,
35 lineage: {
36 rootThreadId: ThreadId.make("thread-1"),
37 parentThreadId: null,
38 relationshipToParent: null,
39 },
40 forkedFrom: null,
41 createdBy: "user",
42 creationSource: "web",
43 activeRunId: null,
44 latestVisibleMessage: null,
45 hasActionableProposedPlan: false,
46 itemCount: 0,
47 visibleItemCount: 0,
48 lastVisitedAt: null,
49 deletedAt: null,
50 branch: null,
51 linkedPullRequest: null,
52 status: "idle",
53 activityRunStatus: null,
54 pendingRuntimeRequest: null,
55 pendingBackgroundTasks: [],
56 latestRunId: null,
57 latestRunRequestedAt: null,
58 latestRunStartedAt: null,
59 latestRunCompletedAt: null,
60 latestUserMessageAt: null,
61 createdAt: at(-30 * DAY_MS),
62 updatedAt: at(-10 * DAY_MS),
63 archivedAt: null,
64 settledOverride: null,
65 settledAt: null,
66 snoozedUntil: null,
67 snoozedAt: null,
68 pinnedAt: null,
69 ...overrides,
70 };
72​
73describe("V2 storage cleanup eligibility", () => {
74 const candidate = () => shell({ branch: "feature", worktreePath: "/worktrees/feature" });
75​
76 it("allows an idle worktree and rejects the project checkout", () => {
77 expect(storageCleanupThreadIdle(candidate(), NOW_MS)).toBe(true);
78 expect(storageCleanupThreadIdle(shell(), NOW_MS)).toBe(false);
79 });
80​
81 it.each(["running", "starting", "preparing", "waiting", "queued"] as const)(
82 "retains a worktree while its thread is %s",
83 (status) => {
84 expect(storageCleanupThreadIdle(candidateWithStatus(status), NOW_MS)).toBe(false);
85 },
86 );
87​
88 it.each(["idle", "completed", "interrupted", "failed", "cancelled", "rolled_back"] as const)(
89 "allows cleanup once its thread is %s",
90 (status) => {
91 expect(storageCleanupThreadIdle(candidateWithStatus(status), NOW_MS)).toBe(true);
92 },
93 );
94​
95 it.each(["completed", "interrupted", "cancelled", "rolled_back"] as const)(
96 "retains %s while background work is pending",
97 (status) => {
98 expect(
99 storageCleanupThreadIdle(
100 {
101 ...candidateWithStatus(status),
102 pendingBackgroundTasks: [{ taskId: "task-1", kind: "command" }],
103 },
104 NOW_MS,
105 ),
106 ).toBe(false);
107 },
108 );
109​
110 it.each(["completed", "interrupted", "cancelled", "rolled_back"] as const)(
111 "retains %s while a runtime request is pending",
112 (status) => {
113 expect(
114 storageCleanupThreadIdle(
115 {
116 ...candidateWithStatus(status),
117 pendingRuntimeRequest: {
118 id: RuntimeRequestId.make("request-1"),
119 kind: "command",
120 createdAt: at(0),
121 },
122 },
123 NOW_MS,
124 ),
125 ).toBe(false);
126 },
127 );
128​
129 it("retains an active run even if the shell status is idle", () => {
130 expect(
131 storageCleanupThreadIdle({ ...candidate(), activeRunId: RunId.make("run") }, NOW_MS),
132 ).toBe(false);
133 });
134​
135 it("retains a queued prompt before the new run has been projected", () => {
136 expect(
137 storageCleanupThreadIdle({ ...candidate(), latestUserMessageAt: at(-1_000) }, NOW_MS),
138 ).toBe(false);
139 });
140​
141 it("uses V2 run activity instead of metadata refreshes for retention", () => {
142 const thread = candidate();
143 const runTime = at(-3 * DAY_MS);
144 expect(
145 storageCleanupActivityAt({ ...thread, latestRunCompletedAt: runTime, updatedAt: at(0) }),
146 ).toBe(DateTime.toEpochMillis(runTime));
147 });
148​
149 function candidateWithStatus(status: OrchestrationV2ThreadShell["status"]) {
150 return { ...candidate(), status };
151 }
152});
153​
154describe("merged pull request cleanup", () => {
155 const HEAD_SHA = "a".repeat(40);
156 const integrated = {
157 branch: "feature",
158 defaultBranch: "main",
159 headSha: HEAD_SHA,
160 integrated: true,
161 };
162 const squashed = { ...integrated, integrated: false };
163 const pullRequest = (
164 overrides: Partial<NonNullable<Parameters<typeof storageCleanupPullRequestMerged>[0]>> = {},
165 ) => ({
166 state: "merged" as const,
167 headRef: "feature",
168 baseRef: "main",
169 headSha: HEAD_SHA,
170 ...overrides,
171 });
172​
173 it("removes a worktree whose head reached the default branch through a merged pull request", () => {
174 expect(storageCleanupPullRequestMerged(pullRequest({ headSha: null }), integrated)).toBe(true);
175 });
176​
177 it("removes a squash-merged worktree when the pull request names its exact head", () => {
178 expect(storageCleanupPullRequestMerged(pullRequest(), squashed)).toBe(true);
179 });
180​
181 it.each([
182 ["has a later commit than the merged head", { headSha: "c".repeat(40) }],
183 ["was merged into a release branch", { baseRef: "release" }],
184 ["was merged into its stack parent", { baseRef: "stack-parent" }],
185 ["was merged without a reported head commit", { headSha: null }],
186 ["belongs to a different branch", { headRef: "other" }],
187 ["is still open", { state: "open" }],
188 ["was closed without merging", { state: "closed" }],
189 ] as const)("keeps a squash worktree whose pull request %s", (_name, overrides) => {
190 expect(storageCleanupPullRequestMerged(pullRequest(overrides), squashed)).toBe(false);
191 });
192​
193 it("keeps a worktree with no pull request, or one that is not merged", () => {
194 expect(storageCleanupPullRequestMerged(null, squashed)).toBe(false);
195 expect(storageCleanupPullRequestMerged(null, integrated)).toBe(false);
196 expect(storageCleanupPullRequestMerged(pullRequest({ state: "open" }), integrated)).toBe(false);
197 });
198});