repositories / pi-ext
pi-ext
bugabingas pi extensions
owned by admin
extensions/mockup/__e2e__/mockup.spec.ts
Rawimport { readFileSync } from "node:fs";
import { expect, test } from "@playwright/test";
import type { MockupResult, MockupServer } from "../mockup.ts";
const mockup: typeof import("../mockup.ts") = await import(
new URL("../../../dist/mockup/mockup.ts", import.meta.url).href
);
const { startMockupServer } = mockup;
let server: MockupServer | undefined;
const interactiveFragment = readFileSync(
new URL("../guidance/mockup-html/SKILL.md", import.meta.url),
"utf8",
).match(/```html\n([\s\S]*?)```/)?.[1];
if (!interactiveFragment)
throw new Error("Missing interactive mockup skill example");
test.afterEach(() => {
if (!server) return;
server.result.catch(() => undefined);
server.close("e2e cleanup");
server = undefined;
});
test("iterates in one page with live Pi progress, stop, and optional selection", async ({
page,
}) => {
let aborts = 0;
server = await startMockupServer(
{
title: "Layout review",
question: "Which layout works?",
options: [
{
id: "dense",
label: "Dense",
description: "Compact layout",
html: `${interactiveFragment}<div style="width:1000px;height:120px">wide content</div><script type="application/json">{"count":0}</script>`,
},
{
id: "calm",
label: "Calm",
html: interactiveFragment,
},
],
},
{
openBrowser: false,
abortAgent: () => {
aborts++;
return true;
},
},
);
await page.goto(server.url);
const stableUrl = page.url();
await expect(
page.getByRole("heading", { name: "Layout review" }),
).toBeVisible();
await expect(page.getByText("Which layout works?")).toBeVisible();
await expect(page.locator(".option-card")).toHaveCount(2);
const denseCounter = page.locator('[data-fragment="dense"]');
await expect(
denseCounter.locator('script[type="application/json"]'),
).toHaveJSProperty("textContent", '{"count":0}');
const output = denseCounter.locator("output");
await expect(output).toHaveText("0");
await denseCounter.getByRole("button", { name: "Increment" }).click();
await expect(output).toHaveText("1");
await expect(page.locator('[data-fragment="calm"] output')).toHaveText("0");
expect(
await page.evaluate(() =>
[...document.scripts].some((script) =>
Object.hasOwn(script, "mockupRoot"),
),
),
).toBe(false);
await expect
.poll(() =>
denseCounter.locator(".counter").evaluate((element) => ({
shadow: element.getRootNode() instanceof ShadowRoot,
reviewFrameControls:
document.querySelectorAll("#accept, #iterate").length,
})),
)
.toEqual({ shadow: true, reviewFrameControls: 2 });
await page.setViewportSize({ width: 360, height: 800 });
await page.emulateMedia({ colorScheme: "dark" });
await page.locator('[data-select-option="dense"]').check();
await page.locator("#global-notes").fill("keep dense");
const fragment = page.locator('[data-fragment="dense"]');
const measureGeometry = () =>
fragment.evaluate((element) => {
const fragmentRect = element.getBoundingClientRect();
const bubbleRect = element
.querySelector(".pin-bubble")
?.getBoundingClientRect();
const pinRects = [...element.querySelectorAll(".pin")].map((pin) =>
pin.getBoundingClientRect(),
);
const pinCenters = pinRects.map(
(pinRect) => (pinRect.left + pinRect.right) / 2 - fragmentRect.left,
);
return {
clientWidth: element.clientWidth,
scrollWidth: element.scrollWidth,
scrollLeft: element.scrollLeft,
bubbleWidth: bubbleRect?.width,
bubbleLeft: bubbleRect && bubbleRect.left - fragmentRect.left,
bubbleWithin:
!!bubbleRect &&
bubbleRect.left >= fragmentRect.left &&
bubbleRect.right <= fragmentRect.right,
pinCenters,
bubblePinGap:
bubbleRect && pinRects.length
? {
x: Math.max(
0,
bubbleRect.left - pinRects.at(-1)!.right,
pinRects.at(-1)!.left - bubbleRect.right,
),
y: Math.max(
0,
bubbleRect.top - pinRects.at(-1)!.bottom,
pinRects.at(-1)!.top - bubbleRect.bottom,
),
}
: undefined,
pinWithin:
pinRects.length > 0 &&
pinRects.every(
(pinRect) =>
pinRect.left >= fragmentRect.left &&
pinRect.right <= fragmentRect.right,
),
};
});
const expectPinAt = async (x: number) => {
await expect
.poll(async () => {
const centers = (await measureGeometry()).pinCenters;
return centers.length ? Math.abs(centers.at(-1)! - x) : Infinity;
})
.toBeLessThanOrEqual(14);
};
const expectBubbleNearPin = async () => {
await expect
.poll(async () => {
const gap = (await measureGeometry()).bubblePinGap;
return {
x: gap?.x ?? Infinity,
y: gap?.y ?? Infinity,
near: (gap?.x ?? Infinity) <= 14 && (gap?.y ?? Infinity) <= 14,
};
})
.toMatchObject({ near: true });
};
const expectContained = async (contentWidth: number) => {
await expect
.poll(async () => {
const geometry = await measureGeometry();
return {
...geometry,
scrollMatchesContent: geometry.scrollWidth === contentWidth,
};
})
.toMatchObject({
scrollMatchesContent: true,
bubbleWithin: true,
pinWithin: true,
});
};
const narrowBox = await fragment.boundingBox();
expect(narrowBox).not.toBeNull();
const contentWidth = await fragment.evaluate(
(element) => element.scrollWidth,
);
expect(contentWidth).toBeGreaterThan(narrowBox?.width ?? 20);
const narrowMidpoint = (narrowBox?.width ?? 20) / 2;
const pin = page.locator('[data-pin-option="dense"]');
await pin.click();
await fragment.click({ position: { x: narrowMidpoint, y: 40 } });
await page.getByLabel("Pin note").fill("narrow-screen note");
await expectPinAt(narrowMidpoint);
await expectContained(contentWidth);
await expectBubbleNearPin();
await page.locator("[data-delete]").click();
await fragment.evaluate((element) => {
element.scrollLeft = 240;
});
const scrollOrigin = await fragment.evaluate((element) => element.scrollLeft);
expect(scrollOrigin).toBeGreaterThan(0);
await pin.click();
await fragment.click({ position: { x: narrowMidpoint, y: 40 } });
await page.getByLabel("Pin note").fill("scrolled note");
await expectPinAt(narrowMidpoint);
await expectContained(contentWidth);
await expectBubbleNearPin();
await fragment.evaluate((element) => {
element.scrollLeft = 280;
});
await expect
.poll(() => fragment.evaluate((element) => element.scrollLeft))
.toBe(280);
await expectContained(contentWidth);
await expectBubbleNearPin();
await page.locator("[data-delete]").click();
await fragment.evaluate((element) => {
element.scrollLeft = 0;
});
await page.setViewportSize({ width: 1280, height: 800 });
await page.locator('[data-pin-option="dense"]').click();
const desktopBox = await fragment.boundingBox();
expect(desktopBox).not.toBeNull();
await fragment.click({
position: { x: (desktopBox?.width ?? 20) / 2, y: 40 },
});
await page.getByLabel("Pin note").fill("tighten spacing");
await expectContained(contentWidth);
await expectBubbleNearPin();
await page.setViewportSize({ width: 360, height: 800 });
await expectContained(contentWidth);
await expectBubbleNearPin();
const beforeCloseGeometry = await measureGeometry();
await page.getByRole("button", { name: "Close" }).click();
await fragment.locator(".pin").click();
await expectContained(contentWidth);
await expectBubbleNearPin();
expect(await measureGeometry()).toEqual(beforeCloseGeometry);
for (const x of [10, (narrowBox?.width ?? 20) - 10]) {
await page.locator('[data-pin-option="dense"]').click();
await fragment.click({ position: { x, y: 40 } });
await expectContained(contentWidth);
await expectBubbleNearPin();
await page.locator("[data-delete]").click();
}
await page.setViewportSize({ width: 360, height: 800 });
await page.locator('[data-select-option="calm"]').check();
const calmFragment = page.locator('[data-fragment="calm"]');
const measureCalm = () =>
calmFragment.evaluate((element) => {
const fragmentRect = element.getBoundingClientRect();
const bubbleRect = element
.querySelector(".pin-bubble")
?.getBoundingClientRect();
const pinRect = element.querySelector(".pin")?.getBoundingClientRect();
const gapX =
bubbleRect && pinRect
? Math.max(
0,
bubbleRect.left - pinRect.right,
pinRect.left - bubbleRect.right,
)
: Infinity;
const gapY =
bubbleRect && pinRect
? Math.max(
0,
bubbleRect.top - pinRect.bottom,
pinRect.top - bubbleRect.bottom,
)
: Infinity;
return {
clientWidth: element.clientWidth,
scrollWidth: element.scrollWidth,
scrollLeft: element.scrollLeft,
bubbleWithin:
!!bubbleRect &&
bubbleRect.left >= fragmentRect.left &&
bubbleRect.right <= fragmentRect.right,
pinWithin:
!!pinRect &&
pinRect.left >= fragmentRect.left &&
pinRect.right <= fragmentRect.right,
bubbleNearPin: gapX <= 14 && gapY <= 14,
};
});
const expectCalmPinVisible = async (contentWidth: number) => {
await expect
.poll(async () => {
const geometry = await measureCalm();
return {
noBubbleOverflow: geometry.scrollWidth === contentWidth,
pinWithin: geometry.pinWithin,
};
})
.toMatchObject({ noBubbleOverflow: true, pinWithin: true });
};
const expectCalmPlacement = async (contentWidth: number) => {
await expect
.poll(async () => {
const geometry = await measureCalm();
return {
...geometry,
noBubbleOverflow: geometry.scrollWidth === contentWidth,
};
})
.toMatchObject({
noBubbleOverflow: true,
bubbleWithin: true,
pinWithin: true,
bubbleNearPin: true,
});
};
const calmMobileWidth = await calmFragment.evaluate(
(element) => element.scrollWidth,
);
const narrowCalmBox = await calmFragment.boundingBox();
expect(narrowCalmBox).not.toBeNull();
expect(calmMobileWidth).toBe(narrowCalmBox?.width);
await page.locator('[data-pin-option="calm"]').click();
await calmFragment.click({
position: { x: (narrowCalmBox?.width ?? 20) / 2, y: 40 },
});
const calmNote = page.getByLabel("Pin note");
await expect(calmNote).toBeVisible();
await calmNote.fill("normal-width option note");
await expectCalmPlacement(calmMobileWidth);
await page.getByRole("button", { name: "Close" }).click();
await calmFragment.locator(".pin").click();
await expect(calmNote).toHaveValue("normal-width option note");
await expectCalmPlacement(calmMobileWidth);
await calmFragment.locator("[data-delete]").click();
await page.setViewportSize({ width: 1280, height: 800 });
const calmDesktopWidth = await calmFragment.evaluate(
(element) => element.scrollWidth,
);
const desktopCalmBox = await calmFragment.boundingBox();
expect(desktopCalmBox).not.toBeNull();
expect(calmDesktopWidth).toBe(desktopCalmBox?.width);
await page.locator('[data-pin-option="calm"]').click();
await calmFragment.click({
position: { x: (desktopCalmBox?.width ?? 20) - 10, y: 40 },
});
await calmNote.fill("desktop edge note");
await expectCalmPlacement(calmDesktopWidth);
await page.setViewportSize({ width: 360, height: 800 });
await expectCalmPlacement(calmMobileWidth);
await expect(calmNote).toHaveValue("desktop edge note");
await page.getByRole("button", { name: "Close" }).click();
await page.setViewportSize({ width: 1280, height: 800 });
await page.setViewportSize({ width: 360, height: 800 });
await expectCalmPinVisible(calmMobileWidth);
await calmFragment.locator(".pin").click();
await expect(calmNote).toHaveValue("desktop edge note");
await expectCalmPlacement(calmMobileWidth);
await calmFragment.locator("[data-delete]").click();
await page.locator('[data-select-option="dense"]').check();
const firstSubmission = server.result;
await page.locator("#iterate").click();
await expect(page.locator("#progress-title")).toHaveText(
"Feedback received. Pi is working.",
);
const first: MockupResult = await firstSubmission;
expect(first).toMatchObject({
action: "iterate",
selected: ["dense"],
notes: "keep dense",
annotations: [{ optionId: "dense", note: "tighten spacing" }],
review: { viewport: { devicePixelRatio: 1 } },
});
server.progress("Pi is working on the revision.");
await expect(page.locator("#progress-title")).toHaveText(
"Pi is working on the revision.",
);
await page.getByRole("button", { name: "Stop agent" }).click();
await expect.poll(() => aborts).toBe(1);
const secondSubmission = server.present({
title: "Layout revision",
question: "Does the denser revision work?",
options: [
{
id: "dense-v2",
label: "Dense revision",
html: interactiveFragment,
},
],
});
await expect(
page.getByRole("heading", { name: "Layout revision" }),
).toBeVisible();
expect(page.url()).toBe(stableUrl);
const revised = page.locator('[data-fragment="dense-v2"]');
await revised.getByRole("button", { name: "Increment" }).click();
await expect(revised.locator("output")).toHaveText("1");
await page.locator("#accept").click();
await expect(page.locator("#progress-title")).toHaveText(
"Accepted. You can close this tab.",
);
const second = await secondSubmission;
expect(second).toMatchObject({
action: "accept",
selected: ["dense-v2"],
notes: "",
annotations: [],
});
});
test("navigates flow pages and scopes pins to their page", async ({ page }) => {
server = await startMockupServer(
{
title: "Flow review",
question: "Does the flow work?",
options: [
{
id: "flow",
label: "Two steps",
pages: [
{
id: "cart",
label: "Cart",
html: '<h2>Cart</h2><p>Two items ready.</p><div style="width:1200px;height:120px">Wide content</div>',
},
{
id: "pay",
label: "Pay",
html: `<h2>Payment</h2><p data-total>0</p><script>(() => {
const script = document.currentScript;
const page = script.mockupPage;
const root = script.mockupRoot;
root.total = 42;
page.querySelector("[data-total]").textContent = "total 42";
root.addEventListener("mockup:pagechange", event => {
page.querySelector("[data-total]").textContent = "page " + event.detail.page;
});
})();</script>`,
},
],
},
],
},
{ openBrowser: false },
);
await page.goto(server.url);
const flow = page.locator('[data-fragment="flow"]');
const tabs = flow.locator('[role="tab"]');
const cartPanel = flow.locator('[data-mkup-page="cart"]');
const payPanel = flow.locator('[data-mkup-page="pay"]');
await expect(tabs).toHaveCount(2);
await expect(tabs.first()).toHaveText("1 · Cart");
await expect(cartPanel).toBeVisible();
await expect(payPanel).toBeHidden();
await expect(payPanel.locator("[data-total]")).toHaveText("page cart");
await page.setViewportSize({ width: 390, height: 850 });
await page.locator('[data-select-option="flow"]').check();
await flow.locator(".mkup-pages").evaluate((element) => {
element.scrollLeft = 240;
});
await page.locator('[data-pin-option="flow"]').click();
const frame = (await flow.boundingBox())!;
const panel = (await cartPanel.boundingBox())!;
const clicked = { x: frame.x + frame.width / 2, y: panel.y + 30 };
await page.mouse.click(clicked.x, clicked.y);
await page.getByLabel("Pin note").fill("scrolled cart");
await expect
.poll(async () => {
const pin = (await cartPanel.locator(".pin").boundingBox())!;
return Math.abs(pin.x + pin.width / 2 - clicked.x);
})
.toBeLessThan(2);
await tabs.nth(1).click();
await expect(cartPanel.locator(".pin")).toBeHidden();
await expect(page.locator(".pin-bubble")).toHaveCount(0);
await tabs.first().click();
await expect(cartPanel.locator(".pin")).toBeVisible();
await page.locator(".annotation-row button").click();
await page.setViewportSize({ width: 1280, height: 800 });
await tabs.nth(1).click();
await expect(payPanel).toBeVisible();
await expect(cartPanel).toBeHidden();
await expect(tabs.nth(1)).toHaveAttribute("aria-selected", "true");
await expect(payPanel.locator("[data-total]")).toHaveText("page pay");
await tabs.first().focus();
await page.keyboard.press("ArrowRight");
await expect(payPanel).toBeVisible();
await flow.getByRole("button", { name: "Previous page" }).click();
await expect(cartPanel).toBeVisible();
await page.locator('[data-select-option="flow"]').check();
await page.locator('[data-pin-option="flow"]').click();
await tabs.nth(1).click();
const box = await flow.boundingBox();
await flow.click({ position: { x: (box?.width ?? 100) / 2, y: 120 } });
await page.getByLabel("Pin note").fill("tighten the payment screen");
await expect(flow.locator(".pin")).toBeVisible();
await expect(page.locator("#annotations-panel")).toContainText("flow · pay");
const submission = server.result;
await page.locator("#iterate").click();
const result: MockupResult = await submission;
expect(result.annotations).toEqual([
expect.objectContaining({
optionId: "flow",
page: "pay",
note: "tighten the payment screen",
}),
]);
});
test("captures headless previews without opening the review", async () => {
const { captureMockupPreviews } = await import(
new URL("../../../dist/mockup/preview.ts", import.meta.url).href
);
const request = {
title: "Preview check",
question: "unused",
options: [
{ id: "solo", label: "Solo", html: "<section><h3>Solo</h3></section>" },
{
id: "flow",
label: "Flow",
pages: [
{ id: "one", label: "One", html: "<section>one</section>" },
{ id: "prev", label: "Two", html: "<section>two</section>" },
],
},
],
};
const capture = await captureMockupPreviews(request);
expect(
capture.shots.map((shot: { optionId: string; pageId?: string }) => [
shot.optionId,
shot.pageId,
]),
).toEqual([
["solo", undefined],
["flow", "one"],
["flow", "prev"],
]);
expect(capture.skipped).toEqual([]);
const png = Buffer.from([0x89, 0x50, 0x4e, 0x47]);
for (const shot of capture.shots) {
expect(shot.data.subarray(0, 4).equals(png)).toBe(true);
expect(shot.data.byteLength).toBeGreaterThan(1000);
}
expect(capture.shots[1].data.equals(capture.shots[2].data)).toBe(false);
const limited = await captureMockupPreviews(request, { limit: 2 });
expect(limited.shots).toHaveLength(2);
expect(limited.skipped).toEqual([{ optionId: "flow", pageId: "prev" }]);
});