11import { beforeEach , describe , expect , it } from 'vitest' ;
2- import type { StackParser } from '../../../src/types-hoist/stacktrace' ;
3- import { getDebugImagesForResources , getFilenameToDebugIdMap } from '../../../src/utils/debug-ids' ;
2+ import { nodeStackLineParser } from '../../../src' ;
3+ import { clearDebugIdCache , getDebugImagesForResources , getFilenameToDebugIdMap } from '../../../src/utils/debug-ids' ;
4+ import { createStackParser } from '../../../src/utils/stacktrace' ;
45
5- describe ( 'getDebugImagesForResources' , ( ) => {
6- const mockStackParser : StackParser = ( stack : string ) => {
7- // Simple mock that extracts filename from a stack string
8- const match = stack . match ( / a t .* \( ( .* ?) : \d + : \d + \) / ) ;
9- if ( match ) {
10- return [ { filename : match [ 1 ] , function : 'mockFunction' , lineno : 1 , colno : 1 } ] ;
11- }
12- return [ ] ;
13- } ;
6+ const nodeStackParser = createStackParser ( nodeStackLineParser ( ) ) ;
147
8+ describe ( 'getDebugImagesForResources' , ( ) => {
159 beforeEach ( ( ) => {
1610 // Clear any existing debug ID maps
1711 delete ( globalThis as any ) . _sentryDebugIds ;
1812 delete ( globalThis as any ) . _debugIds ;
13+ clearDebugIdCache ( ) ;
1914 } ) ;
2015
2116 it ( 'should return debug images for resources without file:// prefix' , ( ) => {
@@ -25,7 +20,7 @@ describe('getDebugImagesForResources', () => {
2520 } ;
2621
2722 const resources = [ '/var/task/index.js' ] ;
28- const images = getDebugImagesForResources ( mockStackParser , resources ) ;
23+ const images = getDebugImagesForResources ( nodeStackParser , resources ) ;
2924
3025 expect ( images ) . toHaveLength ( 1 ) ;
3126 expect ( images [ 0 ] ) . toEqual ( {
@@ -43,7 +38,7 @@ describe('getDebugImagesForResources', () => {
4338
4439 // V8 profiler returns resources WITH file:// prefix
4540 const resources = [ 'file:///var/task/index.js' ] ;
46- const images = getDebugImagesForResources ( mockStackParser , resources ) ;
41+ const images = getDebugImagesForResources ( nodeStackParser , resources ) ;
4742
4843 expect ( images ) . toHaveLength ( 1 ) ;
4944 expect ( images [ 0 ] ) . toEqual ( {
@@ -60,7 +55,7 @@ describe('getDebugImagesForResources', () => {
6055 } ;
6156
6257 const resources = [ 'file:///var/task/index.js' , '/var/task/utils.js' ] ;
63- const images = getDebugImagesForResources ( mockStackParser , resources ) ;
58+ const images = getDebugImagesForResources ( nodeStackParser , resources ) ;
6459
6560 expect ( images ) . toHaveLength ( 2 ) ;
6661 expect ( images [ 0 ] ) . toEqual ( {
@@ -81,14 +76,14 @@ describe('getDebugImagesForResources', () => {
8176 } ;
8277
8378 const resources = [ 'file:///var/task/other.js' ] ;
84- const images = getDebugImagesForResources ( mockStackParser , resources ) ;
79+ const images = getDebugImagesForResources ( nodeStackParser , resources ) ;
8580
8681 expect ( images ) . toHaveLength ( 0 ) ;
8782 } ) ;
8883
8984 it ( 'should return empty array when no debug IDs are registered' , ( ) => {
9085 const resources = [ 'file:///var/task/index.js' ] ;
91- const images = getDebugImagesForResources ( mockStackParser , resources ) ;
86+ const images = getDebugImagesForResources ( nodeStackParser , resources ) ;
9287
9388 expect ( images ) . toHaveLength ( 0 ) ;
9489 } ) ;
@@ -99,28 +94,55 @@ describe('getDebugImagesForResources', () => {
9994 } ;
10095
10196 const resources : string [ ] = [ ] ;
102- const images = getDebugImagesForResources ( mockStackParser , resources ) ;
97+ const images = getDebugImagesForResources ( nodeStackParser , resources ) ;
10398
10499 expect ( images ) . toHaveLength ( 0 ) ;
105100 } ) ;
101+
102+ it ( 'should handle Windows paths with file:// prefix' , ( ) => {
103+ // Stack parser normalizes Windows paths: file:///C:/foo.js -> C:/foo.js
104+ ( globalThis as any ) . _sentryDebugIds = {
105+ 'at mockFunction (C:/Users/dev/project/index.js:1:1)' : 'debug-id-win-123' ,
106+ } ;
107+
108+ // V8 profiler returns Windows paths with file:// prefix
109+ const resources = [ 'file:///C:/Users/dev/project/index.js' ] ;
110+ const images = getDebugImagesForResources ( nodeStackParser , resources ) ;
111+
112+ expect ( images ) . toHaveLength ( 1 ) ;
113+ expect ( images [ 0 ] ) . toEqual ( {
114+ type : 'sourcemap' ,
115+ code_file : 'file:///C:/Users/dev/project/index.js' ,
116+ debug_id : 'debug-id-win-123' ,
117+ } ) ;
118+ } ) ;
119+
120+ it ( 'should handle Windows paths without file:// prefix' , ( ) => {
121+ ( globalThis as any ) . _sentryDebugIds = {
122+ 'at mockFunction (C:/Users/dev/project/index.js:1:1)' : 'debug-id-win-123' ,
123+ } ;
124+
125+ const resources = [ 'C:/Users/dev/project/index.js' ] ;
126+ const images = getDebugImagesForResources ( nodeStackParser , resources ) ;
127+
128+ expect ( images ) . toHaveLength ( 1 ) ;
129+ expect ( images [ 0 ] ) . toEqual ( {
130+ type : 'sourcemap' ,
131+ code_file : 'C:/Users/dev/project/index.js' ,
132+ debug_id : 'debug-id-win-123' ,
133+ } ) ;
134+ } ) ;
106135} ) ;
107136
108137describe ( 'getFilenameToDebugIdMap' , ( ) => {
109- const mockStackParser : StackParser = ( stack : string ) => {
110- const match = stack . match ( / a t .* \( ( .* ?) : \d + : \d + \) / ) ;
111- if ( match ) {
112- return [ { filename : match [ 1 ] , function : 'mockFunction' , lineno : 1 , colno : 1 } ] ;
113- }
114- return [ ] ;
115- } ;
116-
117138 beforeEach ( ( ) => {
118139 delete ( globalThis as any ) . _sentryDebugIds ;
119140 delete ( globalThis as any ) . _debugIds ;
141+ clearDebugIdCache ( ) ;
120142 } ) ;
121143
122144 it ( 'should return empty object when no debug IDs are registered' , ( ) => {
123- const map = getFilenameToDebugIdMap ( mockStackParser ) ;
145+ const map = getFilenameToDebugIdMap ( nodeStackParser ) ;
124146 expect ( map ) . toEqual ( { } ) ;
125147 } ) ;
126148
@@ -130,7 +152,7 @@ describe('getFilenameToDebugIdMap', () => {
130152 'at anotherFunction (/var/task/utils.js:10:5)' : 'debug-id-456' ,
131153 } ;
132154
133- const map = getFilenameToDebugIdMap ( mockStackParser ) ;
155+ const map = getFilenameToDebugIdMap ( nodeStackParser ) ;
134156
135157 expect ( map ) . toEqual ( {
136158 '/var/task/index.js' : 'debug-id-123' ,
@@ -143,7 +165,7 @@ describe('getFilenameToDebugIdMap', () => {
143165 'at mockFunction (/var/task/index.js:1:1)' : 'native-debug-id-123' ,
144166 } ;
145167
146- const map = getFilenameToDebugIdMap ( mockStackParser ) ;
168+ const map = getFilenameToDebugIdMap ( nodeStackParser ) ;
147169
148170 expect ( map ) . toEqual ( {
149171 '/var/task/index.js' : 'native-debug-id-123' ,
@@ -158,7 +180,7 @@ describe('getFilenameToDebugIdMap', () => {
158180 'at mockFunction (/var/task/index.js:1:1)' : 'native-debug-id' ,
159181 } ;
160182
161- const map = getFilenameToDebugIdMap ( mockStackParser ) ;
183+ const map = getFilenameToDebugIdMap ( nodeStackParser ) ;
162184
163185 expect ( map [ '/var/task/index.js' ] ) . toBe ( 'native-debug-id' ) ;
164186 } ) ;
0 commit comments