@@ -8,6 +8,7 @@ const state = vi.hoisted(() => ({
88 settings : { } as typeof DEFAULT_SERVER_SETTINGS ,
99 run : vi . fn ( ) ,
1010 update : vi . fn ( ) ,
11+ targets : [ ] as never [ ] ,
1112 report : null as StorageCleanupReport | null ,
1213} ) ) ;
1314
@@ -53,7 +54,7 @@ vi.mock(import("./SettingsScopeContext"), async (original) => ({
5354 scope : { kind : "environment" } ,
5455 environments : [ environment ] ,
5556 connectedEnvironments : [ environment ] ,
56- targets : [ ] ,
57+ targets : state . targets ,
5758 target : null ,
5859 } ;
5960 } ) as never ,
@@ -103,6 +104,7 @@ function setup() {
103104 projectSettingsOverrides : { } ,
104105 } ;
105106 state . report = null ;
107+ state . targets = [ ] ;
106108 state . run . mockReset ( ) ;
107109 state . update . mockReset ( ) ;
108110}
@@ -143,6 +145,12 @@ async function openDraft(label: string) {
143145 await act ( async ( ) => ruleSwitch ( screen . container , label ) . click ( ) ) ;
144146 return screen ;
145147}
148+ function expectSaved ( rule : Record < string , number | null > ) {
149+ expect ( state . update ) . toHaveBeenCalledExactlyOnceWith ( { storageCleanup : rule } ) ;
150+ }
151+ function retentionTarget ( environmentId : string ) {
152+ return [ { environmentId, projectId : null , sources : { } , settings : state . settings } ] as never [ ] ;
153+ }
146154
147155it . each ( RETENTION_RULES ) ( "turning %s on does not save 8 days" , async ( label ) => {
148156 setup ( ) ;
@@ -160,33 +168,25 @@ it("saves a drafted retention age only when that age is committed", async () =>
160168 setInputValue ( input , "30" ) ;
161169 input . blur ( ) ;
162170 } ) ;
163- expect ( state . update ) . toHaveBeenCalledExactlyOnceWith ( {
164- storageCleanup : { worktreeAfterDays : 30 } ,
165- } ) ;
171+ expectSaved ( { worktreeAfterDays : 30 } ) ;
166172 expect ( daysInput ( screen . container , inactive ) ?. value ) . toBe ( "30" ) ;
167173} ) ;
168174
169175it ( "saves the visible default when Enter commits an untouched retention draft" , async ( ) => {
170176 setup ( ) ;
171177 await using screen = await openDraft ( inactive ) ;
172178 await act ( async ( ) => pressKey ( daysInput ( screen . container , inactive ) ! , "Enter" ) ) ;
173- expect ( state . update ) . toHaveBeenCalledExactlyOnceWith ( {
174- storageCleanup : { worktreeAfterDays : 8 } ,
175- } ) ;
179+ expectSaved ( { worktreeAfterDays : 8 } ) ;
176180} ) ;
177-
178181it ( "commits the parsed retention age when Enter is pressed on grouped text" , async ( ) => {
179182 setup ( ) ;
180183 await using screen = await openDraft ( inactive ) ;
181184 const input = daysInput ( screen . container , inactive ) ! ;
182185 await act ( async ( ) => setInputValue ( input , "1000" ) ) ;
183186 input . value = "1.000" ;
184187 await act ( async ( ) => pressKey ( input , "Enter" ) ) ;
185- expect ( state . update ) . toHaveBeenCalledExactlyOnceWith ( {
186- storageCleanup : { worktreeAfterDays : 1000 } ,
187- } ) ;
188+ expectSaved ( { worktreeAfterDays : 1000 } ) ;
188189} ) ;
189-
190190it ( "returns a retention rule to off when saving its age fails" , async ( ) => {
191191 setup ( ) ;
192192 state . update . mockResolvedValue ( false ) ;
@@ -196,12 +196,9 @@ it("returns a retention rule to off when saving its age fails", async () => {
196196 setInputValue ( input , "30" ) ;
197197 input . blur ( ) ;
198198 } ) ;
199- expect ( state . update ) . toHaveBeenCalledExactlyOnceWith ( {
200- storageCleanup : { worktreeAfterDays : 30 } ,
201- } ) ;
199+ expectSaved ( { worktreeAfterDays : 30 } ) ;
202200 expect ( daysInput ( screen . container , inactive ) ) . toBeNull ( ) ;
203201} ) ;
204-
205202it ( "keeps a retention rule off when its draft is left or cancelled" , async ( ) => {
206203 setup ( ) ;
207204 await using screen = await openDraft ( inactive ) ;
@@ -223,11 +220,8 @@ it("saves a retention step from the drafted age", async () => {
223220 await using screen = await openDraft ( label ) ;
224221 const step = screen . container . querySelector < HTMLElement > ( `[aria-label="Increase ${ label } "]` ) ! ;
225222 await act ( async ( ) => step . click ( ) ) ;
226- expect ( state . update ) . toHaveBeenCalledExactlyOnceWith ( {
227- storageCleanup : { logsAfterDays : 9 } ,
228- } ) ;
223+ expectSaved ( { logsAfterDays : 9 } ) ;
229224} ) ;
230-
231225it ( "does not save a drafted age when that rule's switch is clicked" , async ( ) => {
232226 setup ( ) ;
233227 await using screen = await openDraft ( inactive ) ;
@@ -245,25 +239,27 @@ it("does not save a drafted age when that rule's switch is clicked", async () =>
245239it ( "turns an enabled retention rule off immediately and edits it without saving 8" , async ( ) => {
246240 const label = "Delete old browser artifacts" ;
247241 setup ( ) ;
248- state . settings = {
249- ...state . settings ,
250- storageCleanup : { ...state . settings . storageCleanup , browserArtifactsAfterDays : 30 } ,
251- } ;
242+ Object . assign ( state . settings . storageCleanup , { browserArtifactsAfterDays : 30 } ) ;
252243 await using screen = await render ( panel ( ) ) ;
253244 const input = daysInput ( screen . container , label ) ! ;
254245 await act ( async ( ) => {
255246 input . focus ( ) ;
256247 setInputValue ( input , "14" ) ;
257248 input . blur ( ) ;
258249 } ) ;
259- expect ( state . update ) . toHaveBeenCalledExactlyOnceWith ( {
260- storageCleanup : { browserArtifactsAfterDays : 14 } ,
261- } ) ;
250+ expectSaved ( { browserArtifactsAfterDays : 14 } ) ;
262251 state . update . mockClear ( ) ;
263252 await act ( async ( ) => ruleSwitch ( screen . container , label ) . click ( ) ) ;
264- expect ( state . update ) . toHaveBeenCalledExactlyOnceWith ( {
265- storageCleanup : { browserArtifactsAfterDays : null } ,
266- } ) ;
253+ expectSaved ( { browserArtifactsAfterDays : null } ) ;
254+ } ) ;
255+ it ( "drops a retention draft when the settings scope changes" , async ( ) => {
256+ setup ( ) ;
257+ state . targets = retentionTarget ( "a" ) ;
258+ await using screen = await openDraft ( inactive ) ;
259+ state . targets = retentionTarget ( "b" ) ;
260+ await act ( async ( ) => screen . rerender ( panel ( ) ) ) ;
261+ expect ( daysInput ( screen . container , inactive ) ) . toBeNull ( ) ;
262+ expect ( state . update ) . not . toHaveBeenCalled ( ) ;
267263} ) ;
268264
269265it ( "keeps cleanup disabled when settled retention and all other rules are off" , async ( ) => {
0 commit comments