[ui] Rose: The rose with her face and words, water by holding, care when needed, her numbers on request

This commit is contained in:
2026-11-02 00:20:59 +01:00
parent 2b5cd719c1
commit 6a3ca84aaa
7 changed files with 484 additions and 1 deletions
+138
View File
@@ -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<RoseWiring> & { 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<void> {
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<void> => {
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();
});
});