Skip to content
14 changes: 14 additions & 0 deletions packages/app/component-tests/pairing.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
import { expect, story } from "../../stories/playwright/story"

story("the connect screen can retry a failed health check after scanning a one-time QR code", async ({ mount }) => {
const component = await mount("app-connect-to-server--retry-after-scan")
await component.getByRole("button", { name: "Read fixture QR code" }).click()
await expect(component.getByRole("alert")).toHaveText(
"Could not connect. Check the server address and password, then try again.",
)
await expect(component.getByLabel("Pairing link", { exact: true })).toHaveValue(
"https://build.example.test/auth/connect/example",
)

await component.getByRole("button", { name: "Connect", exact: true }).click()
await expect(component.getByRole("status")).toHaveText("Connected to https://build.example.test in this preview.")
})

story("a Keep screen active read that fails offers Try again and keeps the switch enabled", async ({ mount, page }) => {
// The page reads three more times, 1, 2 and 4 seconds apart, before it shows the failure.
await page.clock.install()
Expand Down
222 changes: 222 additions & 0 deletions packages/app/src/servers/connect/connect.fixture.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
import { Button } from "@opencode/ui/button"
import { useDialog } from "@opencode/ui/context/dialog"
import { createEffect, createSignal, onMount, Show, type ComponentProps } from "solid-js"
import type { ServerConnection } from "@/runtime/server/registry"
import { createServerConnectController, type ServerConnectControllerOptions } from "./controller"
import { DialogServer } from "./dialog"
import type { ServerCameraSupport, ServerConnectEvent, ServerConnectField, ServerConnectTarget } from "./machine"
import { PairingScanner } from "./scanner"
import { ConnectServerForm } from "./screen"

export const address = "https://build.example.test"

export const link = `${address}/auth/connect/example`

export const field = (field: ServerConnectField, value: string) => ({ type: "field.changed", field, value }) as const

export const password = { type: "method.changed", method: "password" } satisfies ServerConnectEvent

export const submit = { type: "submitted" } satisfies ServerConnectEvent

type CameraScene = "ready" | "opening" | "denied" | "invalid" | "decode" | "expired" | "redeeming" | "timeout"

type Preset = {
camera?: ServerCameraSupport | "probing"
events?: Extract<ServerConnectEvent, { type: "field.changed" | "method.changed" | "submitted" }>[]
scanner?: CameraScene
exchange?: "expired" | "unreachable"
check?: "unreachable" | "pending"
}

type DialogPreset = Preset & { target?: Extract<ServerConnectTarget, { kind: "edit" }>; closed?: boolean }

export function serverStory(preset: DialogPreset = {}) {
return { render: () => <DialogStory preset={preset} /> }
}

function DialogStory(props: { preset: DialogPreset }) {
const dialog = useDialog()

const [saved, setSaved] = createSignal("")

const open = () => {
setSaved("")
void dialog.show(() => (
<DialogServer
mode={props.preset.target?.kind ?? "add"}
onSave={(server) => setSaved(server.displayName ?? server.http.url)}
createForm={(dialog) =>
createPreviewForm(
{ ...dialog, target: props.preset.target ?? { kind: "add" }, removeTabs() {} },
props.preset,
)
}
scanner={(scanner) => <CameraPreview {...scanner} scene={props.preset.scanner ?? "ready"} />}
/>
))
}

onMount(() => {
if (!props.preset.closed) open()
})

return (
<div class="flex flex-col items-start gap-3">
<Button variant="neutral" onClick={open}>
Open {props.preset.target?.kind ?? "add"} server dialog
</Button>
<Show when={saved()}>{(name) => <p role="status">Saved {name()} in this preview.</p>}</Show>
</div>
)
}

export function screenStory(preset: Preset & { url?: string } = {}) {
return { render: () => <ScreenStory preset={preset} /> }
}

function ScreenStory(props: { preset: Preset & { url?: string } }) {
const [connected, setConnected] = createSignal("")

// On success the app routes away. In the lab show the saved address and let the next run start with a fresh form.
const Screen = () => {
const form = createPreviewForm(
{ surface: "screen", target: { kind: "connect", url: props.preset.url } },
props.preset,
(server) => setConnected(server.http.url),
)

return (
<ConnectServerForm
form={form}
scanner={(scanner) => <CameraPreview {...scanner} scene={props.preset.scanner ?? "ready"} />}
/>
)
}

return (
<div class="flex h-dvh max-h-full min-h-0 w-full flex-col bg-v2-background-bg-base">
<Show when={connected()} fallback={<Screen />}>
{(url) => (
<div class="m-auto flex flex-col items-center gap-3 p-4">
<p role="status">Connected to {url()} in this preview.</p>
<Button variant="neutral" onClick={() => setConnected("")}>
Pair again
</Button>
</div>
)}
</Show>
</div>
)
}

function createPreviewForm(
options: ServerConnectControllerOptions,
preset: Preset,
onSave?: (server: ServerConnection.Http) => void,
) {
const pending = new Promise<never>(() => {})

const spent = new Set(["expired"])

let check = preset.check

let exchange = preset.exchange

const form = createServerConnectController(options, {
camera: preset.camera === "probing" ? "probe" : (preset.camera ?? "probe"),
probeCamera: () => (preset.camera === "probing" ? pending : Promise.resolve(true)),
pageHref: "https://lab.example.test",
servers: {
list: [],
add(server) {
onSave?.(server)

return server
},
remove() {},
},
exchange: async (request) => {
if (request.code === "pending") return pending

if (spent.has(request.code)) return { type: "expired" }

if (exchange) {
const type = exchange
exchange = undefined

if (type === "expired") spent.add(request.code)

return { type }
}

spent.add(request.code)

return { type: "paired", pairing: { url: request.urls[0], password: "fixture" } }
},
check: async () => {
if (check === "pending") return pending

if (check === "unreachable") {
check = undefined

return { type: "unreachable" }
}

await new Promise<void>((resolve) => setTimeout(resolve, 700))

return { type: "healthy" }
},
})

preset.events?.forEach(form.dispatch)

if (preset.scanner) {
let opened = false
createEffect(() => {
if (opened || !form.view().canScan) return
opened = true
form.dispatch({ type: "scan.started" })
})
}

return form
}

function CameraPreview(props: ComponentProps<typeof PairingScanner> & { scene: CameraScene }) {
let detect: ((data: string) => void) | undefined

const camera: NonNullable<ComponentProps<typeof PairingScanner>["camera"]> = (_, read, failed) => {
detect = read
let frame: ReturnType<typeof requestAnimationFrame>

return {
start: async () => {
if (props.scene === "denied") throw new Error("Camera permission denied")

if (props.scene === "opening") return new Promise<void>(() => {})
frame = requestAnimationFrame(() => {
if (props.scene === "invalid") read("not a pairing code")

if (props.scene === "decode") failed()

if (props.scene === "expired") read(JSON.stringify({ code: "expired", urls: [address] }))

if (props.scene === "redeeming" || props.scene === "timeout") read(`${address}/auth/connect/pending`)
})
},
stop() {},
destroy: () => cancelAnimationFrame(frame),
}
}

return (
<>
<PairingScanner {...props} camera={camera} />
<Show when={props.scene !== "opening" && props.scene !== "denied" && props.scene !== "redeeming"}>
<Button variant="neutral" onClick={() => detect?.(JSON.stringify({ code: "example", urls: [address] }))}>
Read fixture QR code
</Button>
</Show>
</>
)
}
Loading
Loading