- collector, classify, prompt-parser, server (TypeScript) - web panel (app.js, index.html, style.css) - smoke and load-test scripts - DESIGN.md and README.md
162 lines
6.8 KiB
TypeScript
162 lines
6.8 KiB
TypeScript
/**
|
|
* load-test.ts — loads index.ts through jiti (the exact loader pi uses) with a
|
|
* mock ExtensionAPI, fires fake pi events, and verifies the whole wiring:
|
|
* extension factory → TraceStore → web server → REST API.
|
|
*
|
|
* Run: node --experimental-strip-types load-test.ts
|
|
*/
|
|
import { createJiti } from "jiti";
|
|
|
|
const jiti = createJiti(import.meta.url, { interopDefault: true });
|
|
|
|
function assert(cond: unknown, msg: string): void {
|
|
if (!cond) {
|
|
console.error("FAIL:", msg);
|
|
process.exitCode = 1;
|
|
} else {
|
|
console.log("ok:", msg);
|
|
}
|
|
}
|
|
|
|
type Handler = (event: unknown, ctx: unknown) => unknown;
|
|
|
|
const handlers = new Map<string, Handler[]>();
|
|
const commands = new Map<string, { description?: string; handler: (args: string, ctx: unknown) => Promise<void> }>();
|
|
|
|
const fakePi = {
|
|
on(type: string, handler: Handler) {
|
|
const list = handlers.get(type) ?? [];
|
|
list.push(handler);
|
|
handlers.set(type, list);
|
|
return () => undefined;
|
|
},
|
|
registerCommand(name: string, options: unknown) {
|
|
commands.set(name, options as { description?: string; handler: (args: string, ctx: unknown) => Promise<void> });
|
|
},
|
|
getAllTools: () => [
|
|
{ name: "read", exposure: "direct" },
|
|
{ name: "bash", exposure: "direct" },
|
|
],
|
|
getActiveTools: () => ["read", "bash"],
|
|
getMcpServers: () => [{ name: "demo-mcp" }],
|
|
};
|
|
|
|
const fakeSm = {
|
|
getSessionId: () => "sess-test",
|
|
getSessionFile: () => "/tmp/sessions/sess-test.jsonl",
|
|
getSessionName: () => "load-test",
|
|
getCwd: () => "/home/wonder/pi-lens",
|
|
getEntries: () => [{ id: "a" }, { id: "b" }],
|
|
getBranch: () => [{ id: "a" }],
|
|
};
|
|
|
|
const fakeCtx = {
|
|
ui: {
|
|
setStatus: () => undefined,
|
|
notify: () => undefined,
|
|
},
|
|
mode: "tui",
|
|
hasUI: true,
|
|
cwd: "/home/wonder/pi-lens",
|
|
sessionManager: fakeSm,
|
|
model: { id: "test-model", providerId: "test-provider" },
|
|
thinkingLevel: "medium",
|
|
getContextUsage: () => ({ tokens: 1200, contextWindow: 200000, percent: 0.6 }),
|
|
getSystemPrompt: () =>
|
|
"You are pi.\n<tools>\n- read: Read a file.\n</tools>\n<rules>\n- Be concise\n</rules>\n<cwd>\n/home/wonder/pi-lens\n</cwd>",
|
|
};
|
|
|
|
const fakeOptions = {
|
|
selectedTools: ["read"],
|
|
toolSnippets: { read: "Read a file." },
|
|
toolGuidelines: {},
|
|
promptGuidelines: [],
|
|
appendSystemPrompt: "",
|
|
sections: {},
|
|
cwd: "/home/wonder/pi-lens",
|
|
contextFiles: [],
|
|
skills: [
|
|
{
|
|
name: "demo-skill",
|
|
description: "Demo.",
|
|
filePath: "/tmp/skills/demo-skill/SKILL.md",
|
|
baseDir: "/tmp/skills/demo-skill",
|
|
disableModelInvocation: false,
|
|
},
|
|
],
|
|
};
|
|
|
|
async function main() {
|
|
const mod = (await jiti.import(process.env.LENS_INDEX ?? "./index.ts")) as { default: (pi: unknown) => void };
|
|
assert(typeof mod.default === "function", "index.ts loaded through jiti, default export is a factory");
|
|
|
|
mod.default(fakePi);
|
|
assert(commands.has("lens"), "/lens command registered");
|
|
assert(handlers.has("session_start") && handlers.has("before_agent_start") && handlers.has("tool_call"), "event handlers registered");
|
|
|
|
await handlers.get("session_start");
|
|
const status: string[] = [];
|
|
(fakeCtx.ui as { setStatus: (k: string, v?: string) => void }).setStatus = (k, v) => status.push(k + "=" + v);
|
|
assert(true, "session_start handler ran without throwing");
|
|
|
|
await handlers.get("before_agent_start");
|
|
|
|
handlers.get("tool_call");
|
|
handlers.get("tool_result");
|
|
|
|
// find the server URL from the store via the running server's global
|
|
const g = globalThis as Record<string, unknown>;
|
|
const lens = g.__piLens as { server?: { url: string } } | undefined;
|
|
assert(lens?.server?.url, "server started: " + (lens?.server?.url ?? "none"));
|
|
const base = lens!.server!.url;
|
|
|
|
const state = (await (await fetch(base + "api/state")).json()) as { totals: { toolCalls: number; promptBuilds: number }; skills: Array<{ name: string }> };
|
|
assert(state.totals.promptBuilds === 1, "prompt build captured via before_agent_start");
|
|
assert(state.totals.toolCalls === 1, "tool call captured");
|
|
assert(state.skills.some((s) => s.name === "demo-skill"), "skill ledger seeded from systemPromptOptions");
|
|
|
|
const skills = (await (await fetch(base + "api/skills")).json()) as { spans: Array<{ trigger: string; status: string }> };
|
|
assert(skills.spans.some((s) => s.trigger === "model" && s.status === "loaded"), "model skill span recorded");
|
|
|
|
const commandCtx = { ui: fakeCtx.ui, hasUI: true };
|
|
await commands.get("lens")!.handler("", commandCtx);
|
|
assert(true, "/lens command handler ran");
|
|
|
|
// ── duplicate guard: one message must be logged exactly once ─────────────
|
|
const msg = { role: "assistant", content: [{ type: "text", text: "hello world" }] };
|
|
handlers.get("message_end");
|
|
handlers.get("agent_end");
|
|
const tl1 = (await (await fetch(base + "api/timeline?limit=200")).json()) as { events: Array<{ type: string; label: string; seq: number }> };
|
|
const msgRows = tl1.events.filter((e) => e.type === "message_end" && e.label.includes("hello world"));
|
|
assert(msgRows.length === 1, "assistant message appears exactly once (message_end + agent_end do not double-log)");
|
|
|
|
// pagination cursor must be seq-based, never repeating an event
|
|
const page = (await (await fetch(base + "api/timeline?before=" + msgRows[0].seq + "&limit=50")).json()) as { events: Array<{ seq: number }> };
|
|
assert(!page.events.some((e) => e.seq === msgRows[0].seq), "timeline page before cursor excludes the cursor event");
|
|
|
|
// ── session isolation: switching sessions focuses the timeline on the new one ──
|
|
fakeSm.getSessionId = () => "sess-two";
|
|
await handlers.get("session_start");
|
|
const s2 = (await (await fetch(base + "api/state")).json()) as { sessionId: string };
|
|
assert(s2.sessionId === "sess-two", "state follows the switched session id");
|
|
const tl2 = (await (await fetch(base + "api/timeline?limit=50")).json()) as { events: Array<{ type: string }> };
|
|
assert(
|
|
!tl2.events.some((e) => e.type === "tool_call" || e.type === "prompt_build"),
|
|
"new session's timeline carries no events from the previous session",
|
|
);
|
|
const builds2 = (await (await fetch(base + "api/prompt-builds")).json()) as unknown[];
|
|
assert(builds2.length === 0, "prompt builds reset for the new session");
|
|
|
|
await handlers.get("session_shutdown");
|
|
console.log(process.exitCode ? "\nLOAD TEST FAILED" : "\nLOAD TEST PASSED");
|
|
process.exit(process.exitCode ?? 0);
|
|
}
|
|
|
|
main().catch((err) => {
|
|
console.error("LOAD TEST ERROR:", err);
|
|
process.exit(1);
|
|
});
|