deadhead-space-freight-tycoon/apps/web/e2e/install.e2e.ts

60 lines
2.4 KiB
TypeScript

import { expect, test } from "@playwright/test";
test.describe("installable PWA (Step 11.1)", () => {
test("Chrome reports no installability errors", async ({ page, context }) => {
await page.goto("/");
await page.evaluate(() => navigator.serviceWorker.ready.then(() => undefined));
// The check Lighthouse's PWA audit used (Lighthouse 12+ no longer has one).
const cdp = await context.newCDPSession(page);
const { installabilityErrors } = await cdp.send("Page.getInstallabilityErrors");
expect(installabilityErrors).toEqual([]);
});
test("has a standalone manifest with the game's name and icons", async ({ request }) => {
const manifest = await (await request.get("/manifest.webmanifest")).json();
expect(manifest).toMatchObject({
name: "Deadhead: Space Freight Tycoon",
display: "standalone",
theme_color: "#050919",
});
const sizes = manifest.icons.map((i: { sizes: string; purpose?: string }) => i.sizes);
expect(sizes).toEqual(expect.arrayContaining(["192x192", "512x512"]));
expect(manifest.icons.some((i: { purpose?: string }) => i.purpose === "maskable")).toBe(true);
for (const icon of manifest.icons) expect((await request.get(icon.src)).ok()).toBe(true);
});
test("precaches the app shell, fonts and generated map art, not the source art", async ({
page,
}) => {
await page.goto("/");
await page.evaluate(() => navigator.serviceWorker.ready.then(() => undefined));
// Wait for the install to finish filling the precache.
await expect
.poll(async () =>
page.evaluate(async () => {
const urls: string[] = [];
for (const name of await caches.keys()) {
for (const r of await (await caches.open(name)).keys()) urls.push(r.url);
}
return urls;
}),
)
.toEqual(
expect.arrayContaining([
expect.stringContaining("/index.html"),
expect.stringMatching(/\/assets\/map-gen\/body-earth@2x\.png/),
expect.stringMatching(/\/assets\/.*michroma.*\.woff2/),
expect.stringMatching(/\/assets\/.*instrument-sans.*\.woff2/),
]),
);
const urls = await page.evaluate(async () => {
const all: string[] = [];
for (const name of await caches.keys()) {
for (const r of await (await caches.open(name)).keys()) all.push(r.url);
}
return all;
});
expect(urls.filter((u) => u.includes("/assets/map/"))).toEqual([]);
});
});