import { describe, expect, it } from 'vitest' import { isQuietAt, type QuietHours } from './types' function at(iso: string): Date { return new Date(iso) } const ALL_DAYS = [0, 1, 2, 3, 4, 5, 6] describe('isQuietAt', () => { it('returns false when disabled', () => { const qh: QuietHours = { enabled: false, from: '00:00', to: '23:59', days: ALL_DAYS } expect(isQuietAt(qh, at('2026-05-17T12:00:00'))).toBe(false) }) it('same-day window: inside is quiet, outside is not', () => { const qh: QuietHours = { enabled: true, from: '13:00', to: '14:00', days: ALL_DAYS } expect(isQuietAt(qh, at('2026-05-17T13:30:00'))).toBe(true) expect(isQuietAt(qh, at('2026-05-17T12:59:00'))).toBe(false) expect(isQuietAt(qh, at('2026-05-17T14:00:00'))).toBe(false) // exclusive end }) it('wrap-around window 22:00 → 08:00', () => { const qh: QuietHours = { enabled: true, from: '22:00', to: '08:00', days: ALL_DAYS } expect(isQuietAt(qh, at('2026-05-17T23:00:00'))).toBe(true) expect(isQuietAt(qh, at('2026-05-17T02:00:00'))).toBe(true) expect(isQuietAt(qh, at('2026-05-17T07:59:00'))).toBe(true) expect(isQuietAt(qh, at('2026-05-17T08:00:00'))).toBe(false) expect(isQuietAt(qh, at('2026-05-17T15:00:00'))).toBe(false) expect(isQuietAt(qh, at('2026-05-17T21:59:00'))).toBe(false) }) it('day filtering: window inactive on excluded days', () => { const qh: QuietHours = { enabled: true, from: '13:00', to: '14:00', days: [1, 2, 3, 4, 5] // weekdays only } // 2026-05-17 is Sunday (day 0) expect(isQuietAt(qh, at('2026-05-17T13:30:00'))).toBe(false) // 2026-05-18 is Monday (day 1) expect(isQuietAt(qh, at('2026-05-18T13:30:00'))).toBe(true) }) it('wrap-around + day filter: window day matters, not current day', () => { // from=22:00, to=07:00, days=[Mon..Fri]. const qh: QuietHours = { enabled: true, from: '22:00', to: '07:00', days: [1, 2, 3, 4, 5] } // Friday 23:00 -> window starts today, Friday in filter -> quiet expect(isQuietAt(qh, at('2026-05-15T23:00:00'))).toBe(true) // Saturday 02:00 -> window started Friday 22:00, Friday in filter -> quiet expect(isQuietAt(qh, at('2026-05-16T02:00:00'))).toBe(true) // Saturday 23:00 -> would start today, Saturday not in filter -> noisy expect(isQuietAt(qh, at('2026-05-16T23:00:00'))).toBe(false) // Sunday 02:00 -> started Saturday, Saturday not in filter -> noisy expect(isQuietAt(qh, at('2026-05-17T02:00:00'))).toBe(false) // Monday 01:00 -> would have started Sunday, Sunday not in filter -> noisy expect(isQuietAt(qh, at('2026-05-18T01:00:00'))).toBe(false) // Monday 23:00 -> starts today, Monday in filter -> quiet expect(isQuietAt(qh, at('2026-05-18T23:00:00'))).toBe(true) }) it('malformed from/to falls back to non-quiet', () => { const qh: QuietHours = { enabled: true, from: 'bogus', to: '08:00', days: ALL_DAYS } expect(isQuietAt(qh, at('2026-05-17T05:00:00'))).toBe(false) }) it('zero-length window (from === to) is never quiet', () => { const qh: QuietHours = { enabled: true, from: '12:00', to: '12:00', days: ALL_DAYS } expect(isQuietAt(qh, at('2026-05-17T12:00:00'))).toBe(false) expect(isQuietAt(qh, at('2026-05-17T12:00:01'))).toBe(false) }) })