statsDateRange.test.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /**
  2. * The Stats presets are local calendar days (DB-C12).
  3. *
  4. * They were built from UTC dates: at 01:30 in Berlin, "Today" asked for the
  5. * previous day, and This Week / This Month started a day early or late near
  6. * midnight. The backend reads a date as a day in the browser's zone, which the
  7. * API client sends with it.
  8. */
  9. import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest';
  10. import { computeDateRange } from '../../utils/statsDateRange';
  11. const originalTz = process.env.TZ;
  12. beforeAll(() => {
  13. process.env.TZ = 'Europe/Berlin';
  14. vi.useFakeTimers();
  15. // 01:30 on Friday 16 October in Berlin -- still the 15th in UTC.
  16. vi.setSystemTime(new Date('2026-10-15T23:30:00Z'));
  17. });
  18. afterAll(() => {
  19. vi.useRealTimers();
  20. // Assigning undefined would set the string "undefined", a zone of its own.
  21. if (originalTz === undefined) delete process.env.TZ;
  22. else process.env.TZ = originalTz;
  23. });
  24. describe('computeDateRange', () => {
  25. it('takes today as the local date', () => {
  26. expect(computeDateRange('today')).toEqual({ dateFrom: '2026-10-16', dateTo: '2026-10-16' });
  27. });
  28. it('starts this week on the local Monday', () => {
  29. expect(computeDateRange('this-week')).toEqual({ dateFrom: '2026-10-12', dateTo: '2026-10-16' });
  30. });
  31. it('counts the last 7 days back from the local date', () => {
  32. expect(computeDateRange('last-7')).toEqual({ dateFrom: '2026-10-10', dateTo: '2026-10-16' });
  33. });
  34. it('starts this month and this year on their local first days', () => {
  35. expect(computeDateRange('this-month').dateFrom).toBe('2026-10-01');
  36. expect(computeDateRange('this-year').dateFrom).toBe('2026-01-01');
  37. });
  38. it('has no bounds for all time', () => {
  39. expect(computeDateRange('all-time')).toEqual({ dateFrom: undefined, dateTo: undefined });
  40. });
  41. });