@@ -19,9 +19,54 @@ import {
1919} from "vite-plus/test" ;
2020
2121import * as ServerBrowserPage from "./ServerBrowserPage.ts" ;
22+ import { SessionControl } from "./SessionControl.ts" ;
2223
2324const decodeEvaluateInput = Schema . decodeUnknownSync ( PreviewAutomationEvaluateInput ) ;
2425
26+ describe ( "server browser capture deadlines" , ( ) => {
27+ it ( "releases control after an unanswered capture and ignores its late reply" , async ( ) => {
28+ vi . useFakeTimers ( { toFake : [ "setTimeout" , "clearTimeout" ] } ) ;
29+ try {
30+ const captured = Promise . withResolvers < { data : string } > ( ) ;
31+ const send = vi . fn ( async ( method : string ) =>
32+ method === "Page.captureScreenshot"
33+ ? captured . promise
34+ : { result : { value : "still responsive" } } ,
35+ ) ;
36+ const cdp = { send } as unknown as CDPSession ;
37+ const control = new SessionControl ( "agent" ) ;
38+ let outcome : unknown ;
39+ const capture = control
40+ . agent ( "agent" , ( ) => ServerBrowserPage . captureViewport ( cdp , { format : "png" } ) )
41+ . catch ( ( cause : unknown ) => {
42+ outcome = cause ;
43+ } ) ;
44+ const evaluation = control . agent ( "agent" , ( ) =>
45+ ServerBrowserPage . evaluate ( cdp , { expression : "document.title" } , 1_000 ) ,
46+ ) ;
47+
48+ await vi . advanceTimersByTimeAsync ( 3_000 ) ;
49+ expect ( outcome ) . toMatchObject ( {
50+ tag : "PreviewAutomationExecutionError" ,
51+ message : expect . stringContaining ( "not producing screenshot frames" ) ,
52+ } ) ;
53+ await capture ;
54+ expect ( await evaluation ) . toBe ( "still responsive" ) ;
55+ expect ( vi . getTimerCount ( ) ) . toBe ( 0 ) ;
56+
57+ captured . resolve ( { data : "late-frame" } ) ;
58+ await captured . promise ;
59+ expect ( outcome ) . toBeInstanceOf ( ServerBrowserPage . ServerBrowserOperationError ) ;
60+ expect ( send . mock . calls . map ( ( [ method ] ) => method ) ) . toEqual ( [
61+ "Page.captureScreenshot" ,
62+ "Runtime.evaluate" ,
63+ ] ) ;
64+ } finally {
65+ vi . useRealTimers ( ) ;
66+ }
67+ } ) ;
68+ } ) ;
69+
2570describe ( "server browser element refs" , ( ) => {
2671 let browser : Browser ;
2772 let context : BrowserContext ;
0 commit comments