testRecordsTcmWritesFitTheirLexicons

tcmts.dev.lexicons.test.testRecordsTcmWritesFitTheirLexicons · #6wzrs2nx · term

import { releaseRecord } from "#codebase/tcmts/cli/announce";
import type { Codebase } from "#codebase/tcmts/cli/codebase";
import {
  ATTESTATION_KINDS, attestationRecord, lexicons, recordKey, RELEASE_COLLECTION, STAR_COLLECTION, starKey, starRecord,
} from "#codebase/tcmts/records";
import type { Attestation } from "#codebase/tcmts/records";
import type { Causal } from "#codebase/tcmts/store";
import { RecordValidator } from "@atcute/lexicon-doc/validations";

export const testRecordsTcmWritesFitTheirLexicons = async () => {
  type Args = ConstructorParameters<typeof RecordValidator>;
  const docs = Object.fromEntries(lexicons.map((d) => [String(d.id), d])) as unknown as Args[0];
  const fits = (nsid: string, key: string, object: unknown) => {
    try {
      new RecordValidator(docs, nsid as Args[1]).parse({ key, object });
      return true;
    } catch (e) {
      return (e as Error).message;
    }
  };
  const fit = (nsid: string, key: string, object: unknown) => {
    const r = fits(nsid, key, object);
    if (r !== true) throw new Error(`${nsid}/${key}: ${r}\n${JSON.stringify(object)}`);
  };
  // a release, as `tcm release --announce` makes it (a scoped package and a prerelease with build metadata too)
  const cb = {
    store: {
      getCausals: (): Causal[] => [{ namespace: "k4njkepvq7binkbm4yauvbvkz" } as Causal],
      getNamespaces: () => [],
    },
  } as unknown as Codebase;
  for (const [pkg, version] of [
    ["geo", "1.2.0"],
    ["@tcm.codes/records", "0.1.0-beta.1+build.7"],
  ]) {
    const r = await releaseRecord(
      cb,
      {
        package: pkg!,
        version: version!,
        namespace: "",
        causal: "c4hok2jpmykced6b",
        notes: "## Added\n\n- `area`",
        author: "ada",
        ts: 0,
      },
      ["https://tcm.example", "http://127.0.0.1:8080"],
    );
    fit(RELEASE_COLLECTION, recordKey(r), r);
  }
  // each kind of attestation, as `tcm lib attest` writes it (keys are TIDs the PDS makes)
  const subject = {
    namespace: "k4njkepvq7binkbm4yauvbvkz",
    release: "geo@1.2.0",
    author: "did:plc:vh4czvu5aijkaqge5huayq53",
  };
  const as: Attestation[] = [
    { kind: "review", subject, body: "", createdAt: new Date(0).toISOString() },
    {
      kind: "review",
      subject: { namespace: subject.namespace },
      body: "Read it all.",
      createdAt: new Date(0).toISOString(),
    },
    {
      kind: "advisory",
      subject,
      body: "area is wrong",
      severity: "high",
      fixedIn: "1.2.1",
      createdAt: new Date(0).toISOString(),
    },
    { kind: "yank", subject, body: "use 1.2.1", createdAt: new Date(0).toISOString() },
  ];
  for (const a of as) fit(ATTESTATION_KINDS[a.kind], "3m6bkzurm4c7w", attestationRecord(a));
  // a star, as the hub's button writes it
  const author = "did:plc:vh4czvu5aijkaqge5huayq53";
  fit(STAR_COLLECTION, starKey(author, "@tcm.codes/react"), starRecord(author, "@tcm.codes/react"));
  // and what doesn't fit is refused: the validator checks what the lexicons say
  const bad: [string, string, unknown][] = [
    [
      ATTESTATION_KINDS.advisory,
      "3m6bkzurm4c7w",
      attestationRecord({ ...as[2]!, severity: undefined }),
    ],
    [ATTESTATION_KINDS.review, "not-a-tid", attestationRecord(as[0]!)],
    [STAR_COLLECTION, "x", starRecord("alice", "geo")],
    [RELEASE_COLLECTION, "geo:1.2.0", { $type: RELEASE_COLLECTION, package: "geo" }],
  ];
  for (const [nsid, key, object] of bad)
    if (fits(nsid, key, object) === true)
      throw new Error(`${nsid}/${key} fit: ${JSON.stringify(object)}`);
}

uses