From 77a865f625747b450478f4a5aff4f1d36c8bbc03 Mon Sep 17 00:00:00 2001 From: mnovak Date: Mon, 2 Nov 2026 00:01:51 +0100 Subject: [PATCH] [ui] SunsetPlace: Ask where the sunset is, and say when tonight's sunset is --- packages/ui/package.json | 1 + packages/ui/src/Shell.svelte | 116 ++++++++++++- packages/ui/src/copy/en.ts | 21 +++ packages/ui/src/index.ts | 1 + packages/ui/src/sunset/SunsetLine.svelte | 46 ++++++ packages/ui/src/sunset/SunsetPlace.svelte | 189 ++++++++++++++++++++++ packages/ui/src/sunset/types.ts | 19 +++ packages/ui/test/sunset.test.ts | 135 ++++++++++++++++ 8 files changed, 526 insertions(+), 2 deletions(-) create mode 100644 packages/ui/src/sunset/SunsetLine.svelte create mode 100644 packages/ui/src/sunset/SunsetPlace.svelte create mode 100644 packages/ui/src/sunset/types.ts create mode 100644 packages/ui/test/sunset.test.ts diff --git a/packages/ui/package.json b/packages/ui/package.json index 841e212..aa51acb 100644 --- a/packages/ui/package.json +++ b/packages/ui/package.json @@ -23,6 +23,7 @@ "test": "vitest run --reporter=verbose" }, "dependencies": { + "@rosea/engine": "workspace:*", "@rosea/schema": "workspace:*" }, "peerDependencies": { diff --git a/packages/ui/src/Shell.svelte b/packages/ui/src/Shell.svelte index 7da33af..d34a1fb 100644 --- a/packages/ui/src/Shell.svelte +++ b/packages/ui/src/Shell.svelte @@ -1,11 +1,71 @@ + +

{line}

