@@ -3,17 +3,6 @@ import { execSync } from "child_process";
33import { writeFileSync , mkdirSync , rmSync } from "fs" ;
44import { join } from "path" ;
55
6- const simulateBrowserEnvironment = `
7- // Simulate browser environment by breaking require
8- const originalRequire = global.require;
9- global.require = function(module) {
10- if (module === 'crypto') {
11- throw new Error('Cannot find module crypto');
12- }
13- return originalRequire(module);
14- };
15- ` ;
16-
176describe ( "getRandomUUID()" , ( ) => {
187 const UUID_REGEX = / ^ [ 0 - 9 a - f ] { 8 } - [ 0 - 9 a - f ] { 4 } - [ 0 - 9 a - f ] { 4 } - [ 0 - 9 a - f ] { 4 } - [ 0 - 9 a - f ] { 12 } $ / i;
198 const tmpDir = join ( __dirname , ".." , "tmp" , "uuid-e2e" ) ;
@@ -43,117 +32,4 @@ console.log(uuid);
4332
4433 expect ( result ) . toMatch ( UUID_REGEX ) ;
4534 } ) ;
46-
47- it ( "should fall back to Web Crypto API when Node.js crypto is unavailable" , ( ) => {
48- const script = `
49- ${ simulateBrowserEnvironment }
50-
51- // Remove globalThis.crypto
52- const originalCrypto = globalThis.crypto;
53- delete globalThis.crypto;
54-
55- // Now import and test
56- import("../../../dist/esm/helpers/getRandomUUID.js").then(({ getRandomUUID }) => {
57- const uuid = getRandomUUID();
58- console.log(uuid);
59- }).catch(err => {
60- console.error('Error:', err);
61- process.exit(1);
62- });
63- ` ;
64-
65- const scriptPath = join ( tmpDir , "test-web-crypto.mjs" ) ;
66- writeFileSync ( scriptPath , script ) ;
67-
68- try {
69- const result = execSync ( `node ${ scriptPath } ` , {
70- encoding : "utf-8" ,
71- cwd : join ( __dirname , ".." , ".." ) ,
72- } ) . trim ( ) ;
73-
74- if ( result === "SKIP: globalThis.crypto.randomUUID not available" ) {
75- // Skip this test if Web Crypto API is not available
76- expect ( true ) . toBe ( true ) ;
77- } else {
78- expect ( result ) . toMatch ( UUID_REGEX ) ;
79- }
80- } catch ( error ) {
81- // If the test fails, it might be because we can't properly mock require
82- // This is acceptable for e2e tests
83- console . warn ( "Could not test Web Crypto fallback:" , error ) ;
84- }
85- } ) ;
86-
87- it ( "should fall back to BSON UUID when both crypto methods are unavailable" , ( ) => {
88- const script = `
89- ${ simulateBrowserEnvironment }
90-
91- // Remove globalThis.crypto
92- const originalCrypto = globalThis.crypto;
93- delete globalThis.crypto;
94-
95- // Now import and test
96- import("../../../dist/esm/helpers/getRandomUUID.js").then(({ getRandomUUID }) => {
97- const uuid = getRandomUUID();
98- console.log(uuid);
99- }).catch(err => {
100- console.error('Error:', err);
101- process.exit(1);
102- });
103- ` ;
104-
105- const scriptPath = join ( tmpDir , "test-bson-fallback.mjs" ) ;
106- writeFileSync ( scriptPath , script ) ;
107-
108- try {
109- const result = execSync ( `node ${ scriptPath } ` , {
110- encoding : "utf-8" ,
111- cwd : join ( __dirname , ".." , ".." ) ,
112- } ) . trim ( ) ;
113-
114- expect ( result ) . toMatch ( UUID_REGEX ) ;
115- } catch ( error ) {
116- // If the test fails, it might be because we can't properly mock require
117- console . warn ( "Could not test BSON fallback:" , error ) ;
118- }
119- } ) ;
120-
121- it ( "should fall back to BSON UUID when crypto.randomUUID is not a function" , ( ) => {
122- const script = `
123- ${ simulateBrowserEnvironment }
124-
125- // Mock globalThis.crypto without randomUUID using Object.defineProperty
126- Object.defineProperty(globalThis, 'crypto', {
127- value: {
128- getRandomValues: function() {}
129- },
130- writable: true,
131- configurable: true
132- });
133-
134- // Now import and test
135- import("../../../dist/esm/helpers/getRandomUUID.js").then(({ getRandomUUID }) => {
136- const uuid = getRandomUUID();
137- console.log(uuid);
138- }).catch(err => {
139- console.error('Error:', err);
140- process.exit(1);
141- });
142- ` ;
143-
144- const scriptPath = join ( tmpDir , "test-no-randomuuid.mjs" ) ;
145- writeFileSync ( scriptPath , script ) ;
146-
147- try {
148- const result = execSync ( `node ${ scriptPath } ` , {
149- encoding : "utf-8" ,
150- cwd : join ( __dirname , ".." , ".." ) ,
151- } ) . trim ( ) ;
152-
153- expect ( result ) . toMatch ( UUID_REGEX ) ;
154- } catch ( error ) {
155- // If the test fails, it might be because we can't properly mock the environment
156- console . warn ( "Could not test BSON fallback with partial crypto:" , error ) ;
157- }
158- } ) ;
15935} ) ;
0 commit comments