Night Light was all-or-nothing. Add an optional schedule with From/To time inputs so the warm overlay only shows during set hours and toggles itself on/off automatically (the overlay re-checks every minute; no reload). Overnight windows that wrap midnight (e.g. 21:00 -> 07:00) are handled. Window logic is the pure, unit-tested isWithinTimeWindow/parseHHMM in utils/timeWindow.ts. New settings: nightLightSchedule/Start/End (default 21:00-07:00). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
46 lines
1.9 KiB
TypeScript
46 lines
1.9 KiB
TypeScript
import assert from 'node:assert/strict';
|
|
import { test } from 'node:test';
|
|
import { parseHHMM, isWithinTimeWindow } from './timeWindow';
|
|
|
|
const at = (h: number, m: number): Date => new Date(2026, 0, 5, h, m);
|
|
|
|
test('parseHHMM: valid values', () => {
|
|
assert.equal(parseHHMM('00:00'), 0);
|
|
assert.equal(parseHHMM('07:30'), 7 * 60 + 30);
|
|
assert.equal(parseHHMM('23:59'), 23 * 60 + 59);
|
|
});
|
|
|
|
test('parseHHMM: malformed values return null', () => {
|
|
assert.equal(parseHHMM(''), null);
|
|
assert.equal(parseHHMM('7:30'), null);
|
|
assert.equal(parseHHMM('24:00'), null);
|
|
assert.equal(parseHHMM('12:60'), null);
|
|
assert.equal(parseHHMM('abc'), null);
|
|
});
|
|
|
|
test('isWithinTimeWindow: same-day window [09:00, 17:00)', () => {
|
|
assert.equal(isWithinTimeWindow('09:00', '17:00', at(8, 59)), false);
|
|
assert.equal(isWithinTimeWindow('09:00', '17:00', at(9, 0)), true);
|
|
assert.equal(isWithinTimeWindow('09:00', '17:00', at(16, 59)), true);
|
|
assert.equal(isWithinTimeWindow('09:00', '17:00', at(17, 0)), false); // end exclusive
|
|
});
|
|
|
|
test('isWithinTimeWindow: overnight window [21:00, 07:00) wraps midnight', () => {
|
|
assert.equal(isWithinTimeWindow('21:00', '07:00', at(20, 59)), false);
|
|
assert.equal(isWithinTimeWindow('21:00', '07:00', at(21, 0)), true);
|
|
assert.equal(isWithinTimeWindow('21:00', '07:00', at(23, 59)), true);
|
|
assert.equal(isWithinTimeWindow('21:00', '07:00', at(0, 0)), true);
|
|
assert.equal(isWithinTimeWindow('21:00', '07:00', at(6, 59)), true);
|
|
assert.equal(isWithinTimeWindow('21:00', '07:00', at(7, 0)), false); // end exclusive
|
|
});
|
|
|
|
test('isWithinTimeWindow: zero-length window is never active', () => {
|
|
assert.equal(isWithinTimeWindow('08:00', '08:00', at(8, 0)), false);
|
|
assert.equal(isWithinTimeWindow('08:00', '08:00', at(12, 0)), false);
|
|
});
|
|
|
|
test('isWithinTimeWindow: malformed inputs are inactive', () => {
|
|
assert.equal(isWithinTimeWindow('', '07:00', at(3, 0)), false);
|
|
assert.equal(isWithinTimeWindow('21:00', 'nope', at(3, 0)), false);
|
|
});
|