testCodeMapGroupsDefinitionsIntoClouds

tcmts.share.test.testCodeMapGroupsDefinitionsIntoClouds · #5k2hhmno · function

the code map: a dot for each definition (one for each hash; tests and files left out), the references between them, clouds named for their namespaces (the root left off), each dot in its cloud, and the same map each time

import { codeMapOf } from "#codebase/tcmts/share";
import type { CodeMap } from "#codebase/tcmts/share";
import type { NameRow } from "#codebase/tcmts/store";
import assert from "node:assert/strict";

export const testCodeMapGroupsDefinitionsIntoClouds = () => {
  const rows: NameRow[] = [];
  const uses = new Map<string, string[]>();
  const add = (path: string, kind: NameRow["kind"], used: string[] = []) => {
    rows.push({ path, hash: path, kind });
    for (const u of used) uses.set(u, [...(uses.get(u) ?? []), path]);
  };
  // two groups, each using its own (every one what's before it): app.ui.* and app.db.*, with one reference across
  const before = (p: string, i: number) => Array.from({ length: i }, (_, k) => `${p}${k}`);
  for (let i = 0; i < 6; i++)
    add(`app.ui.v${i}`, "term", [...before("app.ui.v", i), "app.ui.Props"]);
  add("app.ui.Props", "type");
  for (let i = 0; i < 6; i++) add(`app.db.q${i}`, "term", [...before("app.db.q", i), "app.db.Row"]);
  add("app.db.Row", "type");
  add("app.ui.v5b", "term", ["app.ui.v4", "app.ui.v5", "app.db.q5"]);
  add("app.db.testQueries", "term", ["app.db.q0", "app.db.fixture"]);
  // (a test's helpers, used by each other and the test only; and API nothing here uses)
  add("app.db.fixture", "term", ["app.db.seed"]);
  add("app.db.seed", "term", ["app.db.fixture"]);
  add("app.ui.unused", "term");
  add("app.docs.intro", "file");
  rows.push({ path: "app.ui.alias", hash: "app.ui.v0", kind: "term" });
  add("app.manifest", "term");
  const make = () => {
    const steps = codeMapOf(
      rows,
      (h) => uses.get(h) ?? [],
      (h) => (h.startsWith("app.ui") ? "browser" : "server"),
      "app",
      // (a manifest's strings: a definition, and a namespace, which is everything in it)
      [
        ["app.manifest", "app.ui.v5"],
        ["app.manifest", "app.db"],
        ["app.manifest", "app.nowhere"],
      ],
    );
    for (let next = steps.next(); ; next = steps.next()) if (next.done) return next.value;
  };
  const map: CodeMap = make();
  assert.equal(
    map.dots.length,
    17,
    "tests, what only tests use, files and second names aren't dots",
  );
  assert.ok(!map.dots.some((d) => d.path === "app.db.fixture" || d.path === "app.db.seed"));
  assert.deepEqual(
    map.dots.filter((d) => d.type).map((d) => [d.path, d.place]),
    [
      ["app.ui.Props", ""],
      ["app.db.Row", ""],
    ],
  );
  assert.equal(map.dots.find((d) => d.path === "app.ui.v1")!.place, "browser");
  const ref = (a: string, b: string) => {
    const i = map.dots.findIndex((d) => d.path === a);
    const j = map.dots.findIndex((d) => d.path === b);
    return map.refs.some((_, k) => k % 2 === 0 && map.refs[k] === i && map.refs[k + 1] === j);
  };
  assert.ok(
    ref("app.ui.v5b", "app.db.q5") && !ref("app.db.q5", "app.ui.v5b"),
    "a reference goes from user to used",
  );
  // (to a namespace: the three most used in it)
  assert.ok(
    ref("app.manifest", "app.ui.v5") &&
      ref("app.manifest", "app.db.Row") &&
      ref("app.manifest", "app.db.q0"),
  );
  assert.ok(!ref("app.manifest", "app.db.q5"));
  assert.equal(
    map.refs.length,
    2 * 2 * (15 + 6) + 2 * 3 + 2 * (1 + 3),
    "and a manifest's, to a name, and to a namespace",
  );
  // (the manifest, directly in the root, a cloud of the root's)
  assert.deepEqual(map.namespaces.clouds.map((c) => c.label).sort(), ["app", "db", "ui"]);
  assert.deepEqual(
    map.clusters.clouds.map((c) => c.label).sort(),
    ["db", "ui"],
    "the communities are the two groups",
  );
  const unused = map.dots.findIndex((d) => d.path === "app.ui.unused");
  assert.equal(
    map.clusters.clouds[map.clusters.cloud[unused]!]!.label,
    "ui",
    "what nothing uses is in its namespace's cloud, not loose",
  );
  assert.equal(map.clusters.links.length, 1, "with references between them");
  for (const layout of [map.clusters, map.namespaces]) {
    for (const [j] of map.dots.entries()) {
      const c = layout.clouds[layout.cloud[j]!]!;
      assert.ok(
        Math.hypot(layout.x[j]! - c.x, layout.y[j]! - c.y) <= c.r + 20,
        `${map.dots[j]!.path} is in its cloud`,
      );
    }
    const [a, b] = layout.clouds;
    assert.ok(Math.hypot(a!.x - b!.x, a!.y - b!.y) >= a!.r + b!.r, "clouds don't overlap");
  }
  assert.deepEqual(make(), map, "a head's map is the same each time it's made");
}

uses