@@ -15,8 +15,30 @@ import {
1515 stubRenderedWhenDefined ,
1616} from "./app-host.test-support.ts" ;
1717import "./app-host.ts" ;
18- import { DEBUG_OVERLAY_ELEMENT , KEYBOARD_SHORTCUTS_ELEMENT } from "./lazy-custom-element.ts" ;
19- import { readLazyShellAction } from "./lazy-shell-action.ts" ;
18+ import {
19+ DEBUG_OVERLAY_ELEMENT ,
20+ KEYBOARD_SHORTCUTS_ELEMENT ,
21+ type LazyCustomElementRequestController ,
22+ } from "./lazy-custom-element.ts" ;
23+ import { readLazyShellAction , SHELL_APPROVALS_OPEN_EVENT } from "./lazy-shell-action.ts" ;
24+
25+ const recovery = vi . hoisted ( ( ) => ( { reload : vi . fn ( ) , pending : new Array < Promise < boolean > > ( ) } ) ) ;
26+ vi . mock ( "./stale-chunk-reload.ts" , async ( importOriginal ) => {
27+ const actual = await importOriginal < typeof import ( "./stale-chunk-reload.ts" ) > ( ) ;
28+ return {
29+ ...actual ,
30+ retryStaleChunkReloadWhenReachable : (
31+ deps : Parameters < typeof actual . retryStaleChunkReloadWhenReachable > [ 0 ] ,
32+ ) => {
33+ const pending = actual . retryStaleChunkReloadWhenReachable ( {
34+ ...deps ,
35+ reload : recovery . reload ,
36+ } ) ;
37+ recovery . pending . push ( pending ) ;
38+ return pending ;
39+ } ,
40+ } ;
41+ } ) ;
2042
2143const storageKey = "openclaw:lazy-event" ;
2244
@@ -34,7 +56,41 @@ async function withConnectedShell(shell: ShellLifecycle, run: () => void | Promi
3456 }
3557}
3658
37- afterEach ( resetAppHostTestGlobals ) ;
59+ afterEach ( ( ) => {
60+ resetAppHostTestGlobals ( ) ;
61+ vi . restoreAllMocks ( ) ;
62+ recovery . reload . mockClear ( ) ;
63+ recovery . pending . length = 0 ;
64+ } ) ;
65+
66+ type PaletteShell = HTMLElement &
67+ ShellLifecycle & {
68+ commandPaletteElement : TestOptionalCustomElement ;
69+ lazyCustomElements : LazyCustomElementRequestController ;
70+ openPalette ( ) : void ;
71+ restorePendingLazyAction ( ) : void ;
72+ resetForContextEpoch ( ) : void ;
73+ } ;
74+
75+ function paletteShell ( element : TestOptionalCustomElement , open : ( ) => void ) : PaletteShell {
76+ const shell = document . createElement ( "openclaw-app-shell" ) as PaletteShell ;
77+ shell . commandPaletteElement = element ;
78+ Object . defineProperty ( shell , "updateComplete" , { get : ( ) => Promise . resolve ( true ) } ) ;
79+ Object . defineProperty ( shell , "commandPalette" , {
80+ get : ( ) =>
81+ customElements . get ( element . tagName )
82+ ? { isOpen : false , openPalette : open , togglePalette : open }
83+ : undefined ,
84+ } ) ;
85+ stubRenderedWhenDefined ( shell ) ;
86+ return shell ;
87+ }
88+
89+ function stalePalette ( ) {
90+ return createLazyElementSpec ( "command palette" , {
91+ firstError : new Error ( "Failed to fetch dynamically imported module: palette-old.js" ) ,
92+ } ) ;
93+ }
3894
3995describe ( "lazy shell action storage" , ( ) => {
4096 it . each ( [
@@ -53,6 +109,116 @@ describe("lazy shell action storage", () => {
53109} ) ;
54110
55111describe ( "shell lazy events" , ( ) => {
112+ it . each ( [ "unavailable" , "denied" , "replacement-write-failed" ] as const ) (
113+ "retries in place without replaying an older action when storage is %s" ,
114+ async ( mode ) => {
115+ const storage = createStorageMock ( ) ;
116+ if ( mode === "replacement-write-failed" ) {
117+ storage . setItem ( storageKey , JSON . stringify ( { eventType : SHELL_APPROVALS_OPEN_EVENT } ) ) ;
118+ }
119+ vi . stubGlobal ( "sessionStorage" , mode === "unavailable" ? null : storage ) ;
120+ if ( mode === "denied" ) {
121+ Object . defineProperty ( globalThis , "sessionStorage" , {
122+ configurable : true ,
123+ get ( ) {
124+ throw new DOMException ( "Storage blocked" , "SecurityError" ) ;
125+ } ,
126+ } ) ;
127+ }
128+ const head = vi . fn ( async ( ) => new Response ( null , { status : 200 } ) ) ;
129+ vi . stubGlobal ( "fetch" , head ) ;
130+ const open = vi . fn ( ) ;
131+ const shell = paletteShell ( stalePalette ( ) , open ) ;
132+ if ( mode === "replacement-write-failed" ) {
133+ vi . spyOn ( storage , "setItem" ) . mockImplementation ( ( ) => {
134+ throw new DOMException ( "Storage full" , "QuotaExceededError" ) ;
135+ } ) ;
136+ }
137+
138+ await withConnectedShell ( shell , async ( ) => {
139+ shell . openPalette ( ) ;
140+ await vi . waitFor ( ( ) => expect ( shell . lazyCustomElements . visibleState ?. status ) . toBe ( "error" ) ) ;
141+ shell . lazyCustomElements . retry ( ) ;
142+ await Promise . all ( recovery . pending ) ;
143+
144+ await vi . waitFor ( ( ) => expect ( open ) . toHaveBeenCalledOnce ( ) ) ;
145+ expect ( recovery . reload ) . not . toHaveBeenCalled ( ) ;
146+ expect ( head ) . not . toHaveBeenCalled ( ) ;
147+ expect ( readLazyShellAction ( ) ) . toBeNull ( ) ;
148+ } ) ;
149+ } ,
150+ ) ;
151+
152+ it . each ( [ "close" , "context-replaced" , "disconnected" , "new-request" ] as const ) (
153+ "does not reload a retired retry after %s while the document probe is pending" ,
154+ async ( retirement ) => {
155+ vi . stubGlobal ( "sessionStorage" , createStorageMock ( ) ) ;
156+ let resolveHead = ( _response : Response ) : void => {
157+ throw new Error ( "Document probe not started" ) ;
158+ } ;
159+ const head = vi . fn (
160+ ( ) =>
161+ new Promise < Response > ( ( resolve ) => {
162+ resolveHead = resolve ;
163+ } ) ,
164+ ) ;
165+ vi . stubGlobal ( "fetch" , head ) ;
166+ const open = vi . fn ( ) ;
167+ const shell = paletteShell ( stalePalette ( ) , open ) ;
168+
169+ await withConnectedShell ( shell , async ( ) => {
170+ shell . openPalette ( ) ;
171+ await vi . waitFor ( ( ) => expect ( shell . lazyCustomElements . visibleState ?. status ) . toBe ( "error" ) ) ;
172+ shell . lazyCustomElements . retry ( ) ;
173+ await vi . waitFor ( ( ) => expect ( head ) . toHaveBeenCalledOnce ( ) ) ;
174+
175+ if ( retirement === "close" ) {
176+ shell . lazyCustomElements . close ( ) ;
177+ } else if ( retirement === "context-replaced" ) {
178+ shell . resetForContextEpoch ( ) ;
179+ } else if ( retirement === "disconnected" ) {
180+ shell . disconnectedCallback ( ) ;
181+ } else {
182+ shell . lazyCustomElements . request ( createLazyElementSpec ( "new request" ) ) ;
183+ }
184+ resolveHead ( new Response ( null , { status : 200 } ) ) ;
185+ await expect ( Promise . all ( recovery . pending ) ) . resolves . toEqual ( [ false ] ) ;
186+
187+ expect ( recovery . reload ) . not . toHaveBeenCalled ( ) ;
188+ expect ( open ) . not . toHaveBeenCalled ( ) ;
189+ } ) ;
190+ } ,
191+ ) ;
192+
193+ it ( "repairs stale stored intent before reloading and replays the current action once" , async ( ) => {
194+ const storage = createStorageMock ( ) ;
195+ vi . stubGlobal ( "sessionStorage" , storage ) ;
196+ vi . stubGlobal (
197+ "fetch" ,
198+ vi . fn ( async ( ) => new Response ( null , { status : 200 } ) ) ,
199+ ) ;
200+ const element = stalePalette ( ) ;
201+ const open = vi . fn ( ) ;
202+ const shell = paletteShell ( element , open ) ;
203+
204+ shell . openPalette ( ) ;
205+ await vi . waitFor ( ( ) => expect ( shell . lazyCustomElements . visibleState ?. status ) . toBe ( "error" ) ) ;
206+ storage . setItem ( storageKey , "{}" ) ;
207+ shell . lazyCustomElements . retry ( ) ;
208+ await expect ( Promise . all ( recovery . pending ) ) . resolves . toEqual ( [ true ] ) ;
209+ expect ( recovery . reload ) . toHaveBeenCalledOnce ( ) ;
210+ expect ( readLazyShellAction ( ) ) . toEqual ( { eventType : COMMAND_PALETTE_OPEN_EVENT } ) ;
211+
212+ const replacement = paletteShell ( element , open ) ;
213+ await withConnectedShell ( replacement , async ( ) => {
214+ replacement . restorePendingLazyAction ( ) ;
215+ await vi . waitFor ( ( ) => expect ( open ) . toHaveBeenCalledOnce ( ) ) ;
216+ replacement . restorePendingLazyAction ( ) ;
217+ expect ( open ) . toHaveBeenCalledOnce ( ) ;
218+ expect ( readLazyShellAction ( ) ) . toBeNull ( ) ;
219+ } ) ;
220+ } ) ;
221+
56222 it ( "requests the keyboard shortcuts dialog even from a focused text input" , async ( ) => {
57223 const requested = vi . fn ( ) ;
58224 const toggled = vi . fn ( ) ;
0 commit comments