Skip to content

Commit b3f806c

Browse files
authored
test(gui-plugins): add interactive workspace, browser, and pairing stories (#63734)
1 parent a5170d6 commit b3f806c

9 files changed

Lines changed: 890 additions & 94 deletions

File tree

‎packages/gui-plugins/src/browser/panel.fixture.tsx‎

Lines changed: 125 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ import { createModel } from "./model"
5353
import SessionBrowserPane from "./panel"
5454
import { BrowserPane, type PaneEvent } from "./ipc"
5555
import { pageShortcut } from "./keys"
56-
import { refusal } from "./policy"
56+
import { normalizeURL, refusal } from "./policy"
5757
import { 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.
114131
const 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

648744
type StripTabs = {
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import { Button } from "@opencode/ui/button"
2+
import { createSignal, For, Show } from "solid-js"
3+
import { usePlugin } from "@opencode/plugin/gui"
4+
import { browserPaneFixture, type BrowserScenario } from "./panel.fixture"
5+
import barStyles from "./bar.css?inline"
6+
import commentStyles from "./comment.css?inline"
7+
8+
function BrowserStory(props: { scenario: BrowserScenario }) {
9+
const language = usePlugin()
10+
11+
return (
12+
<Show when={props.scenario} keyed>
13+
{(scenario) => {
14+
const Fixture = browserPaneFixture({
15+
scenario,
16+
useLanguage: () => language,
17+
Page: (page) => {
18+
const [saved, setSaved] = createSignal(false)
19+
let surface!: HTMLDivElement
20+
21+
return (
22+
<div ref={surface} class="absolute inset-0 overflow-auto px-6 py-10">
23+
<article class="mx-auto flex max-w-md flex-col items-start gap-4" style={{ zoom: page.zoom }}>
24+
<p class="text-12-regular text-v2-text-text-muted break-all" dir="ltr">
25+
{page.url}
26+
</p>
27+
<h2 class="font-medium text-v2-text-text-strong">Project preview</h2>
28+
<p class="text-13-regular text-v2-text-text-base">
29+
{page.picking
30+
? "Select Save changes to annotate this element."
31+
: "Save the sample page or add a note to the session."}
32+
</p>
33+
<Button
34+
variant="outline"
35+
onClick={(event: MouseEvent & { currentTarget: HTMLButtonElement }) => {
36+
if (!page.picking) return setSaved(true)
37+
const box = event.currentTarget.getBoundingClientRect()
38+
const origin = surface.getBoundingClientRect()
39+
page.pick({ x: box.x - origin.x, y: box.y - origin.y, width: box.width, height: box.height })
40+
}}
41+
>
42+
Save changes
43+
</Button>
44+
<Show when={saved()}>
45+
<p role="status">Changes saved to fixture.</p>
46+
</Show>
47+
</article>
48+
</div>
49+
)
50+
},
51+
Frame: (frame) => (
52+
<section class="mx-auto flex w-full max-w-3xl min-w-0 flex-col gap-3">
53+
{/* The in-app web host only installs browser styles on desktop. */}
54+
<style>{barStyles + commentStyles}</style>
55+
<div class="h-[400px] min-w-0 overflow-hidden rounded-lg border border-v2-border-border-muted">
56+
{frame.children}
57+
</div>
58+
<div class="flex flex-wrap items-center gap-3 text-12-regular text-v2-text-text-muted">
59+
<span>Fixture page · Addresses and actions stay in this preview.</span>
60+
<Show when={frame.loading}>
61+
<Button size="small" onClick={frame.finish}>
62+
Finish loading
63+
</Button>
64+
</Show>
65+
<Show when={frame.suspended}>
66+
<Button size="small" onClick={frame.reconnect}>
67+
Reconnect
68+
</Button>
69+
</Show>
70+
</div>
71+
<Show when={frame.external}>
72+
<output class="text-12-regular break-all">System browser request: {frame.external}</output>
73+
</Show>
74+
<For each={frame.comments}>
75+
{(note) => (
76+
<p class="text-12-regular">
77+
Attached: {note.label} {note.comment}
78+
</p>
79+
)}
80+
</For>
81+
</section>
82+
),
83+
})
84+
85+
return <Fixture />
86+
}}
87+
</Show>
88+
)
89+
}
90+
91+
export default {
92+
title: "App/Plugins/Browser",
93+
id: "plugins-browser",
94+
args: { scenario: "Page" },
95+
argTypes: {
96+
scenario: {
97+
control: "select",
98+
options: ["Page", "Empty", "Recent", "Loading", "Failed", "Suspended"] satisfies BrowserScenario[],
99+
},
100+
},
101+
render: (props: { scenario: BrowserScenario }) => <BrowserStory {...props} />,
102+
}
103+
104+
export const Page = { args: { scenario: "Page" } }
105+
106+
export const Empty = { args: { scenario: "Empty" } }
107+
108+
export const RecentPages = { args: { scenario: "Recent" } }
109+
110+
export const Loading = { args: { scenario: "Loading" } }
111+
112+
export const LoadFailed = { args: { scenario: "Failed" } }
113+
114+
export const Suspended = { args: { scenario: "Suspended" } }

0 commit comments

Comments
 (0)