import { invitation, sunDay, type NamedPlace } from '@rosea/engine'; 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 { SunsetChoice, SunsetWiring } from '../src/sunset/types.js'; const brno: NamedPlace = { name: 'Brno', region: '', country: 'Czechia', lat: 49.2, lon: 16.61, timeZone: 'Europe/Prague' }; const prague: NamedPlace = { name: 'Prague', region: '', country: 'Czechia', lat: 50.09, lon: 14.46, timeZone: 'Europe/Prague' }; const tromso: NamedPlace = { name: 'Tromsø', region: 'Troms', country: 'Norway', lat: 69.65, lon: 18.96, timeZone: 'Europe/Oslo' }; /** Noon in Prague on 29 March 2026: tonight's sunset is still to come. */ const NOON = Date.parse('2026-03-29T12:00:00+02:00'); /** The time the line must show, read exactly as the component reads it. */ const timeOf = (place: NamedPlace, at: number): string => new Intl.DateTimeFormat(undefined, { hour: 'numeric', minute: '2-digit', timeZone: place.timeZone }).format(at); function wiring(over: Partial & { saved?: SunsetChoice | null } = {}): SunsetWiring & { saves: SunsetChoice[] } { const saves: SunsetChoice[] = []; return { guess: prague, search: async (q) => [brno, prague].filter((t) => t.name.toLowerCase().startsWith(q.trim().toLowerCase())), load: async () => over.saved ?? null, save: async (c) => { saves.push(c); }, now: () => NOON, ...over, saves }; } afterEach(() => { cleanup(); vi.restoreAllMocks(); }); describe('the garden asks where the sunset is', () => { it('asks when nothing is saved, and does not take focus on first load', async () => { render(Shell, { sunset: wiring() }); expect(await screen.findByRole('heading', { level: 2, name: en.sunset.question })).toBeTruthy(); expect(screen.getByRole('searchbox', { name: en.sunset.searchLabel })).toBeTruthy(); expect(document.activeElement).toBe(document.body); }); it('typing lists towns, choosing one shows its sunset, keeps it, and moves focus to the answer', async () => { const w = wiring(); render(Shell, { sunset: w }); await fireEvent.input(await screen.findByRole('searchbox'), { target: { value: 'br' } }); expect(await screen.findByRole('status')).toHaveProperty('textContent', en.sunset.found(1)); await fireEvent.click(screen.getByRole('button', { name: /Brno/ })); const at = invitation(sunDay('2026-03-29', brno)); expect(await screen.findByText(en.sunset.tonight(timeOf(brno, at), 'Brno'))).toBeTruthy(); expect(w.saves).toEqual([{ kind: 'town', town: brno }]); await waitFor(() => expect(document.activeElement?.className).toContain('sun')); expect(screen.queryByText(en.sunset.guessed)).toBeNull(); }); it('nothing found says a larger town nearby will do, never an error', async () => { render(Shell, { sunset: wiring() }); await fireEvent.input(await screen.findByRole('searchbox'), { target: { value: 'zz' } }); await waitFor(() => expect(screen.getByRole('status').textContent).toBe(en.sunset.found(0))); }); it('"not now" uses the guess, saves the device zone, and says it is a guess', async () => { const w = wiring(); render(Shell, { sunset: w }); await fireEvent.click(await screen.findByRole('button', { name: en.sunset.skip('Prague') })); const at = invitation(sunDay('2026-03-29', prague)); expect(await screen.findByText(en.sunset.tonight(timeOf(prague, at), 'Prague'))).toBeTruthy(); expect(screen.getByText(en.sunset.guessed)).toBeTruthy(); expect(w.saves).toEqual([{ kind: 'device-zone' }]); }); it('with no guess (a UTC clock), "not now" asks the player to choose a town', async () => { render(Shell, { sunset: wiring({ guess: null }) }); await fireEvent.click(await screen.findByRole('button', { name: en.sunset.skipNoGuess })); expect(await screen.findByText(en.sunset.noTown)).toBeTruthy(); expect(screen.getByRole('button', { name: en.sunset.choose })).toBeTruthy(); }); }); describe('the garden remembers', () => { it('a saved town shows its sunset at once, with no question', async () => { render(Shell, { sunset: wiring({ saved: { kind: 'town', town: brno } }) }); expect(await screen.findByText(/in Brno\.$/)).toBeTruthy(); expect(screen.queryByRole('searchbox')).toBeNull(); }); it('after the sunset, it speaks of tomorrow\'s', async () => { const after = invitation(sunDay('2026-03-29', brno)) + 60_000; render(Shell, { sunset: wiring({ saved: { kind: 'town', town: brno }, now: () => after }) }); const tomorrow = invitation(sunDay('2026-03-30', brno)); expect(await screen.findByText(en.sunset.tomorrow(timeOf(brno, tomorrow), 'Brno'))).toBeTruthy(); }); it('in polar night it speaks of the noon twilight; in polar day, of the lowest light', async () => { const winter = Date.parse('2026-12-21T08:00:00+01:00'); render(Shell, { sunset: wiring({ saved: { kind: 'town', town: tromso }, now: () => winter }) }); const noon = sunDay('2026-12-21', tromso).solarNoon; expect(await screen.findByText(en.sunset.polarNight(timeOf(tromso, noon), 'Tromsø'))).toBeTruthy(); cleanup(); const summer = Date.parse('2026-06-21T12:00:00+02:00'); render(Shell, { sunset: wiring({ saved: { kind: 'town', town: tromso }, now: () => summer }) }); expect(await screen.findByText(/In Tromsø the sun stays up tonight\. The light is lowest at/)).toBeTruthy(); }); it('change town: the search opens with focus, and Keep returns without saving anything', async () => { const w = wiring({ saved: { kind: 'town', town: brno } }); render(Shell, { sunset: w }); await fireEvent.click(await screen.findByRole('button', { name: en.sunset.change })); await waitFor(() => expect(document.activeElement).toBe(screen.getByRole('searchbox'))); await fireEvent.click(screen.getByRole('button', { name: en.sunset.keep('Brno') })); expect(await screen.findByText(/in Brno\.$/)).toBeTruthy(); expect(w.saves).toEqual([]); }); }); describe('when the device will not keep anything', () => { it('a failed read still asks, and a failed save still shows the sunset and says it will ask again', async () => { const error = vi.spyOn(console, 'error').mockImplementation(() => undefined); const w = wiring({ load: () => Promise.reject(new Error('IndexedDB blocked')), save: () => Promise.reject(new Error('IndexedDB blocked')) }); render(Shell, { sunset: w }); await fireEvent.click(await screen.findByRole('button', { name: en.sunset.skip('Prague') })); expect(await screen.findByText(/in Prague\.$/)).toBeTruthy(); expect(await screen.findByText(en.sunset.notKept)).toBeTruthy(); expect(error).toHaveBeenCalled(); // never swallowed }); });