import type { NamedPlace } from '@rosea/engine'; import { cleanup, fireEvent, render, screen, within } from '@testing-library/svelte'; import { tick } from 'svelte'; import { afterEach, describe, expect, it, vi } from 'vitest'; import Shell from '../src/Shell.svelte'; import { en } from '../src/copy/en.js'; import { SUNSET_MS } from '../src/sunset/ChairSunset.svelte'; import type { SunsetWiring } from '../src/sunset/types.js'; const brno: NamedPlace = { name: 'Brno', region: '', country: 'Czechia', lat: 49.2, lon: 16.61, timeZone: 'Europe/Prague' }; const NOON = Date.parse('2026-03-29T12:00:00+02:00'); const wiring = (over: Partial = {}): SunsetWiring => ({ guess: brno, search: async () => [], load: async () => ({ kind: 'town', town: brno }), save: async () => undefined, now: () => NOON, ...over }); /** Renders the garden with a town set and opens the in-game sunset; the scene's own timer is faked. */ async function openScene(over: Partial = {}): Promise { render(Shell, { sunset: wiring(over) }); const move = await screen.findByRole('button', { name: en.chair.move }); vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout'] }); await fireEvent.click(move); await tick(); return screen.getByRole('dialog', { name: en.chair.scene }); } afterEach(() => { vi.useRealTimers(); cleanup(); }); describe('the in-game sunset (chair-sunset.md)', () => { it('Move the chair opens a quiet scene: labelled, focused, and with nothing to press', async () => { const scene = await openScene(); expect(document.activeElement).toBe(scene); expect(within(scene).queryAllByRole('button')).toEqual([]); expect(scene.getAttribute('aria-describedby')).toBeTruthy(); expect(document.getElementById(scene.getAttribute('aria-describedby')!)?.textContent).toBe(en.chair.leave); }); it('after half a minute, it says when the real one is, and offers to watch again or go back', async () => { const scene = await openScene(); vi.advanceTimersByTime(SUNSET_MS - 1); await tick(); expect(within(scene).queryAllByRole('button')).toEqual([]); vi.advanceTimersByTime(1); await tick(); expect(within(scene).getByText(/^Tonight the sun sets at .* in Brno\.$/)).toBeTruthy(); expect(within(scene).getByRole('button', { name: en.chair.again })).toBeTruthy(); expect(within(scene).getByRole('button', { name: en.chair.back })).toBeTruthy(); }); it('Watch it again starts it over, with nothing to press again', async () => { const scene = await openScene(); vi.advanceTimersByTime(SUNSET_MS); await tick(); await fireEvent.click(within(scene).getByRole('button', { name: en.chair.again })); await tick(); expect(within(scene).queryAllByRole('button')).toEqual([]); vi.advanceTimersByTime(SUNSET_MS); await tick(); expect(within(scene).getByRole('button', { name: en.chair.again })).toBeTruthy(); }); it('Escape comes back early, and focus returns to Move the chair', async () => { const scene = await openScene(); await fireEvent.keyDown(scene, { key: 'Escape' }); await tick(); expect(screen.queryByRole('dialog')).toBeNull(); expect(document.activeElement).toBe(screen.getByRole('button', { name: en.chair.move })); }); it('a tap anywhere comes back early too', async () => { const scene = await openScene(); await fireEvent.click(scene); await tick(); expect(screen.queryByRole('dialog')).toBeNull(); }); it('Back to the rose closes it after the sunset', async () => { const scene = await openScene(); vi.advanceTimersByTime(SUNSET_MS); await tick(); await fireEvent.click(within(scene).getByRole('button', { name: en.chair.back })); await tick(); expect(screen.queryByRole('dialog')).toBeNull(); }); it('late in the evening its words still name the real sunset, never the sunrise (sunrise.md R4)', async () => { const late = Date.parse('2026-03-29T22:00:00+02:00'); const scene = await openScene({ now: () => late }); vi.advanceTimersByTime(SUNSET_MS); await tick(); expect(within(scene).getByText(/^Tomorrow the sun sets at .* in Brno\.$/)).toBeTruthy(); }); it('with no town and no guess, the words after it ask for a town', async () => { const scene = await openScene({ guess: null, load: async () => ({ kind: 'device-zone' }) }); vi.advanceTimersByTime(SUNSET_MS); await tick(); expect(within(scene).getByText(en.sunset.noTown)).toBeTruthy(); }); });