[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
+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 }
);
});
});