1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
import { describe, expect, it } from 'vitest';
import { instantFromDay } from './temporal';
// A weekday well inside the year so noon UTC never collides with a clamp.
const NOW = Temporal.Instant.from('2026-09-20T08:00:00.000Z');
describe('instantFromDay', () => {
it('stamps a past day at noon UTC', () => {
expect(instantFromDay('2026-09-13', NOW)).toBe('2026-09-13T12:00:00.000Z');
});
it('clamps today to the current instant', () => {
expect(instantFromDay('2026-09-20', NOW)).toBe('2026-09-20T08:00:00.000Z');
});
it('keeps a past day that is only hours old as-is', () => {
// 21:00 the day before: noon of that day is still in the past.
expect(instantFromDay('2026-09-19', NOW)).toBe('2026-09-19T12:00:00.000Z');
});
it('accepts a day a few hours ahead and clamps it to now', () => {
const late = Temporal.Instant.from('2026-09-20T22:00:00.000Z');
expect(instantFromDay('2026-09-21', late)).toBe('2026-09-20T22:00:00.000Z');
});
it('rejects a day more than a day ahead', () => {
expect(instantFromDay('2026-09-22', NOW)).toBeNull();
});
it('rejects malformed and impossible dates', () => {
expect(instantFromDay('', NOW)).toBeNull();
expect(instantFromDay('hier', NOW)).toBeNull();
expect(instantFromDay('2026-02-30', NOW)).toBeNull();
});
});
|