char-slop/berry

multipurpose self-modifiable agentic harness

git clone https://git.t4t.associates/char-slop/berry

Charlotte SomConfigure code editing as naive, hashline, or both per conversation8744a75

main
2.4 KiB57 linesraw
1import { assertEquals, assertMatch, assertRejects, assertStringIncludes } from "@std/assert";
2import { localEnvironment } from "@berry/api/environment.ts";
3import { testExtension } from "@berry/api/testing.ts";
4import { tempDir } from "@berry/util";
5import code from "./mod.ts";
6
7Deno.test("editing modes change reads, diffs, and accepted edits without losing legacy state", async () => {
8  await using root = await tempDir();
9  const path = `${root.path}/notes.txt`;
10  await Deno.writeTextFile(path, "hello\nworld\n");
11  const ext = testExtension(code, {
12    env: localEnvironment(root.path),
13    events: [{ type: "cd", cwd: "/old-directory" }],
14  });
15  const before = await ext.call("read", { path }) as string;
16  assertMatch(before, /^[A-Za-z]{4}│hello\n[A-Za-z]{4}│world$/);
17  const anchor = before.split("│")[0];
18
19  await ext.configure({ editMode: "naive" });
20  assertEquals(await ext.call("read", { path }), "hello\nworld\n");
21  assertEquals(await ext.call("read", { path, offset: 2, limit: 1 }), "world\n");
22  await assertRejects(() => ext.call("edit", { path, edits: [{ old: [anchor], new: ["bad"] }] }));
23  const diff = await ext.call("edit", {
24    path,
25    edits: [{ oldText: "hello", newText: "hi" }],
26  }) as string;
27  assertStringIncludes(diff, "-hello\n+hi");
28  assertEquals(await Deno.readTextFile(path), "hi\nworld\n");
29
30  await ext.configure({ editMode: "hashline" });
31  const read = await ext.call("read", { path }) as string;
32  assertMatch(read, /^[A-Za-z]{4}│hi/);
33  await assertRejects(() =>
34    ext.call("edit", {
35      path,
36      edits: [{ oldText: "hi", newText: "bad" }],
37    })
38  );
39  const hashed = await ext.call("edit", {
40    path,
41    edits: [{ old: [read.split("│")[0]], new: ["hey"] }],
42  }) as string;
43  assertMatch(hashed, /\+[A-Za-z]{4}│hey/);
44  assertEquals(await Deno.readTextFile(path), "hey\nworld\n");
45
46  await Deno.writeTextFile(path, "");
47  await ext.configure({ editMode: "naive" });
48  assertStringIncludes(await ext.call("read", { path }) as string, 'oldText: ""');
49  await ext.call("edit", { path, edits: [{ oldText: "", newText: "seeded" }] });
50  assertEquals(await Deno.readTextFile(path), "seeded");
51
52  await Deno.writeTextFile(path, "one\r\ntwo\nthree");
53  const literal = await ext.call("read", { path }) as string;
54  assertEquals(literal, "one\r\ntwo\nthree");
55  await ext.call("edit", { path, edits: [{ oldText: literal, newText: "replaced" }] });
56  assertEquals(await Deno.readTextFile(path), "replaced");
57});