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

storageCleanup.test.ts

106 lines3.3 KB

1import { describe, expect, it } from "vite-plus/test";
2import {
3 ProjectId,
4 ProviderInstanceId,
5 RunId,
6 ThreadId,
7 type OrchestrationV2ThreadShell,
8} from "@t3tools/contracts";
9import * as DateTime from "effect/DateTime";
10import { storageCleanupActivityAt, storageCleanupThreadIdle } from "./storageCleanup.ts";
11​
12const NOW_MS = Date.parse("2026-06-10T12:00:00.000Z");
13const DAY_MS = 24 * 60 * 60 * 1_000;
14​
15function at(offsetMs: number): DateTime.Utc {
16 return DateTime.makeUnsafe(NOW_MS + offsetMs);
18​
19function shell(overrides: Partial<OrchestrationV2ThreadShell> = {}): OrchestrationV2ThreadShell {
20 return {
21 id: ThreadId.make("thread-1"),
22 projectId: ProjectId.make("project-1"),
23 title: "Thread",
24 providerInstanceId: ProviderInstanceId.make("codex"),
25 modelSelection: { instanceId: ProviderInstanceId.make("codex"), model: "gpt-5.4" },
26 runtimeMode: "full-access",
27 interactionMode: "default",
28 worktreePath: null,
29 activeProviderThreadId: null,
30 lineage: {
31 rootThreadId: ThreadId.make("thread-1"),
32 parentThreadId: null,
33 relationshipToParent: null,
34 },
35 forkedFrom: null,
36 createdBy: "user",
37 creationSource: "web",
38 activeRunId: null,
39 latestVisibleMessage: null,
40 hasActionableProposedPlan: false,
41 itemCount: 0,
42 visibleItemCount: 0,
43 lastVisitedAt: null,
44 deletedAt: null,
45 branch: null,
46 linkedPullRequest: null,
47 status: "idle",
48 activityRunStatus: null,
49 pendingRuntimeRequest: null,
50 pendingBackgroundTasks: [],
51 latestRunId: null,
52 latestRunRequestedAt: null,
53 latestRunStartedAt: null,
54 latestRunCompletedAt: null,
55 latestUserMessageAt: null,
56 createdAt: at(-30 * DAY_MS),
57 updatedAt: at(-10 * DAY_MS),
58 archivedAt: null,
59 settledOverride: null,
60 settledAt: null,
61 snoozedUntil: null,
62 snoozedAt: null,
63 pinnedAt: null,
64 ...overrides,
65 };
67​
68describe("V2 storage cleanup eligibility", () => {
69 const candidate = () => shell({ branch: "feature", worktreePath: "/worktrees/feature" });
70​
71 it("allows an idle worktree and rejects the project checkout", () => {
72 expect(storageCleanupThreadIdle(candidate(), NOW_MS)).toBe(true);
73 expect(storageCleanupThreadIdle(shell(), NOW_MS)).toBe(false);
74 });
75​
76 it.each(["running", "starting", "preparing", "waiting", "queued"] as const)(
77 "retains a worktree while its thread is %s",
78 (status) => {
79 expect(storageCleanupThreadIdle(candidateWithStatus(status), NOW_MS)).toBe(false);
80 },
81 );
82​
83 it("retains an active run even if the shell status is idle", () => {
84 expect(
85 storageCleanupThreadIdle({ ...candidate(), activeRunId: RunId.make("run") }, NOW_MS),
86 ).toBe(false);
87 });
88​
89 it("retains a queued prompt before the new run has been projected", () => {
90 expect(
91 storageCleanupThreadIdle({ ...candidate(), latestUserMessageAt: at(-1_000) }, NOW_MS),
92 ).toBe(false);
93 });
94​
95 it("uses V2 run activity instead of metadata refreshes for retention", () => {
96 const thread = candidate();
97 const runTime = at(-3 * DAY_MS);
98 expect(
99 storageCleanupActivityAt({ ...thread, latestRunCompletedAt: runTime, updatedAt: at(0) }),
100 ).toBe(DateTime.toEpochMillis(runTime));
101 });
102​
103 function candidateWithStatus(status: OrchestrationV2ThreadShell["status"]) {
104 return { ...candidate(), status };
105 }
106});