Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go
4.2 KB
Raw Blame
import { readFileSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import {
  EMBED_TOKENS,
  EMBED_TOKEN_GROUPS,
  EMBED_TOKEN_NAMES,
  THEME_VARIABLES,
  embedTokenDeclarations,
  isPlainEmbedValue,
} from "@multica/core/appearance";

/**
 * EMBED_TOKENS is the public contract for plugin surfaces and HTML blocks.
 * This keeps it resolvable and its reference in docs/theming.md generated.
 * Run with UPDATE_THEME_DOCS=1 to rewrite the table.
 */

const REPO = join(__dirname, "../../..");
const read = (path: string) => readFileSync(join(REPO, path), "utf8");
const foundationCss = ["tokens.css", "appearance.css"].map((f) => read(`packages/ui/styles/${f}`)).join("\n");

const START = "<!-- embed-tokens:start (generated by packages/views/common/embed-tokens.test.ts) -->";
const END = "<!-- embed-tokens:end -->";

const KIND_TEXT = { color: "colour", length: "length (px)", font: "font stack", keyword: "keyword" } as const;

function referenceTables(): string {
  const parts: string[] = [];
  for (const [key, title] of Object.entries(EMBED_TOKEN_GROUPS)) {
    parts.push(`### ${title}`, "", "| Token | Kind | Use |", "| --- | --- | --- |");
    for (const token of EMBED_TOKENS.filter((t) => t.group === key)) {
      parts.push(`| \`${token.name}\` | ${KIND_TEXT[token.kind]} | ${token.description.replace(/\|/g, "\\|")} |`);
    }
    parts.push("");
  }
  return parts.join("\n");
}

describe("embed tokens", () => {
  it("has no duplicates", () => {
    expect(EMBED_TOKEN_NAMES.size).toBe(EMBED_TOKENS.length);
  });

  it("keeps every token the plugin surfaces and HTML blocks had before", () => {
    const before = [
      "--background", "--foreground", "--surface", "--muted", "--muted-foreground", "--border", "--primary",
      "--primary-foreground", "--secondary", "--secondary-foreground", "--accent", "--accent-foreground",
      "--destructive", "--success", "--warning", "--info", "--brand", "--chart-1", "--chart-2", "--chart-3",
      "--chart-4", "--chart-5", "--radius", "--text-caption", "--text-body", "--font-sans",
    ];
    expect(before.filter((name) => !EMBED_TOKEN_NAMES.has(name))).toEqual([]);
  });

  it("reads each token from something the app declares or a fallback", () => {
    const unresolvable = EMBED_TOKENS.filter((t) => t.kind !== "keyword" && !t.source)
      .filter((t) => !new RegExp(`${t.name}:\\s*[^;]+;`).test(foundationCss))
      .map((t) => t.name);
    expect(unresolvable).toEqual([]);
  });

  it("reads theme variables with the app's own fallbacks", () => {
    for (const token of EMBED_TOKENS.filter((t) => t.group === "status" || t.group === "code")) {
      const variable = THEME_VARIABLES.find((v) => `--${v.name}` === token.name)!;
      expect(variable, token.name).toBeDefined();
      if (variable.default) expect(token.source).toBe(`var(${token.name}, ${variable.default})`);
      if (variable.darkDefault) expect(token.darkSource).toBe(`var(${token.name}, ${variable.darkDefault})`);
    }
  });

  it("passes only plain values", () => {
    for (const value of ["oklch(0.5 0.1 200)", "rgb(1 2 3 / 50%)", "12px", '"Inter Variable", system-ui, sans-serif', "dark"]) {
      expect(isPlainEmbedValue(value), value).toBe(true);
    }
    for (const value of ["url(x.png)", "URL (x)", "image-set(a 1x)", "var(--x)", "red;}", "a</style>", "\\61", "/* */", "", "x".repeat(600)]) {
      expect(isPlainEmbedValue(value), value).toBe(false);
    }
    expect(embedTokenDeclarations({ "--foreground": "red", "--evil": "blue", "--muted": "url(x)" })).toBe("--foreground:red");
  });

  it("keeps the reference in docs/theming.md generated", () => {
    const docs = read("docs/theming.md");
    const start = docs.indexOf(START);
    const end = docs.indexOf(END);
    expect(start, "docs/theming.md is missing the generated embed token markers").toBeGreaterThanOrEqual(0);
    const expected = `${START}\n\n${referenceTables()}\n${END}`;
    const current = docs.slice(start, end + END.length);
    if (process.env.UPDATE_THEME_DOCS && current !== expected) {
      writeFileSync(join(REPO, "docs/theming.md"), docs.slice(0, start) + expected + docs.slice(end + END.length));
      return;
    }
    expect(current).toBe(expected);
  });
});