[ui] Sunrise: The garden names the real sunrise from sunset until it comes

This commit is contained in:
2026-11-17 18:52:56 +01:00
parent e0a3b66dc8
commit 0c4f5bb556
9 changed files with 137 additions and 16 deletions
+15 -1
View File
@@ -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);
+1 -1
View File
@@ -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';
+11
View File
@@ -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
+63 -1
View File
@@ -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 }
);
});
});
+3
View File
@@ -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.",
+2 -1
View File
@@ -81,7 +81,8 @@
{#if phase === 'after'}
<div class="after-words">
{#if place}
<SunsetLine {place} {guessed} now={afterAt} />
<!-- The real sunset, always: this scene tells the player when it is (sunrise.md R4). -->
<SunsetLine {place} {guessed} now={afterAt} of="sunset" />
{:else}
<p>{en.sunset.noTown}</p>
{/if}
+19 -9
View File
@@ -1,26 +1,36 @@
<script lang="ts">
import { localDay, nextInvitation, type NamedPlace } from '@rosea/engine';
import { localDay, nextInvitation, nextSunEvent, type NamedPlace } from '@rosea/engine';
import { en } from '../copy/en.js';
/**
* The next sunset, or its polar alternative, in one sentence. The engine says
* when; this only reads it on the town's clock, in the device's own time format
* (sunset-place.md P6).
* The next real thing to look up for, in one sentence: the sunrise until it comes,
* then the sunset, or their polar alternatives (sunrise.md). `of="sunset"` names the
* sunset only, for the in-game sunset's closing words. The engine says when; this
* only reads it on the town's clock, in the device's own time format (sunset-place.md P6).
*/
let { place, guessed = false, now }: { place: NamedPlace; guessed?: boolean; now: number } = $props();
let {
place,
guessed = false,
now,
of = 'next'
}: { place: NamedPlace; guessed?: boolean; now: number; of?: 'next' | 'sunset' } = $props();
const line = $derived.by(() => {
const { sun, at } = nextInvitation(now, place);
const event = of === 'sunset' ? { ...nextInvitation(now, place), kind: 'invitation' as const } : nextSunEvent(now, place);
const { sun, at } = event;
const time = new Intl.DateTimeFormat(undefined, { hour: 'numeric', minute: '2-digit', timeZone: place.timeZone }).format(at);
const today = localDay(now, place.timeZone);
// A sunrise is tomorrow's when it falls on a later local day than now.
if (event.kind === 'sunrise') return localDay(at, place.timeZone) > today ? en.sunset.risesTomorrow(time, place.name) : en.sunset.rises(time, place.name);
// "Tomorrow" for a sunset only when it belongs to a later day: tonight's sunset may
// fall after midnight (sun.md D2).
switch (sun.kind) {
case 'polar-day':
return en.sunset.polarDay(time, place.name);
case 'polar-night':
return en.sunset.polarNight(time, place.name);
case 'sun-sets':
// Tonight's sunset may fall after midnight (sun.md D2), so "tomorrow" only
// when it belongs to a later day than today.
return sun.day > localDay(now, place.timeZone) ? en.sunset.tomorrow(time, place.name) : en.sunset.tonight(time, place.name);
return sun.day > today ? en.sunset.tomorrow(time, place.name) : en.sunset.tonight(time, place.name);
}
});
</script>
+8
View File
@@ -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);
+15 -3
View File
@@ -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 () => {