repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by admin
extensions/loop/__e2e__/site.spec.ts
Rawimport { mkdtempSync, rmSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { expect, type Page, test } from "@playwright/test";
const built: typeof import("../site-test.ts") = await import(
new URL("../../../dist/loop/site-test.ts", import.meta.url).href
);
const { LoopController, LoopStore, startSite } = built;
type Fixture = {
controller: InstanceType<typeof LoopController>;
url: string;
close(): Promise<void>;
};
const fixtures: Fixture[] = [];
test.afterEach(async () => {
for (const fixture of fixtures.splice(0)) await fixture.close();
});
const gate = (name: string, script: string) => ({
name,
command: process.execPath,
args: ["-e", script],
});
async function failingRun(
goal = "Make the loop check task pass",
): Promise<Fixture> {
const root = mkdtempSync(join(tmpdir(), "loop-e2e-"));
const store = await LoopStore.open(join(root, "state.db"));
const controller = new LoopController(store);
controller.start(
{
goal,
gates: [
gate("tsc", ""),
gate(
"tests",
"console.log('2 failing'); console.error('✗ settle injects prompt'); process.exit(1)",
),
],
cwd: root,
},
{ minutes: 60, turns: 30, attempts: 10 },
);
controller.turn();
await controller.checkGates();
const site = await startSite(controller, { pollMs: 100 });
const fixture = {
controller,
url: site.url,
async close() {
await site.close();
store.close();
rmSync(root, { recursive: true, force: true });
},
};
fixtures.push(fixture);
return fixture;
}
function watchErrors(page: Page) {
const errors: string[] = [];
page.on("pageerror", (error) => errors.push(error.message));
page.on("console", (message) => {
if (message.type() === "error") errors.push(message.text());
});
return errors;
}
for (const colorScheme of ["dark", "light"] as const)
test(`renders the stacked run narrative live (${colorScheme})`, async ({
page,
}, info) => {
await page.emulateMedia({ colorScheme });
const errors = watchErrors(page);
const { controller, url } = await failingRun();
await page.goto(url);
await expect(page.locator("#conn")).toHaveText("● live");
await expect(page.locator("#status")).toHaveText("● ACTIVE");
await expect(page.locator("#goal")).toHaveText(
"Make the loop check task pass",
);
await expect(page.locator("#attempt")).toHaveText("attempt 1");
await expect(page.locator("#budgets")).toContainText("turns 1/30");
await expect(page.locator("#budgets")).toContainText("attempts 1/10");
await expect(page.locator("#budgets")).toContainText("left");
const tsc = page.locator('.gate[data-gate="tsc"]');
const tests = page.locator('.gate[data-gate="tests"]');
await expect(tsc).toHaveClass(/pass/u);
await expect(tsc.locator("details")).toHaveCount(0);
await expect(tests).toHaveClass(/fail/u);
await expect(tests.locator("details[open] summary")).toContainText(
"exit 1 · attempt 1",
);
await expect(tests.locator("pre .err")).toHaveText(
"✗ settle injects prompt\n",
);
await expect(tests.locator("pre")).toContainText("2 failing");
await expect(page.locator("#track .mark.turn")).toHaveCount(1);
await expect(page.locator("#track .mark.fail.latest")).toHaveCount(1);
await expect(page.locator("#feed .ev .k")).toHaveText([
"gate_result",
"start",
]);
await expect(page.locator("#history-title")).toHaveText(
"Run history · 1 run",
);
await info.attach(`active-${colorScheme}`, {
body: await page.screenshot({ fullPage: true }),
contentType: "image/png",
});
// SSE pushes the pause without a reload.
controller.interrupt("Escape pressed during baseline");
const banner = page.locator("#banner.pause");
await expect(page.locator("#status")).toHaveText("■ PAUSED");
await expect(banner).toContainText(
"Paused · Escape pressed during baseline",
);
await expect(banner).toContainText("time budget restarts on resume");
const id = controller.status()?.id.slice(0, 8);
await expect(banner.locator("button.resume")).toHaveText(
`⏵ copy /loop resume ${id}`,
);
await expect(page.locator("#feed .ev .k").first()).toHaveText("paused");
await info.attach(`paused-${colorScheme}`, {
body: await page.screenshot({ fullPage: true }),
contentType: "image/png",
});
// A new run that passes shows the success banner and grows the history.
controller.start(
{ goal: "Port gate runner", gates: [gate("tsc", "")], cwd: tmpdir() },
{ minutes: null, turns: null, attempts: null },
);
await controller.checkGates();
await expect(page.locator("#status")).toHaveText("● SUCCEEDED");
await expect(page.locator("#banner.ok")).toContainText(
"All gates exited 0 · run complete",
);
await expect(page.locator("#history-title")).toHaveText(
"Run history · 2 runs",
);
await page.locator("#history summary").click();
await expect(page.locator(".hrow .st-succeeded")).toHaveText("succeeded");
await expect(page.locator(".hrow .st-paused")).toHaveText("paused");
await info.attach(`succeeded-${colorScheme}`, {
body: await page.screenshot({ fullPage: true }),
contentType: "image/png",
});
expect(errors).toEqual([]);
});
test("shows advancing elapsed time and bounded output before a slow gate completes", async ({
page,
}, info) => {
const fixture = await failingRun();
const { controller, url } = fixture;
controller.interrupt("Prepare slow gate");
controller.start({
goal: "Observe slow gate",
gates: [
gate(
"slow",
"console.log('x'.repeat(10000));setTimeout(()=>console.error('still working'),25);setInterval(()=>{},1000)",
),
],
cwd: tmpdir(),
});
const pending = controller.checkGates();
try {
await page.goto(url);
const running = page.locator('.gate[data-gate="slow"]');
await expect(running).toHaveClass(/running/u);
await expect(running.locator(".live-output")).toContainText(
"still working",
);
await expect(running).toContainText("bounded tail");
expect(
(await running.locator(".live-output").textContent())!.length,
).toBeLessThanOrEqual(8192);
const first = await running.locator(".code").textContent();
await expect
.poll(() => running.locator(".code").textContent())
.not.toBe(first);
expect(controller.status()).toMatchObject({
status: "active",
pending: true,
});
await info.attach("running-output", {
body: await page.screenshot({ fullPage: true }),
contentType: "image/png",
});
controller.interrupt("User pause");
await expect(page.locator("#status")).toHaveText("■ PAUSED");
await expect(page.locator(".gate.running")).toHaveCount(0);
} finally {
controller.interrupt("Test cleanup");
await pending;
}
});
for (const colorScheme of ["dark", "light"] as const)
test(`renders untrusted goal Markdown as normal body content (${colorScheme})`, async ({
page,
}) => {
await page.emulateMedia({ colorScheme });
await page.setViewportSize({ width: 480, height: 800 });
const goal = [
"Ordinary paragraph with **emphasis**.",
"",
"## Actual heading",
"",
"- [x] done",
"- [ ] pending",
"",
"> quoted",
"",
"```ts",
"const x = '<safe>';",
"```",
"",
"| Gate | Result |",
"| --- | --- |",
"| tests | pass |",
"",
'[safe](https://example.com/ "title & " onclick=evil")',
"",
"[unsafe](javascript:alert%281%29)",
"",
"",
"",
'<svg onload="window.pwned=1"></svg><img src=x onerror="window.pwned=1">',
"",
"long".repeat(300),
].join("\n");
const requests: string[] = [];
page.on("request", (request) => requests.push(request.url()));
const { controller, url } = await failingRun(goal);
await page.goto(url);
const body = page.locator("#goal");
await expect(body.locator("p").first()).toHaveText(
"Ordinary paragraph with emphasis.",
);
await expect(body.locator("p").first()).toHaveCSS("font-weight", "400");
await expect(body.locator("h1")).toHaveCount(0);
await expect(body.locator("h2")).toHaveText("Actual heading");
await expect(body.locator("blockquote")).toContainText("quoted");
await expect(body.locator("pre code")).toHaveText("const x = '<safe>';\n");
await expect(body.locator("table td")).toHaveText(["tests", "pass"]);
await expect(body.locator('input[type="checkbox"]')).toHaveCount(2);
await expect(
body.locator("img, svg, script, [onclick], [onload], [onerror]"),
).toHaveCount(0);
await expect(body.locator("a")).toHaveCount(1);
await expect(body.locator("a")).toHaveAttribute(
"rel",
"noopener noreferrer",
);
expect(await page.evaluate(() => "pwned" in window)).toBe(false);
expect(requests.some((url) => url.includes("remote.example"))).toBe(false);
expect(
await body.evaluate((node) => node.scrollWidth <= node.clientWidth),
).toBe(true);
controller.interrupt("Markdown live pause");
await expect(page.locator("#status")).toHaveText("■ PAUSED");
await expect(body.locator("h2")).toHaveText("Actual heading");
});
test("rejects the page without its token", async ({ page }) => {
const { url } = await failingRun();
await page.goto(url.replace(/#.*$/u, ""));
await expect(page.locator("#conn")).toContainText("missing token");
await expect(page.locator("#run")).toBeHidden();
});