import { devices, expect, test, type Page } from "@playwright/test";
import { axNode } from "@nibling/web/testing/ax";
import { run, terminalInput, terminalLog } from "./fixtures/terminal.ts";
// A phone: a coarse pointer and a narrow viewport, so the shell picks the compact presentation itself.
test.use({ ...devices["Pixel 7"] });
const shell = (page: Page) => page.locator("nib-shell");
const button = (page: Page, name: string) => page.getByRole("button", { name, exact: true });
const item = (page: Page, name: string) =>
page.locator("nib-tree-item").filter({ hasText: new RegExp(`^${name.replace(/[.]/g, "\\.")}▶?$`) });
const shownBodies = (page: Page) => page.locator("nib-tabpanel:not([hidden])");
test.beforeEach(async ({ page }) => {
await page.goto("/new?reset");
// The editor loads after the header, and sets globalThis.workbench when it starts.
await expect(shell(page)).toBeAttached();
await page.evaluate(() => localStorage.clear());
});
/** Writes an HTML page and opens its file preview. */
async function openPreview(page: Page) {
await page.waitForFunction(() => Boolean((globalThis as any).demo && (globalThis as any).workbench));
await page.evaluate(() => {
(globalThis as any).demo.write("page.html", "\n
Hello from nibling
\n");
(globalThis as any).workbench.open({ kind: "editor", input: { path: "page.html" } });
(globalThis as any).workbench.open({ kind: "preview", input: { path: "page.html" } });
});
}
test("a touch device in a narrow shell gets the editor alone, with no splits", async ({ page }) => {
await expect.poll(() => shell(page).evaluate((el) => el.matches(":state(compact)"))).toBe(true);
await expect(page.locator("nib-split")).toHaveCount(0);
await expect(page.locator("nib-gutter")).toHaveCount(0);
await expect(page.getByRole("textbox", { name: "index.ts" })).toBeVisible();
await expect(shownBodies(page)).toHaveCount(1);
await expect(page.locator("nib-tree")).toBeHidden();
await expect(page.locator("[part=title]")).toHaveText("index.ts");
});
test("touch targets are at least 48px", async ({ page }) => {
await openPreview(page);
for (const name of ["Files", "Preview"]) {
const box = (await button(page, name).boundingBox())!;
expect(box.height).toBeGreaterThanOrEqual(48);
expect(box.width).toBeGreaterThanOrEqual(48);
}
expect((await page.locator("nib-tab").first().boundingBox())!.height).toBeGreaterThanOrEqual(47);
});
test("the Files button opens the explorer in a modal drawer, and picking a file closes it", async ({ page }) => {
await expect(button(page, "Files")).toHaveAttribute("aria-expanded", "false");
await button(page, "Files").tap();
await expect(page.locator("[part=drawer]")).toHaveJSProperty("open", true);
await expect(button(page, "Files")).toHaveAttribute("aria-expanded", "true");
await expect.poll(() => axNode(page, "dialog", "Files")).toBeTruthy();
await expect(item(page, "index.ts")).toBeFocused();
await item(page, "package.json").tap();
await expect(page.locator("[part=drawer]")).toHaveJSProperty("open", false);
await expect(page.getByRole("textbox", { name: "package.json" })).toBeVisible();
await expect(page.locator("[part=title]")).toHaveText("package.json");
await expect(page.locator("nib-tree")).toBeHidden();
});
test("the Files button brings back an explorer that was closed", async ({ page }) => {
await page.evaluate(() => {
const wb = (globalThis as any).workbench;
wb.dispatch({ type: "close", panel: wb.panels("explorer")[0].id });
});
await expect(button(page, "Files")).toBeVisible();
await button(page, "Files").tap();
await expect(page.locator("[part=drawer]")).toHaveJSProperty("open", true);
await expect(item(page, "package.json")).toBeVisible();
});
test("Escape and the close button dismiss the drawer, returning focus to the Files button", async ({ page }) => {
await button(page, "Files").tap();
await page.keyboard.press("Escape");
await expect(page.locator("[part=drawer]")).toHaveJSProperty("open", false);
await expect(button(page, "Files")).toBeFocused();
await button(page, "Files").tap();
await button(page, "Close files").tap();
await expect(page.locator("[part=drawer]")).toHaveJSProperty("open", false);
});
test("Preview swaps the editor area to the preview without reloading its frame", async ({ page }) => {
await openPreview(page);
await page.locator("nib-tablist > nib-tab").filter({ hasText: /^page\.html/ }).tap();
const frame = (await (await page.locator("nib-preview iframe").elementHandle())!.contentFrame())!;
await frame.evaluate(() => Object.assign(globalThis, { marker: 7 }));
await expect(button(page, "Preview")).toHaveAttribute("aria-pressed", "false");
await button(page, "Preview").tap();
await expect(button(page, "Preview")).toHaveAttribute("aria-pressed", "true");
await expect(page.frameLocator("nib-preview iframe").getByRole("heading")).toBeVisible();
await expect(page.getByRole("textbox", { name: "page.html" })).toBeHidden();
await expect(page.locator("[part=title]")).toHaveText("Preview page.html");
expect(await frame.evaluate(() => (globalThis as { marker?: number }).marker)).toBe(7);
// The preview fills the editor area, which is the full width on a phone.
const box = (await page.locator("nib-preview iframe").boundingBox())!;
expect(box.width).toBeCloseTo(page.viewportSize()!.width, 0);
await button(page, "Preview").tap();
await expect(page.getByRole("textbox", { name: "page.html" })).toBeVisible();
});
test("Preview shows the server preview, waiting for a server, on a fresh project", async ({ page }) => {
await expect(button(page, "Preview")).toBeVisible();
await expect(button(page, "Preview")).toHaveAttribute("aria-pressed", "false");
await button(page, "Preview").tap();
await expect(button(page, "Preview")).toHaveAttribute("aria-pressed", "true");
await expect(page.getByRole("heading", { name: "No server is running" })).toBeVisible();
await expect(page.locator("[part=title]")).toHaveText("Preview");
await expect(page.getByRole("textbox", { name: "index.ts" })).toBeHidden();
await button(page, "Preview").tap();
await expect(page.getByRole("textbox", { name: "index.ts" })).toBeVisible();
});
test("a server starting from the terminal switches the editor area to its preview", async ({ page }) => {
await button(page, "Terminal").tap();
await expect(terminalLog(page)).toContainText("~ $", { timeout: 20_000 });
await run(page, "npm run dev");
await expect(page.frameLocator("nib-browser iframe").getByRole("heading", { level: 1 })).toHaveText("Hello from nibling", { timeout: 30_000 });
await expect(button(page, "Preview")).toHaveAttribute("aria-pressed", "true");
await expect(page.locator("[part=title]")).toHaveText("localhost:3000");
await expect(page.getByRole("textbox", { name: "index.ts" })).toBeHidden();
});
test("the terminal is a bottom sheet that opens from its handle and closes with Escape", async ({ page }) => {
const handle = button(page, "Terminal");
await expect(handle).toHaveAttribute("aria-expanded", "false");
await expect(page.locator("nib-terminal")).toBeHidden();
const viewport = page.viewportSize()!;
expect((await handle.boundingBox())!.y + 48).toBeGreaterThan(viewport.height - 2);
await handle.tap();
await expect(handle).toHaveAttribute("aria-expanded", "true");
const terminal = page.locator("nib-terminal");
await expect(terminal).toBeVisible();
await expect(terminalInput(page)).toBeFocused();
await expect(terminalLog(page)).toContainText("~ $", { timeout: 20_000 });
await run(page, "ls");
await expect(terminalLog(page)).toContainText("index.ts");
await page.keyboard.press("Escape");
await expect(handle).toHaveAttribute("aria-expanded", "false");
await expect(handle).toBeFocused();
await expect(terminal).toBeHidden();
});
test("turning the phone sideways past the breakpoint restores the desktop layout", async ({ page }) => {
await page.setViewportSize({ width: 1000, height: 412 });
await expect.poll(() => shell(page).evaluate((el) => el.matches(":state(compact)"))).toBe(false);
await expect(page.locator("nib-split").first()).toBeVisible();
await expect(page.locator("nib-tree")).toBeVisible();
await page.setViewportSize({ width: 412, height: 915 });
await expect.poll(() => shell(page).evaluate((el) => el.matches(":state(compact)"))).toBe(true);
});