55import { datadogRollupPlugin } from '@datadog/rollup-plugin' ;
66import { outputFileSync , readFile , rmSync } from '@dd/core/helpers/fs' ;
77import { defaultPluginOptions } from '@dd/tests/_jest/helpers/mocks' ;
8+ import fsp from 'fs/promises' ;
89import os from 'os' ;
910import path from 'path' ;
1011import { rollup , type Plugin } from 'rollup' ;
1112
12- import { DEBUG_ID_SEARCH_PREFIX_BYTES , extractDebugId } from './debugId' ;
13+ import { DEBUG_ID_SEARCH_CHUNK_BYTES , extractDebugId } from './debugId' ;
1314
1415describe ( 'extractDebugId' , ( ) => {
1516 const debugId = '93fd4850-7b77-4f2e-9aa2-ba013e1a5027' ;
1617 const tempDir = path . join ( os . tmpdir ( ) , 'dd-build-plugins-debug-id-test' ) ;
1718
1819 afterEach ( ( ) => {
20+ jest . restoreAllMocks ( ) ;
1921 rmSync ( tempDir ) ;
2022 } ) ;
2123
@@ -39,6 +41,20 @@ describe('extractDebugId', () => {
3941 await expect ( extractDebugId ( filePath ) ) . resolves . toBe ( debugId ) ;
4042 } ) ;
4143
44+ test ( 'Should stop reading after finding the debug ID in the first chunk' , async ( ) => {
45+ const literal = `ddDebugId:"${ debugId } "` ;
46+ const read = jest . fn ( async ( buffer : Buffer ) => {
47+ buffer . write ( literal ) ;
48+ return { bytesRead : literal . length , buffer } ;
49+ } ) ;
50+ const close = jest . fn ( async ( ) => undefined ) ;
51+ jest . spyOn ( fsp , 'open' ) . mockResolvedValue ( { read, close } as never ) ;
52+
53+ await expect ( extractDebugId ( 'first-chunk.min.js' ) ) . resolves . toBe ( debugId ) ;
54+ expect ( read ) . toHaveBeenCalledTimes ( 1 ) ;
55+ expect ( close ) . toHaveBeenCalledTimes ( 1 ) ;
56+ } ) ;
57+
4258 test ( 'Should return undefined when there is no debug ID in the content' , async ( ) => {
4359 const filePath = path . join ( tempDir , 'no-debug-id.min.js' ) ;
4460 outputFileSync (
@@ -49,6 +65,35 @@ describe('extractDebugId', () => {
4965 await expect ( extractDebugId ( filePath ) ) . resolves . toBeUndefined ( ) ;
5066 } ) ;
5167
68+ test ( 'Should progressively find a debug ID after the first chunk' , async ( ) => {
69+ const filePath = path . join ( tempDir , 'later-debug-id.min.js' ) ;
70+ outputFileSync (
71+ filePath ,
72+ `${ 'x' . repeat ( DEBUG_ID_SEARCH_CHUNK_BYTES + 100 ) } ddDebugId:"${ debugId } "` ,
73+ ) ;
74+
75+ await expect ( extractDebugId ( filePath ) ) . resolves . toBe ( debugId ) ;
76+ } ) ;
77+
78+ test ( 'Should find a debug ID split across two chunks' , async ( ) => {
79+ const filePath = path . join ( tempDir , 'split-debug-id.min.js' ) ;
80+ const literal = `ddDebugId:"${ debugId } "` ;
81+ const literalPrefixBytes = 20 ;
82+ outputFileSync (
83+ filePath ,
84+ `${ 'x' . repeat ( DEBUG_ID_SEARCH_CHUNK_BYTES - literalPrefixBytes ) } ${ literal } ` ,
85+ ) ;
86+
87+ await expect ( extractDebugId ( filePath ) ) . resolves . toBe ( debugId ) ;
88+ } ) ;
89+
90+ test ( 'Should scan to EOF and return undefined when a large file has no debug ID' , async ( ) => {
91+ const filePath = path . join ( tempDir , 'large-no-debug-id.min.js' ) ;
92+ outputFileSync ( filePath , 'x' . repeat ( DEBUG_ID_SEARCH_CHUNK_BYTES * 3 + 100 ) ) ;
93+
94+ await expect ( extractDebugId ( filePath ) ) . resolves . toBeUndefined ( ) ;
95+ } ) ;
96+
5297 test ( 'Should return undefined when the file cannot be read' , async ( ) => {
5398 const filePath = path . join ( tempDir , 'missing.min.js' ) ;
5499
@@ -76,7 +121,7 @@ describe('extractDebugId', () => {
76121 const lateChunkTransform : Plugin = {
77122 name : 'late-chunk-transform' ,
78123 renderChunk ( code ) {
79- const padding = `/*${ 'x' . repeat ( DEBUG_ID_SEARCH_PREFIX_BYTES ) } */` ;
124+ const padding = `/*${ 'x' . repeat ( DEBUG_ID_SEARCH_CHUNK_BYTES ) } */` ;
80125 return `${ padding } \n${ code } ` ;
81126 } ,
82127 } ;
@@ -93,7 +138,7 @@ describe('extractDebugId', () => {
93138 await bundle . close ( ) ;
94139
95140 const content = await readFile ( outputPath ) ;
96- expect ( content . indexOf ( 'ddDebugId' ) ) . toBeLessThan ( DEBUG_ID_SEARCH_PREFIX_BYTES ) ;
141+ expect ( content . indexOf ( 'ddDebugId' ) ) . toBeLessThan ( DEBUG_ID_SEARCH_CHUNK_BYTES ) ;
97142 await expect ( extractDebugId ( outputPath ) ) . resolves . toMatch (
98143 / ^ [ 0 - 9 a - f ] { 8 } - [ 0 - 9 a - f ] { 4 } - 4 [ 0 - 9 a - f ] { 3 } - [ 8 9 a b ] [ 0 - 9 a - f ] { 3 } - [ 0 - 9 a - f ] { 12 } $ / ,
99144 ) ;
0 commit comments