Skip to content

Commit 4c39dd6

Browse files
committed
test: SQL tool formatters — check, equivalence, semantics (38 tests)
Export and test pure formatting functions across three SQL analysis tools that had zero test coverage. Discovered a real bug: null entries in validation_errors crash extractEquivalenceErrors (TypeError on null.message). Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]> https://claude.ai/code/session_01Lz8zxrbwHXfsC2FbHxXZh9
1 parent 9ba2114 commit 4c39dd6

6 files changed

Lines changed: 366 additions & 6 deletions

‎packages/opencode/src/altimate/tools/altimate-core-check.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export const AltimateCoreCheckTool = Tool.define("altimate_core_check", {
5757
},
5858
})
5959

60-
function formatCheckTitle(data: Record<string, any>): string {
60+
export function formatCheckTitle(data: Record<string, any>): string {
6161
const parts: string[] = []
6262
if (!data.validation?.valid) parts.push("validation errors")
6363
if (!data.lint?.clean) parts.push(`${data.lint?.findings?.length ?? 0} lint findings`)
@@ -66,7 +66,7 @@ function formatCheckTitle(data: Record<string, any>): string {
6666
return parts.length ? parts.join(", ") : "PASS"
6767
}
6868

69-
function formatCheck(data: Record<string, any>): string {
69+
export function formatCheck(data: Record<string, any>): string {
7070
const lines: string[] = []
7171

7272
lines.push("=== Validation ===")

‎packages/opencode/src/altimate/tools/altimate-core-equivalence.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export const AltimateCoreEquivalenceTool = Tool.define("altimate_core_equivalenc
6565
},
6666
})
6767

68-
function extractEquivalenceErrors(data: Record<string, any>): string | undefined {
68+
export function extractEquivalenceErrors(data: Record<string, any>): string | undefined {
6969
if (Array.isArray(data.validation_errors) && data.validation_errors.length > 0) {
7070
const msgs = data.validation_errors
7171
.map((e: any) => (typeof e === "string" ? e : (e.message ?? String(e))))
@@ -75,7 +75,7 @@ function extractEquivalenceErrors(data: Record<string, any>): string | undefined
7575
return undefined
7676
}
7777

78-
function formatEquivalence(data: Record<string, any>): string {
78+
export function formatEquivalence(data: Record<string, any>): string {
7979
if (data.error) return `Error: ${data.error}`
8080
const lines: string[] = []
8181
lines.push(data.equivalent ? "Queries are semantically equivalent." : "Queries produce different results.")

‎packages/opencode/src/altimate/tools/altimate-core-semantics.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export const AltimateCoreSemanticsTool = Tool.define("altimate_core_semantics",
6161
},
6262
})
6363

64-
function extractSemanticsErrors(data: Record<string, any>): string | undefined {
64+
export function extractSemanticsErrors(data: Record<string, any>): string | undefined {
6565
if (Array.isArray(data.validation_errors) && data.validation_errors.length > 0) {
6666
const msgs = data.validation_errors
6767
.map((e: any) => (typeof e === "string" ? e : (e.message ?? String(e))))
@@ -71,7 +71,7 @@ function extractSemanticsErrors(data: Record<string, any>): string | undefined {
7171
return undefined
7272
}
7373

74-
function formatSemantics(data: Record<string, any>): string {
74+
export function formatSemantics(data: Record<string, any>): string {
7575
if (data.error) return `Error: ${data.error}`
7676
if (data.valid) return "No semantic issues found."
7777
const lines = ["Semantic issues:\n"]
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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+
})
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import { describe, test, expect } from "bun:test"
2+
import {
3+
extractEquivalenceErrors,
4+
formatEquivalence,
5+
} from "../../src/altimate/tools/altimate-core-equivalence"
6+
7+
describe("extractEquivalenceErrors", () => {
8+
test("returns undefined when validation_errors is absent", () => {
9+
expect(extractEquivalenceErrors({})).toBeUndefined()
10+
})
11+
12+
test("returns undefined for empty validation_errors array", () => {
13+
expect(extractEquivalenceErrors({ validation_errors: [] })).toBeUndefined()
14+
})
15+
16+
test("joins string errors with semicolons", () => {
17+
const data = { validation_errors: ["column X not found", "table Y not found"] }
18+
expect(extractEquivalenceErrors(data)).toBe("column X not found; table Y not found")
19+
})
20+
21+
test("extracts .message from object errors", () => {
22+
const data = {
23+
validation_errors: [
24+
{ message: "unresolved reference to column 'id'" },
25+
{ message: "ambiguous column name" },
26+
],
27+
}
28+
expect(extractEquivalenceErrors(data)).toBe(
29+
"unresolved reference to column 'id'; ambiguous column name",
30+
)
31+
})
32+
33+
test("falls back to String(e) for non-string primitive errors", () => {
34+
const data = { validation_errors: [42] }
35+
// 42 → typeof 42 !== "string" → e.message (undefined) ?? String(42) = "42"
36+
expect(extractEquivalenceErrors(data)).toBe("42")
37+
})
38+
39+
test("crashes on null entries in validation_errors (known bug)", () => {
40+
// BUG: null passes typeof check (not "string") but null.message throws TypeError
41+
// The ?? operator cannot protect because property access on null throws first
42+
const data = { validation_errors: [null] }
43+
expect(() => extractEquivalenceErrors(data)).toThrow(TypeError)
44+
})
45+
46+
test("filters out falsy messages", () => {
47+
const data = { validation_errors: [{ message: "" }, { message: "real error" }] }
48+
// Empty string is filtered by .filter(Boolean)
49+
expect(extractEquivalenceErrors(data)).toBe("real error")
50+
})
51+
52+
test("returns undefined when all messages are empty", () => {
53+
const data = { validation_errors: [{ message: "" }] }
54+
expect(extractEquivalenceErrors(data)).toBeUndefined()
55+
})
56+
})
57+
58+
describe("formatEquivalence", () => {
59+
test("shows error message when data.error is present (short-circuits)", () => {
60+
const data = { error: "Schema not found", equivalent: true }
61+
// error takes priority over equivalent
62+
expect(formatEquivalence(data)).toBe("Error: Schema not found")
63+
})
64+
65+
test("shows equivalent message when queries match", () => {
66+
const data = { equivalent: true }
67+
expect(formatEquivalence(data)).toBe("Queries are semantically equivalent.")
68+
})
69+
70+
test("shows different message when queries don't match", () => {
71+
const data = { equivalent: false }
72+
expect(formatEquivalence(data)).toContain("Queries produce different results.")
73+
})
74+
75+
test("lists differences with description field", () => {
76+
const data = {
77+
equivalent: false,
78+
differences: [
79+
{ description: "WHERE clause differs" },
80+
{ description: "Column order differs" },
81+
],
82+
}
83+
const output = formatEquivalence(data)
84+
expect(output).toContain("Differences:")
85+
expect(output).toContain(" - WHERE clause differs")
86+
expect(output).toContain(" - Column order differs")
87+
})
88+
89+
test("falls back to raw value when description is absent", () => {
90+
const data = {
91+
equivalent: false,
92+
differences: ["plain string difference"],
93+
}
94+
const output = formatEquivalence(data)
95+
expect(output).toContain(" - plain string difference")
96+
})
97+
98+
test("shows confidence level", () => {
99+
const data = { equivalent: true, confidence: "high" }
100+
expect(formatEquivalence(data)).toContain("Confidence: high")
101+
})
102+
103+
test("omits confidence when not present", () => {
104+
const data = { equivalent: true }
105+
expect(formatEquivalence(data)).not.toContain("Confidence")
106+
})
107+
})

0 commit comments

Comments
 (0)