|
| 1 | +import { serializeUploadOptions } from '../src/lib/serialization-utils'; |
| 2 | + |
| 3 | +describe('serializeUploadOptions', () => { |
| 4 | + it('should serialize all special fields correctly while preserving other fields', () => { |
| 5 | + const extensions = [ |
| 6 | + { name: 'google-auto-tagging', maxTags: 10, minConfidence: 80 }, |
| 7 | + { name: 'remove-bg', options: { bg_color: 'white' } }, |
| 8 | + ]; |
| 9 | + |
| 10 | + const customMetadata = { |
| 11 | + photographer: 'John Doe', |
| 12 | + category: 'nature', |
| 13 | + rating: 5, |
| 14 | + }; |
| 15 | + |
| 16 | + const transformation = { |
| 17 | + pre: 'w-500,h-300', |
| 18 | + post: [ |
| 19 | + { type: 'transformation', value: 'w-200,h-150' }, |
| 20 | + { type: 'gif-to-video', value: 'q-80' }, |
| 21 | + ], |
| 22 | + }; |
| 23 | + |
| 24 | + const input = { |
| 25 | + // Special fields that should be serialized |
| 26 | + tags: ['nature', 'landscape', 'photography'], |
| 27 | + responseFields: ['tags', 'customMetadata', 'isPrivateFile'], |
| 28 | + extensions, |
| 29 | + customMetadata, |
| 30 | + transformation, |
| 31 | + |
| 32 | + // Regular fields that should remain unchanged |
| 33 | + fileName: 'test-image.jpg', |
| 34 | + folder: '/photos/2024', |
| 35 | + isPrivateFile: false, |
| 36 | + useUniqueFileName: true, |
| 37 | + description: 'A beautiful landscape photo', |
| 38 | + webhookUrl: 'https://example.com/webhook', |
| 39 | + }; |
| 40 | + |
| 41 | + const result = serializeUploadOptions(input); |
| 42 | + |
| 43 | + // Assert special fields are properly serialized |
| 44 | + expect(result['tags']).toBe('nature,landscape,photography'); |
| 45 | + expect(result['responseFields']).toBe('tags,customMetadata,isPrivateFile'); |
| 46 | + expect(result['extensions']).toBe(JSON.stringify(extensions)); |
| 47 | + expect(result['customMetadata']).toBe(JSON.stringify(customMetadata)); |
| 48 | + expect(result['transformation']).toBe(JSON.stringify(transformation)); |
| 49 | + |
| 50 | + // Assert regular fields remain unchanged |
| 51 | + expect(result['fileName']).toBe('test-image.jpg'); |
| 52 | + expect(result['folder']).toBe('/photos/2024'); |
| 53 | + expect(result['isPrivateFile']).toBe(false); |
| 54 | + expect(result['useUniqueFileName']).toBe(true); |
| 55 | + expect(result['description']).toBe('A beautiful landscape photo'); |
| 56 | + expect(result['webhookUrl']).toBe('https://example.com/webhook'); |
| 57 | + |
| 58 | + // Ensure original object is not modified |
| 59 | + expect(input.tags).toEqual(['nature', 'landscape', 'photography']); |
| 60 | + expect(input.extensions).toBe(extensions); |
| 61 | + expect(input.customMetadata).toBe(customMetadata); |
| 62 | + expect(input.transformation).toBe(transformation); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should handle edge cases with null, undefined, and empty values', () => { |
| 66 | + const input = { |
| 67 | + fileName: 'test.jpg', |
| 68 | + |
| 69 | + // undefined values |
| 70 | + tags: undefined, |
| 71 | + transformation: undefined, |
| 72 | + |
| 73 | + // null values |
| 74 | + responseFields: null, |
| 75 | + customMetadata: null, |
| 76 | + |
| 77 | + // empty arrays and objects |
| 78 | + extensions: [], |
| 79 | + emptyObject: {}, |
| 80 | + emptyArray: [], |
| 81 | + |
| 82 | + // non-special arrays and objects should remain unchanged |
| 83 | + regularArray: ['item1', 'item2'], |
| 84 | + regularObject: { key: 'value' }, |
| 85 | + }; |
| 86 | + |
| 87 | + const result = serializeUploadOptions(input); |
| 88 | + |
| 89 | + // undefined values should remain undefined |
| 90 | + expect(result['tags']).toBeUndefined(); |
| 91 | + expect(result['transformation']).toBeUndefined(); |
| 92 | + |
| 93 | + // null values should remain null |
| 94 | + expect(result['responseFields']).toBeNull(); |
| 95 | + expect(result['customMetadata']).toBeNull(); |
| 96 | + |
| 97 | + // empty arrays for special fields should be serialized |
| 98 | + expect(result['extensions']).toBe('[]'); |
| 99 | + |
| 100 | + // empty arrays/objects for non-special fields should remain unchanged |
| 101 | + expect(result['emptyObject']).toEqual({}); |
| 102 | + expect(result['emptyArray']).toEqual([]); |
| 103 | + expect(result['regularArray']).toEqual(['item1', 'item2']); |
| 104 | + expect(result['regularObject']).toEqual({ key: 'value' }); |
| 105 | + |
| 106 | + // regular field should remain unchanged |
| 107 | + expect(result['fileName']).toBe('test.jpg'); |
| 108 | + }); |
| 109 | +}); |
0 commit comments