[engine] Sun: nextInvitation checks yesterday, today and tomorrow; addDays steps the calendar; NamedPlace

This commit is contained in:
2026-11-02 00:01:50 +01:00
parent fb7171c3a7
commit 1e8d1cda5a
5 changed files with 101 additions and 6 deletions
+25 -1
View File
@@ -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));
}));
});
});
+40 -2
View File
@@ -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/],