Files
oh-my-pi/packages/coding-agent/test/tools/ast-grep.test.ts
T
can1357 50cd2310dc feat(pi-natives): added language module with 39 languages and tree-sitter bindings
- Added language module with 39 supported languages and tree-sitter parser bindings for ast-grep integration.
- Exported dedupeParseErrors utility function to remove duplicate parse error messages in tool output.
- Fixed duplicate parse error messages when multiple patterns fail on the same file.
- Replaced language alias lookup with O(1) phf::Map and added 30+ new language aliases (golang, julia, toml, zig, etc.).
- Migrated from ast-grep-language to direct tree-sitter language dependencies for improved language support.
2026-03-04 17:02:29 +01:00

51 lines
1.9 KiB
TypeScript

import { describe, expect, it } from "bun:test";
import * as fs from "node:fs/promises";
import * as os from "node:os";
import * as path from "node:path";
import { Settings } from "@oh-my-pi/pi-coding-agent/config/settings";
import { createTools, type ToolSession } from "@oh-my-pi/pi-coding-agent/tools";
function createTestSession(cwd = "/tmp/test", overrides: Partial<ToolSession> = {}): ToolSession {
return {
cwd,
hasUI: true,
getSessionFile: () => null,
getSessionSpawns: () => "*",
settings: Settings.isolated(),
...overrides,
};
}
describe("ast_grep parse errors", () => {
it("collapses per-pattern parse errors for the same file", async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "ast-grep-parse-"));
try {
const filePath = path.join(tempDir, "broken.ts");
await Bun.write(filePath, "export function broken( { return 1; }");
const tools = await createTools(createTestSession(tempDir));
const tool = tools.find(entry => entry.name === "ast_grep");
expect(tool).toBeDefined();
const result = await tool!.execute("ast-grep-parse", {
patterns: ["someUnlikelyCall($A)", "anotherUnlikelyCall($A)"],
lang: "typescript",
path: filePath,
});
const text = result.content.find(content => content.type === "text")?.text ?? "";
const details = result.details as { parseErrors?: string[]; matchCount?: number } | undefined;
expect(details?.matchCount).toBe(0);
expect(text).toContain("No matches found");
expect(details?.parseErrors).toHaveLength(1);
expect(details?.parseErrors?.[0]).toContain("broken.ts: parse error (syntax tree contains error nodes)");
expect(details?.parseErrors?.[0]).not.toContain("someUnlikelyCall($A):");
expect(details?.parseErrors?.[0]).not.toContain("anotherUnlikelyCall($A):");
expect(text.match(/parse error \(syntax tree contains error nodes\)/g)?.length ?? 0).toBe(1);
} finally {
await fs.rm(tempDir, { recursive: true, force: true });
}
});
});