ShtukbaseStandard
файл пакета→ .shtukbase/packages/shtukbase-ui-html/test/ui.test.ts

test/ui.test.ts

173 строки, 9.7 КБ.

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

  1. // The library on its own pages and on small temporary projects: the format check, tokens-only, fresh, the build and the
  2. // attribute helpers. Run: `bun test` in the package.
  3. import { afterAll, describe, expect, test } from 'bun:test';
  4. import { cpSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
  5. import { tmpdir } from 'node:os';
  6. import { join } from 'node:path';
  7. import { build } from '../actions/build.ts';
  8. import { check as format } from '../checks/format.ts';
  9. import { check as fresh } from '../checks/fresh.ts';
  10. import { check as tokensOnly } from '../checks/tokens-only.ts';
  11. import { setAttr, setProp, variants } from '../lib/build.ts';
  12. import { parseHeader, scan, validate } from '../lib/components.ts';
  13. import { rawValues } from '../lib/tokens.ts';
  14. const PAGES = join(import.meta.dir, '..', 'pages');
  15. const dirs: string[] = [];
  16. const project = () => {
  17. const d = mkdtempSync(join(tmpdir(), 'sui-'));
  18. dirs.push(d);
  19. cpSync(join(PAGES, 'design'), join(d, 'design'), { recursive: true });
  20. return d;
  21. };
  22. afterAll(() => dirs.forEach((d) => rmSync(d, { recursive: true, force: true })));
  23. const comp = (d: string, rel: string) => join(d, 'design/shtukbase-ui-html/components', rel);
  24. const IDS = ['button', 'badge', 'chip', 'icon-plate', 'kicker', 'input', 'select', 'switch', 'slider', 'swatches',
  25. 'avatar', 'checkbox', 'label', 'progress', 'radio-group', 'separator', 'skeleton', 'textarea', 'tooltip', 'status-dot',
  26. 'card', 'package-card', 'field', 'accordion', 'alert', 'breadcrumb', 'command', 'dialog', 'dropdown-menu', 'popover',
  27. 'table', 'tabs', 'record-card', 'property-row', 'details-card', 'empty-state', 'tree-item', 'toolbar', 'search-field',
  28. 'settings-group', 'area', 'package-grid', 'settings-bar', 'peek'];
  29. // The lens shell (task DSP-1, /design/conventions/lens-shell.md of design-components): the level shell.
  30. const SHELL = ['sidebar', 'panel', 'panel-section', 'collapsible', 'scroll-area', 'sheet',
  31. 'layout-document', 'layout-grid', 'layout-catalog', 'layout-canvas'];
  32. describe('the library as shipped', () => {
  33. const comps = scan(PAGES);
  34. test('has every component of the task, each with CSS, at its level', () => {
  35. expect(comps.map((c) => c.header.id)).toEqual(expect.arrayContaining([...IDS, ...SHELL]));
  36. for (const c of comps) expect(c.css).not.toBeNull();
  37. expect(comps.find((c) => c.header.id === 'package-card')!.header.level).toBe('composite');
  38. expect(comps.find((c) => c.header.id === 'settings-bar')!.header.level).toBe('block');
  39. });
  40. test('passes its own checks', () => {
  41. expect(format(PAGES)).toEqual([]);
  42. expect(tokensOnly(PAGES)).toEqual([]);
  43. // ui.css ships prebuilt, so design-components sync (which runs before our build on install) already sees it;
  44. // after an edit: `bun -e` the build into pages/ (see the package page)
  45. expect(fresh(PAGES)).toEqual([]);
  46. });
  47. test('package-card has the controls the task names', () => {
  48. const h = comps.find((c) => c.header.id === 'package-card')!.header;
  49. expect(h.controls.preset.options).toEqual(['base', 'compact', 'editorial', 'cover', 'tiles']);
  50. expect(h.controls.layout.options).toEqual(['side', 'head', 'top', 'badge']);
  51. expect(h.controls.cover.options).toEqual(['hairline', 'hairline-right', 'tick', 'edge', 'icon-only']);
  52. expect(h.controls.glow.options).toEqual(expect.arrayContaining(['none', 'halo', 'rim', 'rim-thin']));
  53. expect(h.controls['glow-strength'].kind).toBe('range');
  54. });
  55. test('hover changes only the shadow of the package card', () => {
  56. const css = readFileSync(join(PAGES, 'design/shtukbase-ui-html/components/composite/package-card/package-card.css'), 'utf8');
  57. for (const m of css.matchAll(/[^{}]*:is\(:hover, \[data-hover\]\)[^{]*\{([^}]*)\}/g))
  58. for (const decl of m[1].split(';').map((s) => s.trim()).filter(Boolean)) expect(decl.split(':')[0]).toBe('box-shadow');
  59. });
  60. });
  61. describe('the lens shell', () => {
  62. const comps = scan(PAGES);
  63. test('every shell component lies at the level shell, after the blocks', () => {
  64. for (const id of SHELL) expect(comps.find((c) => c.header.id === id)?.header.level, id).toBe('shell');
  65. const order = comps.map((c) => c.header.level);
  66. expect(order.lastIndexOf('block')).toBeLessThan(order.indexOf('shell'));
  67. });
  68. test('the slots of the contract: every part is a data-slot in the default template', () => {
  69. for (const id of SHELL) {
  70. const c = comps.find((x) => x.header.id === id)!;
  71. const all = Object.values(c.templates).join('');
  72. for (const part of c.header.parts) expect(all, `${id}: ${part}`).toContain(`data-slot="${part}"`);
  73. }
  74. });
  75. test('text never overflows: every label, title and meta part truncates or wraps', () => {
  76. for (const id of SHELL) {
  77. const css = readFileSync(join(PAGES, 'design/shtukbase-ui-html/components/shell', id, `${id}.css`), 'utf8');
  78. for (const m of css.matchAll(/^\.([a-z-]+-(?:label|title|meta|description))\s*\{([^}]*)\}/gm))
  79. expect(/text-overflow:\s*ellipsis|overflow-wrap:\s*anywhere/.test(m[2]), `${id}: .${m[1]}`).toBe(true);
  80. }
  81. });
  82. test('a shell component at the wrong level fails, at the level shell passes', () => {
  83. const d = project();
  84. const text = readFileSync(comp(d, 'shell/panel/panel.html'), 'utf8');
  85. expect(validate(text, { level: 'shell', id: 'panel' })).toEqual([]);
  86. expect(validate(text.replace('level: shell', 'level: tray'), { level: 'shell', id: 'panel' }).join('\n')).toMatch(/level "tray" is not one of/);
  87. });
  88. });
  89. describe('format (SUI-R01)', () => {
  90. test('passes a valid project, fails a broken header, a missing template and a root without data-slot', () => {
  91. const d = project();
  92. expect(format(d)).toEqual([]);
  93. const f = comp(d, 'primitive/kicker/kicker.html');
  94. const text = readFileSync(f, 'utf8');
  95. writeFileSync(f, text.replace('level: primitive', 'level: widget'));
  96. expect(format(d).join('\n')).toContain('level "widget"');
  97. writeFileSync(f, text.replace('states: [default]', 'states: [default, hover]'));
  98. expect(format(d).join('\n')).toContain('no <template> for "hover"');
  99. writeFileSync(f, text.replace('data-slot="kicker" class="kicker"', 'class="kicker"'));
  100. expect(format(d).join('\n')).toContain('no data-slot="kicker"');
  101. });
  102. test('validate names a bad control', () => {
  103. const t = '<!---\nid: x\ntitle: X\nlevel: primitive\ndescription: d\nstates: [default]\ncontrols:\n a: {kind: dial}\n b: {kind: select}\n c: {kind: segment, options: [p, q], default: r}\n--->\n<template data-state="default"><i data-slot="x" class="x"></i></template>';
  104. const p = validate(t).join('\n');
  105. expect(p).toContain('kind "dial"');
  106. expect(p).toContain('a select needs options');
  107. expect(p).toContain('default "r"');
  108. expect(parseHeader('<div></div>').problem).toContain('no header');
  109. });
  110. });
  111. describe('tokens-only (SUI-R02)', () => {
  112. test('allows tokens, color-mix of tokens and relative colours from a token', () => {
  113. expect(rawValues('.a{color:var(--foreground);background:color-mix(in srgb,var(--primary) 4%,transparent);border-color:oklch(from var(--primary) l c h);border-radius:calc(var(--radius) + 2px)}')).toEqual([]);
  114. });
  115. test('fails raw colours, named colours and plain radii', () => {
  116. const p = rawValues('.a{\n color:#191919;\n background:rgb(15 15 15 / .1);\n border-color:white;\n border-radius:12px}').join('\n');
  117. expect(p).toContain('line 2 (color): a raw hex colour');
  118. expect(p).toContain('rgb(');
  119. expect(p).toContain('named colour "white"');
  120. expect(p).toContain('radius "12px"');
  121. });
  122. test('the check names the file', () => {
  123. const d = project();
  124. const f = comp(d, 'primitive/badge/badge.css');
  125. writeFileSync(f, readFileSync(f, 'utf8') + '\n.badge{box-shadow:0 0 0 1px #e3e2de}\n');
  126. expect(tokensOnly(d).join('\n')).toContain('badge.css: line');
  127. });
  128. });
  129. describe('build and fresh (SUI-R03)', () => {
  130. test('build writes ui.css with every component, fresh passes, then fails after an edit', () => {
  131. const d = project();
  132. const { problems, css, demo } = build(d);
  133. expect(problems).toEqual([]);
  134. for (const id of IDS) expect(css).toContain(`/* ---- `), expect(css).toContain(`/${id} ---- */`);
  135. expect(demo).toContain('data-slot="package-grid"');
  136. rmSync(join(d, 'design/shtukbase-ui-html/ui.css'));
  137. expect(fresh(d).join('')).toContain('missing');
  138. mkdirSync(join(d, 'design/shtukbase-ui-html'), { recursive: true });
  139. writeFileSync(join(d, 'design/shtukbase-ui-html/ui.css'), css);
  140. expect(fresh(d)).toEqual([]);
  141. const f = comp(d, 'primitive/kicker/kicker.css');
  142. writeFileSync(f, readFileSync(f, 'utf8') + '\n.kicker{opacity:.9}\n');
  143. expect(fresh(d).join('')).toContain('does not match');
  144. });
  145. test('a broken component stops the build', () => {
  146. const d = project();
  147. writeFileSync(comp(d, 'primitive/kicker/kicker.html'), '<span>no header</span>');
  148. expect(build(d).problems.join('')).toContain('no header');
  149. });
  150. });
  151. describe('attribute helpers and variants', () => {
  152. test('setAttr and setProp change only the root', () => {
  153. expect(setAttr('<a data-x="1" class="a"><b data-x="2"></b></a>', 'data-x', '3')).toBe('<a class="a" data-x="3"><b data-x="2"></b></a>');
  154. expect(setAttr('<a data-on class="a"></a>', 'data-on', null)).toBe('<a class="a"></a>');
  155. expect(setProp('<a style="--k: 1px; color: red">', '--k', '2px')).toBe('<a style="color: red; --k: 2px">');
  156. });
  157. test('a preset variant of the package card carries its layout and size', () => {
  158. const c = scan(PAGES).find((x) => x.header.id === 'package-card')!;
  159. const v = Object.fromEntries(variants(c, 'preset', c.header.controls.preset));
  160. expect(v.cover).toContain('data-preset="cover"');
  161. expect(v.cover).toContain('data-layout="top"');
  162. expect(v.compact).toContain('data-size="s"');
  163. });
  164. });