|
3 | 3 | */ |
4 | 4 | import { describe, expect, it } from 'vitest' |
5 | 5 | import { TableRowLimitError } from '@/lib/table/billing' |
6 | | -import { rootErrorMessage, rowWriteErrorResponse } from '@/app/api/table/utils' |
| 6 | +import type { ColumnDefinition } from '@/lib/table/types' |
| 7 | +import { rootErrorMessage, rowWriteErrorResponse, tableFilterError } from '@/app/api/table/utils' |
7 | 8 |
|
8 | 9 | /** Mimics drizzle's DrizzleQueryError: message is the failed SQL, real error on `cause`. */ |
9 | 10 | function wrapLikeDrizzle(cause: Error): Error { |
@@ -53,3 +54,50 @@ describe('rowWriteErrorResponse', () => { |
53 | 54 | expect(rowWriteErrorResponse(wrapLikeDrizzle(new Error('deadlock detected')))).toBeNull() |
54 | 55 | }) |
55 | 56 | }) |
| 57 | + |
| 58 | +/** |
| 59 | + * The async destructive routes (delete-async, cancel-runs, columns/run) |
| 60 | + * validate the WIRE filter here. The predicate branch must reject unknown |
| 61 | + * storage keys the way the sync bulk routes do — the `toLegacyFilter` |
| 62 | + * downgrade compiles a typo'd field into a clause that silently matches |
| 63 | + * nothing, turning a scoped delete/run into a no-op. |
| 64 | + */ |
| 65 | +describe('tableFilterError', () => { |
| 66 | + const columns: ColumnDefinition[] = [{ id: 'col_status', name: 'status', type: 'string' }] |
| 67 | + |
| 68 | + it('returns null for an absent filter and a valid id-keyed predicate', () => { |
| 69 | + expect(tableFilterError(undefined, columns)).toBeNull() |
| 70 | + expect( |
| 71 | + tableFilterError({ all: [{ field: 'col_status', op: 'eq', value: 'x' }] }, columns) |
| 72 | + ).toBeNull() |
| 73 | + expect(tableFilterError({ all: [{ field: 'createdAt', op: 'isNotNull' }] }, columns)).toBeNull() |
| 74 | + }) |
| 75 | + |
| 76 | + it('400s a predicate naming an unknown storage key', async () => { |
| 77 | + const response = tableFilterError( |
| 78 | + { all: [{ field: 'statuss', op: 'eq', value: 'x' }] }, |
| 79 | + columns |
| 80 | + ) |
| 81 | + expect(response?.status).toBe(400) |
| 82 | + const body = await response?.json() |
| 83 | + expect(body.error).toMatch(/Unknown filter column "statuss"/) |
| 84 | + }) |
| 85 | + |
| 86 | + it('400s a structurally invalid predicate (empty group, dual group keys)', () => { |
| 87 | + expect(tableFilterError({ all: [] } as never, columns)?.status).toBe(400) |
| 88 | + expect( |
| 89 | + tableFilterError( |
| 90 | + { |
| 91 | + all: [{ field: 'col_status', op: 'eq', value: 'a' }], |
| 92 | + any: [{ field: 'col_status', op: 'eq', value: 'b' }], |
| 93 | + } as never, |
| 94 | + columns |
| 95 | + )?.status |
| 96 | + ).toBe(400) |
| 97 | + }) |
| 98 | + |
| 99 | + it('still validates the legacy grammar through buildFilterClause', () => { |
| 100 | + expect(tableFilterError({ col_status: 'x' }, columns)).toBeNull() |
| 101 | + expect(tableFilterError({ col_status: { $regex: 'x' } } as never, columns)?.status).toBe(400) |
| 102 | + }) |
| 103 | +}) |
0 commit comments