iterate.com / stdlib

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

stdlib / tests / context-descriptions.test.mjsmain at 41005ca

// tests/context-descriptions.test.mjs — which title and line the table gives a path, the keys
// that make each land once, and when the module appends (../context-descriptions.ts). The root
// here records what it is asked.
// Run from the repo's root: node --test "tests/*.test.mjs"
import assert from "node:assert/strict";
import { test } from "node:test";
import {
  contextDescriptionsEvent,
  contextLine,
  descriptionKey,
} from "../context-descriptions.ts";

const SECRET_LINE =
  "The secret openai: its value stays in the vault, and this log records when it was set, refreshed and used.";

for (const { name, path, description } of [
  {
    name: "the config repo: its own row, before the row of every repo",
    path: "/repos/config",
    description:
      "This mind's wiring: worker.ts, the module the mind loads first. Its AGENTS.md says how the repos fit together.",
  },
  { name: "another repo", path: "/repos/site", description: "The git repository site." },
  { name: "a secret says its name", path: "/secrets/openai", description: SECRET_LINE },
  {
    name: "the mail's log",
    path: "/integrations/email",
    description: "The mind's email: each mail it received and sent, folded into threads.",
  },
  { name: "an agent: no row, its title is its line", path: "/agents/helper", description: undefined },
  { name: "a secret's child: no row", path: "/secrets/openai/more", description: undefined },
  { name: "a path no row names", path: "/elsewhere", description: undefined },
])
  test(`the line the table gives: ${name}`, () => {
    assert.equal(contextLine(path)?.description, description);
  });

for (const { name, path, title } of [
  { name: "the root", path: "/", title: "Mind root" },
  { name: "the config repo, before the row of every repo", path: "/repos/config", title: "Config repo" },
  { name: "another repo says its name", path: "/repos/site", title: "Repo site" },
  { name: "a secret says its name", path: "/secrets/openai", title: "Secret openai" },
  { name: "a repo's task feed says the repo", path: "/task-files/task-board", title: "Task changes of task-board" },
  { name: "the open docs of a repo say the repo", path: "/docs/config", title: "Open docs of config" },
  { name: "an agent: no row, its title is its own", path: "/agents/helper", title: undefined },
])
  test(`the title the table gives: ${name}`, () => {
    assert.equal(contextLine(path)?.title, title);
  });

test("every row has a short title: at most 40 characters, and no full stop", () => {
  for (const path of [
    "/",
    "/agents",
    "/agents/email",
    "/repos/config",
    "/repos/stdlib",
    "/repos/task-board",
    "/repos/kernel",
    "/secrets",
    "/integrations/email",
    "/approvals",
  ]) {
    const title = contextLine(path)?.title;
    assert.ok(title, path);
    assert.ok(title.length <= 40, path);
    assert.doesNotMatch(title, /\.$/, path);
  }
});

test("the key names the path and the text: the same text lands once, a changed text once more", () => {
  const key = descriptionKey("described", "/secrets/openai", "One text.");
  assert.match(key, /^config\/described:\/secrets\/openai:[0-9a-z]+$/);
  assert.equal(descriptionKey("described", "/secrets/openai", "One text."), key);
  assert.equal(
    [
      descriptionKey("described", "/secrets/openai", "Another text."),
      descriptionKey("described", "/secrets/github", "One text."),
      // a title has a key of its own, so the same words as a description still land
      descriptionKey("titled", "/secrets/openai", "One text."),
    ].includes(key),
    false,
  );
});

