fix(coding-agent): used session settings in file guards
Passed session-scoped settings through Edit and Write generated-file checks and fell back to schema defaults when no global singleton exists. Guarded inline image sizing against an uninitialized global settings proxy and added isolated-session regression coverage. Fixes #6549
This commit is contained in:
@@ -1,49 +1,75 @@
|
||||
import { beforeAll, describe, expect, it } from "bun:test";
|
||||
import { afterAll, 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 { resolveImageOptions } from "@oh-my-pi/pi-coding-agent/tools/render-utils";
|
||||
import { ToolError } from "@oh-my-pi/pi-coding-agent/tools/tool-errors";
|
||||
|
||||
let tempDir: string;
|
||||
let testSettings: Settings;
|
||||
const GENERATED_TYPESCRIPT = "// Code generated by sqlc. DO NOT EDIT.\n\nexport const foo = 1;";
|
||||
|
||||
beforeAll(async () => {
|
||||
resetSettingsForTest();
|
||||
tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "auto-gen-guard-"));
|
||||
await Settings.init({ inMemory: true, cwd: tempDir });
|
||||
testSettings = Settings.isolated();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
resetSettingsForTest();
|
||||
await fs.rm(tempDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
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);
|
||||
expect(() => assertEditableFileContent(GENERATED_TYPESCRIPT, "test.ts", testSettings)).toThrow(ToolError);
|
||||
});
|
||||
|
||||
it("detects @generated marker", () => {
|
||||
const content = "// @generated\n\nexport const foo = 1;";
|
||||
expect(() => assertEditableFileContent(content, "test.ts")).toThrow(ToolError);
|
||||
expect(() => assertEditableFileContent(content, "test.ts", testSettings)).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);
|
||||
expect(() => assertEditableFileContent(content, "test.py", testSettings)).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);
|
||||
expect(() => assertEditableFileContent(content, "query.sql", testSettings)).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);
|
||||
expect(() => assertEditableFileContent(content, "test.ts", testSettings)).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);
|
||||
expect(() => assertEditableFileContent(content, "db.ts", testSettings)).toThrow(ToolError);
|
||||
});
|
||||
|
||||
it("defaults to blocking when global settings are uninitialized", () => {
|
||||
resetSettingsForTest();
|
||||
expect(() => assertEditableFileContent(GENERATED_TYPESCRIPT, "test.ts")).toThrow(ToolError);
|
||||
});
|
||||
|
||||
it("honors isolated session settings without global initialization", () => {
|
||||
const permissiveSettings = Settings.isolated({ "edit.blockAutoGenerated": false });
|
||||
expect(() => assertEditableFileContent(GENERATED_TYPESCRIPT, "test.ts", permissiveSettings)).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveImageOptions", () => {
|
||||
it("uses schema defaults when global settings are uninitialized", () => {
|
||||
resetSettingsForTest();
|
||||
const options = resolveImageOptions();
|
||||
expect(options.maxWidthCells).toBe(100);
|
||||
expect(options.maxHeightCells).toBeGreaterThan(0);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -51,17 +77,17 @@ describe("assertEditableFile", () => {
|
||||
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);
|
||||
await expect(assertEditableFile(filePath, undefined, testSettings)).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();
|
||||
await expect(assertEditableFile(filePath, undefined, testSettings)).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
it("handles missing files gracefully", async () => {
|
||||
const filePath = path.join(tempDir, "does-not-exist.ts");
|
||||
await expect(assertEditableFile(filePath)).resolves.toBeUndefined();
|
||||
await expect(assertEditableFile(filePath, undefined, testSettings)).resolves.toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user