Repository Public
1import { describe, expect, it } from "vitest";2import { readFileSync } from "node:fs";3import path from "node:path";4import { THEMES, TONES } from "@/schema";5import { contrast, mixHex, resolveTokens, THEME_PALETTES } from "@/renderer/themes";6import { HEAT_MAX_WITH_TEXT } from "@/renderer/blocks/MatrixSvg";78const css = readFileSync(path.resolve(import.meta.dirname, "../../src/renderer/sheet.css"), "utf8");910function tokensOf(preset: string): Record<string, string> {11 const m = new RegExp(`\\.sheet\\[data-theme="${preset}"\\]\\s*\\{([^}]*)\\}`).exec(css);12 if (!m) throw new Error(`no rule for ${preset}`);13 const out: Record<string, string> = {};14 for (const d of m[1].matchAll(/(--sh-[a-z-]+):\s*(#[0-9a-fA-F]{6})/g)) out[d[1]] = d[2].toLowerCase();15 out.scheme = /color-scheme:\s*(light|dark)/.exec(m[1])?.[1] ?? "";16 return out;17}1819describe("themes.ts mirrors sheet.css", () => {20 for (const preset of THEMES) {21 it(preset, () => {22 const fromCss = tokensOf(preset);23 const resolved = resolveTokens(preset, "blue");24 const colourTokens = Object.keys(fromCss).filter((k) => k.startsWith("--sh-"));25 expect(colourTokens).toHaveLength(8 + TONES.length * 2);26 for (const k of colourTokens) expect(resolved[k], k).toBe(fromCss[k]);27 expect(THEME_PALETTES[preset].scheme).toBe(fromCss.scheme);28 });29 }30 it("resolves the accent", () => {31 for (const tone of TONES) {32 const r = resolveTokens("editorial", tone);33 expect(r["--sh-accent"]).toBe(THEME_PALETTES.editorial.tones[tone].strong);34 expect(r["--sh-accent-soft"]).toBe(THEME_PALETTES.editorial.tones[tone].soft);35 expect(css).toContain(`.sheet[data-accent="${tone}"]`);36 }37 });38});3940describe("contrast (WCAG AA)", () => {41 it("text colours and strong tones are readable on the surfaces", () => {42 for (const preset of THEMES) {43 const p = THEME_PALETTES[preset];44 for (const bg of [p.bg, p.surface, p.raised]) {45 expect(contrast(p.ink, bg), `${preset} ink`).toBeGreaterThanOrEqual(7);46 expect(contrast(p.muted, bg), `${preset} muted`).toBeGreaterThanOrEqual(4.5);47 }48 for (const tone of TONES) {49 expect(contrast(p.tones[tone].strong, p.surface), `${preset} ${tone} on surface`).toBeGreaterThanOrEqual(4.5);50 expect(contrast(p.ink, p.tones[tone].soft), `${preset} ink on ${tone}-soft`).toBeGreaterThanOrEqual(7);51 }52 }53 });54 it("matrix values stay readable on the most saturated cell, for every theme and tone", () => {55 for (const preset of THEMES) for (const tone of TONES) {56 const p = THEME_PALETTES[preset];57 const cell = mixHex(p.tones[tone].strong, HEAT_MAX_WITH_TEXT, p.surface);58 expect(contrast(p.ink, cell), `${preset} ${tone}`).toBeGreaterThanOrEqual(4.5);59 }60 });61 it("mixes like CSS color-mix in srgb", () => {62 expect(mixHex("#000000", 50, "#ffffff")).toBe("#808080");63 expect(mixHex("#ff0000", 100, "#0000ff")).toBe("#ff0000");64 expect(mixHex("#ff0000", 0, "#0000ff")).toBe("#0000ff");65 });66});67