char-slop/berry

multipurpose self-modifiable agentic harness

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

Charlotte Somcut a bunch of code volumecb11725

main
5.6 KiB129 linesraw
1import { assertEquals } from "@std/assert";
2import { homedir } from "node:os";
3import { BACKGROUND_CONTEXT as context } from "@earendil-works/chord/context";
4import { localEnvironment } from "@berry/api/environment.ts";
5import { rootedEnvironment } from "@berry/api/testing.ts";
6import { references } from "./references.ts";
7import { tempDir } from "@berry/util";
8
9Deno.test("reference paths preserve their spelling and browse directories and symlinks", async () => {
10  await using rootDir = await tempDir();
11  const root = rootDir.path;
12  await Deno.mkdir(`${root}/src`);
13  await Deno.writeTextFile(`${root}/src/main.ts`, "");
14  await Deno.writeTextFile(`${root}/README.md`, "");
15  await Deno.writeTextFile(`${root}/has spaces`, "");
16  await Deno.symlink(`${root}/src`, `${root}/link`);
17  assertEquals(await references(localEnvironment(root), "", context), [
18    "link/",
19    "README.md",
20    "src/",
21  ]);
22  assertEquals(await references(localEnvironment(root), "R", context), [
23    "README.md",
24    "src/",
25    "src/main.ts",
26  ]);
27  assertEquals(await references(localEnvironment(root), "src/m", context), ["src/main.ts"]);
28  assertEquals(await references(localEnvironment(root), "./src/", context), ["./src/main.ts"]);
29  assertEquals(await references(localEnvironment(`${root}/src`), "../R", context), [
30    "../README.md",
31    "../src/",
32    "../src/main.ts",
33  ]);
34  assertEquals(await references(localEnvironment(root), `${root}/src/`, context), [
35    `${root}/src/main.ts`,
36  ]);
37  assertEquals(await references(localEnvironment(root), "link/", context), ["link/main.ts"]);
38  assertEquals(await references(localEnvironment(root), "missing/", context), []);
39  assertEquals(await references(localEnvironment(root), "README.md/", context), []);
40  assertEquals(await references(localEnvironment(root), "bad\u0000path", context), []);
41});
42
43Deno.test("home references resolve without changing the completion's tilde", async () => {
44  await using rootDir = await tempDir({ dir: homedir(), prefix: "berry-references-" });
45  const root = rootDir.path;
46  await Deno.writeTextFile(`${root}/test.ts`, "");
47  const prefix = `~/${root.slice(homedir().length + 1)}/`;
48  assertEquals(await references(localEnvironment("/"), prefix, context), [`${prefix}test.ts`]);
49});
50
51Deno.test("references rank exact names, prefixes, substrings, and fuzzy matches across the tree", async () => {
52  await using rootDir = await tempDir();
53  const root = rootDir.path;
54  for (
55    const path of [
56      "deep/nested/cmp",
57      "web/composer.tsx",
58      "cmp-extra.ts",
59      "a-cmp.ts",
60      "c-many-more-parts.ts",
61      "unrelated.ts",
62    ]
63  ) {
64    await Deno.mkdir(`${root}/${path.split("/").slice(0, -1).join("/")}`, { recursive: true });
65    await Deno.writeTextFile(`${root}/${path}`, "");
66  }
67  assertEquals(await references(localEnvironment(root), "CMP", context), [
68    "deep/nested/cmp",
69    "cmp-extra.ts",
70    "a-cmp.ts",
71    "web/composer.tsx",
72    "c-many-more-parts.ts",
73  ]);
74  assertEquals(await references(localEnvironment(root), "wb/cmp", context), ["web/composer.tsx"]);
75  assertEquals(await references(localEnvironment(root), "web/cmp", context), [
76    "web/composer.tsx",
77  ]);
78  assertEquals(await references(localEnvironment(root), "zzzzz", context), []);
79});
80
81Deno.test("recursive references honor nested ignores and do not follow symlink cycles", async () => {
82  await using rootDir = await tempDir();
83  const root = rootDir.path;
84  for (const dir of [".git", ".jj", "node_modules", "data", "src"]) {
85    await Deno.mkdir(`${root}/${dir}`);
86    await Deno.writeTextFile(`${root}/${dir}/target.ts`, "");
87  }
88  await Deno.writeTextFile(`${root}/.gitignore`, "data/\n*.log\n");
89  await Deno.writeTextFile(`${root}/src/.gitignore`, "target.ts\n!keep.log\n");
90  await Deno.writeTextFile(`${root}/src/target.log`, "");
91  await Deno.writeTextFile(`${root}/src/keep.log`, "");
92  await Deno.writeTextFile(`${root}/target.ts`, "");
93  await Deno.symlink(root, `${root}/src/loop`);
94  await Deno.symlink(`${root}/missing`, `${root}/broken`);
95  assertEquals(await references(localEnvironment(root), "target", context), ["target.ts"]);
96  assertEquals(await references(localEnvironment(root), "keep", context), ["src/keep.log"]);
97  assertEquals(await references(localEnvironment(root), "loop", context), ["src/loop/"]);
98  assertEquals(await references(localEnvironment(root), "broken", context), ["broken"]);
99});
100
101Deno.test("reference results are limited only after ranking the whole tree", async () => {
102  await using rootDir = await tempDir();
103  const root = rootDir.path;
104  await Deno.mkdir(`${root}/z`);
105  await Deno.writeTextFile(`${root}/z/target`, "");
106  for (let i = 0; i < 60; i++) {
107    await Deno.writeTextFile(`${root}/a-target-${String(i).padStart(2, "0")}`, "");
108  }
109  const result = await references(localEnvironment(root), "target", context);
110  assertEquals(result.length, 50);
111  assertEquals(result[0], "z/target");
112  assertEquals(
113    result.slice(1),
114    Array.from({ length: 49 }, (_, i) => `a-target-${String(i).padStart(2, "0")}`),
115  );
116});
117
118Deno.test("references browse the conversation's environment, not this machine", async () => {
119  await using rootDir = await tempDir();
120  const root = rootDir.path;
121  await Deno.mkdir(`${root}/project/src`, { recursive: true });
122  await Deno.mkdir(`${root}/home`);
123  await Deno.writeTextFile(`${root}/project/src/main.ts`, "");
124  await Deno.writeTextFile(`${root}/home/notes.md`, "");
125  const env = rootedEnvironment(root, { cwd: "/project" });
126  assertEquals(await references(env, "src/", context), ["src/main.ts"]);
127  assertEquals(await references(env, "/project/src/", context), ["/project/src/main.ts"]);
128  assertEquals(await references(env, "~/", context), ["~/notes.md"]);
129});