|
| 1 | +/** |
| 2 | + * Tests for SqlAnalyzeTool title construction and formatAnalysis output. |
| 3 | + * |
| 4 | + * These test the formatting logic patterns used in sql-analyze.ts. |
| 5 | + * Since formatAnalysis is not exported, we replicate its logic here — |
| 6 | + * same approach as tool-formatters.test.ts. This means these tests |
| 7 | + * will not catch changes to the real function unless the test copy |
| 8 | + * is also updated. This is an accepted tradeoff in this codebase. |
| 9 | + */ |
| 10 | + |
| 11 | +import { describe, test, expect } from "bun:test" |
| 12 | + |
| 13 | +describe("SqlAnalyzeTool: title construction", () => { |
| 14 | + // Replicates the title template from sql-analyze.ts execute() line 25 |
| 15 | + function buildTitle(result: { error?: string; issue_count: number; confidence: string }) { |
| 16 | + return `Analyze: ${result.error ? "PARSE ERROR" : `${result.issue_count} issue${result.issue_count !== 1 ? "s" : ""}`} [${result.confidence}]` |
| 17 | + } |
| 18 | + |
| 19 | + test("zero issues shows '0 issues'", () => { |
| 20 | + expect(buildTitle({ issue_count: 0, confidence: "high" })).toBe("Analyze: 0 issues [high]") |
| 21 | + }) |
| 22 | + |
| 23 | + test("one issue shows singular '1 issue'", () => { |
| 24 | + expect(buildTitle({ issue_count: 1, confidence: "high" })).toBe("Analyze: 1 issue [high]") |
| 25 | + }) |
| 26 | + |
| 27 | + test("multiple issues shows plural", () => { |
| 28 | + expect(buildTitle({ issue_count: 5, confidence: "medium" })).toBe("Analyze: 5 issues [medium]") |
| 29 | + }) |
| 30 | + |
| 31 | + test("error present shows PARSE ERROR", () => { |
| 32 | + expect(buildTitle({ error: "syntax error", issue_count: 0, confidence: "low" })).toBe( |
| 33 | + "Analyze: PARSE ERROR [low]", |
| 34 | + ) |
| 35 | + }) |
| 36 | +}) |
| 37 | + |
| 38 | +describe("SqlAnalyzeTool: formatAnalysis output", () => { |
| 39 | + // Replicates formatAnalysis() from sql-analyze.ts lines 45-70 |
| 40 | + function formatAnalysis(result: { |
| 41 | + error?: string |
| 42 | + issues: Array<{ |
| 43 | + type: string |
| 44 | + severity: string |
| 45 | + message: string |
| 46 | + recommendation: string |
| 47 | + location?: string |
| 48 | + confidence: string |
| 49 | + }> |
| 50 | + issue_count: number |
| 51 | + confidence: string |
| 52 | + confidence_factors: string[] |
| 53 | + }): string { |
| 54 | + if (result.error) return `Analysis failed: ${result.error}` |
| 55 | + if (result.issues.length === 0) return "No anti-patterns or issues detected." |
| 56 | + |
| 57 | + const lines: string[] = [ |
| 58 | + `Found ${result.issue_count} issue${result.issue_count !== 1 ? "s" : ""} (confidence: ${result.confidence}):`, |
| 59 | + ] |
| 60 | + if (result.confidence_factors.length > 0) { |
| 61 | + lines.push(` Note: ${result.confidence_factors.join("; ")}`) |
| 62 | + } |
| 63 | + lines.push("") |
| 64 | + |
| 65 | + for (const issue of result.issues) { |
| 66 | + const loc = issue.location ? ` \u2014 ${issue.location}` : "" |
| 67 | + const conf = issue.confidence !== "high" ? ` [${issue.confidence} confidence]` : "" |
| 68 | + lines.push(` [${issue.severity.toUpperCase()}] ${issue.type}${conf}`) |
| 69 | + lines.push(` ${issue.message}${loc}`) |
| 70 | + lines.push(` \u2192 ${issue.recommendation}`) |
| 71 | + lines.push("") |
| 72 | + } |
| 73 | + |
| 74 | + return lines.join("\n") |
| 75 | + } |
| 76 | + |
| 77 | + test("error result returns failure message", () => { |
| 78 | + const output = formatAnalysis({ |
| 79 | + error: "parse error at line 5", |
| 80 | + issues: [], |
| 81 | + issue_count: 0, |
| 82 | + confidence: "low", |
| 83 | + confidence_factors: [], |
| 84 | + }) |
| 85 | + expect(output).toBe("Analysis failed: parse error at line 5") |
| 86 | + }) |
| 87 | + |
| 88 | + test("zero issues returns clean message", () => { |
| 89 | + const output = formatAnalysis({ |
| 90 | + issues: [], |
| 91 | + issue_count: 0, |
| 92 | + confidence: "high", |
| 93 | + confidence_factors: [], |
| 94 | + }) |
| 95 | + expect(output).toBe("No anti-patterns or issues detected.") |
| 96 | + }) |
| 97 | + |
| 98 | + test("issues are formatted with severity, type, location", () => { |
| 99 | + const output = formatAnalysis({ |
| 100 | + issues: [ |
| 101 | + { |
| 102 | + type: "lint", |
| 103 | + severity: "warning", |
| 104 | + message: "SELECT * detected", |
| 105 | + recommendation: "Use explicit columns", |
| 106 | + location: "line 1", |
| 107 | + confidence: "high", |
| 108 | + }, |
| 109 | + ], |
| 110 | + issue_count: 1, |
| 111 | + confidence: "high", |
| 112 | + confidence_factors: ["lint"], |
| 113 | + }) |
| 114 | + expect(output).toContain("[WARNING] lint") |
| 115 | + expect(output).toContain("SELECT * detected \u2014 line 1") |
| 116 | + expect(output).toContain("\u2192 Use explicit columns") |
| 117 | + }) |
| 118 | + |
| 119 | + test("non-high confidence issues show confidence tag", () => { |
| 120 | + const output = formatAnalysis({ |
| 121 | + issues: [ |
| 122 | + { |
| 123 | + type: "semantic", |
| 124 | + severity: "info", |
| 125 | + message: "Possible unused join", |
| 126 | + recommendation: "Review join necessity", |
| 127 | + confidence: "medium", |
| 128 | + }, |
| 129 | + ], |
| 130 | + issue_count: 1, |
| 131 | + confidence: "medium", |
| 132 | + confidence_factors: ["semantics"], |
| 133 | + }) |
| 134 | + expect(output).toContain("[medium confidence]") |
| 135 | + }) |
| 136 | + |
| 137 | + test("high confidence issues omit confidence tag", () => { |
| 138 | + const output = formatAnalysis({ |
| 139 | + issues: [ |
| 140 | + { |
| 141 | + type: "safety", |
| 142 | + severity: "high", |
| 143 | + message: "SQL injection risk", |
| 144 | + recommendation: "Use parameterized queries", |
| 145 | + confidence: "high", |
| 146 | + }, |
| 147 | + ], |
| 148 | + issue_count: 1, |
| 149 | + confidence: "high", |
| 150 | + confidence_factors: ["safety"], |
| 151 | + }) |
| 152 | + expect(output).not.toContain("[high confidence]") |
| 153 | + }) |
| 154 | + |
| 155 | + test("confidence factors are listed in Note line", () => { |
| 156 | + const output = formatAnalysis({ |
| 157 | + issues: [ |
| 158 | + { |
| 159 | + type: "lint", |
| 160 | + severity: "warning", |
| 161 | + message: "Missing LIMIT", |
| 162 | + recommendation: "Add LIMIT clause", |
| 163 | + confidence: "high", |
| 164 | + }, |
| 165 | + ], |
| 166 | + issue_count: 1, |
| 167 | + confidence: "high", |
| 168 | + confidence_factors: ["lint", "semantics", "safety"], |
| 169 | + }) |
| 170 | + expect(output).toContain("Note: lint; semantics; safety") |
| 171 | + }) |
| 172 | +}) |
0 commit comments