163 lines
8.5 KiB
TypeScript
163 lines
8.5 KiB
TypeScript
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 { said } from './said.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<SunsetWiring> & { 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('on Android the town field is no native text field, so the WebView asks no Autofill service (domi-ui.md D10)', async () => {
|
|
vi.spyOn(navigator, 'userAgent', 'get').mockReturnValue('Mozilla/5.0 (Linux; Android 16; WAVE 10C) AppleWebKit/537.36 Chrome/140.0 Mobile Safari/537.36');
|
|
render(Shell, { sunset: wiring() });
|
|
const field = await screen.findByRole('textbox', { name: en.sunset.searchLabel });
|
|
expect(field.getAttribute('contenteditable')).toBe('plaintext-only');
|
|
expect(document.querySelector('.ask input:not([type="hidden"]), .ask textarea')).toBeNull();
|
|
});
|
|
|
|
it('elsewhere it stays the native search field', async () => {
|
|
render(Shell, { sunset: wiring() });
|
|
expect(await screen.findByRole('searchbox', { name: en.sunset.searchLabel })).toBeTruthy();
|
|
expect(screen.queryByRole('textbox', { name: en.sunset.searchLabel })).toBeNull();
|
|
});
|
|
|
|
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' } });
|
|
await waitFor(() => expect(said().textContent).toBe(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(said().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 names tomorrow's sunrise (sunrise.md)", async () => {
|
|
const after = invitation(sunDay('2026-03-29', brno)) + 60_000;
|
|
render(Shell, { sunset: wiring({ saved: { kind: 'town', town: brno }, now: () => after }) });
|
|
const tomorrow = sunDay('2026-03-30', brno);
|
|
if (tomorrow.kind !== 'sun-sets') throw new Error('Brno has a sunrise in March');
|
|
expect(await screen.findByText(en.sunset.risesTomorrow(timeOf(brno, tomorrow.sunrise), 'Brno'))).toBeTruthy();
|
|
});
|
|
|
|
it('before sunrise, it names this morning\'s sunrise; after it, tonight\'s sunset', async () => {
|
|
const early = Date.parse('2026-03-29T04:00:00+02:00');
|
|
const today = sunDay('2026-03-29', brno);
|
|
if (today.kind !== 'sun-sets') throw new Error('Brno has a sunrise in March');
|
|
render(Shell, { sunset: wiring({ saved: { kind: 'town', town: brno }, now: () => early }) });
|
|
expect(await screen.findByText(en.sunset.rises(timeOf(brno, today.sunrise), 'Brno'))).toBeTruthy();
|
|
cleanup();
|
|
render(Shell, { sunset: wiring({ saved: { kind: 'town', town: brno }, now: () => today.sunrise + 60_000 }) });
|
|
expect(await screen.findByText(en.sunset.tonight(timeOf(brno, today.sunset), '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
|
|
});
|
|
});
|