import { describe, expect, it } from 'vitest';
import { localToInstant, instantToLocal, addDays, currentWeekLabelDate } from '../schedule-time';

describe('store wall time, independent of browser timezone', () => {
  it('chooses the store-local current week before UTC midnight and work-week cutoff', () => {
    const settings = { timezone: 'America/Los_Angeles', work_week_start_day: 'mon' as const, work_week_start_time: '04:00' };
    expect(currentWeekLabelDate(settings, '2026-09-07T02:00:00Z')).toBe('2026-09-06');
    expect(currentWeekLabelDate(settings, '2026-09-07T10:59:00Z')).toBe('2026-09-06');
    expect(currentWeekLabelDate(settings, '2026-09-07T11:00:00Z')).toBe('2026-09-07');
  });
  it('converts normal and overnight local entries without using browser offsets', () => {
    expect(localToInstant('2026-09-07T09:00', 'America/Los_Angeles')).toBe('2026-09-07T16:00:00.000Z');
    expect(instantToLocal('2026-09-08T07:30:00Z', 'America/Los_Angeles')).toBe('2026-09-08T00:30');
    expect(addDays('2026-12-31', 1)).toBe('2027-01-01');
  });
  it('rejects a nonexistent DST time and an ambiguous repeated time rather than shifting silently', () => {
    expect(() => localToInstant('2026-03-08T02:30', 'America/Los_Angeles')).toThrow(/does not exist/i);
    expect(() => localToInstant('2026-11-01T01:30', 'America/Los_Angeles')).toThrow(/ambiguous/i);
    expect(() => localToInstant('2026-02-30T09:00', 'America/Los_Angeles')).toThrow(/valid/i);
  });
});
