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

StorageSettings.test.tsx

237 lines7.0 KB

1// @vitest-environment jsdom
2import { RegistryProvider } from "@effect/atom-react";
3import {
4 DEFAULT_SERVER_SETTINGS,
5 ProjectId,
6 ThreadId,
7 type StorageCleanupReport,
8} from "@t3tools/contracts";
9import { act, type ReactNode } from "react";
10import { createRoot, type Root } from "react-dom/client";
11import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
12​
13const state = vi.hoisted(() => ({
14 settings: {} as typeof DEFAULT_SERVER_SETTINGS,
15 run: vi.fn(),
16 report: null as StorageCleanupReport | null,
17}));
18​
19vi.mock("../../state/server", async () => {
20 const { Atom } = await import("effect/reactivity");
21 const report = Atom.make(() => ({ _tag: "Success", value: state.report, waiting: false }));
22 const allowed = Atom.make(true);
23 return {
24 serverEnvironment: {
25 storageCleanupReport: () => report,
26 runStorageCleanup: { permissionAtom: () => allowed },
27 },
28 };
29});
30vi.mock("../../state/use-atom-command", () => ({ useAtomCommand: () => state.run }));
31vi.mock("./SettingsScopeContext", () => ({
32 useSettingsScope: () => {
33 const environment = {
34 environmentId: "env",
35 label: "Machine",
36 serverConfig: {
37 settings: state.settings,
38 environment: {
39 capabilities: {
40 storageCleanup: true,
41 storageCleanupRun: true,
42 settledWorktreeCleanup: true,
43 },
44 },
45 },
46 };
47 return {
48 scope: { kind: "environment" },
49 environments: [environment],
50 connectedEnvironments: [environment],
51 targets: [],
52 target: null,
53 };
54 },
55}));
56vi.mock("./useScopedSettings", () => ({
57 useScopedSettings: () => state.settings,
58 useScopedSettingsMixed: () => false,
59 useUpdateScopedSettings: () => vi.fn(),
60 useClearScopedSettings: () => vi.fn(),
61}));
62vi.mock("./settingsLayout", () => ({
63 SettingsPageContainer: ({ children }: { children: ReactNode }) => children,
64 SettingsSection: ({ children }: { children: ReactNode }) => children,
65 SettingsRow: ({
66 title,
67 description,
68 control,
69 }: {
70 title: ReactNode;
71 description: ReactNode;
72 control: ReactNode;
73 }) => (
74 <div>
75 <h3>{title}</h3>
76 {description}
77 {control}
78 </div>
79 ),
80 SettingResetButton: () => null,
81 useRelativeTimeTick: () => undefined,
82 useSettingsSearchTargetId: () => null,
83 useSettingsSearchTarget: () => undefined,
84}));
85vi.mock("../ui/toast", () => ({ toastManager: { add: vi.fn() } }));
86​
87import { StorageSettingsPanel } from "./StorageSettings";
88​
89let root: Root;
90let container: HTMLDivElement;
91​
92beforeEach(() => {
93 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
94 state.settings = {
95 ...DEFAULT_SERVER_SETTINGS,
96 storageCleanup: {
97 worktreeKeepWhen: "uncommitted-changes",
98 worktreeAfterDays: null,
99 worktreeSettledAfterDays: null,
100 worktreeOnMerge: false,
101 worktreeOnDelete: false,
102 worktreeUnchanged: false,
103 browserArtifactsAfterDays: null,
104 logsAfterDays: null,
105 },
106 projectSettingsOverrides: {},
107 };
108 state.report = null;
109 state.run.mockReset();
110 container = document.createElement("div");
111 document.body.append(container);
112 root = createRoot(container);
113});
114​
115afterEach(async () => {
116 await act(async () => root.unmount());
117 container.remove();
118 vi.unstubAllGlobals();
119});
120​
121async function render() {
122 await act(async () =>
123 root.render(
124 <RegistryProvider>
125 <StorageSettingsPanel />
126 </RegistryProvider>,
127 ),
128 );
129 return Array.from(container.querySelectorAll("button")).find(
130 (button) => button.textContent === "Delete now",
131 )!;
133​
134it.each(["machine-wide immediate", "project custom"])(
135 "runs cleanup when only %s settled retention is enabled",
136 async (scope) => {
137 if (scope === "machine-wide immediate") {
138 state.settings = {
139 ...state.settings,
140 storageCleanup: { ...state.settings.storageCleanup, worktreeSettledAfterDays: 0 },
141 };
142 } else {
143 state.settings = {
144 ...state.settings,
145 projectSettingsOverrides: {
146 [ProjectId.make("project")]: {
147 worktreeCleanup: {
148 mode: "custom",
149 rules: { ...state.settings.storageCleanup, worktreeSettledAfterDays: 7 },
150 },
151 },
152 },
153 };
154 }
155 const report: StorageCleanupReport = {
156 trigger: "manual",
157 startedAt: "2026-10-14T12:00:00.000Z",
158 finishedAt: "2026-10-14T12:00:01.000Z",
159 entries: [],
160 counts: { removed: 0, kept: 0, failed: 0 },
161 omittedCount: 0,
162 bytesFreed: 0,
163 };
164 let complete!: (result: { _tag: "Success"; value: StorageCleanupReport }) => void;
165 state.run.mockImplementation(() => new Promise((resolve) => (complete = resolve)));
166 const button = await render();
167​
168 expect(button.disabled).toBe(false);
169 await act(async () => button.click());
170 expect(state.run).toHaveBeenCalledExactlyOnceWith({ environmentId: "env", input: {} });
171 expect(button.disabled).toBe(true);
172 expect(button.textContent).toBe("Deleting…");
173​
174 await act(async () => complete({ _tag: "Success", value: report }));
175 expect(button.disabled).toBe(false);
176 expect(button.textContent).toBe("Delete now");
177 },
178);
179​
180it("keeps cleanup disabled when settled retention and all other rules are off", async () => {
181 const button = await render();
182​
183 expect(button.disabled).toBe(true);
184 await act(async () => button.click());
185 expect(state.run).not.toHaveBeenCalled();
186});
187​
188it("shows an orphan's full path and size while keeping thread row titles unchanged", async () => {
189 state.settings = {
190 ...state.settings,
191 storageCleanup: { ...state.settings.storageCleanup, worktreeSettledAfterDays: 0 },
192 };
193 state.report = {
194 trigger: "manual",
195 startedAt: "2026-10-14T12:00:00.000Z",
196 finishedAt: "2026-10-14T12:00:01.000Z",
197 entries: [
198 {
199 kind: "worktree",
200 outcome: "kept",
201 path: "/worktrees/repo/orphan",
202 threadId: null,
203 threadTitle: null,
204 reason: "No thread uses this folder, and it isn't a git worktree",
205 bytes: 6600000,
206 files: null,
207 },
208 {
209 kind: "worktree",
210 outcome: "kept",
211 path: "/worktrees/repo/thread-checkout",
212 threadId: ThreadId.make("thread"),
213 threadTitle: "Thread title",
214 reason: "Thread is running or has pending work",
215 bytes: null,
216 files: null,
217 },
218 ],
219 counts: { removed: 0, kept: 2, failed: 0 },
220 omittedCount: 0,
221 bytesFreed: 0,
222 };
223 await render();
224 const lastRun = Array.from(container.querySelectorAll("button")).find((button) =>
225 button.textContent?.includes("Last run"),
226 )!;
227 await act(async () => lastRun.click());
228 const titles = Array.from(container.querySelectorAll("h3"), (heading) => heading.textContent);
229 expect(titles).toContain("/worktrees/repo/orphan");
230 expect(titles).toContain("thread-checkout");
231 expect(titles).not.toContain("/worktrees/repo/thread-checkout");
232 expect(container.textContent).toContain(
233 "No thread uses this folder, and it isn't a git worktree",
234 );
235 expect(container.textContent).toContain("6.29 MB");
236 expect(container.textContent).toContain("0 B recovered.");
237});