|
| 1 | +import { getLogType } from "../../components/dashboard/docker/logs/utils"; |
| 2 | +import { describe, expect, test } from "vitest"; |
| 3 | + |
| 4 | +describe("getLogType", () => { |
| 5 | + describe("Structured error patterns", () => { |
| 6 | + test("should detect [ERROR] format", () => { |
| 7 | + expect(getLogType("[ERROR] Database connection failed").type).toBe("error"); |
| 8 | + }); |
| 9 | + |
| 10 | + test("should detect ERROR: format", () => { |
| 11 | + expect(getLogType("ERROR: Connection refused").type).toBe("error"); |
| 12 | + }); |
| 13 | + |
| 14 | + test("should detect level=error format", () => { |
| 15 | + expect(getLogType('level=error msg="Database connection failed"').type).toBe("error"); |
| 16 | + }); |
| 17 | + |
| 18 | + test("should detect error emojis", () => { |
| 19 | + expect(getLogType("❌ Operation failed").type).toBe("error"); |
| 20 | + expect(getLogType("🔴 Critical issue").type).toBe("error"); |
| 21 | + }); |
| 22 | + }); |
| 23 | + |
| 24 | + describe("Contextual error patterns", () => { |
| 25 | + test("should detect uncaught exceptions", () => { |
| 26 | + expect(getLogType("Uncaught exception in handler").type).toBe("error"); |
| 27 | + }); |
| 28 | + |
| 29 | + test("should detect 'failed to' patterns", () => { |
| 30 | + expect(getLogType("Failed to start server").type).toBe("error"); |
| 31 | + }); |
| 32 | + |
| 33 | + test("should detect 'X failed with' patterns", () => { |
| 34 | + expect(getLogType("Token exchange failed with status: 400").type).toBe("error"); |
| 35 | + }); |
| 36 | + |
| 37 | + test("should detect HTTP error codes", () => { |
| 38 | + expect(getLogType("Request returned status: 400").type).toBe("error"); |
| 39 | + expect(getLogType("500 Internal Server Error").type).toBe("error"); |
| 40 | + }); |
| 41 | + |
| 42 | + test("should detect stack trace lines", () => { |
| 43 | + expect(getLogType(" at Object.<anonymous> (/app/server.js:123:45)").type).toBe("error"); |
| 44 | + }); |
| 45 | + }); |
| 46 | + |
| 47 | + describe("False positives - should NOT be detected as errors", () => { |
| 48 | + test("should not flag 'Failed=0' as error", () => { |
| 49 | + const result = getLogType('time="2025-11-20T08:19:13Z" level=info msg="Session done" Failed=0 Scanned=1'); |
| 50 | + expect(result.type).toBe("info"); |
| 51 | + }); |
| 52 | + |
| 53 | + test("should not flag '0 failed' as error", () => { |
| 54 | + expect(getLogType("0 practices builds failed, and 0 uploads failed.").type).toBe("info"); |
| 55 | + }); |
| 56 | + |
| 57 | + test("should not flag negative contexts as error", () => { |
| 58 | + expect(getLogType("The operation did not fail").type).toBe("info"); |
| 59 | + }); |
| 60 | + |
| 61 | + test("should not flag conditional statements as error", () => { |
| 62 | + expect(getLogType("If failed, retry the operation").type).toBe("info"); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + describe("Warning patterns", () => { |
| 67 | + test("should detect [WARN] format", () => { |
| 68 | + expect(getLogType("[WARN] API rate limit approaching").type).toBe("warning"); |
| 69 | + }); |
| 70 | + |
| 71 | + test("should detect WARNING: format", () => { |
| 72 | + expect(getLogType("WARNING: Disk space low").type).toBe("warning"); |
| 73 | + }); |
| 74 | + |
| 75 | + test("should detect warning emojis", () => { |
| 76 | + expect(getLogType("⚠️ Token exchange failed with status: 400 Bad Request").type).not.toBe("info"); |
| 77 | + }); |
| 78 | + |
| 79 | + test("should detect deprecated warnings", () => { |
| 80 | + expect(getLogType("Deprecated since version 2.0").type).toBe("warning"); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + describe("Success patterns", () => { |
| 85 | + test("should detect [SUCCESS] format", () => { |
| 86 | + expect(getLogType("[SUCCESS] Deployment completed").type).toBe("success"); |
| 87 | + }); |
| 88 | + |
| 89 | + test("should detect 'listening on port' patterns", () => { |
| 90 | + expect(getLogType("Server listening on port 3000").type).toBe("success"); |
| 91 | + }); |
| 92 | + |
| 93 | + test("should detect 'successfully' patterns", () => { |
| 94 | + expect(getLogType("Successfully connected to database").type).toBe("success"); |
| 95 | + }); |
| 96 | + |
| 97 | + test("should detect success emojis", () => { |
| 98 | + expect(getLogType("✅ Build completed").type).toBe("success"); |
| 99 | + }); |
| 100 | + }); |
| 101 | + |
| 102 | + describe("Debug patterns", () => { |
| 103 | + test("should detect [DEBUG] format", () => { |
| 104 | + expect(getLogType("[DEBUG] Processing request").type).toBe("debug"); |
| 105 | + }); |
| 106 | + |
| 107 | + test("should detect HTTP method patterns", () => { |
| 108 | + expect(getLogType("GET /api/users").type).toBe("debug"); |
| 109 | + expect(getLogType("POST /api/login").type).toBe("debug"); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + describe("Info patterns (default)", () => { |
| 114 | + test("should default to info for generic messages", () => { |
| 115 | + expect(getLogType("Server started").type).toBe("info"); |
| 116 | + }); |
| 117 | + |
| 118 | + test("should detect [INFO] format", () => { |
| 119 | + expect(getLogType("[INFO] Application initialized").type).toBe("info"); |
| 120 | + }); |
| 121 | + }); |
| 122 | +}); |
0 commit comments