import { afterEach, beforeEach, expect, it } from "bun:test"; import * as fs from "node:fs"; import * as os from "node:os"; import * as path from "node:path"; import { resetSettingsForTest, Settings } from "@sayknow-cli/coding-agent/config/settings"; import { EditTool } from "@sayknow-cli/coding-agent/edit"; import type { ToolSession } from "@sayknow-cli/coding-agent/tools"; function makeSession(cwd: string, blockAutoGenerated: boolean): ToolSession { return { cwd, hasUI: false, getSessionFile: () => null, getSessionSpawns: () => "*", enableLsp: false, settings: Settings.isolated({ "edit.mode": "replace", "edit.blockAutoGenerated": blockAutoGenerated, }), getArtifactsDir: () => null, getSessionId: () => null, getPlanModeState: () => undefined, } as unknown as ToolSession; } let tempDir: string; beforeEach(async () => { resetSettingsForTest(); tempDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "edit-replace-autogen-")); await Settings.init({ inMemory: true, cwd: tempDir, overrides: { "edit.blockAutoGenerated": true } }); }); afterEach(async () => { resetSettingsForTest(); await fs.promises.rm(tempDir, { recursive: true, force: true }); }); it("refuses a content-marked auto-generated file through the edit tool", async () => { const original = "// Code generated by sqlc. DO NOT EDIT.\nexport const foo = 1;\n"; const filePath = path.join(tempDir, "generated.ts"); await Bun.write(filePath, original); const tool = new EditTool(makeSession(tempDir, true)); await expect( tool.execute("replace-generated-content", { path: "generated.ts", edits: [{ old_text: "export const foo = 1;", new_text: "export const foo = 2;" }], }), ).rejects.toThrow(/Cannot modify auto-generated file/); expect(await Bun.file(filePath).text()).toBe(original); }); it("refuses a generated filename through the edit tool", async () => { const original = "package widget\n\nconst X = 1\n"; const filePath = path.join(tempDir, "widget.pb.go"); await Bun.write(filePath, original); const tool = new EditTool(makeSession(tempDir, true)); await expect( tool.execute("replace-generated-name", { path: "widget.pb.go", edits: [{ old_text: "const X = 1", new_text: "const X = 2" }], }), ).rejects.toThrow(/Cannot modify auto-generated file/); expect(await Bun.file(filePath).text()).toBe(original); }); it("refuses every replace entry on an auto-generated file", async () => { const original = "// Code generated by sqlc. DO NOT EDIT.\nexport const foo = 1;\nexport const bar = 2;\n"; const filePath = path.join(tempDir, "queries.ts"); await Bun.write(filePath, original); const tool = new EditTool(makeSession(tempDir, true)); const result = await tool.execute("replace-generated-multi", { path: "queries.ts", edits: [ { old_text: "export const foo = 1;", new_text: "export const foo = 11;" }, { old_text: "export const bar = 2;", new_text: "export const bar = 22;" }, ], }); expect(result.isError).toBe(true); const text = (result.content?.find(block => block.type === "text") as { text?: string } | undefined)?.text ?? ""; expect(text).toContain("Cannot modify auto-generated file"); expect(await Bun.file(filePath).text()).toBe(original); }); it("refuses a batch whose earlier entry would make the file generated", async () => { const original = "// handwritten\nconst x = 1;\n"; const filePath = path.join(tempDir, "hand.ts"); await Bun.write(filePath, original); const tool = new EditTool(makeSession(tempDir, true)); const result = await tool.execute("replace-batch-header", { path: "hand.ts", edits: [ { old_text: "// handwritten", new_text: "// Code generated by sqlc. DO NOT EDIT." }, { old_text: "const x = 1;", new_text: "const x = 2;" }, ], }); expect(result.isError).toBe(true); const text = (result.content?.find(block => block.type === "text") as { text?: string } | undefined)?.text ?? ""; expect(text).toContain("Cannot modify auto-generated file"); expect(await Bun.file(filePath).text()).toBe(original); }); it("refuses to leave an earlier replacement when a later entry does not match", async () => { const original = "const a = 1;\nconst b = 2;\n"; const filePath = path.join(tempDir, "hand.ts"); await Bun.write(filePath, original); const tool = new EditTool(makeSession(tempDir, true)); const result = await tool.execute("replace-batch-mismatch", { path: "hand.ts", edits: [ { old_text: "const a = 1;", new_text: "const a = 3;" }, { old_text: "missing", new_text: "nope" }, ], }); expect(result.isError).toBe(true); expect(await Bun.file(filePath).text()).toBe(original); }); it("still allows two handwritten replacements in one call", async () => { const filePath = path.join(tempDir, "hand.ts"); await Bun.write(filePath, "const a = 1;\nconst b = 2;\n"); const tool = new EditTool(makeSession(tempDir, true)); const result = await tool.execute("replace-batch-hand", { path: "hand.ts", edits: [ { old_text: "const a = 1;", new_text: "const a = 3;" }, { old_text: "const b = 2;", new_text: "const b = 4;" }, ], }); expect(result.isError).toBeUndefined(); expect(await Bun.file(filePath).text()).toBe("const a = 3;\nconst b = 4;\n"); }); it("still allows a generated-header batch when the setting is off", async () => { const filePath = path.join(tempDir, "hand.ts"); await Bun.write(filePath, "// handwritten\nconst x = 1;\n"); const tool = new EditTool(makeSession(tempDir, false)); const result = await tool.execute("replace-batch-header-off", { path: "hand.ts", edits: [ { old_text: "// handwritten", new_text: "// Code generated by sqlc. DO NOT EDIT." }, { old_text: "const x = 1;", new_text: "const x = 2;" }, ], }); expect(result.isError).toBeUndefined(); expect(await Bun.file(filePath).text()).toBe("// Code generated by sqlc. DO NOT EDIT.\nconst x = 2;\n"); }); it("still allows a handwritten file edit when the guard is enabled", async () => { const filePath = path.join(tempDir, "hand.ts"); await Bun.write(filePath, "export const foo = 1;\n"); const tool = new EditTool(makeSession(tempDir, true)); const result = await tool.execute("replace-handwritten", { path: "hand.ts", edits: [{ old_text: "export const foo = 1;", new_text: "export const foo = 2;" }], }); expect(result.isError).toBeUndefined(); expect(await Bun.file(filePath).text()).toBe("export const foo = 2;\n"); }); it("still allows an auto-generated file edit when the setting is off", async () => { const filePath = path.join(tempDir, "generated.ts"); await Bun.write(filePath, "// Code generated by sqlc. DO NOT EDIT.\nexport const foo = 1;\n"); const tool = new EditTool(makeSession(tempDir, false)); const result = await tool.execute("replace-generated-off", { path: "generated.ts", edits: [{ old_text: "export const foo = 1;", new_text: "export const foo = 2;" }], }); expect(result.isError).toBeUndefined(); expect(await Bun.file(filePath).text()).toBe("// Code generated by sqlc. DO NOT EDIT.\nexport const foo = 2;\n"); });