repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

extensions/ui/__tests__/widget-layout.test.ts

Raw
File outline
import { existsSync } from "node:fs";
import {
	type BrowserType,
	chromium,
	firefox,
	type Page,
} from "@playwright/test";
import { describe, expect, it } from "vitest";

const chromePath = "C:/Program Files/Google/Chrome/Application/chrome.exe";

describe("ui widget browser layout", () => {
	it("lets unsandboxed srcdoc widgets call parent window.pi in Chrome and Firefox", async () => {
		for (const browser of browsers()) {
			const result = await withBrowser(browser, async (page) => {
				await page.setContent(bridgeProbeHtml());
				await page.waitForFunction(() => window.calls?.length === 1);
				return await page.evaluate(() => window.calls[0]);
			});
			expect(result).toEqual({ from: "srcdoc", sameOrigin: true });
		}
	}, 120_000);

	it("keeps over-wide iframe content bounded at phone width in Chrome and Firefox", async () => {
		for (const browser of browsers()) {
			const result = await withBrowser(browser, async (page) => {
				await page.setViewportSize({ width: 390, height: 760 });
				await page.setContent(layoutProbeHtml());
				await page.waitForFunction(
					() => document.querySelector("iframe")?.clientHeight === 600,
				);
				return await page.evaluate(() => ({
					viewport: document.documentElement.clientWidth,
					scrollWidth: document.documentElement.scrollWidth,
					iframeHeight: document
						.querySelector("iframe")
						?.getBoundingClientRect().height,
				}));
			});
			expect(result).toEqual({
				viewport: 390,
				scrollWidth: 390,
				iframeHeight: 600,
			});
		}
	}, 120_000);
});

function browsers(): Array<{
	name: string;
	type: BrowserType;
	executablePath?: string;
}> {
	return [
		...(existsSync(chromePath)
			? [{ name: "chrome", type: chromium, executablePath: chromePath }]
			: []),
		{ name: "firefox", type: firefox },
	];
}

async function withBrowser<T>(
	browser: { name: string; type: BrowserType; executablePath?: string },
	fn: (page: Page) => Promise<T>,
): Promise<T> {
	const instance = await browser.type.launch({
		headless: true,
		executablePath: browser.executablePath,
	});
	try {
		const page = await instance.newPage();
		return await fn(page);
	} finally {
		await instance.close();
	}
}

function bridgeProbeHtml(): string {
	const srcdoc =
		"<script>parent.pi.action({from:'srcdoc',sameOrigin:!!parent.document});</" +
		"script>";
	const attr = srcdoc.replaceAll(String.fromCharCode(34), "&quot;");
	return [
		"<!doctype html>",
		"<script>window.calls=[];window.pi={action(x){window.calls.push(x)}};</script>",
		`<iframe srcdoc="${attr}"></iframe>`,
	].join("");
}

function layoutProbeHtml(): string {
	const srcdoc = [
		"<style>body{margin:0}.wide{width:2200px;height:80px}.tall{height:900px}</style>",
		"<div class=wide>x</div><div class=tall>y</div>",
		"<script>parent.postMessage({height:document.documentElement.scrollHeight},'*')</" +
			"script>",
	].join("");
	const attr = srcdoc
		.replaceAll("&", "&amp;")
		.replaceAll(String.fromCharCode(34), "&quot;");
	return [
		"<!doctype html>",
		"<style>*{box-sizing:border-box}body{margin:0}.wrap{width:min(960px,100vw);overflow:auto}.visual{min-width:0;overflow:auto;contain:layout paint;padding:12px}.visual iframe{display:block;width:100%;height:120px;max-height:600px;border:0}</style>",
		`<div class=wrap><div class=visual><iframe srcdoc="${attr}"></iframe></div></div>`,
		"<script>addEventListener('message',e=>{document.querySelector('iframe').style.height=Math.min(e.data.height,600)+'px'})</script>",
	].join("");
}