Files

165 lines
6.8 KiB
TypeScript

import { mkdirSync } from 'node:fs';
import { readFile } from 'node:fs/promises';
import { fileURLToPath } from 'node:url';
import { expect, test, type Browser, type BrowserContext, type Page } from '@playwright/test';
import { en } from '@rosea/ui/copy';
import { openRing, pastGardener } from './garden.js';
import { startStaticServer, type StaticServer } from './static-server.js';
import { storedCare, storedTown } from './stored.js';
/**
* The pictures for Rosea's Aula page (.agents/features/fabrica-release.md F7), taken from the
* BUILT PWA. The demo garden is made by the game itself, as a player makes one: the first look
* of the gardener, the town Prague, and two weeks of watering, once each morning, in an invented
* March. No file, profile or town of a real player is read. `.fabrica/aula.yaml` lists the files;
* `fabrica aula shots` checks and publishes them.
*/
const OUT = fileURLToPath(new URL('./aula/', import.meta.url));
const ZONE = 'Europe/Prague';
const DAYS = 14;
/** 08:00 in Prague on day `n` of March 2026 (CET, before the clocks change on the 29th). */
const morning = (n: number): number => Date.parse(`2026-03-${String(n).padStart(2, '0')}T08:00:00+01:00`);
const LAST = morning(DAYS + 1);
let server: StaticServer;
test.beforeAll(async () => {
mkdirSync(OUT, { recursive: true });
server = await startStaticServer();
});
test.afterAll(async () => {
await server.close();
});
/** Holds Water with the mouse, as a player holds it with a finger, until she thanks them. */
async function water(page: Page): Promise<void> {
await openRing(page);
await page.getByRole('button', { name: en.rose.water }).hover();
await page.mouse.down();
await page.waitForTimeout(1500);
await page.mouse.up();
await expect(page.getByText(en.rose.says.thanks)).toBeVisible();
}
async function shot(page: Page, name: string): Promise<void> {
await page.screenshot({ path: `${OUT}${name}.png`, animations: 'disabled', caret: 'hide' });
}
/** Every request the page makes, to prove the pictures were taken with nothing from elsewhere. */
function watch(page: Page): string[] {
const requests: string[] = [];
page.on('request', (r) => requests.push(r.url()));
return requests;
}
async function chooseGardenerAndTown(page: Page): Promise<void> {
await pastGardener(page);
await page.getByLabel(en.sunset.searchLabel).fill('prague');
await page.getByRole('button', { name: /^Prague/ }).first().click();
await storedTown(page, 'Prague');
}
async function newContext(browser: Browser, phone: boolean): Promise<BrowserContext> {
const common = { timezoneId: ZONE, locale: 'en-US', colorScheme: 'light' as const, reducedMotion: 'reduce' as const };
return phone
? browser.newContext({
...common,
viewport: { width: 360, height: 800 },
deviceScaleFactor: 2,
isMobile: true,
hasTouch: true,
userAgent: 'Mozilla/5.0 (Linux; Android 14; Pixel 7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Mobile Safari/537.36'
})
: browser.newContext({ ...common, viewport: { width: 1280, height: 800 }, deviceScaleFactor: 1 });
}
test.describe.configure({ mode: 'serial', timeout: 300_000 });
let saved = '';
test('two weeks in an invented garden, photographed on a desktop', async ({ browser }) => {
const context = await newContext(browser, false);
try {
const page = await context.newPage();
const requests = watch(page);
await page.clock.install({ time: morning(1) });
await page.goto(server.url);
await expect(page.getByRole('heading', { level: 2, name: en.gardener.question })).toBeVisible({ timeout: 20_000 });
await shot(page, 'first-open');
await chooseGardenerAndTown(page);
for (let day = 1; day <= DAYS; day += 1) {
if (day > 1) {
await page.clock.setSystemTime(morning(day));
await page.reload();
await expect(page.getByRole('button', { name: en.rose.care })).toBeVisible();
}
await water(page);
await storedCare(page, day);
}
// The fifteenth morning: what she needs today, before any care.
await page.clock.setSystemTime(LAST);
await page.reload();
await expect(page.getByRole('button', { name: en.rose.care })).toBeVisible();
await expect(page.locator('.sun p').first()).toHaveText(/in Prague\.$/);
await shot(page, 'garden');
await openRing(page);
await shot(page, 'ring');
await page.keyboard.press('Escape');
await page.getByRole('button', { name: en.rose.numbers }).click();
await expect(page.locator('#rose-numbers')).toContainText(en.rose.daysOfCare);
await page.locator('#rose-numbers').evaluate((el) => el.scrollIntoView({ block: 'center' }));
await shot(page, 'numbers');
await page.getByRole('button', { name: en.rose.safe.title }).click();
await expect(page.getByRole('button', { name: en.rose.safe.save })).toBeVisible();
await page.getByRole('button', { name: en.rose.safe.save }).scrollIntoViewIfNeeded();
await shot(page, 'safe');
// The same rose, carried to the phone below as a player carries her: in a file.
const download = page.waitForEvent('download');
await page.getByRole('button', { name: en.rose.safe.save }).click();
saved = await readFile((await (await download).path())!, 'utf8');
expect(saved).not.toMatch(/Prague|privateKey/);
await page.reload();
await page.getByRole('button', { name: en.chair.move }).click();
const scene = page.getByRole('dialog', { name: en.chair.scene });
await expect(scene).toBeVisible();
await page.clock.runFor(30_000);
await expect(scene.getByText(/^Tonight the sun sets at .* in Prague\.$/)).toBeVisible();
await shot(page, 'sunset');
expect(requests.filter((url) => !url.startsWith(server.origin + '/'))).toEqual([]);
} finally {
await context.close();
}
});
test('the same rose on a phone', async ({ browser }) => {
expect(saved, 'the desktop test saves the rose this one opens').not.toBe('');
const context = await newContext(browser, true);
try {
const page = await context.newPage();
const requests = watch(page);
await page.clock.install({ time: LAST });
await page.goto(server.url);
await chooseGardenerAndTown(page);
await page.getByRole('button', { name: en.rose.safe.title }).click();
await page.getByLabel(en.rose.safe.bringBack).setInputFiles({ name: 'rose.json', mimeType: 'application/json', buffer: Buffer.from(saved) });
await expect(page.getByText(/^She is back/)).toBeVisible();
await storedCare(page, DAYS);
await page.reload();
await expect(page.getByRole('button', { name: en.rose.care })).toBeVisible();
await shot(page, 'phone-garden');
await openRing(page);
await shot(page, 'phone-ring');
expect(requests.filter((url) => !url.startsWith(server.origin + '/'))).toEqual([]);
} finally {
await context.close();
}
});