файл пакета→ .shtukbase/packages/design-components/test/health.test.ts
test/health.test.ts
115 строк, 7.2 КБ.
Файл как есть в пакете: код и комментарии в нём не переводятся.
- // The component-library convention (/design/conventions/component-library.md) and its health, DSC-R05: bands of a library
- // (headers, tokens only, coverage of the shadcn list), mode warn (never a non-zero exit); passing and failing fixtures; the
- // convention page has its six sections and lists only names of the token contract. Run: `bun test` in the package.
- import { afterEach, describe, expect, test } from 'bun:test';
- import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs';
- import { join } from 'node:path';
- import { check } from '../checks/health.ts';
- import { SHADCN } from '../lib/health.ts';
- import { readContract } from '../../design-tokens/lib/contract.ts';
- import { drop, project, put } from './fixture.ts';
- const pkg = join(import.meta.dir, '..');
- const roots: string[] = [];
- const make = () => { const r = project(); roots.push(r); return r; };
- afterEach(() => { for (const r of roots.splice(0)) drop(r); });
- const line = (out: string[], lib: string, band: string) => out.find((l) => l.startsWith(`health component-library/${lib}: ${band}`));
- describe('the fixture library', () => {
- test('the HTML library has valid headers and tokens only: the missing shadcn components are fallbacks, its own ids are extra', () => {
- const out = check(make());
- const f = line(out, 'fix-ui-html', 'fallbacks')!;
- expect(f).toMatch(/fallbacks: accordion, alert,/);
- expect(f).toContain(`(${SHADCN.length - 1} of ${SHADCN.length} shadcn components are not in this library)`);
- expect(f).not.toMatch(/: [^()]*\bcard\b/);
- expect(line(out, 'fix-ui-html', 'missing')).toBeUndefined();
- expect(line(out, 'fix-ui-html', 'full')).toBeUndefined();
- expect(line(out, 'fix-ui-html', 'extra: components')).toBe('health component-library/fix-ui-html: extra: components card-grid, chip');
- });
- test('the React library is judged on its own files', () => {
- const out = check(make());
- expect(line(out, 'fix-ui-react', 'fallbacks')).toContain('(29 of 29');
- expect(line(out, 'fix-ui-react', 'extra: components')).toBe('health component-library/fix-ui-react: extra: components chip');
- });
- test('--lib judges one library; a project with no components is silent', () => {
- expect(check(make(), 'fix-ui-react').every((l) => l.startsWith('health component-library/fix-ui-react:'))).toBe(true);
- expect(check(make(), 'nope')).toEqual([]);
- const empty = make();
- drop(join(empty, 'design/fix-ui-html'));
- drop(join(empty, 'design/fix-ui-react'));
- expect(check(empty)).toEqual([]);
- });
- });
- describe('what is missing', () => {
- test('a header that does not parse and a raw colour are named by file', () => {
- const root = make();
- put(root, 'design/fix-ui-html/components/primitive/bad/bad.html', '<!---\nid: Bad Id\nlevel: primitive\n--->\n<template data-state="default"><i></i></template>\n');
- put(root, 'design/fix-ui-html/components/primitive/chip/chip.css', '.chip { color: #c00; }\n');
- const m = line(check(root), 'fix-ui-html', 'missing')!;
- expect(m).toContain('design/fix-ui-html/components/primitive/bad/bad.html (header)');
- expect(m).toContain('design/fix-ui-html/components/primitive/chip/chip.css (raw colour)');
- expect(m).not.toContain('card.css');
- });
- test('the exit code is 0 for such a library (mode warn)', () => {
- const root = make();
- put(root, 'design/fix-ui-html/components/primitive/chip/chip.css', '.chip { color: #c00; }\n');
- const run = Bun.spawnSync([process.execPath, join(pkg, 'checks/health.ts')], { cwd: root });
- expect(run.exitCode).toBe(0);
- expect(run.stdout.toString()).toMatch(/health component-library\/fix-ui-html: missing: .*chip\.css \(raw colour\)/);
- const one = Bun.spawnSync([process.execPath, join(pkg, 'checks/health.ts'), '--lib', 'fix-ui-react'], { cwd: root });
- expect(one.exitCode).toBe(0);
- expect(one.stdout.toString()).not.toContain('fix-ui-html');
- });
- });
- describe('css names', () => {
- test('a name outside the contract that the library reads is extra; its own and its control properties are not', () => {
- const root = make();
- put(root, 'design/fix-ui-html/components/primitive/chip/chip.css', '.chip { --local: 1px; gap: var(--local); padding: var(--chip-pad); margin: var(--weird-name); color: var(--foreground); }\n');
- expect(line(check(root), 'fix-ui-html', 'extra: css names')).toBe('health component-library/fix-ui-html: extra: css names --weird-name');
- });
- });
- describe('a full library', () => {
- test('every shadcn component with a valid header and tokens only is full', () => {
- const root = make();
- for (const id of SHADCN) {
- put(root, `design/all-lib/components/primitive/${id}/${id}.html`, `<!---\nid: ${id}\nlevel: primitive\nstates: [default]\n--->\n<template data-state="default"><i data-slot="${id}"></i></template>\n`);
- put(root, `design/all-lib/components/primitive/${id}/${id}.css`, `[data-slot="${id}"] { color: var(--foreground); }\n`);
- }
- expect(check(root, 'all-lib')).toEqual([`health component-library/all-lib: full (${SHADCN.length} components)`]);
- });
- });
- describe('the convention page and its norm', () => {
- const page = readFileSync(join(pkg, 'pages/design/conventions/component-library.md'), 'utf8');
- test('type Convention and the six sections, in order', () => {
- expect(page).toMatch(/^---\ntype: Convention\n/);
- expect([...page.matchAll(/^## (.+)$/gm)].map((m) => m[1])).toEqual(['1. What it covers', '2. Gives', '3. Expects', '4. Free to choose', '5. Make an alternative', '6. Health']);
- });
- const contract = readContract(make());
- const known = new Set([...contract.required, ...Object.keys(contract.extension)]);
- const expects = page.slice(page.indexOf('## 3. Expects'), page.indexOf('## 4. Free to choose'));
- const listed = [...expects.matchAll(/`(--[\w-]+)`/g)].map((m) => m[1]);
- test('"Expects" lists only names of the token contract (a … range stands for --sb-hue-1 … 12)', () => {
- expect(listed.length).toBeGreaterThan(20);
- for (const n of listed) expect(known.has(n)).toBe(true);
- });
- test('every contract name that shtukbase-ui-html reads is listed there', () => {
- const dir = join(pkg, '..', 'shtukbase-ui-html/pages/design/shtukbase-ui-html/components');
- if (!existsSync(dir)) return;
- const css = (d: string): string[] => readdirSync(d).flatMap((n) => (statSync(join(d, n)).isDirectory() ? css(join(d, n)) : n.endsWith('.css') ? [join(d, n)] : []));
- const used = new Set<string>();
- for (const f of css(dir)) for (const m of readFileSync(f, 'utf8').matchAll(/var\(\s*(--[\w-]+)/g)) if (known.has(m[1])) used.add(m[1]);
- const hues = (n: string) => /^--sb-hue-\d+$/.test(n);
- const missing = [...used].filter((n) => !listed.includes(n) && !hues(n));
- expect(missing).toEqual([]);
- });
- test('DSC-R05 runs in the mode warn and its enforcer is the health check', () => {
- const rule = readFileSync(join(pkg, 'pages/norms/rules/design-components/DSC-R05.md'), 'utf8');
- expect(rule).toMatch(/^mode: warn$/m);
- expect(rule).toContain('enforcers: [.shtukbase/packages/design-components/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-components\/DSC-R05\.md/);
- });
- });