From 5576af4fac004b468e88a7b6f51b5fba6d3295b8 Mon Sep 17 00:00:00 2001 From: mnovak Date: Tue, 17 Nov 2026 19:07:57 +0100 Subject: [PATCH] [ui] PlanetGarden: Her round planet, the player's gardener, the ring of care, and care by gesture --- apps/rosea/src/App.svelte | 2 + packages/engine/src/index.ts | 2 +- packages/engine/src/sun/sun.ts | 21 + packages/engine/test/sun.test.ts | 26 +- packages/ui/src/Shell.svelte | 63 ++- packages/ui/src/copy/en.ts | 36 ++ packages/ui/src/rose/Rose.svelte | 165 +++++--- packages/ui/src/rose/types.ts | 7 +- packages/ui/src/scene/Gardener.svelte | 110 +++++ packages/ui/src/scene/GardenerChoice.svelte | 177 +++++++++ packages/ui/src/scene/Planet.svelte | 419 ++++++++++++++++++++ packages/ui/src/scene/look.ts | 4 + packages/ui/test/gardener.test.ts | 81 ++++ packages/ui/test/petal.test.ts | 2 +- packages/ui/test/planet.test.ts | 187 +++++++++ packages/ui/test/rose.test.ts | 24 +- packages/ui/test/touch.test.ts | 19 +- packages/ui/test/two-tabs.test.ts | 2 + 18 files changed, 1268 insertions(+), 79 deletions(-) create mode 100644 packages/ui/src/scene/Gardener.svelte create mode 100644 packages/ui/src/scene/GardenerChoice.svelte create mode 100644 packages/ui/src/scene/Planet.svelte create mode 100644 packages/ui/src/scene/look.ts create mode 100644 packages/ui/test/gardener.test.ts create mode 100644 packages/ui/test/planet.test.ts diff --git a/apps/rosea/src/App.svelte b/apps/rosea/src/App.svelte index ab70679..71955db 100644 --- a/apps/rosea/src/App.svelte +++ b/apps/rosea/src/App.svelte @@ -99,6 +99,8 @@ sentences, loadOpened: () => storage.loadOpened(), appendOpened: (event) => storage.appendOpened(event), + // The player's gardener, chosen at the first open (planet-garden.md G6). + gardener: { load: () => storage.loadGardener(), save: (look) => storage.saveGardener(look) }, // Another tab gave care, opened a petal or brought her back (two-tabs.md). changes: (listener) => storage.onChange((what) => what === 'log' && listener()), clientVersion: version, diff --git a/packages/engine/src/index.ts b/packages/engine/src/index.ts index f2aa862..0b29bc6 100644 --- a/packages/engine/src/index.ts +++ b/packages/engine/src/index.ts @@ -3,7 +3,7 @@ export { careEvent, careIdentity, judgeCare, newCare, OWN_TRACE, petalsOn, pour, ROSE, roseAt, seeded, stageFor, weedsOn, windOn, type Judged, type Mood, type Needs, type RoseKeys, type RoseNow, type Stage, type Trend } from './rose/rose.js'; -export { invitation, nextInvitation, nextSunEvent, sunDay, type NamedPlace, type Place, type SunDay } from './sun/sun.js'; +export { invitation, lightAt, nextInvitation, nextSunEvent, sunDay, type Light, type NamedPlace, type Place, type SunDay } from './sun/sun.js'; export { goodMoment, newOpened, openedIdentity, PETAL, petalAt, petalOrder, sentenceOpened, type PetalNow, type PetalSentence } from './petal/petal.js'; diff --git a/packages/engine/src/sun/sun.ts b/packages/engine/src/sun/sun.ts index 72309d8..e82cec6 100644 --- a/packages/engine/src/sun/sun.ts +++ b/packages/engine/src/sun/sun.ts @@ -159,6 +159,27 @@ export function nextInvitation(now: number, place: Place): { readonly sun: SunDa throw new Error(`nextInvitation: no invitation within two days of ${today}`); } +/** The light over her planet now (planet-garden.md): the scene's sky follows the real sun. */ +export type Light = 'day' | 'golden' | 'night'; + +/** + * Golden within 45 minutes before and 30 after the sunset or sunrise (in polar day, around the + * lowest light; in polar night, around the brightest twilight at noon); day between, night outside. + */ +export function lightAt(now: number, place: Place): Light { + const sun = sunDay(localDay(now, place.timeZone), place); + const near = (at: number, before: number, after: number): boolean => now >= at - before * MINUTE && now <= at + after * MINUTE; + switch (sun.kind) { + case 'polar-day': + return near(sun.lowestLight, 60, 60) ? 'golden' : 'day'; + case 'polar-night': + return near(sun.solarNoon, 90, 90) ? 'golden' : 'night'; + case 'sun-sets': + if (near(sun.sunset, 45, 30) || near(sun.sunrise, 30, 30)) return 'golden'; + return now > sun.sunrise && now < sun.sunset ? 'day' : 'night'; + } +} + /** * The next thing to look up for (sunrise.md R1): the sunrise of the next invitation's * day while it is still ahead, otherwise the invitation itself. Built on diff --git a/packages/engine/test/sun.test.ts b/packages/engine/test/sun.test.ts index 2fd582c..92aa84a 100644 --- a/packages/engine/test/sun.test.ts +++ b/packages/engine/test/sun.test.ts @@ -1,7 +1,7 @@ import fc from 'fast-check'; import { readFileSync } from 'node:fs'; import { describe, expect, it } from 'vitest'; -import { invitation, nextInvitation, nextSunEvent, sunDay, type Place, type SunDay } from '../src/sun/sun.js'; +import { invitation, lightAt, nextInvitation, nextSunEvent, sunDay, type Place, type SunDay } from '../src/sun/sun.js'; import { addDays, localDay } from '../src/time/localDay.js'; /** Recorded by scripts/fetch-sun-golden.ts from the U.S. Naval Observatory. */ @@ -317,3 +317,27 @@ describe('nextSunEvent — the next thing to look up for (sunrise.md)', () => { ); }); }); + +describe('lightAt — the sky over her planet (planet-garden.md)', () => { + const brno: Place = { lat: 49.1951, lon: 16.6068, timeZone: 'Europe/Prague' }; + const day = sunDay('2026-03-02', brno); + if (day.kind !== 'sun-sets') throw new Error('Brno has a sunset in March'); + + it('night before dawn, golden at sunrise, day at noon, golden at sunset, night after', () => { + expect(lightAt(day.sunrise - 2 * HOUR, brno)).toBe('night'); + expect(lightAt(day.sunrise, brno)).toBe('golden'); + expect(lightAt(day.solarNoon, brno)).toBe('day'); + expect(lightAt(day.sunset - 30 * MINUTE, brno)).toBe('golden'); + expect(lightAt(day.sunset + 2 * HOUR, brno)).toBe('night'); + }); + + it('polar day is day, golden around the lowest light; polar night is night, golden at noon', () => { + const tromso: Place = { lat: 69.6492, lon: 18.9553, timeZone: 'Europe/Oslo' }; + const summer = sunDay('2026-06-21', tromso); + const winter = sunDay('2026-12-21', tromso); + if (summer.kind !== 'polar-day' || winter.kind !== 'polar-night') throw new Error('Tromsø: polar day and night'); + expect(lightAt(summer.solarNoon, tromso)).toBe('day'); + expect(lightAt(winter.solarNoon, tromso)).toBe('golden'); + expect(lightAt(winter.solarNoon + 4 * HOUR, tromso)).toBe('night'); + }); +}); diff --git a/packages/ui/src/Shell.svelte b/packages/ui/src/Shell.svelte index 6996b68..4f0358f 100644 --- a/packages/ui/src/Shell.svelte +++ b/packages/ui/src/Shell.svelte @@ -1,5 +1,5 @@ + + + + + + diff --git a/packages/ui/src/scene/GardenerChoice.svelte b/packages/ui/src/scene/GardenerChoice.svelte new file mode 100644 index 0000000..370f757 --- /dev/null +++ b/packages/ui/src/scene/GardenerChoice.svelte @@ -0,0 +1,177 @@ + + +
+

