diff options
| author | Bryan Morgan <[email protected]> | 2025-07-14 16:20:06 -0400 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-07-14 20:20:06 +0000 |
| commit | ff3722a3a74b09cd25b03de41933944a55db6351 (patch) | |
| tree | 9777d89dc9f984077a895c3aea2910a34eecf846 /packages/core/src/utils/safeJsonStringify.test.ts | |
| parent | 5008aea90d4ea7ac6bb5872f3702f3c7a7878ed0 (diff) | |
Fix circular reference JSON serialization in telemetry logging (#4150)
Diffstat (limited to 'packages/core/src/utils/safeJsonStringify.test.ts')
| -rw-r--r-- | packages/core/src/utils/safeJsonStringify.test.ts | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/packages/core/src/utils/safeJsonStringify.test.ts b/packages/core/src/utils/safeJsonStringify.test.ts new file mode 100644 index 00000000..9a38c048 --- /dev/null +++ b/packages/core/src/utils/safeJsonStringify.test.ts @@ -0,0 +1,73 @@ +/** + * @license + * Copyright 2025 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import { describe, it, expect } from 'vitest'; +import { safeJsonStringify } from './safeJsonStringify.js'; + +describe('safeJsonStringify', () => { + it('should stringify normal objects without issues', () => { + const obj = { name: 'test', value: 42 }; + const result = safeJsonStringify(obj); + expect(result).toBe('{"name":"test","value":42}'); + }); + + it('should handle circular references by replacing them with [Circular]', () => { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const obj: any = { name: 'test' }; + obj.circular = obj; // Create circular reference + + const result = safeJsonStringify(obj); + expect(result).toBe('{"name":"test","circular":"[Circular]"}'); + }); + + it('should handle complex circular structures like HttpsProxyAgent', () => { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const agent: any = { + sockets: {}, + options: { host: 'example.com' }, + }; + agent.sockets['example.com'] = [{ agent }]; + + const result = safeJsonStringify(agent); + expect(result).toContain('[Circular]'); + expect(result).toContain('example.com'); + }); + + it('should respect the space parameter for formatting', () => { + const obj = { name: 'test', value: 42 }; + const result = safeJsonStringify(obj, 2); + expect(result).toBe('{\n "name": "test",\n "value": 42\n}'); + }); + + it('should handle circular references with formatting', () => { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const obj: any = { name: 'test' }; + obj.circular = obj; + + const result = safeJsonStringify(obj, 2); + expect(result).toBe('{\n "name": "test",\n "circular": "[Circular]"\n}'); + }); + + it('should handle arrays with circular references', () => { + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const arr: any[] = [{ id: 1 }]; + arr[0].parent = arr; // Create circular reference + + const result = safeJsonStringify(arr); + expect(result).toBe('[{"id":1,"parent":"[Circular]"}]'); + }); + + it('should handle null and undefined values', () => { + expect(safeJsonStringify(null)).toBe('null'); + expect(safeJsonStringify(undefined)).toBe(undefined); + }); + + it('should handle primitive values', () => { + expect(safeJsonStringify('test')).toBe('"test"'); + expect(safeJsonStringify(42)).toBe('42'); + expect(safeJsonStringify(true)).toBe('true'); + }); +}); |