for (const { name, event, listed, appended, listCalls } of [
  {
    name: "a context born that a row names: its line, and its title beside the metadata it has by then",
    event: { type: "events.iterate.com/itx/created", path: "/secrets/openai" },
    listed: [{ path: "/secrets/openai", eventCount: 2, metadata: { setAtBirth: "by its maker" } }],
    appended: [
      ["/secrets/openai", "description"],
      ["/secrets/openai", "title"],
    ],
    listCalls: 1,
  },
  {
    name: "a context born that the catalog does not list yet: its line, and no title that could erase metadata",
    event: { type: "events.iterate.com/itx/created", path: "/secrets/openai" },
    listed: [{ path: "/secrets/openai/child" }],
    appended: [["/secrets/openai", "description"]],
    listCalls: 1,
  },
  {
    name: "a context born whose row has the title already: its line alone",
    event: { type: "events.iterate.com/itx/created", path: "/secrets/openai" },
    listed: [{ path: "/secrets/openai", eventCount: 2, metadata: { title: "Secret openai" } }],
    appended: [["/secrets/openai", "description"]],
    listCalls: 1,
  },
  {
    name: "metadata that a title would push over the catalog's 4,000 characters: no title, at birth",
    event: { type: "events.iterate.com/itx/created", path: "/secrets/openai" },
    listed: [{ path: "/secrets/openai", eventCount: 2, metadata: { notes: "n".repeat(3_980) } }],
    appended: [["/secrets/openai", "description"]],
    listCalls: 1,
  },
  {
    name: "a context born that no row names: no call",
    event: { type: "events.iterate.com/itx/created", path: "/agents/helper" },
    listed: [],
    appended: [],
    listCalls: 0,
  },
  {
    name: "a publish: each listed context a row names, for the line and the title it does not say yet",
    event: { type: "events.iterate.com/mind/worker-updated", path: "/" },
    listed: [
      { path: "/" },
      { path: "/agents/helper" },
      // says both already: no call
      { path: "/repos/site", description: "The git repository site.", metadata: { title: "Repo site" } },
      // the line is a person's, the title is the table's already: the line alone
      { path: "/secrets/openai", description: "A person's own words.", metadata: { title: "Secret openai" } },
      // the line is the table's, the title is missing: the title alone, beside what is there
      { path: "/secrets/github", description: SECRET_LINE.replace("openai", "github"), metadata: { kept: true } },
    ],
    appended: [
      ["/", "description"],
      ["/", "title"],
      ["/secrets/openai", "description"],
      ["/secrets/github", "title"],
    ],
    listCalls: 1,
  },
  {
    name: "any other event, an itx/described among them: no call at all",
    event: { type: "events.iterate.com/itx/described", path: "/secrets/openai" },
    listed: [{ path: "/" }],
    appended: [],
    listCalls: 0,
  },
])
  test(`when the module appends: ${name}`, async () => {
    const mind = recordingMind(listed);
    await contextDescriptionsEvent(mind.itx, event);
    assert.equal(mind.calls.listCalls, listCalls);
    assert.deepEqual(
      mind.calls.appended,
      appended.map(([path, what]) => {
        const line = contextLine(path);
        return {
          path,
          event:
            what === "description"
              ? {
                  type: "events.iterate.com/itx/described",
                  idempotencyKey: descriptionKey("described", path, line.description),
                  payload: { description: line.description },
                }
              : {
                  type: "events.iterate.com/itx/described",
                  idempotencyKey: descriptionKey("titled", path, line.title),
                  // the title goes in beside what the context's metadata holds
                  payload: {
                    metadata: {
                      ...listed.find((context) => context.path === path)?.metadata,
                      title: line.title,
                    },
                  },
                },
        };
      }),
    );
  });

test("an append that fails skips that one context: the others get theirs", async () => {
  const mind = recordingMind([{ path: "/repos/site" }, { path: "/secrets/openai" }, { path: "/" }]);
  const cd = mind.itx.cd;
  mind.itx.cd = (path) =>
    path === "/secrets/openai"
      ? {
          append: async () => {
            throw new Error("the mind refused this append");
          },
        }
      : cd(path);
  await contextDescriptionsEvent(mind.itx, {
    type: "events.iterate.com/mind/worker-updated",
    path: "/",
  });
  // both writes of the two other contexts landed
  assert.deepEqual(mind.calls.appended.map(({ path }) => path).toSorted(), [
    "/",
    "/",
    "/repos/site",
    "/repos/site",
  ]);
});

/** A root whose catalog lists `listed`, and that records each list and each append. */
function recordingMind(listed) {
  const calls = { listCalls: 0, appended: [] };
  const itx = {
    cd: (path) => ({
      append: async (event) => {
        calls.appended.push({ path, event });
      },
    }),
    contexts: {
      list: async () => {
        calls.listCalls += 1;
        return listed;
      },
    },
  };
  return { itx, calls };
}