|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import { generateDebugId, injectDebugIdIntoJs, injectDebugIdIntoSourceMap } from './debug-id'; |
| 10 | + |
| 11 | +const UUID = /^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/; |
| 12 | + |
| 13 | +describe('debug-id', () => { |
| 14 | + describe('generateDebugId', () => { |
| 15 | + it('produces a canonical UUIDv5 string', () => { |
| 16 | + expect(generateDebugId('hello')).toMatch(UUID); |
| 17 | + expect(generateDebugId(new TextEncoder().encode('hello'))).toMatch(UUID); |
| 18 | + }); |
| 19 | + |
| 20 | + it('is deterministic for identical inputs', () => { |
| 21 | + expect(generateDebugId('same content')).toBe(generateDebugId('same content')); |
| 22 | + }); |
| 23 | + |
| 24 | + it('differs for different inputs', () => { |
| 25 | + expect(generateDebugId('one')).not.toBe(generateDebugId('two')); |
| 26 | + }); |
| 27 | + }); |
| 28 | + |
| 29 | + describe('injectDebugIdIntoJs', () => { |
| 30 | + const id = '11111111-2222-5333-9444-555555555555'; |
| 31 | + |
| 32 | + it('inserts the debugId comment immediately above sourceMappingURL', () => { |
| 33 | + const text = 'console.log(1);\n//# sourceMappingURL=foo.js.map\n'; |
| 34 | + const result = injectDebugIdIntoJs(text, id); |
| 35 | + expect(result).toBe( |
| 36 | + 'console.log(1);\n//# debugId=11111111-2222-5333-9444-555555555555\n//# sourceMappingURL=foo.js.map\n', |
| 37 | + ); |
| 38 | + }); |
| 39 | + |
| 40 | + it('appends the comment when no sourceMappingURL is present', () => { |
| 41 | + const text = 'console.log(1);\n'; |
| 42 | + const result = injectDebugIdIntoJs(text, id); |
| 43 | + expect(result).toBe('console.log(1);\n//# debugId=11111111-2222-5333-9444-555555555555\n'); |
| 44 | + }); |
| 45 | + |
| 46 | + it('appends a leading newline when input does not end with one', () => { |
| 47 | + const text = 'console.log(1);'; |
| 48 | + const result = injectDebugIdIntoJs(text, id); |
| 49 | + expect(result).toBe('console.log(1);\n//# debugId=11111111-2222-5333-9444-555555555555\n'); |
| 50 | + }); |
| 51 | + |
| 52 | + it('replaces an existing debugId comment (idempotent)', () => { |
| 53 | + const original = |
| 54 | + 'console.log(1);\n//# debugId=00000000-0000-5000-8000-000000000000\n//# sourceMappingURL=foo.js.map\n'; |
| 55 | + const result = injectDebugIdIntoJs(original, id); |
| 56 | + expect(result).toBe( |
| 57 | + 'console.log(1);\n//# debugId=11111111-2222-5333-9444-555555555555\n//# sourceMappingURL=foo.js.map\n', |
| 58 | + ); |
| 59 | + // Re-running with the same id is a no-op. |
| 60 | + expect(injectDebugIdIntoJs(result, id)).toBe(result); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + describe('injectDebugIdIntoSourceMap', () => { |
| 65 | + const id = '11111111-2222-5333-9444-555555555555'; |
| 66 | + |
| 67 | + it('adds a top-level debugId field', () => { |
| 68 | + const map = JSON.stringify({ version: 3, sources: ['a.ts'], mappings: '' }); |
| 69 | + const updated = JSON.parse(injectDebugIdIntoSourceMap(map, id)); |
| 70 | + expect(updated.debugId).toBe(id); |
| 71 | + expect(updated.version).toBe(3); |
| 72 | + }); |
| 73 | + |
| 74 | + it('overwrites an existing debugId field', () => { |
| 75 | + const map = JSON.stringify({ version: 3, debugId: 'old', mappings: '' }); |
| 76 | + const updated = JSON.parse(injectDebugIdIntoSourceMap(map, id)); |
| 77 | + expect(updated.debugId).toBe(id); |
| 78 | + }); |
| 79 | + |
| 80 | + it('returns the original input when debugId already matches', () => { |
| 81 | + const map = |
| 82 | + '{\n "version": 3,\n "debugId": "11111111-2222-5333-9444-555555555555",\n "mappings": ""\n}'; |
| 83 | + expect(injectDebugIdIntoSourceMap(map, id)).toBe(map); |
| 84 | + }); |
| 85 | + |
| 86 | + it('preserves indentation when updating a pretty-printed source map', () => { |
| 87 | + const map = '{\n\t"version": 3,\n\t"mappings": ""\n}'; |
| 88 | + expect(injectDebugIdIntoSourceMap(map, id)).toBe( |
| 89 | + '{\n\t"version": 3,\n\t"mappings": "",\n\t"debugId": "11111111-2222-5333-9444-555555555555"\n}', |
| 90 | + ); |
| 91 | + }); |
| 92 | + |
| 93 | + it('returns the original input when JSON is malformed', () => { |
| 94 | + const malformed = '{ this is not json'; |
| 95 | + expect(injectDebugIdIntoSourceMap(malformed, id)).toBe(malformed); |
| 96 | + }); |
| 97 | + }); |
| 98 | +}); |
0 commit comments