|
| 1 | +import { |
| 2 | + decodeMagicIdentifier, |
| 3 | + MAGIC_IDENTIFIER_REGEX, |
| 4 | + deobfuscateModuleId, |
| 5 | + removeFreeCallWrapper, |
| 6 | + deobfuscateText, |
| 7 | + deobfuscateTextParts, |
| 8 | +} from './magic-identifier' |
| 9 | + |
| 10 | +describe('decodeMagicIdentifier', () => { |
| 11 | + // Basic decoding tests (ported from Rust) |
| 12 | + test('decodes module evaluation', () => { |
| 13 | + expect(decodeMagicIdentifier('__TURBOPACK__module__evaluation__')).toBe( |
| 14 | + 'module evaluation' |
| 15 | + ) |
| 16 | + }) |
| 17 | + |
| 18 | + test('decodes path with slashes', () => { |
| 19 | + expect(decodeMagicIdentifier('__TURBOPACK__Hello$2f$World__')).toBe( |
| 20 | + 'Hello/World' |
| 21 | + ) |
| 22 | + }) |
| 23 | + |
| 24 | + test('decodes emoji', () => { |
| 25 | + expect(decodeMagicIdentifier('__TURBOPACK__Hello$_1f600$World__')).toBe( |
| 26 | + 'Hello😀World' |
| 27 | + ) |
| 28 | + }) |
| 29 | + |
| 30 | + test('returns unchanged if not a magic identifier', () => { |
| 31 | + expect(decodeMagicIdentifier('regular_identifier')).toBe( |
| 32 | + 'regular_identifier' |
| 33 | + ) |
| 34 | + }) |
| 35 | +}) |
| 36 | + |
| 37 | +describe('MAGIC_IDENTIFIER_REGEX', () => { |
| 38 | + test('matches magic identifiers globally', () => { |
| 39 | + const text = |
| 40 | + 'Hello __TURBOPACK__Hello__World__ and __TURBOPACK__foo$2f$bar__' |
| 41 | + const matches = text.match(MAGIC_IDENTIFIER_REGEX) |
| 42 | + expect(matches).toHaveLength(2) |
| 43 | + }) |
| 44 | +}) |
| 45 | + |
| 46 | +describe('deobfuscateModuleId', () => { |
| 47 | + test('replaces [project] with .', () => { |
| 48 | + expect( |
| 49 | + deobfuscateModuleId('[project]/examples/with-turbopack/app/foo.ts') |
| 50 | + ).toBe('./examples/with-turbopack/app/foo.ts') |
| 51 | + }) |
| 52 | + |
| 53 | + test('removes content in square brackets', () => { |
| 54 | + expect( |
| 55 | + deobfuscateModuleId('./examples/with-turbopack/app/foo.ts [app-rsc]') |
| 56 | + ).toBe('./examples/with-turbopack/app/foo.ts') |
| 57 | + }) |
| 58 | + |
| 59 | + test('removes content in parentheses', () => { |
| 60 | + expect( |
| 61 | + deobfuscateModuleId('./examples/with-turbopack/app/foo.ts (ecmascript)') |
| 62 | + ).toBe('./examples/with-turbopack/app/foo.ts') |
| 63 | + }) |
| 64 | + |
| 65 | + test('removes content in angle brackets', () => { |
| 66 | + expect( |
| 67 | + deobfuscateModuleId('./examples/with-turbopack/app/foo.ts <locals>') |
| 68 | + ).toBe('./examples/with-turbopack/app/foo.ts') |
| 69 | + }) |
| 70 | + |
| 71 | + test('handles combined cleanup', () => { |
| 72 | + expect( |
| 73 | + deobfuscateModuleId( |
| 74 | + '[project]/examples/with-turbopack/app/foo.ts [app-rsc] (ecmascript)' |
| 75 | + ) |
| 76 | + ).toBe('./examples/with-turbopack/app/foo.ts') |
| 77 | + }) |
| 78 | + |
| 79 | + test('handles parenthesis in path', () => { |
| 80 | + expect( |
| 81 | + deobfuscateModuleId( |
| 82 | + '[project]/examples/(group)/with-turbopack/app/foo.ts [app-rsc] (ecmascript)' |
| 83 | + ) |
| 84 | + ).toBe('./examples/(group)/with-turbopack/app/foo.ts') |
| 85 | + }) |
| 86 | +}) |
| 87 | + |
| 88 | +describe('removeFreeCallWrapper', () => { |
| 89 | + test('removes (0, ) wrapper', () => { |
| 90 | + expect(removeFreeCallWrapper('(0, __TURBOPACK__foo__.bar)')).toBe( |
| 91 | + '__TURBOPACK__foo__.bar' |
| 92 | + ) |
| 93 | + }) |
| 94 | + |
| 95 | + test('removes (0 , ) wrapper with spaces', () => { |
| 96 | + expect(removeFreeCallWrapper('(0 , __TURBOPACK__foo__.bar)')).toBe( |
| 97 | + '__TURBOPACK__foo__.bar' |
| 98 | + ) |
| 99 | + }) |
| 100 | + |
| 101 | + test('leaves non-free-call expressions unchanged', () => { |
| 102 | + expect(removeFreeCallWrapper('(foo, bar)')).toBe('(foo, bar)') |
| 103 | + expect(removeFreeCallWrapper('foo()')).toBe('foo()') |
| 104 | + }) |
| 105 | +}) |
| 106 | + |
| 107 | +describe('deobfuscateText', () => { |
| 108 | + test('deobfuscates complete error message with imported module', () => { |
| 109 | + const input = |
| 110 | + '(0 , __TURBOPACK__imported__module__$5b$project$5d2f$examples$2f$with$2d$turbopack$2f$app$2f$foo$2e$ts__$5b$app$2d$rsc$5d$__$28$ecmascript$29$__.foo) is not a function' |
| 111 | + const output = deobfuscateText(input) |
| 112 | + expect(output).toBe( |
| 113 | + '{imported module ./examples/with-turbopack/app/foo.ts}.foo is not a function' |
| 114 | + ) |
| 115 | + }) |
| 116 | + |
| 117 | + test('handles multiple magic identifiers', () => { |
| 118 | + const input = |
| 119 | + '__TURBOPACK__module__evaluation__ called __TURBOPACK__foo$2f$bar__' |
| 120 | + const output = deobfuscateText(input) |
| 121 | + expect(output).toBe('{module evaluation} called {foo/bar}') |
| 122 | + }) |
| 123 | + |
| 124 | + test('leaves regular text unchanged', () => { |
| 125 | + const input = 'This is a regular error message' |
| 126 | + expect(deobfuscateText(input)).toBe(input) |
| 127 | + }) |
| 128 | +}) |
| 129 | + |
| 130 | +describe('deobfuscateTextParts', () => { |
| 131 | + test('returns discriminated parts with raw and deobfuscated text', () => { |
| 132 | + const input = 'Error in __TURBOPACK__module__evaluation__ at line 10' |
| 133 | + const output = deobfuscateTextParts(input) |
| 134 | + expect(output).toEqual([ |
| 135 | + ['raw', 'Error in '], |
| 136 | + ['deobfuscated', '{module evaluation}'], |
| 137 | + ['raw', ' at line 10'], |
| 138 | + ]) |
| 139 | + }) |
| 140 | + |
| 141 | + test('handles multiple magic identifiers with interleaved raw text', () => { |
| 142 | + const input = |
| 143 | + '__TURBOPACK__module__evaluation__ called __TURBOPACK__foo$2f$bar__' |
| 144 | + const output = deobfuscateTextParts(input) |
| 145 | + expect(output).toEqual([ |
| 146 | + ['deobfuscated', '{module evaluation}'], |
| 147 | + ['raw', ' called '], |
| 148 | + ['deobfuscated', '{foo/bar}'], |
| 149 | + ]) |
| 150 | + }) |
| 151 | + |
| 152 | + test('returns single raw part for text without magic identifiers', () => { |
| 153 | + const input = 'This is a regular error message' |
| 154 | + const output = deobfuscateTextParts(input) |
| 155 | + expect(output).toEqual([['raw', 'This is a regular error message']]) |
| 156 | + }) |
| 157 | + |
| 158 | + test('handles imported module with free call wrapper', () => { |
| 159 | + const input = |
| 160 | + '(0 , __TURBOPACK__imported__module__$5b$project$5d2f$examples$2f$with$2d$turbopack$2f$app$2f$foo$2e$ts__$5b$app$2d$rsc$5d$__$28$ecmascript$29$__.foo) is not a function' |
| 161 | + const output = deobfuscateTextParts(input) |
| 162 | + expect(output).toEqual([ |
| 163 | + [ |
| 164 | + 'deobfuscated', |
| 165 | + '{imported module ./examples/with-turbopack/app/foo.ts}', |
| 166 | + ], |
| 167 | + ['raw', '.foo is not a function'], |
| 168 | + ]) |
| 169 | + }) |
| 170 | + |
| 171 | + test('produces same result as deobfuscateText when joined', () => { |
| 172 | + const input = |
| 173 | + 'Error in __TURBOPACK__module__evaluation__ at __TURBOPACK__foo$2f$bar__' |
| 174 | + const parts = deobfuscateTextParts(input) |
| 175 | + const joined = parts.map((part) => part[1]).join('') |
| 176 | + expect(joined).toBe(deobfuscateText(input)) |
| 177 | + }) |
| 178 | +}) |
0 commit comments