package file→ .shtukbase/packages/design-tokens/test/health.test.ts
test/health.test.ts
126 lines, 7.2 KB.
- // The style convention (/design/conventions/style.md) and its health, DTK-R04: bands of a style against the token contract,
- // in the mode warn (never a non-zero exit); passing and failing fixtures; the skeleton style of the recipe really is a style
- // that meets the contract; the convention page has its six sections. Run: `bun test` in the package.
- import { afterAll, describe, expect, test } from 'bun:test';
- import { cpSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
- import { tmpdir } from 'node:os';
- import { dirname, join } from 'node:path';
- import { buildStyle } from '../actions/build.ts';
- import { check as contractCheck } from '../checks/contract.ts';
- import { check, lines } from '../checks/health.ts';
- import { health, readContract } from '../lib/contract.ts';
- const pkg = join(import.meta.dir, '..');
- const fixture = (n: string) => readFileSync(join(import.meta.dir, 'fixtures', n), 'utf8');
- const tmps: string[] = [];
- function project(files: Record<string, string>): string {
- const dir = mkdtempSync(join(tmpdir(), 'dtk-health-'));
- tmps.push(dir);
- for (const [rel, text] of Object.entries(files)) {
- mkdirSync(dirname(join(dir, rel)), { recursive: true });
- writeFileSync(join(dir, rel), text);
- }
- return dir;
- }
- afterAll(() => { for (const d of tmps) rmSync(d, { recursive: true, force: true }); });
- const contract = readContract(project({}));
- const designPage = (style: string) => `---\ntype: Page\ntitle: Design\nstyle: ${style}\nlibraries: []\n---\n`;
- const everything = () => `${fixture('pass.css')}\n:root {\n${Object.entries(contract.extension).map(([k, v]) => ` ${k}: ${v};`).join('\n')}\n}\n`;
- describe('health(): the bands as names', () => {
- test('a style with the required names and no extension: fallbacks only', () => {
- const h = health(fixture('pass.css'), contract);
- expect(h.missing).toEqual([]);
- expect(h.fallbacks.length).toBe(55);
- expect(h.extra).toEqual([]);
- });
- test('missing names: a light name, a dark value, no dark scheme at all, a broken reference', () => {
- const h = health(fixture('fail-missing.css'), contract);
- expect(h.missing).toEqual(['--ring', '--card (dark)', '--sidebar-ring (dark)']);
- expect(health(fixture('fail-no-dark.css'), contract).missing).toHaveLength(contract.colors.length);
- expect(health(fixture('pass.css').replace(/(--primary): [^;]+;/, '$1: var(--nope);'), contract).missing).toEqual(['--primary (broken in light)']);
- });
- test('names outside the contract are extra; the Tailwind mirror names and animations are not', () => {
- const css = `${fixture('pass.css')}\n:root { --my-own: 1px; --color-card: var(--card); --animate-spin: spin 1s; }`;
- expect(health(css, contract).extra).toEqual(['--my-own']);
- });
- });
- describe('the lines of the check', () => {
- test('a complete style is full', () => {
- expect(lines(everything(), 's')).toEqual(['health style/s: full']);
- });
- test('fallbacks are listed with the hues folded; extra comes with full or with the others', () => {
- const out = lines(`${fixture('pass.css')}\n:root { --mine: 1; }`, 's');
- expect(out).toHaveLength(2);
- expect(out[0]).toMatch(/^health style\/s: fallbacks: --font-display,.*\(48 names\)$/);
- expect(out[1]).toBe('health style/s: extra: --mine');
- expect(lines(`${everything()}\n:root { --mine: 1; }`, 's')).toEqual(['health style/s: full', 'health style/s: extra: --mine']);
- });
- test('missing comes first and long lists are cut', () => {
- const out = lines(fixture('fail-no-dark.css'), 's');
- expect(out[0]).toMatch(/^health style\/s: missing: --background \(dark\),.*\(\+19 more\)$/);
- });
- });
- describe('the check in a project', () => {
- test('no style chosen: silent; the chosen style is judged; --style, --all and --css judge others', () => {
- expect(check(project({}))).toEqual([]);
- const root = project({
- 'design/design.md': designPage('a'),
- 'design/a/tokens.css': everything(),
- 'design/b/tokens.css': fixture('fail-missing.css'),
- 'themes/x.css': fixture('fail-no-dark.css'),
- });
- expect(check(root)).toEqual(['health style/a: full']);
- expect(check(root, { style: 'b' })[0]).toMatch(/^health style\/b: missing: --ring, --card \(dark\)/);
- expect(check(root, { all: true }).filter((l) => /: (full|missing)/.test(l)).map((l) => l.split(':')[0])).toEqual(['health style/a', 'health style/b']);
- expect(check(root, { css: 'themes/x.css' })[0]).toMatch(/^health style\/themes\/x\.css: missing:/);
- });
- test('a chosen style that is not built is reported as missing, not as a failure', () => {
- expect(check(project({ 'design/design.md': designPage('ghost') }))).toEqual(["health style/ghost: missing: design/ghost/tokens.css (the style's tokens are not built)"]);
- });
- test('the exit code is 0 even for a style that fails the contract (mode warn); the gate contract still fails it', () => {
- const root = project({ 'design/design.md': designPage('b'), 'design/b/tokens.css': fixture('fail-missing.css') });
- const run = Bun.spawnSync(['bun', join(pkg, 'checks/health.ts')], { cwd: root });
- expect(run.exitCode).toBe(0);
- expect(run.stdout.toString()).toMatch(/health style\/b: missing: --ring/);
- expect(contractCheck(root).findings.length).toBe(3);
- const all = Bun.spawnSync(['bun', join(pkg, 'checks/health.ts'), '--all'], { cwd: root });
- expect(all.exitCode).toBe(0);
- });
- });
- describe('the skeleton of the recipe', () => {
- const skel = join(pkg, 'skeleton/style');
- test('built as a style, it has every required name in both schemes: nothing missing, only fallbacks', () => {
- const root = project({});
- cpSync(join(skel, 'pages'), root, { recursive: true });
- expect(buildStyle('my-style', root).ok).toBe(true);
- writeFileSync(join(root, 'design/design.md'), designPage('my-style'));
- const out = check(root);
- expect(out).toHaveLength(1);
- expect(out[0]).toMatch(/^health style\/my-style: fallbacks: /);
- expect(contractCheck(root).findings).toEqual([]);
- });
- test('its manifest follows the convention and the build action uses the library of design-tokens', () => {
- const text = readFileSync(join(skel, 'package.md'), 'utf8');
- expect(text).toContain('follows: [design/conventions/style]');
- expect(readFileSync(join(skel, 'actions/build.ts'), 'utf8')).toContain("../../design-tokens/actions/build.ts");
- });
- });
- describe('the convention page and its norm', () => {
- const page = readFileSync(join(pkg, 'pages/design/conventions/style.md'), 'utf8');
- test('type Convention and the six sections, in order', () => {
- expect(page).toMatch(/^---\ntype: Convention\n/);
- const heads = [...page.matchAll(/^## (.+)$/gm)].map((m) => m[1]);
- expect(heads).toEqual(['1. What it covers', '2. Gives', '3. Expects', '4. Free to choose', '5. Make an alternative', '6. Health']);
- });
- test('DTK-R04 runs in the mode warn and its enforcer is the health check', () => {
- const rule = readFileSync(join(pkg, 'pages/norms/rules/design-tokens/DTK-R04.md'), 'utf8');
- expect(rule).toMatch(/^mode: warn$/m);
- expect(rule).toContain('enforcers: [.shtukbase/packages/design-tokens/checks/health.ts]');
- expect(readFileSync(join(pkg, 'package.md'), 'utf8')).toMatch(/- name: health\n\s+run: checks\/health\.ts\n\s+check: true\n\s+norm: \/norms\/rules\/design-tokens\/DTK-R04\.md/);
- });
- });