1+ import { existsSync , writeFileSync , mkdirSync } from 'node:fs' ;
2+
3+ // Mock logger
4+ export const mockLogger = {
5+ debug : ( ) => { } ,
6+ error : ( ) => { } ,
7+ } ;
8+
9+ // Store original functions
10+ export const originalFunctions = {
11+ existsSync,
12+ writeFileSync,
13+ mkdirSync,
14+ fetch : global . fetch ,
15+ require : require ,
16+ } ;
17+
18+ // Mock Response type
19+ export interface MockResponse extends Response {
20+ ok : boolean ;
21+ text : ( ) => Promise < string > ;
22+ headers : Headers ;
23+ redirected : boolean ;
24+ status : number ;
25+ statusText : string ;
26+ type : ResponseType ;
27+ url : string ;
28+ clone : ( ) => Response ;
29+ body : null ;
30+ bodyUsed : boolean ;
31+ bytes : ( ) => Promise < Uint8Array > ;
32+ arrayBuffer : ( ) => Promise < ArrayBuffer > ;
33+ blob : ( ) => Promise < Blob > ;
34+ formData : ( ) => Promise < FormData > ;
35+ json : ( ) => Promise < any > ;
36+ }
37+
38+ // Create a mock Response
39+ export function createMockResponse ( options : Partial < MockResponse > = { } ) : MockResponse {
40+ return {
41+ ok : true ,
42+ text : async ( ) => 'const example = true;' ,
43+ headers : new Headers ( ) ,
44+ redirected : false ,
45+ status : 200 ,
46+ statusText : 'OK' ,
47+ type : 'default' as ResponseType ,
48+ url : '' ,
49+ clone : ( ) => new Response ( ) ,
50+ body : null ,
51+ bodyUsed : false ,
52+ bytes : async ( ) => new Uint8Array ( ) ,
53+ arrayBuffer : async ( ) => new ArrayBuffer ( 0 ) ,
54+ blob : async ( ) => new Blob ( ) ,
55+ formData : async ( ) => new FormData ( ) ,
56+ json : async ( ) => ( { } ) ,
57+ ...options ,
58+ } as MockResponse ;
59+ }
60+
61+ // Setup mocks
62+ export function setupMocks ( options : {
63+ existsSync ?: ( path : string ) => boolean ;
64+ writeFileSync ?: ( path : string , content : string ) => void ;
65+ mkdirSync ?: ( path : string , options ?: any ) => void ;
66+ fetch ?: ( url : string ) => Promise < MockResponse > ;
67+ } = { } ) {
68+ // Override global functions
69+ Object . defineProperty ( global , 'existsSync' , {
70+ value : options . existsSync || ( ( ) => false ) ,
71+ configurable : true
72+ } ) ;
73+ Object . defineProperty ( global , 'writeFileSync' , {
74+ value : options . writeFileSync || ( ( ) => { } ) ,
75+ configurable : true
76+ } ) ;
77+ Object . defineProperty ( global , 'mkdirSync' , {
78+ value : options . mkdirSync || ( ( ) => { } ) ,
79+ configurable : true
80+ } ) ;
81+ Object . defineProperty ( global , 'fetch' , {
82+ value : options . fetch || ( async ( ) => createMockResponse ( ) ) ,
83+ configurable : true
84+ } ) ;
85+
86+ // Override module system
87+ ( global as any ) . require = ( module : string ) => {
88+ if ( module === './logger.js' ) {
89+ return { logger : ( ) => mockLogger } ;
90+ }
91+ if ( module === './config.js' ) {
92+ return { isDebuggingEnabled : false } ;
93+ }
94+ return originalFunctions . require ( module ) ;
95+ } ;
96+ }
97+
98+ // Restore original functions
99+ export function restoreMocks ( ) {
100+ Object . defineProperty ( global , 'existsSync' , { value : originalFunctions . existsSync } ) ;
101+ Object . defineProperty ( global , 'writeFileSync' , { value : originalFunctions . writeFileSync } ) ;
102+ Object . defineProperty ( global , 'mkdirSync' , { value : originalFunctions . mkdirSync } ) ;
103+ Object . defineProperty ( global , 'fetch' , { value : originalFunctions . fetch } ) ;
104+ ( global as any ) . require = originalFunctions . require ;
105+ }
0 commit comments