- workspaces list + new-workspace flow (add -> ensure -> navigate), server context bar - connect: manual/pair-code redeem, QR scan with paste fallback, paired summary, disconnect - status: info/health/resources/models/devices/connection cards with 3s visible-only polling - chat: 3/2/1-column layout, session tree, virtualized stream, tool/thinking/todo/batch blocks, composer with queue chips and abort, subagents/todo/tasks panel, attention + diff review modals, history pagination merged with live entries - canonical reducer reused via @protocol alias (no duplicated types) - 63 vitest tests, typecheck + build green, zh/en parity at 411 keys
75 lines
2.6 KiB
TypeScript
75 lines
2.6 KiB
TypeScript
import { vi } from "vitest";
|
|
|
|
export interface MockRequest {
|
|
url: URL;
|
|
method: string;
|
|
body: unknown;
|
|
}
|
|
|
|
export interface MockReply {
|
|
status?: number;
|
|
body?: unknown;
|
|
}
|
|
|
|
export interface MockRoute {
|
|
method?: "GET" | "POST" | "DELETE" | "PUT";
|
|
path: string | RegExp;
|
|
handler: (request: MockRequest) => MockReply | Promise<MockReply>;
|
|
}
|
|
|
|
function matches(route: MockRoute, method: string, pathname: string): boolean {
|
|
if (route.method && route.method !== method) return false;
|
|
return typeof route.path === "string" ? route.path === pathname : route.path.test(pathname);
|
|
}
|
|
|
|
/**
|
|
* MSW-style fetch fake: a small route table instead of per-URL stubs.
|
|
* Unhandled requests fail loudly so a page cannot silently hit the network.
|
|
*/
|
|
export function mockApi(routes: MockRoute[]) {
|
|
const fetchMock = vi.fn(async (input: RequestInfo | URL, init?: RequestInit) => {
|
|
const raw =
|
|
typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
|
|
const url = new URL(raw, "http://localhost");
|
|
const method = (init?.method ?? "GET").toUpperCase();
|
|
let body: unknown;
|
|
if (typeof init?.body === "string") {
|
|
try {
|
|
body = JSON.parse(init.body);
|
|
} catch {
|
|
body = init.body;
|
|
}
|
|
}
|
|
|
|
const route = routes.find((candidate) => matches(candidate, method, url.pathname));
|
|
if (!route) throw new Error(`Unhandled request: ${method} ${url.pathname}`);
|
|
const reply = await route.handler({ url, method, body });
|
|
const status = reply.status ?? 200;
|
|
return new Response(status === 204 ? null : JSON.stringify(reply.body ?? {}), {
|
|
status,
|
|
headers: { "content-type": "application/json" },
|
|
});
|
|
});
|
|
vi.stubGlobal("fetch", fetchMock);
|
|
return fetchMock;
|
|
}
|
|
|
|
export interface CommandCall {
|
|
method: string;
|
|
params?: Record<string, unknown>;
|
|
}
|
|
|
|
/** Parse the recorded POST /api/v1/cmd bodies. */
|
|
export function commandCalls(fetchMock: ReturnType<typeof mockApi>): CommandCall[] {
|
|
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 === "/api/v1/cmd" && (init?.method ?? "GET").toUpperCase() === "POST";
|
|
})
|
|
.map(([, init]) => {
|
|
const parsed = JSON.parse(String(init?.body ?? "{}")) as CommandCall;
|
|
return parsed;
|
|
});
|
|
}
|