|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { describe, expect, it } from 'vitest' |
| 5 | +import { organizationUsageEventsQuerySchema } from '@/lib/api/contracts/organization-usage' |
| 6 | + |
| 7 | +/** The shared window fields every usage contract extends, exercised through one of them. */ |
| 8 | +function parseWindow(input: Record<string, unknown>) { |
| 9 | + return organizationUsageEventsQuerySchema.safeParse({ preset: 'custom', ...input }) |
| 10 | +} |
| 11 | + |
| 12 | +describe('organization usage window contract', () => { |
| 13 | + it('accepts a real calendar date', () => { |
| 14 | + expect(parseWindow({ startDate: '2026-08-01', endDate: '2026-08-31' }).success).toBe(true) |
| 15 | + }) |
| 16 | + |
| 17 | + it('refuses a date that does not exist', () => { |
| 18 | + // `Date.parse` accepts this and rolls it forward to March 2, so a request for |
| 19 | + // February would otherwise be answered about March without saying so. |
| 20 | + expect(parseWindow({ startDate: '2026-02-30' }).success).toBe(false) |
| 21 | + }) |
| 22 | + |
| 23 | + it('refuses a nonexistent day inside a datetime, not just a bare date', () => { |
| 24 | + expect(parseWindow({ startDate: '2026-02-30T00:00:00' }).success).toBe(false) |
| 25 | + }) |
| 26 | + |
| 27 | + it('refuses a parseable non-date such as a bare month', () => { |
| 28 | + // `new Date('2026-08')` is August 1. Accepting it returned a window the caller |
| 29 | + // never asked for, with nothing to indicate the value had been reinterpreted. |
| 30 | + expect(parseWindow({ startDate: '2026-08' }).success).toBe(false) |
| 31 | + }) |
| 32 | + |
| 33 | + it('treats an empty limit as omitted rather than as zero', () => { |
| 34 | + // `z.coerce.number()` turns `''` into `0`, which then fails `.min(1)` — so a |
| 35 | + // client serializing an unset filter got a 400 instead of the declared default. |
| 36 | + const parsed = parseWindow({ limit: '' }) |
| 37 | + expect(parsed.success).toBe(true) |
| 38 | + if (parsed.success) expect(parsed.data.limit).toBe(50) |
| 39 | + }) |
| 40 | + |
| 41 | + it('normalizes a single source to a one-item array', () => { |
| 42 | + // One selected filter arrives as a scalar, which a bare `z.array` rejected. |
| 43 | + const parsed = parseWindow({ source: 'workflow' }) |
| 44 | + expect(parsed.success).toBe(true) |
| 45 | + if (parsed.success) expect(parsed.data.source).toEqual(['workflow']) |
| 46 | + }) |
| 47 | + |
| 48 | + it('refuses an unknown source instead of matching nothing', () => { |
| 49 | + expect(parseWindow({ source: 'not-a-source' }).success).toBe(false) |
| 50 | + }) |
| 51 | + |
| 52 | + it('refuses a timezone the runtime does not recognize', () => { |
| 53 | + expect(parseWindow({ timezone: 'Mars/Olympus_Mons' }).success).toBe(false) |
| 54 | + }) |
| 55 | +}) |
0 commit comments