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 { said } from './said.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[]; saved: { name: string; text: string }[] } { const appended: CareEvent[] = []; const saved: { name: string; text: string }[] = []; 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, pastKeys: async () => [], exportRose: async (key) => ({ format: 'rosea-rose', version: 1, keys: [key], events: [...(over.log ?? []), ...appended] }), importRose: async () => ({ added: 0, petals: 0 }), sentences: [], loadOpened: async () => [], appendOpened: async () => undefined, saveFile: (name, text) => { saved.push({ name, text }); }, keeping: { persisted: async () => false, persist: async () => true }, installHint: false, ...over, appended, saved }; } 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 { 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', { name: /^The rose/ }); await hold(1500); // half a can expect(said().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', { name: /^The rose/ }); await hold(3000); expect(said().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', { name: /^The rose/ }); 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 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()); let clear = MORNING; while (weedsOn(ME, localDay(clear, ZONE))) clear += DAY; cleanup(); render(Rose, { wiring: wiring({ at: clear }), timeZone: ZONE }); await screen.findByRole('img', { name: /^The rose/ }); await openRing(); 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.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)); 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', { name: /^The rose/ }); 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(); }); }); // jsdom's File has no text(); every browser the app runs in does. Read it as a browser would. if (typeof File.prototype.text !== 'function') { File.prototype.text = function (this: File): Promise { return new Promise((resolve, reject) => { const reader = new FileReader(); reader.onload = () => resolve(String(reader.result)); reader.onerror = () => reject(reader.error); reader.readAsText(this); }); }; } describe('keep her safe (keep-her-safe.md)', () => { const OLD = 'O'.repeat(87); const open = async (): Promise => { await screen.findByRole('img', { name: /^The rose/ }); await fireEvent.click(screen.getByRole('button', { name: en.rose.safe.title })); }; it('says the browser may clear her, asks it to keep her on request, and says when it promised', async () => { render(Rose, { wiring: wiring(), timeZone: ZONE }); await open(); expect(await screen.findByText(en.rose.safe.mayClear)).toBeTruthy(); await fireEvent.click(screen.getByRole('button', { name: en.rose.safe.ask })); expect(await screen.findByText(en.rose.safe.kept)).toBeTruthy(); expect(screen.queryByRole('button', { name: en.rose.safe.ask })).toBeNull(); }); it('a browser that will not promise is told calmly, and the file is offered', async () => { render(Rose, { wiring: wiring({ keeping: { persisted: async () => false, persist: async () => false } }), timeZone: ZONE }); await open(); await fireEvent.click(await screen.findByRole('button', { name: en.rose.safe.ask })); expect(await screen.findByText(en.rose.safe.refused)).toBeTruthy(); }); it('saves her as a dated file holding her care, and says the file never holds the town', async () => { const log = [water(MORNING - 60_000, 0.5)]; const w = wiring({ log }); render(Rose, { wiring: w, timeZone: ZONE }); await open(); expect(screen.getByText(en.rose.safe.fileNote)).toBeTruthy(); await fireEvent.click(screen.getByRole('button', { name: en.rose.safe.save })); await waitFor(() => expect(w.saved).toHaveLength(1)); expect(w.saved[0]?.name).toBe('rose-2026-03-01.json'); expect(JSON.parse(w.saved[0]?.text ?? '{}')).toMatchObject({ keys: [ME], events: log }); }); it('bringing her back from a file restores her days of care under her old key', async () => { const old = [{ ...water(MORNING - 2 * DAY, 0.5), from: OLD, to: OLD }]; let restored = false; const w = wiring({ loadCare: async () => (restored ? old : []), pastKeys: async () => (restored ? [OLD] : []), importRose: async (file) => { expect(file).toMatchObject({ keys: [OLD] }); restored = true; return { added: 1, petals: 0 }; } }); render(Rose, { wiring: w, timeZone: ZONE }); await open(); const input = screen.getByLabelText(en.rose.safe.bringBack) as HTMLInputElement; const file = new File([JSON.stringify({ format: 'rosea-rose', version: 1, keys: [OLD], events: old })], 'rose.json', { type: 'application/json' }); await fireEvent.change(input, { target: { files: [file] } }); expect(await screen.findByText(en.rose.safe.broughtBack(1))).toBeTruthy(); await fireEvent.click(screen.getByRole('button', { name: en.rose.numbers })); await waitFor(() => expect(document.getElementById('rose-numbers')?.textContent).toContain(`${en.rose.daysOfCare}1`)); }); it('in an app, says where she lives and asks no browser, which is not there (keep-her-safe-apps.md A1)', async () => { const persisted = vi.fn(async () => false); const persist = vi.fn(async () => false); render(Rose, { wiring: wiring({ keeping: { where: 'app', persisted, persist }, installHint: true }), timeZone: ZONE }); await open(); expect(await screen.findByText(en.rose.safe.app)).toBeTruthy(); for (const said of [en.rose.safe.mayClear, en.rose.safe.unknown, en.rose.safe.kept, en.rose.safe.install]) { expect(screen.queryByText(said)).toBeNull(); } expect(screen.queryByRole('button', { name: en.rose.safe.ask })).toBeNull(); expect(screen.getByRole('button', { name: en.rose.safe.save })).toBeTruthy(); expect(screen.getByLabelText(en.rose.safe.bringBack)).toBeTruthy(); expect(persisted).not.toHaveBeenCalled(); expect(persist).not.toHaveBeenCalled(); }); it('the app words never speak of a browser', () => { expect(en.rose.safe.app).not.toMatch(/browser/i); expect(en.rose.safe.saved).not.toMatch(/browser/i); }); it('says she is in the file only when the app knows it was written (A4)', async () => { let answer = true; render(Rose, { wiring: wiring({ saveFile: async () => answer }), timeZone: ZONE }); await open(); const save = screen.getByRole('button', { name: en.rose.safe.save }); await fireEvent.click(save); expect(await screen.findByText(en.rose.safe.saved)).toBeTruthy(); // A second save the player steps back from: the earlier word goes, and nothing is said. answer = false; await fireEvent.click(save); await waitFor(() => expect(screen.queryByText(en.rose.safe.saved)).toBeNull()); }); it('a browser download says nothing of its own: the browser shows it', async () => { const w = wiring(); render(Rose, { wiring: w, timeZone: ZONE }); await open(); await fireEvent.click(screen.getByRole('button', { name: en.rose.safe.save })); await waitFor(() => expect(w.saved).toHaveLength(1)); expect(screen.queryByText(en.rose.safe.saved)).toBeNull(); }); it('a file that is not a rose is said calmly, and nothing changes', async () => { const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined); render(Rose, { wiring: wiring({ importRose: () => Promise.reject(new TypeError('storage: this file does not hold a rose')) }), timeZone: ZONE }); await open(); const input = screen.getByLabelText(en.rose.safe.bringBack); await fireEvent.change(input, { target: { files: [new File(['not json'], 'x.json')] } }); expect(await screen.findByText(en.rose.safe.notARose)).toBeTruthy(); expect(warn).toHaveBeenCalled(); }); });