iterate.com / kernel

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

kernel / core / mind-worker / src / context / module-resolution.test.tsmain at 949894f

import { expect, test } from "vitest";
import { resolveModules, type ResolveOptions } from "./module-resolution.ts";

// A folder import loads every file under the folder as text, code too (a page's). A file there can
// also be imported as code by another module: both must be loaded, in either order.
test.for([
  {
    order: "the folder first",
    worker:
      'import files from "./lib/";\nimport { x } from "./lib/x.ts";\nexport default { files, x };\n',
  },
  {
    order: "the code first",
    worker:
      'import { x } from "./lib/x.ts";\nimport files from "./lib/";\nexport default { files, x };\n',
  },
])(
  "a file imported as code and as part of a folder is both a module and a text ($order)",
  async ({ worker }) => {
    const { modules, files } = await resolveModules(sourceWith(worker), options());
    expect(modules).toMatchObject({
      "lib/x.js": expect.stringContaining("export const x = 1"),
      "lib/x.ts.file.js": `export default ${JSON.stringify("export const x: number = 1;\n")};\n`,
      "worker.js": expect.stringContaining('from "./lib/x.js"'),
    });
    expect(Object.keys(files).sort()).toEqual(["lib/x.ts", "package.json", "worker.ts"]);
  },
);

test("a file of another repo is imported by the repo's name, as code, as a file or as a folder, and is no npm package though package.json lists it", async () => {
  const tree = {
    "package.json": JSON.stringify({ main: "config/worker.ts" }),
    "config/worker.ts": [
      'import { init } from "@repos/stdlib/init.ts";',
      'import theme from "@repos/stdlib/theme.css";',
      'import files from "@repos/dash/public/";',
      "export default { init, theme, files };",
    ].join("\n"),
    "stdlib/init.ts": 'import { step } from "./step.ts";\nexport const init = step;\n',
    "stdlib/step.ts": "export const step = () => {};\n",
    "stdlib/theme.css": ":root { color-scheme: light; }\n",
    "dash/public/app.js": "console.log('the Dash');\n",
  };

  const { modules } = await resolveModules(tree, options());

  expect(modules).toMatchObject({
    "config/worker.js": expect.stringContaining('from "../stdlib/init.js"'),
    "stdlib/init.js": expect.stringContaining('from "./step.js"'),
    "stdlib/theme.css.file.js": expect.stringContaining("color-scheme: light"),
    "dash/public.files.js": expect.stringContaining('"app.js"'),
  });
});

test("a name under @repos with no such file says so, and one that climbs out of its repo is refused", async () => {
  const worker = (specifier: string) => ({
    "package.json": JSON.stringify({ main: "config/worker.ts" }),
    "config/worker.ts": `import x from "${specifier}";\nexport default x;\n`,
  });

  await expect(resolveModules(worker("@repos/stdlib/init.ts"), options())).rejects.toThrow(
    "config/worker.ts imports @repos/stdlib/init.ts, and there is no such file",
  );
  await expect(resolveModules(worker("@repos/stdlib/../../x.ts"), options())).rejects.toThrow(
    "config/worker.ts imports @repos/stdlib/../../x.ts, which is no path of a repo",
  );
});

test("a main module named from the entry repo's root loads that repo's file, in a tree of repos", async () => {
  const tree = {
    "package.json": JSON.stringify({ main: "config/worker.ts" }),
    "config/worker.ts": "export default {};\n",
    "config/apps/agents/backend.ts": 'import { step } from "@repos/stdlib/step.ts";\nexport const run = step;\n',
    "stdlib/step.ts": "export const step = () => {};\n",
    "stdlib/agents.ts": "export const agents = 1;\n",
    "config/agents.ts": "export const agents = 2;\n",
  };

  const byRootName = await resolveModules(tree, { ...options(), mainModule: "apps/agents/backend.ts" });
  const byTreeName = await resolveModules(tree, {
    ...options(),
    mainModule: "config/apps/agents/backend.ts",
  });

  expect(byRootName).toMatchObject({
    mainModule: "config/apps/agents/backend.js",
    modules: { "config/apps/agents/backend.js": expect.stringContaining('from "../../../stdlib/step.js"') },
  });
  expect(byRootName.modules).toEqual(byTreeName.modules);
  // a name that two repos have, with no folder, is the entry repo's
  expect(await resolveModules(tree, { ...options(), mainModule: "agents.ts" })).toMatchObject({
    mainModule: "config/agents.js",
  });
  // and with another repo's folder it is that repo's
  expect(await resolveModules(tree, { ...options(), mainModule: "stdlib/agents.ts" })).toMatchObject({
    mainModule: "stdlib/agents.js",
  });
  await expect(resolveModules(tree, { ...options(), mainModule: "apps/none.ts" })).rejects.toThrow(
    'no module "apps/none.ts" to load as the main module',
  );
});

/** A config whose worker.ts is `worker`, beside one code file in a folder. */
function sourceWith(worker: string) {
  return {
    "package.json": JSON.stringify({ main: "worker.ts" }),
    "worker.ts": worker,
    "lib/x.ts": "export const x: number = 1;\n",
  };
}

/** No platform module, no npm package, no network: the resolution of relative imports alone. */
function options(): ResolveOptions {
  return {
    platform: { modules: {}, imports: {} },
    store: { get: async () => null, put: async () => {} },
    fetch: () => Promise.reject(new Error("no network in this test")),
    where: "test",
  };
}