[ui] PlanetGarden: Her round planet, the player's gardener, the ring of care, and care by gesture

This commit is contained in:
2026-11-17 19:07:57 +01:00
parent 9eef8a3bf7
commit 5576af4fac
18 changed files with 1268 additions and 79 deletions
+81
View File
@@ -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();
});
});
+1 -1
View File
@@ -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');
});
+187
View File
@@ -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<void> {
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);
});
});
+17 -7
View File
@@ -44,8 +44,16 @@ function wiring(over: Partial<RoseWiring> & { 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<void> {
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<void> {
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<void> => {
await screen.findByRole('img');
await screen.findByRole('img', { name: /^The rose/ });
await fireEvent.click(screen.getByRole('button', { name: en.rose.safe.title }));
};
+14 -5
View File
@@ -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<HTMLElement> {
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<typeof wiring>, at: number): Promise<void> {
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();
});
+2
View File
@@ -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 });