testStandardSiteRecords

tcmts.content.test.testStandardSiteRecords · #ejkw7cwx · function

import { standardSite } from "#codebase/tcmts/content";
import { expect } from "vitest";

export const testStandardSiteRecords = () => {
  const site = standardSite({
    did: "did:plc:abc",
    url: "https://example.com/blog/",
    name: "A blog",
    description: "Writing.",
    posts: [
      {
        name: "hello",
        path: "hello.html",
        title: "Hello",
        publishedAt: "2026-11-07",
        description: "The first.",
        tags: ["tcm"],
        markdown:
          "# Hello\n\nSee {@link lib.square} and [the docs](/docs/a.html) or [b](b.html).\n",
      },
    ],
  });
  // keys are TIDs, as the lexicons ask, the same each time: a post's from its day, so they sort by it
  const TID = /^[234567abcdefghij][234567abcdefghijklmnopqrstuvwxyz]{12}$/;
  const pub = site.publication.rkey;
  const key = site.documents[0]!.rkey;
  expect(pub).toMatch(TID);
  expect(key).toMatch(TID);
  const again = standardSite({
    did: "did:plc:abc",
    url: "https://example.com/blog",
    name: "Renamed",
    posts: [
      { name: "hello", path: "/x", title: "Hello", publishedAt: "2026-11-07" },
      { name: "later", path: "/y", title: "Later", publishedAt: "2026-11-08" },
    ],
  });
  expect(again.publication.rkey).toBe(pub);
  expect(again.documents[0]!.rkey).toBe(key);
  expect(again.documents[1]!.rkey > key).toBe(true);
  expect(site.publication).toEqual({
    collection: "site.standard.publication",
    rkey: pub,
    uri: `at://did:plc:abc/site.standard.publication/${pub}`,
    record: {
      $type: "site.standard.publication",
      url: "https://example.com/blog",
      name: "A blog",
      description: "Writing.",
    },
  });
  const doc = site.documents[0]!;
  expect(doc.uri).toBe(`at://did:plc:abc/site.standard.document/${key}`);
  expect(doc.record).toEqual({
    $type: "site.standard.document",
    site: `at://did:plc:abc/site.standard.publication/${pub}`,
    path: "/hello.html",
    title: "Hello",
    publishedAt: "2026-11-07T00:00:00.000Z",
    description: "The first.",
    tags: ["tcm"],
    textContent: "Hello\nSee square and the docs or b.",
    content: {
      $type: "at.markpub.markdown",
      text: {
        $type: "at.markpub.text",
        markdown:
          "# Hello\n\nSee `square` and [the docs](https://example.com/docs/a.html) or [b](https://example.com/blog/b.html).\n",
      },
      flavor: "gfm",
      renderingRules: "marked",
    },
  });
  expect(site.wellKnown).toEqual({
    path: "/.well-known/site.standard.publication/blog",
    text: `at://did:plc:abc/site.standard.publication/${pub}`,
  });
  expect(site.link("hello")).toBe(
    `<link rel="site.standard.document" href="at://did:plc:abc/site.standard.document/${key}">`,
  );
  // a publication at a domain's root
  expect(
    standardSite({ did: "did:plc:abc", url: "https://blog.example", name: "B", posts: [] })
      .wellKnown.path,
  ).toBe("/.well-known/site.standard.publication");
  expect(() =>
    standardSite({ did: "nope", url: "https://x.example", name: "B", posts: [] }),
  ).toThrow(/isn't a DID/);
}

uses