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}));
67// The real page container needs a router and the real row reads write permission and
68// overrides from live environment state; this suite is about the cleanup panel.
69vi.mock(import("./settingsLayout"), async (original) => ({
70 ...(await original()),
71 SettingsPageContainer: (({ children }: { children: ReactNode }) => children) as never,
72 SettingsRow: ({ title, description, control, children }) => (
73 <div>
74 <h3>{title}</h3>
75 {description}
76 {control}
77 {children}
78 </div>
79 ),
80 SettingResetButton: (() => null) as never,
81}));
82vi.mock(import("../ui/toast"), async (original) => {
83 const actual = await original();
84 return { ...actual, toastManager: { ...actual.toastManager, add: vi.fn() } };
85});
86
87import { StorageSettingsPanel } from "./StorageSettings";
88
89function setup() {
90 state.settings = {
91 ...DEFAULT_SERVER_SETTINGS,
92 storageCleanup: {
93 worktreeKeepWhen: "uncommitted-changes",
94 worktreeAfterDays: null,
95 worktreeSettledAfterDays: null,
96 worktreeOnMerge: false,
97 worktreeOnDelete: false,
98 worktreeUnchanged: false,
99 browserArtifactsAfterDays: null,
100 logsAfterDays: null,
101 },
102 projectSettingsOverrides: {},
103 };
104 state.report = null;
105 state.run.mockReset();
106}
107
108const panel = () => (
109 <RegistryProvider>
110 <StorageSettingsPanel />
111 </RegistryProvider>
112);
113const deleteButton = (container: HTMLElement) =>
114 Array.from(container.querySelectorAll("button")).find(
115 (button) => button.textContent === "Delete now",
116 )!;
117
118it("keeps cleanup disabled when settled retention and all other rules are off", async () => {
119 setup();
120 await using screen = await render(panel());
121 const button = deleteButton(screen.container);
122
123 expect(button.disabled).toBe(true);
124 await act(async () => button.click());
125 expect(state.run).not.toHaveBeenCalled();
126});