55
66import { LogFormatter } from '../formatter' ;
77import { LogLevel } from '../types' ;
8+ import { styleData } from '../formatter/data-formatter' ;
89
910describe ( 'LogFormatter' , ( ) => {
1011 it ( 'should format messages with timestamp and level' , ( ) => {
@@ -94,8 +95,9 @@ describe('LogFormatter', () => {
9495 expect ( formatted ) . toContain ( message ) ;
9596
9697 // Should contain timestamp components
97- expect ( formatted ) . toMatch ( / \[ \d { 4 } - \d { 2 } - \d { 2 } T \d { 2 } : \d { 2 } : \d { 2 } \. \d { 3 } Z \] / ) ; // ISO timestamp
98- expect ( formatted ) . toMatch ( / \[ \d { 1 , 2 } : \d { 2 } [ A P ] M \] / ) ; // Local time
98+ const cleanFormatted = formatted . replace ( / \x1b \[ [ 0 - 9 ; ] * m / g, '' ) ;
99+ expect ( cleanFormatted ) . toMatch ( / \[ \d { 4 } - \d { 2 } - \d { 2 } T \d { 2 } : \d { 2 } : \d { 2 } \. \d { 3 } Z \] / ) ; // ISO timestamp
100+ expect ( cleanFormatted ) . toMatch ( / \[ \d { 1 , 2 } : \d { 2 } [ A P ] M \] / ) ; // Local time
99101 } ) ;
100102
101103 it ( 'should format system messages with colors' , ( ) => {
@@ -122,4 +124,124 @@ describe('LogFormatter', () => {
122124 expect ( cleanFormatted ) . toMatch ( formatPattern ) ;
123125 } ) ;
124126 } ) ;
127+
128+ describe ( 'formatData edge cases' , ( ) => {
129+ it ( 'should handle null values' , ( ) => {
130+ // Test null handling (covers line 98)
131+ const formatted = LogFormatter . format ( LogLevel . INFO , 'Message' , null ) ;
132+ expect ( formatted ) . toContain ( 'null' ) ;
133+ } ) ;
134+
135+ it ( 'should handle undefined values' , ( ) => {
136+ // Test undefined handling (covers line 102)
137+ const formatted = LogFormatter . format ( LogLevel . INFO , 'Message' , undefined ) ;
138+ expect ( formatted ) . toContain ( 'Message' ) ;
139+ // The formatted message should not contain 'undefined'
140+ // Remove ANSI color codes before pattern matching
141+ const cleanFormatted = formatted . replace ( / \x1b \[ [ 0 - 9 ; ] * m / g, '' ) ;
142+ expect ( cleanFormatted ) . toMatch ( / M e s s a g e $ / ) ;
143+ } ) ;
144+
145+ it ( 'should handle number values' , ( ) => {
146+ // Test number handling (covers line 106)
147+ const formatted = LogFormatter . format ( LogLevel . INFO , 'Message' , 42 ) ;
148+ expect ( formatted ) . toContain ( '42' ) ;
149+ } ) ;
150+
151+ it ( 'should handle boolean values' , ( ) => {
152+ // Test boolean handling (covers line 110)
153+ const formatted = LogFormatter . format ( LogLevel . INFO , 'Message' , true ) ;
154+ expect ( formatted ) . toContain ( 'true' ) ;
155+
156+ const formatted2 = LogFormatter . format ( LogLevel . INFO , 'Message' , false ) ;
157+ expect ( formatted2 ) . toContain ( 'false' ) ;
158+ } ) ;
159+
160+ it ( 'should handle objects that cannot be stringified' , ( ) => {
161+ // Test JSON.stringify error handling (covers line 117)
162+ const circularObj : any = { } ;
163+ circularObj . self = circularObj ; // Create circular reference
164+
165+ const formatted = LogFormatter . format ( LogLevel . INFO , 'Message' , circularObj ) ;
166+ expect ( formatted ) . toContain ( '[Object]' ) ;
167+ } ) ;
168+
169+ it ( 'should handle string data types' , ( ) => {
170+ // Test to cover line 106 - string handling
171+ const formatted = LogFormatter . format ( LogLevel . INFO , 'Message' , 'test string' ) ;
172+ expect ( formatted ) . toContain ( 'test string' ) ;
173+ } ) ;
174+
175+ it ( 'should handle undefined values in formatData' , ( ) => {
176+ // Test to cover line 102 - undefined returns empty string
177+ // Import the formatData function from the new modular structure
178+ const { formatData } = require ( '../formatter/data-formatter' ) ;
179+ const result = formatData ( undefined ) ;
180+ expect ( result ) . toBe ( '' ) ;
181+ } ) ;
182+
183+ it ( 'should handle Symbol values in formatData' , ( ) => {
184+ // Test to verify Symbol handling - should return string representation
185+ const { formatData } = require ( '../formatter/data-formatter' ) ;
186+ const testSymbol = Symbol ( 'test' ) ;
187+ const result = formatData ( testSymbol ) ;
188+ expect ( typeof result ) . toBe ( 'string' ) ;
189+ expect ( result ) . toBe ( 'Symbol(test)' ) ;
190+ } ) ;
191+
192+ it ( 'should handle Symbol values without description in formatData' , ( ) => {
193+ // Test to verify Symbol handling for symbols without description
194+ const { formatData } = require ( '../formatter/data-formatter' ) ;
195+ const testSymbol = Symbol ( ) ;
196+ const result = formatData ( testSymbol ) ;
197+ expect ( typeof result ) . toBe ( 'string' ) ;
198+ expect ( result ) . toBe ( 'Symbol()' ) ;
199+ } ) ;
200+ } ) ;
201+
202+ describe ( 'styleData function' , ( ) => {
203+ const mockColors = { data : '\x1b[36m' , reset : '\x1b[0m' } ;
204+
205+ it ( 'should return empty string for empty dataString' , ( ) => {
206+ // Test to cover line 45 - empty string case
207+ expect ( styleData ( '' , mockColors ) ) . toBe ( '' ) ;
208+ } ) ;
209+
210+ it ( 'should return empty string for null/undefined dataString' , ( ) => {
211+ // Test to cover line 45 - falsy values
212+ expect ( styleData ( null as any , mockColors ) ) . toBe ( '' ) ;
213+ expect ( styleData ( undefined as any , mockColors ) ) . toBe ( '' ) ;
214+ } ) ;
215+
216+ it ( 'should style non-empty data string correctly' , ( ) => {
217+ // Test normal case
218+ const result = styleData ( 'test data' , mockColors ) ;
219+ expect ( result ) . toBe ( ` ${ mockColors . data } test data${ mockColors . reset } ` ) ;
220+ } ) ;
221+ } ) ;
222+
223+ describe ( 'Module exports' , ( ) => {
224+ it ( 'should export all formatter functions and classes' , ( ) => {
225+ const formatter = require ( '../formatter' ) ;
226+
227+ // Test that all expected exports are available
228+ expect ( formatter . MessageFormatter ) . toBeDefined ( ) ;
229+ expect ( formatter . LogFormatter ) . toBeDefined ( ) ; // Backward compatibility
230+ expect ( formatter . colors ) . toBeDefined ( ) ;
231+ expect ( formatter . colorScheme ) . toBeDefined ( ) ;
232+ expect ( formatter . getTimestampComponents ) . toBeDefined ( ) ;
233+ expect ( formatter . formatTimestamp ) . toBeDefined ( ) ;
234+ expect ( formatter . formatData ) . toBeDefined ( ) ;
235+ expect ( formatter . styleData ) . toBeDefined ( ) ;
236+
237+ // Test that LogFormatter is alias for MessageFormatter
238+ expect ( formatter . LogFormatter ) . toBe ( formatter . MessageFormatter ) ;
239+
240+ // Test that functions are callable
241+ expect ( typeof formatter . getTimestampComponents ) . toBe ( 'function' ) ;
242+ expect ( typeof formatter . formatTimestamp ) . toBe ( 'function' ) ;
243+ expect ( typeof formatter . formatData ) . toBe ( 'function' ) ;
244+ expect ( typeof formatter . styleData ) . toBe ( 'function' ) ;
245+ } ) ;
246+ } ) ;
125247} ) ;
0 commit comments