-
Notifications
You must be signed in to change notification settings - Fork 19
test: command builtins + sql_analyze formatting — 16 new tests #441
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
anandgupta42
wants to merge
1
commit into
main
Choose a base branch
from
test/hourly-20260324-0905
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+288
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
172 changes: 172 additions & 0 deletions
172
packages/opencode/test/altimate/sql-analyze-tool.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| /** | ||
| * Tests for SqlAnalyzeTool title construction and formatAnalysis output. | ||
| * | ||
| * These test the formatting logic patterns used in sql-analyze.ts. | ||
| * Since formatAnalysis is not exported, we replicate its logic here — | ||
| * same approach as tool-formatters.test.ts. This means these tests | ||
| * will not catch changes to the real function unless the test copy | ||
| * is also updated. This is an accepted tradeoff in this codebase. | ||
| */ | ||
|
|
||
| import { describe, test, expect } from "bun:test" | ||
|
|
||
| describe("SqlAnalyzeTool: title construction", () => { | ||
| // Replicates the title template from sql-analyze.ts execute() line 25 | ||
| function buildTitle(result: { error?: string; issue_count: number; confidence: string }) { | ||
| return `Analyze: ${result.error ? "PARSE ERROR" : `${result.issue_count} issue${result.issue_count !== 1 ? "s" : ""}`} [${result.confidence}]` | ||
| } | ||
|
|
||
| test("zero issues shows '0 issues'", () => { | ||
| expect(buildTitle({ issue_count: 0, confidence: "high" })).toBe("Analyze: 0 issues [high]") | ||
| }) | ||
|
|
||
| test("one issue shows singular '1 issue'", () => { | ||
| expect(buildTitle({ issue_count: 1, confidence: "high" })).toBe("Analyze: 1 issue [high]") | ||
| }) | ||
|
|
||
| test("multiple issues shows plural", () => { | ||
| expect(buildTitle({ issue_count: 5, confidence: "medium" })).toBe("Analyze: 5 issues [medium]") | ||
| }) | ||
|
|
||
| test("error present shows PARSE ERROR", () => { | ||
| expect(buildTitle({ error: "syntax error", issue_count: 0, confidence: "low" })).toBe( | ||
| "Analyze: PARSE ERROR [low]", | ||
| ) | ||
| }) | ||
| }) | ||
|
|
||
| describe("SqlAnalyzeTool: formatAnalysis output", () => { | ||
| // Replicates formatAnalysis() from sql-analyze.ts lines 45-70 | ||
| function formatAnalysis(result: { | ||
| error?: string | ||
| issues: Array<{ | ||
| type: string | ||
| severity: string | ||
| message: string | ||
| recommendation: string | ||
| location?: string | ||
| confidence: string | ||
| }> | ||
| issue_count: number | ||
| confidence: string | ||
| confidence_factors: string[] | ||
| }): string { | ||
| if (result.error) return `Analysis failed: ${result.error}` | ||
| if (result.issues.length === 0) return "No anti-patterns or issues detected." | ||
|
|
||
| const lines: string[] = [ | ||
| `Found ${result.issue_count} issue${result.issue_count !== 1 ? "s" : ""} (confidence: ${result.confidence}):`, | ||
| ] | ||
| if (result.confidence_factors.length > 0) { | ||
| lines.push(` Note: ${result.confidence_factors.join("; ")}`) | ||
| } | ||
| lines.push("") | ||
|
|
||
| for (const issue of result.issues) { | ||
| const loc = issue.location ? ` \u2014 ${issue.location}` : "" | ||
| const conf = issue.confidence !== "high" ? ` [${issue.confidence} confidence]` : "" | ||
| lines.push(` [${issue.severity.toUpperCase()}] ${issue.type}${conf}`) | ||
| lines.push(` ${issue.message}${loc}`) | ||
| lines.push(` \u2192 ${issue.recommendation}`) | ||
| lines.push("") | ||
| } | ||
|
|
||
| return lines.join("\n") | ||
| } | ||
|
|
||
| test("error result returns failure message", () => { | ||
| const output = formatAnalysis({ | ||
| error: "parse error at line 5", | ||
| issues: [], | ||
| issue_count: 0, | ||
| confidence: "low", | ||
| confidence_factors: [], | ||
| }) | ||
| expect(output).toBe("Analysis failed: parse error at line 5") | ||
| }) | ||
|
|
||
| test("zero issues returns clean message", () => { | ||
| const output = formatAnalysis({ | ||
| issues: [], | ||
| issue_count: 0, | ||
| confidence: "high", | ||
| confidence_factors: [], | ||
| }) | ||
| expect(output).toBe("No anti-patterns or issues detected.") | ||
| }) | ||
|
|
||
| test("issues are formatted with severity, type, location", () => { | ||
| const output = formatAnalysis({ | ||
| issues: [ | ||
| { | ||
| type: "lint", | ||
| severity: "warning", | ||
| message: "SELECT * detected", | ||
| recommendation: "Use explicit columns", | ||
| location: "line 1", | ||
| confidence: "high", | ||
| }, | ||
| ], | ||
| issue_count: 1, | ||
| confidence: "high", | ||
| confidence_factors: ["lint"], | ||
| }) | ||
| expect(output).toContain("[WARNING] lint") | ||
| expect(output).toContain("SELECT * detected \u2014 line 1") | ||
| expect(output).toContain("\u2192 Use explicit columns") | ||
| }) | ||
|
|
||
| test("non-high confidence issues show confidence tag", () => { | ||
| const output = formatAnalysis({ | ||
| issues: [ | ||
| { | ||
| type: "semantic", | ||
| severity: "info", | ||
| message: "Possible unused join", | ||
| recommendation: "Review join necessity", | ||
| confidence: "medium", | ||
| }, | ||
| ], | ||
| issue_count: 1, | ||
| confidence: "medium", | ||
| confidence_factors: ["semantics"], | ||
| }) | ||
| expect(output).toContain("[medium confidence]") | ||
| }) | ||
|
|
||
| test("high confidence issues omit confidence tag", () => { | ||
| const output = formatAnalysis({ | ||
| issues: [ | ||
| { | ||
| type: "safety", | ||
| severity: "high", | ||
| message: "SQL injection risk", | ||
| recommendation: "Use parameterized queries", | ||
| confidence: "high", | ||
| }, | ||
| ], | ||
| issue_count: 1, | ||
| confidence: "high", | ||
| confidence_factors: ["safety"], | ||
| }) | ||
| expect(output).not.toContain("[high confidence]") | ||
| }) | ||
|
|
||
| test("confidence factors are listed in Note line", () => { | ||
| const output = formatAnalysis({ | ||
| issues: [ | ||
| { | ||
| type: "lint", | ||
| severity: "warning", | ||
| message: "Missing LIMIT", | ||
| recommendation: "Add LIMIT clause", | ||
| confidence: "high", | ||
| }, | ||
| ], | ||
| issue_count: 1, | ||
| confidence: "high", | ||
| confidence_factors: ["lint", "semantics", "safety"], | ||
| }) | ||
| expect(output).toContain("Note: lint; semantics; safety") | ||
| }) | ||
| }) |
116 changes: 116 additions & 0 deletions
116
packages/opencode/test/command/builtin-commands.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| import { describe, test, expect } from "bun:test" | ||
| import { Command } from "../../src/command/index" | ||
| import { Instance } from "../../src/project/instance" | ||
| import { tmpdir } from "../fixture/fixture" | ||
|
|
||
| async function withInstance(fn: () => Promise<void>) { | ||
| await using tmp = await tmpdir({ git: true }) | ||
| await Instance.provide({ directory: tmp.path, fn }) | ||
| } | ||
|
|
||
| describe("altimate builtin commands", () => { | ||
| describe("discover-and-add-mcps", () => { | ||
| test("is registered as a default command", async () => { | ||
| await withInstance(async () => { | ||
| const cmd = await Command.get("discover-and-add-mcps") | ||
| expect(cmd).toBeDefined() | ||
| expect(cmd.name).toBe("discover-and-add-mcps") | ||
| expect(cmd.source).toBe("command") | ||
| }) | ||
| }) | ||
|
|
||
| test("has correct description", async () => { | ||
| await withInstance(async () => { | ||
| const cmd = await Command.get("discover-and-add-mcps") | ||
| expect(cmd.description).toBe("discover MCP servers from external AI tool configs and add them") | ||
| }) | ||
| }) | ||
|
|
||
| test("template references MCP discovery workflow", async () => { | ||
| await withInstance(async () => { | ||
| const cmd = await Command.get("discover-and-add-mcps") | ||
| const template = await cmd.template | ||
| expect(typeof template).toBe("string") | ||
| expect(template.length).toBeGreaterThan(0) | ||
| // The template should reference MCP-related concepts | ||
| expect(template.toLowerCase()).toContain("mcp") | ||
| }) | ||
| }) | ||
|
|
||
| test("is present in Command.Default constants", () => { | ||
| expect(Command.Default.DISCOVER_MCPS).toBe("discover-and-add-mcps") | ||
| }) | ||
| }) | ||
|
|
||
| describe("configure-claude", () => { | ||
| test("is registered as a default command", async () => { | ||
| await withInstance(async () => { | ||
| const cmd = await Command.get("configure-claude") | ||
| expect(cmd).toBeDefined() | ||
| expect(cmd.name).toBe("configure-claude") | ||
| expect(cmd.source).toBe("command") | ||
| }) | ||
| }) | ||
|
|
||
| test("has correct description", async () => { | ||
| await withInstance(async () => { | ||
| const cmd = await Command.get("configure-claude") | ||
| expect(cmd.description).toBe("configure /altimate command in Claude Code") | ||
| }) | ||
| }) | ||
|
|
||
| test("is present in Command.Default constants", () => { | ||
| expect(Command.Default.CONFIGURE_CLAUDE).toBe("configure-claude") | ||
| }) | ||
| }) | ||
|
|
||
| describe("configure-codex", () => { | ||
| test("is registered as a default command", async () => { | ||
| await withInstance(async () => { | ||
| const cmd = await Command.get("configure-codex") | ||
| expect(cmd).toBeDefined() | ||
| expect(cmd.name).toBe("configure-codex") | ||
| expect(cmd.source).toBe("command") | ||
| }) | ||
| }) | ||
|
|
||
| test("has correct description", async () => { | ||
| await withInstance(async () => { | ||
| const cmd = await Command.get("configure-codex") | ||
| expect(cmd.description).toBe("configure altimate skill in Codex CLI") | ||
| }) | ||
| }) | ||
|
|
||
| test("is present in Command.Default constants", () => { | ||
| expect(Command.Default.CONFIGURE_CODEX).toBe("configure-codex") | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| describe("Command.hints()", () => { | ||
| test("extracts numbered placeholders in order", () => { | ||
| expect(Command.hints("Do $1 then $2")).toEqual(["$1", "$2"]) | ||
| }) | ||
|
|
||
| test("extracts $ARGUMENTS", () => { | ||
| expect(Command.hints("Run with $ARGUMENTS")).toEqual(["$ARGUMENTS"]) | ||
| }) | ||
|
|
||
| test("extracts both numbered and $ARGUMENTS", () => { | ||
| expect(Command.hints("Do $1 with $ARGUMENTS")).toEqual(["$1", "$ARGUMENTS"]) | ||
| }) | ||
|
|
||
| test("deduplicates repeated placeholders", () => { | ||
| expect(Command.hints("$1 and $1 again")).toEqual(["$1"]) | ||
| }) | ||
|
|
||
| test("returns empty array for no placeholders", () => { | ||
| expect(Command.hints("plain text")).toEqual([]) | ||
| }) | ||
|
|
||
| test("sorts numbered placeholders lexicographically", () => { | ||
| // Note: uses .sort() with no comparator, so $10 sorts before $2. | ||
| // For single-digit placeholders, lexicographic order matches numeric. | ||
| expect(Command.hints("$3 then $1 then $2")).toEqual(["$1", "$2", "$3"]) | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lexicographic-sort test misses the multi-digit edge case.
The test name says lexicographic ordering, but the assertion only uses single-digit placeholders; it won’t catch
$10vs$2regressions.Suggested assertion update
test("sorts numbered placeholders lexicographically", () => { - // Note: uses .sort() with no comparator, so $10 sorts before $2. - // For single-digit placeholders, lexicographic order matches numeric. - expect(Command.hints("$3 then $1 then $2")).toEqual(["$1", "$2", "$3"]) + expect(Command.hints("$3 then $1 then $2")).toEqual(["$1", "$2", "$3"]) + expect(Command.hints("$10 then $2 then $1")).toEqual(["$1", "$10", "$2"]) })📝 Committable suggestion
🤖 Prompt for AI Agents