diff --git a/e2e/apps/check-garden.ts b/e2e/apps/check-garden.ts index 1434301..1ac7666 100644 --- a/e2e/apps/check-garden.ts +++ b/e2e/apps/check-garden.ts @@ -81,37 +81,117 @@ async function firstVisit(page: Page): Promise { } /** After the app was closed and opened again: she is still watered. */ +/** On a failure, what the screen showed: a picture in the temporary folder, its path in the report. */ +let lastPage: Page | undefined; + async function secondVisit(page: Page): Promise { + lastPage = page; await garden(page); // By keyboard: on the tablet a pointer click once waited 30 s for the button to stop moving // while the garden animated (review). Enter is a real activation, as for a keyboard player. + // Her picture first: the garden's heading comes before her care is read back, and a press in + // that moment was lost twice on the desktop (keep-her-safe-apps review). Then press until the + // button says it is open: a press is only repeated while it still says closed. + await expect(page.getByRole('img', { name: /^The rose/ })).toBeVisible({ timeout: 20_000 }); const numbers = page.getByRole('button', { name: en.rose.numbers }); - await numbers.focus(); - await numbers.press('Enter'); + await expect(async () => { + if ((await numbers.getAttribute('aria-expanded')) !== 'true') { + await numbers.focus(); + await numbers.press('Enter'); + } + await expect(numbers).toHaveAttribute('aria-expanded', 'true', { timeout: 1_000 }); + }).toPass({ timeout: 15_000 }); await expect(page.locator('#rose-numbers')).toContainText(`${en.rose.daysOfCare}1`); await storedCare(page, 1); report['afterRestart'] = 'still watered: 1 day of care, the care log kept'; } -/** What *Save her to a file* does here (G-T4): a download event, or nothing. */ -async function trySave(page: Page): Promise { - const title = page.getByRole('button', { name: en.rose.safe.title }); - if (!(await title.isVisible().catch(() => false))) { - report['saveToFile'] = 'Keep her safe not visible'; - return; +/** Opens *Keep her safe* and checks the app's own words (keep-her-safe-apps.md A1). */ +async function openKeepSafe(page: Page): Promise { + await page.getByRole('button', { name: en.rose.safe.title }).click(); + await expect(page.getByText(en.rose.safe.app)).toBeVisible(); + for (const browserWords of [en.rose.safe.mayClear, en.rose.safe.unknown, en.rose.safe.kept, en.rose.safe.ask]) { + await expect(page.getByText(browserWords)).toHaveCount(0); + } + report['keepHerSafeWords'] = en.rose.safe.app; +} + +/** Desktop: *Save her to a file* starts a WebView2 download (cancelled by the check). */ +async function desktopSave(page: Page): Promise { + await openKeepSafe(page); + const download = page.waitForEvent('download', { timeout: 10_000 }); + await page.getByRole('button', { name: en.rose.safe.save }).click(); + const d = await download; + report['saveToFile'] = `a download started: ${d.suggestedFilename()} (cancelled by the check)`; + await d.cancel(); +} + +interface UiNode { + readonly text: string; + readonly desc: string; + readonly cx: number; + readonly cy: number; +} + +/** The screen's accessibility nodes, through uiautomator, printed to stdout (no file left behind). */ +function screenNodes(adb: (...args: string[]) => string): UiNode[] { + const xml = adb('exec-out', 'uiautomator', 'dump', '/dev/tty'); + const nodes: UiNode[] = []; + for (const m of xml.matchAll(/]*?text="([^"]*)"[^>]*?content-desc="([^"]*)"[^>]*?bounds="\[(\d+),(\d+)\]\[(\d+),(\d+)\]"/g)) { + const [x1, y1, x2, y2] = [m[3], m[4], m[5], m[6]].map(Number) as [number, number, number, number]; + nodes.push({ text: m[1] ?? '', desc: m[2] ?? '', cx: Math.round((x1 + x2) / 2), cy: Math.round((y1 + y2) / 2) }); + } + return nodes; +} + +/** Taps the first node on Android's own screen that matches, waiting up to `ms` for it. */ +async function tapSystem(adb: (...args: string[]) => string, match: (n: UiNode) => boolean, what: string, ms = 20_000): Promise { + const until = Date.now() + ms; + while (Date.now() < until) { + const node = screenNodes(adb).find(match); + if (node) { + adb('shell', 'input', 'tap', String(node.cx), String(node.cy)); + return; + } + await new Promise((r) => setTimeout(r, 700)); + } + throw new Error(`Android's screen never showed ${what}`); +} + +/** + * Android: the round trip (keep-her-safe-apps.md §5). *Save her to a file* opens Android's save + * screen; the check presses its Save; the garden says she is in that file. Then *Bring her back + * from a file* opens the picker; the check chooses that file; the garden says everything in it is + * already here. The file is removed from the tablet afterwards if this run made it. + */ +async function androidRoundTrip(page: Page, adb: (...args: string[]) => string): Promise { + await openKeepSafe(page); + const name = `rose-${await page.evaluate(() => { + const d = new Date(); + return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, '0')}-${String(d.getDate()).padStart(2, '0')}`; + })}.json`; + const before = adb('shell', 'ls', '/sdcard/Download/'); + const existed = before.split(/\s+/).includes(name); + if (existed) throw new Error(`/sdcard/Download/${name} is already on the tablet; the check will not overwrite a file it did not make`); + try { + await page.getByRole('button', { name: en.rose.safe.save }).click(); + await tapSystem(adb, (n) => /^save$/i.test(n.text), "the save screen's Save button"); + await expect(page.getByText(en.rose.safe.saved)).toBeVisible({ timeout: 15_000 }); + report['saveToFile'] = `Android's save screen; the garden said: ${en.rose.safe.saved}`; + + await page.locator('input[type="file"]').click({ force: true }); + await tapSystem(adb, (n) => n.text === name || n.desc.includes(name), `the file ${name} in the picker`); + await expect(page.getByText(en.rose.safe.alreadyHere)).toBeVisible({ timeout: 15_000 }); + report['bringBack'] = `chose ${name} in Android's picker; the garden said: ${en.rose.safe.alreadyHere}`; + } finally { + const after = adb('shell', 'ls', '/sdcard/Download/').split(/\s+/); + if (after.includes(name)) { + adb('shell', 'rm', `/sdcard/Download/${name}`); + report['cleanedUp'] = `/sdcard/Download/${name}`; + } else { + report['cleanedUp'] = `nothing at /sdcard/Download/${name} (saved elsewhere, or not saved)`; + } } - await title.click(); - const save = page.getByRole('button', { name: en.rose.safe.save }); - await expect(save).toBeVisible(); - const download = page.waitForEvent('download', { timeout: 5_000 }).then( - async (d) => { - await d.cancel(); - return `a download started: ${d.suggestedFilename()} (cancelled by the check)`; - }, - () => 'nothing happened within 5 s' - ); - await save.click(); - report['saveToFile'] = await download; } async function desktop(exe: string): Promise { @@ -153,7 +233,7 @@ async function desktop(exe: string): Promise { app = start(); session = await attach(); await secondVisit(session.page); - await trySave(session.page); + await desktopSave(session.page); await session.close(); report['dataFolder'] = data; } finally { @@ -203,7 +283,7 @@ async function androidDevice(serial: string | undefined): Promise { await new Promise((r) => setTimeout(r, 1500)); session = await open(); await secondVisit(session.page); - await trySave(session.page); + await androidRoundTrip(session.page, adb); } finally { adb('shell', 'am', 'force-stop', PACKAGE_ID); await session?.close().catch((error: unknown) => console.error('check-garden: closing the device handle failed', error)); @@ -222,6 +302,13 @@ try { console.log(JSON.stringify(report, null, 2)); console.log(`check-garden ${target}: PASSED`); } catch (error) { + if (lastPage) { + const shot = join(tmpdir(), `rosea-check-${target ?? 'unknown'}-failure.png`); + await lastPage.screenshot({ path: shot }).then( + () => (report['failureScreenshot'] = shot), + (e: unknown) => (report['failureScreenshot'] = `could not take one: ${String(e)}`) + ); + } console.log(JSON.stringify(report, null, 2)); console.error(`check-garden ${target}: FAILED — ${(error as Error).message}`); process.exitCode = 1;