|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | + |
| 3 | +// Copy of the validation function from image.ts for testing |
| 4 | +function validateNoUrlsInConfig(obj: any, path: string = ''): void { |
| 5 | + if (typeof obj === 'string') { |
| 6 | + if (obj.startsWith('http://') || obj.startsWith('https://')) { |
| 7 | + throw new Error( |
| 8 | + `Invalid configuration: Found full URL instead of key at ${path || 'root'}. ` + |
| 9 | + `URL: "${obj.slice(0, 100)}${obj.length > 100 ? '...' : ''}". ` + |
| 10 | + `All URLs must be converted to storage keys before database insertion.`, |
| 11 | + ); |
| 12 | + } |
| 13 | + } else if (Array.isArray(obj)) { |
| 14 | + obj.forEach((item, index) => { |
| 15 | + validateNoUrlsInConfig(item, `${path}[${index}]`); |
| 16 | + }); |
| 17 | + } else if (obj && typeof obj === 'object') { |
| 18 | + Object.entries(obj).forEach(([key, value]) => { |
| 19 | + const currentPath = path ? `${path}.${key}` : key; |
| 20 | + validateNoUrlsInConfig(value, currentPath); |
| 21 | + }); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +describe('imageRouter', () => { |
| 26 | + describe('validateNoUrlsInConfig utility', () => { |
| 27 | + describe('valid configurations', () => { |
| 28 | + it('should pass with normal keys', () => { |
| 29 | + const config = { |
| 30 | + imageUrl: 'images/photo.jpg', |
| 31 | + imageUrls: ['files/doc.pdf', 'assets/video.mp4'], |
| 32 | + prompt: 'Generate an image', |
| 33 | + }; |
| 34 | + |
| 35 | + expect(() => validateNoUrlsInConfig(config)).not.toThrow(); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should pass with empty strings', () => { |
| 39 | + const config = { |
| 40 | + imageUrl: '', |
| 41 | + imageUrls: [], |
| 42 | + prompt: 'Generate an image', |
| 43 | + }; |
| 44 | + |
| 45 | + expect(() => validateNoUrlsInConfig(config)).not.toThrow(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should pass with null/undefined values', () => { |
| 49 | + const config = { |
| 50 | + imageUrl: null, |
| 51 | + imageUrls: undefined, |
| 52 | + prompt: 'Generate an image', |
| 53 | + }; |
| 54 | + |
| 55 | + expect(() => validateNoUrlsInConfig(config)).not.toThrow(); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe('invalid configurations', () => { |
| 60 | + it('should throw for https URL in imageUrl', () => { |
| 61 | + const config = { |
| 62 | + imageUrl: 'https://s3.amazonaws.com/bucket/image.jpg', |
| 63 | + prompt: 'Generate an image', |
| 64 | + }; |
| 65 | + |
| 66 | + expect(() => validateNoUrlsInConfig(config)).toThrow( |
| 67 | + 'Invalid configuration: Found full URL instead of key at imageUrl', |
| 68 | + ); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should throw for http URL in imageUrls array', () => { |
| 72 | + const config = { |
| 73 | + imageUrls: ['files/doc.pdf', 'http://example.com/image.jpg'], |
| 74 | + prompt: 'Generate an image', |
| 75 | + }; |
| 76 | + |
| 77 | + expect(() => validateNoUrlsInConfig(config)).toThrow( |
| 78 | + 'Invalid configuration: Found full URL instead of key at imageUrls[1]', |
| 79 | + ); |
| 80 | + }); |
| 81 | + |
| 82 | + it('should throw for nested URL in complex object', () => { |
| 83 | + const config = { |
| 84 | + settings: { |
| 85 | + imageConfig: { |
| 86 | + url: 'https://cdn.example.com/very-long-url-that-exceeds-100-characters-to-test-truncation-functionality.jpg', |
| 87 | + }, |
| 88 | + }, |
| 89 | + }; |
| 90 | + |
| 91 | + expect(() => validateNoUrlsInConfig(config)).toThrow( |
| 92 | + 'Invalid configuration: Found full URL instead of key at settings.imageConfig.url', |
| 93 | + ); |
| 94 | + expect(() => validateNoUrlsInConfig(config)).toThrow( |
| 95 | + 'https://cdn.example.com/very-long-url-that-exceeds-100-characters-to-test-truncation-func', |
| 96 | + ); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should throw for presigned URL with query parameters', () => { |
| 100 | + const config = { |
| 101 | + imageUrl: |
| 102 | + 'https://s3.amazonaws.com/bucket/file.jpg?X-Amz-Signature=abc&X-Amz-Expires=3600', |
| 103 | + }; |
| 104 | + |
| 105 | + expect(() => validateNoUrlsInConfig(config)).toThrow( |
| 106 | + 'All URLs must be converted to storage keys before database insertion', |
| 107 | + ); |
| 108 | + }); |
| 109 | + }); |
| 110 | + |
| 111 | + describe('edge cases', () => { |
| 112 | + it('should handle deeply nested structures', () => { |
| 113 | + const config = { |
| 114 | + level1: { |
| 115 | + level2: { |
| 116 | + level3: { |
| 117 | + level4: ['normal-key', 'https://bad-url.com'], |
| 118 | + }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + }; |
| 122 | + |
| 123 | + expect(() => validateNoUrlsInConfig(config)).toThrow( |
| 124 | + 'Invalid configuration: Found full URL instead of key at level1.level2.level3.level4[1]', |
| 125 | + ); |
| 126 | + }); |
| 127 | + |
| 128 | + it('should not throw for strings that contain but do not start with http', () => { |
| 129 | + const config = { |
| 130 | + imageUrl: 'some-prefix-https://example.com', |
| 131 | + description: 'This text contains http:// but is not a URL', |
| 132 | + }; |
| 133 | + |
| 134 | + expect(() => validateNoUrlsInConfig(config)).not.toThrow(); |
| 135 | + }); |
| 136 | + }); |
| 137 | + }); |
| 138 | +}); |
0 commit comments