|
| 1 | +/** |
| 2 | + * Unit tests for AST tool argument shaping. |
| 3 | + */ |
| 4 | + |
| 5 | +/// <reference path="../bun-test.d.ts" /> |
| 6 | + |
| 7 | +import { describe, expect, test } from "bun:test"; |
| 8 | +import type { BridgePool } from "@cortexkit/aft-bridge"; |
| 9 | +import type { ToolContext } from "@opencode-ai/plugin"; |
| 10 | +import { astTools } from "../tools/ast.js"; |
| 11 | +import type { PluginContext } from "../types.js"; |
| 12 | + |
| 13 | +function createSdkContext(directory: string): ToolContext { |
| 14 | + return { |
| 15 | + sessionID: "test-session", |
| 16 | + messageID: "test-message", |
| 17 | + agent: "build", |
| 18 | + abort: new AbortController().signal, |
| 19 | + directory, |
| 20 | + worktree: directory, |
| 21 | + metadata: () => {}, |
| 22 | + } as ToolContext; |
| 23 | +} |
| 24 | + |
| 25 | +describe("AST tool adapters", () => { |
| 26 | + test('ast_grep_replace treats string dryRun "true" as preview (dry_run true)', async () => { |
| 27 | + const calls: Array<{ command: string; params: Record<string, unknown> }> = []; |
| 28 | + const bridge = { |
| 29 | + send: async (command: string, params: Record<string, unknown> = {}) => { |
| 30 | + calls.push({ command, params }); |
| 31 | + return { success: true, dry_run: true, text: "preview" }; |
| 32 | + }, |
| 33 | + }; |
| 34 | + const pool = { getBridge: () => bridge } as unknown as BridgePool; |
| 35 | + const ctx: PluginContext = { |
| 36 | + pool, |
| 37 | + client: { |
| 38 | + lsp: { status: async () => ({ data: [] }) }, |
| 39 | + find: { symbols: async () => ({ data: [] }) }, |
| 40 | + }, |
| 41 | + config: {} as PluginContext["config"], |
| 42 | + storageDir: "/tmp/aft-test", |
| 43 | + }; |
| 44 | + const tools = astTools(ctx); |
| 45 | + const dir = "/tmp/aft-ast-test"; |
| 46 | + const sdkCtx = createSdkContext(dir); |
| 47 | + |
| 48 | + await tools.ast_grep_replace.execute( |
| 49 | + { |
| 50 | + pattern: "foo($A)", |
| 51 | + rewrite: "bar($A)", |
| 52 | + lang: "javascript", |
| 53 | + dryRun: "true" as unknown as boolean, |
| 54 | + }, |
| 55 | + sdkCtx, |
| 56 | + ); |
| 57 | + |
| 58 | + expect(calls).toHaveLength(1); |
| 59 | + expect(calls[0]?.command).toBe("ast_replace"); |
| 60 | + expect(calls[0]?.params.dry_run).toBe(true); |
| 61 | + }); |
| 62 | +}); |
0 commit comments