iterate.com / kernel

git clone https://git.iterate.com/repos/kernel

kernel / core / mind-worker / src / mind / tree.test.tsmain at 949894f

// The mind's code as one tree of several repos (tree.ts): which repos a publication reads, what
// the tree looks like to the loader, and what it says when a repo it needs is not there.
import { expect, test } from "vitest";
import { importedTopFolders, readPackage } from "../context/module-resolution.ts";
import { gatheredTree, mergedTree, originBeside, reposNamedIn, treeKeyOf } from "./tree.ts";

test("a publication reads the entry repo and every repo that its imports reach, each at one commit, and no other repo", async () => {
  const mind = mindWith({
    "/repos/config": {
      "worker.ts": `import { init } from "@repos/stdlib/init.ts"; import { dashApp } from "@repos/dash/app.ts"; init(dashApp());`,
    },
    // stdlib's own import reaches a third repo, which the entry never names
    "/repos/stdlib": {
      "init.ts": `import { say } from "@repos/words/say.ts"; export const init = say;`,
    },
    "/repos/dash": { "app.ts": `export const dashApp = () => {};`, "index.html": "<p>the Dash</p>" },
    "/repos/words": { "say.ts": `export const say = () => "hello";` },
    // nothing imports from the kernel's repo: it is never read
    "/repos/kernel": { "README.md": "what only a deploy can change" },
  });

  const tree = await gatheredTree({ entry: CONFIG_WORKER, ...mind.reads });

  expect(tree).toMatchObject({
    commits: {
      "/repos/config": "config@1",
      "/repos/stdlib": "stdlib@1",
      "/repos/dash": "dash@1",
      "/repos/words": "words@1",
    },
    files: {
      "config/worker.ts": expect.stringContaining("@repos/stdlib/init.ts"),
      "stdlib/init.ts": expect.any(String),
      "dash/app.ts": expect.any(String),
      // a repo comes whole: a page is one of its files
      "dash/index.html": "<p>the Dash</p>",
      "words/say.ts": expect.any(String),
    },
  });
  expect(Object.keys(tree.commits)).not.toContain("/repos/kernel");
  expect(mind.read).not.toContain("/repos/kernel");
});

test("the tree's root names the entry module, and takes its npm packages from the entry repo's package.json alone: a line that names a repo is no npm package", () => {
  const tree = mergedTree(CONFIG_WORKER, {
    "/repos/config": {
      "worker.ts": "export default class {}",
      "package.json": JSON.stringify({
        dependencies: { yjs: "13.6.33", "@repos/stdlib": "../stdlib" },
      }),
    },
    "/repos/stdlib": {
      "init.ts": "export const init = () => {};",
      "package.json": JSON.stringify({ dependencies: { diff: "8.0.4" } }),
    },
  });

  expect(readPackage(tree, "the mind's code")).toMatchObject({
    entry: "config/worker.ts",
    dependencies: { yjs: "13.6.33" },
  });
  expect(readPackage(tree, "the mind's code").dependencies).toEqual({ yjs: "13.6.33" });
});

test("a repo that the code imports from and that has no commit is refused in words that name the file that imports it", async () => {
  const mind = mindWith({
    "/repos/config": { "worker.ts": `import { init } from "@repos/stdlib/init.ts"; init();` },
  });

  await expect(gatheredTree({ entry: CONFIG_WORKER, ...mind.reads })).rejects.toThrow(
    "config/worker.ts imports from /repos/stdlib, which has no commit on main",
  );
});

test("a relative import that leaves its repo is refused, in words that give the name to write", async () => {
  const mind = mindWith({
    "/repos/config": { "worker.ts": `import { init } from "../stdlib/init.ts"; init();` },
    "/repos/stdlib": { "init.ts": `export const init = () => {};` },
  });

  await expect(gatheredTree({ entry: CONFIG_WORKER, ...mind.reads })).rejects.toThrow(
    "config/worker.ts imports ../stdlib/init.ts, which is outside its own repo: write @repos/stdlib/init.ts",
  );
});

