test: verify console history in production browser
This commit is contained in:
@@ -0,0 +1,220 @@
|
||||
import { extname, join, normalize } from "jsr:@std/path@1.1.4";
|
||||
|
||||
const port = Number(Deno.args[0]);
|
||||
const buildRoot = Deno.args[1];
|
||||
if (!Number.isInteger(port) || !buildRoot) {
|
||||
throw new Error("usage: console_history_fixture_server.ts <port> <build-root>");
|
||||
}
|
||||
|
||||
const workspaceId = "console-history-review";
|
||||
const runtimeId = "runtime-a";
|
||||
const workerId = "worker-a";
|
||||
const workerResourceKey = "W-900";
|
||||
let historyRequests = 0;
|
||||
|
||||
const json = (value: unknown, status = 200) =>
|
||||
new Response(JSON.stringify(value), {
|
||||
status,
|
||||
headers: { "content-type": "application/json; charset=utf-8" },
|
||||
});
|
||||
|
||||
const worker = {
|
||||
worker_id: workerId,
|
||||
runtime_id: runtimeId,
|
||||
resource_key: workerResourceKey,
|
||||
display_name: "Console Fixture",
|
||||
label: "Console Fixture",
|
||||
availability: "observed",
|
||||
state: "stopped",
|
||||
retention_state: "retained",
|
||||
host_id: "fixture-host",
|
||||
profile: "default",
|
||||
tags: [],
|
||||
diagnostics: [],
|
||||
capabilities: { can_stop: false, can_spawn_followup: false },
|
||||
workspace: {
|
||||
workspace_id: workspaceId,
|
||||
visibility: "workspace",
|
||||
identity: workspaceId,
|
||||
},
|
||||
implementation: { kind: "builtin", display_hint: "fixture" },
|
||||
workdir_attachments: [],
|
||||
};
|
||||
|
||||
function historyEntry(index: number, role: "user" | "assistant") {
|
||||
const user = role === "user";
|
||||
return {
|
||||
entry_id: `${role}-${index}`,
|
||||
timestamp: index,
|
||||
provenance: user ? "human_input" : "model_output",
|
||||
kind: "message",
|
||||
role,
|
||||
content: [{
|
||||
kind: "text",
|
||||
text: user
|
||||
? `question ${index}`
|
||||
: `done ${index}\ndetail ${index}.1\ndetail ${index}.2\ndetail ${index}.3`,
|
||||
}],
|
||||
};
|
||||
}
|
||||
|
||||
function historyPage(indices: number[], cursor: string | null) {
|
||||
return {
|
||||
availability: "page",
|
||||
page: {
|
||||
session_id: "session-a",
|
||||
lineage_id: "lineage-a",
|
||||
turns: indices.map((index) => ({
|
||||
turn_id: `user-${index}`,
|
||||
entries: [historyEntry(index, "user"), historyEntry(index, "assistant")],
|
||||
})),
|
||||
next_cursor: cursor,
|
||||
has_more: cursor !== null,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const mime: Record<string, string> = {
|
||||
".html": "text/html; charset=utf-8",
|
||||
".js": "text/javascript; charset=utf-8",
|
||||
".css": "text/css; charset=utf-8",
|
||||
".json": "application/json; charset=utf-8",
|
||||
".svg": "image/svg+xml",
|
||||
".png": "image/png",
|
||||
".woff2": "font/woff2",
|
||||
};
|
||||
|
||||
async function staticResponse(pathname: string): Promise<Response> {
|
||||
const relative = pathname === "/" ? "index.html" : pathname.replace(/^\/+/, "");
|
||||
const normalized = normalize(relative);
|
||||
if (normalized.startsWith("..")) return new Response("not found", { status: 404 });
|
||||
let filePath = join(buildRoot, normalized);
|
||||
try {
|
||||
const stat = await Deno.stat(filePath);
|
||||
if (stat.isDirectory) filePath = join(filePath, "index.html");
|
||||
return new Response(await Deno.readFile(filePath), {
|
||||
headers: { "content-type": mime[extname(filePath)] ?? "application/octet-stream" },
|
||||
});
|
||||
} catch {
|
||||
return new Response(await Deno.readFile(join(buildRoot, "index.html")), {
|
||||
headers: { "content-type": "text/html; charset=utf-8" },
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Deno.serve({ hostname: "127.0.0.1", port }, async (request) => {
|
||||
const url = new URL(request.url);
|
||||
if (url.pathname === "/health") return new Response("ok");
|
||||
if (url.pathname === "/fixture-state") return json({ history_requests: historyRequests });
|
||||
if (url.pathname === "/api/workspaces") {
|
||||
return json([{
|
||||
workspace_id: workspaceId,
|
||||
owner_account_id: "owner",
|
||||
display_name: "Console History Review",
|
||||
state: "active",
|
||||
created_at: "2026-01-01T00:00:00Z",
|
||||
updated_at: "2026-01-02T00:00:00Z",
|
||||
}]);
|
||||
}
|
||||
if (url.pathname === `/api/w/${workspaceId}/workspace`) {
|
||||
return json({
|
||||
workspace_id: workspaceId,
|
||||
display_name: "Console History Review",
|
||||
record_authority: "fixture",
|
||||
schema_version: 1,
|
||||
auth: {
|
||||
Passkey: {
|
||||
rp_id: "127.0.0.1",
|
||||
origin: `http://127.0.0.1:${port}`,
|
||||
public_base_url: `http://127.0.0.1:${port}`,
|
||||
cookie_name: "fixture",
|
||||
},
|
||||
},
|
||||
permissions: {
|
||||
manage_repositories: false,
|
||||
manage_secrets: false,
|
||||
manage_runtimes: false,
|
||||
delete_workspace: false,
|
||||
},
|
||||
extension_points: {
|
||||
store: "fixture",
|
||||
event_stream: { status: "ready", note: "fixture", diagnostics: [] },
|
||||
host_worker_bridge: { status: "ready", note: "fixture", diagnostics: [] },
|
||||
companion_console: { status: "ready", note: "fixture", diagnostics: [] },
|
||||
},
|
||||
});
|
||||
}
|
||||
if (url.pathname === `/api/w/${workspaceId}/repositories`) {
|
||||
return json({ workspace_id: workspaceId, items: [], source: "fixture", diagnostics: [] });
|
||||
}
|
||||
if (url.pathname === `/api/w/${workspaceId}/working-directories`) {
|
||||
return json({ workspace_id: workspaceId, items: [], diagnostics: [] });
|
||||
}
|
||||
if (
|
||||
url.pathname === `/api/w/${workspaceId}/workers/${workerResourceKey}` ||
|
||||
url.pathname === `/api/w/${workspaceId}/runtimes/${runtimeId}/workers/${workerId}`
|
||||
) {
|
||||
return json(worker);
|
||||
}
|
||||
if (
|
||||
url.pathname ===
|
||||
`/api/w/${workspaceId}/runtimes/${runtimeId}/workers/${workerId}/session`
|
||||
) {
|
||||
return json({
|
||||
availability: "retained_snapshot",
|
||||
identity: { session_id: "session-a", segment_id: "segment-a", entry_count: 0 },
|
||||
snapshot: {
|
||||
pending_submissions: {
|
||||
revision: 0,
|
||||
notification_count: 0,
|
||||
head_id: null,
|
||||
submissions: [],
|
||||
},
|
||||
entries: [],
|
||||
},
|
||||
});
|
||||
}
|
||||
if (
|
||||
url.pathname ===
|
||||
`/api/w/${workspaceId}/runtimes/${runtimeId}/workers/${workerId}/session/history`
|
||||
) {
|
||||
historyRequests += 1;
|
||||
switch (url.searchParams.get("cursor")) {
|
||||
case null:
|
||||
return json(historyPage([8, 9, 10, 11, 12], "older-8"));
|
||||
case "older-8":
|
||||
return json(historyPage([3, 4, 5, 6, 7], "older-3"));
|
||||
case "older-3":
|
||||
return json(historyPage([1, 2], null));
|
||||
default:
|
||||
return json({ availability: "unavailable", reason: "invalid_cursor", message: "invalid" });
|
||||
}
|
||||
}
|
||||
if (
|
||||
url.pathname === `/api/w/${workspaceId}/protocol/ws` &&
|
||||
request.headers.get("upgrade") === "websocket"
|
||||
) {
|
||||
const { socket, response } = Deno.upgradeWebSocket(request);
|
||||
socket.onmessage = (event) => {
|
||||
const frame = JSON.parse(String(event.data));
|
||||
if (frame?.message?.method !== "subscribe_events") return;
|
||||
const requestId = frame.message.params.request_id;
|
||||
socket.send(JSON.stringify({
|
||||
protocol_version: 1,
|
||||
frame: "response",
|
||||
message: {
|
||||
result: "subscribed",
|
||||
payload: {
|
||||
request_id: requestId,
|
||||
subscription_id: "fixture-workers",
|
||||
selector: frame.message.params.selector,
|
||||
snapshot_revision: 1,
|
||||
snapshot: { topic: "workers", data: { workers: [] } },
|
||||
},
|
||||
},
|
||||
}));
|
||||
};
|
||||
return response;
|
||||
}
|
||||
return await staticResponse(url.pathname);
|
||||
});
|
||||
Reference in New Issue
Block a user