mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-20 18:57:17 +03:00
fix(app): model the Extensions flyout, MCP toggles and config actions as state machines (#61886)
This commit is contained in:
committed by
GitHub
Unverified
parent
16c4b36c39
commit
c579f9ab7c
@@ -147,12 +147,29 @@ for (const surface of ["popover", "dialog"] as const) {
|
||||
|
||||
test("the MCP popover shows a failed server and reconnects it from its details", async ({ page }) => {
|
||||
const error = "Streamable HTTP error: Error POSTing to endpoint: 404 Not Found"
|
||||
const again = "Streamable HTTP error: Error POSTing to endpoint: 503 Service Unavailable"
|
||||
const retries = { connecting: false, failing: true }
|
||||
|
||||
const view = await open(page, {
|
||||
surface: "popover",
|
||||
mcp: (target) => [
|
||||
{ name: "figma-desktop", status: target !== workspace ? { status: "connected" } : { status: "failed", error } },
|
||||
{
|
||||
name: "figma-desktop",
|
||||
status:
|
||||
target !== workspace
|
||||
? { status: "connected" }
|
||||
: retries.connecting
|
||||
? { status: "pending" }
|
||||
: { status: "failed", error },
|
||||
},
|
||||
],
|
||||
// The first retry fails again with another error, reported by the refreshed status; the second connects.
|
||||
onMcpAction: (input) => {
|
||||
if (input.action !== "connect" || !retries.failing) return
|
||||
retries.failing = false
|
||||
|
||||
return { status: "failed", error: again }
|
||||
},
|
||||
})
|
||||
|
||||
const row = view.panel.locator(".session-mcp-row").filter({ hasText: "figma-desktop" })
|
||||
@@ -164,14 +181,62 @@ test("the MCP popover shows a failed server and reconnects it from its details",
|
||||
const details = page.getByRole("dialog", { name: "figma-desktop", exact: true })
|
||||
await expect(details).toHaveAccessibleDescription("Connection failed")
|
||||
await expect(details.getByText(error, { exact: true })).toBeVisible()
|
||||
await details.getByRole("button", { name: "Try again", exact: true }).click()
|
||||
const retry = details.getByRole("button", { name: "Try again", exact: true })
|
||||
const connect = await holdRoute(page, /\/api\/experimental\/mcp\/figma-desktop\/connect(?:\?|$)/, { method: "POST" })
|
||||
await retry.click()
|
||||
await connect.arrived
|
||||
// The server reports that it is connecting; the details stay open while the retry runs, and take no second retry.
|
||||
retries.connecting = true
|
||||
|
||||
const listed = page.waitForResponse((response) => {
|
||||
const url = new URL(response.url())
|
||||
|
||||
return (
|
||||
response.request().method() === "GET" &&
|
||||
url.pathname === "/api/mcp" &&
|
||||
url.searchParams.get("location[directory]") === workspace
|
||||
)
|
||||
})
|
||||
|
||||
await view.push([
|
||||
{
|
||||
id: "evt_mcp_connecting",
|
||||
created: 1,
|
||||
type: "mcp.status.changed",
|
||||
location: { directory: workspace },
|
||||
data: { server: "figma-desktop" },
|
||||
},
|
||||
])
|
||||
await listed
|
||||
await expect(details.getByText(error, { exact: true })).toBeVisible()
|
||||
await expect(retry).toBeDisabled()
|
||||
await expect(details.getByRole("button", { name: "Disable", exact: true })).toBeDisabled()
|
||||
connect.release()
|
||||
// A retry that fails again shows its new error in place.
|
||||
await expect(details.getByText(again, { exact: true })).toBeVisible()
|
||||
await expect(retry).toBeEnabled()
|
||||
|
||||
const reconnect = await holdRoute(page, /\/api\/experimental\/mcp\/figma-desktop\/connect(?:\?|$)/, {
|
||||
method: "POST",
|
||||
})
|
||||
|
||||
await retry.click()
|
||||
await reconnect.arrived
|
||||
await page.keyboard.press("Escape")
|
||||
await expect(details).toBeHidden()
|
||||
|
||||
// The details closed and opened again still show the retry the dialog started, and take no second connect.
|
||||
if (await view.panel.isVisible()) await page.getByRole("button", { name: "Session details", exact: true }).click()
|
||||
await expect(view.panel).toBeHidden()
|
||||
await view.show()
|
||||
await expect(view.toggle).toBeDisabled()
|
||||
reconnect.release()
|
||||
await expect(view.toggle).toHaveAccessibleDescription("Connected")
|
||||
await expect(view.toggle).toBeEnabled()
|
||||
expect(view.requests.filter((request) => request.path.endsWith("/connect"))).toEqual([
|
||||
{ path: "/api/experimental/mcp/figma-desktop/connect", directory: workspace },
|
||||
{ path: "/api/experimental/mcp/figma-desktop/connect", directory: workspace },
|
||||
])
|
||||
|
||||
if (!(await view.panel.isVisible())) await view.show()
|
||||
await expect(view.toggle).toHaveAccessibleDescription("Connected")
|
||||
})
|
||||
|
||||
for (const surface of ["dialog"] as const) {
|
||||
|
||||
@@ -563,6 +563,8 @@ test("MCP rows toggle once per action and keep sign-in, errors, and management a
|
||||
if (pending) {
|
||||
await pending.arrived
|
||||
await expect(connected).toBeDisabled()
|
||||
// Each row toggles its own server: the others stay free meanwhile.
|
||||
await expect(disabled).toBeEnabled()
|
||||
pending.release()
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import {
|
||||
CONFIG_ACTION_TIMEOUT_MS,
|
||||
CONFIG_COPIED_MS,
|
||||
configActionView,
|
||||
initialConfigActionState,
|
||||
transitionConfigAction,
|
||||
type ConfigActionEvent,
|
||||
type ConfigActionState,
|
||||
type ConfigActionTransition,
|
||||
} from "./config-action-machine"
|
||||
|
||||
// Folds the events in order and returns the last transition.
|
||||
const replay = (events: readonly ConfigActionEvent[], state: ConfigActionState = initialConfigActionState) =>
|
||||
events.reduce<ConfigActionTransition>((last, event) => transitionConfigAction(last.state, event), {
|
||||
state,
|
||||
effects: [],
|
||||
})
|
||||
|
||||
describe("transitionConfigAction", () => {
|
||||
test("a copy moves from opening to copied in one step and expires", () => {
|
||||
const opening = transitionConfigAction(initialConfigActionState, { type: "activate" })
|
||||
|
||||
expect(opening.effects).toEqual([{ type: "run", attempt: 1, timeoutMs: CONFIG_ACTION_TIMEOUT_MS }])
|
||||
expect(configActionView(opening.state)).toEqual({ disabled: true, copied: false })
|
||||
|
||||
const copied = transitionConfigAction(opening.state, { type: "copied", attempt: 1 })
|
||||
|
||||
expect(copied.effects).toEqual([
|
||||
{ type: "abort", attempt: 1 },
|
||||
{ type: "schedule-reset", attempt: 1, delayMs: CONFIG_COPIED_MS },
|
||||
])
|
||||
expect(configActionView(copied.state)).toEqual({ disabled: false, copied: true })
|
||||
|
||||
const expired = transitionConfigAction(copied.state, { type: "copied.expired", attempt: 1 })
|
||||
|
||||
expect(expired.state).toEqual({ status: "idle", issued: 1 })
|
||||
})
|
||||
|
||||
test("copying again restarts and ignores the first copy's reset", () => {
|
||||
const again = replay([{ type: "activate" }, { type: "copied", attempt: 1 }, { type: "activate" }])
|
||||
|
||||
expect(again.state).toEqual({ status: "opening", attempt: 2 })
|
||||
expect(again.effects).toEqual([
|
||||
{ type: "cancel-reset" },
|
||||
{ type: "run", attempt: 2, timeoutMs: CONFIG_ACTION_TIMEOUT_MS },
|
||||
])
|
||||
expect(transitionConfigAction(again.state, { type: "copied.expired", attempt: 1 }).state).toBe(again.state)
|
||||
})
|
||||
|
||||
test("a second click while the action runs starts nothing", () => {
|
||||
const opening = transitionConfigAction(initialConfigActionState, { type: "activate" }).state
|
||||
|
||||
expect(transitionConfigAction(opening, { type: "activate" })).toEqual({ state: opening, effects: [] })
|
||||
})
|
||||
|
||||
test("hiding the popover drops the copied mark", () => {
|
||||
const copied = replay([{ type: "activate" }, { type: "copied", attempt: 1 }]).state
|
||||
const hidden = transitionConfigAction(copied, { type: "hidden" })
|
||||
|
||||
expect(hidden).toEqual({ state: { status: "idle", issued: 1 }, effects: [{ type: "cancel-reset" }] })
|
||||
|
||||
const opening = transitionConfigAction(initialConfigActionState, { type: "activate" }).state
|
||||
|
||||
expect(transitionConfigAction(opening, { type: "hidden" }).state).toBe(opening)
|
||||
})
|
||||
|
||||
test("an opened file closes the popover only when a side tab replaced it", () => {
|
||||
const tab = replay([{ type: "activate" }, { type: "opened", attempt: 1, close: true }])
|
||||
|
||||
expect(tab).toEqual({
|
||||
state: { status: "idle", issued: 1 },
|
||||
effects: [{ type: "abort", attempt: 1 }, { type: "close-owner" }],
|
||||
})
|
||||
|
||||
const revealed = replay([{ type: "activate" }, { type: "opened", attempt: 1, close: false }])
|
||||
|
||||
expect(revealed.effects).toEqual([{ type: "abort", attempt: 1 }])
|
||||
})
|
||||
|
||||
test("a failure or a timeout ends the action with a toast", () => {
|
||||
const failed = replay([{ type: "activate" }, { type: "failed", attempt: 1, error: "No configuration file found" }])
|
||||
|
||||
expect(failed).toEqual({
|
||||
state: { status: "idle", issued: 1 },
|
||||
effects: [
|
||||
{ type: "abort", attempt: 1 },
|
||||
{ type: "toast-failed", error: "No configuration file found" },
|
||||
],
|
||||
})
|
||||
|
||||
const timedOut = replay([{ type: "activate" }, { type: "timed-out", attempt: 1 }])
|
||||
|
||||
expect(timedOut.effects).toEqual([{ type: "abort", attempt: 1 }, { type: "toast-timed-out" }])
|
||||
|
||||
// The request that hung settles later and changes nothing.
|
||||
expect(transitionConfigAction(timedOut.state, { type: "copied", attempt: 1 }).state).toBe(timedOut.state)
|
||||
})
|
||||
|
||||
test("disposal aborts the running action and ignores its result", () => {
|
||||
const disposed = replay([{ type: "activate" }, { type: "disposed" }])
|
||||
|
||||
expect(disposed).toEqual({ state: { status: "disposed" }, effects: [{ type: "abort", attempt: 1 }] })
|
||||
|
||||
for (const event of [
|
||||
{ type: "opened", attempt: 1, close: true },
|
||||
{ type: "copied", attempt: 1 },
|
||||
{ type: "failed", attempt: 1, error: "late" },
|
||||
{ type: "activate" },
|
||||
] satisfies ConfigActionEvent[])
|
||||
expect(transitionConfigAction(disposed.state, event).state).toBe(disposed.state)
|
||||
|
||||
const copied = replay([{ type: "activate" }, { type: "copied", attempt: 1 }, { type: "disposed" }])
|
||||
|
||||
expect(copied.effects).toEqual([{ type: "cancel-reset" }])
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,107 @@
|
||||
/** How long reading the configuration and opening, revealing or copying its file may take before it gives up. */
|
||||
export const CONFIG_ACTION_TIMEOUT_MS = 15_000
|
||||
|
||||
/** How long a copied path keeps its mark. */
|
||||
export const CONFIG_COPIED_MS = 2_000
|
||||
|
||||
/**
|
||||
* A footer action that reads the server's configuration: the configuration file link or a setup button. Each click
|
||||
* takes the next attempt number, so a result whose attempt the state no longer holds is stale; `issued` is the last
|
||||
* one handed out.
|
||||
*/
|
||||
export type ConfigActionState =
|
||||
| { readonly status: "idle"; readonly issued: number }
|
||||
| { readonly status: "opening"; readonly attempt: number }
|
||||
/** A remote server's configuration path is on the clipboard. */
|
||||
| { readonly status: "copied"; readonly attempt: number }
|
||||
| { readonly status: "disposed" }
|
||||
|
||||
export type ConfigActionEvent =
|
||||
| { readonly type: "activate" }
|
||||
/** The file or draft opened; `close` closes the popover, which a side tab or a draft replaces. */
|
||||
| { readonly type: "opened"; readonly attempt: number; readonly close: boolean }
|
||||
| { readonly type: "copied"; readonly attempt: number }
|
||||
| { readonly type: "failed"; readonly attempt: number; readonly error: string }
|
||||
| { readonly type: "timed-out"; readonly attempt: number }
|
||||
| { readonly type: "copied.expired"; readonly attempt: number }
|
||||
/** The popover or drawer that shows the action hid. */
|
||||
| { readonly type: "hidden" }
|
||||
| { readonly type: "disposed" }
|
||||
|
||||
export type ConfigActionEffect =
|
||||
/** Starts the attempt's controller and timeout, then runs the action. */
|
||||
| { readonly type: "run"; readonly attempt: number; readonly timeoutMs: number }
|
||||
/** Ends the attempt's controller and timeout. */
|
||||
| { readonly type: "abort"; readonly attempt: number }
|
||||
| { readonly type: "schedule-reset"; readonly attempt: number; readonly delayMs: number }
|
||||
| { readonly type: "cancel-reset" }
|
||||
| { readonly type: "close-owner" }
|
||||
| { readonly type: "toast-failed"; readonly error: string }
|
||||
| { readonly type: "toast-timed-out" }
|
||||
|
||||
export type ConfigActionTransition = {
|
||||
readonly state: ConfigActionState
|
||||
readonly effects: readonly ConfigActionEffect[]
|
||||
}
|
||||
|
||||
export const initialConfigActionState: ConfigActionState = { status: "idle", issued: 0 }
|
||||
|
||||
const NONE: readonly ConfigActionEffect[] = []
|
||||
|
||||
const CANCEL_RESET: readonly ConfigActionEffect[] = [{ type: "cancel-reset" }]
|
||||
|
||||
export function transitionConfigAction(state: ConfigActionState, event: ConfigActionEvent): ConfigActionTransition {
|
||||
if (state.status === "disposed") return { state, effects: NONE }
|
||||
|
||||
if (event.type === "disposed") {
|
||||
if (state.status === "opening")
|
||||
return { state: { status: "disposed" }, effects: [{ type: "abort", attempt: state.attempt }] }
|
||||
|
||||
return { state: { status: "disposed" }, effects: state.status === "copied" ? CANCEL_RESET : NONE }
|
||||
}
|
||||
|
||||
if (event.type === "hidden") {
|
||||
if (state.status !== "copied") return { state, effects: NONE }
|
||||
|
||||
return { state: { status: "idle", issued: state.attempt }, effects: CANCEL_RESET }
|
||||
}
|
||||
|
||||
if (event.type === "activate") {
|
||||
if (state.status === "opening") return { state, effects: NONE }
|
||||
const attempt = (state.status === "idle" ? state.issued : state.attempt) + 1
|
||||
|
||||
return {
|
||||
state: { status: "opening", attempt },
|
||||
effects: [
|
||||
...(state.status === "copied" ? CANCEL_RESET : NONE),
|
||||
{ type: "run", attempt, timeoutMs: CONFIG_ACTION_TIMEOUT_MS },
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
if (event.type === "copied.expired") {
|
||||
if (state.status !== "copied" || state.attempt !== event.attempt) return { state, effects: NONE }
|
||||
|
||||
return { state: { status: "idle", issued: state.attempt }, effects: NONE }
|
||||
}
|
||||
|
||||
if (state.status !== "opening" || state.attempt !== event.attempt) return { state, effects: NONE }
|
||||
const abort: ConfigActionEffect = { type: "abort", attempt: state.attempt }
|
||||
const idle: ConfigActionState = { status: "idle", issued: state.attempt }
|
||||
|
||||
if (event.type === "opened") return { state: idle, effects: event.close ? [abort, { type: "close-owner" }] : [abort] }
|
||||
|
||||
if (event.type === "copied")
|
||||
return {
|
||||
state: { status: "copied", attempt: state.attempt },
|
||||
effects: [abort, { type: "schedule-reset", attempt: state.attempt, delayMs: CONFIG_COPIED_MS }],
|
||||
}
|
||||
|
||||
if (event.type === "failed") return { state: idle, effects: [abort, { type: "toast-failed", error: event.error }] }
|
||||
|
||||
return { state: idle, effects: [abort, { type: "toast-timed-out" }] }
|
||||
}
|
||||
|
||||
export function configActionView(state: ConfigActionState) {
|
||||
return { disabled: state.status === "opening", copied: state.status === "copied" }
|
||||
}
|
||||
@@ -0,0 +1,218 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import {
|
||||
AIM_TIMEOUT_MS,
|
||||
flyoutActive,
|
||||
flyoutPane,
|
||||
initialFlyoutState,
|
||||
transitionFlyout,
|
||||
type FlyoutAnchor,
|
||||
type FlyoutEvent,
|
||||
type FlyoutState,
|
||||
type FlyoutTransition,
|
||||
} from "./flyout-machine"
|
||||
|
||||
const mcpRow: FlyoutAnchor = { row: 40, above: 100, below: 600 }
|
||||
|
||||
const pluginsRow: FlyoutAnchor = { row: 72, above: 100, below: 600 }
|
||||
|
||||
// Folds the events in order and returns the last transition.
|
||||
const replay = (events: readonly FlyoutEvent[], state: FlyoutState = initialFlyoutState) =>
|
||||
events.reduce<FlyoutTransition>((last, event) => transitionFlyout(last.state, event), { state, effects: [] })
|
||||
|
||||
const enterMcp: FlyoutEvent = { type: "row.entered", service: "mcp", shown: true, anchor: mcpRow }
|
||||
|
||||
const aimPlugins: FlyoutEvent = { type: "row.moved", service: "plugins", aiming: true, anchor: undefined }
|
||||
|
||||
describe("transitionFlyout", () => {
|
||||
test("a row that cannot be measured leaves the flyout closed, so the next open still snaps and refreshes", () => {
|
||||
const failed = transitionFlyout(initialFlyoutState, { ...enterMcp, anchor: undefined })
|
||||
|
||||
expect(failed.state).toBe(initialFlyoutState)
|
||||
expect(failed.effects).toEqual([])
|
||||
|
||||
const opened = transitionFlyout(failed.state, enterMcp)
|
||||
|
||||
expect(opened.state).toEqual({ status: "open", service: "mcp", anchor: mcpRow, refreshed: ["mcp"], issued: 0 })
|
||||
expect(opened.effects).toEqual([{ type: "refresh", service: "mcp" }, { type: "snap-open" }])
|
||||
|
||||
// A later switch glides: it does not snap again.
|
||||
const switched = transitionFlyout(opened.state, {
|
||||
type: "row.moved",
|
||||
service: "plugins",
|
||||
aiming: false,
|
||||
anchor: pluginsRow,
|
||||
})
|
||||
|
||||
expect(switched.state).toMatchObject({ status: "open", service: "plugins", refreshed: ["mcp", "plugins"] })
|
||||
expect(switched.effects).toEqual([{ type: "refresh", service: "plugins" }])
|
||||
})
|
||||
|
||||
test("hiding the details cancels a pending aim, fences its timer and refreshes each list on the next open", () => {
|
||||
const aimed = replay([enterMcp, aimPlugins])
|
||||
|
||||
expect(aimed.state).toMatchObject({ status: "aiming", service: "mcp", target: "plugins", attempt: 1 })
|
||||
expect(aimed.effects).toEqual([{ type: "schedule-aim", attempt: 1, target: "plugins", delayMs: AIM_TIMEOUT_MS }])
|
||||
|
||||
const hidden = transitionFlyout(aimed.state, { type: "hidden" })
|
||||
|
||||
expect(hidden.state).toEqual({ status: "closed", pane: "mcp", anchor: mcpRow, issued: 1 })
|
||||
expect(hidden.effects).toEqual([{ type: "cancel-aim" }])
|
||||
|
||||
// The aim timer of before the hide cannot open the flyout.
|
||||
const late = transitionFlyout(hidden.state, { type: "aim.due", attempt: 1, anchor: pluginsRow })
|
||||
|
||||
expect(late.state).toBe(hidden.state)
|
||||
expect(late.effects).toEqual([])
|
||||
|
||||
const reopened = transitionFlyout(hidden.state, enterMcp)
|
||||
|
||||
expect(reopened.effects).toEqual([{ type: "refresh", service: "mcp" }, { type: "snap-open" }])
|
||||
})
|
||||
|
||||
test("a pointer resting inside the safe triangle hands over to the row it aimed across", () => {
|
||||
const aimed = replay([enterMcp, aimPlugins])
|
||||
const due = transitionFlyout(aimed.state, { type: "aim.due", attempt: 1, anchor: pluginsRow })
|
||||
|
||||
expect(due.state).toEqual({
|
||||
status: "open",
|
||||
service: "plugins",
|
||||
anchor: pluginsRow,
|
||||
refreshed: ["mcp", "plugins"],
|
||||
issued: 1,
|
||||
})
|
||||
expect(flyoutActive(due.state)).toBe("plugins")
|
||||
})
|
||||
|
||||
test("each move across the same row restarts the aim timer without replacing the state", () => {
|
||||
const aimed = replay([enterMcp, aimPlugins])
|
||||
const again = transitionFlyout(aimed.state, aimPlugins)
|
||||
|
||||
expect(again.state).toBe(aimed.state)
|
||||
expect(again.effects).toEqual([{ type: "schedule-aim", attempt: 1, target: "plugins", delayMs: AIM_TIMEOUT_MS }])
|
||||
|
||||
const other = transitionFlyout(aimed.state, { ...aimPlugins, service: "skills" })
|
||||
|
||||
expect(other.state).toMatchObject({ status: "aiming", target: "skills", attempt: 2 })
|
||||
|
||||
// The first target's timer is stale.
|
||||
const stale = transitionFlyout(other.state, { type: "aim.due", attempt: 1, anchor: pluginsRow })
|
||||
|
||||
expect(stale.state).toBe(other.state)
|
||||
})
|
||||
|
||||
test("leaving the rows or reaching the flyout keeps the list on show", () => {
|
||||
const aimed = replay([enterMcp, aimPlugins])
|
||||
|
||||
for (const event of [
|
||||
{ type: "aim.cancelled" },
|
||||
{ type: "row.moved", service: "mcp", aiming: false, anchor: undefined },
|
||||
{ type: "row.moved", service: undefined, aiming: false, anchor: undefined },
|
||||
] satisfies FlyoutEvent[]) {
|
||||
const result = transitionFlyout(aimed.state, event)
|
||||
|
||||
expect(result.state).toEqual({ status: "open", service: "mcp", anchor: mcpRow, refreshed: ["mcp"], issued: 1 })
|
||||
expect(result.effects).toEqual([{ type: "cancel-aim" }])
|
||||
}
|
||||
})
|
||||
|
||||
test("pointer moves that change nothing keep the same state", () => {
|
||||
const opened = transitionFlyout(initialFlyoutState, enterMcp).state
|
||||
|
||||
for (const event of [
|
||||
{ type: "row.moved", service: "mcp", aiming: false, anchor: undefined },
|
||||
{ type: "row.moved", service: undefined, aiming: false, anchor: undefined },
|
||||
{ type: "aim.cancelled" },
|
||||
{ type: "row.entered", service: "plugins", shown: true, anchor: pluginsRow },
|
||||
{ type: "remeasured", anchor: { ...mcpRow } },
|
||||
{ type: "remeasured", anchor: undefined },
|
||||
{ type: "aim.due", attempt: 7, anchor: pluginsRow },
|
||||
] satisfies FlyoutEvent[])
|
||||
expect(transitionFlyout(opened, event)).toEqual({ state: opened, effects: [] })
|
||||
|
||||
for (const event of [
|
||||
{ type: "row.moved", service: "plugins", aiming: false, anchor: pluginsRow },
|
||||
{ type: "aim.cancelled" },
|
||||
{ type: "close", focusRow: true },
|
||||
{ type: "hidden" },
|
||||
] satisfies FlyoutEvent[])
|
||||
expect(transitionFlyout(initialFlyoutState, event).state).toBe(initialFlyoutState)
|
||||
})
|
||||
|
||||
test("a row that cannot be measured during a switch keeps the current list", () => {
|
||||
const opened = transitionFlyout(initialFlyoutState, enterMcp).state
|
||||
const moved = transitionFlyout(opened, { type: "row.moved", service: "plugins", aiming: false, anchor: undefined })
|
||||
|
||||
expect(moved.state).toBe(opened)
|
||||
|
||||
const clicked = transitionFlyout(opened, {
|
||||
type: "row.activated",
|
||||
service: "plugins",
|
||||
focus: false,
|
||||
shown: true,
|
||||
anchor: undefined,
|
||||
})
|
||||
|
||||
expect(clicked.state).toBe(opened)
|
||||
})
|
||||
|
||||
test("activating a row by keyboard opens it and moves focus into its list", () => {
|
||||
const result = transitionFlyout(initialFlyoutState, {
|
||||
type: "row.activated",
|
||||
service: "skills",
|
||||
focus: true,
|
||||
shown: true,
|
||||
anchor: mcpRow,
|
||||
})
|
||||
|
||||
expect(result.effects).toEqual([
|
||||
{ type: "refresh", service: "skills" },
|
||||
{ type: "snap-open" },
|
||||
{ type: "focus-pane", service: "skills" },
|
||||
])
|
||||
|
||||
// Activating it again only moves focus: the list already refreshed.
|
||||
const again = transitionFlyout(result.state, {
|
||||
type: "row.activated",
|
||||
service: "skills",
|
||||
focus: true,
|
||||
shown: true,
|
||||
anchor: mcpRow,
|
||||
})
|
||||
|
||||
expect(again.state).toBe(result.state)
|
||||
expect(again.effects).toEqual([{ type: "focus-pane", service: "skills" }])
|
||||
})
|
||||
|
||||
test("rows of hidden details do not open the flyout", () => {
|
||||
const result = transitionFlyout(initialFlyoutState, { ...enterMcp, shown: false })
|
||||
|
||||
expect(result.state).toBe(initialFlyoutState)
|
||||
})
|
||||
|
||||
test("closing keeps the pane for the exit fade and returns focus to the row only when asked", () => {
|
||||
const opened = transitionFlyout(initialFlyoutState, enterMcp).state
|
||||
const back = transitionFlyout(opened, { type: "close", focusRow: true })
|
||||
|
||||
expect(back.state).toEqual({ status: "closed", pane: "mcp", anchor: mcpRow, issued: 0 })
|
||||
expect(back.effects).toEqual([{ type: "focus-row", service: "mcp" }])
|
||||
expect(flyoutActive(back.state)).toBeUndefined()
|
||||
expect(flyoutPane(back.state)).toBe("mcp")
|
||||
expect(transitionFlyout(opened, { type: "close", focusRow: false }).effects).toEqual([])
|
||||
})
|
||||
|
||||
test("scrolling moves the anchor of the open list", () => {
|
||||
const opened = transitionFlyout(initialFlyoutState, enterMcp).state
|
||||
const moved = transitionFlyout(opened, { type: "remeasured", anchor: pluginsRow })
|
||||
|
||||
expect(moved.state).toMatchObject({ status: "open", service: "mcp", anchor: pluginsRow })
|
||||
})
|
||||
|
||||
test("disposal cancels a pending aim and ignores everything after", () => {
|
||||
const aimed = replay([enterMcp, aimPlugins])
|
||||
const disposed = transitionFlyout(aimed.state, { type: "disposed" })
|
||||
|
||||
expect(disposed).toEqual({ state: { status: "disposed" }, effects: [{ type: "cancel-aim" }] })
|
||||
expect(transitionFlyout(disposed.state, enterMcp).state).toBe(disposed.state)
|
||||
expect(transitionFlyout(initialFlyoutState, { type: "disposed" }).effects).toEqual([])
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,252 @@
|
||||
/** How long a pointer may rest inside the safe triangle before the row under it takes over. */
|
||||
export const AIM_TIMEOUT_MS = 100
|
||||
|
||||
export type Service = "mcp" | "plugins" | "skills"
|
||||
|
||||
/** Where the trigger row sits in the flyout's containing block, and the room around it in the viewport. */
|
||||
export type FlyoutAnchor = { readonly row: number; readonly above: number; readonly below: number }
|
||||
|
||||
/**
|
||||
* The Extensions flyout beside the details popover. Each aim at another row takes the next attempt number, so an aim
|
||||
* timer whose attempt the state no longer holds is stale; `issued` is the last one handed out.
|
||||
*/
|
||||
export type FlyoutState =
|
||||
/** No list shows. `pane` is the last list, kept through the exit so it fades out with its content. */
|
||||
| {
|
||||
readonly status: "closed"
|
||||
readonly pane: Service | undefined
|
||||
readonly anchor: FlyoutAnchor
|
||||
readonly issued: number
|
||||
}
|
||||
/** `refreshed` lists the lists refreshed since the flyout opened, each the first time it showed. */
|
||||
| {
|
||||
readonly status: "open"
|
||||
readonly service: Service
|
||||
readonly anchor: FlyoutAnchor
|
||||
readonly refreshed: readonly Service[]
|
||||
readonly issued: number
|
||||
}
|
||||
/** The pointer heads for the open list across another row; `target` takes over once the pointer rests. */
|
||||
| {
|
||||
readonly status: "aiming"
|
||||
readonly service: Service
|
||||
readonly target: Service
|
||||
readonly attempt: number
|
||||
readonly anchor: FlyoutAnchor
|
||||
readonly refreshed: readonly Service[]
|
||||
}
|
||||
| { readonly status: "disposed" }
|
||||
|
||||
export type FlyoutEvent =
|
||||
/** The pointer entered a row: opens a closed flyout. `anchor` is undefined when the row could not be measured. */
|
||||
| {
|
||||
readonly type: "row.entered"
|
||||
readonly service: Service
|
||||
readonly shown: boolean
|
||||
readonly anchor: FlyoutAnchor | undefined
|
||||
}
|
||||
/** A click or the inline-end arrow key on a row opens its list; `focus` moves focus into it. */
|
||||
| {
|
||||
readonly type: "row.activated"
|
||||
readonly service: Service
|
||||
readonly focus: boolean
|
||||
readonly shown: boolean
|
||||
readonly anchor: FlyoutAnchor | undefined
|
||||
}
|
||||
/**
|
||||
* The pointer moved over the rows, or focus moved to another row: switches an open flyout. `service` is undefined
|
||||
* between rows; `aiming` is whether the pointer heads for the open list. `anchor` is only measured for a switch.
|
||||
*/
|
||||
| {
|
||||
readonly type: "row.moved"
|
||||
readonly service: Service | undefined
|
||||
readonly aiming: boolean
|
||||
readonly anchor: FlyoutAnchor | undefined
|
||||
}
|
||||
/** The pointer left the rows or reached the flyout. */
|
||||
| { readonly type: "aim.cancelled" }
|
||||
| { readonly type: "aim.due"; readonly attempt: number; readonly anchor: FlyoutAnchor | undefined }
|
||||
/** Scrolling or resizing measured the open row again. */
|
||||
| { readonly type: "remeasured"; readonly anchor: FlyoutAnchor | undefined }
|
||||
/** Escape, the inline-start arrow key or a footer action; `focusRow` returns focus from the list to its row. */
|
||||
| { readonly type: "close"; readonly focusRow: boolean }
|
||||
/** The details popover or drawer hid. */
|
||||
| { readonly type: "hidden" }
|
||||
| { readonly type: "disposed" }
|
||||
|
||||
export type FlyoutEffect =
|
||||
/** Starts the aim timer again; it reports `aim.due` with the target row measured. */
|
||||
| { readonly type: "schedule-aim"; readonly attempt: number; readonly target: Service; readonly delayMs: number }
|
||||
| { readonly type: "cancel-aim" }
|
||||
/** The first open lands beside its row at its list's height without a transition; later switches glide. */
|
||||
| { readonly type: "snap-open" }
|
||||
| { readonly type: "refresh"; readonly service: Service }
|
||||
| { readonly type: "focus-pane"; readonly service: Service }
|
||||
/** Runs before the state commits, so focus leaves the list before it goes inert. */
|
||||
| { readonly type: "focus-row"; readonly service: Service }
|
||||
|
||||
export type FlyoutTransition = { readonly state: FlyoutState; readonly effects: readonly FlyoutEffect[] }
|
||||
|
||||
const NO_ANCHOR: FlyoutAnchor = { row: 0, above: 0, below: 0 }
|
||||
|
||||
export const initialFlyoutState: FlyoutState = { status: "closed", pane: undefined, anchor: NO_ANCHOR, issued: 0 }
|
||||
|
||||
const NONE: readonly FlyoutEffect[] = []
|
||||
|
||||
const CANCEL_AIM: readonly FlyoutEffect[] = [{ type: "cancel-aim" }]
|
||||
|
||||
type Opened = Extract<FlyoutState, { status: "open" | "aiming" }>
|
||||
|
||||
export function transitionFlyout(state: FlyoutState, event: FlyoutEvent): FlyoutTransition {
|
||||
if (state.status === "disposed") return { state, effects: NONE }
|
||||
|
||||
if (event.type === "disposed")
|
||||
return { state: { status: "disposed" }, effects: state.status === "aiming" ? CANCEL_AIM : NONE }
|
||||
|
||||
if (state.status === "closed") return opening(state, event)
|
||||
|
||||
if (event.type === "hidden") return close(state, NONE)
|
||||
|
||||
if (event.type === "close")
|
||||
return close(state, event.focusRow ? [{ type: "focus-row", service: state.service }] : NONE)
|
||||
|
||||
if (event.type === "aim.cancelled") return settle(state)
|
||||
|
||||
if (event.type === "remeasured") {
|
||||
if (!event.anchor || sameAnchor(state.anchor, event.anchor)) return { state, effects: NONE }
|
||||
|
||||
return { state: { ...state, anchor: event.anchor }, effects: NONE }
|
||||
}
|
||||
|
||||
if (event.type === "aim.due") {
|
||||
if (state.status !== "aiming" || event.attempt !== state.attempt) return { state, effects: NONE }
|
||||
|
||||
return event.anchor ? show(state, state.target, event.anchor, false) : settle(state)
|
||||
}
|
||||
|
||||
if (event.type === "row.entered") return { state, effects: NONE }
|
||||
|
||||
if (event.type === "row.activated") {
|
||||
if (!event.shown || !event.anchor) return settle(state)
|
||||
|
||||
return show(state, event.service, event.anchor, event.focus)
|
||||
}
|
||||
|
||||
if (!event.service || event.service === state.service) return settle(state)
|
||||
|
||||
if (event.aiming) return aim(state, event.service)
|
||||
|
||||
return event.anchor ? show(state, event.service, event.anchor, false) : settle(state)
|
||||
}
|
||||
|
||||
/** The open flyout, or undefined while it is closed. */
|
||||
export function flyoutActive(state: FlyoutState) {
|
||||
return state.status === "open" || state.status === "aiming" ? state.service : undefined
|
||||
}
|
||||
|
||||
/** The list on show, kept while the flyout fades out. */
|
||||
export function flyoutPane(state: FlyoutState) {
|
||||
if (state.status === "disposed") return undefined
|
||||
|
||||
return state.status === "closed" ? state.pane : state.service
|
||||
}
|
||||
|
||||
export function flyoutAnchor(state: FlyoutState) {
|
||||
return state.status === "disposed" ? NO_ANCHOR : state.anchor
|
||||
}
|
||||
|
||||
export function sameAnchor(a: FlyoutAnchor, b: FlyoutAnchor) {
|
||||
return a.row === b.row && a.above === b.above && a.below === b.below
|
||||
}
|
||||
|
||||
function opening(state: Extract<FlyoutState, { status: "closed" }>, event: FlyoutEvent): FlyoutTransition {
|
||||
if (event.type !== "row.entered" && event.type !== "row.activated") return { state, effects: NONE }
|
||||
|
||||
if (!event.shown || !event.anchor) return { state, effects: NONE }
|
||||
const focus = event.type === "row.activated" && event.focus
|
||||
|
||||
return {
|
||||
state: {
|
||||
status: "open",
|
||||
service: event.service,
|
||||
anchor: event.anchor,
|
||||
refreshed: [event.service],
|
||||
issued: state.issued,
|
||||
},
|
||||
effects: [
|
||||
{ type: "refresh", service: event.service },
|
||||
{ type: "snap-open" },
|
||||
...(focus ? [{ type: "focus-pane", service: event.service } as const] : []),
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
function show(state: Opened, service: Service, anchor: FlyoutAnchor, focus: boolean): FlyoutTransition {
|
||||
const fresh = !state.refreshed.includes(service)
|
||||
|
||||
const effects: readonly FlyoutEffect[] = [
|
||||
...(state.status === "aiming" ? CANCEL_AIM : NONE),
|
||||
...(fresh ? [{ type: "refresh", service } as const] : []),
|
||||
...(focus ? [{ type: "focus-pane", service } as const] : []),
|
||||
]
|
||||
|
||||
if (state.status === "open" && state.service === service && sameAnchor(state.anchor, anchor))
|
||||
return { state, effects }
|
||||
|
||||
return {
|
||||
state: {
|
||||
status: "open",
|
||||
service,
|
||||
anchor,
|
||||
refreshed: fresh ? [...state.refreshed, service] : state.refreshed,
|
||||
issued: issued(state),
|
||||
},
|
||||
effects,
|
||||
}
|
||||
}
|
||||
|
||||
// Each move keeps the pointer's aim alive for another timeout; a new target takes a new attempt.
|
||||
function aim(state: Opened, target: Service): FlyoutTransition {
|
||||
if (state.status === "aiming" && state.target === target)
|
||||
return { state, effects: [{ type: "schedule-aim", attempt: state.attempt, target, delayMs: AIM_TIMEOUT_MS }] }
|
||||
const attempt = issued(state) + 1
|
||||
|
||||
return {
|
||||
state: {
|
||||
status: "aiming",
|
||||
service: state.service,
|
||||
target,
|
||||
attempt,
|
||||
anchor: state.anchor,
|
||||
refreshed: state.refreshed,
|
||||
},
|
||||
effects: [{ type: "schedule-aim", attempt, target, delayMs: AIM_TIMEOUT_MS }],
|
||||
}
|
||||
}
|
||||
|
||||
// The pointer gave up its aim: the list on show stays.
|
||||
function settle(state: Opened): FlyoutTransition {
|
||||
if (state.status === "open") return { state, effects: NONE }
|
||||
|
||||
return {
|
||||
state: {
|
||||
status: "open",
|
||||
service: state.service,
|
||||
anchor: state.anchor,
|
||||
refreshed: state.refreshed,
|
||||
issued: state.attempt,
|
||||
},
|
||||
effects: CANCEL_AIM,
|
||||
}
|
||||
}
|
||||
|
||||
function close(state: Opened, effects: readonly FlyoutEffect[]): FlyoutTransition {
|
||||
return {
|
||||
state: { status: "closed", pane: state.service, anchor: state.anchor, issued: issued(state) },
|
||||
effects: [...(state.status === "aiming" ? CANCEL_AIM : NONE), ...effects],
|
||||
}
|
||||
}
|
||||
|
||||
function issued(state: Opened) {
|
||||
return state.status === "aiming" ? state.attempt : state.issued
|
||||
}
|
||||
@@ -21,6 +21,7 @@ export default {
|
||||
configure: "Configuration file",
|
||||
copyConfigPath: "Copy configuration file path",
|
||||
configFileMissing: "No configuration file found",
|
||||
timedOut: "The server took too long to respond",
|
||||
"setup.mcp": "Add a server with OpenCode",
|
||||
"setup.mcp.prompt":
|
||||
"Help me add an MCP server to my global OpenCode config. Ask me which service I want to connect, then set it up and check that it connects.",
|
||||
@@ -59,5 +60,6 @@ export default {
|
||||
"mcp.error.copy": "Copy details",
|
||||
"mcp.error.copyFailed": "Couldn't copy error details",
|
||||
"mcp.error.disable": "Disable",
|
||||
"mcp.timedOut": "{{name}} took too long to respond",
|
||||
"mcp.error.details": "MCP server: {{name}}\nStatus: failed\nConfiguration: mcp.servers.{{name}}\nError: {{error}}",
|
||||
}
|
||||
|
||||
@@ -0,0 +1,315 @@
|
||||
import { describe, expect, test } from "bun:test"
|
||||
import {
|
||||
MCP_TOGGLE_TIMEOUT_MS,
|
||||
initialMcpState,
|
||||
mcpDialogView,
|
||||
mcpRowView,
|
||||
transitionMcp,
|
||||
type McpEvent,
|
||||
type McpState,
|
||||
type McpTransition,
|
||||
} from "./mcp-machine"
|
||||
|
||||
// The state once a popover lists the servers, as every controller starts.
|
||||
const mounted = transitionMcp(initialMcpState, { type: "mounted" }).state
|
||||
|
||||
// Folds the events in order and returns the last transition.
|
||||
const replay = (events: readonly McpEvent[], state: McpState = mounted) =>
|
||||
events.reduce<McpTransition>((last, event) => transitionMcp(last.state, event), { state, effects: [] })
|
||||
|
||||
const toggle = (
|
||||
name: string,
|
||||
enabled: boolean,
|
||||
status: "connected" | "disabled" | "failed" = "connected",
|
||||
): McpEvent => ({
|
||||
type: "toggle.requested",
|
||||
name,
|
||||
enabled,
|
||||
status,
|
||||
})
|
||||
|
||||
const opened: McpEvent = { type: "error.opened", name: "playwright", error: "Connection refused" }
|
||||
|
||||
describe("transitionMcp", () => {
|
||||
test("toggling one server leaves every other row free to toggle", () => {
|
||||
const figma = transitionMcp(mounted, toggle("figma", false))
|
||||
|
||||
expect(figma.effects).toEqual([
|
||||
{ type: "run-toggle", name: "figma", attempt: 1, enabled: false, timeoutMs: MCP_TOGGLE_TIMEOUT_MS },
|
||||
])
|
||||
expect(mcpRowView(figma.state, "figma", "connected")).toEqual({ busy: true, pending: true })
|
||||
expect(mcpRowView(figma.state, "off", "disabled")).toEqual({ busy: false, pending: false })
|
||||
|
||||
const off = transitionMcp(figma.state, toggle("off", true, "disabled"))
|
||||
|
||||
expect(off.effects).toEqual([
|
||||
{ type: "run-toggle", name: "off", attempt: 2, enabled: true, timeoutMs: MCP_TOGGLE_TIMEOUT_MS },
|
||||
])
|
||||
|
||||
const settled = transitionMcp(off.state, { type: "toggle.succeeded", name: "off", attempt: 2 })
|
||||
|
||||
expect(settled.effects).toEqual([{ type: "abort-toggle", attempt: 2 }])
|
||||
expect(mcpRowView(settled.state, "off", "connected").busy).toBe(false)
|
||||
expect(mcpRowView(settled.state, "figma", "connected").busy).toBe(true)
|
||||
})
|
||||
|
||||
test("the row and the error dialog share one toggle per server", () => {
|
||||
const retrying = replay([opened, toggle("playwright", true, "failed")])
|
||||
|
||||
expect(retrying.effects).toEqual([
|
||||
{ type: "run-toggle", name: "playwright", attempt: 2, enabled: true, timeoutMs: MCP_TOGGLE_TIMEOUT_MS },
|
||||
])
|
||||
|
||||
const disable = transitionMcp(retrying.state, toggle("playwright", false, "failed"))
|
||||
|
||||
expect(disable).toEqual({ state: retrying.state, effects: [] })
|
||||
})
|
||||
|
||||
test("a server that is still connecting takes no toggle", () => {
|
||||
const result = transitionMcp(mounted, {
|
||||
type: "toggle.requested",
|
||||
name: "waiting",
|
||||
enabled: false,
|
||||
status: "pending",
|
||||
})
|
||||
|
||||
expect(result).toEqual({ state: mounted, effects: [] })
|
||||
expect(mcpRowView(mounted, "waiting", "pending")).toEqual({ busy: false, pending: true })
|
||||
})
|
||||
|
||||
test("the error dialog stays open while a retry connects and shows the retry's new failure", () => {
|
||||
const retrying = replay([opened, toggle("playwright", true, "failed")])
|
||||
|
||||
const pending = transitionMcp(retrying.state, {
|
||||
type: "server.synced",
|
||||
attempt: 1,
|
||||
loaded: true,
|
||||
status: { status: "pending" },
|
||||
})
|
||||
|
||||
expect(pending.effects).toEqual([])
|
||||
expect(mcpDialogView(pending.state, 1, "pending")).toEqual({
|
||||
name: "playwright",
|
||||
error: "Connection refused",
|
||||
copied: false,
|
||||
busy: true,
|
||||
})
|
||||
|
||||
const failed = replay(
|
||||
[
|
||||
{
|
||||
type: "server.synced",
|
||||
attempt: 1,
|
||||
loaded: true,
|
||||
status: { status: "failed", error: "Port 9222 unreachable" },
|
||||
},
|
||||
{ type: "toggle.failed", name: "playwright", attempt: 2, error: "Port 9222 unreachable", server: true },
|
||||
],
|
||||
pending.state,
|
||||
)
|
||||
|
||||
// The dialog shows the failure, so no toast repeats it.
|
||||
expect(failed.effects).toEqual([{ type: "abort-toggle", attempt: 2 }])
|
||||
expect(mcpDialogView(failed.state, 1, "failed")).toEqual({
|
||||
name: "playwright",
|
||||
error: "Port 9222 unreachable",
|
||||
copied: false,
|
||||
busy: false,
|
||||
})
|
||||
|
||||
const connected = replay(
|
||||
[
|
||||
toggle("playwright", true, "failed"),
|
||||
{ type: "server.synced", attempt: 1, loaded: true, status: { status: "connected" } },
|
||||
],
|
||||
failed.state,
|
||||
)
|
||||
|
||||
expect(connected.effects).toEqual([{ type: "close-dialog", attempt: 1 }])
|
||||
expect(mcpDialogView(connected.state, 1, "connected")).toBeUndefined()
|
||||
})
|
||||
|
||||
test("a retry that succeeds closes the dialog even when the server list did not change", () => {
|
||||
const result = replay([
|
||||
opened,
|
||||
toggle("playwright", true, "failed"),
|
||||
{ type: "toggle.succeeded", name: "playwright", attempt: 2 },
|
||||
])
|
||||
|
||||
expect(result.effects).toEqual([
|
||||
{ type: "abort-toggle", attempt: 2 },
|
||||
{ type: "close-dialog", attempt: 1 },
|
||||
])
|
||||
})
|
||||
|
||||
test("a request that throws shows a toast and keeps the dialog's server error", () => {
|
||||
const result = replay([
|
||||
opened,
|
||||
toggle("playwright", true, "failed"),
|
||||
{ type: "toggle.failed", name: "playwright", attempt: 2, error: "Network error", server: false },
|
||||
])
|
||||
|
||||
expect(result.effects).toEqual([
|
||||
{ type: "abort-toggle", attempt: 2 },
|
||||
{ type: "toast-failed", message: "Network error" },
|
||||
])
|
||||
expect(mcpDialogView(result.state, 1, "failed")?.error).toBe("Connection refused")
|
||||
})
|
||||
|
||||
test("a failed status without a dialog shows a toast that names the server", () => {
|
||||
const result = replay([
|
||||
toggle("figma", true, "disabled"),
|
||||
{ type: "toggle.failed", name: "figma", attempt: 1, error: "404 Not Found", server: true },
|
||||
])
|
||||
|
||||
expect(result.effects).toEqual([
|
||||
{ type: "abort-toggle", attempt: 1 },
|
||||
{ type: "toast-failed", message: "figma: 404 Not Found" },
|
||||
])
|
||||
})
|
||||
|
||||
test("the server list decides when the dialog closes", () => {
|
||||
const open = transitionMcp(mounted, opened).state
|
||||
|
||||
expect(open).toMatchObject({ dialog: { status: "open", attempt: 1 } })
|
||||
|
||||
for (const status of [
|
||||
{ status: "needs_auth", error: "Sign in" },
|
||||
{ status: "failed", error: "Connection refused" },
|
||||
] as const)
|
||||
expect(transitionMcp(open, { type: "server.synced", attempt: 1, loaded: true, status }).state).toBe(open)
|
||||
|
||||
expect(transitionMcp(open, { type: "server.synced", attempt: 1, loaded: false, status: undefined }).state).toBe(
|
||||
open,
|
||||
)
|
||||
|
||||
for (const status of [{ status: "disabled" }, undefined] as const) {
|
||||
const result = transitionMcp(open, { type: "server.synced", attempt: 1, loaded: true, status })
|
||||
|
||||
expect(result.effects).toEqual([{ type: "close-dialog", attempt: 1 }])
|
||||
expect(mcpDialogView(result.state, 1, status?.status)).toBeUndefined()
|
||||
}
|
||||
})
|
||||
|
||||
test("a sign-in hands over from the dialog to the browser before the resources refresh", () => {
|
||||
const result = replay([
|
||||
opened,
|
||||
toggle("playwright", true, "failed"),
|
||||
{
|
||||
type: "toggle.authorizing",
|
||||
name: "playwright",
|
||||
attempt: 2,
|
||||
url: "https://auth.example/authorize",
|
||||
signIn: { integrationID: "oauth", attemptID: "a1", url: "https://auth.example/authorize", lifetime: 60_000 },
|
||||
},
|
||||
])
|
||||
|
||||
expect(result.effects).toEqual([
|
||||
{ type: "close-dialog", attempt: 1 },
|
||||
{ type: "open-external", url: "https://auth.example/authorize" },
|
||||
{
|
||||
type: "open-sign-in",
|
||||
name: "playwright",
|
||||
signIn: { integrationID: "oauth", attemptID: "a1", url: "https://auth.example/authorize", lifetime: 60_000 },
|
||||
},
|
||||
])
|
||||
expect(mcpRowView(result.state, "playwright", "needs_auth").busy).toBe(true)
|
||||
})
|
||||
|
||||
test("results of an attempt that ended change nothing", () => {
|
||||
const timedOut = replay([toggle("figma", false), { type: "toggle.timed-out", name: "figma", attempt: 1 }])
|
||||
|
||||
expect(timedOut.effects).toEqual([
|
||||
{ type: "abort-toggle", attempt: 1 },
|
||||
{ type: "toast-timed-out", name: "figma" },
|
||||
])
|
||||
|
||||
for (const event of [
|
||||
{ type: "toggle.succeeded", name: "figma", attempt: 1 },
|
||||
{ type: "toggle.failed", name: "figma", attempt: 1, error: "late", server: false },
|
||||
{ type: "toggle.authorizing", name: "figma", attempt: 1, url: "https://auth.example", signIn: undefined },
|
||||
] satisfies McpEvent[])
|
||||
expect(transitionMcp(timedOut.state, event)).toEqual({ state: timedOut.state, effects: [] })
|
||||
})
|
||||
|
||||
test("a closed or replaced dialog ignores its late events", () => {
|
||||
const first = transitionMcp(mounted, opened).state
|
||||
const second = transitionMcp(first, { type: "error.opened", name: "figma", error: "Timeout" })
|
||||
|
||||
expect(second.effects).toEqual([
|
||||
{ type: "close-dialog", attempt: 1 },
|
||||
{ type: "open-dialog", attempt: 2, name: "figma" },
|
||||
])
|
||||
|
||||
for (const event of [
|
||||
{ type: "error.dismissed", attempt: 1 },
|
||||
{ type: "error.copied", attempt: 1, error: "Connection refused" },
|
||||
{ type: "server.synced", attempt: 1, loaded: true, status: { status: "connected" } },
|
||||
] satisfies McpEvent[])
|
||||
expect(transitionMcp(second.state, event).state).toBe(second.state)
|
||||
})
|
||||
|
||||
test("the copied mark follows the error text it copied", () => {
|
||||
const copied = replay([opened, { type: "error.copied", attempt: 1, error: "Connection refused" }])
|
||||
|
||||
expect(mcpDialogView(copied.state, 1, "failed")?.copied).toBe(true)
|
||||
|
||||
const changed = transitionMcp(copied.state, {
|
||||
type: "server.synced",
|
||||
attempt: 1,
|
||||
loaded: true,
|
||||
status: { status: "failed", error: "Port 9222 unreachable" },
|
||||
})
|
||||
|
||||
expect(mcpDialogView(changed.state, 1, "failed")).toMatchObject({ error: "Port 9222 unreachable", copied: false })
|
||||
})
|
||||
|
||||
test("a popover that closes and reopens keeps the toggle its dialog's retry started", () => {
|
||||
const retrying = replay([opened, toggle("playwright", true, "failed"), { type: "error.dismissed", attempt: 1 }])
|
||||
const reopened = replay([{ type: "unmounted" }, { type: "mounted" }], retrying.state)
|
||||
|
||||
expect(reopened.state).toMatchObject({ status: "active", mounts: 1, toggles: [{ name: "playwright", attempt: 2 }] })
|
||||
expect(mcpRowView(reopened.state, "playwright", "failed")).toEqual({ busy: true, pending: true })
|
||||
|
||||
// The reopened row and a dialog opened again cannot start a second connect for the same server.
|
||||
const again = replay([opened, toggle("playwright", true, "failed")], reopened.state)
|
||||
|
||||
expect(again.effects).toEqual([])
|
||||
expect(mcpDialogView(again.state, 3, "failed")?.busy).toBe(true)
|
||||
expect(transitionMcp(reopened.state, toggle("playwright", false, "failed")).state).toBe(reopened.state)
|
||||
})
|
||||
|
||||
test("a list ends once no popover lists it, no toggle runs and no dialog shows", () => {
|
||||
const closed = replay([toggle("figma", false), opened, { type: "unmounted" }])
|
||||
|
||||
expect(closed.state).toMatchObject({ status: "active", mounts: 0 })
|
||||
|
||||
const settled = transitionMcp(closed.state, { type: "toggle.succeeded", name: "figma", attempt: 1 })
|
||||
|
||||
expect(settled.state).toMatchObject({ status: "active", mounts: 0 })
|
||||
|
||||
const dismissed = transitionMcp(settled.state, { type: "error.dismissed", attempt: 2 })
|
||||
|
||||
expect(dismissed.state).toEqual({ status: "disposed" })
|
||||
expect(transitionMcp(mounted, { type: "unmounted" }).state).toEqual({ status: "disposed" })
|
||||
|
||||
// A second popover or drawer keeps it while the first one closes.
|
||||
const both = transitionMcp(mounted, { type: "mounted" }).state
|
||||
|
||||
expect(transitionMcp(both, { type: "unmounted" }).state).toMatchObject({ status: "active", mounts: 1 })
|
||||
})
|
||||
|
||||
test("disposal aborts every toggle and closes the dialog", () => {
|
||||
const result = replay([toggle("figma", false), toggle("off", true, "disabled"), opened, { type: "disposed" }])
|
||||
|
||||
expect(result).toEqual({
|
||||
state: { status: "disposed" },
|
||||
effects: [
|
||||
{ type: "abort-toggle", attempt: 1 },
|
||||
{ type: "abort-toggle", attempt: 2 },
|
||||
{ type: "close-dialog", attempt: 3 },
|
||||
],
|
||||
})
|
||||
expect(transitionMcp(result.state, toggle("figma", true)).state).toBe(result.state)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,278 @@
|
||||
/** How long one connect, disconnect or sign-in may run before it gives up with an error. */
|
||||
export const MCP_TOGGLE_TIMEOUT_MS = 60_000
|
||||
|
||||
/** An MCP server's status, as the server lists it. */
|
||||
export type McpServerStatus =
|
||||
| { readonly status: "connected" | "pending" | "disabled" }
|
||||
| { readonly status: "failed" | "needs_auth"; readonly error: string }
|
||||
|
||||
/** A sign-in that also accepts the redirect URL pasted from the browser. */
|
||||
export type McpSignIn = {
|
||||
readonly integrationID: string
|
||||
readonly attemptID: string
|
||||
readonly url: string
|
||||
/** The attempt's lifetime in milliseconds. */
|
||||
readonly lifetime: number
|
||||
}
|
||||
|
||||
/** One server's connect, disconnect or sign-in in flight. */
|
||||
export type McpToggle = { readonly name: string; readonly attempt: number }
|
||||
|
||||
export type McpErrorDialog =
|
||||
| { readonly status: "closed" }
|
||||
/**
|
||||
* One server's failure. `error` is its latest failure, kept while a retry runs; `copied` is the error text the user
|
||||
* last copied.
|
||||
*/
|
||||
| {
|
||||
readonly status: "open"
|
||||
readonly attempt: number
|
||||
readonly name: string
|
||||
readonly error: string
|
||||
readonly copied: string | undefined
|
||||
}
|
||||
|
||||
/**
|
||||
* The MCP list of one server and directory: its toggles in flight and its error dialog, shared by every popover or
|
||||
* drawer that lists it. It lives while one is mounted, a toggle runs or the dialog shows. Toggles and dialogs take
|
||||
* attempt numbers from `issued`, so a result whose attempt the state no longer holds is stale.
|
||||
*/
|
||||
export type McpState =
|
||||
| {
|
||||
readonly status: "active"
|
||||
/** The popovers and drawers that list the servers. */
|
||||
readonly mounts: number
|
||||
readonly issued: number
|
||||
readonly toggles: readonly McpToggle[]
|
||||
readonly dialog: McpErrorDialog
|
||||
}
|
||||
| { readonly status: "disposed" }
|
||||
|
||||
export type McpEvent =
|
||||
/** A row's switch or sign-in button, or the error dialog's Disable or Try again. */
|
||||
| {
|
||||
readonly type: "toggle.requested"
|
||||
readonly name: string
|
||||
readonly enabled: boolean
|
||||
readonly status: McpServerStatus["status"] | undefined
|
||||
}
|
||||
/** The server started an OAuth sign-in; the toggle goes on to refresh the server's resources. */
|
||||
| {
|
||||
readonly type: "toggle.authorizing"
|
||||
readonly name: string
|
||||
readonly attempt: number
|
||||
readonly url: string
|
||||
readonly signIn: McpSignIn | undefined
|
||||
}
|
||||
| { readonly type: "toggle.succeeded"; readonly name: string; readonly attempt: number }
|
||||
/** `server` is true when the refreshed status failed, false when a request threw. */
|
||||
| {
|
||||
readonly type: "toggle.failed"
|
||||
readonly name: string
|
||||
readonly attempt: number
|
||||
readonly error: string
|
||||
readonly server: boolean
|
||||
}
|
||||
| { readonly type: "toggle.timed-out"; readonly name: string; readonly attempt: number }
|
||||
| { readonly type: "error.opened"; readonly name: string; readonly error: string }
|
||||
/** The dialog closed, by the user or by the machine. */
|
||||
| { readonly type: "error.dismissed"; readonly attempt: number }
|
||||
| { readonly type: "error.copied"; readonly attempt: number; readonly error: string }
|
||||
/** The dialog's server changed in the server's list. `loaded` is false while the list is not known. */
|
||||
| {
|
||||
readonly type: "server.synced"
|
||||
readonly attempt: number
|
||||
readonly loaded: boolean
|
||||
readonly status: McpServerStatus | undefined
|
||||
}
|
||||
/** A popover or drawer that lists the servers mounted or unmounted. */
|
||||
| { readonly type: "mounted" }
|
||||
| { readonly type: "unmounted" }
|
||||
| { readonly type: "disposed" }
|
||||
|
||||
export type McpEffect =
|
||||
/** Starts the attempt's controller and timeout, then runs the toggle. */
|
||||
| {
|
||||
readonly type: "run-toggle"
|
||||
readonly name: string
|
||||
readonly attempt: number
|
||||
readonly enabled: boolean
|
||||
readonly timeoutMs: number
|
||||
}
|
||||
/** Ends the attempt's controller and timeout. */
|
||||
| { readonly type: "abort-toggle"; readonly attempt: number }
|
||||
| { readonly type: "open-external"; readonly url: string }
|
||||
| { readonly type: "open-sign-in"; readonly name: string; readonly signIn: McpSignIn }
|
||||
| { readonly type: "open-dialog"; readonly attempt: number; readonly name: string }
|
||||
| { readonly type: "close-dialog"; readonly attempt: number }
|
||||
| { readonly type: "toast-failed"; readonly message: string }
|
||||
| { readonly type: "toast-timed-out"; readonly name: string }
|
||||
|
||||
export type McpTransition = { readonly state: McpState; readonly effects: readonly McpEffect[] }
|
||||
|
||||
export const initialMcpState: McpState = {
|
||||
status: "active",
|
||||
mounts: 0,
|
||||
issued: 0,
|
||||
toggles: [],
|
||||
dialog: { status: "closed" },
|
||||
}
|
||||
|
||||
const NONE: readonly McpEffect[] = []
|
||||
|
||||
const CLOSED: McpErrorDialog = { status: "closed" }
|
||||
|
||||
type Live = Exclude<McpState, { status: "disposed" }>
|
||||
|
||||
export function transitionMcp(state: McpState, event: McpEvent): McpTransition {
|
||||
if (state.status === "disposed") return { state, effects: NONE }
|
||||
|
||||
if (event.type === "disposed")
|
||||
return {
|
||||
state: { status: "disposed" },
|
||||
effects: [
|
||||
...state.toggles.map((toggle): McpEffect => ({ type: "abort-toggle", attempt: toggle.attempt })),
|
||||
...closeDialog(state.dialog),
|
||||
],
|
||||
}
|
||||
|
||||
return release(step(state, event))
|
||||
}
|
||||
|
||||
/** What a row shows: `busy` while its own toggle runs, `pending` while it takes no new toggle. */
|
||||
export function mcpRowView(state: McpState, name: string, status: McpServerStatus["status"]) {
|
||||
const busy = toggling(state, name)
|
||||
|
||||
return { busy, pending: busy || status === "pending" }
|
||||
}
|
||||
|
||||
/** What an open error dialog shows, or undefined once it closed. */
|
||||
export function mcpDialogView(state: McpState, attempt: number, status: McpServerStatus["status"] | undefined) {
|
||||
if (state.status === "disposed" || state.dialog.status !== "open" || state.dialog.attempt !== attempt) return
|
||||
|
||||
return {
|
||||
name: state.dialog.name,
|
||||
error: state.dialog.error,
|
||||
copied: state.dialog.copied === state.dialog.error,
|
||||
busy: toggling(state, state.dialog.name) || status === "pending",
|
||||
}
|
||||
}
|
||||
|
||||
function step(state: Live, event: Exclude<McpEvent, { type: "disposed" }>): McpTransition {
|
||||
if (event.type === "mounted") return { state: { ...state, mounts: state.mounts + 1 }, effects: NONE }
|
||||
|
||||
if (event.type === "unmounted") return { state: { ...state, mounts: Math.max(0, state.mounts - 1) }, effects: NONE }
|
||||
|
||||
if (event.type === "toggle.requested") {
|
||||
// One toggle per server at a time, from its row and its dialog alike.
|
||||
if (toggling(state, event.name) || event.status === "pending") return { state, effects: NONE }
|
||||
const attempt = state.issued + 1
|
||||
|
||||
return {
|
||||
state: { ...state, issued: attempt, toggles: [...state.toggles, { name: event.name, attempt }] },
|
||||
effects: [
|
||||
{ type: "run-toggle", name: event.name, attempt, enabled: event.enabled, timeoutMs: MCP_TOGGLE_TIMEOUT_MS },
|
||||
],
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
event.type === "toggle.authorizing" ||
|
||||
event.type === "toggle.succeeded" ||
|
||||
event.type === "toggle.failed" ||
|
||||
event.type === "toggle.timed-out"
|
||||
)
|
||||
return settled(state, event)
|
||||
|
||||
if (event.type === "error.opened") {
|
||||
const attempt = state.issued + 1
|
||||
|
||||
return {
|
||||
state: {
|
||||
...state,
|
||||
issued: attempt,
|
||||
dialog: { status: "open", attempt, name: event.name, error: event.error, copied: undefined },
|
||||
},
|
||||
effects: [...closeDialog(state.dialog), { type: "open-dialog", attempt, name: event.name }],
|
||||
}
|
||||
}
|
||||
|
||||
const dialog = state.dialog
|
||||
|
||||
if (dialog.status !== "open" || dialog.attempt !== event.attempt) return { state, effects: NONE }
|
||||
|
||||
if (event.type === "error.dismissed") return { state: { ...state, dialog: CLOSED }, effects: NONE }
|
||||
|
||||
if (event.type === "error.copied")
|
||||
return { state: { ...state, dialog: { ...dialog, copied: event.error } }, effects: NONE }
|
||||
|
||||
if (!event.loaded) return { state, effects: NONE }
|
||||
const status = event.status
|
||||
|
||||
// A reconnect, a disable or a removal, here or elsewhere, leaves nothing to show.
|
||||
if (!status || status.status === "connected" || status.status === "disabled")
|
||||
return { state: { ...state, dialog: CLOSED }, effects: closeDialog(dialog) }
|
||||
|
||||
// A retry stays open while it connects, and a new failure replaces the error in place.
|
||||
if (status.status !== "failed" || status.error === dialog.error) return { state, effects: NONE }
|
||||
|
||||
return { state: { ...state, dialog: { ...dialog, error: status.error } }, effects: NONE }
|
||||
}
|
||||
|
||||
function settled(
|
||||
state: Live,
|
||||
event: Extract<McpEvent, { type: "toggle.authorizing" | "toggle.succeeded" | "toggle.failed" | "toggle.timed-out" }>,
|
||||
): McpTransition {
|
||||
if (!state.toggles.some((toggle) => toggle.name === event.name && toggle.attempt === event.attempt))
|
||||
return { state, effects: NONE }
|
||||
const dialog = state.dialog
|
||||
const shown = dialog.status === "open" && dialog.name === event.name
|
||||
|
||||
// The browser takes over from the dialog; the toggle still refreshes the server's resources.
|
||||
if (event.type === "toggle.authorizing")
|
||||
return {
|
||||
state: shown ? { ...state, dialog: CLOSED } : state,
|
||||
effects: [
|
||||
...(shown ? closeDialog(dialog) : NONE),
|
||||
{ type: "open-external", url: event.url },
|
||||
...(event.signIn ? [{ type: "open-sign-in", name: event.name, signIn: event.signIn } as const] : []),
|
||||
],
|
||||
}
|
||||
|
||||
const next = { ...state, toggles: state.toggles.filter((toggle) => toggle.attempt !== event.attempt) }
|
||||
const abort: McpEffect = { type: "abort-toggle", attempt: event.attempt }
|
||||
|
||||
if (event.type === "toggle.succeeded")
|
||||
return shown
|
||||
? { state: { ...next, dialog: CLOSED }, effects: [abort, ...closeDialog(dialog)] }
|
||||
: { state: next, effects: [abort] }
|
||||
|
||||
if (event.type === "toggle.timed-out")
|
||||
return { state: next, effects: [abort, { type: "toast-timed-out", name: event.name }] }
|
||||
|
||||
// The open dialog shows the server's new failure itself.
|
||||
if (shown && event.server) return { state: { ...next, dialog: { ...dialog, error: event.error } }, effects: [abort] }
|
||||
|
||||
return {
|
||||
state: next,
|
||||
effects: [abort, { type: "toast-failed", message: event.server ? `${event.name}: ${event.error}` : event.error }],
|
||||
}
|
||||
}
|
||||
|
||||
// The list ends once nothing uses it: no popover or drawer lists it, no toggle runs and no dialog shows.
|
||||
function release(transition: McpTransition): McpTransition {
|
||||
const state = transition.state
|
||||
|
||||
if (state.status !== "active" || state.mounts > 0 || state.toggles.length > 0 || state.dialog.status === "open")
|
||||
return transition
|
||||
|
||||
return { state: { status: "disposed" }, effects: transition.effects }
|
||||
}
|
||||
|
||||
function closeDialog(dialog: McpErrorDialog): readonly McpEffect[] {
|
||||
return dialog.status === "open" ? [{ type: "close-dialog", attempt: dialog.attempt }] : NONE
|
||||
}
|
||||
|
||||
function toggling(state: McpState, name: string) {
|
||||
return state.status !== "disposed" && state.toggles.some((toggle) => toggle.name === name)
|
||||
}
|
||||
@@ -1,10 +1,12 @@
|
||||
import type { MountedSession } from "@opencode/plugin/gui"
|
||||
import { SessionServerPanel } from "./server-panel"
|
||||
import { SessionServerPanel, type McpControllers } from "./server-panel"
|
||||
|
||||
export type DetailsPanelProps = {
|
||||
session: MountedSession
|
||||
shown?: boolean
|
||||
mobile?: boolean
|
||||
/** The plugin instance's MCP controllers, which outlive each panel. */
|
||||
mcp: McpControllers
|
||||
/** Closes the popover that shows the panel, once an action opened something beside it. */
|
||||
onClose?: () => void
|
||||
}
|
||||
@@ -17,6 +19,7 @@ export default function SessionDetailsPanel(props: DetailsPanelProps) {
|
||||
directory={props.session.directory}
|
||||
shown={props.shown !== false}
|
||||
mobile={props.mobile}
|
||||
mcp={props.mcp}
|
||||
onClose={props.onClose}
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -6,6 +6,7 @@ import { Tooltip } from "@opencode/ui/tooltip"
|
||||
import { onCleanup, Show, Suspense, type Component, type ParentProps } from "solid-js"
|
||||
import { Command, createVisitState, usePlugin, type MountedSession } from "@opencode/plugin/gui"
|
||||
import type { DetailsPanelProps } from "./panel"
|
||||
import type { McpControllers } from "./server-panel"
|
||||
|
||||
/** Every mounted details popover, so a shortcut finds the one on screen. */
|
||||
export type DetailsMenus = Set<{ trigger: () => HTMLButtonElement | undefined; toggle: () => void }>
|
||||
@@ -16,6 +17,7 @@ export function DetailsHeader(props: {
|
||||
session: MountedSession
|
||||
active: boolean
|
||||
panel: Component<DetailsPanelProps>
|
||||
mcp: McpControllers
|
||||
}) {
|
||||
const Panel = props.panel
|
||||
// Cached timelines stay mounted while hidden; routing away from the session closes its details.
|
||||
@@ -25,7 +27,7 @@ export function DetailsHeader(props: {
|
||||
<Show when={props.session.project}>
|
||||
<DetailsPopover menus={props.menus} active={props.active} open={open()} onOpenChange={setOpen}>
|
||||
<Suspense>
|
||||
<Panel session={props.session} shown={open()} onClose={() => setOpen(false)} />
|
||||
<Panel session={props.session} shown={open()} mcp={props.mcp} onClose={() => setOpen(false)} />
|
||||
</Suspense>
|
||||
</DetailsPopover>
|
||||
</Show>
|
||||
|
||||
@@ -3,11 +3,14 @@ import { Panel, Slot, Style, usePanel, type Plugin } from "@opencode/plugin/gui"
|
||||
import type Details from "./index"
|
||||
import { DetailsHeader, type DetailsMenus } from "./popover"
|
||||
import { BuiltinSlot, onIdle } from "@opencode/plugin/gui/builtin"
|
||||
import type { McpControllers } from "./server-panel"
|
||||
|
||||
const setup: Plugin.Setup<typeof Details> = (ctx) => {
|
||||
const sessions = ctx.sessions
|
||||
|
||||
const menus: DetailsMenus = new Set()
|
||||
// MCP toggles and error dialogs outlive the popover or drawer that started them.
|
||||
const mcp: McpControllers = new Map()
|
||||
|
||||
const DetailsPanel = lazy(() =>
|
||||
Promise.all([import("./panel"), import("./details.css?inline")]).then(([panel, css]) => {
|
||||
@@ -33,7 +36,7 @@ const setup: Plugin.Setup<typeof Details> = (ctx) => {
|
||||
at: "session.header",
|
||||
order: 20,
|
||||
render: (input) => (
|
||||
<DetailsHeader menus={menus} session={input.session} active={input.active} panel={DetailsPanel} />
|
||||
<DetailsHeader menus={menus} session={input.session} active={input.active} panel={DetailsPanel} mcp={mcp} />
|
||||
),
|
||||
})
|
||||
|
||||
@@ -74,7 +77,7 @@ const setup: Plugin.Setup<typeof Details> = (ctx) => {
|
||||
return (
|
||||
<Show when={props.session.project}>
|
||||
<Suspense>
|
||||
<DetailsPanel mobile shown={frame.visible()} session={props.session} />
|
||||
<DetailsPanel mobile shown={frame.visible()} session={props.session} mcp={mcp} />
|
||||
</Suspense>
|
||||
</Show>
|
||||
)
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user