+{#if guessed} +

{en.sunset.guessed}

+{/if} + + diff --git a/packages/ui/src/sunset/SunsetPlace.svelte b/packages/ui/src/sunset/SunsetPlace.svelte new file mode 100644 index 0000000..87b3fb6 --- /dev/null +++ b/packages/ui/src/sunset/SunsetPlace.svelte @@ -0,0 +1,189 @@ + + +
+

{en.sunset.question}

+

{en.sunset.why}

+ + + +

{query.trim() === '' ? '' : en.sunset.found(results.length)}

+ + {#if results.length > 0} + + {/if} + +
+ {#if current && oncancel} + + {/if} + +
+
+ + diff --git a/packages/ui/src/sunset/types.ts b/packages/ui/src/sunset/types.ts new file mode 100644 index 0000000..08c6e2a --- /dev/null +++ b/packages/ui/src/sunset/types.ts @@ -0,0 +1,19 @@ +import type { NamedPlace } from '@rosea/engine'; + +/** What the player answered: a town, or "not now" (the guess from the device's zone). */ +export type SunsetChoice = { readonly kind: 'town'; readonly town: NamedPlace } | { readonly kind: 'device-zone' }; + +/** + * Everything the garden needs to ask where the sunset is and to show it. The app + * provides it; ui imports neither content nor storage (CLAUDE.md §2). + */ +export interface SunsetWiring { + /** The device's guess for its own zone, or null for a zone of no place (UTC). */ + readonly guess: NamedPlace | null; + /** Towns matching a query. The app loads the town list on the first call. */ + readonly search: (query: string) => Promise; + readonly load: () => Promise; + readonly save: (choice: SunsetChoice) => Promise; + /** The clock. A parameter, so tests and the e2e suite can fix it. */ + readonly now: () => number; +} diff --git a/packages/ui/test/sunset.test.ts b/packages/ui/test/sunset.test.ts new file mode 100644 index 0000000..08ef00a --- /dev/null +++ b/packages/ui/test/sunset.test.ts @@ -0,0 +1,135 @@ +import { invitation, sunDay, type NamedPlace } from '@rosea/engine'; +import { cleanup, fireEvent, render, screen, waitFor } from '@testing-library/svelte'; +import { afterEach, describe, expect, it, vi } from 'vitest'; +import Shell from '../src/Shell.svelte'; +import { en } from '../src/copy/en.js'; +import type { SunsetChoice, SunsetWiring } from '../src/sunset/types.js'; + +const brno: NamedPlace = { name: 'Brno', region: '', country: 'Czechia', lat: 49.2, lon: 16.61, timeZone: 'Europe/Prague' }; +const prague: NamedPlace = { name: 'Prague', region: '', country: 'Czechia', lat: 50.09, lon: 14.46, timeZone: 'Europe/Prague' }; +const tromso: NamedPlace = { name: 'Tromsø', region: 'Troms', country: 'Norway', lat: 69.65, lon: 18.96, timeZone: 'Europe/Oslo' }; + +/** Noon in Prague on 29 March 2026: tonight's sunset is still to come. */ +const NOON = Date.parse('2026-03-29T12:00:00+02:00'); + +/** The time the line must show, read exactly as the component reads it. */ +const timeOf = (place: NamedPlace, at: number): string => + new Intl.DateTimeFormat(undefined, { hour: 'numeric', minute: '2-digit', timeZone: place.timeZone }).format(at); + +function wiring(over: Partial & { saved?: SunsetChoice | null } = {}): SunsetWiring & { saves: SunsetChoice[] } { + const saves: SunsetChoice[] = []; + return { + guess: prague, + search: async (q) => [brno, prague].filter((t) => t.name.toLowerCase().startsWith(q.trim().toLowerCase())), + load: async () => over.saved ?? null, + save: async (c) => { + saves.push(c); + }, + now: () => NOON, + ...over, + saves + }; +} + +afterEach(() => { + cleanup(); + vi.restoreAllMocks(); +}); + +describe('the garden asks where the sunset is', () => { + it('asks when nothing is saved, and does not take focus on first load', async () => { + render(Shell, { sunset: wiring() }); + expect(await screen.findByRole('heading', { level: 2, name: en.sunset.question })).toBeTruthy(); + expect(screen.getByRole('searchbox', { name: en.sunset.searchLabel })).toBeTruthy(); + expect(document.activeElement).toBe(document.body); + }); + + it('typing lists towns, choosing one shows its sunset, keeps it, and moves focus to the answer', async () => { + const w = wiring(); + render(Shell, { sunset: w }); + await fireEvent.input(await screen.findByRole('searchbox'), { target: { value: 'br' } }); + expect(await screen.findByRole('status')).toHaveProperty('textContent', en.sunset.found(1)); + await fireEvent.click(screen.getByRole('button', { name: /Brno/ })); + + const at = invitation(sunDay('2026-03-29', brno)); + expect(await screen.findByText(en.sunset.tonight(timeOf(brno, at), 'Brno'))).toBeTruthy(); + expect(w.saves).toEqual([{ kind: 'town', town: brno }]); + await waitFor(() => expect(document.activeElement?.className).toContain('sun')); + expect(screen.queryByText(en.sunset.guessed)).toBeNull(); + }); + + it('nothing found says a larger town nearby will do, never an error', async () => { + render(Shell, { sunset: wiring() }); + await fireEvent.input(await screen.findByRole('searchbox'), { target: { value: 'zz' } }); + await waitFor(() => expect(screen.getByRole('status').textContent).toBe(en.sunset.found(0))); + }); + + it('"not now" uses the guess, saves the device zone, and says it is a guess', async () => { + const w = wiring(); + render(Shell, { sunset: w }); + await fireEvent.click(await screen.findByRole('button', { name: en.sunset.skip('Prague') })); + const at = invitation(sunDay('2026-03-29', prague)); + expect(await screen.findByText(en.sunset.tonight(timeOf(prague, at), 'Prague'))).toBeTruthy(); + expect(screen.getByText(en.sunset.guessed)).toBeTruthy(); + expect(w.saves).toEqual([{ kind: 'device-zone' }]); + }); + + it('with no guess (a UTC clock), "not now" asks the player to choose a town', async () => { + render(Shell, { sunset: wiring({ guess: null }) }); + await fireEvent.click(await screen.findByRole('button', { name: en.sunset.skipNoGuess })); + expect(await screen.findByText(en.sunset.noTown)).toBeTruthy(); + expect(screen.getByRole('button', { name: en.sunset.choose })).toBeTruthy(); + }); +}); + +describe('the garden remembers', () => { + it('a saved town shows its sunset at once, with no question', async () => { + render(Shell, { sunset: wiring({ saved: { kind: 'town', town: brno } }) }); + expect(await screen.findByText(/in Brno\.$/)).toBeTruthy(); + expect(screen.queryByRole('searchbox')).toBeNull(); + }); + + it('after the sunset, it speaks of tomorrow\'s', async () => { + const after = invitation(sunDay('2026-03-29', brno)) + 60_000; + render(Shell, { sunset: wiring({ saved: { kind: 'town', town: brno }, now: () => after }) }); + const tomorrow = invitation(sunDay('2026-03-30', brno)); + expect(await screen.findByText(en.sunset.tomorrow(timeOf(brno, tomorrow), 'Brno'))).toBeTruthy(); + }); + + it('in polar night it speaks of the noon twilight; in polar day, of the lowest light', async () => { + const winter = Date.parse('2026-12-21T08:00:00+01:00'); + render(Shell, { sunset: wiring({ saved: { kind: 'town', town: tromso }, now: () => winter }) }); + const noon = sunDay('2026-12-21', tromso).solarNoon; + expect(await screen.findByText(en.sunset.polarNight(timeOf(tromso, noon), 'Tromsø'))).toBeTruthy(); + cleanup(); + + const summer = Date.parse('2026-06-21T12:00:00+02:00'); + render(Shell, { sunset: wiring({ saved: { kind: 'town', town: tromso }, now: () => summer }) }); + expect(await screen.findByText(/In Tromsø the sun stays up tonight\. The light is lowest at/)).toBeTruthy(); + }); + + it('change town: the search opens with focus, and Keep returns without saving anything', async () => { + const w = wiring({ saved: { kind: 'town', town: brno } }); + render(Shell, { sunset: w }); + await fireEvent.click(await screen.findByRole('button', { name: en.sunset.change })); + await waitFor(() => expect(document.activeElement).toBe(screen.getByRole('searchbox'))); + await fireEvent.click(screen.getByRole('button', { name: en.sunset.keep('Brno') })); + expect(await screen.findByText(/in Brno\.$/)).toBeTruthy(); + expect(w.saves).toEqual([]); + }); +}); + +describe('when the device will not keep anything', () => { + it('a failed read still asks, and a failed save still shows the sunset and says it will ask again', async () => { + const error = vi.spyOn(console, 'error').mockImplementation(() => undefined); + const w = wiring({ + load: () => Promise.reject(new Error('IndexedDB blocked')), + save: () => Promise.reject(new Error('IndexedDB blocked')) + }); + render(Shell, { sunset: w }); + await fireEvent.click(await screen.findByRole('button', { name: en.sunset.skip('Prague') })); + expect(await screen.findByText(/in Prague\.$/)).toBeTruthy(); + expect(await screen.findByText(en.sunset.notKept)).toBeTruthy(); + expect(error).toHaveBeenCalled(); // never swallowed + }); +});