[engine] Sun: nextInvitation checks yesterday, today and tomorrow; addDays steps the calendar; NamedPlace
This commit is contained in:
@@ -1,2 +1,2 @@
|
||||
export { isLocalDay, localDay, type LocalDay } from './time/localDay.js';
|
||||
export { invitation, sunDay, type Place, type SunDay } from './sun/sun.js';
|
||||
export { addDays, isLocalDay, localDay, type LocalDay } from './time/localDay.js';
|
||||
export { invitation, nextInvitation, sunDay, type NamedPlace, type Place, type SunDay } from './sun/sun.js';
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
* (.agents/features/sun.md D3). The place is a city the player chose, or a guess
|
||||
* from the device's zone: never GPS (CLAUDE.md §5.6 rule 6).
|
||||
*/
|
||||
import { isLocalDay, localDay, type LocalDay } from '../time/localDay.js';
|
||||
import { addDays, isLocalDay, localDay, type LocalDay } from '../time/localDay.js';
|
||||
|
||||
export interface Place {
|
||||
/** Degrees, north positive. */
|
||||
@@ -22,6 +22,14 @@ export interface Place {
|
||||
readonly timeZone: string;
|
||||
}
|
||||
|
||||
/** A place with the name the player knows it by: a town they chose, or the guess. */
|
||||
export interface NamedPlace extends Place {
|
||||
readonly name: string;
|
||||
/** Province, state or region, or ''. */
|
||||
readonly region: string;
|
||||
readonly country: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* One local day of sun. Sunrise and sunset are the ones around this day's solar
|
||||
* noon, so tonight's sunset may fall after midnight, on the next local day (D2).
|
||||
@@ -134,6 +142,23 @@ export function sunDay(day: LocalDay, place: Place): SunDay {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The next invitation at or after `now`, and the sun's day it belongs to. Yesterday
|
||||
* is checked first: just after midnight in Reykjavík in June, last evening's sunset
|
||||
* is still a few minutes away (D2).
|
||||
*/
|
||||
export function nextInvitation(now: number, place: Place): { readonly sun: SunDay; readonly at: number } {
|
||||
if (!Number.isFinite(now)) throw new RangeError(`nextInvitation: now must be a finite instant, got ${String(now)}`);
|
||||
const today = localDay(now, place.timeZone);
|
||||
for (const offset of [-1, 0, 1, 2]) {
|
||||
const sun = sunDay(addDays(today, offset), place);
|
||||
const at = invitation(sun);
|
||||
if (at >= now) return { sun, at };
|
||||
}
|
||||
// Unreachable: tomorrow's invitation is always after now. Kept so a broken sun fails loudly.
|
||||
throw new Error(`nextInvitation: no invitation within two days of ${today}`);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
|
||||
@@ -61,6 +61,14 @@ export function localDay(epochMs: number, timeZone: string): LocalDay {
|
||||
return day;
|
||||
}
|
||||
|
||||
/** The calendar day `days` after `day` (before, if negative). A calendar step: no zone, no DST. */
|
||||
export function addDays(day: LocalDay, days: number): LocalDay {
|
||||
if (!isLocalDay(day)) throw new RangeError(`addDays: '${day}' is not a LocalDay`);
|
||||
if (!Number.isInteger(days)) throw new RangeError(`addDays: ${days} is not a whole number of days`);
|
||||
const [y, m, d] = day.split('-').map(Number) as [number, number, number];
|
||||
return localDay(Date.UTC(y, m - 1, d + days), 'UTC');
|
||||
}
|
||||
|
||||
/** Whether `value` has the shape of a LocalDay. Does not check that the date exists. */
|
||||
export function isLocalDay(value: string): value is LocalDay {
|
||||
return LOCAL_DAY.test(value);
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import fc from 'fast-check';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { isLocalDay, localDay } from '../src/time/localDay.js';
|
||||
import { addDays, isLocalDay, localDay } from '../src/time/localDay.js';
|
||||
|
||||
const at = (iso: string): number => Date.parse(iso);
|
||||
|
||||
@@ -86,3 +86,27 @@ describe('localDay — properties', () => {
|
||||
fc.assert(fc.property(instant, zone, (t, z) => localDay(t, z) === localDay(t, z)));
|
||||
});
|
||||
});
|
||||
|
||||
describe('addDays — a calendar step, no zone', () => {
|
||||
it.each([
|
||||
['2026-03-28', 1, '2026-03-29'], // across a DST change: still one day
|
||||
['2026-12-31', 1, '2027-01-01'],
|
||||
['2028-02-28', 1, '2028-02-29'], // leap year
|
||||
['2026-03-01', -1, '2026-02-28'],
|
||||
['2026-06-21', 0, '2026-06-21']
|
||||
])('%s + %s = %s', (day, n, expected) => {
|
||||
expect(addDays(day, n)).toBe(expected);
|
||||
});
|
||||
|
||||
it('refuses a day that is not a LocalDay, and a part of a day', () => {
|
||||
expect(() => addDays('2026-6-1', 1)).toThrow(/not a LocalDay/);
|
||||
expect(() => addDays('2026-06-01', 0.5)).toThrow(/whole number/);
|
||||
});
|
||||
|
||||
it('stepping forward and back again returns the same day', () => {
|
||||
fc.assert(fc.property(fc.integer({ min: 0, max: 3000 }), fc.integer({ min: -400, max: 400 }), (k, n) => {
|
||||
const day = addDays('2025-01-01', k);
|
||||
return addDays(addDays(day, n), -n) === day && isLocalDay(addDays(day, n));
|
||||
}));
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import fc from 'fast-check';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { invitation, sunDay, type Place, type SunDay } from '../src/sun/sun.js';
|
||||
import { localDay } from '../src/time/localDay.js';
|
||||
import { invitation, nextInvitation, 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. */
|
||||
interface Row {
|
||||
@@ -205,6 +205,44 @@ describe('sunDay — a clock twelve hours off the sun (D8)', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('nextInvitation — the one the garden shows', () => {
|
||||
const prague: Place = { lat: 50.0755, lon: 14.4378, timeZone: 'Europe/Prague' };
|
||||
const reykjavik: Place = { lat: 64.1466, lon: -21.9426, timeZone: 'Atlantic/Reykjavik' };
|
||||
|
||||
it('at noon in Prague it is this evening\'s sunset; after it, tomorrow\'s', () => {
|
||||
const noon = Date.parse('2026-03-29T12:00:00+02:00');
|
||||
const today = nextInvitation(noon, prague);
|
||||
expect(today.sun.day).toBe('2026-03-29');
|
||||
expect(today.at).toBe(invitation(sunDay('2026-03-29', prague)));
|
||||
const evening = nextInvitation(today.at + MINUTE, prague);
|
||||
expect(evening.sun.day).toBe('2026-03-30');
|
||||
});
|
||||
|
||||
it('just after midnight in Reykjavík in June, last evening\'s sunset is still to come', () => {
|
||||
const lastEvening = sunDay('2026-06-21', reykjavik);
|
||||
if (lastEvening.kind !== 'sun-sets') throw new Error(lastEvening.kind);
|
||||
const justAfterMidnight = lastEvening.sunset - 2 * MINUTE;
|
||||
expect(localDay(justAfterMidnight, reykjavik.timeZone)).toBe('2026-06-22');
|
||||
expect(nextInvitation(justAfterMidnight, reykjavik).sun.day).toBe('2026-06-21');
|
||||
});
|
||||
|
||||
it('is never in the past, never more than two days ahead, and no earlier invitation lies between', () => {
|
||||
fc.assert(fc.property(
|
||||
fc.double({ min: -89, max: 89, noNaN: true }), fc.double({ min: -180, max: 180, noNaN: true }), fc.integer({ min: Date.UTC(2026, 0, 2), max: Date.UTC(2026, 11, 30) }),
|
||||
(lat, lon, now) => {
|
||||
const place = zoned(lat, lon);
|
||||
const { sun, at } = nextInvitation(now, place);
|
||||
const before = invitation(sunDay(addDays(sun.day, -1), place));
|
||||
return at >= now && at - now <= 48 * HOUR && before < now;
|
||||
}
|
||||
), { numRuns: 400 });
|
||||
});
|
||||
|
||||
it('refuses an instant that is not one', () => {
|
||||
expect(() => nextInvitation(Number.NaN, prague)).toThrow(/finite instant/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('sunDay — refusals', () => {
|
||||
it.each([
|
||||
['2026-02-30', UTC(50, 14), /does not exist/],
|
||||
|
||||
Reference in New Issue
Block a user