|
| 1 | +/** |
| 2 | + * Unit tests for type handling and schema conversion |
| 3 | + */ |
| 4 | + |
| 5 | +import type { MetadataExtractionStrategySchema, ExtractionOptions } from '../src/types'; |
| 6 | + |
| 7 | +describe('MetadataExtractionStrategySchema', () => { |
| 8 | + it('should accept schema as a string', () => { |
| 9 | + const schema: MetadataExtractionStrategySchema = { |
| 10 | + id: 'test', |
| 11 | + schema: '{"invoice_number": "string", "total": "number"}' |
| 12 | + }; |
| 13 | + expect(schema.id).toBe('test'); |
| 14 | + expect(schema.schema).toBe('{"invoice_number": "string", "total": "number"}'); |
| 15 | + }); |
| 16 | + |
| 17 | + it('should accept schema as an object', () => { |
| 18 | + const schema: MetadataExtractionStrategySchema = { |
| 19 | + id: 'test', |
| 20 | + schema: { invoice_number: 'string', total: 'number' } |
| 21 | + }; |
| 22 | + expect(schema.id).toBe('test'); |
| 23 | + expect(schema.schema).toEqual({ invoice_number: 'string', total: 'number' }); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should accept nested object schemas', () => { |
| 27 | + const schema: MetadataExtractionStrategySchema = { |
| 28 | + id: 'invoice-data', |
| 29 | + schema: { |
| 30 | + invoice_number: 'string', |
| 31 | + date: 'string', |
| 32 | + total_amount: 'number', |
| 33 | + vendor_name: 'string', |
| 34 | + items: [{ |
| 35 | + description: 'string', |
| 36 | + quantity: 'number', |
| 37 | + price: 'number' |
| 38 | + }] |
| 39 | + } |
| 40 | + }; |
| 41 | + expect(schema.id).toBe('invoice-data'); |
| 42 | + expect(typeof schema.schema).toBe('object'); |
| 43 | + }); |
| 44 | +}); |
| 45 | + |
| 46 | +describe('ExtractionOptions with metadataSchemas', () => { |
| 47 | + it('should accept metadataSchemas with string schema', () => { |
| 48 | + const options: ExtractionOptions = { |
| 49 | + metadataSchemas: [{ |
| 50 | + id: 'doc-info', |
| 51 | + schema: 'Extract title, author, and main topics' |
| 52 | + }] |
| 53 | + }; |
| 54 | + expect(options.metadataSchemas).toHaveLength(1); |
| 55 | + expect(options.metadataSchemas![0].schema).toBe('Extract title, author, and main topics'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should accept metadataSchemas with object schema', () => { |
| 59 | + const options: ExtractionOptions = { |
| 60 | + metadataSchemas: [{ |
| 61 | + id: 'invoice-data', |
| 62 | + schema: { |
| 63 | + invoice_number: 'string', |
| 64 | + date: 'string', |
| 65 | + total_amount: 'number', |
| 66 | + vendor_name: 'string', |
| 67 | + items: [{ |
| 68 | + description: 'string', |
| 69 | + quantity: 'number', |
| 70 | + price: 'number' |
| 71 | + }] |
| 72 | + } |
| 73 | + }] |
| 74 | + }; |
| 75 | + expect(options.metadataSchemas).toHaveLength(1); |
| 76 | + expect(options.metadataSchemas![0].id).toBe('invoice-data'); |
| 77 | + expect(typeof options.metadataSchemas![0].schema).toBe('object'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should accept mixed string and object schemas', () => { |
| 81 | + const options: ExtractionOptions = { |
| 82 | + metadataSchemas: [ |
| 83 | + { |
| 84 | + id: 'string-schema', |
| 85 | + schema: 'Extract basic info' |
| 86 | + }, |
| 87 | + { |
| 88 | + id: 'object-schema', |
| 89 | + schema: { field: 'string' } |
| 90 | + } |
| 91 | + ] |
| 92 | + }; |
| 93 | + expect(options.metadataSchemas).toHaveLength(2); |
| 94 | + expect(typeof options.metadataSchemas![0].schema).toBe('string'); |
| 95 | + expect(typeof options.metadataSchemas![1].schema).toBe('object'); |
| 96 | + }); |
| 97 | +}); |
| 98 | + |
| 99 | +describe('Schema conversion to JSON string', () => { |
| 100 | + it('should convert object schema to JSON string for API request', () => { |
| 101 | + const schemas: MetadataExtractionStrategySchema[] = [ |
| 102 | + { |
| 103 | + id: 'test', |
| 104 | + schema: { invoice_number: 'string', total: 'number' } |
| 105 | + } |
| 106 | + ]; |
| 107 | + |
| 108 | + // Simulate the conversion that happens in index.ts |
| 109 | + const normalizedSchemas = schemas.map(s => ({ |
| 110 | + id: s.id, |
| 111 | + schema: typeof s.schema === 'string' ? s.schema : JSON.stringify(s.schema) |
| 112 | + })); |
| 113 | + |
| 114 | + expect(normalizedSchemas[0].schema).toBe('{"invoice_number":"string","total":"number"}'); |
| 115 | + }); |
| 116 | + |
| 117 | + it('should keep string schema as is', () => { |
| 118 | + const schemas: MetadataExtractionStrategySchema[] = [ |
| 119 | + { |
| 120 | + id: 'test', |
| 121 | + schema: '{"invoice_number": "string"}' |
| 122 | + } |
| 123 | + ]; |
| 124 | + |
| 125 | + // Simulate the conversion that happens in index.ts |
| 126 | + const normalizedSchemas = schemas.map(s => ({ |
| 127 | + id: s.id, |
| 128 | + schema: typeof s.schema === 'string' ? s.schema : JSON.stringify(s.schema) |
| 129 | + })); |
| 130 | + |
| 131 | + expect(normalizedSchemas[0].schema).toBe('{"invoice_number": "string"}'); |
| 132 | + }); |
| 133 | + |
| 134 | + it('should handle nested object schemas', () => { |
| 135 | + const schemas: MetadataExtractionStrategySchema[] = [ |
| 136 | + { |
| 137 | + id: 'invoice-data', |
| 138 | + schema: { |
| 139 | + invoice_number: 'string', |
| 140 | + items: [{ |
| 141 | + description: 'string', |
| 142 | + quantity: 'number' |
| 143 | + }] |
| 144 | + } |
| 145 | + } |
| 146 | + ]; |
| 147 | + |
| 148 | + // Simulate the conversion that happens in index.ts |
| 149 | + const normalizedSchemas = schemas.map(s => ({ |
| 150 | + id: s.id, |
| 151 | + schema: typeof s.schema === 'string' ? s.schema : JSON.stringify(s.schema) |
| 152 | + })); |
| 153 | + |
| 154 | + const parsed = JSON.parse(normalizedSchemas[0].schema); |
| 155 | + expect(parsed.invoice_number).toBe('string'); |
| 156 | + expect(parsed.items).toHaveLength(1); |
| 157 | + expect(parsed.items[0].description).toBe('string'); |
| 158 | + }); |
| 159 | +}); |
0 commit comments