файл пакета→ .shtukbase/packages/icons/test/registry.test.ts
test/registry.test.ts
131 строка, 8.5 КБ.
Файл как есть в пакете: код и комментарии в нём не переводятся.
- // The icon registry (ICN-R04), the IconStyle roles, the icon lens data and its header: pure parts and the actions on
- // scratch projects, each check with a passing and a failing fixture.
- import { afterAll, describe, expect, test } from 'bun:test';
- import { mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
- import { tmpdir } from 'node:os';
- import { dirname, join } from 'node:path';
- import { fileURLToPath } from 'node:url';
- import { lensHeader } from '../../lenses/kit/src/build.js';
- import { iconStyle, iconStyles, settings } from '../lib/iconlib.ts';
- import { lensData } from '../lib/lensdata.ts';
- import { type Registry, checkRegistry, search, shapeSignature } from '../lib/registry.ts';
- const PKG = join(dirname(fileURLToPath(import.meta.url)), '..');
- const dirs: string[] = [];
- afterAll(() => { for (const d of dirs) rmSync(d, { recursive: true, force: true }); });
- const BOX = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 96 96">\n<rect x="16" y="36" width="64" height="48" rx="6" fill="#EF7F16"/>\n</svg>\n';
- const DOT = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 96 96">\n<circle cx="48" cy="48" r="30" fill="#2383E2"/>\n</svg>\n';
- const rec = (name: string, extra: Record<string, unknown> = {}) => ({ name, file: `/design/demo/flat/${name}.svg`, description: `a ${name}`, use: 'tests', aliases: [`${name}-word`], ...extra });
- const STYLE = (role: string, id: string) => `---\ntype: IconStyle\ntitle: "${id}"\nstyleId: "${id}"\nrole: ${role}\nset: /design/demo/registry.json\nviewBox: "0 0 96 96"\npalette:\n tangerine: ["#EF7F16", "#7A3D02", "#FBBA8F"]\n blue: ["#2383E2", "#12508E", "#A5CEFF"]\n white: "#FFFFFF"\n shadow: "#000000"\nrender: {outline: hueink, outlineWidth: 1.75, rim: 5, rimColor: "#FFFFFF", shadow: 0.13, scale: 0.88}\n---\n`;
- function project(reg: Registry, files: Record<string, string>): string {
- const d = mkdtempSync(join(tmpdir(), 'icons-reg-'));
- dirs.push(d);
- mkdirSync(join(d, 'design/demo/flat'), { recursive: true });
- mkdirSync(join(d, 'design/demo/styles'), { recursive: true });
- writeFileSync(join(d, 'design/demo/registry.json'), JSON.stringify(reg));
- for (const [n, t] of Object.entries(files)) writeFileSync(join(d, 'design/demo/flat', n), t);
- writeFileSync(join(d, 'design/demo/styles/a-reserve.md'), STYLE('reserve', 'r1'));
- writeFileSync(join(d, 'design/demo/styles/b-base.md'), STYLE('base', 'b1'));
- 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() };
- };
- const GOOD: Registry = { version: 1, set: 'demo', prefix: 'dm', icons: [rec('package'), rec('dot')] };
- describe('checkRegistry (pure)', () => {
- const read = (files: Record<string, string>) => (p: string) => files[p.split('/').pop()!] ?? null;
- test('passes: every file has a record, every record a file, words unique', () => {
- expect(checkRegistry(GOOD, ['/design/demo/flat/package.svg', '/design/demo/flat/dot.svg'], read({ 'package.svg': BOX, 'dot.svg': DOT }))).toEqual([]);
- });
- test('fails: a file without a record, a record without a file, a shared name and alias, no description or use, a bad name', () => {
- const reg: Registry = { icons: [rec('package'), rec('package'), rec('ghost'), rec('Bad_Name'), { ...rec('dot'), aliases: ['package-word'], description: '', use: '' }] };
- const f = checkRegistry(reg, ['/design/demo/flat/package.svg', '/design/demo/flat/dot.svg', '/design/demo/flat/stray.svg'], read({ 'package.svg': BOX, 'dot.svg': DOT, 'stray.svg': DOT })).map((x) => x.text).join('\n');
- expect(f).toContain('the name "package" is used by 2 records');
- expect(f).toContain('"ghost": the file /design/demo/flat/ghost.svg does not exist');
- expect(f).toContain('stray.svg has no record');
- expect(f).toContain('the alias "package-word"');
- expect(f).toContain('"dot": no description');
- expect(f).toContain('"dot": no use');
- expect(f).toContain('"Bad_Name": the name is not kebab-case');
- });
- test('a near duplicate is a warning: the same shapes in other colours', () => {
- const recolour = BOX.replace('#EF7F16', '#2383E2');
- const f = checkRegistry({ icons: [rec('package'), rec('crate')] }, [], read({ 'package.svg': BOX, 'crate.svg': recolour }));
- expect(f).toEqual([{ level: 'warn', text: expect.stringContaining('"crate" draws the same shapes as "package"') }]);
- expect(shapeSignature(BOX)).toBe(shapeSignature(recolour));
- expect(shapeSignature(BOX)).not.toBe(shapeSignature(DOT));
- });
- test('search by meaning: name, alias, description', () => {
- const reg: Registry = { icons: [{ ...rec('search'), aliases: ['find', 'magnifier'] }, { ...rec('bot'), description: 'a robot head', aliases: ['agent'] }] };
- expect(search(reg, 'find things')[0].rec.name).toBe('search');
- expect(search(reg, 'agents')[0].rec.name).toBe('bot');
- expect(search(reg, 'robot')[0].rec.name).toBe('bot');
- expect(search(reg, 'zebra')).toEqual([]);
- });
- });
- describe('check registry (ICN-R04, warn) on a project', () => {
- test('passing fixture: the health line says full', () => {
- const d = project(GOOD, { 'package.svg': BOX, 'dot.svg': DOT });
- const r = run('checks/i04_registry.ts', d);
- expect(r.code).toBe(0);
- expect(r.out.trim()).toBe('health icon-set/demo: full');
- });
- test('failing fixture: missing and extra lines, never a failure; in enforce mode it fails', () => {
- const d = project({ ...GOOD, icons: [...GOOD.icons, rec('crate')] }, { 'package.svg': BOX, 'crate.svg': BOX.replace('#EF7F16', '#2383E2'), 'stray.svg': DOT });
- const r = run('checks/i04_registry.ts', d);
- expect(r.code).toBe(0);
- expect(r.out).toContain('health icon-set/demo: missing: 2 (');
- expect(r.out).toContain(' /design/demo/registry.json: "dot": the file');
- expect(r.out).toContain('stray.svg has no record');
- expect(r.out).toContain('health icon-set/demo: extra: 1 near duplicate(s)');
- expect(r.out).toContain('"crate" draws the same shapes as "package"');
- mkdirSync(join(d, 'norms/rules/icons'), { recursive: true });
- writeFileSync(join(d, 'norms/rules/icons/ICN-R04.md'), '---\ntype: Rule\nruleId: ICN-R04\nstatus: active\nmode: enforce\n---\n');
- expect(run('checks/i04_registry.ts', d).code).toBe(1);
- });
- });
- describe('IconStyle roles and the lens data', () => {
- test('the held style is the base one, not the first page found; iconStyles puts it first', () => {
- const d = project(GOOD, { 'package.svg': BOX, 'dot.svg': DOT });
- const s = settings(d);
- expect(iconStyle(d, s)?.id).toBe('b1');
- expect(iconStyles(d, s).map((x) => x.id)).toEqual(['b1', 'r1']);
- expect(iconStyle(d, s)?.render).toEqual({ outline: 'hueink', outlineWidth: 1.75, rim: 5, rimColor: '#FFFFFF', shadow: 0.13, scale: 0.88 });
- });
- test('lens data: the set with its drawings, the styles, the held one', () => {
- const d = project(GOOD, { 'package.svg': BOX, 'dot.svg': DOT });
- const data = lensData(d);
- expect(data.style).toBe('b1');
- expect(data.sets[0].icons.map((i) => i.name)).toEqual(['package', 'dot']);
- expect(data.sets[0].icons[0].svg).toBe(BOX);
- expect(data.errors).toEqual([]);
- });
- test('the action lens writes the data and the renderer; --check passes when fresh, fails when stale', () => {
- const d = project(GOOD, { 'package.svg': BOX, 'dot.svg': DOT });
- expect(run('actions/lens.ts', d, '--check').code).toBe(1);
- expect(run('actions/lens.ts', d).code).toBe(0);
- expect(readFileSync(join(d, 'lib/design/icons.data.js'), 'utf8')).toContain('window.SB_ICONS = {');
- expect(readFileSync(join(d, 'lib/design/icon-render.js'), 'utf8')).toBe(readFileSync(join(PKG, 'lib/render.js'), 'utf8'));
- expect(run('actions/lens.ts', d, '--check').code).toBe(0);
- writeFileSync(join(d, 'design/demo/flat/dot.svg'), BOX);
- expect(run('actions/lens.ts', d, '--check').code).toBe(1);
- });
- test('the renderer runs in a page: it sets window.SBIconRender without module', () => {
- const src = readFileSync(join(PKG, 'lib/render.js'), 'utf8');
- const win: Record<string, unknown> = {};
- new Function('globalThis', 'module', src)(win, undefined);
- expect(typeof (win.SBIconRender as { render: unknown }).render).toBe('function');
- });
- test('the lens header is a lens of template custom', () => {
- const h = lensHeader(readFileSync(join(PKG, 'pages/design/icons.html'), 'utf8'));
- expect(h?.cfg.type).toBe('Lens');
- expect(h?.cfg.template).toBe('custom');
- expect(h?.about).toContain('?icon=<name>');
- });
- });