Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions apps/web/src/routers/usage-analytics-router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,36 @@ describe('usage analytics organization breakdown', () => {
});
});

describe('usage analytics date range validation', () => {
const invertedFilters = {
...baseFilters,
startDate: '2026-06-05T00:00:00.000Z',
endDate: '2026-06-04T00:00:00.000Z',
};

it('rejects an end date before the start date on the endDate field', () => {
const result = UsageAnalyticsFiltersSchema.safeParse(invertedFilters);
expect(result.success).toBe(false);
if (!result.success) {
expect(result.error.issues[0]).toMatchObject({
path: ['endDate'],
message: 'endDate must not be before startDate',
});
}
});

it('keeps valid ranges accepted, including an empty single-instant window', () => {
expect(UsageAnalyticsFiltersSchema.safeParse(baseFilters).success).toBe(true);
expect(
UsageAnalyticsFiltersSchema.safeParse({
...baseFilters,
startDate: '2026-06-04T10:00:00.000Z',
endDate: '2026-06-04T10:00:00.000Z',
}).success
).toBe(true);
});
});

describe('usage analytics procedures', () => {
beforeEach(() => {
jest.clearAllMocks();
Expand Down Expand Up @@ -315,6 +345,24 @@ describe('usage analytics procedures', () => {
});
});

it('rejects an inverted date range with a 400 carrying the specific message', async () => {
await expect(
caller().getSummary({
...baseFilters,
startDate: '2026-06-05T00:00:00.000Z',
endDate: '2026-06-04T00:00:00.000Z',
})
).rejects.toMatchObject({
code: 'BAD_REQUEST',
cause: {
issues: [
{ code: 'custom', path: ['endDate'], message: 'endDate must not be before startDate' },
],
},
});
expect(mockExecuteSnowflakeStatement).not.toHaveBeenCalled();
});

it.each([
() => caller().getSummary(baseFilters),
() => caller().getTimeseries({ ...baseFilters, metric: 'cost' }),
Expand Down
14 changes: 13 additions & 1 deletion apps/web/src/routers/usage-analytics-schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,19 @@ const FiltersShape = {
excludedProjects: z.array(z.string()).optional(),
} as const;

export const UsageAnalyticsFiltersSchema = z.object(FiltersShape);
export const UsageAnalyticsFiltersSchema = z
.object(FiltersShape)
.superRefine((filters, context) => {
// An inverted window cannot match any usage row, so it would otherwise
// return a silently empty result instead of a clear client error.
if (new Date(filters.endDate).getTime() < new Date(filters.startDate).getTime()) {
context.addIssue({
code: 'custom',
path: ['endDate'],
message: 'endDate must not be before startDate',
});
}
});
export type UsageAnalyticsFilters = z.infer<typeof UsageAnalyticsFiltersSchema>;

export const SummaryOutputSchema = z.object({
Expand Down
Loading