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

StorageSettings.browser.test.tsx

240 lines7.4 KB

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