import { assertEquals, assertMatch, assertRejects, assertStringIncludes } from "@std/assert"; import { localEnvironment } from "@berry/api/environment.ts"; import { testExtension } from "@berry/api/testing.ts"; import { tempDir } from "@berry/util"; import code from "./mod.ts"; Deno.test("editing modes change reads, diffs, and accepted edits without losing legacy state", async () => { await using root = await tempDir(); const path = `${root.path}/notes.txt`; await Deno.writeTextFile(path, "hello\nworld\n"); const ext = testExtension(code, { env: localEnvironment(root.path), events: [{ type: "cd", cwd: "/old-directory" }], }); const before = await ext.call("read", { path }) as string; assertMatch(before, /^[A-Za-z]{4}│hello\n[A-Za-z]{4}│world$/); const anchor = before.split("│")[0]; await ext.configure({ editMode: "naive" }); assertEquals(await ext.call("read", { path }), "hello\nworld\n"); assertEquals(await ext.call("read", { path, offset: 2, limit: 1 }), "world\n"); await assertRejects(() => ext.call("edit", { path, edits: [{ old: [anchor], new: ["bad"] }] })); const diff = await ext.call("edit", { path, edits: [{ oldText: "hello", newText: "hi" }], }) as string; assertStringIncludes(diff, "-hello\n+hi"); assertEquals(await Deno.readTextFile(path), "hi\nworld\n"); await ext.configure({ editMode: "hashline" }); const read = await ext.call("read", { path }) as string; assertMatch(read, /^[A-Za-z]{4}│hi/); await assertRejects(() => ext.call("edit", { path, edits: [{ oldText: "hi", newText: "bad" }], }) ); const hashed = await ext.call("edit", { path, edits: [{ old: [read.split("│")[0]], new: ["hey"] }], }) as string; assertMatch(hashed, /\+[A-Za-z]{4}│hey/); assertEquals(await Deno.readTextFile(path), "hey\nworld\n"); await Deno.writeTextFile(path, ""); await ext.configure({ editMode: "naive" }); assertStringIncludes(await ext.call("read", { path }) as string, 'oldText: ""'); await ext.call("edit", { path, edits: [{ oldText: "", newText: "seeded" }] }); assertEquals(await Deno.readTextFile(path), "seeded"); await Deno.writeTextFile(path, "one\r\ntwo\nthree"); const literal = await ext.call("read", { path }) as string; assertEquals(literal, "one\r\ntwo\nthree"); await ext.call("edit", { path, edits: [{ oldText: literal, newText: "replaced" }] }); assertEquals(await Deno.readTextFile(path), "replaced"); });