char-slop/berry

multipurpose self-modifiable agentic harness

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

Charlotte Somdiff: add (codemirror) inline editing of diffsc5dd844

main
1.7 KiB45 linesraw
1import { assertEquals } from "@std/assert";
2import { EditorView } from "@codemirror/view";
3import { until } from "@berry/util";
4import { withDom } from "@berry/util/dom.ts";
5import { highlighting } from "./highlight.ts";
6
7Deno.test("highlight replies decorate the current text across lines without moving selection or applying stale tokens", () =>
8  withDom(async () => {
9    const replies: { source: string; resolve(lines: Node[][]): void }[] = [];
10    const parent = document.createElement("div");
11    document.body.append(parent);
12    const view = new EditorView({
13      parent,
14      doc: "old\nname",
15      extensions: [highlighting("a.ts", (source) =>
16        new Promise((resolve) => {
17          replies.push({ source, resolve });
18        }))],
19    });
20    const lit = (a: string, b: string) => {
21      const keyword = document.createElement("a-k");
22      keyword.textContent = a;
23      const name = document.createElement("a-vm");
24      name.textContent = b;
25      return [[keyword], [name]];
26    };
27    try {
28      await until(() => replies.length === 1);
29      view.dispatch({
30        changes: { from: 0, to: view.state.doc.length, insert: "new\nother" },
31        selection: { anchor: 6 },
32      });
33      await until(() => replies.length === 2);
34      replies[1].resolve(lit("new", "other"));
35      await until(() => !!parent.querySelector("a-k"));
36      replies[0].resolve(lit("old", "name"));
37      await Promise.resolve();
38      assertEquals(parent.querySelector("a-k")!.textContent, "new");
39      assertEquals(parent.querySelector("a-vm")!.textContent, "other");
40      assertEquals(view.state.doc.toString(), "new\nother");
41      assertEquals(view.state.selection.main.anchor, 6);
42    } finally {
43      view.destroy();
44    }
45  }));