Files
Rosea/e2e/petal.spec.ts
T

92 lines
3.9 KiB
TypeScript

import AxeBuilder from '@axe-core/playwright';
import { expect, test, type Page } from '@playwright/test';
import { en } from '@rosea/ui/copy';
import { execSync } from 'node:child_process';
import { mkdtempSync, rmSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { startStaticServer, type StaticServer } from './static-server.js';
import { storedCare } from './stored.js';
import fixture from './fixtures/library/fixture.json' with { type: 'json' };
/**
* The rose's folded petal (.agents/features/day-sentence.md), in the BUILT app.
*
* The shipped library is empty until the author verifies sentences, so the main
* build can only show that there is no petal. This spec also builds the app a second
* time, with a fixture library (ROSEA_LIBRARY_DIR, P5), into a folder of its own, and
* serves that. The second build passes through the same validate:library step.
*/
const ROOT = fileURLToPath(new URL('..', import.meta.url));
const MORNING = Date.parse('2026-03-01T10:00:00+01:00');
const texts = fixture.map((s) => s.text.en);
test.use({ timezoneId: 'Europe/Prague', locale: 'en-US' });
let server: StaticServer;
test.beforeEach(async () => {
server = await startStaticServer();
});
test.afterEach(async () => {
await server.close();
});
async function open(page: Page): Promise<void> {
await page.clock.setFixedTime(MORNING);
await page.goto(server.url);
await expect(page.getByRole('button', { name: en.rose.water })).toBeVisible();
}
test('with the shipped library still empty, she holds no petal and says nothing about one', async ({ page }) => {
await open(page);
await expect(page.getByRole('button', { name: en.rose.petal.open })).toHaveCount(0);
await expect(page.getByText(en.rose.petal.waiting)).toHaveCount(0);
});
test.describe('with a fixture library', () => {
let dist: string;
test.beforeAll(() => {
dist = mkdtempSync(join(tmpdir(), 'rosea-petal-'));
// The same build the player gets, with the fixture library in place of the real one.
execSync(`pnpm --filter @rosea/app run build --outDir "${dist}" --emptyOutDir`, {
cwd: ROOT,
env: { ...process.env, ROSEA_LIBRARY_DIR: 'e2e/fixtures/library' },
stdio: 'pipe'
});
});
test.afterAll(() => {
rmSync(dist, { recursive: true, force: true });
});
test.beforeEach(() => server.serveFrom(dist));
test('open her petal: the words and their source; a reload keeps it open, and no second one today', async ({ page }) => {
await open(page);
await page.getByRole('button', { name: en.rose.petal.open }).click();
const words = page.locator('blockquote');
await expect(words).toHaveCount(1);
const said = (await words.textContent()) ?? '';
expect(texts).toContain(said);
await storedCare(page, 1);
await page.reload();
await expect(page.getByRole('button', { name: en.rose.petal.today })).toHaveAttribute('aria-expanded', 'true');
await expect(page.locator('blockquote')).toHaveText(said);
await expect(page.getByRole('button', { name: en.rose.petal.open })).toHaveCount(0);
// Tomorrow, the next one is folded.
await page.clock.setFixedTime(MORNING + 24 * 3_600_000);
await page.reload();
await expect(page.getByRole('button', { name: en.rose.petal.open })).toBeVisible();
});
test('the folded and the open petal have no accessibility violations', async ({ page }) => {
await open(page);
await expect(page.getByRole('button', { name: en.rose.petal.open })).toBeVisible();
expect((await new AxeBuilder({ page }).analyze()).violations.map((v) => `${v.id}: ${v.help}`)).toEqual([]);
await page.getByRole('button', { name: en.rose.petal.open }).click();
await expect(page.locator('blockquote')).toBeVisible();
expect((await new AxeBuilder({ page }).analyze()).violations.map((v) => `${v.id}: ${v.help}`)).toEqual([]);
});
});