archivo del paquete→ .shtukbase/packages/shtukbase-ui-html/test/ui.test.ts
test/ui.test.ts
173 líneas, 9.7 KB.
El archivo tal como está en el paquete: el código y sus comentarios no se traducen.
- // The library on its own pages and on small temporary projects: the format check, tokens-only, fresh, the build and the
- // attribute helpers. 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 { join } from 'node:path';
- import { build } from '../actions/build.ts';
- import { check as format } from '../checks/format.ts';
- import { check as fresh } from '../checks/fresh.ts';
- import { check as tokensOnly } from '../checks/tokens-only.ts';
- import { setAttr, setProp, variants } from '../lib/build.ts';
- import { parseHeader, scan, validate } from '../lib/components.ts';
- import { rawValues } from '../lib/tokens.ts';
- const PAGES = join(import.meta.dir, '..', 'pages');
- const dirs: string[] = [];
- const project = () => {
- const d = mkdtempSync(join(tmpdir(), 'sui-'));
- dirs.push(d);
- cpSync(join(PAGES, 'design'), join(d, 'design'), { recursive: true });
- return d;
- };
- afterAll(() => dirs.forEach((d) => rmSync(d, { recursive: true, force: true })));
- const comp = (d: string, rel: string) => join(d, 'design/shtukbase-ui-html/components', rel);
- const IDS = ['button', 'badge', 'chip', 'icon-plate', 'kicker', 'input', 'select', 'switch', 'slider', 'swatches',
- 'avatar', 'checkbox', 'label', 'progress', 'radio-group', 'separator', 'skeleton', 'textarea', 'tooltip', 'status-dot',
- 'card', 'package-card', 'field', 'accordion', 'alert', 'breadcrumb', 'command', 'dialog', 'dropdown-menu', 'popover',
- 'table', 'tabs', 'record-card', 'property-row', 'details-card', 'empty-state', 'tree-item', 'toolbar', 'search-field',
- 'settings-group', 'area', 'package-grid', 'settings-bar', 'peek'];
- // The lens shell (task DSP-1, /design/conventions/lens-shell.md of design-components): the level shell.
- const SHELL = ['sidebar', 'panel', 'panel-section', 'collapsible', 'scroll-area', 'sheet',
- 'layout-document', 'layout-grid', 'layout-catalog', 'layout-canvas'];
- describe('the library as shipped', () => {
- const comps = scan(PAGES);
- test('has every component of the task, each with CSS, at its level', () => {
- expect(comps.map((c) => c.header.id)).toEqual(expect.arrayContaining([...IDS, ...SHELL]));
- for (const c of comps) expect(c.css).not.toBeNull();
- expect(comps.find((c) => c.header.id === 'package-card')!.header.level).toBe('composite');
- expect(comps.find((c) => c.header.id === 'settings-bar')!.header.level).toBe('block');
- });
- test('passes its own checks', () => {
- expect(format(PAGES)).toEqual([]);
- expect(tokensOnly(PAGES)).toEqual([]);
- // ui.css ships prebuilt, so design-components sync (which runs before our build on install) already sees it;
- // after an edit: `bun -e` the build into pages/ (see the package page)
- expect(fresh(PAGES)).toEqual([]);
- });
- test('package-card has the controls the task names', () => {
- const h = comps.find((c) => c.header.id === 'package-card')!.header;
- expect(h.controls.preset.options).toEqual(['base', 'compact', 'editorial', 'cover', 'tiles']);
- expect(h.controls.layout.options).toEqual(['side', 'head', 'top', 'badge']);
- expect(h.controls.cover.options).toEqual(['hairline', 'hairline-right', 'tick', 'edge', 'icon-only']);
- expect(h.controls.glow.options).toEqual(expect.arrayContaining(['none', 'halo', 'rim', 'rim-thin']));
- expect(h.controls['glow-strength'].kind).toBe('range');
- });
- test('hover changes only the shadow of the package card', () => {
- const css = readFileSync(join(PAGES, 'design/shtukbase-ui-html/components/composite/package-card/package-card.css'), 'utf8');
- for (const m of css.matchAll(/[^{}]*:is\(:hover, \[data-hover\]\)[^{]*\{([^}]*)\}/g))
- for (const decl of m[1].split(';').map((s) => s.trim()).filter(Boolean)) expect(decl.split(':')[0]).toBe('box-shadow');
- });
- });
- describe('the lens shell', () => {
- const comps = scan(PAGES);
- test('every shell component lies at the level shell, after the blocks', () => {
- for (const id of SHELL) expect(comps.find((c) => c.header.id === id)?.header.level, id).toBe('shell');
- const order = comps.map((c) => c.header.level);
- expect(order.lastIndexOf('block')).toBeLessThan(order.indexOf('shell'));
- });
- test('the slots of the contract: every part is a data-slot in the default template', () => {
- for (const id of SHELL) {
- const c = comps.find((x) => x.header.id === id)!;
- const all = Object.values(c.templates).join('');
- for (const part of c.header.parts) expect(all, `${id}: ${part}`).toContain(`data-slot="${part}"`);
- }
- });
- test('text never overflows: every label, title and meta part truncates or wraps', () => {
- for (const id of SHELL) {
- const css = readFileSync(join(PAGES, 'design/shtukbase-ui-html/components/shell', id, `${id}.css`), 'utf8');
- for (const m of css.matchAll(/^\.([a-z-]+-(?:label|title|meta|description))\s*\{([^}]*)\}/gm))
- expect(/text-overflow:\s*ellipsis|overflow-wrap:\s*anywhere/.test(m[2]), `${id}: .${m[1]}`).toBe(true);
- }
- });
- test('a shell component at the wrong level fails, at the level shell passes', () => {
- const d = project();
- const text = readFileSync(comp(d, 'shell/panel/panel.html'), 'utf8');
- expect(validate(text, { level: 'shell', id: 'panel' })).toEqual([]);
- expect(validate(text.replace('level: shell', 'level: tray'), { level: 'shell', id: 'panel' }).join('\n')).toMatch(/level "tray" is not one of/);
- });
- });
- describe('format (SUI-R01)', () => {
- test('passes a valid project, fails a broken header, a missing template and a root without data-slot', () => {
- const d = project();
- expect(format(d)).toEqual([]);
- const f = comp(d, 'primitive/kicker/kicker.html');
- const text = readFileSync(f, 'utf8');
- writeFileSync(f, text.replace('level: primitive', 'level: widget'));
- expect(format(d).join('\n')).toContain('level "widget"');
- writeFileSync(f, text.replace('states: [default]', 'states: [default, hover]'));
- expect(format(d).join('\n')).toContain('no <template> for "hover"');
- writeFileSync(f, text.replace('data-slot="kicker" class="kicker"', 'class="kicker"'));
- expect(format(d).join('\n')).toContain('no data-slot="kicker"');
- });
- test('validate names a bad control', () => {
- 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>';
- const p = validate(t).join('\n');
- expect(p).toContain('kind "dial"');
- expect(p).toContain('a select needs options');
- expect(p).toContain('default "r"');
- expect(parseHeader('<div></div>').problem).toContain('no header');
- });
- });
- describe('tokens-only (SUI-R02)', () => {
- test('allows tokens, color-mix of tokens and relative colours from a token', () => {
- 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([]);
- });
- test('fails raw colours, named colours and plain radii', () => {
- const p = rawValues('.a{\n color:#191919;\n background:rgb(15 15 15 / .1);\n border-color:white;\n border-radius:12px}').join('\n');
- expect(p).toContain('line 2 (color): a raw hex colour');
- expect(p).toContain('rgb(');
- expect(p).toContain('named colour "white"');
- expect(p).toContain('radius "12px"');
- });
- test('the check names the file', () => {
- const d = project();
- const f = comp(d, 'primitive/badge/badge.css');
- writeFileSync(f, readFileSync(f, 'utf8') + '\n.badge{box-shadow:0 0 0 1px #e3e2de}\n');
- expect(tokensOnly(d).join('\n')).toContain('badge.css: line');
- });
- });
- describe('build and fresh (SUI-R03)', () => {
- test('build writes ui.css with every component, fresh passes, then fails after an edit', () => {
- const d = project();
- const { problems, css, demo } = build(d);
- expect(problems).toEqual([]);
- for (const id of IDS) expect(css).toContain(`/* ---- `), expect(css).toContain(`/${id} ---- */`);
- expect(demo).toContain('data-slot="package-grid"');
- rmSync(join(d, 'design/shtukbase-ui-html/ui.css'));
- expect(fresh(d).join('')).toContain('missing');
- mkdirSync(join(d, 'design/shtukbase-ui-html'), { recursive: true });
- writeFileSync(join(d, 'design/shtukbase-ui-html/ui.css'), css);
- expect(fresh(d)).toEqual([]);
- const f = comp(d, 'primitive/kicker/kicker.css');
- writeFileSync(f, readFileSync(f, 'utf8') + '\n.kicker{opacity:.9}\n');
- expect(fresh(d).join('')).toContain('does not match');
- });
- test('a broken component stops the build', () => {
- const d = project();
- writeFileSync(comp(d, 'primitive/kicker/kicker.html'), '<span>no header</span>');
- expect(build(d).problems.join('')).toContain('no header');
- });
- });
- describe('attribute helpers and variants', () => {
- test('setAttr and setProp change only the root', () => {
- 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>');
- expect(setAttr('<a data-on class="a"></a>', 'data-on', null)).toBe('<a class="a"></a>');
- expect(setProp('<a style="--k: 1px; color: red">', '--k', '2px')).toBe('<a style="color: red; --k: 2px">');
- });
- test('a preset variant of the package card carries its layout and size', () => {
- const c = scan(PAGES).find((x) => x.header.id === 'package-card')!;
- const v = Object.fromEntries(variants(c, 'preset', c.header.controls.preset));
- expect(v.cover).toContain('data-preset="cover"');
- expect(v.cover).toContain('data-layout="top"');
- expect(v.compact).toContain('data-size="s"');
- });
- });