repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by admin
extensions/loop/__tests__/site.test.ts
Rawimport { mkdtempSync, rmSync } from "node:fs";
import { request } from "node:http";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, describe, expect, it } from "vitest";
import { type Site, startSite } from "../site.ts";
import { LoopController } from "../src/controller.ts";
import { LoopStore } from "../src/store.ts";
let root: string | undefined;
let store: LoopStore | undefined;
let site: Site | undefined;
afterEach(async () => {
await site?.close();
store?.close();
if (root) rmSync(root, { recursive: true, force: true });
site = undefined;
store = undefined;
root = undefined;
});
const node = (name: string, script: string) => ({
name,
command: process.execPath,
args: ["-e", script],
});
async function failingRun(goal = "Make tests pass") {
root = mkdtempSync(join(tmpdir(), "loop-site-"));
store = await LoopStore.open(join(root, "state.db"));
const controller = new LoopController(store);
controller.start(
{
goal,
gates: [
node("types", "console.log('fine')"),
node(
"tests",
"console.log('2 failing'); console.error('✗ settle'); process.exit(1)",
),
],
cwd: root,
},
{ minutes: null, turns: 30, attempts: 10 },
);
await controller.checkGates();
site = await startSite(controller, { pollMs: 50 });
const url = new URL(site.url);
return { controller, origin: url.origin, token: url.hash.slice(1) };
}
function raw(
origin: string,
path: string,
headers: Record<string, string>,
method = "GET",
) {
return new Promise<number>((resolve, reject) => {
const req = request(`${origin}${path}`, { method, headers }, (res) => {
res.resume();
resolve(res.statusCode ?? 0);
});
req.on("error", reject);
req.end();
});
}
async function* sse(origin: string, token: string, signal: AbortSignal) {
const res = await fetch(`${origin}/events?token=${token}`, { signal });
const reader = res.body!.pipeThrough(new TextDecoderStream()).getReader();
let buffer = "";
for (;;) {
const { value, done } = await reader.read();
if (done) return;
buffer += value;
for (
let end = buffer.indexOf("\n\n");
end >= 0;
end = buffer.indexOf("\n\n")
) {
const frame = buffer.slice(0, end);
buffer = buffer.slice(end + 2);
const event = /^event: (.*)$/mu.exec(frame)?.[1];
const data = /^data: (.*)$/mu.exec(frame)?.[1];
if (event === "snapshot" && data) yield JSON.parse(data);
}
}
}
describe("loop site", () => {
it("adds safe Markdown to API and live snapshots without changing stored goal or history", async () => {
const goal = "**Fix** tests\n\n- [ ] pass\n\n<img src=x onerror=alert(1)>";
const { controller, origin, token } = await failingRun(goal);
const snapshot = await (
await fetch(`${origin}/api/snapshot?token=${token}`)
).json();
expect(snapshot.run.goal).toBe(goal);
expect(snapshot.run.goalHtml).toContain("<strong>Fix</strong> tests");
expect(snapshot.run.goalHtml).toContain("<img");
expect(snapshot.run.goalHtml).not.toContain("<img");
expect(snapshot.history[0].goal).toBe("**Fix** tests");
expect(store?.getRun(snapshot.run.id)?.goal).toBe(goal);
const abort = new AbortController();
const stream = sse(
origin,
token,
AbortSignal.any([abort.signal, AbortSignal.timeout(3000)]),
);
try {
expect((await stream.next()).value.run.goalHtml).toBe(
snapshot.run.goalHtml,
);
controller.interrupt("Pause Markdown goal");
let next = (await stream.next()).value;
for (let n = 0; n < 5 && next.run.status !== "paused"; n++)
next = (await stream.next()).value;
expect(next.run).toMatchObject({
goal,
goalHtml: snapshot.run.goalHtml,
status: "paused",
});
} finally {
abort.abort();
}
});
it("pushes an external restore while preserving the last activity timestamp", async () => {
root = mkdtempSync(join(tmpdir(), "loop-site-restore-"));
store = await LoopStore.open(join(root, "state.db"));
const original = new LoopController(store, () => 1000);
const run = original.start({
goal: "Interrupted",
gates: [node("tests", "")],
cwd: root,
});
await original.release();
store.saveRun(run); // Crash snapshot: active, with no live owner.
site = await startSite(new LoopController(store), { pollMs: 20 });
const url = new URL(site.url);
const abort = new AbortController();
const stream = sse(
url.origin,
url.hash.slice(1),
AbortSignal.any([abort.signal, AbortSignal.timeout(3000)]),
);
const other = await LoopStore.open(store.path);
const restored = new LoopController(other, () => 3000);
try {
expect((await stream.next()).value.run.status).toBe("active");
const before = store.version();
restored.restore(other.getRun(run.id)!);
expect(store.version()).not.toBe(before);
expect(store.getRun(run.id)?.updatedAt).toBe(1000);
// SSE may already contain the initial poll's duplicate snapshot.
let next = (await stream.next()).value;
for (let n = 0; n < 5 && next.run.status !== "paused"; n++)
next = (await stream.next()).value;
expect(next.run).toMatchObject({
id: run.id,
status: "paused",
updatedAt: 1000,
});
expect(other.events(run.id).at(-1)?.kind).toBe("restored");
} finally {
abort.abort();
await restored.release();
other.close();
}
});
it("picks up runs written by another process on the next poll", async () => {
const { origin, token } = await failingRun();
const abort = new AbortController();
const stream = sse(origin, token, abort.signal);
const other = await LoopStore.open(join(root ?? "", "state.db"));
try {
expect((await stream.next()).value.history).toHaveLength(1);
new LoopController(other).start(
{ goal: "Elsewhere", gates: [node("ok", "")], cwd: tmpdir() },
{ minutes: null, turns: null, attempts: null },
);
const next = (await stream.next()).value;
expect(next.history.map((run: { goal: string }) => run.goal)).toContain(
"Elsewhere",
);
} finally {
abort.abort();
other.close();
}
});
it("serves assets publicly and state only with the token", async () => {
const { origin, token } = await failingRun();
const page = await fetch(`${origin}/`);
expect(page.status).toBe(200);
expect(page.headers.get("content-security-policy")).toContain(
"script-src 'self'",
);
expect(await page.text()).toContain('src="/app.js"');
expect((await fetch(`${origin}/app.js`)).status).toBe(200);
expect((await fetch(`${origin}/api/snapshot`)).status).toBe(401);
expect((await fetch(`${origin}/api/snapshot?token=wrong`)).status).toBe(
401,
);
expect(
await raw(origin, `/api/snapshot?token=${token}`, {
Host: "evil.example",
}),
).toBe(403);
expect(
await raw(origin, `/api/snapshot?token=${token}`, {
Origin: "http://evil.example",
}),
).toBe(403);
expect(await raw(origin, `/api/snapshot?token=${token}`, {}, "POST")).toBe(
405,
);
});
it("reports the run, gate results with the failing drawer, events, and history", async () => {
const { origin, token } = await failingRun();
const snapshot = await (
await fetch(`${origin}/api/snapshot?token=${token}`)
).json();
expect(snapshot.run).toMatchObject({
goal: "Make tests pass",
status: "active",
live: true,
attempts: 1,
budgets: { minutes: null, turns: 30, attempts: 10 },
gates: [{ label: "types" }, { label: "tests" }],
lastPass: { attempt: 1, failedIndex: 1 },
});
const [passed, failed] = snapshot.run.lastPass.results;
expect(passed).toMatchObject({ exitCode: 0, stdout: "", stderr: "" });
expect(failed).toMatchObject({
exitCode: 1,
stdout: "2 failing\n",
stderr: "✗ settle\n",
});
expect(snapshot.timeline).toEqual([
expect.objectContaining({
kind: "fail",
n: 1,
label: "tests",
exitCode: 1,
}),
]);
expect(
snapshot.events.map((event: { kind: string }) => event.kind),
).toEqual(["gate_result", "start"]);
expect(snapshot.history).toHaveLength(1);
});
it("pushes a new snapshot over SSE when the run changes", async () => {
const { controller, origin, token } = await failingRun();
const abort = new AbortController();
const stream = sse(origin, token, abort.signal);
try {
expect((await stream.next()).value.run.status).toBe("active");
controller.interrupt("User pause");
const next = (await stream.next()).value;
expect(next.run).toMatchObject({
status: "paused",
reason: "User pause",
});
expect(next.events[0]).toMatchObject({
kind: "paused",
summary: "User pause",
});
} finally {
abort.abort();
}
});
});