файл пакета→ .shtukbase/packages/shtukbase-icons/test/set.test.ts
test/set.test.ts
150 строк, 8.5 КБ.
Файл как есть в пакете: код и комментарии в нём не переводятся.
- // The Shtukbase icon set as shipped: the registry is strictly clean, every drawing keeps the style, every style renders
- // every icon inside the contract and the budget, the Iconify JSON is fresh. Then the style check (SBI-R01) and the build
- // on scratch projects, each with a passing and a failing fixture.
- import { afterAll, describe, expect, test } from 'bun:test';
- import { cpSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
- import { tmpdir } from 'node:os';
- import { dirname, join } from 'node:path';
- import { fileURLToPath } from 'node:url';
- import { checkRegistry } from '../../icons/lib/registry.ts';
- import { validate } from '../../icons/lib/svg.ts';
- import { checkFlat } from '../lib/style-check.ts';
- import { ICONIFY, R, iconify, iconifyText, registry, renderIcon, setStyles, styleById } from '../lib/sbicons.ts';
- const PKG = join(dirname(fileURLToPath(import.meta.url)), '..');
- const PAGES = join(PKG, 'pages'); // the pages lie as they lie in a project, so the pages folder is a project root
- const dirs: string[] = [];
- afterAll(() => { for (const d of dirs) rmSync(d, { recursive: true, force: true }); });
- const read = (p: string) => { const a = join(PAGES, p.slice(1)); return existsSync(a) ? readFileSync(a, 'utf8') : null; };
- describe('the set as shipped', () => {
- const reg = registry(PAGES);
- const files = readdirSync(join(PAGES, 'design/shtukbase-icons/flat')).map((f) => `/design/shtukbase-icons/flat/${f}`);
- test('128 icons; the registry and the drawings agree with no error and no near duplicate', () => {
- expect(reg.icons.length).toBe(131);
- expect(files.length).toBe(131);
- expect(checkRegistry(reg, files, read)).toEqual([]);
- });
- test('every record has what it draws, where it fits, where not, aliases, a group and its origin', () => {
- for (const r of reg.icons) {
- expect(r.avoid, r.name).toBeTruthy();
- expect(r.aliases?.length, r.name).toBeGreaterThan(0);
- expect(r.group, r.name).toBeTruthy();
- expect(r.added?.from, r.name).toBeTruthy();
- }
- });
- test('the styles: 79 is the base, 79h a variant, nine reserves', () => {
- const st = setStyles(PAGES);
- expect(st.map((s) => s.id).sort()).toEqual(['65', '65h', '66', '66h', '71', '73', '76', '79', '79h', '81', '82']);
- expect(st.filter((s) => s.role === 'base').map((s) => s.id)).toEqual(['79']);
- expect(st.find((s) => s.id === '79h')?.role).toBe('variant');
- expect(styleById(PAGES, '79').render).toEqual({ outline: 'hueink', outlineWidth: 1.75, rim: 5, rimColor: '#FFFFFF', shadow: 0.13, scale: 0.88 });
- });
- const base = styleById(PAGES, '79');
- test('every drawing keeps the style (SBI-R01)', () => {
- const bad = reg.icons.map((r) => [r.name, checkFlat(read(r.file)!, base.rawPalette)] as const).filter(([, f]) => f.length);
- expect(bad).toEqual([]);
- });
- test('every icon in every style keeps the SVG contract and the 4096 budget; in 79 also the palette', () => {
- for (const st of setStyles(PAGES)) {
- for (const r of reg.icons) {
- const svg = renderIcon(PAGES, r, st);
- const errs = validate(svg, 4096, st.id === '79' ? base.rules : {}).filter((f) => f.level === 'error');
- expect(errs, `${st.id} ${r.name}`).toEqual([]);
- }
- }
- });
- test('no plate under any icon: no drawing starts with a rect over 70% of the canvas', () => {
- for (const r of reg.icons) {
- const e = R.parse(read(r.file)!).els[0];
- const a = (k: string) => Number(e.attrs.find(([n]) => n === k)?.[1] ?? 0);
- expect(e.tag === 'rect' && a('width') >= 67.2 && a('height') >= 67.2, r.name).toBe(false);
- }
- });
- test('the shipped Iconify JSON is the set in style 79', () => {
- expect(readFileSync(join(PAGES, ICONIFY.slice(1)), 'utf8')).toBe(iconifyText(iconify(PAGES, reg, base)));
- const j = JSON.parse(read(ICONIFY)!);
- expect(j.prefix).toBe('sb');
- expect(Object.keys(j.icons).length).toBe(131);
- expect(j.aliases.agents.parent).toBe('bot');
- });
- });
- // ---- the check and the build on scratch projects ----
- function scratch(): string {
- const d = mkdtempSync(join(tmpdir(), 'sb-icons-'));
- dirs.push(d);
- cpSync(PAGES, d, { recursive: true });
- return d;
- }
- const run = (file: string, cwd: string, ...args: string[]) => {
- const r = Bun.spawnSync([process.execPath, join(PKG, file), ...args], { cwd });
- return { code: r.exitCode, out: r.stdout.toString() + r.stderr.toString() };
- };
- describe('check style (SBI-R01)', () => {
- test('passing fixture: the set as shipped, then built', () => {
- const d = scratch();
- expect(run('checks/s01_style.ts', d)).toEqual({ code: 0, out: '' });
- expect(run('actions/build.ts', d).code).toBe(0);
- expect(run('checks/s01_style.ts', d)).toEqual({ code: 0, out: '' });
- });
- test('failing fixture: a plate, an outline of its own, a colour off the palette, a group; a rendered file edited by hand', () => {
- const d = scratch();
- const flat = join(d, 'design/shtukbase-icons/flat');
- 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');
- 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');
- const r = run('checks/s01_style.ts', d);
- expect(r.code).toBe(1);
- expect(r.out).toContain('package.svg: a plate');
- expect(r.out).toContain('package.svg: <rect> has an outline of its own');
- expect(r.out).toContain('package.svg: colour #123456 is not in the palette');
- expect(r.out).toContain('plus.svg: a group');
- const d2 = scratch();
- expect(run('actions/build.ts', d2).code).toBe(0);
- writeFileSync(join(d2, 'assets/icons/set/x.svg'), '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 96 96"></svg>');
- const r2 = run('checks/s01_style.ts', d2);
- expect(r2.code).toBe(1);
- expect(r2.out).toContain('/assets/icons/set/x.svg: not the drawing in style 79');
- });
- test('checkFlat names each fault (pure)', () => {
- const pal = styleById(PAGES, '79').rawPalette;
- 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))
- .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']);
- 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');
- });
- });
- describe('build', () => {
- test('renders the set into /assets/icons/set in the held style; --check fails before and passes after', () => {
- const d = scratch();
- expect(run('actions/build.ts', d, '--check').code).toBe(1);
- const r = run('actions/build.ts', d);
- expect(r.out).toContain('style 79');
- expect(readdirSync(join(d, 'assets/icons/set')).length).toBe(131);
- expect(run('actions/build.ts', d, '--check').code).toBe(0);
- });
- test('another style on request; an unknown style is refused', () => {
- const d = scratch();
- expect(run('actions/build.ts', d, '--style', '73', '--out', '/assets/icons/s73').code).toBe(0);
- expect(readFileSync(join(d, 'assets/icons/s73/check.svg'), 'utf8')).toContain('scale(0.9)');
- const bad = run('actions/build.ts', d, '--style', 'nope');
- expect(bad.code).toBe(2);
- expect(bad.out).toContain('no icon style "nope"');
- });
- });
- describe('import', () => {
- test('brings a drawing in: tile dropped, colours snapped; refuses a taken name', () => {
- const d = scratch();
- mkdirSync(join(d, 'in'), { recursive: true });
- 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>');
- const r = run('actions/import.ts', d, 'in/thing.svg', '--name', 'thing', '--write');
- expect(r.code).toBe(0);
- expect(r.out).toContain('app-tile background dropped');
- expect(r.out).toContain('#FFDC49 → #F0AD15 (amber)');
- const svg = readFileSync(join(d, 'design/shtukbase-icons/flat/thing.svg'), 'utf8');
- expect(checkFlat(svg, styleById(d, '79').rawPalette)).toEqual([]);
- expect(run('actions/import.ts', d, 'in/thing.svg', '--name', 'bot').out).toContain('the name "bot" is taken');
- });
- });