archivo del paquete→ .shtukbase/packages/design-lenses/test/build.test.ts
test/build.test.ts
94 líneas, 4.1 KB.
El archivo tal como está en el paquete: el código y sus comentarios no se traducen.
- // The actions `build` and `check` on a copy of the fixture project; and the two lenses' headers.
- import { afterAll, describe, expect, test } from 'bun:test';
- import { cpSync, existsSync, 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';
- const PKG = join(dirname(fileURLToPath(import.meta.url)), '..');
- const FIX = join(PKG, 'test', 'fixtures', 'project');
- const dirs: string[] = [];
- const scratch = () => {
- const d = mkdtempSync(join(tmpdir(), 'design-lenses-'));
- dirs.push(d);
- cpSync(FIX, d, { recursive: true });
- return d;
- };
- const run = (action: string, cwd: string) => {
- const r = Bun.spawnSync([process.execPath, join(PKG, 'actions', `${action}.ts`)], { cwd });
- return { code: r.exitCode, out: r.stdout.toString() + r.stderr.toString() };
- };
- afterAll(() => { for (const d of dirs) rmSync(d, { recursive: true, force: true }); });
- describe('build and check', () => {
- test('build writes the data of the chosen style; check passes', () => {
- const d = scratch();
- expect(run('check', d).code).toBe(1); // missing before the first build
- const b = run('build', d);
- expect(b.code).toBe(0);
- const text = readFileSync(join(d, 'lib/design/style.data.js'), 'utf8');
- const box: { SB_STYLE?: any } = {};
- new Function('window', text)(box);
- const S = box.SB_STYLE;
- expect(S.version).toBe(1);
- expect(S.id).toBe('demo');
- expect(S.sections).toEqual([{ title: 'Principles', page: '/design/demo/principles.md' }]);
- expect(S.css).toEqual(['/design/demo/tokens.css']);
- expect(S.assets).toEqual(['/design/demo/assets/mark.svg']);
- expect(S.styles.map((s: { id: string }) => s.id)).toEqual(['demo']);
- expect(S.problems).toEqual([]);
- const bg = S.tokens.find((t: { name: string }) => t.name === '--background');
- expect(bg).toMatchObject({ group: 'Colors', light: '#ffffff', dark: '#191919', description: 'Page background' });
- for (const n of ['--chart-5', '--sidebar-ring', '--font-sans', '--shadow-lg', '--sb-glow', '--sb-hue-1', '--sb-dur']) {
- expect(S.tokens.some((t: { name: string }) => t.name === n)).toBe(true);
- }
- expect(run('check', d).code).toBe(0);
- });
- test('check fails when the style changed after the build', () => {
- const d = scratch();
- expect(run('build', d).code).toBe(0);
- const p = join(d, 'design/demo/tokens.json');
- writeFileSync(p, readFileSync(p, 'utf8').replace('"#e04a72"', '"#ff0000"'));
- const c = run('check', d);
- expect(c.code).toBe(1);
- expect(c.out).toContain('stale');
- });
- test('build fails with a reason when no style is chosen or it is not installed', () => {
- const d = scratch();
- writeFileSync(join(d, 'design/design.md'), '---\ntype: Page\ntitle: Design\nstyle: \n---\n');
- let b = run('build', d);
- expect(b.code).toBe(1);
- expect(b.out).toContain('no style is chosen');
- writeFileSync(join(d, 'design/design.md'), '---\ntype: Page\ntitle: Design\nstyle: nope\n---\n');
- b = run('build', d);
- expect(b.code).toBe(1);
- expect(b.out).toContain('not installed');
- expect(existsSync(join(d, 'lib/design/style.data.js'))).toBe(false);
- });
- test('a style with only a built tokens.css still gives a specimen', () => {
- const d = scratch();
- rmSync(join(d, 'design/demo/tokens.json'));
- rmSync(join(d, 'design/demo/tokens.dark.json'));
- expect(run('build', d).code).toBe(0);
- const text = readFileSync(join(d, 'lib/design/style.data.js'), 'utf8');
- expect(text).toContain('"--background"');
- expect(text).toContain('"#191919"');
- });
- });
- describe('the lenses', () => {
- for (const f of ['style.html', 'components.html']) {
- test(`${f} is a kit lens with a custom template`, () => {
- const html = readFileSync(join(PKG, 'pages', 'design', f), 'utf8');
- const h = lensHeader(html);
- expect(h?.cfg.type).toBe('Lens');
- expect(h?.cfg.template).toBe('custom');
- expect(html).toContain('../lib/kit.js');
- expect(html).toContain('../lib/design/style.data.js');
- });
- }
- });