|
| 1 | +import { describe, test, expect } from "bun:test" |
| 2 | +import { formatCheckTitle, formatCheck } from "../../src/altimate/tools/altimate-core-check" |
| 3 | + |
| 4 | +describe("formatCheckTitle", () => { |
| 5 | + test("returns PASS for all-clean result", () => { |
| 6 | + const data = { |
| 7 | + validation: { valid: true }, |
| 8 | + lint: { clean: true, findings: [] }, |
| 9 | + safety: { safe: true }, |
| 10 | + pii: { findings: [] }, |
| 11 | + } |
| 12 | + expect(formatCheckTitle(data)).toBe("PASS") |
| 13 | + }) |
| 14 | + |
| 15 | + test("lists all failure categories when everything fails", () => { |
| 16 | + const data = { |
| 17 | + validation: { valid: false, errors: [{ message: "bad syntax" }] }, |
| 18 | + lint: { clean: false, findings: [{ rule: "L001" }, { rule: "L002" }] }, |
| 19 | + safety: { safe: false, threats: [{ type: "injection" }] }, |
| 20 | + pii: { findings: [{ column: "ssn", category: "SSN" }] }, |
| 21 | + } |
| 22 | + const result = formatCheckTitle(data) |
| 23 | + expect(result).toContain("validation errors") |
| 24 | + expect(result).toContain("2 lint findings") |
| 25 | + expect(result).toContain("safety threats") |
| 26 | + expect(result).toContain("PII detected") |
| 27 | + }) |
| 28 | + |
| 29 | + test("treats missing sections as failures (undefined is falsy)", () => { |
| 30 | + // When data is empty, !undefined is true, so each section looks like a failure |
| 31 | + const data = {} as Record<string, any> |
| 32 | + const result = formatCheckTitle(data) |
| 33 | + expect(result).toContain("validation errors") |
| 34 | + expect(result).toContain("safety threats") |
| 35 | + // lint.findings?.length is undefined, ?? 0 yields "0 lint findings" |
| 36 | + expect(result).toContain("0 lint findings") |
| 37 | + }) |
| 38 | + |
| 39 | + test("shows lint finding count when clean is false but findings is undefined", () => { |
| 40 | + const data = { |
| 41 | + validation: { valid: true }, |
| 42 | + lint: { clean: false }, |
| 43 | + safety: { safe: true }, |
| 44 | + pii: { findings: [] }, |
| 45 | + } |
| 46 | + // lint.findings?.length is undefined, ?? 0 yields "0 lint findings" |
| 47 | + expect(formatCheckTitle(data)).toBe("0 lint findings") |
| 48 | + }) |
| 49 | + |
| 50 | + test("only shows failing sections, not passing ones", () => { |
| 51 | + const data = { |
| 52 | + validation: { valid: true }, |
| 53 | + lint: { clean: true }, |
| 54 | + safety: { safe: false, threats: [{ type: "drop_table" }] }, |
| 55 | + pii: { findings: [] }, |
| 56 | + } |
| 57 | + expect(formatCheckTitle(data)).toBe("safety threats") |
| 58 | + }) |
| 59 | +}) |
| 60 | + |
| 61 | +describe("formatCheck", () => { |
| 62 | + test("formats all-pass result with four sections", () => { |
| 63 | + const data = { |
| 64 | + validation: { valid: true }, |
| 65 | + lint: { clean: true }, |
| 66 | + safety: { safe: true }, |
| 67 | + pii: { findings: [] }, |
| 68 | + } |
| 69 | + const output = formatCheck(data) |
| 70 | + expect(output).toContain("=== Validation ===") |
| 71 | + expect(output).toContain("Valid SQL.") |
| 72 | + expect(output).toContain("=== Lint ===") |
| 73 | + expect(output).toContain("No lint findings.") |
| 74 | + expect(output).toContain("=== Safety ===") |
| 75 | + expect(output).toContain("Safe — no threats.") |
| 76 | + expect(output).toContain("=== PII ===") |
| 77 | + expect(output).toContain("No PII detected.") |
| 78 | + }) |
| 79 | + |
| 80 | + test("formats validation errors", () => { |
| 81 | + const data = { |
| 82 | + validation: { valid: false, errors: [{ message: "syntax error at line 3" }, { message: "unknown column" }] }, |
| 83 | + lint: { clean: true }, |
| 84 | + safety: { safe: true }, |
| 85 | + pii: {}, |
| 86 | + } |
| 87 | + const output = formatCheck(data) |
| 88 | + expect(output).toContain("Invalid: syntax error at line 3; unknown column") |
| 89 | + }) |
| 90 | + |
| 91 | + test("formats lint findings with severity and rule", () => { |
| 92 | + const data = { |
| 93 | + validation: { valid: true }, |
| 94 | + lint: { |
| 95 | + clean: false, |
| 96 | + findings: [ |
| 97 | + { severity: "warning", rule: "L001", message: "Unnecessary whitespace" }, |
| 98 | + { severity: "error", rule: "L003", message: "Indentation not consistent" }, |
| 99 | + ], |
| 100 | + }, |
| 101 | + safety: { safe: true }, |
| 102 | + pii: {}, |
| 103 | + } |
| 104 | + const output = formatCheck(data) |
| 105 | + expect(output).toContain("[warning] L001: Unnecessary whitespace") |
| 106 | + expect(output).toContain("[error] L003: Indentation not consistent") |
| 107 | + }) |
| 108 | + |
| 109 | + test("formats safety threats", () => { |
| 110 | + const data = { |
| 111 | + validation: { valid: true }, |
| 112 | + lint: { clean: true }, |
| 113 | + safety: { |
| 114 | + safe: false, |
| 115 | + threats: [{ severity: "critical", type: "sql_injection", description: "Tautology detected: 1=1" }], |
| 116 | + }, |
| 117 | + pii: {}, |
| 118 | + } |
| 119 | + const output = formatCheck(data) |
| 120 | + expect(output).toContain("[critical] sql_injection: Tautology detected: 1=1") |
| 121 | + }) |
| 122 | + |
| 123 | + test("formats PII findings with column and confidence", () => { |
| 124 | + const data = { |
| 125 | + validation: { valid: true }, |
| 126 | + lint: { clean: true }, |
| 127 | + safety: { safe: true }, |
| 128 | + pii: { |
| 129 | + findings: [ |
| 130 | + { column: "ssn", category: "SSN", confidence: "high" }, |
| 131 | + { column: "email", category: "EMAIL", confidence: "medium" }, |
| 132 | + ], |
| 133 | + }, |
| 134 | + } |
| 135 | + const output = formatCheck(data) |
| 136 | + expect(output).toContain("ssn: SSN (high confidence)") |
| 137 | + expect(output).toContain("email: EMAIL (medium confidence)") |
| 138 | + }) |
| 139 | + |
| 140 | + test("handles empty/missing sections without crashing", () => { |
| 141 | + const data = {} as Record<string, any> |
| 142 | + const output = formatCheck(data) |
| 143 | + // Should still produce all four section headers |
| 144 | + expect(output).toContain("=== Validation ===") |
| 145 | + expect(output).toContain("=== Lint ===") |
| 146 | + expect(output).toContain("=== Safety ===") |
| 147 | + expect(output).toContain("=== PII ===") |
| 148 | + // validation.valid is undefined (falsy) → "Invalid: unknown" |
| 149 | + expect(output).toContain("Invalid:") |
| 150 | + }) |
| 151 | +}) |
0 commit comments