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

test/design.test.ts

86 строк, 4.6 КБ.

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

  1. // The build includes the style and library CSS. A separate layout supplies the frame. Run: `bun test`.
  2. import { afterAll, describe, expect, test } from 'bun:test';
  3. import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
  4. import { tmpdir } from 'node:os';
  5. import { join } from 'node:path';
  6. // @ts-ignore - plain JS module
  7. import { designFiles, kitFiles } from '../kit/src/build.js';
  8. const dirs: string[] = [];
  9. afterAll(() => dirs.forEach((d) => rmSync(d, { recursive: true, force: true })));
  10. function project(design: { style?: string; libraries?: string[] } | null, opts: { shell?: boolean; tokens?: boolean } = {}) {
  11. const d = mkdtempSync(join(tmpdir(), 'lenses-design-'));
  12. dirs.push(d);
  13. mkdirSync(join(d, 'design'), { recursive: true });
  14. if (design)
  15. writeFileSync(join(d, 'design', 'design.md'),
  16. `---\ntype: Page\ntitle: Design\nstyle: "${design.style ?? ''}"\nlibraries: [${(design.libraries ?? []).join(', ')}]\n---\n`);
  17. if (opts.tokens) {
  18. mkdirSync(join(d, 'design', 'my-style'), { recursive: true });
  19. writeFileSync(join(d, 'design', 'my-style', 'tokens.css'), ':root { --primary: #cf3a63; }\n');
  20. }
  21. mkdirSync(join(d, 'design', 'my-ui', 'components', opts.shell === false ? 'block' : 'shell', 'panel'), { recursive: true });
  22. writeFileSync(join(d, 'design', 'my-ui', 'ui.css'), '.panel { color: var(--foreground); } /* my-ui */\n');
  23. return d;
  24. }
  25. const KIT_CSS = readFileSync(join(import.meta.dir, '..', 'kit', 'kit', 'kit.css'), 'utf8');
  26. describe('the kit finds the style and the library', () => {
  27. test('passes: the chosen style and library go into kit.css with the built-in frame', () => {
  28. const d = project({ style: 'my-style', libraries: ['my-ui'] }, { tokens: true });
  29. expect(designFiles(d).library?.id).toBe('my-ui');
  30. expect(designFiles(d).style?.id).toBe('my-style');
  31. const css = kitFiles(d)['kit.css'];
  32. expect(css).toContain('/* my-ui */');
  33. expect(css).toContain('--primary: #cf3a63');
  34. expect(css).not.toContain('lenses kit/shell.css, the fallback');
  35. expect(css).toContain("the built-in layout");
  36. // The library comes after the kit's own CSS so its component styles win.
  37. expect(css.indexOf('/* my-ui */')).toBeGreaterThan(css.indexOf('---- lenses: kit.css ----'));
  38. });
  39. test('fails over: no library with the shell means the kit fallback draws the frame', () => {
  40. const d = project({ style: '', libraries: [] }, { shell: false });
  41. expect(designFiles(d).library).toBeNull();
  42. const css = kitFiles(d)['kit.css'];
  43. expect(css).toContain('lenses kit/shell.css, the fallback');
  44. expect(css).not.toContain('/* my-ui */');
  45. });
  46. test('an older library bundle loses its frame CSS while keeping the other parts', () => {
  47. const d = project({ libraries: ['my-ui'] });
  48. writeFileSync(join(d, 'design/my-ui/ui.css'), [
  49. '/* ---- shell/shell ---- */', '.shell { background: purple; }',
  50. '/* ---- shell/sidebar ---- */', '.sidebar { width: 100%; }',
  51. ].join('\n'));
  52. const css = designFiles(d).library?.css;
  53. expect(css).not.toContain('background: purple');
  54. expect(css).toContain('.sidebar { width: 100%; }');
  55. });
  56. test('a library named on design.md but without the shell is not taken', () => {
  57. expect(designFiles(project({ libraries: ['my-ui'] }, { shell: false })).library).toBeNull();
  58. });
  59. test('no design.md: the first library with the shell is taken', () => {
  60. expect(designFiles(project(null)).library?.id).toBe('my-ui');
  61. });
  62. test('a resolved partial library can style controls while the built-in shell supplies the frame', () => {
  63. const d = project({ libraries: ['my-ui'] }, { shell: false });
  64. mkdirSync(join(d, 'lib/design'), { recursive: true });
  65. writeFileSync(join(d, 'lib/design/ui.css'), '/* resolved button */\n.button { border-radius: 999px; }\n');
  66. const css = kitFiles(d)['kit.css'];
  67. expect(css).toContain('lenses kit/shell.css, the fallback');
  68. expect(css).toContain('the built-in layout');
  69. expect(css.indexOf('/* resolved button */')).toBeGreaterThan(css.indexOf('the fallback components'));
  70. expect(css).not.toContain('/* my-ui */');
  71. });
  72. });
  73. describe('the kit keeps no chrome of its own', () => {
  74. test('its CSS has no rules for the old header, side panel, section cards, buttons or segmented choice', () => {
  75. for (const cls of ['lx-top', 'lx-side', 'lx-sec', 'lx-sech', 'lx-seg', 'lx-btn', 'lx-zoomctl', 'lx-app', 'lx-skin-btn'])
  76. expect(new RegExp(`\\.${cls}\\b`).test(KIT_CSS), cls).toBe(false);
  77. });
  78. test('the kit names its clashing variables --lx-*, never the contract names', () => {
  79. expect(/--(accent|muted|card|radius)\s*:/.test(KIT_CSS)).toBe(false);
  80. });
  81. });