diff --git a/e2e/shell.spec.ts b/e2e/shell.spec.ts index 763ab09..aaa7455 100644 --- a/e2e/shell.spec.ts +++ b/e2e/shell.spec.ts @@ -3,7 +3,7 @@ import { expect, test, type Page } from '@playwright/test'; import { PRODUCT_NAME } from '@rosea/schema'; import { en } from '@rosea/ui/copy'; import { parseLetter } from '@rosea/ui/letter'; -import { nextInvitation } from '@rosea/engine'; +import { nextInvitation, nextSunEvent } from '@rosea/engine'; import { searchTowns } from '@rosea/content/places'; import { readFileSync } from 'node:fs'; import { BASE, startStaticServer, type StaticServer } from './static-server.js'; @@ -163,6 +163,20 @@ test.describe('the sunset place, in Prague\'s time zone at noon on 29 March 2026 expect(requests.some((url) => /\/assets\/places-.*\.js$/.test(url))).toBe(true); }); + test('at five in the morning the garden names the sunrise, not the sunset (sunrise.md)', async ({ page }) => { + const early = Date.parse('2026-03-29T05:00:00+02:00'); + await page.clock.setFixedTime(early); + await open(page); + await page.getByRole('searchbox', { name: en.sunset.searchLabel }).fill('brno'); + await page.getByRole('button', { name: /^Brno/ }).first().click(); + const brno = searchTowns('brno')[0]!; + const { at } = nextSunEvent(early, brno); + const time = new Intl.DateTimeFormat('en-US', { hour: 'numeric', minute: '2-digit', timeZone: brno.timeZone }).format(at); + const line = page.locator('.sun p').first(); + await expect(line).toHaveText(/^The sun rises at .* in Brno\.$/); + expect(spaced((await line.textContent()) ?? '')).toBe(spaced(en.sunset.rises(time, 'Brno'))); + }); + test('"not now" guesses Prague from the device\'s zone and says it is a guess', async ({ page }) => { await page.clock.setFixedTime(NOON); await open(page); diff --git a/packages/engine/src/index.ts b/packages/engine/src/index.ts index 00ce3da..21fe8d8 100644 --- a/packages/engine/src/index.ts +++ b/packages/engine/src/index.ts @@ -3,7 +3,7 @@ export { careEvent, careIdentity, judgeCare, newCare, OWN_TRACE, petalsOn, pour, ROSE, roseAt, seeded, stageFor, weedsOn, windOn, type Judged, type Mood, type Needs, type RoseKeys, type RoseNow, type Stage, type Trend } from './rose/rose.js'; -export { invitation, nextInvitation, sunDay, type NamedPlace, type Place, type SunDay } from './sun/sun.js'; +export { invitation, nextInvitation, nextSunEvent, sunDay, type NamedPlace, type Place, type SunDay } from './sun/sun.js'; export { goodMoment, newOpened, openedIdentity, PETAL, petalAt, petalOrder, sentenceOpened, type PetalNow, type PetalSentence } from './petal/petal.js'; diff --git a/packages/engine/src/sun/sun.ts b/packages/engine/src/sun/sun.ts index aa1d42c..72309d8 100644 --- a/packages/engine/src/sun/sun.ts +++ b/packages/engine/src/sun/sun.ts @@ -159,6 +159,17 @@ export function nextInvitation(now: number, place: Place): { readonly sun: SunDa throw new Error(`nextInvitation: no invitation within two days of ${today}`); } +/** + * The next thing to look up for (sunrise.md R1): the sunrise of the next invitation's + * day while it is still ahead, otherwise the invitation itself. Built on + * `nextInvitation`, so Reykjavík's after-midnight sunset stays tonight's (D2). + */ +export function nextSunEvent(now: number, place: Place): { readonly sun: SunDay; readonly at: number; readonly kind: 'sunrise' | 'invitation' } { + const next = nextInvitation(now, place); + if (next.sun.kind === 'sun-sets' && next.sun.sunrise >= now) return { sun: next.sun, at: next.sun.sunrise, kind: 'sunrise' }; + return { ...next, kind: 'invitation' }; +} + /** * When the game invites the player outside: the sunset; in polar day the lowest * light; in polar night noon, when the twilight is brightest (D4; decided by the diff --git a/packages/engine/test/sun.test.ts b/packages/engine/test/sun.test.ts index 71af9d1..2fd582c 100644 --- a/packages/engine/test/sun.test.ts +++ b/packages/engine/test/sun.test.ts @@ -1,7 +1,7 @@ import fc from 'fast-check'; import { readFileSync } from 'node:fs'; import { describe, expect, it } from 'vitest'; -import { invitation, nextInvitation, sunDay, type Place, type SunDay } from '../src/sun/sun.js'; +import { invitation, nextInvitation, nextSunEvent, sunDay, type Place, type SunDay } from '../src/sun/sun.js'; import { addDays, localDay } from '../src/time/localDay.js'; /** Recorded by scripts/fetch-sun-golden.ts from the U.S. Naval Observatory. */ @@ -255,3 +255,65 @@ describe('sunDay — refusals', () => { expect(() => sunDay(day, place)).toThrow(message); }); }); + +describe('nextSunEvent — the next thing to look up for (sunrise.md)', () => { + const brno: Place = { lat: 49.1951, lon: 16.6068, timeZone: 'Europe/Prague' }; + const day = sunDay('2026-03-02', brno); + if (day.kind !== 'sun-sets') throw new Error('Brno has a sunset in March'); + + it('before sunrise, the sunrise of the same day', () => { + const early = Date.parse('2026-03-02T03:00:00+01:00'); + expect(nextSunEvent(early, brno)).toMatchObject({ kind: 'sunrise', at: day.sunrise }); + }); + + it('from sunrise until the sunset, the sunset, exactly as nextInvitation says', () => { + for (const at of [day.sunrise + MINUTE, day.solarNoon, day.sunset]) { + expect(nextSunEvent(at, brno)).toEqual({ ...nextInvitation(at, brno), kind: 'invitation' }); + } + }); + + it('after the sunset, tomorrow\'s sunrise', () => { + const tomorrow = sunDay('2026-03-03', brno); + if (tomorrow.kind !== 'sun-sets') throw new Error('Brno has a sunset in March'); + expect(nextSunEvent(day.sunset + MINUTE, brno)).toMatchObject({ kind: 'sunrise', at: tomorrow.sunrise }); + expect(localDay(tomorrow.sunrise, brno.timeZone)).toBe('2026-03-03'); + }); + + it('Reykjavík at midsummer, just after midnight: the late sunset still to come, not a sunrise (D2)', () => { + const reykjavik: Place = { lat: 64.1466, lon: -21.9426, timeZone: 'Atlantic/Reykjavik' }; + const midsummer = sunDay('2026-06-21', reykjavik); + if (midsummer.kind !== 'sun-sets') throw new Error('Reykjavík has a sunset on 21 June'); + const justAfterMidnight = Date.parse('2026-06-22T00:01:00Z'); + expect(justAfterMidnight).toBeLessThan(midsummer.sunset); + expect(nextSunEvent(justAfterMidnight, reykjavik)).toMatchObject({ kind: 'invitation', at: midsummer.sunset }); + }); + + it('in polar day and polar night there is no sunrise to name: the invitation, as before', () => { + const tromso: Place = { lat: 69.6492, lon: 18.9553, timeZone: 'Europe/Oslo' }; + for (const at of [Date.parse('2026-06-21T03:00:00+02:00'), Date.parse('2026-12-21T03:00:00+01:00')]) { + expect(nextSunEvent(at, tromso)).toEqual({ ...nextInvitation(at, tromso), kind: 'invitation' }); + } + }); + + it('property: the event is never in the past, and no sunrise or sunset falls between now and it', () => { + fc.assert( + fc.property( + fc.double({ min: -60, max: 60, noNaN: true }), + fc.double({ min: -179, max: 179, noNaN: true }), + fc.integer({ min: Date.parse('2026-01-01T00:00:00Z'), max: Date.parse('2026-12-31T00:00:00Z') }), + (lat, lon, now) => { + const place = zoned(lat, lon); + const event = nextSunEvent(now, place); + expect(event.at).toBeGreaterThanOrEqual(now); + const today = localDay(now, place.timeZone); + for (const offset of [-1, 0, 1, 2]) { + const s = sunDay(addDays(today, offset), place); + if (s.kind !== 'sun-sets') continue; + for (const t of [s.sunrise, s.sunset]) expect(t < now || t >= event.at).toBe(true); + } + } + ), + { numRuns: 300 } + ); + }); +}); diff --git a/packages/ui/src/copy/en.ts b/packages/ui/src/copy/en.ts index 67ba21b..386e48c 100644 --- a/packages/ui/src/copy/en.ts +++ b/packages/ui/src/copy/en.ts @@ -106,6 +106,9 @@ export const en = { choose: 'Choose a town', tonight: (time: string, town: string): string => `Tonight the sun sets at ${time} in ${town}.`, tomorrow: (time: string, town: string): string => `Tomorrow the sun sets at ${time} in ${town}.`, + /** The sunrise, named from sunset until it comes (sunrise.md). Only a time: nothing invites. */ + rises: (time: string, town: string): string => `The sun rises at ${time} in ${town}.`, + risesTomorrow: (time: string, town: string): string => `Tomorrow the sun rises at ${time} in ${town}.`, polarDay: (time: string, town: string): string => `In ${town} the sun stays up tonight. The light is lowest at ${time}.`, polarNight: (time: string, town: string): string => `In ${town} the sun stays below the horizon today. The twilight is brightest at ${time}.`, guessed: "A guess from this device's time zone.", diff --git a/packages/ui/src/sunset/ChairSunset.svelte b/packages/ui/src/sunset/ChairSunset.svelte index 4262fac..497cda4 100644 --- a/packages/ui/src/sunset/ChairSunset.svelte +++ b/packages/ui/src/sunset/ChairSunset.svelte @@ -81,7 +81,8 @@ {#if phase === 'after'}
{en.sunset.noTown}
{/if} diff --git a/packages/ui/src/sunset/SunsetLine.svelte b/packages/ui/src/sunset/SunsetLine.svelte index b0cdd88..b0405d4 100644 --- a/packages/ui/src/sunset/SunsetLine.svelte +++ b/packages/ui/src/sunset/SunsetLine.svelte @@ -1,26 +1,36 @@ diff --git a/packages/ui/test/chair-sunset.test.ts b/packages/ui/test/chair-sunset.test.ts index 288cdd8..cc344fd 100644 --- a/packages/ui/test/chair-sunset.test.ts +++ b/packages/ui/test/chair-sunset.test.ts @@ -91,6 +91,14 @@ describe('the in-game sunset (chair-sunset.md)', () => { 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); diff --git a/packages/ui/test/sunset.test.ts b/packages/ui/test/sunset.test.ts index 08ef00a..91213c6 100644 --- a/packages/ui/test/sunset.test.ts +++ b/packages/ui/test/sunset.test.ts @@ -89,11 +89,23 @@ describe('the garden remembers', () => { expect(screen.queryByRole('searchbox')).toBeNull(); }); - it('after the sunset, it speaks of tomorrow\'s', async () => { + 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 = invitation(sunDay('2026-03-30', brno)); - expect(await screen.findByText(en.sunset.tomorrow(timeOf(brno, tomorrow), 'Brno'))).toBeTruthy(); + 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 () => {