|
| 1 | +/** |
| 2 | + * Tests for serialization utilities to prevent DataCloneError |
| 3 | + * |
| 4 | + * The structured clone algorithm used by postMessage cannot handle: |
| 5 | + * - Functions |
| 6 | + * - DOM nodes |
| 7 | + * - MediaStreamTrack objects |
| 8 | + * - Symbols |
| 9 | + * - WeakMap/WeakSet |
| 10 | + * - Error objects (partially) |
| 11 | + * |
| 12 | + * These tests verify that our sanitization utilities properly handle these cases. |
| 13 | + */ |
| 14 | + |
| 15 | +import { sanitizeForPostMessage, describeMediaSource } from '../vapi'; |
| 16 | + |
| 17 | +describe('sanitizeForPostMessage', () => { |
| 18 | + it('should pass through primitive values unchanged', () => { |
| 19 | + expect(sanitizeForPostMessage('hello')).toBe('hello'); |
| 20 | + expect(sanitizeForPostMessage(123)).toBe(123); |
| 21 | + expect(sanitizeForPostMessage(true)).toBe(true); |
| 22 | + expect(sanitizeForPostMessage(false)).toBe(false); |
| 23 | + expect(sanitizeForPostMessage(null)).toBe(null); |
| 24 | + expect(sanitizeForPostMessage(undefined)).toBe(undefined); |
| 25 | + }); |
| 26 | + |
| 27 | + it('should pass through simple objects unchanged', () => { |
| 28 | + const obj = { a: 1, b: 'test', c: true }; |
| 29 | + const result = sanitizeForPostMessage(obj); |
| 30 | + expect(result).toEqual(obj); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should pass through arrays unchanged', () => { |
| 34 | + const arr = [1, 2, 'test', true]; |
| 35 | + const result = sanitizeForPostMessage(arr); |
| 36 | + expect(result).toEqual(arr); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should convert functions to descriptive strings', () => { |
| 40 | + const fn = function testFunc() { return 42; }; |
| 41 | + const result = sanitizeForPostMessage(fn); |
| 42 | + expect(result).toBe('[Function: testFunc]'); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should convert arrow functions to descriptive strings', () => { |
| 46 | + const fn = () => 42; |
| 47 | + const result = sanitizeForPostMessage(fn); |
| 48 | + expect(typeof result).toBe('string'); |
| 49 | + expect(result).toContain('[Function'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should convert anonymous functions to descriptive strings', () => { |
| 53 | + // Note: Modern JS engines infer function names from variable assignments |
| 54 | + // so `const fn = function() {}` results in a function named 'fn' |
| 55 | + // To get a truly anonymous function, we need to pass it directly |
| 56 | + const result = sanitizeForPostMessage(function() { return 42; }); |
| 57 | + expect(result).toBe('[Function: anonymous]'); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should sanitize nested objects with functions', () => { |
| 61 | + const obj = { |
| 62 | + name: 'test', |
| 63 | + callback: () => {}, |
| 64 | + nested: { |
| 65 | + fn: function handler() {} |
| 66 | + } |
| 67 | + }; |
| 68 | + const result = sanitizeForPostMessage(obj); |
| 69 | + expect(result).toEqual({ |
| 70 | + name: 'test', |
| 71 | + callback: '[Function: callback]', // Arrow functions in object properties get inferred names |
| 72 | + nested: { |
| 73 | + fn: '[Function: handler]' |
| 74 | + } |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should sanitize arrays containing functions', () => { |
| 79 | + const arr = [1, () => {}, 'test']; |
| 80 | + const result = sanitizeForPostMessage(arr) as unknown[]; |
| 81 | + expect(Array.isArray(result)).toBe(true); |
| 82 | + expect(result[0]).toBe(1); |
| 83 | + expect(typeof result[1]).toBe('string'); |
| 84 | + expect(result[1]).toContain('[Function'); |
| 85 | + expect(result[2]).toBe('test'); |
| 86 | + }); |
| 87 | + |
| 88 | + it('should convert Symbol values to strings', () => { |
| 89 | + const sym = Symbol('test'); |
| 90 | + const result = sanitizeForPostMessage(sym); |
| 91 | + expect(result).toBe('Symbol(test)'); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should handle objects with Symbol values', () => { |
| 95 | + const obj = { |
| 96 | + name: 'test', |
| 97 | + sym: Symbol('mySymbol') |
| 98 | + }; |
| 99 | + const result = sanitizeForPostMessage(obj); |
| 100 | + expect(result).toEqual({ |
| 101 | + name: 'test', |
| 102 | + sym: 'Symbol(mySymbol)' |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should handle Date objects', () => { |
| 107 | + const date = new Date('2024-01-15T12:00:00Z'); |
| 108 | + const result = sanitizeForPostMessage(date); |
| 109 | + // Dates should be converted to ISO strings for safe serialization |
| 110 | + expect(result).toBe(date.toISOString()); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should handle objects with circular references by returning a placeholder', () => { |
| 114 | + const obj: any = { name: 'test' }; |
| 115 | + obj.self = obj; |
| 116 | + // This should not throw and should handle the circular reference |
| 117 | + const result = sanitizeForPostMessage(obj) as Record<string, unknown>; |
| 118 | + expect(result).toBeDefined(); |
| 119 | + expect(result.name).toBe('test'); |
| 120 | + expect(result.self).toBe('[Circular Reference]'); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should handle deeply nested structures', () => { |
| 124 | + const obj = { |
| 125 | + level1: { |
| 126 | + level2: { |
| 127 | + level3: { |
| 128 | + value: 'deep', |
| 129 | + fn: () => {} |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + }; |
| 134 | + const result = sanitizeForPostMessage(obj) as any; |
| 135 | + expect(result.level1.level2.level3.value).toBe('deep'); |
| 136 | + expect(result.level1.level2.level3.fn).toContain('[Function'); |
| 137 | + }); |
| 138 | +}); |
| 139 | + |
| 140 | +describe('describeMediaSource', () => { |
| 141 | + it('should return boolean values as-is', () => { |
| 142 | + expect(describeMediaSource(true)).toBe(true); |
| 143 | + expect(describeMediaSource(false)).toBe(false); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should return string device IDs as-is', () => { |
| 147 | + expect(describeMediaSource('device-123')).toBe('device-123'); |
| 148 | + }); |
| 149 | + |
| 150 | + it('should describe MediaStreamTrack objects', () => { |
| 151 | + // Create a mock MediaStreamTrack |
| 152 | + const mockTrack = { |
| 153 | + kind: 'audio', |
| 154 | + id: 'track-abc123', |
| 155 | + label: 'Built-in Microphone', |
| 156 | + }; |
| 157 | + |
| 158 | + const result = describeMediaSource(mockTrack as unknown as MediaStreamTrack); |
| 159 | + expect(result).toBe('[MediaStreamTrack: audio, id=track-abc123]'); |
| 160 | + }); |
| 161 | + |
| 162 | + it('should handle MediaStreamTrack without label', () => { |
| 163 | + const mockTrack = { |
| 164 | + kind: 'video', |
| 165 | + id: 'track-xyz789', |
| 166 | + }; |
| 167 | + |
| 168 | + const result = describeMediaSource(mockTrack as unknown as MediaStreamTrack); |
| 169 | + expect(result).toBe('[MediaStreamTrack: video, id=track-xyz789]'); |
| 170 | + }); |
| 171 | + |
| 172 | + it('should handle null and undefined', () => { |
| 173 | + expect(describeMediaSource(null as any)).toBe(null); |
| 174 | + expect(describeMediaSource(undefined as any)).toBe(undefined); |
| 175 | + }); |
| 176 | +}); |
| 177 | + |
| 178 | +describe('DataCloneError prevention in call-start-progress events', () => { |
| 179 | + it('should produce serializable metadata when audioSource is a MediaStreamTrack', () => { |
| 180 | + // Simulate what happens when a MediaStreamTrack is passed as audioSource |
| 181 | + const mockTrack = { |
| 182 | + kind: 'audio', |
| 183 | + id: 'track-123', |
| 184 | + readyState: 'live', |
| 185 | + enabled: true, |
| 186 | + }; |
| 187 | + |
| 188 | + const metadata = { |
| 189 | + audioSource: describeMediaSource(mockTrack as unknown as MediaStreamTrack), |
| 190 | + videoSource: describeMediaSource(true), |
| 191 | + isVideoRecordingEnabled: false, |
| 192 | + isVideoEnabled: false, |
| 193 | + }; |
| 194 | + |
| 195 | + // Verify it can be JSON serialized (which postMessage also requires) |
| 196 | + expect(() => JSON.stringify(metadata)).not.toThrow(); |
| 197 | + |
| 198 | + // Verify the values are correct |
| 199 | + expect(metadata.audioSource).toBe('[MediaStreamTrack: audio, id=track-123]'); |
| 200 | + expect(metadata.videoSource).toBe(true); |
| 201 | + }); |
| 202 | + |
| 203 | + it('should handle typical call-start-progress event metadata', () => { |
| 204 | + const mockAudioTrack = { |
| 205 | + kind: 'audio', |
| 206 | + id: 'audio-track-456', |
| 207 | + readyState: 'live', |
| 208 | + enabled: true, |
| 209 | + }; |
| 210 | + |
| 211 | + const mockVideoTrack = { |
| 212 | + kind: 'video', |
| 213 | + id: 'video-track-789', |
| 214 | + readyState: 'live', |
| 215 | + enabled: true, |
| 216 | + }; |
| 217 | + |
| 218 | + const progressEvent = { |
| 219 | + stage: 'daily-call-object-creation', |
| 220 | + status: 'started', |
| 221 | + timestamp: new Date().toISOString(), |
| 222 | + metadata: { |
| 223 | + audioSource: describeMediaSource(mockAudioTrack as unknown as MediaStreamTrack), |
| 224 | + videoSource: describeMediaSource(mockVideoTrack as unknown as MediaStreamTrack), |
| 225 | + isVideoRecordingEnabled: true, |
| 226 | + isVideoEnabled: false, |
| 227 | + } |
| 228 | + }; |
| 229 | + |
| 230 | + // Verify it can be serialized |
| 231 | + expect(() => JSON.stringify(progressEvent)).not.toThrow(); |
| 232 | + |
| 233 | + // Verify structure |
| 234 | + expect(progressEvent.metadata.audioSource).toBe('[MediaStreamTrack: audio, id=audio-track-456]'); |
| 235 | + expect(progressEvent.metadata.videoSource).toBe('[MediaStreamTrack: video, id=video-track-789]'); |
| 236 | + }); |
| 237 | +}); |
0 commit comments