ShtukbaseStandard
файл пакета→ .shtukbase/packages/design-components/test/health.test.ts

test/health.test.ts

115 строк, 7.2 КБ.

Файл как есть в пакете: код и комментарии в нём не переводятся.

  1. // The component-library convention (/design/conventions/component-library.md) and its health, DSC-R05: bands of a library
  2. // (headers, tokens only, coverage of the shadcn list), mode warn (never a non-zero exit); passing and failing fixtures; the
  3. // convention page has its six sections and lists only names of the token contract. Run: `bun test` in the package.
  4. import { afterEach, describe, expect, test } from 'bun:test';
  5. import { existsSync, readFileSync, readdirSync, statSync } from 'node:fs';
  6. import { join } from 'node:path';
  7. import { check } from '../checks/health.ts';
  8. import { SHADCN } from '../lib/health.ts';
  9. import { readContract } from '../../design-tokens/lib/contract.ts';
  10. import { drop, project, put } from './fixture.ts';
  11. const pkg = join(import.meta.dir, '..');
  12. const roots: string[] = [];
  13. const make = () => { const r = project(); roots.push(r); return r; };
  14. afterEach(() => { for (const r of roots.splice(0)) drop(r); });
  15. const line = (out: string[], lib: string, band: string) => out.find((l) => l.startsWith(`health component-library/${lib}: ${band}`));
  16. describe('the fixture library', () => {
  17. test('the HTML library has valid headers and tokens only: the missing shadcn components are fallbacks, its own ids are extra', () => {
  18. const out = check(make());
  19. const f = line(out, 'fix-ui-html', 'fallbacks')!;
  20. expect(f).toMatch(/fallbacks: accordion, alert,/);
  21. expect(f).toContain(`(${SHADCN.length - 1} of ${SHADCN.length} shadcn components are not in this library)`);
  22. expect(f).not.toMatch(/: [^()]*\bcard\b/);
  23. expect(line(out, 'fix-ui-html', 'missing')).toBeUndefined();
  24. expect(line(out, 'fix-ui-html', 'full')).toBeUndefined();
  25. expect(line(out, 'fix-ui-html', 'extra: components')).toBe('health component-library/fix-ui-html: extra: components card-grid, chip');
  26. });
  27. test('the React library is judged on its own files', () => {
  28. const out = check(make());
  29. expect(line(out, 'fix-ui-react', 'fallbacks')).toContain('(29 of 29');
  30. expect(line(out, 'fix-ui-react', 'extra: components')).toBe('health component-library/fix-ui-react: extra: components chip');
  31. });
  32. test('--lib judges one library; a project with no components is silent', () => {
  33. expect(check(make(), 'fix-ui-react').every((l) => l.startsWith('health component-library/fix-ui-react:'))).toBe(true);
  34. expect(check(make(), 'nope')).toEqual([]);
  35. const empty = make();
  36. drop(join(empty, 'design/fix-ui-html'));
  37. drop(join(empty, 'design/fix-ui-react'));
  38. expect(check(empty)).toEqual([]);
  39. });
  40. });
  41. describe('what is missing', () => {
  42. test('a header that does not parse and a raw colour are named by file', () => {
  43. const root = make();
  44. 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');
  45. put(root, 'design/fix-ui-html/components/primitive/chip/chip.css', '.chip { color: #c00; }\n');
  46. const m = line(check(root), 'fix-ui-html', 'missing')!;
  47. expect(m).toContain('design/fix-ui-html/components/primitive/bad/bad.html (header)');
  48. expect(m).toContain('design/fix-ui-html/components/primitive/chip/chip.css (raw colour)');
  49. expect(m).not.toContain('card.css');
  50. });
  51. test('the exit code is 0 for such a library (mode warn)', () => {
  52. const root = make();
  53. put(root, 'design/fix-ui-html/components/primitive/chip/chip.css', '.chip { color: #c00; }\n');
  54. const run = Bun.spawnSync([process.execPath, join(pkg, 'checks/health.ts')], { cwd: root });
  55. expect(run.exitCode).toBe(0);
  56. expect(run.stdout.toString()).toMatch(/health component-library\/fix-ui-html: missing: .*chip\.css \(raw colour\)/);
  57. const one = Bun.spawnSync([process.execPath, join(pkg, 'checks/health.ts'), '--lib', 'fix-ui-react'], { cwd: root });
  58. expect(one.exitCode).toBe(0);
  59. expect(one.stdout.toString()).not.toContain('fix-ui-html');
  60. });
  61. });
  62. describe('css names', () => {
  63. test('a name outside the contract that the library reads is extra; its own and its control properties are not', () => {
  64. const root = make();
  65. 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');
  66. expect(line(check(root), 'fix-ui-html', 'extra: css names')).toBe('health component-library/fix-ui-html: extra: css names --weird-name');
  67. });
  68. });
  69. describe('a full library', () => {
  70. test('every shadcn component with a valid header and tokens only is full', () => {
  71. const root = make();
  72. for (const id of SHADCN) {
  73. 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`);
  74. put(root, `design/all-lib/components/primitive/${id}/${id}.css`, `[data-slot="${id}"] { color: var(--foreground); }\n`);
  75. }
  76. expect(check(root, 'all-lib')).toEqual([`health component-library/all-lib: full (${SHADCN.length} components)`]);
  77. });
  78. });
  79. describe('the convention page and its norm', () => {
  80. const page = readFileSync(join(pkg, 'pages/design/conventions/component-library.md'), 'utf8');
  81. test('type Convention and the six sections, in order', () => {
  82. expect(page).toMatch(/^---\ntype: Convention\n/);
  83. 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']);
  84. });
  85. const contract = readContract(make());
  86. const known = new Set([...contract.required, ...Object.keys(contract.extension)]);
  87. const expects = page.slice(page.indexOf('## 3. Expects'), page.indexOf('## 4. Free to choose'));
  88. const listed = [...expects.matchAll(/`(--[\w-]+)`/g)].map((m) => m[1]);
  89. test('"Expects" lists only names of the token contract (a … range stands for --sb-hue-1 … 12)', () => {
  90. expect(listed.length).toBeGreaterThan(20);
  91. for (const n of listed) expect(known.has(n)).toBe(true);
  92. });
  93. test('every contract name that shtukbase-ui-html reads is listed there', () => {
  94. const dir = join(pkg, '..', 'shtukbase-ui-html/pages/design/shtukbase-ui-html/components');
  95. if (!existsSync(dir)) return;
  96. const css = (d: string): string[] => readdirSync(d).flatMap((n) => (statSync(join(d, n)).isDirectory() ? css(join(d, n)) : n.endsWith('.css') ? [join(d, n)] : []));
  97. const used = new Set<string>();
  98. 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]);
  99. const hues = (n: string) => /^--sb-hue-\d+$/.test(n);
  100. const missing = [...used].filter((n) => !listed.includes(n) && !hues(n));
  101. expect(missing).toEqual([]);
  102. });
  103. test('DSC-R05 runs in the mode warn and its enforcer is the health check', () => {
  104. const rule = readFileSync(join(pkg, 'pages/norms/rules/design-components/DSC-R05.md'), 'utf8');
  105. expect(rule).toMatch(/^mode: warn$/m);
  106. expect(rule).toContain('enforcers: [.shtukbase/packages/design-components/checks/health.ts]');
  107. 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/);
  108. });
  109. });