import { describe, expect, it } from "vitest";
import { renderDocument, renderMarkup } from "$lib/markup/render";
import type { MarkupContext } from "$lib/markup/paths";
const ctx: MarkupContext = { repo: "ada.test/infra", ref: "main", host: "tangled.org" };
const keyOf = (html: string | null): string => {
const key = /^
/.exec(html ?? "")?.[1];
if (!key) throw new Error(`no markup key in ${html}`);
return key;
};
describe("renderDocument", () => {
it("wraps what it renders under a key", async () => {
const html = await renderDocument("README.md", "# hello", ctx);
expect(keyOf(html)).toMatch(/^[0-9a-z]+\.[0-9a-z]+\.[0-9a-z]+$/);
expect(html).toContain("
{
expect(await renderDocument("notes.txt", "# hello", ctx)).toBeNull();
});
it("keys the same contents rendered elsewhere apart", async () => {
const here = keyOf(await renderDocument("README.md", "see [x](y)", ctx));
const elsewhere = keyOf(
await renderDocument("README.md", "see [x](y)", { ...ctx, ref: "dev" })
);
const copied = keyOf(await renderDocument("docs/README.md", "see [x](y)", ctx));
expect(elsewhere).not.toBe(here);
expect(copied).not.toBe(here);
});
it("keys different contents apart", async () => {
expect(keyOf(await renderDocument("README.md", "# a", ctx))).not.toBe(
keyOf(await renderDocument("README.md", "# b", ctx))
);
});
it("gives the same contents the same key every time", async () => {
expect(keyOf(await renderDocument("README.md", "# a", ctx))).toBe(
keyOf(await renderDocument("README.md", "# a", ctx))
);
});
});
describe("renderMarkup", () => {
// a body and a file with the same contents need different keys
it("wraps a body under a key of its own", async () => {
const html = await renderMarkup("a **body**", ctx);
expect(html).toContain("body");
expect(keyOf(html)).not.toBe(keyOf(await renderDocument("README.md", "a **body**", ctx)));
});
});