testRegistrySearchRanksByMatch

tcmts.registry.test.testRegistrySearchRanksByMatch · #2snldx7m · term

The registry's search scores names as their library shows them (not by where the registry mounts it: every name under ….react.v0_1_0 holds "react"), puts the library with the best match first, leaves tests and their helpers out, and says where each match runs.

import { releaseNamespace } from "#codebase/tcmts/cli/libraries";
import { save } from "#codebase/tcmts/cli/main";
import { matchLibraries } from "#codebase/tcmts/registry";
import type { Library, RegistryRelease } from "#codebase/tcmts/registry";
import { codebaseHolding } from "#codebase/tcmts/registry/test";
import { shareCache } from "#codebase/tcmts/share";
import assert from "node:assert/strict";
import fs from "node:fs";
import path_1 from "node:path";

export const testRegistrySearchRanksByMatch = async () => {
  const atproto = "plc_a.atproto.v0_1_0";
  const react = "plc_a.react.v0_1_0";
  const { cb, dir } = codebaseHolding(
    "search",
    `// tcm: namespace ${atproto}\nexport const refreshAccessToken = (t: string) => t;\nexport const square = (n: number) => n * n;\nexport const identity = <T,>(x: T): T => x;\nexport const now = () => Date.now();\n`,
    atproto,
  );
  const add = (file: string, text: string) => {
    fs.writeFileSync(path_1.join(dir, file), text);
    save(cb, path_1.join(dir, file), "add");
  };
  add(
    "tests.ts",
    `// tcm: namespace ${atproto}.test\nexport const testReactRefresh = () => {};\nexport const withReact = (f: () => void) => f();\n`,
  );
  add(
    "react.ts",
    `// tcm: namespace ${react}\n/** Pages, routed. */\nexport const Pages = (routes: string[]) => routes.length;\nexport const href = (p: string) => p;\n`,
  );
  const library = async (mount: string, pkg: string): Promise<Library> => {
    const namespace = await releaseNamespace(cb.store, {
      package: pkg,
      version: "now",
      namespace: mount,
      causal: cb.store.head(),
    });
    const latest = {
      did: "did:plc:a",
      package: pkg,
      version: "0.1.0",
      state: "mirrored",
      mount,
      namespace,
    } as RegistryRelease;
    return {
      did: "did:plc:a",
      package: pkg,
      latest,
      versions: [latest],
      yanked: [],
      dependents: 0,
    };
  };
  const shown = [await library(atproto, "atproto"), await library(react, "react")];
  const s = { cb, cache: shareCache() };

  const found = matchLibraries(s, shown, "react");
  // (react's names hold the word; atproto's refreshAccessToken only its letters, in order)
  assert.deepEqual(
    found.map((g) => [g.l.package, g.hits.map((h) => h.name)]),
    [
      ["react", ["react.Pages", "react.href"]],
      ["atproto", ["atproto.refreshAccessToken"]],
    ],
  );
  assert.equal(found[0]!.hits[0]!.place, "anywhere");
  assert.equal(found[0]!.hits[0]!.detail, "Pages, routed.");

  assert.deepEqual(
    matchLibraries(s, shown, "square").map((g) => g.hits.map((h) => h.name)),
    [["atproto.square"]],
  );
  // by type, a more general one is a close match; by docs, docs alone; and what reaches outside is said
  assert.deepEqual(
    matchLibraries(s, shown, ":(n: number) => number").flatMap((g) =>
      g.hits.map((h) => [h.name, !!h.close]),
    ),
    [
      ["atproto.square", false],
      ["atproto.identity", true],
    ],
  );
  assert.deepEqual(
    matchLibraries(s, shown, "routed", "docs").flatMap((g) => g.hits.map((h) => h.name)),
    ["react.Pages"],
  );
  assert.deepEqual(matchLibraries(s, shown, "href", "docs"), []);
  assert.deepEqual(
    matchLibraries(s, shown, "now").flatMap((g) => g.hits.map((h) => [h.name, !!h.reaches])),
    [["atproto.now", true]],
  );
  assert.equal(matchLibraries(s, shown, "square")[0]!.hits[0]!.reaches, undefined);
}

uses