ShtukbaseStandard
包文件→ .shtukbase/packages/design-tokens/test/health.test.ts

test/health.test.ts

126 行, 7.2 KB.

文件按包中的原样显示:代码及其注释不做翻译。

  1. // The style convention (/design/conventions/style.md) and its health, DTK-R04: bands of a style against the token contract,
  2. // in the mode warn (never a non-zero exit); passing and failing fixtures; the skeleton style of the recipe really is a style
  3. // that meets the contract; the convention page has its six sections. Run: `bun test` in the package.
  4. import { afterAll, describe, expect, test } from 'bun:test';
  5. import { cpSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
  6. import { tmpdir } from 'node:os';
  7. import { dirname, join } from 'node:path';
  8. import { buildStyle } from '../actions/build.ts';
  9. import { check as contractCheck } from '../checks/contract.ts';
  10. import { check, lines } from '../checks/health.ts';
  11. import { health, readContract } from '../lib/contract.ts';
  12. const pkg = join(import.meta.dir, '..');
  13. const fixture = (n: string) => readFileSync(join(import.meta.dir, 'fixtures', n), 'utf8');
  14. const tmps: string[] = [];
  15. function project(files: Record<string, string>): string {
  16. const dir = mkdtempSync(join(tmpdir(), 'dtk-health-'));
  17. tmps.push(dir);
  18. for (const [rel, text] of Object.entries(files)) {
  19. mkdirSync(dirname(join(dir, rel)), { recursive: true });
  20. writeFileSync(join(dir, rel), text);
  21. }
  22. return dir;
  23. }
  24. afterAll(() => { for (const d of tmps) rmSync(d, { recursive: true, force: true }); });
  25. const contract = readContract(project({}));
  26. const designPage = (style: string) => `---\ntype: Page\ntitle: Design\nstyle: ${style}\nlibraries: []\n---\n`;
  27. const everything = () => `${fixture('pass.css')}\n:root {\n${Object.entries(contract.extension).map(([k, v]) => ` ${k}: ${v};`).join('\n')}\n}\n`;
  28. describe('health(): the bands as names', () => {
  29. test('a style with the required names and no extension: fallbacks only', () => {
  30. const h = health(fixture('pass.css'), contract);
  31. expect(h.missing).toEqual([]);
  32. expect(h.fallbacks.length).toBe(55);
  33. expect(h.extra).toEqual([]);
  34. });
  35. test('missing names: a light name, a dark value, no dark scheme at all, a broken reference', () => {
  36. const h = health(fixture('fail-missing.css'), contract);
  37. expect(h.missing).toEqual(['--ring', '--card (dark)', '--sidebar-ring (dark)']);
  38. expect(health(fixture('fail-no-dark.css'), contract).missing).toHaveLength(contract.colors.length);
  39. expect(health(fixture('pass.css').replace(/(--primary): [^;]+;/, '$1: var(--nope);'), contract).missing).toEqual(['--primary (broken in light)']);
  40. });
  41. test('names outside the contract are extra; the Tailwind mirror names and animations are not', () => {
  42. const css = `${fixture('pass.css')}\n:root { --my-own: 1px; --color-card: var(--card); --animate-spin: spin 1s; }`;
  43. expect(health(css, contract).extra).toEqual(['--my-own']);
  44. });
  45. });
  46. describe('the lines of the check', () => {
  47. test('a complete style is full', () => {
  48. expect(lines(everything(), 's')).toEqual(['health style/s: full']);
  49. });
  50. test('fallbacks are listed with the hues folded; extra comes with full or with the others', () => {
  51. const out = lines(`${fixture('pass.css')}\n:root { --mine: 1; }`, 's');
  52. expect(out).toHaveLength(2);
  53. expect(out[0]).toMatch(/^health style\/s: fallbacks: --font-display,.*\(48 names\)$/);
  54. expect(out[1]).toBe('health style/s: extra: --mine');
  55. expect(lines(`${everything()}\n:root { --mine: 1; }`, 's')).toEqual(['health style/s: full', 'health style/s: extra: --mine']);
  56. });
  57. test('missing comes first and long lists are cut', () => {
  58. const out = lines(fixture('fail-no-dark.css'), 's');
  59. expect(out[0]).toMatch(/^health style\/s: missing: --background \(dark\),.*\(\+19 more\)$/);
  60. });
  61. });
  62. describe('the check in a project', () => {
  63. test('no style chosen: silent; the chosen style is judged; --style, --all and --css judge others', () => {
  64. expect(check(project({}))).toEqual([]);
  65. const root = project({
  66. 'design/design.md': designPage('a'),
  67. 'design/a/tokens.css': everything(),
  68. 'design/b/tokens.css': fixture('fail-missing.css'),
  69. 'themes/x.css': fixture('fail-no-dark.css'),
  70. });
  71. expect(check(root)).toEqual(['health style/a: full']);
  72. expect(check(root, { style: 'b' })[0]).toMatch(/^health style\/b: missing: --ring, --card \(dark\)/);
  73. expect(check(root, { all: true }).filter((l) => /: (full|missing)/.test(l)).map((l) => l.split(':')[0])).toEqual(['health style/a', 'health style/b']);
  74. expect(check(root, { css: 'themes/x.css' })[0]).toMatch(/^health style\/themes\/x\.css: missing:/);
  75. });
  76. test('a chosen style that is not built is reported as missing, not as a failure', () => {
  77. expect(check(project({ 'design/design.md': designPage('ghost') }))).toEqual(["health style/ghost: missing: design/ghost/tokens.css (the style's tokens are not built)"]);
  78. });
  79. test('the exit code is 0 even for a style that fails the contract (mode warn); the gate contract still fails it', () => {
  80. const root = project({ 'design/design.md': designPage('b'), 'design/b/tokens.css': fixture('fail-missing.css') });
  81. const run = Bun.spawnSync(['bun', join(pkg, 'checks/health.ts')], { cwd: root });
  82. expect(run.exitCode).toBe(0);
  83. expect(run.stdout.toString()).toMatch(/health style\/b: missing: --ring/);
  84. expect(contractCheck(root).findings.length).toBe(3);
  85. const all = Bun.spawnSync(['bun', join(pkg, 'checks/health.ts'), '--all'], { cwd: root });
  86. expect(all.exitCode).toBe(0);
  87. });
  88. });
  89. describe('the skeleton of the recipe', () => {
  90. const skel = join(pkg, 'skeleton/style');
  91. test('built as a style, it has every required name in both schemes: nothing missing, only fallbacks', () => {
  92. const root = project({});
  93. cpSync(join(skel, 'pages'), root, { recursive: true });
  94. expect(buildStyle('my-style', root).ok).toBe(true);
  95. writeFileSync(join(root, 'design/design.md'), designPage('my-style'));
  96. const out = check(root);
  97. expect(out).toHaveLength(1);
  98. expect(out[0]).toMatch(/^health style\/my-style: fallbacks: /);
  99. expect(contractCheck(root).findings).toEqual([]);
  100. });
  101. test('its manifest follows the convention and the build action uses the library of design-tokens', () => {
  102. const text = readFileSync(join(skel, 'package.md'), 'utf8');
  103. expect(text).toContain('follows: [design/conventions/style]');
  104. expect(readFileSync(join(skel, 'actions/build.ts'), 'utf8')).toContain("../../design-tokens/actions/build.ts");
  105. });
  106. });
  107. describe('the convention page and its norm', () => {
  108. const page = readFileSync(join(pkg, 'pages/design/conventions/style.md'), 'utf8');
  109. test('type Convention and the six sections, in order', () => {
  110. expect(page).toMatch(/^---\ntype: Convention\n/);
  111. const heads = [...page.matchAll(/^## (.+)$/gm)].map((m) => m[1]);
  112. expect(heads).toEqual(['1. What it covers', '2. Gives', '3. Expects', '4. Free to choose', '5. Make an alternative', '6. Health']);
  113. });
  114. test('DTK-R04 runs in the mode warn and its enforcer is the health check', () => {
  115. const rule = readFileSync(join(pkg, 'pages/norms/rules/design-tokens/DTK-R04.md'), 'utf8');
  116. expect(rule).toMatch(/^mode: warn$/m);
  117. expect(rule).toContain('enforcers: [.shtukbase/packages/design-tokens/checks/health.ts]');
  118. 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/);
  119. });
  120. });