Files
oh-my-pi/packages/coding-agent/test/tools/auto-generated-guard.test.ts
T
can1357 7cd4c65574 feat(coding-agent): added auto-generated file detection controls and improved accuracy
- Added `edit.blockAutoGenerated` setting to control enforcement of auto-generated file detection.
- Improved auto-generated file detection to use language-specific comment parsing instead of broad regex patterns, reducing false positives.
- Enhanced marker detection to scan only leading header comments (1024-byte limit) rather than entire file prefix for better accuracy.
- Fixed tool argument validation to properly handle string 'null' values on optional LLM tool arguments.
- Improved type safety by changing validateToolCall and validateToolArguments return types from any to ToolCall["arguments"].
2026-03-18 10:53:37 +01:00

103 lines
4.5 KiB
TypeScript

import { beforeAll, 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 { _resetSettingsForTest, Settings } from "@oh-my-pi/pi-coding-agent/config/settings";
import {
checkAutoGeneratedFile,
checkAutoGeneratedFileContent,
} from "@oh-my-pi/pi-coding-agent/tools/auto-generated-guard";
import { ToolError } from "@oh-my-pi/pi-coding-agent/tools/tool-errors";
let tempDir: string;
beforeAll(async () => {
_resetSettingsForTest();
tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "auto-gen-guard-"));
await Settings.init({ inMemory: true, cwd: tempDir });
});
describe("checkAutoGeneratedFileContent", () => {
it("detects canonical TypeScript generated header", async () => {
const content = "// Code generated by sqlc. DO NOT EDIT.\n\nexport const foo = 1;";
await expect(checkAutoGeneratedFileContent(content, "test.ts")).rejects.toBeInstanceOf(ToolError);
});
it("detects @generated marker", async () => {
const content = "// @generated\n\nexport const foo = 1;";
await expect(checkAutoGeneratedFileContent(content, "test.ts")).rejects.toBeInstanceOf(ToolError);
});
it("detects generated-by marker for Python files", async () => {
const content = "# Generated by buf\n\nvalue = 1";
await expect(checkAutoGeneratedFileContent(content, "test.py")).rejects.toBeInstanceOf(ToolError);
});
it("detects generated-by marker for SQL files", async () => {
const content = "-- generated by sqlc\n\nselect 1;";
await expect(checkAutoGeneratedFileContent(content, "query.sql")).rejects.toBeInstanceOf(ToolError);
});
it("detects generated markers in leading block comments", async () => {
const content = "/*\n * Code generated by mockery. DO NOT EDIT.\n */\nexport const foo = 1;";
await expect(checkAutoGeneratedFileContent(content, "test.ts")).rejects.toBeInstanceOf(ToolError);
});
it("does not block broad prose comment markers", async () => {
const content = "// auto generated dont edit bla bla\n// this is a hand-written file note\nexport const foo = 1;";
await expect(checkAutoGeneratedFileContent(content, "test.ts")).resolves.toBeUndefined();
});
it("does not match generated markers after code starts", async () => {
const content = "export const foo = 1;\n\n// Code generated by sqlc. DO NOT EDIT.";
await expect(checkAutoGeneratedFileContent(content, "test.ts")).resolves.toBeUndefined();
});
it("uses language-specific comment styles", async () => {
const tsContent = "# Code generated by sqlc. DO NOT EDIT.\nexport const foo = 1;";
await expect(checkAutoGeneratedFileContent(tsContent, "test.ts")).resolves.toBeUndefined();
const pyContent = "// Code generated by sqlc. DO NOT EDIT.\nvalue = 1";
await expect(checkAutoGeneratedFileContent(pyContent, "test.py")).resolves.toBeUndefined();
});
it("does not block editing the guard file itself", async () => {
const guardPath = path.join(import.meta.dir, "../../src/tools/auto-generated-guard.ts");
const content = await Bun.file(guardPath).text();
await expect(
checkAutoGeneratedFileContent(content, "packages/coding-agent/src/tools/auto-generated-guard.ts"),
).resolves.toBeUndefined();
});
it("checks only first 1024 bytes of content", async () => {
const prefix = "A".repeat(1024);
const content = `${prefix}\n// Code generated by sqlc. DO NOT EDIT.`;
await expect(checkAutoGeneratedFileContent(content, "test.ts")).resolves.toBeUndefined();
});
});
describe("checkAutoGeneratedFile", () => {
it("detects auto-generated filename patterns", async () => {
const filePath = path.join(tempDir, "zz_generated.deepcopy.go");
await Bun.write(filePath, "package generated");
await expect(checkAutoGeneratedFile(filePath)).rejects.toBeInstanceOf(ToolError);
});
it("detects content marker from file prefix", async () => {
const filePath = path.join(tempDir, "service.ts");
await Bun.write(filePath, "// Code generated by sqlc. DO NOT EDIT.\nexport const foo = 1;");
await expect(checkAutoGeneratedFile(filePath)).rejects.toBeInstanceOf(ToolError);
});
it("allows normal files", async () => {
const filePath = path.join(tempDir, "normal.ts");
await Bun.write(filePath, "// Regular source file\nexport const foo = 1;");
await expect(checkAutoGeneratedFile(filePath)).resolves.toBeUndefined();
});
it("handles missing files gracefully", async () => {
const filePath = path.join(tempDir, "does-not-exist.ts");
await expect(checkAutoGeneratedFile(filePath)).resolves.toBeUndefined();
});
});