Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions packages/app/component-tests/browser-pane.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1223,3 +1223,23 @@ story("keeps the selected device and its rotated viewport with its browser tab",
await moreDevices.hover()
await expect(pixel).toBeChecked()
})

story("hands a sign-in to the system browser and reports the user's answer", async ({ page }) => {
const root = page.getByTestId("browser-pane-fixture")
await root.getByRole("button", { name: "Start handoff" }).click()

const banner = root.locator('[data-component="browser-handoff"]')
await expect(banner).toHaveAttribute("data-state", "pending")
await expect(banner).toContainText("Sign in to alpha.example in your system browser")
await expect(banner).toContainText("https://alpha.example/")
await expect(banner.getByRole("button", { name: "Cancel" })).toBeVisible()

await banner.getByRole("button", { name: "Open" }).click()
await expect(banner).toHaveAttribute("data-state", "opened")
await expect(banner).toContainText("Finish signing in to alpha.example")
await expect(banner).toContainText("OpenCode never sees that session")

await banner.getByRole("button", { name: "Done" }).click()
await expect(banner).toHaveAttribute("data-state", "completed")
await expect(banner).toContainText("Finished in the system browser")
})
19 changes: 18 additions & 1 deletion packages/gui-plugins/src/browser/connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function fixture(reject?: Error) {
const registered: Parameters<Client["register"]>[0][] = []
const closed: string[] = []
const highlights: Parameters<Client["highlight"]>[0][] = []
const handoffs: Parameters<Client["handoff"]>[0][] = []
const outcomes: string[] = []
const events: PaneEvent[] = []

Expand All @@ -49,6 +50,9 @@ function fixture(reject?: Error) {
},
load: async () => undefined,
command: async () => undefined,
handoff: async (input) => {
handoffs.push(input)
},
inspect: async () => undefined,
highlight: async (input) => {
highlights.push(input)
Expand Down Expand Up @@ -80,7 +84,7 @@ function fixture(reject?: Error) {
const binding = registered[0].binding
const emit = (event: PaneEvent) => listeners.get(binding)?.(event)

return { registration, binding, emit, listeners, closed, highlights, outcomes, events }
return { registration, binding, emit, listeners, closed, highlights, handoffs, outcomes, events }
}

test("highlights of picked elements reach the page", async () => {
Expand All @@ -94,6 +98,19 @@ test("highlights of picked elements reach the page", async () => {
])
})

test("the user's handoff answers reach main with the id and the choice", async () => {
const app = fixture()
app.registration.handoff("handoff-1", "open")
app.registration.handoff("handoff-1", "done")
app.registration.handoff("handoff-1", "cancel")
await Bun.sleep(0)
expect(app.handoffs).toEqual([
{ binding: app.binding, id: "handoff-1", action: "open" },
{ binding: app.binding, id: "handoff-1", action: "done" },
{ binding: app.binding, id: "handoff-1", action: "cancel" },
])
})

test("a closed binding stops its events and answers, and closes once with main", async () => {
const app = fixture()
app.emit({ type: "focus", tabID })
Expand Down
2 changes: 2 additions & 0 deletions packages/gui-plugins/src/browser/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type Registration = {
/** Creates a restored tab's page, which then reports its embed. */
load(tabID: Browser.TabID): void
command(command: Browser.Action): Promise<void>
handoff(id: string, action: "open" | "done" | "cancel"): Promise<void>
inspect(tabID: Browser.TabID, enabled: boolean): void
highlight(tabID: Browser.TabID, ref?: Browser.Ref): void
zoom(tabID: Browser.TabID, zoom: Zoom): void
Expand Down Expand Up @@ -62,6 +63,7 @@ export function openBinding(input: {
void ready.then(() => client.load({ binding, tabID })).catch(() => undefined)
},
command: (command) => ready.then(() => client.command({ binding, command })),
handoff: (id, action) => ready.then(() => client.handoff({ binding, id, action })),
inspect(tabID, enabled) {
if (status.closed) return
void ready.then(() => client.inspect({ binding, tabID, enabled })).catch(() => undefined)
Expand Down
188 changes: 188 additions & 0 deletions packages/gui-plugins/src/browser/handoff.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
import { expect, test } from "bun:test"
import { Browser } from "@opencode/plugin-browser/rpc"
import { createHandoffs } from "./handoff"
import type { HandoffEvent } from "./ipc"

const tab = {
first: Browser.TabID.make(`tab_${crypto.randomUUID()}`),
second: Browser.TabID.make(`tab_${crypto.randomUUID()}`),
}

const flush = () => new Promise((resolve) => setTimeout(resolve, 0))

function setup(options: { openExternal?: (url: string) => Promise<void>; timeoutMs?: number } = {}) {
const opened: string[] = []
const events: HandoffEvent[] = []
let ids = 0

const handoffs = createHandoffs({
openExternal:
options.openExternal ??
((url) => {
opened.push(url)

return Promise.resolve()
}),
timeoutMs: options.timeoutMs,
createID: () => `handoff-${++ids}`,
})

const begin = (input: Partial<Parameters<typeof handoffs.begin>[0]> = {}) =>
handoffs.begin({
binding: "binding",
tabID: tab.first,
url: "https://github.com/settings/tokens",
signal: new AbortController().signal,
emit: (event) => events.push(event),
...input,
})

return { handoffs, opened, events, begin }
}

test("nothing opens until the user's Open, then the answer settles once", async () => {
const { handoffs, opened, events, begin } = setup()

const result = begin()
await flush()

expect(opened).toEqual([])
expect(events).toEqual([
{
type: "handoff",
tabID: tab.first,
id: "handoff-1",
host: "github.com",
url: "https://github.com/settings/tokens",
state: "pending",
},
])

expect(handoffs.settle("binding", "handoff-2", "open")).toBe(false)
expect(handoffs.settle("other-binding", "handoff-1", "open")).toBe(false)
expect(handoffs.settle("binding", "handoff-1", "open")).toBe(true)
expect(handoffs.settle("binding", "handoff-1", "open")).toBe(true)
await flush()

expect(opened).toEqual(["https://github.com/settings/tokens"])
expect(events.at(-1)).toEqual({
type: "handoff",
tabID: tab.first,
id: "handoff-1",
host: "github.com",
url: "https://github.com/settings/tokens",
state: "opened",
})

expect(handoffs.settle("binding", "handoff-1", "done")).toBe(true)
expect(handoffs.settle("binding", "handoff-1", "done")).toBe(false)
expect(await result).toEqual({ state: "completed", host: "github.com" })
expect(events.at(-1)).toEqual({
type: "handoff",
tabID: tab.first,
id: "handoff-1",
host: "github.com",
state: "completed",
})
})

test("the user can abandon the handoff before or after opening", async () => {
const { handoffs, opened, events, begin } = setup()

const result = begin()
await flush()
handoffs.settle("binding", "handoff-1", "cancel")
expect(await result).toEqual({ state: "cancelled", host: "github.com" })
expect(opened).toEqual([])
expect(events.at(-1)?.state).toBe("cancelled")

const second = begin()
await flush()
handoffs.settle("binding", "handoff-2", "open")
await flush()
handoffs.settle("binding", "handoff-2", "cancel")
expect(await second).toEqual({ state: "cancelled", host: "github.com" })
expect(opened).toEqual(["https://github.com/settings/tokens"])
})

test("a handoff nobody answers times out and frees the pane", async () => {
const { opened, events, begin } = setup({ timeoutMs: 5 })

const result = begin()
expect(await result).toEqual({ state: "timedOut", host: "github.com" })
expect(opened).toEqual([])
expect(events.map((event) => event.state)).toEqual(["pending", "timedOut"])
})

test("cancelling the browser request ends a waiting handoff", async () => {
const abort = new AbortController()
const { opened, events, begin } = setup()

const result = begin({ signal: abort.signal })
await flush()
abort.abort()
expect(await result).toEqual({ state: "cancelled", host: "github.com" })
expect(opened).toEqual([])
expect(events.at(-1)?.state).toBe("cancelled")
})

test("one pane waits on one handoff at a time", async () => {
const { handoffs, opened, begin } = setup()

const first = begin()
await flush()
expect(() => begin()).toThrow("A handoff is already waiting")
expect(() => begin({ tabID: tab.second })).toThrow("A handoff is already waiting")
expect(opened).toEqual([])

handoffs.settle("binding", "handoff-1", "cancel")
await first
})

test("only web pages without embedded credentials can move to the system browser", async () => {
const { opened, begin } = setup()

for (const url of ["file:///tmp/secret.txt", "about:blank", "https://user:pass@github.com/", "not a url"]) {
await expect(begin({ url })).rejects.toThrow()
}

expect(opened).toEqual([])
})

test("a launch the user started but the system refused never echoes the URL", async () => {
const { handoffs, events, begin } = setup({
openExternal: () => Promise.reject(new Error("LSOpenURLsWithRole() failed for https://github.com/settings/tokens")),
})

const result = begin()
await flush()
handoffs.settle("binding", "handoff-1", "open")

let message = ""

try {
await result
} catch (error) {
message = error instanceof Error ? error.message : String(error)
}

expect(message).toContain("Could not open the system browser")
expect(message).not.toContain("github.com")
expect(events.at(-1)?.state).toBe("failed")
})

test("a closed tab or binding ends its waiting handoff", async () => {
const { handoffs, events, begin } = setup()

const result = begin()
await flush()
handoffs.cancelTab("binding", tab.second)
expect(events.at(-1)?.state).toBe("pending")
handoffs.cancelTab("binding", tab.first)
expect(await result).toEqual({ state: "cancelled", host: "github.com" })

const second = begin({ tabID: tab.second })
await flush()
handoffs.cancelBinding("binding")
expect(await second).toEqual({ state: "cancelled", host: "github.com" })
})
Loading
Loading