@@ -185,6 +185,96 @@ describe("createCanvasSurfaceLease", () => {
185185 expect ( clock . nextDelayMs ) . toBe ( 60_000 ) ;
186186 } ) ;
187187
188+ it . each ( [ "stops" , "reconnects without a canvas" ] as const ) (
189+ "does not schedule a retired generation when publishing a refreshed URL %s" ,
190+ async ( transition ) => {
191+ const clock = new FakeClock ( ) ;
192+ const changes : Array < string | null > = [ ] ;
193+ const request = vi . fn ( async ( ) => ( {
194+ surface : "canvas" ,
195+ pluginSurfaceUrls : { canvas : "https://canvas.test/__openclaw__/cap/refreshed" } ,
196+ expiresAtMs : 120_000 ,
197+ } ) ) ;
198+ const lease = createCanvasSurfaceLease ( {
199+ request,
200+ onChange : ( url ) => {
201+ changes . push ( url ) ;
202+ if ( url === "https://canvas.test/__openclaw__/cap/refreshed" ) {
203+ if ( transition === "stops" ) {
204+ lease . stop ( ) ;
205+ } else {
206+ lease . start ( undefined ) ;
207+ }
208+ }
209+ } ,
210+ now : clock . now ,
211+ setTimer : clock . setTimer ,
212+ clearTimer : clock . clearTimer ,
213+ } ) ;
214+
215+ lease . start ( "https://canvas.test/__openclaw__/cap/original" ) ;
216+ await flushPromises ( ) ;
217+
218+ expect ( request ) . toHaveBeenCalledOnce ( ) ;
219+ expect ( changes ) . toEqual ( [
220+ "https://canvas.test/__openclaw__/cap/original" ,
221+ "https://canvas.test/__openclaw__/cap/refreshed" ,
222+ null ,
223+ ] ) ;
224+ expect ( clock . pendingCount ) . toBe ( 0 ) ;
225+ } ,
226+ ) ;
227+
228+ it ( "keeps the reconnect renewal owned when a retired timer callback arrives late" , async ( ) => {
229+ const request = vi
230+ . fn < ( method : string , params : unknown ) => Promise < unknown > > ( )
231+ . mockResolvedValueOnce ( {
232+ surface : "canvas" ,
233+ pluginSurfaceUrls : { canvas : "https://canvas.test/__openclaw__/cap/first" } ,
234+ expiresAtMs : 120_000 ,
235+ } )
236+ . mockResolvedValueOnce ( {
237+ surface : "canvas" ,
238+ pluginSurfaceUrls : { canvas : "https://canvas.test/__openclaw__/cap/reconnected" } ,
239+ expiresAtMs : 180_000 ,
240+ } ) ;
241+ const { clock, lease } = createLeaseHarness ( request ) ;
242+
243+ lease . start ( "https://canvas.test/__openclaw__/cap/original" ) ;
244+ await flushPromises ( ) ;
245+ const retiredCallback = clock . takeNextCallback ( ) ;
246+ expect ( retiredCallback ) . toBeDefined ( ) ;
247+
248+ lease . stop ( ) ;
249+ lease . start ( "https://canvas.test/__openclaw__/cap/reconnect" ) ;
250+ await flushPromises ( ) ;
251+ expect ( clock . pendingCount ) . toBe ( 1 ) ;
252+
253+ retiredCallback ?.( ) ;
254+ expect ( request ) . toHaveBeenCalledTimes ( 2 ) ;
255+ lease . stop ( ) ;
256+
257+ expect ( clock . pendingCount ) . toBe ( 0 ) ;
258+ } ) ;
259+
260+ it . each ( [ Number . MAX_SAFE_INTEGER , 2 ** 32 + 115_000 ] ) (
261+ "clamps an advertised canvas expiry of %d to a browser-safe native timer" ,
262+ async ( expiresAtMs ) => {
263+ const request = vi . fn ( async ( ) => ( {
264+ surface : "canvas" ,
265+ pluginSurfaceUrls : { canvas : "https://canvas.test/__openclaw__/cap/refreshed" } ,
266+ expiresAtMs,
267+ } ) ) ;
268+ const { clock, lease } = createLeaseHarness ( request ) ;
269+
270+ lease . start ( "https://canvas.test/__openclaw__/cap/original" ) ;
271+ await flushPromises ( ) ;
272+
273+ expect ( clock . nextDelayMs ) . toBe ( 2_147_483_647 ) ;
274+ lease . stop ( ) ;
275+ } ,
276+ ) ;
277+
188278 it ( "stop clears timers, ignores an in-flight result, and publishes null once" , async ( ) => {
189279 const pending = deferred < unknown > ( ) ;
190280 const { changes, clock, connectionChanges, lease } = createLeaseHarness ( ( ) => pending . promise ) ;
0 commit comments