From 6a3ca84aaa42f319a80e8728c10584b54256bfdc Mon Sep 17 00:00:00 2001 From: mnovak Date: Mon, 2 Nov 2026 00:20:59 +0100 Subject: [PATCH] [ui] Rose: The rose with her face and words, water by holding, care when needed, her numbers on request --- packages/ui/src/Shell.svelte | 12 +- packages/ui/src/copy/en.ts | 41 +++++ packages/ui/src/index.ts | 1 + packages/ui/src/rose/Rose.svelte | 275 +++++++++++++++++++++++++++++++ packages/ui/src/rose/types.ts | 17 ++ packages/ui/src/vite-env.d.ts | 1 + packages/ui/test/rose.test.ts | 138 ++++++++++++++++ 7 files changed, 484 insertions(+), 1 deletion(-) create mode 100644 packages/ui/src/rose/Rose.svelte create mode 100644 packages/ui/src/rose/types.ts create mode 100644 packages/ui/src/vite-env.d.ts create mode 100644 packages/ui/test/rose.test.ts diff --git a/packages/ui/src/Shell.svelte b/packages/ui/src/Shell.svelte index d34a1fb..0843403 100644 --- a/packages/ui/src/Shell.svelte +++ b/packages/ui/src/Shell.svelte @@ -4,18 +4,23 @@ import { en } from './copy/en.js'; import Letter from './letter/Letter.svelte'; import { parseLetter } from './letter/parse.js'; + import Rose from './rose/Rose.svelte'; + import type { RoseWiring } from './rose/types.js'; import SunsetLine from './sunset/SunsetLine.svelte'; import SunsetPlace from './sunset/SunsetPlace.svelte'; import type { SunsetChoice, SunsetWiring } from './sunset/types.js'; let { letter = '', - sunset + sunset, + rose }: { /** The author's Markdown from docs/LETTER.md. Empty means there is no way to the page. */ letter?: string; /** Where the sunset is, and how to keep it. Absent, the garden shows no sunset at all. */ sunset?: SunsetWiring; + /** The rose and her care log. Absent, the garden has no rose. */ + rose?: RoseWiring; } = $props(); // --- The sunset (sunset-place.md) --------------------------------------- @@ -28,6 +33,8 @@ let sunLine = $state(); const place = $derived(choice?.kind === 'town' ? choice.town : (sunset?.guess ?? null)); + // One zone for the sunset and the rose's days: the chosen town's, else the device's (rose.md R8). + const roseZone = $derived(choice?.kind === 'town' ? choice.town.timeZone : (rose?.deviceZone ?? 'UTC')); $effect(() => { const wiring = sunset; @@ -105,6 +112,9 @@

{PRODUCT_NAME}

{en.shell.preparing}

