import assert from 'node:assert/strict' import { readFileSync, readdirSync } from 'node:fs' import { describe, it } from 'node:test' import { clockWindow, instantForWallTime, isQuietByClock, localDateKey, nextLocalHour, previousLocalHour, } from './clock.ts' import { buildOverride } from './override.ts' import { computeStatus } from './status-core.ts' const at = (iso: string) => new Date(iso) describe('quiet window basics (MDT, UTC-6)', () => { it('is quiet from 10:00 PM to 8:00 AM, including weekends', () => { // Sat Oct 17, 2026 (weekend) assert.equal(isQuietByClock(at('2026-10-18T03:59:59Z')), false) // 9:59:59 PM MDT assert.equal(isQuietByClock(at('2026-10-18T04:00:00Z')), true) // 10:00 PM MDT assert.equal(isQuietByClock(at('2026-10-18T13:59:59Z')), true) // 7:59:59 AM MDT Sun assert.equal(isQuietByClock(at('2026-10-18T14:00:00Z')), false) // 8:00 AM MDT Sun }) it('until = next 8 AM while quiet, next 10 PM while not', () => { assert.deepEqual(clockWindow(at('2026-10-18T05:35:00Z')), { quiet: true, until: '2026-10-18T14:00:00.000Z', }) assert.deepEqual(clockWindow(at('2026-10-18T18:00:00Z')), { quiet: false, until: '2026-10-19T04:00:00.000Z', }) }) }) describe('DST end: Sun Nov 1, 2026 (2:00 AM MDT -> 1:00 AM MST)', () => { it('night spanning the change ends at 8:00 AM MST (15:00Z, not 14:00Z)', () => { const start = at('2026-11-01T04:00:00Z') // Sat Oct 31 10:00 PM MDT assert.deepEqual(clockWindow(start), { quiet: true, until: '2026-11-01T15:00:00.000Z' }) assert.equal(isQuietByClock(at('2026-11-01T03:59:00Z')), false) }) it('both 1:30 AM occurrences are quiet', () => { assert.equal(isQuietByClock(at('2026-11-01T07:30:00Z')), true) // 1:30 MDT assert.equal(isQuietByClock(at('2026-11-01T08:30:00Z')), true) // 1:30 MST }) it('8 AM boundary after the change is 15:00Z', () => { assert.equal(isQuietByClock(at('2026-11-01T14:59:59Z')), true) assert.equal(isQuietByClock(at('2026-11-01T15:00:00Z')), false) }) it('first MST evening starts at 05:00Z', () => { assert.equal(isQuietByClock(at('2026-11-02T04:59:59Z')), false) assert.equal(isQuietByClock(at('2026-11-02T05:00:00Z')), true) assert.deepEqual(clockWindow(at('2026-11-01T18:00:00Z')), { quiet: false, until: '2026-11-02T05:00:00.000Z', }) }) it('ambiguous 1:30 AM wall time resolves to the earlier (MDT) instant', () => { assert.equal( instantForWallTime({ year: 2026, month: 11, day: 1, hour: 1, minute: 30 }).toISOString(), '2026-11-01T07:30:00.000Z', ) }) }) describe('DST start: Sun Mar 14, 2027 (2:00 AM MST -> 3:00 AM MDT)', () => { it('night spanning the change ends at 8:00 AM MDT (14:00Z, not 15:00Z)', () => { const start = at('2027-03-14T05:00:00Z') // Sat Mar 13 10:00 PM MST assert.deepEqual(clockWindow(start), { quiet: true, until: '2027-03-14T14:00:00.000Z' }) assert.equal(isQuietByClock(at('2027-03-14T04:59:59Z')), false) }) it('8 AM boundary after the change is 14:00Z', () => { assert.equal(isQuietByClock(at('2027-03-14T13:59:59Z')), true) assert.equal(isQuietByClock(at('2027-03-14T14:00:00Z')), false) }) it('first MDT evening starts at 04:00Z', () => { assert.equal(isQuietByClock(at('2027-03-15T03:59:59Z')), false) assert.equal(isQuietByClock(at('2027-03-15T04:00:00Z')), true) }) it('nonexistent 2:30 AM wall time resolves forward to 3:30 MDT', () => { assert.equal( instantForWallTime({ year: 2027, month: 3, day: 14, hour: 2, minute: 30 }).toISOString(), '2027-03-14T09:30:00.000Z', ) }) }) describe('overrides across DST', () => { it("'off' at 11 PM Sat Oct 31 lasts until 10 PM MST Sun Nov 1 (05:00Z)", () => { const now = at('2026-11-01T05:00:00Z') // Oct 31 11:00 PM MDT const record = buildOverride('off', now) assert.equal(record.expiresAt, '2026-11-02T05:00:00.000Z') const s = computeStatus(now, record) assert.equal(s.quiet, false) assert.equal(s.until, '2026-11-02T05:00:00.000Z') // Once expired, the clock decides again (quiet at 10 PM MST). assert.equal(computeStatus(at('2026-11-02T05:00:00Z'), record).quiet, true) }) it("'off' at 2 AM lasts until 10 PM the same day", () => { const record = buildOverride('off', at('2026-10-20T08:00:00Z')) // 2 AM MDT assert.equal(record.expiresAt, '2026-10-21T04:00:00.000Z') }) it("'on' at 3 PM MST Sat Mar 13 lasts until 8 AM MDT Sun (14:00Z)", () => { const now = at('2027-03-13T22:00:00Z') const record = buildOverride('on', now) assert.equal(record.expiresAt, '2027-03-14T14:00:00.000Z') const s = computeStatus(now, record) assert.equal(s.quiet, true) assert.equal(s.override?.mode, 'on') }) }) describe('helpers', () => { it('localDateKey uses Denver dates', () => { assert.equal(localDateKey(at('2026-11-02T05:30:00Z')), '2026-11-01') }) it('previous/next local hour around fall-back', () => { assert.equal(previousLocalHour(at('2026-11-01T15:15:00Z'), 22).toISOString(), '2026-11-01T04:00:00.000Z') assert.equal(nextLocalHour(at('2026-11-01T04:00:00Z'), 22).toISOString(), '2026-11-02T05:00:00.000Z') }) it('source never uses a fixed UTC offset', () => { for (const file of readdirSync(new URL('.', import.meta.url))) { if (!file.endsWith('.ts') || file.endsWith('.test.ts')) continue const source = readFileSync(new URL(file, import.meta.url), 'utf8') assert.doesNotMatch(source, /getTimezoneOffset|[+-]0[67]:00|UTC-[67]\b/, file) } }) })