Files
oh-my-pi/packages/coding-agent/test/tools/auto-generated-guard.test.ts
T
can1357 3f4384f6b1 fix(auto-generated-guard): corrected file not found handling in marker detection
- Fixed file not found error handling in auto-generated marker detection to gracefully return undefined instead of propagating errors.
- Made assertEditableFileContent function synchronous to simplify API and improve error handling consistency.
- Updated test assertions to match synchronous function behavior using toThrow instead of rejects patterns.
- Removed obsolete test case for doc comment preservation during replace operations.
2026-04-08 15:06:47 +02:00

106 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 { assertEditableFile, assertEditableFileContent } 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("assertEditableFileContent", () => {
it("detects canonical TypeScript generated header", () => {
const content = "// Code generated by sqlc. DO NOT EDIT.\n\nexport const foo = 1;";
expect(() => assertEditableFileContent(content, "test.ts")).toThrow(ToolError);
});
it("detects @generated marker", () => {
const content = "// @generated\n\nexport const foo = 1;";
expect(() => assertEditableFileContent(content, "test.ts")).toThrow(ToolError);
});
it("detects generated-by marker for Python files", () => {
const content = "# Generated by buf\n\nvalue = 1";
expect(() => assertEditableFileContent(content, "test.py")).toThrow(ToolError);
});
it("detects generated-by marker for SQL files", () => {
const content = "-- generated by sqlc\n\nselect 1;";
expect(() => assertEditableFileContent(content, "query.sql")).toThrow(ToolError);
});
it("detects generated markers in leading block comments", () => {
const content = "/*\n * Code generated by mockery. DO NOT EDIT.\n */\nexport const foo = 1;";
expect(() => assertEditableFileContent(content, "test.ts")).toThrow(ToolError);
});
it("detects kysely-codegen generated header", () => {
const content =
"/**\n * This file was generated by kysely-codegen.\n * Please do not edit it manually.\n */\n\nexport interface Database {}";
expect(() => assertEditableFileContent(content, "db.ts")).toThrow(ToolError);
});
it("does not block broad prose comment markers", () => {
const content = "// auto generated dont edit bla bla\n// this is a hand-written file note\nexport const foo = 1;";
expect(() => assertEditableFileContent(content, "test.ts")).not.toThrow();
});
it("does not match generated markers after code starts", () => {
const content = "export const foo = 1;\n\n// Code generated by sqlc. DO NOT EDIT.";
expect(() => assertEditableFileContent(content, "test.ts")).not.toThrow();
});
it("uses language-specific comment styles", () => {
const tsContent = "# Code generated by sqlc. DO NOT EDIT.\nexport const foo = 1;";
expect(() => assertEditableFileContent(tsContent, "test.ts")).not.toThrow();
const pyContent = "// Code generated by sqlc. DO NOT EDIT.\nvalue = 1";
expect(() => assertEditableFileContent(pyContent, "test.py")).not.toThrow();
});
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();
expect(() =>
assertEditableFileContent(content, "packages/coding-agent/src/tools/auto-generated-guard.ts"),
).not.toThrow();
});
it("checks only first 1024 bytes of content", () => {
const prefix = "A".repeat(1024);
const content = `${prefix}\n// Code generated by sqlc. DO NOT EDIT.`;
expect(() => assertEditableFileContent(content, "test.ts")).not.toThrow();
});
});
describe("assertEditableFile", () => {
it("detects auto-generated filename patterns", async () => {
const filePath = path.join(tempDir, "zz_generated.deepcopy.go");
await Bun.write(filePath, "package generated");
await expect(assertEditableFile(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(assertEditableFile(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(assertEditableFile(filePath)).resolves.toBeUndefined();
});
it("handles missing files gracefully", async () => {
const filePath = path.join(tempDir, "does-not-exist.ts");
await expect(assertEditableFile(filePath)).resolves.toBeUndefined();
});
});