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

test/build.test.ts

94 строки, 4.1 КБ.

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

  1. // The actions `build` and `check` on a copy of the fixture project; and the two lenses' headers.
  2. import { afterAll, describe, expect, test } from 'bun:test';
  3. import { cpSync, existsSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
  4. import { tmpdir } from 'node:os';
  5. import { dirname, join } from 'node:path';
  6. import { fileURLToPath } from 'node:url';
  7. import { lensHeader } from '../../lenses/kit/src/build.js';
  8. const PKG = join(dirname(fileURLToPath(import.meta.url)), '..');
  9. const FIX = join(PKG, 'test', 'fixtures', 'project');
  10. const dirs: string[] = [];
  11. const scratch = () => {
  12. const d = mkdtempSync(join(tmpdir(), 'design-lenses-'));
  13. dirs.push(d);
  14. cpSync(FIX, d, { recursive: true });
  15. return d;
  16. };
  17. const run = (action: string, cwd: string) => {
  18. const r = Bun.spawnSync([process.execPath, join(PKG, 'actions', `${action}.ts`)], { cwd });
  19. return { code: r.exitCode, out: r.stdout.toString() + r.stderr.toString() };
  20. };
  21. afterAll(() => { for (const d of dirs) rmSync(d, { recursive: true, force: true }); });
  22. describe('build and check', () => {
  23. test('build writes the data of the chosen style; check passes', () => {
  24. const d = scratch();
  25. expect(run('check', d).code).toBe(1); // missing before the first build
  26. const b = run('build', d);
  27. expect(b.code).toBe(0);
  28. const text = readFileSync(join(d, 'lib/design/style.data.js'), 'utf8');
  29. const box: { SB_STYLE?: any } = {};
  30. new Function('window', text)(box);
  31. const S = box.SB_STYLE;
  32. expect(S.version).toBe(1);
  33. expect(S.id).toBe('demo');
  34. expect(S.sections).toEqual([{ title: 'Principles', page: '/design/demo/principles.md' }]);
  35. expect(S.css).toEqual(['/design/demo/tokens.css']);
  36. expect(S.assets).toEqual(['/design/demo/assets/mark.svg']);
  37. expect(S.styles.map((s: { id: string }) => s.id)).toEqual(['demo']);
  38. expect(S.problems).toEqual([]);
  39. const bg = S.tokens.find((t: { name: string }) => t.name === '--background');
  40. expect(bg).toMatchObject({ group: 'Colors', light: '#ffffff', dark: '#191919', description: 'Page background' });
  41. for (const n of ['--chart-5', '--sidebar-ring', '--font-sans', '--shadow-lg', '--sb-glow', '--sb-hue-1', '--sb-dur']) {
  42. expect(S.tokens.some((t: { name: string }) => t.name === n)).toBe(true);
  43. }
  44. expect(run('check', d).code).toBe(0);
  45. });
  46. test('check fails when the style changed after the build', () => {
  47. const d = scratch();
  48. expect(run('build', d).code).toBe(0);
  49. const p = join(d, 'design/demo/tokens.json');
  50. writeFileSync(p, readFileSync(p, 'utf8').replace('"#e04a72"', '"#ff0000"'));
  51. const c = run('check', d);
  52. expect(c.code).toBe(1);
  53. expect(c.out).toContain('stale');
  54. });
  55. test('build fails with a reason when no style is chosen or it is not installed', () => {
  56. const d = scratch();
  57. writeFileSync(join(d, 'design/design.md'), '---\ntype: Page\ntitle: Design\nstyle: \n---\n');
  58. let b = run('build', d);
  59. expect(b.code).toBe(1);
  60. expect(b.out).toContain('no style is chosen');
  61. writeFileSync(join(d, 'design/design.md'), '---\ntype: Page\ntitle: Design\nstyle: nope\n---\n');
  62. b = run('build', d);
  63. expect(b.code).toBe(1);
  64. expect(b.out).toContain('not installed');
  65. expect(existsSync(join(d, 'lib/design/style.data.js'))).toBe(false);
  66. });
  67. test('a style with only a built tokens.css still gives a specimen', () => {
  68. const d = scratch();
  69. rmSync(join(d, 'design/demo/tokens.json'));
  70. rmSync(join(d, 'design/demo/tokens.dark.json'));
  71. expect(run('build', d).code).toBe(0);
  72. const text = readFileSync(join(d, 'lib/design/style.data.js'), 'utf8');
  73. expect(text).toContain('"--background"');
  74. expect(text).toContain('"#191919"');
  75. });
  76. });
  77. describe('the lenses', () => {
  78. for (const f of ['style.html', 'components.html']) {
  79. test(`${f} is a kit lens with a custom template`, () => {
  80. const html = readFileSync(join(PKG, 'pages', 'design', f), 'utf8');
  81. const h = lensHeader(html);
  82. expect(h?.cfg.type).toBe('Lens');
  83. expect(h?.cfg.template).toBe('custom');
  84. expect(html).toContain('../lib/kit.js');
  85. expect(html).toContain('../lib/design/style.data.js');
  86. });
  87. }
  88. });