From 03dc69702a2dfaaab2b2ef1af8031ee661173a68 Mon Sep 17 00:00:00 2001 From: mnovak Date: Mon, 2 Nov 2026 08:55:06 +0100 Subject: [PATCH] [ui] DaySentence: Her folded petal, the words and their source; the library bundled after validation --- apps/rosea/package.json | 2 +- apps/rosea/src/App.svelte | 5 + apps/rosea/src/vite-env.d.ts | 7 ++ apps/rosea/vite.config.ts | 34 ++++++ e2e/fixtures/library/fixture.json | 18 +++ e2e/petal.spec.ts | 91 +++++++++++++++ e2e/playwright.config.ts | 2 +- e2e/shell.spec.ts | 2 +- packages/ui/src/Shell.svelte | 2 +- packages/ui/src/copy/en.ts | 21 +++- packages/ui/src/rose/KeepSafe.svelte | 4 +- packages/ui/src/rose/Petal.svelte | 128 ++++++++++++++++++++ packages/ui/src/rose/Rose.svelte | 45 +++++-- packages/ui/src/rose/types.ts | 8 +- packages/ui/test/petal.test.ts | 168 +++++++++++++++++++++++++++ packages/ui/test/rose.test.ts | 7 +- 16 files changed, 525 insertions(+), 19 deletions(-) create mode 100644 e2e/fixtures/library/fixture.json create mode 100644 e2e/petal.spec.ts create mode 100644 packages/ui/src/rose/Petal.svelte create mode 100644 packages/ui/test/petal.test.ts diff --git a/apps/rosea/package.json b/apps/rosea/package.json index e2c723e..0c65151 100644 --- a/apps/rosea/package.json +++ b/apps/rosea/package.json @@ -6,7 +6,7 @@ "description": "The PWA. Renders what the engine returns; contains no growth, taming or sky logic.", "scripts": { "dev": "vite --host 127.0.0.1 --port 5173 --strictPort", - "build": "vite build", + "build": "tsx ../../scripts/validate-library.ts && vite build", "typecheck": "svelte-check --threshold error --output human" }, "dependencies": { diff --git a/apps/rosea/src/App.svelte b/apps/rosea/src/App.svelte index 3887d94..cdbb75d 100644 --- a/apps/rosea/src/App.svelte +++ b/apps/rosea/src/App.svelte @@ -7,6 +7,8 @@ import { version } from '../package.json'; // Bundled at build time, so the letter is precached and opens offline (letter-slot.md L1). import letter from '../../../docs/LETTER.md?raw'; + // The verified hope library, bundled after validate:library passed (day-sentence.md P5). + import sentences from 'virtual:rosea-library'; // The device's own zone, read here and nowhere else: the engine never asks the // device anything (CLAUDE.md §2). Only the zone's name; never GPS, never a lookup. @@ -92,6 +94,9 @@ await storage.appendCare(event); keepAfterCare(); }, + sentences, + loadOpened: () => storage.loadOpened(), + appendOpened: (event) => storage.appendOpened(event), clientVersion: version, deviceZone, now: () => Date.now(), diff --git a/apps/rosea/src/vite-env.d.ts b/apps/rosea/src/vite-env.d.ts index 0cde2af..65e826c 100644 --- a/apps/rosea/src/vite-env.d.ts +++ b/apps/rosea/src/vite-env.d.ts @@ -1,3 +1,10 @@ /// /// /// + +declare module 'virtual:rosea-library' { + import type { HopeSentence } from '@rosea/schema'; + /** The verified hope library, bundled at build time after validate:library passed (day-sentence.md P5). */ + const library: readonly HopeSentence[]; + export default library; +} diff --git a/apps/rosea/vite.config.ts b/apps/rosea/vite.config.ts index cc5886a..0e4aed1 100644 --- a/apps/rosea/vite.config.ts +++ b/apps/rosea/vite.config.ts @@ -1,4 +1,7 @@ import { svelte } from '@sveltejs/vite-plugin-svelte'; +import { readdirSync, readFileSync } from 'node:fs'; +import { join, resolve } from 'node:path'; +import { fileURLToPath } from 'node:url'; import { defineConfig, type Plugin } from 'vite'; import { VitePWA } from 'vite-plugin-pwa'; // A relative path, not '@rosea/schema', on purpose. Vite bundles a config's @@ -55,12 +58,43 @@ function contentSecurityPolicy(): Plugin { }; } +/** + * The hope library, bundled at build time as `virtual:rosea-library` + * (day-sentence.md P5). The build script runs `validate:library` first and stops if + * it fails, so the app holds exactly what passed and never validates in the browser + * (GAP_ANALYSIS G0.12: Ajv needs `new Function`, which the CSP forbids). + * `ROSEA_LIBRARY_DIR` swaps the folder, for the e2e build only. + */ +const LIBRARY_DIR = resolve(fileURLToPath(new URL('../..', import.meta.url)), process.env['ROSEA_LIBRARY_DIR'] ?? 'content/library'); + +function hopeLibrary(): Plugin { + const id = 'virtual:rosea-library'; + const resolved = `\0${id}`; + return { + name: 'rosea-library', + resolveId: (source) => (source === id ? resolved : undefined), + load(source) { + if (source !== resolved) return undefined; + const entries: unknown[] = []; + for (const file of readdirSync(LIBRARY_DIR).filter((f) => f.endsWith('.json')).sort()) { + const path = join(LIBRARY_DIR, file); + this.addWatchFile(path); + const data: unknown = JSON.parse(readFileSync(path, 'utf8')); + if (!Array.isArray(data)) throw new Error(`${path}: must be an array of entries`); + entries.push(...data); + } + return `export default ${JSON.stringify(entries)};`; + } + }; +} + // Nothing binds to anything but 127.0.0.1 (CLAUDE.md §5.7). export default defineConfig({ base: BASE, plugins: [ svelte(), contentSecurityPolicy(), + hopeLibrary(), VitePWA({ strategies: 'generateSW', // Never swap a running app silently. A new worker waits until the player diff --git a/e2e/fixtures/library/fixture.json b/e2e/fixtures/library/fixture.json new file mode 100644 index 0000000..11e10f8 --- /dev/null +++ b/e2e/fixtures/library/fixture.json @@ -0,0 +1,18 @@ +[ + { + "id": "e2e-first", + "text": { "en": "An e2e fixture sentence, not a real one." }, + "source": { "work": "Fixture 1", "author": null, "tradition": "fixture", "translation": null, "publicDomain": true, "verifiedBy": "e2e", "verifiedAt": "2026-11-01" }, + "use": ["sunset"], + "tone": "gentle", + "promisesOutcome": false + }, + { + "id": "e2e-second", + "text": { "en": "A second e2e fixture sentence." }, + "source": { "work": "Fixture 2", "author": "A Writer", "tradition": "fixture", "translation": null, "publicDomain": true, "verifiedBy": "e2e", "verifiedAt": "2026-11-01" }, + "use": ["sunset"], + "tone": "gentle", + "promisesOutcome": false + } +] diff --git a/e2e/petal.spec.ts b/e2e/petal.spec.ts new file mode 100644 index 0000000..430185e --- /dev/null +++ b/e2e/petal.spec.ts @@ -0,0 +1,91 @@ +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 { + 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([]); + }); +}); diff --git a/e2e/playwright.config.ts b/e2e/playwright.config.ts index 7632fe3..4101856 100644 --- a/e2e/playwright.config.ts +++ b/e2e/playwright.config.ts @@ -7,7 +7,7 @@ import { defineConfig, devices } from '@playwright/test'; */ export default defineConfig({ testDir: '.', - testMatch: /(shell|update)\.spec\.ts/, + testMatch: /(shell|update|petal)\.spec\.ts/, fullyParallel: false, workers: 1, retries: 0, diff --git a/e2e/shell.spec.ts b/e2e/shell.spec.ts index 2d97d3f..ce39632 100644 --- a/e2e/shell.spec.ts +++ b/e2e/shell.spec.ts @@ -253,7 +253,7 @@ test.describe('keep her safe, in Prague at 08:00 on 1 March 2026', () => { } finally { await first.close(); } - expect(JSON.parse(saved)).toMatchObject({ format: 'rosea-rose', version: 1 }); + expect(JSON.parse(saved)).toMatchObject({ format: 'rosea-rose', version: 2 }); expect(saved).not.toMatch(/Brno|privateKey/); // A fresh profile: the shape of an evicted garden, or a new phone. diff --git a/packages/ui/src/Shell.svelte b/packages/ui/src/Shell.svelte index f031126..d11e73b 100644 --- a/packages/ui/src/Shell.svelte +++ b/packages/ui/src/Shell.svelte @@ -129,7 +129,7 @@

{PRODUCT_NAME}

{en.shell.preparing}

{#if rose} - + {/if} {#if sunset && (status === 'asking' || changing)} + `${author ? `${author}, ` : ''}${work}${translation ? ` (${translation})` : ''}` + }, /** Keeping her (keep-her-safe.md). True and calm: a browser can clear a site's data. */ safe: { title: 'Keep her safe', @@ -62,7 +80,8 @@ export const en = { save: 'Save her to a file', fileNote: 'The file holds her care and when it was given. It never holds your town.', bringBack: 'Bring her back from a file', - broughtBack: (n: number): string => (n === 1 ? 'She is back, with 1 act of care.' : `She is back, with ${n} acts of care.`), + broughtBack: (n: number): string => + n === 0 ? 'She is back.' : n === 1 ? 'She is back, with 1 act of care.' : `She is back, with ${n} acts of care.`, alreadyHere: 'Everything in that file is already here.', notARose: 'That file does not hold a rose.' } diff --git a/packages/ui/src/rose/KeepSafe.svelte b/packages/ui/src/rose/KeepSafe.svelte index 87a885f..4b3366c 100644 --- a/packages/ui/src/rose/KeepSafe.svelte +++ b/packages/ui/src/rose/KeepSafe.svelte @@ -49,8 +49,8 @@ const file = input.files?.[0]; if (!file) return; try { - const { added } = await wiring.importRose(JSON.parse(await file.text()), currentKey); - said = added === 0 ? en.rose.safe.alreadyHere : en.rose.safe.broughtBack(added); + const { added, petals } = await wiring.importRose(JSON.parse(await file.text()), currentKey); + said = added === 0 && petals === 0 ? en.rose.safe.alreadyHere : en.rose.safe.broughtBack(added); onrestored(); } catch (error) { // A wrong file is an ordinary thing to choose; the player is told calmly, the console fully. diff --git a/packages/ui/src/rose/Petal.svelte b/packages/ui/src/rose/Petal.svelte new file mode 100644 index 0000000..6c12538 --- /dev/null +++ b/packages/ui/src/rose/Petal.svelte @@ -0,0 +1,128 @@ + + +{#if petal.kind === 'folded'} +
+ {en.rose.petal.folded} + +
+{:else if petal.kind === 'open' && today} +
+ + {#if showing} +
+
+
{today.text.en}
+
{source(today)}
+
+ {#if remembered} +

{en.rose.petal.remember}

+
+
{remembered.text.en}
+
{source(remembered)}
+
+ {/if} +
+ {/if} +
+{/if} + + diff --git a/packages/ui/src/rose/Rose.svelte b/packages/ui/src/rose/Rose.svelte index b022a16..40a86d8 100644 --- a/packages/ui/src/rose/Rose.svelte +++ b/packages/ui/src/rose/Rose.svelte @@ -1,8 +1,12 @@