11import { Log } from "./log" ;
2+ import { readFileSync } from "fs" ;
23
34
45// Unit tests for Log utility
@@ -62,6 +63,16 @@ describe('Log', () => {
6263 // ERROR - [07:58:18][00:00:00.0] [Object.<anonymous>(Log.spec.ts:15:17)]: Basic test
6364 expect ( logOutput [ 0 ] ) . toMatch ( / ^ .* : B a s i c t e s t ] $ / ) ;
6465 } ) ;
66+ it ( 'Log Level NoOutput' , ( ) => {
67+ Log . writeLine ( Log . LogLevels . NoOutput , 'Basic test' ) ;
68+ expect ( logOutput . length ) . toStrictEqual ( 0 ) ;
69+ } ) ;
70+ it ( 'Check Error throwning if our output is bad stores' , ( ) => {
71+ Log . throwErrorIfLogOutputFails = true ;
72+ expect ( Log . throwErrorIfLogOutputFails ) . toEqual ( true ) ;
73+ Log . throwErrorIfLogOutputFails = false ;
74+ expect ( Log . throwErrorIfLogOutputFails ) . toEqual ( false ) ;
75+ } ) ;
6576 } ) ;
6677
6778 describe ( 'Multiline logging' , ( ) => {
@@ -207,6 +218,10 @@ describe('Log', () => {
207218 Log . loggingCurrentLevel = level ;
208219 expect ( Log . loggingCurrentLevelText ) . toMatch ( expectedText ) ;
209220 } ) ;
221+ it ( 'Level is unknown (Should be impossible!)' , ( ) => {
222+ ( Log as any ) [ '_loggingCurrentLevel' ] = - 10 ;
223+ expect ( Log . loggingCurrentLevelText ) . toMatch ( "Unknown!" ) ;
224+ } ) ;
210225 it . each ( [
211226 [ "Framework debug" , "Framework debug (FKDBG)" ] ,
212227 [ "Framework information" , "Framework information (FKINF)" ] ,
@@ -277,6 +292,34 @@ describe('Log', () => {
277292
278293 } ) ;
279294
295+ describe ( 'General Attach test steps' , ( ) => {
296+ let logOutput = new Array < string > ;
297+ beforeEach ( ( ) => {
298+ logOutput = [ ] ;
299+ Log . logOutputCallback = ( message , mediaType ) => {
300+ logOutput . push ( `MediaType: [${ mediaType ?? 'Not Defined' } ], Message: [${ message } ]` ) ;
301+ } ;
302+ } ) ;
303+ it ( 'Verify a bad type is detected in error message' , ( ) => {
304+ Log . logOutputCallback = ( message , mediaType ) => {
305+ if ( mediaType === 'base64:image/png' ) throw new Error ( 'My Error' ) ;
306+ logOutput . push ( `MediaType: [${ mediaType ?? 'Not Defined' } ], Message: [${ message } ]` ) ;
307+ } ;
308+ ( Log . attach as ( logLevel : Log . LogLevels , badParam : any , mediaType : string ) => void ) ( 0 , 24 , "base64:image/png" ) ;
309+ expect ( logOutput . length ) . toEqual ( 6 ) ;
310+ expect ( logOutput [ 3 ] ) . toMatch ( / N o t a s t r i n g ! I s t y p e n u m b e r / ) ;
311+ } ) ;
312+ it ( 'Verify a very long attach is truncated to avoid results overruns' , ( ) => {
313+ Log . logOutputCallback = ( message , mediaType ) => {
314+ if ( mediaType === 'base64:image/png' ) throw new Error ( 'My Error' ) ;
315+ logOutput . push ( `MediaType: [${ mediaType ?? 'Not Defined' } ], Message: [${ message } ]` ) ;
316+ } ;
317+ Log . attach ( 0 , "A very long text that will have to be truncated so that it doesnt overrun" , "base64:image/png" )
318+ expect ( logOutput . length ) . toEqual ( 6 ) ;
319+ expect ( logOutput [ 3 ] ) . toMatch ( / A v e r y l o n g t e x t t h a t w i l .\. \. r u n / ) ;
320+ } ) ;
321+ } ) ;
322+
280323 describe ( 'Screenshot test steps' , ( ) => {
281324 let logOutput = new Array < string > ;
282325 beforeEach ( ( ) => {
@@ -295,6 +338,194 @@ describe('Log', () => {
295338 expect ( Log . screenshotSteps ) . toEqual ( true ) ;
296339 } ) ;
297340
341+ it ( 'Check screen buffer attachment' , ( ) => {
342+ const testString = "Test string" ;
343+ const testBuffer = Buffer . from ( "Test string" , 'utf8' ) ;
344+ const base64 = testBuffer . toString ( 'base64' ) ;
345+ Log . attachScreenshot ( 0 , testBuffer ) ;
346+ expect ( logOutput . length ) . toEqual ( 1 ) ;
347+ expect ( logOutput [ 0 ] ) . toMatch ( new RegExp ( `MediaType\: \[base64\:image\/png\]\, Message\: \[${ base64 } \]` . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' ) ) ) ;
348+ } ) ;
349+
350+ it ( 'Check screen string attachment' , ( ) => {
351+ const testString = "Test string" ;
352+ const testBuffer = Buffer . from ( "Test string" , 'utf8' ) ;
353+ const base64 = testBuffer . toString ( 'base64' ) ;
354+ Log . attachScreenshot ( 0 , testString ) ;
355+ expect ( logOutput . length ) . toEqual ( 1 ) ;
356+ expect ( logOutput [ 0 ] ) . toMatch ( new RegExp ( `MediaType\: \[base64\:image\/png\]\, Message\: \[${ base64 } \]` . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' ) ) ) ;
357+ } ) ;
358+
359+ it ( 'Check if Reporter doesnt like it' , ( ) => {
360+ const testText = 'No Like' ;
361+ const screenShot = 'test' ;
362+ Log . logOutputCallback = ( message , mediaType ) => {
363+ if ( mediaType === 'base64:image/png' ) throw new Error ( testText ) ;
364+ logOutput . push ( `MediaType: [${ mediaType ?? 'Not Defined' } ], Message: [${ message } ]` ) ;
365+ } ;
366+ Log . attachScreenshot ( 0 , screenShot ) ;
367+ expect ( logOutput . length ) . toEqual ( 6 ) ;
368+ expect ( logOutput [ 0 ] ) . toMatch ( / \[ E R R O R / ) ; // First is the error description
369+ expect ( logOutput [ 1 ] ) . toMatch ( new RegExp ( `\[${ testText } \]` ) ) ; // Then the error the Callback threw
370+ expect ( logOutput [ 3 ] ) // Forth line is the attachment text
371+ . toMatch ( new RegExp ( `\[${ Buffer // In our case base64 encoded screenshot
372+ . from ( screenShot , 'utf8' ) . toString ( 'base64' ) } \]`) ) ;
373+ expect ( logOutput [ 5 ] ) . toMatch ( / \[ b a s e 6 4 : i m a g e \/ p n g \] / ) ; // Last line is the media type
374+ } ) ;
375+
376+ it ( 'Check if Reporter doesnt like it but throwing an error' , ( ) => {
377+ const testText = 'No Like' ;
378+ const screenShot = 'test' ;
379+ Log . throwErrorIfLogOutputFails = true ;
380+ Log . logOutputCallback = ( message , mediaType ) => {
381+ if ( mediaType === 'base64:image/png' ) throw new Error ( testText ) ;
382+ logOutput . push ( `MediaType: [${ mediaType ?? 'Not Defined' } ], Message: [${ message } ]` ) ;
383+ } ;
384+ let errorText = "" ;
385+ try {
386+ Log . attachScreenshot ( 0 , screenShot ) ;
387+ }
388+ catch ( err ) {
389+ errorText = ( err as Error ) . message ;
390+ }
391+ logOutput = errorText . split ( '\n' ) ;
392+ expect ( logOutput . length ) . toEqual ( 6 ) ;
393+ expect ( logOutput [ 0 ] ) . toMatch ( / E r r o r t h r o w n f r o m L o g O u t p u t C a l l b a c k / ) ; // First is the error description
394+ expect ( logOutput [ 1 ] ) . toMatch ( new RegExp ( `${ testText } ` ) ) ; // Then the error the Callback threw
395+ expect ( logOutput [ 3 ] ) // Forth line is the attachment text
396+ . toMatch ( new RegExp ( `${ Buffer // In our case base64 encoded screenshot
397+ . from ( screenShot , 'utf8' ) . toString ( 'base64' ) } `) ) ;
398+ expect ( logOutput [ 5 ] ) . toMatch ( / b a s e 6 4 : i m a g e \/ p n g / ) ; // Last line is the media type
399+ } ) ;
400+
401+
402+
403+ } ) ;
404+
405+ describe ( 'Video test steps' , ( ) => {
406+ let logOutput = new Array < string > ;
407+ beforeEach ( ( ) => {
408+ logOutput = [ ] ;
409+ Log . logOutputCallback = ( message , mediaType ) => {
410+ logOutput . push ( `MediaType: [${ mediaType ?? 'Not Defined' } ], Message: [${ message } ]` ) ;
411+ } ;
412+ } ) ;
413+ afterEach ( ( ) => {
414+ Log . reset ( ) ;
415+ } ) ;
416+
417+ it ( 'Check video buffer attachment' , ( ) => {
418+ const testString = "Test string" ;
419+ const testBuffer = Buffer . from ( testString , 'utf8' ) ;
420+ const base64 = testBuffer . toString ( 'base64' ) ;
421+
422+ Log . attachVideo ( 0 , testBuffer ) ;
423+
424+ expect ( logOutput . length ) . toEqual ( 1 ) ;
425+ const videoHTML = `<video controls width="320" height="180"><source src="data:video/webm;base64,${ base64 } " type="video/webm">Video (Codec webm) not supported by browser</video>` ;
426+ const expected = `MediaType: [text/html], Message: [${ videoHTML } ]` ;
427+ expect ( logOutput [ 0 ] ) . toMatch ( new RegExp ( expected . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' ) ) ) ;
428+ } ) ;
429+
430+ it ( 'Check video buffer attachment using options' , ( ) => {
431+ const testString = "Test string" ;
432+ const width = 300 ;
433+ const height = 200 ;
434+ const codec = "Mats_Own" ;
435+ const testBuffer = Buffer . from ( testString , 'utf8' ) ;
436+ const base64 = testBuffer . toString ( 'base64' ) ;
437+
438+ Log . attachVideo ( 0 , testBuffer , { width : width , height : height , videoCodec : codec } ) ;
439+
440+ expect ( logOutput . length ) . toEqual ( 1 ) ;
441+ const videoHTML = `<video controls width="${ width } " height="${ height } "><source src="data:video/${ codec } ;base64,${ base64 } " type="video/${ codec } ">Video (Codec ${ codec } ) not supported by browser</video>` ;
442+ const expected = `MediaType: [text/html], Message: [${ videoHTML } ]` ;
443+ expect ( logOutput [ 0 ] ) . toMatch ( new RegExp ( expected . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' ) ) ) ;
444+
445+ } ) ;
446+
447+ it ( 'Check video buffer attachment with only hight modified' , ( ) => {
448+ const testString = "Test string" ;
449+ const width = 320 ;
450+ const height = 200 ;
451+ const codec = "webm" ;
452+ const testBuffer = Buffer . from ( testString , 'utf8' ) ;
453+ const base64 = testBuffer . toString ( 'base64' ) ;
454+
455+ Log . videoResolution = { height : height } ;
456+
457+ Log . attachVideo ( 0 , testBuffer ) ;
458+
459+ expect ( logOutput . length ) . toEqual ( 1 ) ;
460+ const videoHTML = `<video controls width="${ width } " height="${ height } "><source src="data:video/${ codec } ;base64,${ base64 } " type="video/${ codec } ">Video (Codec ${ codec } ) not supported by browser</video>` ;
461+ const expected = `MediaType: [text/html], Message: [${ videoHTML } ]` ;
462+ expect ( logOutput [ 0 ] ) . toMatch ( new RegExp ( expected . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' ) ) ) ;
463+
464+ } ) ;
465+
466+ it ( 'Check video buffer attachment with only width modified' , ( ) => {
467+ const testString = "Test string" ;
468+ const width = 320 ;
469+ const height = 180 ;
470+ const codec = "webm" ;
471+ const testBuffer = Buffer . from ( testString , 'utf8' ) ;
472+ const base64 = testBuffer . toString ( 'base64' ) ;
473+
474+ Log . videoResolution = { width : width } ;
475+
476+ Log . attachVideo ( 0 , testBuffer ) ;
477+
478+ expect ( logOutput . length ) . toEqual ( 1 ) ;
479+ const videoHTML = `<video controls width="${ width } " height="${ height } "><source src="data:video/${ codec } ;base64,${ base64 } " type="video/${ codec } ">Video (Codec ${ codec } ) not supported by browser</video>` ;
480+ const expected = `MediaType: [text/html], Message: [${ videoHTML } ]` ;
481+ expect ( logOutput [ 0 ] ) . toMatch ( new RegExp ( expected . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' ) ) ) ;
482+ } ) ;
483+
484+ it ( 'Check video buffer attachment as file' , ( ) => {
485+ const fileName = "testData/testData.txt" ;
486+ const testBuffer = readFileSync ( fileName ) ;
487+ const testString = testBuffer . toString ( 'utf8' ) ;
488+ const base64 = testBuffer . toString ( 'base64' ) ;
489+
490+ Log . attachVideoFile ( 0 , fileName ) ;
491+
492+ expect ( logOutput . length ) . toEqual ( 1 ) ;
493+ const videoHTML = `<video controls width="320" height="180"><source src="data:video/webm;base64,${ base64 } " type="video/webm">Video (Codec webm) not supported by browser</video>` ;
494+ const expected = `MediaType: [text/html], Message: [${ videoHTML } ]` ;
495+ expect ( logOutput [ 0 ] ) . toMatch ( new RegExp ( expected . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' ) ) ) ;
496+
497+ } ) ;
498+
499+ it ( 'Check video buffer attachment as file but bad file' , ( ) => {
500+ const fileName = "testData/testData.txt" ;
501+ const testBuffer = Buffer . from ( '' , 'utf8' ) ;
502+ const testString = testBuffer . toString ( 'utf8' ) ;
503+ const base64 = testBuffer . toString ( 'base64' ) ;
504+
505+ Log . attachVideoFile ( 0 , 'nothere/' + fileName ) ;
506+
507+ expect ( logOutput . length ) . toEqual ( 2 ) ;
508+ expect ( logOutput [ 0 ] ) . toMatch ( / \[ E R R O R .* E r r o r t h r o w n r e a d i n g v i d e o d a t a / ) ;
509+ expect ( logOutput [ 1 ] ) . toMatch ( / \[ E N O E N T .* n o s u c h f i l e o r d i r e c t o r y / ) ;
510+ } ) ;
511+
512+ } ) ;
513+
514+ describe ( 'HTML test steps' , ( ) => {
515+ let logOutput = new Array < string > ;
516+ beforeEach ( ( ) => {
517+ logOutput = [ ] ;
518+ Log . logOutputCallback = ( message , mediaType ) => {
519+ logOutput . push ( `MediaType: [${ mediaType ?? 'Not Defined' } ], Message: [${ message } ]` ) ;
520+ } ;
521+ } ) ;
522+
523+ it ( 'Check HTML attachment' , ( ) => {
524+ const testHTML = "<div>hello</div>"
525+ Log . attachHTML ( Log . LogLevels . Error , testHTML ) ;
526+ expect ( logOutput . length ) . toEqual ( 1 ) ;
527+ expect ( logOutput [ 0 ] ) . toMatch ( new RegExp ( `MediaType\: \[text\/html\]\, Message\: \[${ testHTML } \]` . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, '\\$&' ) ) ) ;
528+ } ) ;
298529 } ) ;
299530
300531 describe ( 'Validate console logging' , ( ) => {
@@ -344,6 +575,12 @@ describe('Log', () => {
344575 Log . writeLine ( Log . LogLevels . TestInformation , 'test meformation test' ) ;
345576 expect ( logSpy ) . toHaveBeenCalledTimes ( 1 ) ;
346577 } ) ;
578+ it ( 'Veg' , ( ) => {
579+ ( Log . logOutputCallback as any ) = "asdf" ;
580+ Log . attach ( 0 , "Test text" , "base64:image/png" )
581+ expect ( logSpy ) . toHaveBeenCalledTimes ( 1 ) ;
582+ expect ( logSpy ) . toHaveBeenCalledWith ( expect . stringMatching ( / L o g O u t p u t c a l l b a c k i s t y p e \[ s t r i n g \] / ) ) ;
583+ } ) ;
347584 } ) ;
348585
349586 describe ( 'Validate setting of video window dimensions' , ( ) => {
0 commit comments