Files
pi2d/web/tests/ws.instance-state.test.ts
yuez d5d205a7eb feat(web): explain a failed open, keep the dialog, and follow instance_state
Opening a project showed a toast on success and a bare code (`E_FORBIDDEN`) on
failure: no reason, no retry, and a workspace card that kept claiming "Instance
stopped" because `instance_state` frames were dropped by the socket layer.

- The dialog validates a non-absolute path inline, sends `alias` (the field the
  daemon persists), maps the daemon's status/code/message to a specific sentence
  (`~` expansions, missing directory, roots allowlist, permissions), and keeps
  itself open on a spawn failure with the daemon's reason plus a retry that
  re-sends only `instance.ensure`.
- `lib/ws` handles `instance_state` by invalidating `workspaces`/`health`
  (coalesced at 250 ms across sockets) so the grid reflects reality.
- A never-started workspace renders as "not started · opens on demand" instead
  of "stopped", and a degraded/stopped instance shows `instance.reason`.
2026-10-28 17:32:53 +08:00

147 lines
5.2 KiB
TypeScript

import { QueryClient, QueryObserver } from "@tanstack/react-query";
import { afterEach, describe, expect, it, vi } from "vitest";
import type { WorkspaceInfo } from "@/lib/protocol";
import { PiWsClient, type WebSocketLike, type WsFrame } from "@/lib/ws";
/** Same fake socket as `ws.test.ts`: no real transport anywhere in unit tests. */
class FakeSocket implements WebSocketLike {
readyState = 0;
sent: WsFrame[] = [];
closed = false;
onopen: ((event: unknown) => void) | null = null;
onmessage: ((event: { data: unknown }) => void) | null = null;
onclose: ((event: { code?: number; reason?: string }) => void) | null = null;
onerror: ((event: unknown) => void) | null = null;
send(data: string): void {
this.sent.push(JSON.parse(data) as WsFrame);
}
close(): void {
this.closed = true;
this.readyState = 3;
this.onclose?.({ code: 1000, reason: "closed" });
}
open(): void {
this.readyState = 1;
this.onopen?.({});
}
receive(frame: Record<string, unknown>): void {
this.onmessage?.({ data: JSON.stringify(frame) });
}
}
/** Short window so the coalescing assertions stay fast without fake timers. */
const COALESCE_MS = 20;
function connect(queryClient?: QueryClient) {
const sockets: FakeSocket[] = [];
const client = new PiWsClient({
url: "ws://127.0.0.1:7676/api/v1/ws",
token: "pkt_test",
heartbeatMs: 0,
instanceStateCoalesceMs: COALESCE_MS,
...(queryClient ? { queryClient } : {}),
socketFactory: () => {
const socket = new FakeSocket();
sockets.push(socket);
return socket;
},
});
client.connect();
const socket = sockets[0]!;
socket.open();
socket.receive({ v: 1, type: "hello_ack", payload: {} });
return { client, socket };
}
/** Two queries the workspace page actually runs, observed the way the page does. */
function observe(queryClient: QueryClient) {
const workspaces = vi.fn(async (): Promise<WorkspaceInfo[]> => []);
const health = vi.fn(async () => ({ ok: true, items: [] }));
const observers = [
new QueryObserver(queryClient, { queryKey: ["workspaces"], queryFn: workspaces }),
new QueryObserver(queryClient, { queryKey: ["health"], queryFn: health }),
];
const unsubscribes = observers.map((observer) => observer.subscribe(() => {}));
return {
workspaces,
health,
stop: () => {
for (const unsubscribe of unsubscribes) unsubscribe();
},
};
}
function newQueryClient() {
return new QueryClient({
defaultOptions: { queries: { retry: false, staleTime: Infinity, gcTime: Infinity } },
});
}
afterEach(() => {
vi.useRealTimers();
});
describe("instance_state frames", () => {
it("refetches the workspaces list (and health) instead of being dropped", async () => {
const queryClient = newQueryClient();
const observed = observe(queryClient);
await vi.waitFor(() => expect(observed.workspaces).toHaveBeenCalledTimes(1));
expect(observed.health).toHaveBeenCalledTimes(1);
const { client, socket } = connect(queryClient);
socket.receive({
v: 1,
type: "instance_state",
workspaceId: "ws-1",
payload: { status: "ready", sessions: [] },
});
// Invalidation, not a local patch: the row (and the health strip) refetch.
await vi.waitFor(() => expect(observed.workspaces).toHaveBeenCalledTimes(2));
await vi.waitFor(() => expect(observed.health).toHaveBeenCalledTimes(2));
client.close();
observed.stop();
});
it("coalesces a startup burst into a single refetch round", async () => {
const queryClient = newQueryClient();
const observed = observe(queryClient);
await vi.waitFor(() => expect(observed.workspaces).toHaveBeenCalledTimes(1));
const { client, socket } = connect(queryClient);
for (const status of ["starting", "ready", "degraded"]) {
socket.receive({ v: 1, type: "instance_state", workspaceId: "ws-1", payload: { status } });
}
await vi.waitFor(() => expect(observed.workspaces).toHaveBeenCalledTimes(2));
// A burst must not fan out into one refetch per frame.
await new Promise((resolve) => setTimeout(resolve, COALESCE_MS * 3));
expect(observed.workspaces).toHaveBeenCalledTimes(2);
expect(observed.health).toHaveBeenCalledTimes(2);
client.close();
observed.stop();
});
it("stays tolerant without a query client and keeps ignoring unknown frames", () => {
const { client, socket } = connect();
expect(client.getState()).toBe("open");
for (const type of ["instance_state", "mystery_frame", "presence", "pong"]) {
expect(() => socket.receive({ v: 1, type, payload: { status: "ready" } })).not.toThrow();
}
expect(client.getState()).toBe("open");
// Still usable afterwards: a second connection state change is still reported.
socket.receive({ v: 1, type: "error", payload: { code: "E_INTERNAL", message: "boom" } });
expect(client.getState()).toBe("open");
client.close();
});
});