ShtukbaseStandard
archivo del paquete→ .shtukbase/packages/shtukbase-icons/test/set.test.ts

test/set.test.ts

150 líneas, 8.5 KB.

El archivo tal como está en el paquete: el código y sus comentarios no se traducen.

  1. // The Shtukbase icon set as shipped: the registry is strictly clean, every drawing keeps the style, every style renders
  2. // every icon inside the contract and the budget, the Iconify JSON is fresh. Then the style check (SBI-R01) and the build
  3. // on scratch projects, each with a passing and a failing fixture.
  4. import { afterAll, describe, expect, test } from 'bun:test';
  5. import { cpSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
  6. import { tmpdir } from 'node:os';
  7. import { dirname, join } from 'node:path';
  8. import { fileURLToPath } from 'node:url';
  9. import { checkRegistry } from '../../icons/lib/registry.ts';
  10. import { validate } from '../../icons/lib/svg.ts';
  11. import { checkFlat } from '../lib/style-check.ts';
  12. import { ICONIFY, R, iconify, iconifyText, registry, renderIcon, setStyles, styleById } from '../lib/sbicons.ts';
  13. const PKG = join(dirname(fileURLToPath(import.meta.url)), '..');
  14. const PAGES = join(PKG, 'pages'); // the pages lie as they lie in a project, so the pages folder is a project root
  15. const dirs: string[] = [];
  16. afterAll(() => { for (const d of dirs) rmSync(d, { recursive: true, force: true }); });
  17. const read = (p: string) => { const a = join(PAGES, p.slice(1)); return existsSync(a) ? readFileSync(a, 'utf8') : null; };
  18. describe('the set as shipped', () => {
  19. const reg = registry(PAGES);
  20. const files = readdirSync(join(PAGES, 'design/shtukbase-icons/flat')).map((f) => `/design/shtukbase-icons/flat/${f}`);
  21. test('128 icons; the registry and the drawings agree with no error and no near duplicate', () => {
  22. expect(reg.icons.length).toBe(131);
  23. expect(files.length).toBe(131);
  24. expect(checkRegistry(reg, files, read)).toEqual([]);
  25. });
  26. test('every record has what it draws, where it fits, where not, aliases, a group and its origin', () => {
  27. for (const r of reg.icons) {
  28. expect(r.avoid, r.name).toBeTruthy();
  29. expect(r.aliases?.length, r.name).toBeGreaterThan(0);
  30. expect(r.group, r.name).toBeTruthy();
  31. expect(r.added?.from, r.name).toBeTruthy();
  32. }
  33. });
  34. test('the styles: 79 is the base, 79h a variant, nine reserves', () => {
  35. const st = setStyles(PAGES);
  36. expect(st.map((s) => s.id).sort()).toEqual(['65', '65h', '66', '66h', '71', '73', '76', '79', '79h', '81', '82']);
  37. expect(st.filter((s) => s.role === 'base').map((s) => s.id)).toEqual(['79']);
  38. expect(st.find((s) => s.id === '79h')?.role).toBe('variant');
  39. expect(styleById(PAGES, '79').render).toEqual({ outline: 'hueink', outlineWidth: 1.75, rim: 5, rimColor: '#FFFFFF', shadow: 0.13, scale: 0.88 });
  40. });
  41. const base = styleById(PAGES, '79');
  42. test('every drawing keeps the style (SBI-R01)', () => {
  43. const bad = reg.icons.map((r) => [r.name, checkFlat(read(r.file)!, base.rawPalette)] as const).filter(([, f]) => f.length);
  44. expect(bad).toEqual([]);
  45. });
  46. test('every icon in every style keeps the SVG contract and the 4096 budget; in 79 also the palette', () => {
  47. for (const st of setStyles(PAGES)) {
  48. for (const r of reg.icons) {
  49. const svg = renderIcon(PAGES, r, st);
  50. const errs = validate(svg, 4096, st.id === '79' ? base.rules : {}).filter((f) => f.level === 'error');
  51. expect(errs, `${st.id} ${r.name}`).toEqual([]);
  52. }
  53. }
  54. });
  55. test('no plate under any icon: no drawing starts with a rect over 70% of the canvas', () => {
  56. for (const r of reg.icons) {
  57. const e = R.parse(read(r.file)!).els[0];
  58. const a = (k: string) => Number(e.attrs.find(([n]) => n === k)?.[1] ?? 0);
  59. expect(e.tag === 'rect' && a('width') >= 67.2 && a('height') >= 67.2, r.name).toBe(false);
  60. }
  61. });
  62. test('the shipped Iconify JSON is the set in style 79', () => {
  63. expect(readFileSync(join(PAGES, ICONIFY.slice(1)), 'utf8')).toBe(iconifyText(iconify(PAGES, reg, base)));
  64. const j = JSON.parse(read(ICONIFY)!);
  65. expect(j.prefix).toBe('sb');
  66. expect(Object.keys(j.icons).length).toBe(131);
  67. expect(j.aliases.agents.parent).toBe('bot');
  68. });
  69. });
  70. // ---- the check and the build on scratch projects ----
  71. function scratch(): string {
  72. const d = mkdtempSync(join(tmpdir(), 'sb-icons-'));
  73. dirs.push(d);
  74. cpSync(PAGES, d, { recursive: true });
  75. return d;
  76. }
  77. const run = (file: string, cwd: string, ...args: string[]) => {
  78. const r = Bun.spawnSync([process.execPath, join(PKG, file), ...args], { cwd });
  79. return { code: r.exitCode, out: r.stdout.toString() + r.stderr.toString() };
  80. };
  81. describe('check style (SBI-R01)', () => {
  82. test('passing fixture: the set as shipped, then built', () => {
  83. const d = scratch();
  84. expect(run('checks/s01_style.ts', d)).toEqual({ code: 0, out: '' });
  85. expect(run('actions/build.ts', d).code).toBe(0);
  86. expect(run('checks/s01_style.ts', d)).toEqual({ code: 0, out: '' });
  87. });
  88. test('failing fixture: a plate, an outline of its own, a colour off the palette, a group; a rendered file edited by hand', () => {
  89. const d = scratch();
  90. const flat = join(d, 'design/shtukbase-icons/flat');
  91. writeFileSync(join(flat, 'package.svg'), '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 96 96">\n<rect x="8" y="8" width="80" height="80" rx="16" fill="#2383E2"/>\n<rect x="30" y="30" width="36" height="36" fill="#EF7F16" stroke="#7A3D02" stroke-width="2"/>\n<circle cx="48" cy="48" r="6" fill="#123456"/>\n</svg>\n');
  92. writeFileSync(join(flat, 'plus.svg'), '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 96 96"><g fill="#01A26F"><rect x="40" y="14" width="16" height="68"/></g></svg>\n');
  93. const r = run('checks/s01_style.ts', d);
  94. expect(r.code).toBe(1);
  95. expect(r.out).toContain('package.svg: a plate');
  96. expect(r.out).toContain('package.svg: <rect> has an outline of its own');
  97. expect(r.out).toContain('package.svg: colour #123456 is not in the palette');
  98. expect(r.out).toContain('plus.svg: a group');
  99. const d2 = scratch();
  100. expect(run('actions/build.ts', d2).code).toBe(0);
  101. writeFileSync(join(d2, 'assets/icons/set/x.svg'), '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 96 96"></svg>');
  102. const r2 = run('checks/s01_style.ts', d2);
  103. expect(r2.code).toBe(1);
  104. expect(r2.out).toContain('/assets/icons/set/x.svg: not the drawing in style 79');
  105. });
  106. test('checkFlat names each fault (pure)', () => {
  107. const pal = styleById(PAGES, '79').rawPalette;
  108. expect(checkFlat('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M1 1L5 5" stroke="#2383E2" stroke-width="1" fill="none"/></svg>', pal))
  109. .toEqual(['viewBox "0 0 24 24": the set draws on "0 0 96 96"', '<path> a line 1 wide: lines of less than 2 vanish at 16 px']);
  110. expect(checkFlat('<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 96 96" style="x"><rect x="1" y="1" width="9" height="9" fill="#2383E2"/></svg>', pal)[0]).toContain('a style attribute');
  111. });
  112. });
  113. describe('build', () => {
  114. test('renders the set into /assets/icons/set in the held style; --check fails before and passes after', () => {
  115. const d = scratch();
  116. expect(run('actions/build.ts', d, '--check').code).toBe(1);
  117. const r = run('actions/build.ts', d);
  118. expect(r.out).toContain('style 79');
  119. expect(readdirSync(join(d, 'assets/icons/set')).length).toBe(131);
  120. expect(run('actions/build.ts', d, '--check').code).toBe(0);
  121. });
  122. test('another style on request; an unknown style is refused', () => {
  123. const d = scratch();
  124. expect(run('actions/build.ts', d, '--style', '73', '--out', '/assets/icons/s73').code).toBe(0);
  125. expect(readFileSync(join(d, 'assets/icons/s73/check.svg'), 'utf8')).toContain('scale(0.9)');
  126. const bad = run('actions/build.ts', d, '--style', 'nope');
  127. expect(bad.code).toBe(2);
  128. expect(bad.out).toContain('no icon style "nope"');
  129. });
  130. });
  131. describe('import', () => {
  132. test('brings a drawing in: tile dropped, colours snapped; refuses a taken name', () => {
  133. const d = scratch();
  134. mkdirSync(join(d, 'in'), { recursive: true });
  135. writeFileSync(join(d, 'in/thing.svg'), '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 96 96"><rect x="12" y="12" width="72" height="72" rx="14" fill="#4DAB9A"/><circle cx="48" cy="48" r="20" fill="#FFDC49"/></svg>');
  136. const r = run('actions/import.ts', d, 'in/thing.svg', '--name', 'thing', '--write');
  137. expect(r.code).toBe(0);
  138. expect(r.out).toContain('app-tile background dropped');
  139. expect(r.out).toContain('#FFDC49 → #F0AD15 (amber)');
  140. const svg = readFileSync(join(d, 'design/shtukbase-icons/flat/thing.svg'), 'utf8');
  141. expect(checkFlat(svg, styleById(d, '79').rawPalette)).toEqual([]);
  142. expect(run('actions/import.ts', d, 'in/thing.svg', '--name', 'bot').out).toContain('the name "bot" is taken');
  143. });
  144. });