test("an entry repo with no commit is refused as itself", async () => {
  const mind = mindWith({});

  await expect(gatheredTree({ entry: CONFIG_WORKER, ...mind.reads })).rejects.toThrow(
    "/repos/config has no commit on main",
  );
});

test("the repos that a module's graph imports from are found by reading: code and files, static and dynamic, but no import that is only a type", () => {
  const folders = importedTopFolders(
    {
      "config/worker.ts": [
        `import type { Thing } from "@repos/types-only/thing.ts";`,
        `import page from "@repos/dash/index.html";`,
        `import { helper } from "./helper.ts";`,
        `export const answer = (thing: Thing) => [page, helper(), thing];`,
      ].join("\n"),
      "config/helper.ts": `export const helper = async () => import("@repos/tasks/app.ts");`,
      // not reached from the entry: what it imports is nobody's business
      "config/unused.ts": `import "@repos/elsewhere/x.ts";`,
    },
    "config/worker.ts",
    "the mind's code",
  );

  expect(folders.map(({ folder }) => folder).sort()).toEqual(["dash", "tasks"]);
  expect(folders).toContainEqual({ folder: "tasks", importedBy: "config/helper.ts" });
});

test("the repos that a package.json names are its dependencies under @repos, each with what its line says of where the repo is", () => {
  const packageJson = JSON.stringify({
    main: "worker.ts",
    dependencies: {
      yjs: "13.6.33",
      "@repos/stdlib": "../stdlib",
      "@repos/words": "https://git.elsewhere.org/repos/words",
    },
  });

  expect(reposNamedIn(packageJson)).toEqual([
    { path: "/repos/stdlib", from: "../stdlib" },
    { path: "/repos/words", from: "https://git.elsewhere.org/repos/words" },
  ]);
  // a repo with no package.json, or one that does not parse, names none: the loader says what is wrong
  expect(reposNamedIn(null)).toEqual([]);
  expect(reposNamedIn("not JSON")).toEqual([]);
});

test("at a birth a named repo comes from beside where the entry repo came from, an address is itself, and with no origin there is nowhere", () => {
  const config = "https://git.example.org/repos/config-template";

  expect(originBeside(config, "../stdlib")).toBe("https://git.example.org/repos/stdlib");
  expect(originBeside(`${config}/`, "../stdlib")).toBe("https://git.example.org/repos/stdlib");
  expect(originBeside(config, "https://git.elsewhere.org/repos/words")).toBe(
    "https://git.elsewhere.org/repos/words",
  );
  // an entry repo that was cloned from nowhere: its named repos are made empty
  expect(originBeside(null, "../stdlib")).toBeNull();
  // what another tool means by a line is not an address
  expect(originBeside(config, "link:../stdlib")).toBeNull();
});

test("the same commits have the same key whatever order they are named in, and another commit has another", () => {
  const key = treeKeyOf({ "/repos/config": "c1", "/repos/stdlib": "s1" });

  expect(treeKeyOf({ "/repos/stdlib": "s1", "/repos/config": "c1" })).toBe(key);
  expect(treeKeyOf({ "/repos/config": "c1", "/repos/stdlib": "s2" })).not.toBe(key);
});

const CONFIG_WORKER = { repo: "/repos/config", module: "worker.ts" };

/** A mind's repos, each with one commit named `<name>@1`, and what a publication read of them. */
function mindWith(repos: Record<string, Record<string, string>>) {
  const read: string[] = [];
  const commitOf = (repoPath: string) => `${repoPath.slice("/repos/".length)}@1`;
  return {
    read,
    reads: {
      commitOf: async (repoPath: string) => (repos[repoPath] ? commitOf(repoPath) : null),
      filesOf: async (repoPath: string, commitOid: string) => {
        expect(commitOid).toBe(commitOf(repoPath));
        read.push(repoPath);
        return repos[repoPath]!;
      },
    },
  };
}