import { afterEach, describe, expect, it } from 'vitest'; import { operatorCustomRangeValid, operatorRangeSelection, resetOperatorContextForTests, setOperatorCustomRange, setOperatorPaused, setOperatorRangePreset, } from '../../src/operatorContext'; afterEach(() => { resetOperatorContextForTests(); window.localStorage.clear(); window.history.replaceState({}, '', '/'); }); describe('global operator context', () => { it('encodes a valid custom range in the shareable URL', () => { setOperatorCustomRange('2026-09-01T10:00', '2026-09-01T11:00'); const params = new URLSearchParams(window.location.search); expect(params.get('range')).toBe('custom'); expect(params.get('from')).toBe(new Date('2026-09-01T10:00').toISOString()); expect(params.get('to')).toBe(new Date('2026-09-01T11:00').toISOString()); }); it('permits pause only for the live range', () => { setOperatorRangePreset('live'); setOperatorPaused(true); expect(window.localStorage.getItem('pulse.operator.paused')).toBe('true'); setOperatorRangePreset('1h'); expect(window.localStorage.getItem('pulse.operator.paused')).toBe('false'); }); it('fails closed to one hour when a custom range is invalid', () => { const value = { range: 'custom' as const, customFrom: '2026-09-01T11:00', customTo: '2026-09-01T10:00', paused: false, refreshVersion: 0 }; expect(operatorCustomRangeValid(value)).toBe(false); expect(operatorRangeSelection(value)).toBe('1h'); }); });