@@ -13,6 +13,7 @@ import { page } from "vite-plus/test/browser";
1313const state = vi . hoisted ( ( ) => ( {
1414 settings : { } as typeof DEFAULT_SERVER_SETTINGS ,
1515 run : vi . fn ( ) ,
16+ update : vi . fn ( ) ,
1617 report : null as StorageCleanupReport | null ,
1718} ) ) ;
1819
@@ -67,7 +68,7 @@ vi.mock(import("./useScopedSettings"), async (original) => ({
6768 ...( await original ( ) ) ,
6869 useScopedSettings : ( ( ) => state . settings ) as never ,
6970 useScopedSettingsMixed : ( ) => false ,
70- useUpdateScopedSettings : ( ) => vi . fn ( ) ,
71+ useUpdateScopedSettings : ( ) => state . update ,
7172 useClearScopedSettings : ( ) => vi . fn ( ) ,
7273} ) ) ;
7374// The real page container needs a router and the real row reads write permission and
@@ -95,7 +96,14 @@ import { StorageSettingsPanel } from "./StorageSettings";
9596let root : Root ;
9697let container : HTMLDivElement ;
9798
99+ function setInputValue ( input : HTMLInputElement , value : string ) {
100+ const prototype = Object . getOwnPropertyDescriptor ( HTMLInputElement . prototype , "value" ) ;
101+ prototype ?. set ?. call ( input , value ) ;
102+ input . dispatchEvent ( new Event ( "input" , { bubbles : true } ) ) ;
103+ }
104+
98105beforeEach ( ( ) => {
106+ vi . stubGlobal ( "IS_REACT_ACT_ENVIRONMENT" , true ) ;
99107 state . settings = {
100108 ...DEFAULT_SERVER_SETTINGS ,
101109 storageCleanup : {
@@ -112,6 +120,7 @@ beforeEach(() => {
112120 } ;
113121 state . report = null ;
114122 state . run . mockReset ( ) ;
123+ state . update . mockReset ( ) ;
115124 container = document . createElement ( "div" ) ;
116125 document . body . append ( container ) ;
117126 root = createRoot ( container ) ;
@@ -120,6 +129,7 @@ beforeEach(() => {
120129afterEach ( async ( ) => {
121130 await act ( async ( ) => root . unmount ( ) ) ;
122131 container . remove ( ) ;
132+ vi . unstubAllGlobals ( ) ;
123133} ) ;
124134
125135async function render ( ) {
@@ -181,6 +191,129 @@ it.each(["machine-wide immediate", "project custom"])(
181191 } ,
182192) ;
183193
194+ const RETENTION_RULES = [
195+ [ "Delete inactive worktrees" , "worktreeAfterDays" ] ,
196+ [ "Delete settled worktrees" , "worktreeSettledAfterDays" ] ,
197+ [ "Delete old browser artifacts" , "browserArtifactsAfterDays" ] ,
198+ [ "Delete old rotated logs" , "logsAfterDays" ] ,
199+ ] as const ;
200+
201+ function retentionSwitch ( label : string ) {
202+ const element = container . querySelector < HTMLElement > ( `[role="switch"][aria-label="${ label } "]` ) ;
203+ if ( ! element ) throw new Error ( `Missing switch ${ label } ` ) ;
204+ return element ;
205+ }
206+
207+ function daysInput ( label : string ) {
208+ return container . querySelector < HTMLInputElement > ( `input[aria-label="${ label } in days"]` ) ;
209+ }
210+
211+ it . each ( RETENTION_RULES ) ( "turning %s on does not save 8 days" , async ( label ) => {
212+ await render ( ) ;
213+
214+ await act ( async ( ) => retentionSwitch ( label ) . click ( ) ) ;
215+
216+ expect ( state . update ) . not . toHaveBeenCalled ( ) ;
217+ expect ( retentionSwitch ( label ) . getAttribute ( "aria-checked" ) ) . toBe ( "true" ) ;
218+ expect ( daysInput ( label ) ?. value ) . toBe ( "8" ) ;
219+ } ) ;
220+
221+ it ( "saves a drafted retention age only when that age is committed" , async ( ) => {
222+ const label = "Delete inactive worktrees" ;
223+ await render ( ) ;
224+ await act ( async ( ) => retentionSwitch ( label ) . click ( ) ) ;
225+ const input = daysInput ( label ) ! ;
226+
227+ await act ( async ( ) => {
228+ setInputValue ( input , "30" ) ;
229+ input . blur ( ) ;
230+ } ) ;
231+
232+ expect ( state . update ) . toHaveBeenCalledExactlyOnceWith ( {
233+ storageCleanup : { worktreeAfterDays : 30 } ,
234+ } ) ;
235+ expect ( daysInput ( label ) ?. value ) . toBe ( "30" ) ;
236+ } ) ;
237+
238+ it ( "saves the visible default when Enter commits an untouched retention draft" , async ( ) => {
239+ const label = "Delete inactive worktrees" ;
240+ await render ( ) ;
241+ await act ( async ( ) => retentionSwitch ( label ) . click ( ) ) ;
242+
243+ await act ( async ( ) => {
244+ daysInput ( label ) ?. dispatchEvent (
245+ new KeyboardEvent ( "keydown" , { key : "Enter" , bubbles : true , cancelable : true } ) ,
246+ ) ;
247+ } ) ;
248+
249+ expect ( state . update ) . toHaveBeenCalledExactlyOnceWith ( {
250+ storageCleanup : { worktreeAfterDays : 8 } ,
251+ } ) ;
252+ } ) ;
253+
254+ it ( "keeps a retention rule off when its draft is left or cancelled" , async ( ) => {
255+ const label = "Delete inactive worktrees" ;
256+ await render ( ) ;
257+ await act ( async ( ) => retentionSwitch ( label ) . click ( ) ) ;
258+ const outside = document . createElement ( "button" ) ;
259+ document . body . append ( outside ) ;
260+
261+ await act ( async ( ) => outside . focus ( ) ) ;
262+
263+ expect ( state . update ) . not . toHaveBeenCalled ( ) ;
264+ expect ( daysInput ( label ) ) . toBeNull ( ) ;
265+ outside . remove ( ) ;
266+
267+ await act ( async ( ) => retentionSwitch ( label ) . click ( ) ) ;
268+ await act ( async ( ) => {
269+ daysInput ( label ) ?. dispatchEvent (
270+ new KeyboardEvent ( "keydown" , { key : "Escape" , bubbles : true , cancelable : true } ) ,
271+ ) ;
272+ } ) ;
273+
274+ expect ( state . update ) . not . toHaveBeenCalled ( ) ;
275+ expect ( daysInput ( label ) ) . toBeNull ( ) ;
276+ } ) ;
277+
278+ it ( "saves a retention step from the drafted age" , async ( ) => {
279+ const label = "Delete old rotated logs" ;
280+ await render ( ) ;
281+ await act ( async ( ) => retentionSwitch ( label ) . click ( ) ) ;
282+
283+ await act ( async ( ) => {
284+ container . querySelector < HTMLElement > ( `[aria-label="Increase ${ label } "]` ) ?. click ( ) ;
285+ } ) ;
286+
287+ expect ( state . update ) . toHaveBeenCalledExactlyOnceWith ( {
288+ storageCleanup : { logsAfterDays : 9 } ,
289+ } ) ;
290+ } ) ;
291+
292+ it ( "turns an enabled retention rule off immediately and edits it without saving 8" , async ( ) => {
293+ const label = "Delete old browser artifacts" ;
294+ state . settings = {
295+ ...state . settings ,
296+ storageCleanup : { ...state . settings . storageCleanup , browserArtifactsAfterDays : 30 } ,
297+ } ;
298+ await render ( ) ;
299+ const input = daysInput ( label ) ! ;
300+
301+ await act ( async ( ) => {
302+ input . focus ( ) ;
303+ setInputValue ( input , "14" ) ;
304+ input . blur ( ) ;
305+ } ) ;
306+ expect ( state . update ) . toHaveBeenCalledExactlyOnceWith ( {
307+ storageCleanup : { browserArtifactsAfterDays : 14 } ,
308+ } ) ;
309+
310+ state . update . mockClear ( ) ;
311+ await act ( async ( ) => retentionSwitch ( label ) . click ( ) ) ;
312+ expect ( state . update ) . toHaveBeenCalledExactlyOnceWith ( {
313+ storageCleanup : { browserArtifactsAfterDays : null } ,
314+ } ) ;
315+ } ) ;
316+
184317it ( "keeps cleanup disabled when settled retention and all other rules are off" , async ( ) => {
185318 const button = await render ( ) ;
186319
0 commit comments