|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { describe, expect, it } from 'vitest' |
| 5 | +import { |
| 6 | + listKnowledgeDocumentsQuerySchema, |
| 7 | + parseDocumentTagFiltersParam, |
| 8 | +} from '@/lib/api/contracts/knowledge/documents' |
| 9 | + |
| 10 | +describe('listKnowledgeDocumentsQuerySchema.tagFilters', () => { |
| 11 | + it('keeps tagFilters a raw string (must NOT transform to an array)', () => { |
| 12 | + // A transform-to-array here breaks requestJson outbound serialization |
| 13 | + // (the array serializes as "[object Object]"). The wire type must stay a |
| 14 | + // string; decoding happens server-side via parseDocumentTagFiltersParam. |
| 15 | + const tagFilters = JSON.stringify([ |
| 16 | + { tagSlot: 'tag1', fieldType: 'text', operator: 'contains', value: 'x' }, |
| 17 | + ]) |
| 18 | + const parsed = listKnowledgeDocumentsQuerySchema.parse({ tagFilters }) |
| 19 | + expect(parsed.tagFilters).toBe(tagFilters) |
| 20 | + expect(typeof parsed.tagFilters).toBe('string') |
| 21 | + }) |
| 22 | +}) |
| 23 | + |
| 24 | +describe('parseDocumentTagFiltersParam', () => { |
| 25 | + it('returns undefined for an absent param', () => { |
| 26 | + expect(parseDocumentTagFiltersParam(undefined)).toBeUndefined() |
| 27 | + expect(parseDocumentTagFiltersParam('')).toBeUndefined() |
| 28 | + }) |
| 29 | + |
| 30 | + it('decodes a valid JSON array of filters', () => { |
| 31 | + const filters = [ |
| 32 | + { tagSlot: 'tag1', fieldType: 'text', operator: 'contains', value: 'x' }, |
| 33 | + { tagSlot: 'date1', fieldType: 'date', operator: 'eq', value: '2026-04-21' }, |
| 34 | + ] |
| 35 | + expect(parseDocumentTagFiltersParam(JSON.stringify(filters))).toEqual(filters) |
| 36 | + }) |
| 37 | + |
| 38 | + it('throws on malformed JSON', () => { |
| 39 | + expect(() => parseDocumentTagFiltersParam('[object Object]')).toThrow() |
| 40 | + expect(() => parseDocumentTagFiltersParam('{not json')).toThrow() |
| 41 | + }) |
| 42 | + |
| 43 | + it('throws when the shape is wrong', () => { |
| 44 | + expect(() => parseDocumentTagFiltersParam(JSON.stringify([{ tagSlot: '' }]))).toThrow() |
| 45 | + }) |
| 46 | + |
| 47 | + it('rejects an operator that is not valid for the field type', () => { |
| 48 | + // unknown operator |
| 49 | + expect(() => |
| 50 | + parseDocumentTagFiltersParam( |
| 51 | + JSON.stringify([{ tagSlot: 'tag1', fieldType: 'text', operator: 'bogus', value: 'x' }]) |
| 52 | + ) |
| 53 | + ).toThrow() |
| 54 | + // valid operator name, wrong field type (contains is text-only) |
| 55 | + expect(() => |
| 56 | + parseDocumentTagFiltersParam( |
| 57 | + JSON.stringify([ |
| 58 | + { tagSlot: 'number1', fieldType: 'number', operator: 'contains', value: '1' }, |
| 59 | + ]) |
| 60 | + ) |
| 61 | + ).toThrow() |
| 62 | + }) |
| 63 | + |
| 64 | + it('rejects a fieldType that does not match the tag slot', () => { |
| 65 | + // number1 is a numeric column; claiming it is text must fail |
| 66 | + expect(() => |
| 67 | + parseDocumentTagFiltersParam( |
| 68 | + JSON.stringify([ |
| 69 | + { tagSlot: 'number1', fieldType: 'text', operator: 'contains', value: 'x' }, |
| 70 | + ]) |
| 71 | + ) |
| 72 | + ).toThrow() |
| 73 | + }) |
| 74 | + |
| 75 | + it('rejects an unknown tag slot', () => { |
| 76 | + expect(() => |
| 77 | + parseDocumentTagFiltersParam( |
| 78 | + JSON.stringify([{ tagSlot: 'tag99', fieldType: 'text', operator: 'eq', value: 'x' }]) |
| 79 | + ) |
| 80 | + ).toThrow() |
| 81 | + }) |
| 82 | + |
| 83 | + it('rejects values that are unusable for the field type', () => { |
| 84 | + // non-numeric value on a number field |
| 85 | + expect(() => |
| 86 | + parseDocumentTagFiltersParam( |
| 87 | + JSON.stringify([{ tagSlot: 'number1', fieldType: 'number', operator: 'eq', value: 'abc' }]) |
| 88 | + ) |
| 89 | + ).toThrow() |
| 90 | + // non-date value on a date field |
| 91 | + expect(() => |
| 92 | + parseDocumentTagFiltersParam( |
| 93 | + JSON.stringify([{ tagSlot: 'date1', fieldType: 'date', operator: 'eq', value: 'nope' }]) |
| 94 | + ) |
| 95 | + ).toThrow() |
| 96 | + // well-formed but impossible calendar dates (would 500 on ::date) |
| 97 | + for (const value of ['2026-02-30', '2026-99-99', '2026-13-01', '2026-00-10']) { |
| 98 | + expect(() => |
| 99 | + parseDocumentTagFiltersParam( |
| 100 | + JSON.stringify([{ tagSlot: 'date1', fieldType: 'date', operator: 'eq', value }]) |
| 101 | + ) |
| 102 | + ).toThrow() |
| 103 | + } |
| 104 | + // non-boolean value on a boolean field |
| 105 | + expect(() => |
| 106 | + parseDocumentTagFiltersParam( |
| 107 | + JSON.stringify([ |
| 108 | + { tagSlot: 'boolean1', fieldType: 'boolean', operator: 'eq', value: 'maybe' }, |
| 109 | + ]) |
| 110 | + ) |
| 111 | + ).toThrow() |
| 112 | + }) |
| 113 | + |
| 114 | + it('rejects a between filter missing a usable upper bound', () => { |
| 115 | + expect(() => |
| 116 | + parseDocumentTagFiltersParam( |
| 117 | + JSON.stringify([ |
| 118 | + { |
| 119 | + tagSlot: 'number1', |
| 120 | + fieldType: 'number', |
| 121 | + operator: 'between', |
| 122 | + value: '1', |
| 123 | + valueTo: 'x', |
| 124 | + }, |
| 125 | + ]) |
| 126 | + ) |
| 127 | + ).toThrow() |
| 128 | + }) |
| 129 | + |
| 130 | + it('accepts a valid number, date, boolean, and between filter', () => { |
| 131 | + const filters = [ |
| 132 | + { tagSlot: 'number1', fieldType: 'number', operator: 'gte', value: '42' }, |
| 133 | + { tagSlot: 'date1', fieldType: 'date', operator: 'eq', value: '2026-04-21' }, |
| 134 | + { tagSlot: 'boolean1', fieldType: 'boolean', operator: 'eq', value: 'true' }, |
| 135 | + { |
| 136 | + tagSlot: 'number2', |
| 137 | + fieldType: 'number', |
| 138 | + operator: 'between', |
| 139 | + value: '1', |
| 140 | + valueTo: '10', |
| 141 | + }, |
| 142 | + ] |
| 143 | + expect(parseDocumentTagFiltersParam(JSON.stringify(filters))).toEqual(filters) |
| 144 | + }) |
| 145 | +}) |
0 commit comments