@@ -53,7 +53,7 @@ import { createModel } from "./model"
5353import SessionBrowserPane from "./panel"
5454import { BrowserPane , type PaneEvent } from "./ipc"
5555import { pageShortcut } from "./keys"
56- import { refusal } from "./policy"
56+ import { normalizeURL , refusal } from "./policy"
5757import { type BuiltinLayout } from "@opencode/plugin/gui/builtin"
5858
5959/** The renderer host pieces the pane runs on, passed in by `packages/app/component-tests/browser-pane.spec.ts`. */
@@ -110,6 +110,23 @@ type PaneFixtureState = {
110110 selection : string
111111}
112112
113+ export type BrowserScenario = "Page" | "Empty" | "Recent" | "Loading" | "Failed" | "Suspended"
114+
115+ type ElementRect = Pick < DOMRect , "x" | "y" | "width" | "height" >
116+
117+ type StoryHost = Pick < PaneHost , "useLanguage" > & {
118+ scenario : BrowserScenario
119+ Page : Component < { url : string ; zoom : number ; picking : boolean ; pick : ( rect : ElementRect ) => void } >
120+ Frame : ParentComponent < {
121+ loading : boolean
122+ finish : ( ) => void
123+ suspended : boolean
124+ reconnect : ( ) => void
125+ external : string | undefined
126+ comments : readonly ComposerNote [ ]
127+ } >
128+ }
129+
113130// A 16px blue square, as main reports a page icon.
114131const icon =
115132 "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAxNiAxNiI+PHJlY3Qgd2lkdGg9IjE2IiBoZWlnaHQ9IjE2IiByeD0iMyIgZmlsbD0iIzNiODJmNiIvPjwvc3ZnPg=="
@@ -137,21 +154,40 @@ export function mountBrowserPane(input: PaneHost) {
137154 host . style . cssText = "position:fixed;inset:0;z-index:1000;background:#181818;color:#eee;padding:24px"
138155 document . body . appendChild ( host )
139156
140- function Fixture ( ) {
157+ const Fixture = browserPaneFixture ( input )
158+
159+ return render (
160+ ( ) => (
161+ < input . LanguageProvider locale = "en" >
162+ < input . UiI18nBridge >
163+ < Fixture />
164+ </ input . UiI18nBridge >
165+ </ input . LanguageProvider >
166+ ) ,
167+ host ,
168+ )
169+ }
170+
171+ // Both hosts exercise the real model and pane. The lab draws an in-memory document where the component tests
172+ // measure native embed layouts and captures.
173+ export function browserPaneFixture ( input : PaneHost | StoryHost ) {
174+ return function Fixture ( ) {
141175 const language = input . useLanguage ( )
142176 const messages = new Map ( Object . entries ( browserEn ) )
143177
178+ const scenario = "scenario" in input ? input . scenario : undefined
179+
144180 const [ store , setStore ] = createStore < PaneFixtureState > ( {
145181 session : "Alpha" ,
146182 mounted : true ,
147183 visible : true ,
148- url : undefined ,
149- loading : false ,
184+ url : scenario === "Empty" || scenario === "Recent" ? "about:blank" : undefined ,
185+ loading : scenario === "Loading" ,
150186 generation : 0 ,
151187 delayNavigation : false ,
152188 pendingURL : undefined ,
153- loadErrors : { } ,
154- error : undefined ,
189+ loadErrors : scenario === "Failed" ? { Alpha : "ERR_CONNECTION_REFUSED" } : { } ,
190+ error : scenario === "Suspended" ? "browser.pane.suspended" : undefined ,
155191 layouts : { } ,
156192 covered : false ,
157193 moving : false ,
@@ -164,7 +200,7 @@ export function mountBrowserPane(input: PaneHost) {
164200 highlights : [ ] ,
165201 comments : [ ] ,
166202 zoom : 1 ,
167- history : { visits : [ ] } ,
203+ history : { visits : scenario === "Recent" ? structuredClone ( visits ) : [ ] } ,
168204 external : [ ] ,
169205 cookies : 3 ,
170206 failSite : false ,
@@ -223,12 +259,30 @@ export function mountBrowserPane(input: PaneHost) {
223259 }
224260
225261 // SAFETY: the host embeds call only `embed`, `capture` and `on` on their bridge (`runtime/plugin/embeds.tsx`).
226- const embeds = input . createEmbeds ( {
227- bridge : bridge as Bridge ,
228- zoom : ( ) => ( store . selection === "zoomed" ? 1.25 : 1 ) ,
229- dialog : ( ) => store . dialog ,
230- moving : ( ) => store . moving || input . panelMotion . panelsMoving ( ) ,
231- } )
262+ const embeds : Embeds =
263+ "createEmbeds" in input
264+ ? input . createEmbeds ( {
265+ bridge : bridge as Bridge ,
266+ zoom : ( ) => ( store . selection === "zoomed" ? 1.25 : 1 ) ,
267+ dialog : ( ) => store . dialog ,
268+ moving : ( ) => store . moving || input . panelMotion . panelsMoving ( ) ,
269+ } )
270+ : {
271+ capture : async ( ) => undefined ,
272+ View : ( props ) => (
273+ < div class = { props . class } >
274+ < Show when = { props . id && props . visible && ! store . loading && ! model . suspended ( { key : store . session } ) } >
275+ < input . Page
276+ url = { store . url ?? current ( ) . url }
277+ zoom = { store . zoom }
278+ picking = { ! ! store . picker [ store . session ] }
279+ pick = { pick }
280+ />
281+ </ Show >
282+ { props . children }
283+ </ div >
284+ ) ,
285+ }
232286
233287 const t = ( key : string , params ?: Readonly < Partial < Record < string , string | number > > > ) =>
234288 ( messages . get ( key ) ?? language . t ( key ) ) . replace ( / \{ \{ ( \w + ) \} \} / g, ( _ , name : string ) =>
@@ -297,6 +351,9 @@ export function mountBrowserPane(input: PaneHost) {
297351 composer : { attach : ( note : ComposerNote ) => setStore ( "comments" , ( items ) => [ ...items , note ] ) } ,
298352 } as unknown as SessionScreen
299353
354+ const back : string [ ] = [ ]
355+ const forward : string [ ] = [ ]
356+
300357 const report = ( key = store . session ) => {
301358 const tab = tabs . find ( ( item ) => item . title === key )
302359
@@ -308,6 +365,8 @@ export function mountBrowserPane(input: PaneHost) {
308365 {
309366 ...tab ,
310367 url : store . url ?? tab . url ,
368+ canGoBack : back . length > 0 ,
369+ canGoForward : forward . length > 0 ,
311370 loading : store . loading ,
312371 generation : store . generation ,
313372 loadError : store . loadErrors [ key ] ,
@@ -325,6 +384,14 @@ export function mountBrowserPane(input: PaneHost) {
325384 bindings . set ( value . session , value . binding )
326385 queueMicrotask ( ( ) => {
327386 report ( value . session )
387+
388+ // A suspended attachment reports its retained inventory once. The next interaction can reconnect it.
389+ if ( store . error === "browser.pane.suspended" ) {
390+ setStore ( "error" , undefined )
391+
392+ return
393+ }
394+
328395 const tab = tabs . find ( ( item ) => item . title === value . session )
329396
330397 if ( tab ) emit ( value . session , { type : "embed" , tabID : tab . id , embed : `embed-${ tab . title } ` } )
@@ -342,15 +409,32 @@ export function mountBrowserPane(input: PaneHost) {
342409 if ( command . type === "navigate" || command . type === "reload" ) setStore ( "loadErrors" , store . session , undefined )
343410
344411 if ( command . type === "navigate" ) {
412+ const url = normalizeURL ( command . url )
413+
345414 if ( store . delayNavigation ) {
346- setStore ( "pendingURL" , command . url )
415+ setStore ( "pendingURL" , url )
347416
348417 return
349418 }
350419
351- setStore ( { url : command . url , loading : false , generation : store . generation + 1 } )
420+ back . push ( store . url ?? current ( ) . url )
421+ forward . length = 0
422+ setStore ( { url, loading : false , generation : store . generation + 1 } )
423+ }
424+
425+ if ( command . type === "back" || command . type === "forward" ) {
426+ const stack = command . type === "back" ? back : forward
427+ const url = stack . pop ( )
428+
429+ if ( url ) {
430+ const other = command . type === "back" ? forward : back
431+ other . push ( store . url ?? current ( ) . url )
432+ setStore ( { url, loading : false , generation : store . generation + 1 } )
433+ }
352434 }
353435
436+ if ( command . type === "reload" ) setStore ( { loading : false , generation : store . generation + 1 } )
437+
354438 if ( command . type === "stop" ) setStore ( "loading" , false )
355439 report ( )
356440 } ,
@@ -437,7 +521,7 @@ export function mountBrowserPane(input: PaneHost) {
437521 const plugin = fake as unknown as Context & SetupContext < typeof definition >
438522 const model = createModel ( plugin )
439523
440- const pick = ( ) => {
524+ const pick = ( rect ?: ElementRect ) => {
441525 const selector = ( ) => {
442526 if ( store . selection === "fallback" ) return ""
443527
@@ -458,11 +542,34 @@ export function mountBrowserPane(input: PaneHost) {
458542 label : "button.primary" ,
459543 role : "button" ,
460544 name : "Save changes" ,
461- rect : { x : 48 , y : store . selection === "low" ? 180 : 40 , width : 160 , height : 36 } ,
545+ rect : rect ?? { x : 48 , y : store . selection === "low" ? 180 : 40 , width : 160 , height : 36 } ,
462546 } ,
463547 } )
464548 }
465549
550+ if ( "Frame" in input )
551+ return (
552+ < PluginContext . Provider value = { plugin } >
553+ < PanelContext . Provider value = { panel } >
554+ < input . Frame
555+ loading = { store . loading }
556+ finish = { ( ) => {
557+ setStore ( "loading" , false )
558+ report ( )
559+ } }
560+ suspended = { model . suspended ( { key : store . session } ) }
561+ reconnect = { ( ) => model . command ( { key : store . session } , { type : "reload" , tabID : current ( ) . id } ) }
562+ comments = { store . comments }
563+ external = { store . external . at ( - 1 ) }
564+ >
565+ < Show when = { session ( ) } >
566+ { ( view ) => < SessionBrowserPane tab = { current } session = { view ( ) } screen = { screen } model = { model } /> }
567+ </ Show >
568+ </ input . Frame >
569+ </ PanelContext . Provider >
570+ </ PluginContext . Provider >
571+ )
572+
466573 return (
467574 < PluginContext . Provider value = { plugin } >
468575 < PanelContext . Provider value = { panel } >
@@ -546,7 +653,7 @@ export function mountBrowserPane(input: PaneHost) {
546653 ) }
547654 </ For >
548655 < button onClick = { ( ) => setStore ( "dialog" , ( open ) => ! open ) } > Toggle dialog</ button >
549- < button onClick = { pick } > Pick element</ button >
656+ < button onClick = { ( ) => pick ( ) } > Pick element</ button >
550657 < button onClick = { ( ) => setStore ( "dropInspect" , true ) } > Drop picker requests</ button >
551658 < select
552659 aria-label = "Picked element scenario"
@@ -632,17 +739,6 @@ export function mountBrowserPane(input: PaneHost) {
632739 </ PluginContext . Provider >
633740 )
634741 }
635-
636- return render (
637- ( ) => (
638- < input . LanguageProvider locale = "en" >
639- < input . UiI18nBridge >
640- < Fixture />
641- </ input . UiI18nBridge >
642- </ input . LanguageProvider >
643- ) ,
644- host ,
645- )
646742}
647743
648744type StripTabs = {
0 commit comments