+ {#if rose} + + {/if} {#if sunset && (status === 'asking' || changing)} (n === 0 ? 'resting' : n === 1 ? '1 day this week' : `${n} days this week`), + trendWords: { growing: 'growing', steady: 'steady', waiting: 'waiting' }, + stageWords: { bud: 'a bud', opening: 'opening', 'half-bloom': 'in half bloom', 'full-bloom': 'in full bloom' }, + moodWords: { + content: '', + blushing: ', blushing', + 'too-full': ', full of water', + windblown: ', in the wind', + waiting: ', turned a little away, waiting', + thirsty: ', thirsty' + }, + picture: (stage: string, mood: string): string => `The rose, ${stage}${mood}.`, + /** What she says. One line at a time; nothing at all when she needs nothing. Never blame. */ + says: { + back: 'There you are.', + thanks: 'Thank you.', + wind: 'The wind is pulling at me. Could you shade me?', + thirsty: 'A little water, please. Pour slowly, and stop when my leaves lift.', + dry: 'My soil is getting dry.', + weeds: 'Some weeds came up in the night.', + petals: 'A few of my petals have fallen. Would you gather them?', + tooFull: 'That was a lot of water. I will be fine by tomorrow.' + }, + notKept: 'This device could not keep today’s care, so it lasts only until the garden closes.' + }, sunset: { /** ROADMAP Phase 1, word for word. */ question: 'Where do you want your sunset?', diff --git a/packages/ui/src/index.ts b/packages/ui/src/index.ts index ee26dd1..b6c3a56 100644 --- a/packages/ui/src/index.ts +++ b/packages/ui/src/index.ts @@ -2,3 +2,4 @@ export { default as Shell } from './Shell.svelte'; export { en, type Copy } from './copy/en.js'; export { parseLetter, type Inline, type LetterBlock } from './letter/parse.js'; export type { SunsetChoice, SunsetWiring } from './sunset/types.js'; +export type { RoseWiring } from './rose/types.js'; diff --git a/packages/ui/src/rose/Rose.svelte b/packages/ui/src/rose/Rose.svelte new file mode 100644 index 0000000..dd6ae7f --- /dev/null +++ b/packages/ui/src/rose/Rose.svelte @@ -0,0 +1,275 @@ + + +{#if rose} +
+
+ 0} /> + {#if says} +
{says}
+ {/if} +
+ +
+ + {#if rose.needs.wind} + + {/if} + {#if rose.needs.weeds} + + {/if} + {#if rose.needs.petals} + + {/if} +
+

{en.rose.waterHint}

+ + {#if pouring && can} +
+ +
+ {/if} +

{pouring && can ? (can.tooFull ? en.rose.plenty : can.leavesLift ? en.rose.leavesLift : '') : ''}

+ + {#if !kept} +

{en.rose.notKept}

+ {/if} + + + {#if showNumbers} +
+
{en.rose.waterLevel}
{percent(rose.water)}
+
{en.rose.daysOfCare}
{rose.daysOfCare}
+
{en.rose.warmth}
{en.rose.warmthDays(rose.warmth)}
+
{en.rose.trend}
{en.rose.trendWords[rose.trend]}
+
+ {/if} +
+{/if} + + diff --git a/packages/ui/src/rose/types.ts b/packages/ui/src/rose/types.ts new file mode 100644 index 0000000..a09d245 --- /dev/null +++ b/packages/ui/src/rose/types.ts @@ -0,0 +1,17 @@ +import type { CareEvent } from '@rosea/schema'; + +/** + * What the rose needs from the app: her care log, her key, and the clock. The app + * provides it; ui imports neither storage nor identity (CLAUDE.md §2). + */ +export interface RoseWiring { + /** The player's public key, which names the rose. `kept` is false if the device could not keep it. */ + readonly identity: () => Promise<{ readonly publicKey: string; readonly kept: boolean }>; + readonly loadCare: () => Promise; + readonly appendCare: (event: CareEvent) => Promise; + readonly clientVersion: string; + /** The device's own zone: the rose's day when no sunset town is chosen (rose.md R8). */ + readonly deviceZone: string; + /** The clock. A parameter, so tests and the e2e suite can fix it. */ + readonly now: () => number; +} diff --git a/packages/ui/src/vite-env.d.ts b/packages/ui/src/vite-env.d.ts new file mode 100644 index 0000000..11f02fe --- /dev/null +++ b/packages/ui/src/vite-env.d.ts @@ -0,0 +1 @@ +/// diff --git a/packages/ui/test/rose.test.ts b/packages/ui/test/rose.test.ts new file mode 100644 index 0000000..12ae379 --- /dev/null +++ b/packages/ui/test/rose.test.ts @@ -0,0 +1,138 @@ +import { careEvent, localDay, weedsOn } from '@rosea/engine'; +import type { CareEvent } from '@rosea/schema'; +import { cleanup, fireEvent, render, screen, waitFor } 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'; + +const ZONE = 'Europe/Prague'; +const ME = 'M'.repeat(87); +/** 08:00 in Prague: before any wind can start, so the wind button never surprises a test. */ +const MORNING = Date.parse('2026-03-01T08:00:00+01:00'); +const DAY = 86_400_000; + +function wiring(over: Partial & { log?: CareEvent[]; at?: number } = {}): RoseWiring & { appended: CareEvent[] } { + const appended: CareEvent[] = []; + const at = over.at ?? MORNING; + return { + identity: async () => ({ publicKey: ME, kept: true }), + loadCare: async () => over.log ?? [], + appendCare: async (e) => { + appended.push(e); + }, + clientVersion: '0.1.0', + deviceZone: ZONE, + now: () => at, + ...over, + appended + }; +} +const water = (now: number, amount: number): CareEvent => careEvent({ now, timeZone: ZONE, from: ME, to: ME, act: 'water', amount, clientVersion: '0.1.0' }); + +/** Holds the Water button for `ms`, with the pour's own timer under control. */ +async function hold(ms: number, how: 'pointer' | 'keyboard' = 'pointer'): Promise { + const button = screen.getByRole('button', { name: en.rose.water }); + vi.useFakeTimers({ toFake: ['setInterval', 'clearInterval'] }); + try { + if (how === 'pointer') await fireEvent.pointerDown(button, { pointerId: 1 }); + else await fireEvent.keyDown(button, { key: ' ' }); + vi.advanceTimersByTime(ms); + await tick(); + } finally { + vi.useRealTimers(); + } +} +const release = async (how: 'pointer' | 'keyboard' = 'pointer'): Promise => { + const button = screen.getByRole('button', { name: en.rose.water }); + if (how === 'pointer') await fireEvent.pointerUp(button, { pointerId: 1 }); + else await fireEvent.keyUp(button, { key: ' ' }); +}; + +afterEach(() => { + cleanup(); + vi.useRealTimers(); + vi.restoreAllMocks(); +}); + +describe('the rose', () => { + it('a new rose is a bud whose soil is getting dry, and says so', async () => { + render(Rose, { wiring: wiring(), timeZone: ZONE }); + expect(await screen.findByRole('img', { name: 'The rose, a bud.' })).toBeTruthy(); + expect(screen.getByText(en.rose.says.dry)).toBeTruthy(); + }); + + 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 hold(1500); // half a can + expect(screen.getByRole('status').textContent).toBe(en.rose.leavesLift); + await release(); + expect(w.appended).toHaveLength(1); + expect(w.appended[0]).toMatchObject({ act: 'water', amount: 0.5, localDay: '2026-03-01', from: ME, to: ME }); + expect(await screen.findByText(en.rose.says.thanks)).toBeTruthy(); + + await fireEvent.click(screen.getByRole('button', { name: en.rose.numbers })); + const numbers = document.getElementById('rose-numbers'); + expect(numbers?.textContent).toContain(`${en.rose.daysOfCare}1`); + expect(numbers?.textContent).toContain(en.rose.warmthDays(1)); + }); + + 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 hold(3000); + expect(screen.getByRole('status').textContent).toBe(en.rose.plenty); + }); + + 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 hold(1200, 'keyboard'); + await release('keyboard'); + expect(w.appended[0]).toMatchObject({ act: 'water', amount: 0.4 }); + }); + + it('Pull the weeds shows only on a day with weeds, and goes once they are pulled', async () => { + let day = MORNING; + while (!weedsOn(ME, localDay(day, ZONE))) day += DAY; + const w = wiring({ at: day }); + render(Rose, { wiring: w, timeZone: ZONE }); + 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()); + + let clear = MORNING; + while (weedsOn(ME, localDay(clear, ZONE))) clear += DAY; + cleanup(); + render(Rose, { wiring: wiring({ at: clear }), timeZone: ZONE }); + await screen.findByRole('img'); + expect(screen.queryByRole('button', { name: en.rose.weed })).toBeNull(); + }); + + it('after weeks away she is turned a little away and waiting, and greets, never blames', async () => { + 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(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)); + expect(document.getElementById('rose-numbers')?.textContent).toContain(`${en.rose.daysOfCare}1`); + }); + + it('if the device cannot keep the care, the care still counts now, and she says it lasts only until closing', async () => { + 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 hold(1500); + await release(); + expect(await screen.findByText(en.rose.says.thanks)).toBeTruthy(); + expect(await screen.findByText(en.rose.notKept)).toBeTruthy(); + expect(error).toHaveBeenCalled(); + }); +});