fix(web): make the web console work end-to-end

- Pair through the public POST /api/v1/pair and persist the device scopes;
  keep the allow-listed next deep link. Rename the manual field to pairing code.
- Authenticate the WebSocket with the raw device token (strip Bearer) and
  recover from a 401 by clearing the connection once.
- Fix chat payloads: setModel/setThinking, attention/diff responses, subagent
  dispatch items, queue ids/mode, task output and WS resume since.
- Render exactly one assistant message per turn: one-to-one snapshot/history
  merge plus a stale-delta guard after message_end (fixes PONGONG).
- Fix dialogs (content above the overlay), unknown-session deep links
  (error + retry, no silent fallback), device list/revoke, status identity and
  live pairing code, reconnect banner and Playwright selectors.
This commit is contained in:
yuez committed 2026-10-21 20:42:19 +08:00
1 parent 5572ac0d84
commit 0ef019f58d
57 files changed
+3549 -684

No files matched your search

+229 -23
View File
@@ -1,17 +1,25 @@
import { fireEvent, screen, waitFor, within } from "@testing-library/react";
import { act, fireEvent, screen, waitFor, within } from "@testing-library/react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { commandCalls, mockApi } from "./helpers/api-mock";
import { commandCalls, mockApi, type MockRoute } from "./helpers/api-mock";
import { renderPage } from "./helpers/render-page";
import { setLanguage } from "@/lib/i18n";
import { StatusPage } from "@/pages/status";
import { useConnectionStore } from "@/stores/connection";
/** `/info` as the daemon answers it (src/daemon/http.ts §/api/v1/info). */
const infoBody = {
v: 1,
daemonId: "daemon-1",
machineId: "machine-1",
platform: "linux",
hostname: "pi2-box",
os: "Linux 6.1.0",
version: "0.2.0",
uptimeMs: 2 * 3_600_000 + 5 * 60_000,
pid: 4242,
port: 4321,
capabilities: ["read", "steer"],
limits: { maxInstances: 5 },
};
@@ -26,7 +34,8 @@ const workspacesBody = [
sessionCount: 2,
liveCount: 1,
lastActiveAt: Date.now(),
instance: null,
// A live instance is what makes `/sessions` answer without spawning one.
instance: { workspaceId: "ws-1", status: "ready", pid: 4242, since: Date.now() },
},
];
@@ -65,6 +74,46 @@ const modelsBody = {
],
};
const pairBody = {
accepted: true,
code: "ABCD1234",
url: "http://127.0.0.1:4321/pair/ABCD1234",
expiresAt: Date.now() + 9 * 60_000,
nonce: "nonce-1",
};
const tasksBody = {
sessions: [
{
sessionId: "sess-1",
workspaceId: "ws-1",
tasks: [
{
id: "task-1",
command: "pnpm build",
status: "running",
startTime: Date.now() - 5_000,
durationMs: 4_200,
exitCode: null,
},
],
},
],
};
/** `GET /api/v1/devices` answers a BARE ARRAY (`auth.listDevices()`). */
const devicesBody = [
{
id: "dev-1",
name: "Pixel",
kind: "phone",
scopes: ["read", "steer"],
createdAt: Date.now() - 90_000,
lastSeenAt: Date.now() - 60_000,
revokedAt: null,
},
];
function routes() {
return [
{ path: "/status", component: StatusPage },
@@ -73,28 +122,47 @@ function routes() {
];
}
function apiRoutes(devicesHandler = () => ({ body: { devices: devicesBody } })) {
return [
const SESSIONS_PATH = /^\/api\/v1\/workspaces\/[^/]+\/sessions$/;
function apiRoutes(overrides: MockRoute[] = []) {
const base: MockRoute[] = [
{ path: "/api/v1/info", handler: () => ({ body: infoBody }) },
{ path: "/api/v1/health", handler: () => ({ body: healthBody }) },
{ path: "/api/v1/resources", handler: () => ({ body: resourcesBody }) },
{ path: "/api/v1/workspaces", handler: () => ({ body: workspacesBody }) },
{ path: SESSIONS_PATH, handler: () => ({ body: tasksBody }) },
{ path: "/api/v1/models", handler: () => ({ body: modelsBody }) },
{ path: "/api/v1/devices", handler: devicesHandler },
{ path: "/api/v1/devices", handler: () => ({ body: devicesBody }) },
{ method: "DELETE" as const, path: "/api/v1/devices/dev-1", handler: () => ({ status: 204 }) },
{ method: "POST" as const, path: "/api/v1/pair", handler: () => ({ body: pairBody }) },
{ method: "POST" as const, path: "/api/v1/cmd", handler: () => ({ body: { accepted: true } }) },
];
const overridePaths = overrides.map((route) => route.path);
return [...overrides, ...base.filter((route) => !overridePaths.includes(route.path))];
}
const devicesBody = [
{
id: "dev-1",
name: "Pixel",
kind: "phone",
scopes: ["read", "steer"],
lastActiveAt: Date.now() - 60_000,
revokedAt: null,
},
];
type FetchMock = ReturnType<typeof mockApi>;
/** Recorded `method path` requests (the mock keeps every call). */
function requests(fetchMock: FetchMock, method: string, path: string): { body: unknown }[] {
return fetchMock.mock.calls
.filter(([input, init]) => {
const raw = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
const url = new URL(raw, "http://localhost");
return url.pathname === path && (init?.method ?? "GET").toUpperCase() === method;
})
.map(([, init]) => {
const body = init?.body;
return { body: typeof body === "string" ? (JSON.parse(body) as unknown) : undefined };
});
}
function card(title: string): HTMLElement {
const heading = screen.getByText(title);
const root = heading.closest(".status-card");
if (!(root instanceof HTMLElement)) throw new Error(`card "${title}" not found`);
return root;
}
beforeEach(async () => {
await setLanguage("en");
@@ -105,7 +173,7 @@ afterEach(() => {
});
describe("StatusPage", () => {
it("renders server, health, resources, models and devices", async () => {
it("renders server, health, resources, models, tasks and devices", async () => {
mockApi(apiRoutes());
await renderPage(routes(), "/status", { paired: true });
@@ -119,24 +187,162 @@ describe("StatusPage", () => {
expect(await screen.findByText("claude-sonnet-4")).toBeInTheDocument();
expect(screen.getByText("200k")).toBeInTheDocument();
expect(screen.getByText("reasoning")).toBeInTheDocument();
// Devices come from a bare array, tasks from every workspace's sessions.
expect(await screen.findByText("Pixel")).toBeInTheDocument();
expect(await screen.findByText("pnpm build")).toBeInTheDocument();
expect(screen.getByText("Paired devices")).toBeInTheDocument();
});
it("revokes a device only after the confirmation dialog", async () => {
it("renders the daemon identity fields from /info and the /resources counts", async () => {
mockApi(apiRoutes());
await renderPage(routes(), "/status", { paired: true });
await screen.findByText("Server info");
await waitFor(() => expect(screen.getByText("daemon-1")).toBeInTheDocument());
const info = card("Server info");
// hostname and bind address both resolve to the daemon's hostname.
expect(within(info).getAllByText("pi2-box").length).toBeGreaterThan(0);
expect(within(info).getByText("Linux 6.1.0")).toBeInTheDocument();
expect(within(info).getByText("linux")).toBeInTheDocument();
expect(within(info).getByText("0.2.0")).toBeInTheDocument();
expect(within(info).getByText("machine-1")).toBeInTheDocument();
expect(within(info).getByText("2h 5m")).toBeInTheDocument();
expect(within(info).getByText("4321")).toBeInTheDocument();
// /resources: one instance, 512 MiB total RSS.
expect(within(info).getByText("512 MiB")).toBeInTheDocument();
expect(within(info).getByText("Instances")).toBeInTheDocument();
// No placeholder for fields the daemon really sends.
expect(within(info).queryByText("—")).not.toBeInTheDocument();
});
it("reports the WebSocket state from the shared connection store", async () => {
mockApi(apiRoutes());
await renderPage(routes(), "/status", { paired: true });
expect(await screen.findByTestId("ws-status")).toHaveTextContent("Disconnected");
act(() => useConnectionStore.getState().setWsState("open"));
await waitFor(() => expect(screen.getByTestId("ws-status")).toHaveTextContent("Connected"));
});
it("mints a pairing code for the connection card", async () => {
const fetchMock = mockApi(apiRoutes());
await renderPage(routes(), "/status", { paired: true });
expect(await screen.findByTestId("pairing-code")).toHaveTextContent("ABCD1234");
expect(screen.getByTestId("pairing-url")).toHaveTextContent(pairBody.url);
expect(screen.getByText(/Pairing code expires in \d+ minutes/)).toBeInTheDocument();
const calls = requests(fetchMock, "POST", "/api/v1/pair");
expect(calls).toHaveLength(1);
expect(calls[0]?.body).toEqual({ ttlSec: 600 });
});
it("offers a retry when minting a pairing code fails", async () => {
let attempts = 0;
mockApi(
apiRoutes([
{
method: "POST",
path: "/api/v1/pair",
handler: () => {
attempts += 1;
return attempts === 1
? { status: 500, body: { error: { code: "E_INTERNAL", message: "boom" } } }
: { body: pairBody };
},
},
]),
);
await renderPage(routes(), "/status", { paired: true });
const connection = card("Connection");
expect(await within(connection).findByText("Internal server error")).toBeInTheDocument();
expect(within(connection).queryByTestId("pairing-code")).not.toBeInTheDocument();
fireEvent.click(within(connection).getByRole("button", { name: "Retry" }));
expect(await within(connection).findByTestId("pairing-code")).toHaveTextContent("ABCD1234");
expect(attempts).toBe(2);
});
it("revokes a device with DELETE after the confirmation dialog", async () => {
const fetchMock = mockApi(apiRoutes());
await renderPage(routes(), "/status", { paired: true });
await screen.findByText("Pixel");
fireEvent.click(screen.getByRole("button", { name: "Revoke" }));
// Duplicate "Revoke" names would break strict locators: each row names its device.
fireEvent.click(screen.getByRole("button", { name: "Revoke Pixel" }));
const dialog = await screen.findByRole("dialog");
expect(within(dialog).getByText('Revoke access for "Pixel"? The phone\'s next request will fail.')).toBeInTheDocument();
expect(
within(dialog).getByText('Revoke access for "Pixel"? The phone\'s next request will fail.'),
).toBeInTheDocument();
fireEvent.click(within(dialog).getByRole("button", { name: "Revoke" }));
await waitFor(() => expect(screen.queryByText("Pixel")).not.toBeInTheDocument());
const commands = commandCalls(fetchMock);
expect(commands).toHaveLength(1);
expect(commands[0]).toEqual({ method: "device.revoke", params: { deviceId: "dev-1" } });
expect(requests(fetchMock, "DELETE", "/api/v1/devices/dev-1")).toHaveLength(1);
expect(commandCalls(fetchMock)).toHaveLength(0);
});
it("still lists devices when the daemon answers the legacy {devices} envelope", async () => {
mockApi(apiRoutes([{ path: "/api/v1/devices", handler: () => ({ body: { devices: devicesBody } }) }]));
await renderPage(routes(), "/status", { paired: true });
expect(await screen.findByText("Pixel")).toBeInTheDocument();
expect(screen.getByRole("button", { name: "Revoke Pixel" })).toBeInTheDocument();
});
it("shows an error with retry when the device list fails", async () => {
let attempts = 0;
mockApi(
apiRoutes([
{
path: "/api/v1/devices",
handler: () => {
attempts += 1;
return attempts === 1
? { status: 500, body: { error: { code: "E_INTERNAL", message: "boom" } } }
: { body: devicesBody };
},
},
]),
);
await renderPage(routes(), "/status", { paired: true });
const devices = card("Paired devices");
expect(await within(devices).findByText("Internal server error")).toBeInTheDocument();
fireEvent.click(within(devices).getByRole("button", { name: "Retry" }));
expect(await within(devices).findByText("Pixel")).toBeInTheDocument();
expect(attempts).toBe(2);
});
it("keeps the tasks empty state when no workspace reports a task", async () => {
mockApi(
apiRoutes([
{ path: SESSIONS_PATH, handler: () => ({ body: { sessions: [{ sessionId: "sess-1", tasks: [] }] } }) },
]),
);
await renderPage(routes(), "/status", { paired: true });
const tasks = card("Background tasks");
expect(await within(tasks).findByText("No background tasks")).toBeInTheDocument();
});
it("does not ask for sessions of workspaces without a live instance", async () => {
// `/sessions` relays through instances.ensure(), which would spawn a Neovim
// for every idle workspace — the status page must never trigger that.
const fetchMock = mockApi(
apiRoutes([
{
path: "/api/v1/workspaces",
handler: () => ({ body: workspacesBody.map((workspace) => ({ ...workspace, instance: null })) }),
},
]),
);
await renderPage(routes(), "/status", { paired: true });
const tasks = card("Background tasks");
expect(await within(tasks).findByText("No background tasks")).toBeInTheDocument();
expect(requests(fetchMock, "GET", "/api/v1/workspaces/ws-1/sessions")).toHaveLength(0);
});
});