Repository Public
1/**2 * =============================================================================3 * SP BOUCHER | SOFTWARE & RESEARCH4 * ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project : Note SPBoucher AI8 * File : tests/e2e/next-p-offline-phones.spec.ts9 * Description : web offline-first on phone viewports (360 / 375 / 430):10 * the offline app opens from the service-worker shell, lists11 * the notes kept on the device, opens one in the editor,12 * creates a note and a task, with no horizontal overflow and13 * a reachable sync inspector. Production build, real offline14 * (context.setOffline).15 * Module : tests16 * Repository : https://git.spboucher.ai/note-spboucher-ai17 * Project Website : https://note.spboucher.ai18 * Created : 2026-11-1919 * Last Modified : 2026-11-1920 *21 * Author : Simon-Pierre Boucher22 * Position : Professor of Finance23 * Institution : Université du Québec en Outaouais (UQO)24 * Organization : SP Boucher25 * Website : https://spboucher.ai26 * Git : https://git.spboucher.ai27 *28 * Preferred Contact: contact@spboucher.ai29 * Development : dev@spboucher.ai30 * Support : support@spboucher.ai31 * Security/Admin : admin@spboucher.ai32 *33 * Copyright (c) 202634 * Simon-Pierre Boucher35 * All rights reserved where applicable. This file is governed by the36 * applicable project license, copyright notices and relevant law.37 * Existing third-party rights and notices remain unaffected.38 *39 * License : None specified (no license file in this repository)40 *41 * Do not embed credentials, private keys, access tokens,42 * certificates or confidential personal information.43 *44 * =============================================================================45 * END OF SP BOUCHER SOURCE HEADER46 * =============================================================================47 */4849import { test, expect, type Page } from "@playwright/test";50import fs from "node:fs";51import { apiCall, docOf, para, sql, expectNoHorizontalOverflow } from "./helpers";5253const SHOTS = process.env.SHOTS_DIR ?? `${process.env.HOME}/dev/note-next-p-data/shots`;54const waitFor = async <T,>(fn: () => Promise<T | null | undefined | false>, ms = 20_000): Promise<T> => { const t0 = Date.now(); for (;;) { const v = await fn(); if (v) return v; if (Date.now() - t0 > ms) throw new Error("condition not met in time"); await new Promise((r) => setTimeout(r, 300)); } };55const ids = (page: Page, name: string) => page.evaluate((n) => new Promise<string[]>((resolve) => { const r = indexedDB.open("note-local"); r.onsuccess = () => { const d = r.result; if (!d.objectStoreNames.contains(n)) { d.close(); return resolve([]); } const g = d.transaction(n, "readonly").objectStore(n).getAll(); g.onsuccess = () => { d.close(); resolve(g.result.map((x: any) => x.id ?? x.pageId ?? String(x.seq))); }; }; r.onerror = () => resolve([]); }), name);56test.afterAll(async () => { await sql`delete from tasks where title like ${"E2E-offphone %"}`; await sql`delete from pages where title like ${"E2E-offphone %"}`; });5758test("phone: the offline app lists, opens, creates and stays inside the viewport", async ({ page, context }, info) => {59 test.setTimeout(150_000);60 const tag = `${info.project.name} ${Date.now().toString(36)}`; const title = `E2E-offphone ${tag}`;61 await page.goto("/");62 const a = await apiCall(page, "POST", "/api/pages", { title, content: docOf(para("Readable in the metro.")) }); expect(a.status).toBe(201);63 const controlled = () => page.evaluate(() => Boolean(navigator.serviceWorker.controller));64 try { await waitFor(controlled, 15_000); } catch { await page.reload(); await waitFor(controlled, 30_000); }65 await page.evaluate(() => new Promise<void>((resolve) => { const ch = new MessageChannel(); ch.port1.onmessage = () => resolve(); navigator.serviceWorker.controller!.postMessage({ type: "REFRESH_SHELL" }, [ch.port2]); setTimeout(resolve, 15_000); }));66 await page.reload(); await waitFor(async () => (await ids(page, "pages")).includes(a.body.id));67 await context.setOffline(true);68 await page.goto("/"); await expect(page.getByTestId("offline-app")).toBeVisible();69 await expect(page.getByTestId("offline-row").filter({ hasText: title })).toBeVisible(); await expectNoHorizontalOverflow(page);70 fs.mkdirSync(SHOTS, { recursive: true }); await page.screenshot({ path: `${SHOTS}/offline-phone-home-${info.project.name}.png` });71 await page.getByTestId("offline-row").filter({ hasText: title }).click();72 await expect(page.locator("[data-testid=offline-page] .ProseMirror").first()).toContainText("Readable in the metro."); await expectNoHorizontalOverflow(page);73 await page.getByRole("button", { name: "New note" }).first().click();74 await expect(page.locator(`[data-testid=offline-page]:not([data-page-id="${a.body.id}"])`)).toBeVisible();75 await page.getByLabel("Page title").fill(`E2E-offphone new ${tag}`);76 await page.getByLabel("New task for this note").fill(`E2E-offphone task ${tag}`); await page.getByRole("button", { name: "Add task" }).click();77 await expect(page.getByTestId("offline-page-task")).toHaveText(`E2E-offphone task ${tag}`); await expectNoHorizontalOverflow(page);78 await page.getByTestId("sync-status").click(); await expect(page.getByTestId("sync-inspector")).toBeVisible(); await expect(page.getByTestId("sync-entry")).toHaveCount(2); await expectNoHorizontalOverflow(page);79 await page.screenshot({ path: `${SHOTS}/offline-phone-inspector-${info.project.name}.png` }); await page.keyboard.press("Escape");80 await page.getByRole("navigation", { name: "Offline navigation" }).last().getByRole("button", { name: "Tasks" }).click();81 await expect(page.getByTestId("offline-task").filter({ hasText: `E2E-offphone task ${tag}` })).toBeVisible(); await expectNoHorizontalOverflow(page);82 await context.setOffline(false);83 await expect(page.getByTestId("offline-app")).toBeHidden({ timeout: 45_000 });84 await waitFor(async () => (await sql`select 1 from pages where title = ${`E2E-offphone new ${tag}`}`).length === 1);85 await waitFor(async () => (await sql`select 1 from tasks where title = ${`E2E-offphone task ${tag}`}`).length === 1);86});87