// src/utils/dateUtils.test.ts
import { describe, it, expect, vi, afterEach, beforeEach } from 'vitest';
import { getSimpleWeekAndYear } from './dateUtils';

describe('dateUtils', () => {
  describe('getSimpleWeekAndYear', () => {
    beforeEach(() => {
      // Use fake timers to control the current date in tests
      vi.useFakeTimers();
    });

    afterEach(() => {
      // Restore real timers after each test
      vi.useRealTimers();
    });

    it('should return week 1 for the first day of the year', () => {
      const date = new Date('2024-01-01T12:00:00Z');
      expect(getSimpleWeekAndYear(date)).toEqual({ year: 2024, week: 1 });
    });

    it('should return week 1 for the 7th day of the year', () => {
      const date = new Date('2024-01-07T12:00:00Z');
      expect(getSimpleWeekAndYear(date)).toEqual({ year: 2024, week: 1 });
    });

    it('should return week 2 for the 8th day of the year', () => {
      const date = new Date('2024-01-08T12:00:00Z');
      expect(getSimpleWeekAndYear(date)).toEqual({ year: 2024, week: 2 });
    });

    it('should correctly calculate the week for a date in the middle of the year', () => {
      // July 1st is the 183rd day of a leap year. 182 / 7 = 26. So it's week 27.
      const date = new Date('2024-07-01T12:00:00Z');
      expect(getSimpleWeekAndYear(date)).toEqual({ year: 2024, week: 27 });
    });

    it('should correctly calculate the week for the last day of a non-leap year', () => {
      // Dec 31, 2023 is the 365th day. 364 / 7 = 52. So it's week 53.
      const date = new Date('2023-12-31T12:00:00Z');
      expect(getSimpleWeekAndYear(date)).toEqual({ year: 2023, week: 53 });
    });

    it('should correctly calculate the week for the last day of a leap year', () => {
      // Dec 31, 2024 is the 366th day. 365 / 7 = 52.14. floor(52.14) + 1 = 53.
      const date = new Date('2024-12-31T12:00:00Z');
      expect(getSimpleWeekAndYear(date)).toEqual({ year: 2024, week: 53 });
    });

    it('should use the current date if no date is provided', () => {
      const fakeCurrentDate = new Date('2025-02-10T12:00:00Z'); // 41st day of the year
      vi.setSystemTime(fakeCurrentDate);

      // 40 / 7 = 5.71. floor(5.71) + 1 = 6.
      expect(getSimpleWeekAndYear()).toEqual({ year: 2025, week: 6 });
    });
  });
});