Files
Rosea/packages/ui/test/gardener.test.ts
T

82 lines
4.2 KiB
TypeScript

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();
});
});