Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.
Go
import { readFileSync, readdirSync, statSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import {
THEME_VARIABLES,
THEME_VARIABLE_NAMES,
THEME_VARIABLE_SECTIONS,
type ThemeVariable,
} from "@multica/core/appearance";
/**
* THEME_VARIABLES is the public contract for themes. This keeps it honest:
* each variable is read where the registry says, with the default it
* documents, and the reference table in docs/theming.md is generated from
* it. Run with UPDATE_THEME_DOCS=1 to rewrite the table.
*/
const REPO = join(__dirname, "../../..");
const read = (path: string) => readFileSync(join(REPO, path), "utf8");
/** Stylesheets whose fallbacks are public variables. */
const THEMED_CSS = [
"packages/ui/styles/theme-variables.css",
"packages/views/editor/styles/prose.css",
"packages/views/editor/styles/code.css",
];
/** Fallbacks in those files that belong to the app, not to themes. */
const INTERNAL = new Set(["content-scale", "font-scale", "code-gutter-bg", "line-digits", "font-ui", "font-cjk"]);
function sourceFiles(dir: string, out: string[] = []): string[] {
let entries: string[];
try {
entries = readdirSync(dir);
} catch {
return out;
}
for (const entry of entries) {
if (entry === "node_modules" || entry.startsWith(".")) continue;
const path = join(dir, entry);
if (statSync(path).isDirectory()) sourceFiles(path, out);
else if (/\.(tsx?|css)$/.test(entry) && !/\.test\.tsx?$/.test(entry)) out.push(path);
}
return out;
}
const squash = (value: string) => value.replace(/\s+/g, "");
const themedCss = THEMED_CSS.map(read).join("\n");
const sources = ["packages/views", "packages/ui", "packages/core/issues", "apps/desktop/src/renderer/src"]
.flatMap((root) => sourceFiles(join(REPO, root)))
.map((file) => readFileSync(file, "utf8"))
.join("\n");
const corpus = squash(sources);
const foundationCss = ["tokens.css", "appearance.css"].map((f) => read(`packages/ui/styles/${f}`)).join("\n");
function block(css: string, selector: string): string {
const start = css.indexOf(`\n${selector} {`);
if (start < 0) return "";
return css.slice(start, css.indexOf("\n}", start));
}
const LIGHT = block(foundationCss, ":root") + block(foundationCss, "@theme");
const DARK = block(foundationCss, ".dark");
function declared(css: string, name: string): string | undefined {
const match = new RegExp(`--${name}:\\s*([^;]+);`).exec(css);
return match?.[1]?.replace(/\s+/g, " ").trim();
}
function defaults(variable: ThemeVariable): string {
if (variable.defaultText) return variable.defaultText;
const light = variable.default ?? declared(LIGHT, variable.name);
const dark = variable.darkDefault ?? (variable.default === undefined ? declared(DARK, variable.name) : undefined);
if (light === undefined) return "unset";
return dark && dark !== light ? `\`${light}\` / \`${dark}\`` : `\`${light}\``;
}
const escape = (text: string) => text.replace(/\|/g, "\\|");
function referenceTables(): string {
const parts: string[] = [];
for (const [key, title] of Object.entries(THEME_VARIABLE_SECTIONS)) {
const rows = THEME_VARIABLES.filter((v) => v.section === key);
parts.push(`### ${title}`, "", "| Variable | Default (light / dark) | Use |", "| --- | --- | --- |");
for (const v of rows) parts.push(`| \`--${v.name}\` | ${escape(defaults(v))} | ${escape(v.description)} |`);
parts.push("");
}
return parts.join("\n");
}
const START = "<!-- theme-variables:start (generated by packages/views/common/theme-variables.test.ts) -->";
const END = "<!-- theme-variables:end -->";
describe("theme variables", () => {
it("has no duplicates", () => {
expect(THEME_VARIABLE_NAMES.size).toBe(THEME_VARIABLES.length);
});
it("reads every component variable with its documented default", () => {
const missing = THEME_VARIABLES.flatMap((v) =>
[v.default, v.darkDefault]
.filter((def): def is string => def !== undefined)
.filter((def) => !corpus.includes(squash(`var(--${v.name},${def})`)))
.map((def) => `--${v.name}: ${def}`),
);
expect(missing).toEqual([]);
});
it("declares every foundation token", () => {
const missing = THEME_VARIABLES.filter((v) => v.default === undefined && !v.defaultText)
.filter((v) => !declared(foundationCss, v.name) && !sources.includes(`--${v.name}`))
.map((v) => v.name);
expect(missing).toEqual([]);
});
it("registers every variable the themed stylesheets read", () => {
const read = [...themedCss.matchAll(/var\(--([a-z0-9-]+),/g)].map((m) => m[1]!);
const unknown = [...new Set(read)].filter((name) => !THEME_VARIABLE_NAMES.has(name) && !INTERNAL.has(name));
expect(unknown).toEqual([]);
});
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 reference 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);
});
});