char-slop/berry

multipurpose self-modifiable agentic harness

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

Charlotte Sommake /workspace add workspace dir to project workspacesf94442c

main
3.9 KiB99 linesraw
1import { assertEquals } from "@std/assert";
2import { BACKGROUND_CONTEXT as context } from "@earendil-works/chord/context";
3import { projectFor } from "@berry/api/projects.ts";
4import { onPath, tempDir } from "@berry/util";
5import { join } from "node:path";
6import code from "../extensions/code/mod.ts";
7import { type Environments, localOnly } from "./environment.ts";
8import { setup } from "./fixture.ts";
9import { Projects } from "./projects.ts";
10
11Deno.test({
12  name:
13    "workspace inherits the source directory's project on its machine without changing the default",
14  ignore: !onPath("jj"),
15  async fn() {
16    await using root = await tempDir();
17    const repo = join(root.path, "repo");
18    const config = join(root.path, "config.toml");
19    await Deno.writeTextFile(config, '[user]\nname = "Test"\nemail = "test@example.com"\n');
20    const variables = {
21      ...Deno.env.toObject(),
22      JJ_CONFIG: config,
23      BERRY_WORKSPACES_DIR: join(root.path, "workspaces"),
24    };
25    const jj = async (...args: string[]) => {
26      const result = await new Deno.Command("jj", {
27        args: ["--no-pager", "--color=never", ...args],
28        cwd: root.path,
29        env: variables,
30        stdout: "piped",
31        stderr: "piped",
32      }).output();
33      assertEquals(result.success, true, new TextDecoder().decode(result.stderr));
34    };
35    await jj("git", "init", repo);
36    await Deno.writeTextFile(join(repo, "baseline"), "base\n");
37    await jj("-R", repo, "new");
38    const src = join(repo, "src");
39    const outside = join(root.path, "outside");
40    await Deno.mkdir(src);
41    await Deno.mkdir(outside);
42    const environments: Environments = {
43      async open(name, cwd, during) {
44        const env = await localOnly.open("local", cwd, during);
45        Object.defineProperty(env, "variables", { value: variables });
46        return Object.assign(env, { name });
47      },
48      close: () => Promise.resolve(),
49    };
50    await using harness = await setup(environments);
51    const { berry } = harness;
52    await berry.install(code);
53    const local = await berry.saveProject(undefined, {
54      name: "Local",
55      roots: [{ environment: "local", cwd: repo }],
56      defaultRoot: 0,
57    });
58    const broad = await berry.saveProject(undefined, {
59      name: "Broad",
60      roots: [{ environment: "other", cwd: root.path }],
61      defaultRoot: 0,
62    });
63    const project = await berry.saveProject(undefined, {
64      name: "Repo",
65      roots: [{ environment: "other", cwd: repo }],
66      defaultRoot: 0,
67    });
68    const before = (await berry.harness.snapshot(Projects, context))!;
69    const tid = await berry.create({
70      extensions: { code: {}, environment: { environment: "other", cwd: src } },
71    });
72    const target = await berry.command(tid, "workspace", "") as string;
73    const after = (await berry.harness.snapshot(Projects, context))!;
74    assertEquals(after[project], {
75      ...before[project],
76      roots: [...before[project].roots, {
77        environment: "other",
78        cwd: target,
79        platform: before[project].roots[0].platform,
80      }],
81    });
82    assertEquals(after[local], before[local]);
83    assertEquals(after[broad], before[broad]);
84    const binding = await berry.state(tid, "environment") as { environment: string; cwd: string };
85    assertEquals(binding.cwd, target);
86    assertEquals(projectFor(after, binding), project);
87    assertEquals(await berry.command(tid, "workspace", ""), target);
88    await berry.command(tid, "cd", src);
89    assertEquals(await berry.command(tid, "workspace", ""), target);
90    await berry.command(tid, "cd", outside);
91    assertEquals(await berry.harness.snapshot(Projects, context), after);
92
93    const unassigned = await berry.create({ extensions: { code: {}, environment: { cwd: src } } });
94    await berry.deleteProject(local);
95    const withoutLocal = (await berry.harness.snapshot(Projects, context))!;
96    await berry.command(unassigned, "workspace", "");
97    assertEquals(await berry.harness.snapshot(Projects, context), withoutLocal);
98  },
99});