|
19 | 19 | import { describe, expect, test } from 'vitest'; |
20 | 20 |
|
21 | 21 | import type { JsonColorScheme } from './json-colorizer.js'; |
22 | | -import { JsonColorizer } from './json-colorizer.js'; |
| 22 | +import { detectJsonLogs, JsonColorizer } from './json-colorizer.js'; |
23 | 23 |
|
24 | 24 | describe('JsonColorizer', () => { |
25 | 25 | const colorScheme: JsonColorScheme = { |
@@ -283,3 +283,157 @@ describe('JsonColorizer', () => { |
283 | 283 | expect(result).toContain('\u001b[32m0.5\u001b[0m'); |
284 | 284 | }); |
285 | 285 | }); |
| 286 | + |
| 287 | +describe('detectJsonLogs', () => { |
| 288 | + test('should detect JSON logs when 80% or more lines are valid JSON', () => { |
| 289 | + const logs = [ |
| 290 | + '{"timestamp":"2025-11-18T10:00:00Z","level":"info","message":"Starting application"}', |
| 291 | + '{"timestamp":"2025-11-18T10:00:01Z","level":"info","message":"Connected to database"}', |
| 292 | + '{"timestamp":"2025-11-18T10:00:02Z","level":"info","message":"Server listening on port 8080"}', |
| 293 | + '{"timestamp":"2025-11-18T10:00:03Z","level":"debug","message":"Request received"}', |
| 294 | + '{"timestamp":"2025-11-18T10:00:04Z","level":"debug","message":"Processing request"}', |
| 295 | + '{"timestamp":"2025-11-18T10:00:05Z","level":"info","message":"Request completed"}', |
| 296 | + '{"timestamp":"2025-11-18T10:00:06Z","level":"info","message":"Cache updated"}', |
| 297 | + '{"timestamp":"2025-11-18T10:00:07Z","level":"info","message":"Background job started"}', |
| 298 | + 'Not a JSON line', |
| 299 | + '{"timestamp":"2025-11-18T10:00:08Z","level":"info","message":"Job completed"}', |
| 300 | + ]; |
| 301 | + |
| 302 | + expect(detectJsonLogs(logs)).toBe(true); |
| 303 | + }); |
| 304 | + |
| 305 | + test('should not detect JSON logs when less than 80% are valid JSON', () => { |
| 306 | + const logs = [ |
| 307 | + '{"timestamp":"2025-11-18T10:00:00Z","level":"info","message":"Starting"}', |
| 308 | + '{"timestamp":"2025-11-18T10:00:01Z","level":"info","message":"Running"}', |
| 309 | + 'Regular log line without JSON', |
| 310 | + 'Another regular log line', |
| 311 | + 'Yet another non-JSON line', |
| 312 | + 'Still not JSON', |
| 313 | + 'Nope, not JSON either', |
| 314 | + '{"timestamp":"2025-11-18T10:00:02Z","level":"info","message":"Done"}', |
| 315 | + 'More regular logs', |
| 316 | + 'Last regular log', |
| 317 | + ]; |
| 318 | + |
| 319 | + expect(detectJsonLogs(logs)).toBe(false); |
| 320 | + }); |
| 321 | + |
| 322 | + test('should handle empty log array', () => { |
| 323 | + expect(detectJsonLogs([])).toBe(false); |
| 324 | + }); |
| 325 | + |
| 326 | + test('should ignore empty lines when calculating ratio', () => { |
| 327 | + const logs = [ |
| 328 | + '{"timestamp":"2025-11-18T10:00:00Z","level":"info","message":"Line 1"}', |
| 329 | + '', |
| 330 | + '{"timestamp":"2025-11-18T10:00:01Z","level":"info","message":"Line 2"}', |
| 331 | + '', |
| 332 | + '{"timestamp":"2025-11-18T10:00:02Z","level":"info","message":"Line 3"}', |
| 333 | + '', |
| 334 | + '{"timestamp":"2025-11-18T10:00:03Z","level":"info","message":"Line 4"}', |
| 335 | + '', |
| 336 | + '{"timestamp":"2025-11-18T10:00:04Z","level":"info","message":"Line 5"}', |
| 337 | + '', |
| 338 | + '{"timestamp":"2025-11-18T10:00:05Z","level":"info","message":"Line 6"}', |
| 339 | + '', |
| 340 | + '{"timestamp":"2025-11-18T10:00:06Z","level":"info","message":"Line 7"}', |
| 341 | + '', |
| 342 | + '{"timestamp":"2025-11-18T10:00:07Z","level":"info","message":"Line 8"}', |
| 343 | + '', |
| 344 | + ]; |
| 345 | + |
| 346 | + expect(detectJsonLogs(logs)).toBe(true); |
| 347 | + }); |
| 348 | + |
| 349 | + test('should only check first 10 non-empty lines', () => { |
| 350 | + const logs = [ |
| 351 | + '{"timestamp":"2025-11-18T10:00:00Z","level":"info","message":"Line 1"}', |
| 352 | + '{"timestamp":"2025-11-18T10:00:01Z","level":"info","message":"Line 2"}', |
| 353 | + '{"timestamp":"2025-11-18T10:00:02Z","level":"info","message":"Line 3"}', |
| 354 | + '{"timestamp":"2025-11-18T10:00:03Z","level":"info","message":"Line 4"}', |
| 355 | + '{"timestamp":"2025-11-18T10:00:04Z","level":"info","message":"Line 5"}', |
| 356 | + '{"timestamp":"2025-11-18T10:00:05Z","level":"info","message":"Line 6"}', |
| 357 | + '{"timestamp":"2025-11-18T10:00:06Z","level":"info","message":"Line 7"}', |
| 358 | + '{"timestamp":"2025-11-18T10:00:07Z","level":"info","message":"Line 8"}', |
| 359 | + '{"timestamp":"2025-11-18T10:00:08Z","level":"info","message":"Line 9"}', |
| 360 | + '{"timestamp":"2025-11-18T10:00:09Z","level":"info","message":"Line 10"}', |
| 361 | + // These lines after the 10th should be ignored |
| 362 | + 'Not JSON line 11', |
| 363 | + 'Not JSON line 12', |
| 364 | + 'Not JSON line 13', |
| 365 | + ]; |
| 366 | + |
| 367 | + expect(detectJsonLogs(logs)).toBe(true); |
| 368 | + }); |
| 369 | + |
| 370 | + test('should detect JSON with nested objects', () => { |
| 371 | + const logs = [ |
| 372 | + '{"user":{"id":123,"name":"John"},"action":"login"}', |
| 373 | + '{"user":{"id":124,"name":"Jane"},"action":"logout"}', |
| 374 | + '{"data":{"key":"value","nested":{"deep":"data"}},"timestamp":"2025-11-18"}', |
| 375 | + '{"array":[1,2,3],"object":{"a":"b"}}', |
| 376 | + '{"simple":"test","number":42}', |
| 377 | + '{"bool":true,"null":null}', |
| 378 | + '{"str":"value","num":123}', |
| 379 | + '{"x":1,"y":2}', |
| 380 | + '{"foo":"bar"}', |
| 381 | + '{"last":"one"}', |
| 382 | + ]; |
| 383 | + |
| 384 | + expect(detectJsonLogs(logs)).toBe(true); |
| 385 | + }); |
| 386 | + |
| 387 | + test('should not detect lines with braces but no key-value pairs', () => { |
| 388 | + const logs = [ |
| 389 | + 'Processing {item}', |
| 390 | + 'Found {value} in cache', |
| 391 | + 'Error: {error message}', |
| 392 | + 'Debug: {info}', |
| 393 | + 'Status: {ok}', |
| 394 | + 'Result: {success}', |
| 395 | + 'Output: {data}', |
| 396 | + 'Input: {params}', |
| 397 | + 'Config: {settings}', |
| 398 | + 'State: {ready}', |
| 399 | + ]; |
| 400 | + |
| 401 | + expect(detectJsonLogs(logs)).toBe(false); |
| 402 | + }); |
| 403 | + |
| 404 | + test('should detect exactly 80% threshold', () => { |
| 405 | + const logs = [ |
| 406 | + '{"valid":"json","line":1}', |
| 407 | + '{"valid":"json","line":2}', |
| 408 | + '{"valid":"json","line":3}', |
| 409 | + '{"valid":"json","line":4}', |
| 410 | + '{"valid":"json","line":5}', |
| 411 | + '{"valid":"json","line":6}', |
| 412 | + '{"valid":"json","line":7}', |
| 413 | + '{"valid":"json","line":8}', |
| 414 | + 'Not JSON line 9', |
| 415 | + 'Not JSON line 10', |
| 416 | + ]; |
| 417 | + |
| 418 | + // 8 out of 10 = 80% |
| 419 | + expect(detectJsonLogs(logs)).toBe(true); |
| 420 | + }); |
| 421 | + |
| 422 | + test('should not detect just below 80% threshold', () => { |
| 423 | + const logs = [ |
| 424 | + '{"valid":"json","line":1}', |
| 425 | + '{"valid":"json","line":2}', |
| 426 | + '{"valid":"json","line":3}', |
| 427 | + '{"valid":"json","line":4}', |
| 428 | + '{"valid":"json","line":5}', |
| 429 | + '{"valid":"json","line":6}', |
| 430 | + '{"valid":"json","line":7}', |
| 431 | + 'Not JSON line 8', |
| 432 | + 'Not JSON line 9', |
| 433 | + 'Not JSON line 10', |
| 434 | + ]; |
| 435 | + |
| 436 | + // 7 out of 10 = 70% |
| 437 | + expect(detectJsonLogs(logs)).toBe(false); |
| 438 | + }); |
| 439 | +}); |
0 commit comments