{en.gardener.question}

+

{en.gardener.why}

+ + + + +
{ + e.preventDefault(); + onchoose({ ...look }); + }} + > + {#each GARDENER_FIELDS as field (field)} +
+ {en.gardener.labels[field]} +
+ {#each GARDENER_OPTIONS[field] as value (value)} + + {/each} +
+
+ {/each} +
+ + {#if oncancel}{/if} +
+
+
+ + diff --git a/packages/ui/src/scene/Planet.svelte b/packages/ui/src/scene/Planet.svelte new file mode 100644 index 0000000..8245834 --- /dev/null +++ b/packages/ui/src/scene/Planet.svelte @@ -0,0 +1,419 @@ + + + + +
+ { + if (petals > 0) start(e, { kind: 'sweep', samples: [point(e)] }); + }} + onpointermove={move} + onpointerup={end} + onpointercancel={() => { + if (drag?.kind === 'can') onpourend(); + drag = null; + windbreakAt = null; + }} + > + + + + + + + + + + + + + + {#if wind} + + {/if} + + + + + {#each [-58, -52, -46, 46, 52] as a (a)} + {@const s = surface(a, 3)} + + {/each} + + + + + + start(e, { kind: 'rose', samples: [point(e)] })} + onkeydown={(e) => { + if (e.key === 'Enter' || e.key === ' ') { + e.preventDefault(); + if (!e.repeat) onrose(); + } + }} + /> + + + {#each WEED_AT.slice(0, weeds) as a, i (a)} + {#if !pulled.has(i)} + {@const s = surface(a, 1)} + start(e, { kind: 'weed', index: i, samples: [point(e)] })} + > + + + + {/if} + {/each} + + + {#each PETAL_AT.slice(0, petals) as a (a)} + {@const s = surface(a, 2)} + + {/each} + + + {#if wind} + + {@const at = windbreakAt ?? resting} + start(e, { kind: 'windbreak', samples: [point(e)] })} + > + + {#each [-12, -6, 0, 6, 12] as x (x)}{/each} + + {/if} + + + {#if !pouring} + start(e, { kind: 'can', samples: [point(e)] })} + > + + + + + {:else} + {@const p = surface(-6, 40)} + + {/if} + + + + + + + + +

{said}

+
+ + diff --git a/packages/ui/src/scene/look.ts b/packages/ui/src/scene/look.ts new file mode 100644 index 0000000..f5c6700 --- /dev/null +++ b/packages/ui/src/scene/look.ts @@ -0,0 +1,4 @@ +import type { Gardener } from '@rosea/schema'; + +/** The gardener the choice starts from, and who tends her if the device could not keep a choice. */ +export const FIRST_LOOK: Gardener = { presentation: 'girl', skin: 'skin-3', hair: 'braids', hairColour: 'brown', clothes: 'sage', hat: 'straw-hat' }; diff --git a/packages/ui/test/gardener.test.ts b/packages/ui/test/gardener.test.ts new file mode 100644 index 0000000..6a8c0d5 --- /dev/null +++ b/packages/ui/test/gardener.test.ts @@ -0,0 +1,81 @@ +import type { Gardener } from '@rosea/schema'; +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 { RoseWiring } from '../src/rose/types.js'; + +const ZONE = 'Europe/Prague'; +const MORNING = Date.parse('2026-03-01T09:00:00+01:00'); +const saved: Gardener = { presentation: 'boy', skin: 'skin-5', hair: 'curly', hairColour: 'black', clothes: 'dusk', hat: 'cap' }; + +function rose(gardener: RoseWiring['gardener'] | undefined): RoseWiring { + return { + identity: async () => ({ publicKey: 'M'.repeat(87), kept: true }), + loadCare: async () => [], + appendCare: async () => undefined, + sentences: [], + loadOpened: async () => [], + appendOpened: async () => undefined, + clientVersion: '0.1.0', + deviceZone: ZONE, + now: () => MORNING, + pastKeys: async () => [], + exportRose: async () => ({}), + importRose: async () => ({ added: 0, petals: 0 }), + saveFile: () => undefined, + keeping: { persisted: async () => false, persist: async () => true }, + installHint: false, + ...(gardener ? { gardener } : {}) + }; +} + +afterEach(() => { + vi.restoreAllMocks(); + cleanup(); +}); + +describe('who tends her (planet-garden.md §1)', () => { + it('the first open asks before the garden; the choice is kept and the garden opens', async () => { + const saves: Gardener[] = []; + render(Shell, { rose: rose({ load: async () => null, save: async (g) => void saves.push(g) }) }); + expect(await screen.findByRole('heading', { level: 2, name: en.gardener.question })).toBeTruthy(); + expect(screen.queryByRole('button', { name: en.rose.care })).toBeNull(); + await fireEvent.click(screen.getByLabelText(en.gardener.values.presentation.boy)); + await fireEvent.click(screen.getByLabelText(en.gardener.values.hair.curly)); + await fireEvent.click(screen.getByLabelText(en.gardener.values.hat.cap)); + await fireEvent.click(screen.getByRole('button', { name: en.gardener.done })); + expect(saves).toEqual([{ presentation: 'boy', skin: 'skin-3', hair: 'curly', hairColour: 'brown', clothes: 'sage', hat: 'cap' }]); + expect(await screen.findByRole('button', { name: en.rose.care })).toBeTruthy(); + expect(screen.queryByRole('heading', { name: en.gardener.question })).toBeNull(); + }); + + it('a saved gardener: no question; Change gardener opens it with their choices, and Close keeps them', async () => { + const save = vi.fn(async () => undefined); + render(Shell, { rose: rose({ load: async () => saved, save }) }); + await screen.findByRole('button', { name: en.rose.care }); + expect(screen.queryByRole('heading', { name: en.gardener.question })).toBeNull(); + await fireEvent.click(screen.getByRole('button', { name: en.gardener.change })); + expect((screen.getByLabelText(en.gardener.values.presentation.boy) as HTMLInputElement).checked).toBe(true); + expect((screen.getByLabelText(en.gardener.values.skin['skin-5']) as HTMLInputElement).checked).toBe(true); + await fireEvent.click(screen.getByRole('button', { name: en.scene.close })); + expect(await screen.findByRole('button', { name: en.rose.care })).toBeTruthy(); + expect(save).not.toHaveBeenCalled(); + }); + + it('a device that cannot keep the choice: the garden still opens, and says it will ask again', async () => { + const error = vi.spyOn(console, 'error').mockImplementation(() => undefined); + render(Shell, { rose: rose({ load: async () => null, save: () => Promise.reject(new Error('IndexedDB blocked')) }) }); + await fireEvent.click(await screen.findByRole('button', { name: en.gardener.done })); + expect(await screen.findByRole('button', { name: en.rose.care })).toBeTruthy(); + await waitFor(() => expect(screen.getByText(en.gardener.notKept)).toBeTruthy()); + expect(error).toHaveBeenCalled(); + }); + + it('without a place to keep a gardener, the garden asks nothing', async () => { + render(Shell, { rose: rose(undefined) }); + expect(await screen.findByRole('button', { name: en.rose.care })).toBeTruthy(); + expect(screen.queryByRole('heading', { name: en.gardener.question })).toBeNull(); + expect(screen.queryByRole('button', { name: en.gardener.change })).toBeNull(); + }); +}); diff --git a/packages/ui/test/petal.test.ts b/packages/ui/test/petal.test.ts index 4f6b577..fe8989f 100644 --- a/packages/ui/test/petal.test.ts +++ b/packages/ui/test/petal.test.ts @@ -98,7 +98,7 @@ describe('the folded petal (day-sentence.md)', () => { it('with an empty library: no petal, and she says nothing about one', async () => { const q = quietMoment(MORNING); render(Rose, { wiring: wiring({ log: q.log, at: q.now, sentences: [] }), timeZone: ZONE }); - await screen.findByRole('button', { name: en.rose.water }); + await screen.findByRole('button', { name: en.rose.care }); expect(screen.queryByRole('button', { name: en.rose.petal.open })).toBeNull(); expect(bubble()).not.toContain('petal'); }); diff --git a/packages/ui/test/planet.test.ts b/packages/ui/test/planet.test.ts new file mode 100644 index 0000000..b94328e --- /dev/null +++ b/packages/ui/test/planet.test.ts @@ -0,0 +1,187 @@ +import { careEvent, localDay, petalsOn, petalsToday, weedsOn, weedsToday, windOn } from '@rosea/engine'; +import type { CareEvent } from '@rosea/schema'; +import { cleanup, fireEvent, render, screen } from '@testing-library/svelte'; +import { tick } from 'svelte'; +import { afterEach, describe, expect, it, vi } from 'vitest'; +import { en } from '../src/copy/en.js'; +import Rose from '../src/rose/Rose.svelte'; +import type { RoseWiring } from '../src/rose/types.js'; + +// jsdom has no PointerEvent: without it a test pointer carries no coordinates. Every browser has one. +if (typeof window.PointerEvent !== 'function') { + class PointerEventPolyfill extends MouseEvent { + readonly pointerId: number; + readonly pointerType: string; + constructor(type: string, init: PointerEventInit = {}) { + super(type, init); + this.pointerId = init.pointerId ?? 1; + this.pointerType = init.pointerType ?? 'touch'; + } + } + Object.assign(window, { PointerEvent: PointerEventPolyfill }); +} + +const ZONE = 'Europe/Prague'; +const ME = 'M'.repeat(87); +const DAY = 86_400_000; +const MINUTE = 60_000; +/** 1 March 2026, local midnight in Prague (CET, before the clocks change). */ +const MARCH = Date.parse('2026-03-01T00:00:00+01:00'); + +function wiring(at: number, log: CareEvent[] = []): RoseWiring & { appended: CareEvent[] } { + const appended: CareEvent[] = []; + return { + identity: async () => ({ publicKey: ME, kept: true }), + loadCare: async () => log, + appendCare: async (e) => void appended.push(e), + sentences: [], + loadOpened: async () => [], + appendOpened: async () => undefined, + clientVersion: '0.1.0', + deviceZone: ZONE, + now: () => at, + pastKeys: async () => [], + exportRose: async () => ({}), + importRose: async () => ({ added: 0, petals: 0 }), + saveFile: () => undefined, + keeping: { persisted: async () => false, persist: async () => true }, + installHint: false, + appended + }; +} + +/** The first morning in March for which `has` holds. */ +function morningWhen(has: (day: string) => boolean): number { + for (let i = 0; i < 27; i++) { + const at = MARCH + i * DAY + 9 * 60 * MINUTE; + if (has(localDay(at, ZONE))) return at; + } + throw new Error('no such day in March'); +} + +/** + * A pointer moving from a to b on `target`, then let go on the scene, as a finger does. jsdom stamps + * events with the real clock, so `ms` is waited for real when a gesture's speed matters. + */ +async function gesture(target: Element, a: [number, number], b: [number, number], ms = 0, steps = 6): Promise { + const scene = screen.getByRole('group', { name: en.scene.planet }); + await fireEvent.pointerDown(target, { pointerId: 1, clientX: a[0], clientY: a[1] }); + for (let i = 1; i <= steps; i++) { + if (ms > 0) await new Promise((r) => setTimeout(r, ms / steps)); + const x = a[0] + ((b[0] - a[0]) * i) / steps, y = a[1] + ((b[1] - a[1]) * i) / steps; + await fireEvent.pointerMove(scene, { pointerId: 1, clientX: x, clientY: y }); + } + await fireEvent.pointerUp(scene, { pointerId: 1, clientX: b[0], clientY: b[1] }); + await tick(); +} + +afterEach(() => { + vi.useRealTimers(); + vi.unstubAllGlobals(); + cleanup(); +}); + +describe('her planet (planet-garden.md)', () => { + it('tapping her opens the ring of what she needs, with Touch her; Escape closes it and focus comes back to her', async () => { + render(Rose, { wiring: wiring(morningWhen((d) => !weedsOn(ME, d) && !petalsOn(ME, d))), timeZone: ZONE }); + const her = await screen.findByRole('button', { name: en.rose.care }); + expect(her.getAttribute('aria-expanded')).toBe('false'); + await fireEvent.keyDown(her, { key: 'Enter' }); + await tick(); + const ring = screen.getByRole('group', { name: en.scene.ring }); + expect(her.getAttribute('aria-expanded')).toBe('true'); + expect(document.activeElement?.textContent).toBe(en.rose.water); + expect(screen.getByRole('button', { name: en.rose.touch.label })).toBeTruthy(); + expect(screen.queryByRole('button', { name: en.rose.weed })).toBeNull(); + await fireEvent.keyDown(ring, { key: 'Escape' }); + await tick(); + expect(screen.queryByRole('group', { name: en.scene.ring })).toBeNull(); + expect(document.activeElement).toBe(her); + }); + + it('weeds: each one swiped up comes out; only when the last is out is it one act of care', async () => { + const at = morningWhen((d) => weedsOn(ME, d)); + const w = wiring(at); + render(Rose, { wiring: w, timeZone: ZONE }); + await screen.findByRole('button', { name: en.rose.care }); + const n = weedsToday(ME, localDay(at, ZONE)); + expect(screen.getAllByRole('img', { name: en.scene.weed })).toHaveLength(n); + + // A sideways swipe does not pull it. + await gesture(screen.getAllByRole('img', { name: en.scene.weed })[0]!, [200, 180], [280, 175]); + expect(screen.getAllByRole('img', { name: en.scene.weed })).toHaveLength(n); + + for (let pulled = 1; pulled <= n; pulled++) { + await gesture(screen.getAllByRole('img', { name: en.scene.weed })[0]!, [200, 180], [202, 110]); + expect(w.appended).toHaveLength(pulled === n ? 1 : 0); + } + expect(w.appended[0]?.act).toBe('weed'); + }); + + it('fallen petals: one sweep across all of them gathers them; a short swipe does not', async () => { + const at = morningWhen((d) => petalsOn(ME, d) && !weedsOn(ME, d)); + // Petals fall only once she has opened: four earlier days of helpful watering. + const opened = [4, 3, 2, 1].map((d) => careEvent({ now: at - d * DAY, timeZone: ZONE, from: ME, to: ME, act: 'water', amount: 0.5, clientVersion: '0.1.0' })); + const w = wiring(at, opened); + render(Rose, { wiring: w, timeZone: ZONE }); + await screen.findByRole('button', { name: en.rose.care }); + expect(screen.getAllByRole('img', { name: en.scene.petals })).toHaveLength(petalsToday(ME, localDay(at, ZONE))); + const scene = screen.getByRole('group', { name: en.scene.planet }); + await gesture(scene, [200, 160], [230, 160]); + expect(w.appended).toEqual([]); + await gesture(scene, [180, 160], [320, 170]); + expect(w.appended.map((e) => e.act)).toEqual(['petals']); + }); + + it('wind: the windbreak dragged to her windward side is shade; dropped elsewhere it goes back', async () => { + let at = 0; + for (let i = 0; i < 27 && !at; i++) { + const day = localDay(MARCH + i * DAY + 12 * 60 * MINUTE, ZONE); + const wind = windOn(ME, day); + if (wind && !petalsOn(ME, day)) at = MARCH + i * DAY + (wind[0] + 10) * MINUTE; + } + expect(at).toBeGreaterThan(0); + const w = wiring(at); + render(Rose, { wiring: w, timeZone: ZONE }); + const windbreak = await screen.findByRole('img', { name: en.scene.windbreak }); + await gesture(windbreak, [258, 250], [330, 380]); + expect(w.appended).toEqual([]); + await gesture(screen.getByRole('img', { name: en.scene.windbreak }), [258, 250], [140, 150]); + expect(w.appended.map((e) => e.act)).toEqual(['shade']); + }); + + it('the can: pressed and tilted far, it pours faster than a steady hold', async () => { + const at = morningWhen(() => true); + const w = wiring(at); + render(Rose, { wiring: w, timeZone: ZONE }); + const can = await screen.findByRole('img', { name: en.scene.can }); + vi.useFakeTimers({ toFake: ['setInterval', 'clearInterval'] }); + const scene = screen.getByRole('group', { name: en.scene.planet }); + await fireEvent.pointerDown(can, { pointerId: 1, clientX: 100, clientY: 220 }); + await fireEvent.pointerMove(scene, { pointerId: 1, clientX: 260, clientY: 220 }); + vi.advanceTimersByTime(1000); + await fireEvent.pointerUp(scene, { pointerId: 1, clientX: 260, clientY: 220 }); + vi.useRealTimers(); + await tick(); + expect(w.appended[0]?.act).toBe('water'); + // A steady hold pours a third of a can in a second; fully tilted pours half again as fast. + expect(w.appended[0]?.amount).toBeGreaterThan(0.45); + }); + + it('a slow stroke across her petals touches her; nothing is written', async () => { + const w = wiring(morningWhen(() => true)); + render(Rose, { wiring: w, timeZone: ZONE }); + const her = await screen.findByRole('button', { name: en.rose.care }); + await gesture(her, [170, 100], [200, 104], 500); + expect(document.querySelector('.bubble')?.textContent).toBe(en.rose.touch.fond[0]); + expect(w.appended).toEqual([]); + expect(screen.queryByRole('group', { name: en.scene.ring })).toBeNull(); + }); + + it('with reduced motion the gardener does not walk', async () => { + vi.stubGlobal('matchMedia', (q: string) => ({ matches: q.includes('reduce'), media: q, addEventListener: () => undefined, removeEventListener: () => undefined })); + render(Rose, { wiring: wiring(morningWhen(() => true)), timeZone: ZONE }); + await screen.findByRole('button', { name: en.rose.care }); + expect(document.querySelector('.planet')?.classList.contains('reduced')).toBe(true); + }); +}); diff --git a/packages/ui/test/rose.test.ts b/packages/ui/test/rose.test.ts index 0690545..20d5317 100644 --- a/packages/ui/test/rose.test.ts +++ b/packages/ui/test/rose.test.ts @@ -44,8 +44,16 @@ function wiring(over: Partial & { log?: CareEvent[]; at?: number } = } const water = (now: number, amount: number): CareEvent => careEvent({ now, timeZone: ZONE, from: ME, to: ME, act: 'water', amount, clientVersion: '0.1.0' }); +/** Opens the ring of care, as a tap on her, or Enter on her, does (planet-garden.md G7). */ +async function openRing(): Promise { + if (screen.queryByRole('group', { name: en.scene.ring })) return; + await fireEvent.keyDown(await screen.findByRole('button', { name: en.rose.care }), { key: 'Enter' }); + await tick(); +} + /** Holds the Water button for `ms`, with the pour's own timer under control. */ async function hold(ms: number, how: 'pointer' | 'keyboard' = 'pointer'): Promise { + await openRing(); const button = screen.getByRole('button', { name: en.rose.water }); vi.useFakeTimers({ toFake: ['setInterval', 'clearInterval'] }); try { @@ -79,7 +87,7 @@ describe('the rose', () => { it('hold Water: the leaves lift while pouring; let go and it is one act of care, and she thanks you', async () => { const w = wiring(); render(Rose, { wiring: w, timeZone: ZONE }); - await screen.findByRole('img'); + await screen.findByRole('img', { name: /^The rose/ }); await hold(1500); // half a can expect(screen.getByRole('status').textContent).toBe(en.rose.leavesLift); await release(); @@ -95,7 +103,7 @@ describe('the rose', () => { it('pouring too long says that is plenty', async () => { render(Rose, { wiring: wiring({ log: [water(MORNING - 60_000, 0.5)] }), timeZone: ZONE }); - await screen.findByRole('img'); + await screen.findByRole('img', { name: /^The rose/ }); await hold(3000); expect(screen.getByRole('status').textContent).toBe(en.rose.plenty); }); @@ -103,7 +111,7 @@ describe('the rose', () => { it('pours with the keyboard too: hold Space, let go', async () => { const w = wiring(); render(Rose, { wiring: w, timeZone: ZONE }); - await screen.findByRole('img'); + await screen.findByRole('img', { name: /^The rose/ }); await hold(1200, 'keyboard'); await release('keyboard'); expect(w.appended[0]).toMatchObject({ act: 'water', amount: 0.4 }); @@ -114,6 +122,7 @@ describe('the rose', () => { while (!weedsOn(ME, localDay(day, ZONE))) day += DAY; const w = wiring({ at: day }); render(Rose, { wiring: w, timeZone: ZONE }); + await openRing(); await fireEvent.click(await screen.findByRole('button', { name: en.rose.weed })); expect(w.appended[0]?.act).toBe('weed'); await waitFor(() => expect(screen.queryByRole('button', { name: en.rose.weed })).toBeNull()); @@ -122,7 +131,8 @@ describe('the rose', () => { while (weedsOn(ME, localDay(clear, ZONE))) clear += DAY; cleanup(); render(Rose, { wiring: wiring({ at: clear }), timeZone: ZONE }); - await screen.findByRole('img'); + await screen.findByRole('img', { name: /^The rose/ }); + await openRing(); expect(screen.queryByRole('button', { name: en.rose.weed })).toBeNull(); }); @@ -130,7 +140,7 @@ describe('the rose', () => { const log = [water(MORNING - 30 * DAY, 0.5)]; render(Rose, { wiring: wiring({ log }), timeZone: ZONE }); const img = await screen.findByRole('img', { name: /waiting/ }); - expect(img.className).toContain('turned'); + expect(img.getAttribute('class')).toContain('turned'); expect(screen.getByText(new RegExp(`^${en.rose.says.back}`))).toBeTruthy(); await fireEvent.click(screen.getByRole('button', { name: en.rose.numbers })); expect(document.getElementById('rose-numbers')?.textContent).toContain(en.rose.warmthDays(0)); @@ -141,7 +151,7 @@ describe('the rose', () => { const error = vi.spyOn(console, 'error').mockImplementation(() => undefined); const w = wiring({ appendCare: () => Promise.reject(new Error('IndexedDB blocked')) }); render(Rose, { wiring: w, timeZone: ZONE }); - await screen.findByRole('img'); + await screen.findByRole('img', { name: /^The rose/ }); await hold(1500); await release(); expect(await screen.findByText(en.rose.says.thanks)).toBeTruthy(); @@ -165,7 +175,7 @@ if (typeof File.prototype.text !== 'function') { describe('keep her safe (keep-her-safe.md)', () => { const OLD = 'O'.repeat(87); const open = async (): Promise => { - await screen.findByRole('img'); + await screen.findByRole('img', { name: /^The rose/ }); await fireEvent.click(screen.getByRole('button', { name: en.rose.safe.title })); }; diff --git a/packages/ui/test/touch.test.ts b/packages/ui/test/touch.test.ts index 6ba08f9..b0d6442 100644 --- a/packages/ui/test/touch.test.ts +++ b/packages/ui/test/touch.test.ts @@ -39,9 +39,18 @@ function wiring(log: CareEvent[] = []): RoseWiring & { clock: { now: number }; w const bubble = (): string => document.querySelector('.bubble')?.textContent ?? ''; +/** Opens the ring of care, where Touch her lives (planet-garden.md G7). */ +async function openRing(): Promise { + if (!screen.queryByRole('group', { name: en.scene.ring })) { + await fireEvent.keyDown(await screen.findByRole('button', { name: en.rose.care }), { key: 'Enter' }); + await tick(); + } + return screen.getByRole('button', { name: en.rose.touch.label }); +} + async function touchAt(w: ReturnType, at: number): Promise { w.clock.now = at; - await fireEvent.click(screen.getByRole('button', { name: en.rose.touch.label })); + await fireEvent.click(await openRing()); await tick(); } @@ -54,7 +63,7 @@ describe('touching her (thorns.md)', () => { it('a touch is answered fondly, then her usual line returns; nothing is written', async () => { const w = wiring(); render(Rose, { wiring: w, timeZone: ZONE }); - await screen.findByRole('button', { name: en.rose.touch.label }); + await openRing(); const usual = bubble(); vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] }); await touchAt(w, MORNING); @@ -70,7 +79,7 @@ describe('touching her (thorns.md)', () => { it('three quick taps: Gently. I have thorns. Taps while she rests change nothing', async () => { const w = wiring(); render(Rose, { wiring: w, timeZone: ZONE }); - await screen.findByRole('button', { name: en.rose.touch.label }); + await openRing(); vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] }); await touchAt(w, MORNING); await touchAt(w, MORNING + 500); @@ -84,7 +93,7 @@ describe('touching her (thorns.md)', () => { it('a held key is one touch, not a rush', async () => { const w = wiring(); render(Rose, { wiring: w, timeZone: ZONE }); - const button = await screen.findByRole('button', { name: en.rose.touch.label }); + const button = await openRing(); const repeat = new KeyboardEvent('keydown', { key: 'Enter', repeat: true, cancelable: true, bubbles: true }); button.dispatchEvent(repeat); expect(repeat.defaultPrevented).toBe(true); @@ -102,7 +111,7 @@ describe('touching her (thorns.md)', () => { w.clock.now = MORNING + 25 * DAY + 3 * 3_600_000; render(Rose, { wiring: w, timeZone: ZONE }); const img = await screen.findByRole('img', { name: /full bloom/ }); - expect(img.getAttribute('alt')).not.toMatch(/blushing/); + expect(img.getAttribute('aria-label')).not.toMatch(/blushing/); await touchAt(w, w.clock.now); expect(screen.getByRole('img', { name: /full bloom, blushing/ })).toBeTruthy(); }); diff --git a/packages/ui/test/two-tabs.test.ts b/packages/ui/test/two-tabs.test.ts index d7ed8af..e171b44 100644 --- a/packages/ui/test/two-tabs.test.ts +++ b/packages/ui/test/two-tabs.test.ts @@ -69,6 +69,8 @@ describe('two tabs, one garden (two-tabs.md)', () => { it("this tab's own care, still being written, is not lost when another tab's news arrives first", async () => { const w = roseWiring(); render(Rose, { wiring: w, timeZone: ZONE }); + // The ring of care opens on her (planet-garden.md G7); Water is in it. + await fireEvent.keyDown(await screen.findByRole('button', { name: en.rose.care }), { key: 'Enter' }); const button = await screen.findByRole('button', { name: en.rose.water }); vi.useFakeTimers({ toFake: ['setInterval', 'clearInterval'] }); await fireEvent.pointerDown(button, { pointerId: 1 });