@@ -12,11 +12,11 @@ describe('Logger Basic Functionality', () => {
1212 let mockConsoleInfo : ReturnType < typeof vi . spyOn > ;
1313
1414 beforeEach ( ( ) => {
15- mockConsoleLog = vi . spyOn ( console , 'log' ) . mockImplementation ( ( ) => { } ) ;
16- mockConsoleWarn = vi . spyOn ( console , 'warn' ) . mockImplementation ( ( ) => { } ) ;
17- mockConsoleError = vi . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
18- mockConsoleDebug = vi . spyOn ( console , 'debug' ) . mockImplementation ( ( ) => { } ) ;
19- mockConsoleInfo = vi . spyOn ( console , 'info' ) . mockImplementation ( ( ) => { } ) ;
15+ mockConsoleLog = vi . spyOn ( console , 'log' ) . mockImplementation ( ( ) => { } ) ;
16+ mockConsoleWarn = vi . spyOn ( console , 'warn' ) . mockImplementation ( ( ) => { } ) ;
17+ mockConsoleError = vi . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
18+ mockConsoleDebug = vi . spyOn ( console , 'debug' ) . mockImplementation ( ( ) => { } ) ;
19+ mockConsoleInfo = vi . spyOn ( console , 'info' ) . mockImplementation ( ( ) => { } ) ;
2020 } ) ;
2121
2222 afterEach ( ( ) => {
@@ -136,6 +136,11 @@ describe('Logger Basic Functionality', () => {
136136 thresholdLevel : LogLevel . Verbose ,
137137 name : 'sampleLogger' ,
138138 env : 'node' ,
139+ setup ( ) {
140+ return {
141+ env : 'browser' ,
142+ } ;
143+ } ,
139144 } )
140145 . use ( consolePlugin )
141146 . build ( ) ;
@@ -170,19 +175,152 @@ describe('Logger Basic Functionality', () => {
170175
171176 expect ( executeMock ) . toHaveBeenCalledTimes ( 5 ) ;
172177 expect ( executeMock . mock . calls [ 0 ] [ 0 ] ) . toEqual (
173- '[error] ctx: sampleLogger node 0 error error message'
178+ '[error] ctx: sampleLogger browser 0 error error message'
174179 ) ;
175180 expect ( executeMock . mock . calls [ 1 ] [ 0 ] ) . toEqual (
176- '[warn] ctx: sampleLogger node 1 warn warn message'
181+ '[warn] ctx: sampleLogger browser 1 warn warn message'
177182 ) ;
178183 expect ( executeMock . mock . calls [ 2 ] [ 0 ] ) . toEqual (
179- '[info] ctx: sampleLogger node 2 info info message'
184+ '[info] ctx: sampleLogger browser 2 info info message'
180185 ) ;
181186 expect ( executeMock . mock . calls [ 3 ] [ 0 ] ) . toEqual (
182- '[debug] ctx: sampleLogger node 3 debug debug message'
187+ '[debug] ctx: sampleLogger browser 3 debug debug message'
183188 ) ;
184189 expect ( executeMock . mock . calls [ 4 ] [ 0 ] ) . toEqual (
185- '[verbose] ctx: sampleLogger node 4 verbose verbose message'
190+ '[verbose] ctx: sampleLogger browser 4 verbose verbose message'
191+ ) ;
192+ } ) ;
193+
194+ // Test case for function messages
195+ it ( 'should handle function messages correctly' , async ( ) => {
196+ const executeMock = vi . fn ( ) ;
197+
198+ const consolePlugin = definePlugin ( {
199+ pluginName : 'consolePlugin' ,
200+ execute ( { message } ) {
201+ if ( typeof message === 'string' ) {
202+ executeMock ( message ) ;
203+ } else if ( typeof message === 'object' ) {
204+ executeMock ( `${ message . prefix } ${ message . message } ` ) ;
205+ }
206+ } ,
207+ } ) ;
208+
209+ type AppContext = {
210+ userId : string ;
211+ environment : string ;
212+ } ;
213+
214+ const logger = createLogger < AppContext > ( {
215+ thresholdLevel : LogLevel . Verbose ,
216+ name : 'functionLogger' ,
217+ environment : 'production' ,
218+ userId : '' ,
219+ setup : async ( ) => Promise . resolve ( { userId : 'user-123' } ) ,
220+ } )
221+ . use ( consolePlugin )
222+ . build ( ) ;
223+ // 1. Test string-returning function message
224+ logger . info (
225+ ( ctx ) => `User ${ ctx . userId } logged in from ${ ctx . environment } `
226+ ) ;
227+
228+ // 2. Test object-returning function message
229+ logger . warn ( ( ctx ) => ( {
230+ message : `Warning for ${ ctx . userId } ` ,
231+ prefix : '[WARNING]' ,
232+ } ) ) ;
233+
234+ // 3. Test dynamic context modification
235+ logger . debug ( ( ctx ) => {
236+ // Test that context is immutable - create a new object instead
237+ const localCtx = { ...ctx , userId : 'user-456' } ;
238+ return `Updated user: ${ localCtx . userId } ` ;
239+ } ) ;
240+
241+ // 4. Test using modified context
242+ logger . info ( ( ctx ) => `Now user is ${ ctx . userId } ` ) ;
243+
244+ // 5. Test error handling
245+ logger . error ( ( ) => {
246+ throw new Error ( 'Test error in message function' ) ;
247+ } ) ;
248+
249+ await sleep ( 100 ) ;
250+
251+ // Verify results
252+ expect ( executeMock ) . toHaveBeenCalledTimes ( 5 ) ;
253+ expect ( executeMock . mock . calls [ 0 ] [ 0 ] ) . toBe (
254+ 'User user-123 logged in from production'
255+ ) ;
256+ expect ( executeMock . mock . calls [ 1 ] [ 0 ] ) . toBe ( '[WARNING] Warning for user-123' ) ;
257+ expect ( executeMock . mock . calls [ 2 ] [ 0 ] ) . toBe ( 'Updated user: user-456' ) ;
258+ expect ( executeMock . mock . calls [ 3 ] [ 0 ] ) . toBe ( 'Now user is user-123' ) ;
259+
260+ // Verify error message handling
261+ expect ( executeMock . mock . calls [ 4 ] [ 0 ] ) . toContain (
262+ 'Message function execution failed'
263+ ) ;
264+ } ) ;
265+
266+ // New test case: Handling function messages in pipeline plugins
267+ it ( 'should handle function messages in pipeline plugins' , async ( ) => {
268+ type AppContext = {
269+ env : string ;
270+ version : string ;
271+ } ;
272+
273+ const executeMock = vi . fn ( ) ;
274+
275+ const consolePlugin = definePlugin < AppContext > ( {
276+ pluginName : 'consolePlugin' ,
277+ execute ( { ctx, level, message, pipe } ) {
278+ pipe (
279+ ( ctx : AppContext ) => ctx ,
280+ // Resolve function message
281+ ( ) => message ,
282+ ( msg ) => {
283+ if ( typeof msg === 'string' ) {
284+ executeMock ( `[${ LogLevel [ level ] } ] ${ msg } ` ) ;
285+ } else {
286+ executeMock ( `[${ LogLevel [ level ] } ] ${ msg . message } ` ) ;
287+ }
288+ }
289+ ) ( ctx ) ;
290+ } ,
291+ } ) ;
292+
293+ const logger = createLogger < AppContext > ( {
294+ thresholdLevel : LogLevel . Verbose ,
295+ name : 'pipelineLogger' ,
296+ env : 'production' ,
297+ version : '1.0.0' ,
298+ } )
299+ . use ( consolePlugin )
300+ . build ( ) ;
301+
302+ // Function message - string
303+ logger . info ( ( ctx ) => `App v${ ctx . version } running in ${ ctx . env } ` ) ;
304+
305+ // Function message - object
306+ logger . warn ( ( ctx ) => ( {
307+ message : `Deprecated feature in ${ ctx . env } v${ ctx . version } ` ,
308+ } ) ) ;
309+
310+ // Regular string message
311+ logger . error ( 'Critical error occurred' ) ;
312+
313+ await sleep ( 100 ) ;
314+
315+ expect ( executeMock ) . toHaveBeenCalledTimes ( 3 ) ;
316+ expect ( executeMock . mock . calls [ 0 ] [ 0 ] ) . toBe (
317+ '[Info] App v1.0.0 running in production'
318+ ) ;
319+ expect ( executeMock . mock . calls [ 1 ] [ 0 ] ) . toBe (
320+ '[Warn] Deprecated feature in production v1.0.0'
321+ ) ;
322+ expect ( executeMock . mock . calls [ 2 ] [ 0 ] ) . toBe (
323+ '[Error] Critical error occurred'
186324 ) ;
187325 } ) ;
188- } ) ;
326+ } ) ;
0 commit comments