[app] SunsetPlace: Wire the town list, the guess and storage into the garden; e2e and offline cover it
This commit is contained in:
@@ -10,7 +10,9 @@
|
||||
"typecheck": "svelte-check --threshold error --output human"
|
||||
},
|
||||
"dependencies": {
|
||||
"@rosea/content": "workspace:*",
|
||||
"@rosea/schema": "workspace:*",
|
||||
"@rosea/storage": "workspace:*",
|
||||
"@rosea/ui": "workspace:*"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
||||
@@ -1,7 +1,31 @@
|
||||
<script lang="ts">
|
||||
import { Shell } from '@rosea/ui';
|
||||
import { guessTown } from '@rosea/content/guess';
|
||||
import { openProfile } from '@rosea/storage';
|
||||
import { Shell, type SunsetWiring } from '@rosea/ui';
|
||||
// Bundled at build time, so the letter is precached and opens offline (letter-slot.md L1).
|
||||
import letter from '../../../docs/LETTER.md?raw';
|
||||
|
||||
// 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.
|
||||
const deviceZone = new Intl.DateTimeFormat().resolvedOptions().timeZone;
|
||||
const profile = openProfile();
|
||||
|
||||
// The town list is large, so it loads on the first search (sunset-place.md P5).
|
||||
// A failed load is forgotten, so the next keystroke tries again.
|
||||
let towns: Promise<typeof import('@rosea/content/places')> | undefined;
|
||||
const loadTowns = (): Promise<typeof import('@rosea/content/places')> =>
|
||||
(towns ??= import('@rosea/content/places').catch((error: unknown) => {
|
||||
towns = undefined;
|
||||
throw error;
|
||||
}));
|
||||
|
||||
const sunset: SunsetWiring = {
|
||||
guess: guessTown(deviceZone),
|
||||
search: async (query) => (await loadTowns()).searchTowns(query),
|
||||
load: () => profile.loadSunsetPlace(),
|
||||
save: (choice) => profile.saveSunsetPlace(choice),
|
||||
now: () => Date.now()
|
||||
};
|
||||
</script>
|
||||
|
||||
<Shell {letter} />
|
||||
<Shell {letter} {sunset} />
|
||||
|
||||
+21
-1
@@ -2,6 +2,7 @@ import { expect, test } from '@playwright/test';
|
||||
import { PRODUCT_NAME } from '@rosea/schema';
|
||||
import { en } from '@rosea/ui/copy';
|
||||
import { startStaticServer } from './static-server.js';
|
||||
import { storedTown } from './stored.js';
|
||||
|
||||
/**
|
||||
* The offline suite. CLAUDE.md §3 describes its full shape: tend the rose, walk
|
||||
@@ -14,14 +15,22 @@ import { startStaticServer } from './static-server.js';
|
||||
*/
|
||||
test('the shell opens from the service worker with the server gone', async ({ browser }) => {
|
||||
const server = await startStaticServer();
|
||||
const context = await browser.newContext();
|
||||
// A fixed place and day, so the sunset line is the same on every run.
|
||||
const context = await browser.newContext({ timezoneId: 'Europe/Prague', locale: 'en-US' });
|
||||
let serverOpen = true;
|
||||
|
||||
try {
|
||||
const page = await context.newPage();
|
||||
await page.clock.setFixedTime(Date.parse('2026-03-29T12:00:00+02:00'));
|
||||
await page.goto(server.url);
|
||||
await expect(page.getByRole('heading', { level: 1 })).toHaveText(PRODUCT_NAME);
|
||||
|
||||
// The player answers the sunset question while online (sunset-place.md).
|
||||
await page.getByRole('searchbox', { name: en.sunset.searchLabel }).fill('brno');
|
||||
await page.getByRole('button', { name: /^Brno/ }).first().click();
|
||||
await expect(page.locator('.sun p').first()).toHaveText(/^Tonight the sun sets at .* in Brno\.$/);
|
||||
await storedTown(page, 'Brno');
|
||||
|
||||
// `clientsClaim` makes the FIRST worker control the page that registered it.
|
||||
// Without it, a player who opens the link once and loses signal has nothing.
|
||||
await page.waitForFunction(() => navigator.serviceWorker.controller !== null, null, { timeout: 20_000 });
|
||||
@@ -47,13 +56,24 @@ test('the shell opens from the service worker with the server gone', async ({ br
|
||||
await page.reload();
|
||||
await expect(page.getByRole('heading', { level: 1 })).toHaveText(PRODUCT_NAME);
|
||||
await expect(page.getByText(en.shell.preparing)).toBeVisible();
|
||||
// The town survived in IndexedDB, and the sun is computed with no network.
|
||||
await expect(page.locator('.sun p').first()).toHaveText(/^Tonight the sun sets at .* in Brno\.$/);
|
||||
|
||||
// Changing the town offline: the town list was precached with the shell.
|
||||
await page.getByRole('button', { name: en.sunset.change }).click();
|
||||
await page.getByRole('searchbox').fill('plzen');
|
||||
await page.getByRole('button', { name: /^Plzeň/ }).first().click();
|
||||
await expect(page.locator('.sun p').first()).toHaveText(/in Plzeň\.$/);
|
||||
await storedTown(page, 'Plzeň');
|
||||
|
||||
// 2. Close the tab and open a fresh one: a cold start, the shape of a player
|
||||
// coming back days later with no signal.
|
||||
await page.close();
|
||||
const again = await context.newPage();
|
||||
await again.clock.setFixedTime(Date.parse('2026-03-29T12:00:00+02:00'));
|
||||
await again.goto(server.url);
|
||||
await expect(again.getByRole('heading', { level: 1 })).toHaveText(PRODUCT_NAME);
|
||||
await expect(again.locator('.sun p').first()).toHaveText(/in Plzeň\.$/);
|
||||
|
||||
// 3. A deep link inside the scope falls back to the shell, not an error page.
|
||||
await again.goto(`${server.url}somewhere/deeper`);
|
||||
|
||||
@@ -3,8 +3,11 @@ import { expect, test, type Page } from '@playwright/test';
|
||||
import { PRODUCT_NAME } from '@rosea/schema';
|
||||
import { en } from '@rosea/ui/copy';
|
||||
import { parseLetter } from '@rosea/ui/letter';
|
||||
import { nextInvitation } from '@rosea/engine';
|
||||
import { searchTowns } from '@rosea/content/places';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { BASE, startStaticServer, type StaticServer } from './static-server.js';
|
||||
import { storedTown } from './stored.js';
|
||||
|
||||
let server: StaticServer;
|
||||
|
||||
@@ -127,3 +130,55 @@ test('the Letter: no way to it while empty, and it opens from the garden once wr
|
||||
await page.getByRole('link', { name: en.letter.back }).click();
|
||||
await expect(page.getByRole('heading', { level: 1 })).toHaveText(PRODUCT_NAME);
|
||||
});
|
||||
|
||||
// --- Where the sunset is (.agents/features/sunset-place.md) ------------------
|
||||
test.describe('the sunset place, in Prague\'s time zone at noon on 29 March 2026', () => {
|
||||
test.use({ timezoneId: 'Europe/Prague', locale: 'en-US' });
|
||||
const NOON = Date.parse('2026-03-29T12:00:00+02:00');
|
||||
/** Browsers and Node both put a narrow no-break space before PM; compare words, not spaces. */
|
||||
const spaced = (s: string): string => s.replace(/\s+/g, ' ');
|
||||
|
||||
test('first open asks; choosing Brno shows the sunset the engine computes; a reload keeps it', async ({ page }) => {
|
||||
await page.clock.setFixedTime(NOON);
|
||||
const { requests } = await open(page);
|
||||
await expect(page.getByRole('heading', { level: 2, name: en.sunset.question })).toBeVisible();
|
||||
|
||||
await page.getByRole('searchbox', { name: en.sunset.searchLabel }).fill('brno');
|
||||
await page.getByRole('button', { name: /^Brno/ }).first().click();
|
||||
|
||||
const brno = searchTowns('brno')[0]!;
|
||||
const { at } = nextInvitation(NOON, brno);
|
||||
const time = new Intl.DateTimeFormat('en-US', { hour: 'numeric', minute: '2-digit', timeZone: brno.timeZone }).format(at);
|
||||
const line = page.locator('.sun p').first();
|
||||
await expect(line).toHaveText(/in Brno\.$/);
|
||||
expect(spaced((await line.textContent()) ?? '')).toBe(spaced(en.sunset.tonight(time, 'Brno')));
|
||||
|
||||
await storedTown(page, 'Brno');
|
||||
await page.reload();
|
||||
await expect(line).toHaveText(/in Brno\.$/);
|
||||
await expect(page.getByRole('searchbox')).toHaveCount(0);
|
||||
|
||||
// Searching and choosing loaded the town list from this origin and nothing else.
|
||||
expect(requests.filter((url) => !url.startsWith(server.origin + '/'))).toEqual([]);
|
||||
expect(requests.some((url) => /\/assets\/places-.*\.js$/.test(url))).toBe(true);
|
||||
});
|
||||
|
||||
test('"not now" guesses Prague from the device\'s zone and says it is a guess', async ({ page }) => {
|
||||
await page.clock.setFixedTime(NOON);
|
||||
await open(page);
|
||||
await page.getByRole('button', { name: en.sunset.skip('Prague') }).click();
|
||||
await expect(page.locator('.sun p').first()).toHaveText(/^Tonight the sun sets at .* in Prague\.$/);
|
||||
await expect(page.getByText(en.sunset.guessed)).toBeVisible();
|
||||
});
|
||||
|
||||
test('the question and the answer have no accessibility violations', async ({ page }) => {
|
||||
await page.clock.setFixedTime(NOON);
|
||||
await open(page);
|
||||
await page.getByRole('searchbox').fill('pra');
|
||||
await expect(page.getByRole('status')).not.toHaveText('');
|
||||
expect((await new AxeBuilder({ page }).analyze()).violations.map((v) => `${v.id}: ${v.help}`)).toEqual([]);
|
||||
await page.getByRole('button', { name: /^Prague/ }).first().click();
|
||||
await expect(page.locator('.sun')).toBeVisible();
|
||||
expect((await new AxeBuilder({ page }).analyze()).violations.map((v) => `${v.id}: ${v.help}`)).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
import type { Page } from '@playwright/test';
|
||||
|
||||
/**
|
||||
* Waits until the player's sunset town is written to IndexedDB. The garden shows a
|
||||
* choice before the write commits, so a test that reloads or closes the tab at once
|
||||
* races the write: it passed and failed on alternate runs before this existed.
|
||||
*/
|
||||
export async function storedTown(page: Page, name: string): Promise<void> {
|
||||
await page.waitForFunction(
|
||||
(expected) =>
|
||||
new Promise<boolean>((resolve) => {
|
||||
const req = indexedDB.open('rosea');
|
||||
req.onerror = () => resolve(false);
|
||||
req.onsuccess = () => {
|
||||
const db = req.result;
|
||||
if (!db.objectStoreNames.contains('profile')) {
|
||||
db.close();
|
||||
resolve(false);
|
||||
return;
|
||||
}
|
||||
const get = db.transaction('profile').objectStore('profile').get('sunsetPlace');
|
||||
get.onsuccess = () => {
|
||||
db.close();
|
||||
const value = get.result as { value?: { town?: { name?: string } } } | undefined;
|
||||
resolve(value?.value?.town?.name === expected);
|
||||
};
|
||||
get.onerror = () => {
|
||||
db.close();
|
||||
resolve(false);
|
||||
};
|
||||
};
|
||||
}),
|
||||
name,
|
||||
{ timeout: 5_000 }
|
||||
);
|
||||
}
|
||||
@@ -25,6 +25,7 @@
|
||||
"@eslint/js": "9.39.5",
|
||||
"@playwright/test": "1.62.1",
|
||||
"@rosea/content": "workspace:*",
|
||||
"@rosea/engine": "workspace:*",
|
||||
"@rosea/schema": "workspace:*",
|
||||
"@rosea/ui": "workspace:*",
|
||||
"@types/node": "22.20.1",
|
||||
|
||||
Generated
+42
@@ -20,6 +20,9 @@ importers:
|
||||
'@rosea/content':
|
||||
specifier: workspace:*
|
||||
version: link:packages/content
|
||||
'@rosea/engine':
|
||||
specifier: workspace:*
|
||||
version: link:packages/engine
|
||||
'@rosea/schema':
|
||||
specifier: workspace:*
|
||||
version: link:packages/schema
|
||||
@@ -62,9 +65,15 @@ importers:
|
||||
|
||||
apps/rosea:
|
||||
dependencies:
|
||||
'@rosea/content':
|
||||
specifier: workspace:*
|
||||
version: link:../../packages/content
|
||||
'@rosea/schema':
|
||||
specifier: workspace:*
|
||||
version: link:../../packages/schema
|
||||
'@rosea/storage':
|
||||
specifier: workspace:*
|
||||
version: link:../../packages/storage
|
||||
'@rosea/ui':
|
||||
specifier: workspace:*
|
||||
version: link:../../packages/ui
|
||||
@@ -136,8 +145,30 @@ importers:
|
||||
specifier: 3.2.7
|
||||
version: 3.2.7(@types/node@22.20.1)(jsdom@25.0.1)(terser@5.51.2)(tsx@4.23.12)
|
||||
|
||||
packages/storage:
|
||||
dependencies:
|
||||
'@rosea/engine':
|
||||
specifier: workspace:*
|
||||
version: link:../engine
|
||||
dexie:
|
||||
specifier: 4.4.6
|
||||
version: 4.4.6
|
||||
devDependencies:
|
||||
fake-indexeddb:
|
||||
specifier: 6.2.5
|
||||
version: 6.2.5
|
||||
typescript:
|
||||
specifier: 5.9.3
|
||||
version: 5.9.3
|
||||
vitest:
|
||||
specifier: 3.2.7
|
||||
version: 3.2.7(@types/node@22.20.1)(jsdom@25.0.1)(terser@5.51.2)(tsx@4.23.12)
|
||||
|
||||
packages/ui:
|
||||
dependencies:
|
||||
'@rosea/engine':
|
||||
specifier: workspace:*
|
||||
version: link:../engine
|
||||
'@rosea/schema':
|
||||
specifier: workspace:*
|
||||
version: link:../schema
|
||||
@@ -1924,6 +1955,9 @@ packages:
|
||||
devalue@5.9.4:
|
||||
resolution: {integrity: sha512-sPAT4pztbu6586/hrhOnMKS17IJrvg12mXiSPSS3W5qDeN2RGgvZ0diZCm31dBbnevfVmujNO3IM2wrS4Y2Rhg==}
|
||||
|
||||
dexie@4.4.6:
|
||||
resolution: {integrity: sha512-hJP/BO6mjB+tX6hToIO1kmxYLNmun90wYbfcoAoLpKEyDYal/k33dg0TAfoUe2TDsbbLoVIzljJ3BN2UbR5EOg==}
|
||||
|
||||
dom-accessibility-api@0.5.16:
|
||||
resolution: {integrity: sha512-X7BJ2yElsnOJ30pZF4uIIDfBEVgF4XEBxL9Bxhy6dnrm5hkzqmsWHGTiHqRiITNhMyFLyAiWndIJP7Z1NTteDg==}
|
||||
|
||||
@@ -2074,6 +2108,10 @@ packages:
|
||||
resolution: {integrity: sha512-KfYbmpRm0VbLjEvVa9yGwCi9GI34xvi7A/HXYWQO65CSD2u3MczUJSuwXKFIxlGsgBQizV9q5J9NHj4VG0n+pA==}
|
||||
engines: {node: '>=12.0.0'}
|
||||
|
||||
fake-indexeddb@6.2.5:
|
||||
resolution: {integrity: sha512-CGnyrvbhPlWYMngksqrSSUT1BAVP49dZocrHuK0SvtR0D5TMs5wP0o3j7jexDJW01KSadjBp1M/71o/KR3nD1w==}
|
||||
engines: {node: '>=18'}
|
||||
|
||||
fast-check@3.23.2:
|
||||
resolution: {integrity: sha512-h5+1OzzfCC3Ef7VbtKdcv7zsstUQwUDlYpUTvjeUsJAssPgLn7QzbboPtL5ro04Mq0rPOsMzl7q5hIbRs2wD1A==}
|
||||
engines: {node: '>=8.0.0'}
|
||||
@@ -4990,6 +5028,8 @@ snapshots:
|
||||
|
||||
devalue@5.9.4: {}
|
||||
|
||||
dexie@4.4.6: {}
|
||||
|
||||
dom-accessibility-api@0.5.16: {}
|
||||
|
||||
dunder-proto@1.0.1:
|
||||
@@ -5262,6 +5302,8 @@ snapshots:
|
||||
|
||||
expect-type@1.4.0: {}
|
||||
|
||||
fake-indexeddb@6.2.5: {}
|
||||
|
||||
fast-check@3.23.2:
|
||||
dependencies:
|
||||
pure-rand: 6.1.0
|
||||
|
||||
Reference in New Issue
Block a user