Open History to see the last commit
1import { act, render } from "../../../test/render";
2import { RegistryProvider } from "@effect/atom-react";
3import { DEFAULT_SERVER_SETTINGS, type StorageCleanupReport } from "@t3tools/contracts";
4import { type ReactNode } from "react";
5import { expect, it, vi } from "vite-plus/test";
6
7const state = vi.hoisted(() => ({
8 settings: {} as typeof DEFAULT_SERVER_SETTINGS,
9 run: vi.fn(),
10 report: null as StorageCleanupReport | null,
11}));
12
13vi.mock(import("../../state/server"), async (original) => {
14 const actual = await original();
15 const { Atom } = await import("effect/reactivity");
16 const report = Atom.make(() => ({ _tag: "Success", value: state.report, waiting: false }));
17 const allowed = Atom.make(true);
18 return {
19 ...actual,
20 serverEnvironment: {
21 ...actual.serverEnvironment,
22 storageCleanupReport: (() => report) as never,
23 runStorageCleanup: {
24 ...actual.serverEnvironment.runStorageCleanup,
25 permissionAtom: () => allowed,
26 },
27 },
28 };
29});
30vi.mock(import("../../state/use-atom-command"), async (original) => ({
31 ...(await original()),
32 useAtomCommand: (() => state.run) as never,
33}));
34vi.mock(import("./SettingsScopeContext"), async (original) => ({
35 ...(await original()),
36 useSettingsScope: (() => {
37 const environment = {
38 environmentId: "env",
39 label: "Build workstation",
40 serverConfig: {
41 settings: state.settings,
42 environment: {
43 capabilities: {
44 storageCleanup: true,
45 storageCleanupRun: true,
46 settledWorktreeCleanup: true,
47 },
48 },
49 },
50 };
51 return {
52 scope: { kind: "environment" },
53 environments: [environment],
54 connectedEnvironments: [environment],
55 targets: [],
56 target: null,
57 };
58 }) as never,
59}));
60vi.mock(import("./useScopedSettings"), async (original) => ({
61 ...(await original()),
62 useScopedSettings: (() => state.settings) as never,
63 useScopedSettingsMixed: () => false,
64 useUpdateScopedSettings: () => vi.fn(),
65 useClearScopedSettings: () => vi.fn(),
66}));
67vi.mock(import("./settingsLayout"), async (original) => ({
68 ...(await original()),
69 SettingsPageContainer: (({ children }: { children: ReactNode }) => children) as never,
70 SettingsRow: ({ title, description, control, children }) => (
71 <div>
72 <h3>{title}</h3>
73 {description}
74 {control}
75 {children}
76 </div>
77 ),
78 SettingResetButton: (() => null) as never,
79}));
80vi.mock(import("../ui/toast"), async (original) => {
81 const actual = await original();
82 return { ...actual, toastManager: { ...actual.toastManager, add: vi.fn() } };
83});
84
85import { StorageCleanupSections } from "./StorageSettings";
86
87function setup() {
88 state.settings = {
89 ...DEFAULT_SERVER_SETTINGS,
90 storageCleanup: {
91 worktreeKeepWhen: "uncommitted-changes",
92 worktreeAfterDays: null,
93 worktreeSettledAfterDays: null,
94 worktreeOnMerge: false,
95 worktreeOnDelete: false,
96 worktreeUnchanged: false,
97 browserArtifactsAfterDays: null,
98 logsAfterDays: null,
99 },
100 projectSettingsOverrides: {},
101 };
102 state.report = null;
103 state.run.mockReset();
104}
105
106const panel = () => (
107 <RegistryProvider>
108 <StorageCleanupSections />
109 </RegistryProvider>
110);
111const deleteButton = (container: HTMLElement) =>
112 Array.from(container.querySelectorAll("button")).find(
113 (button) => button.textContent === "Delete now",
114 )!;
115
116it("keeps cleanup disabled when settled retention and all other rules are off", async () => {
117 setup();
118 await using screen = await render(panel());
119 const button = deleteButton(screen.container);
120
121 expect(button.disabled).toBe(true);
122 await act(async () => button.click());
123 expect(state.run).not.toHaveBeenCalled();
124});