package file→ .shtukbase/packages/shtukbase-ui-react/test/place.test.ts
test/place.test.ts
154 lines, 7.3 KB.
- // The `place` action and the three checks on a scratch project: the library lands in the folder of the config page, an
- // update keeps what the project edited, and each check has a passing and a failing fixture.
- import { afterEach, beforeEach, describe, expect, test } from 'bun:test';
- import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
- import { tmpdir } from 'node:os';
- import { dirname, join } from 'node:path';
- import { check as same } from '../checks/same.ts';
- import { check as states } from '../checks/states.ts';
- import { check as library } from '../checks/library.ts';
- import { HTML_COMPONENTS, PKG } from './lib.ts';
- import { files, MANIFEST } from '../actions/kitlib.ts';
- let root: string;
- beforeEach(() => {
- root = mkdtempSync(join(tmpdir(), 'sb-ui-react-'));
- });
- afterEach(() => rmSync(root, { recursive: true, force: true }));
- const put = (rel: string, text: string) => {
- mkdirSync(dirname(join(root, rel)), { recursive: true });
- writeFileSync(join(root, rel), text);
- };
- const place = (...args: string[]) => {
- const p = Bun.spawnSync(['bun', join(PKG, 'actions', 'place.ts'), ...args], { cwd: root, stdout: 'pipe', stderr: 'pipe' });
- if (p.exitCode !== 0) throw new Error(`place failed: ${p.stderr.toString()}${p.stdout.toString()}`);
- return p.stdout.toString();
- };
- /** A project that has the HTML library's CSS (what `@import` finds) and, when the package is there, its components. */
- const withHtml = () => {
- put('design/shtukbase-ui-html/ui.css', '.button{}\n');
- if (existsSync(HTML_COMPONENTS)) for (const f of files(HTML_COMPONENTS)) put(`design/shtukbase-ui-html/components/${f}`, readFileSync(join(HTML_COMPONENTS, f), 'utf8'));
- };
- describe('place', () => {
- test('lays the library in the default folder, every file and a manifest', () => {
- withHtml();
- const out = place();
- expect(out).toContain('src/sb-ui/');
- const man = JSON.parse(readFileSync(join(root, 'src/sb-ui', MANIFEST), 'utf8'));
- expect(Object.keys(man.files)).toContain('primitives/button.tsx');
- expect(Object.keys(man.files)).toContain('styles/index.css');
- for (const rel of Object.keys(man.files)) expect(existsSync(join(root, 'src/sb-ui', rel)), rel).toBe(true);
- });
- test('the CSS import points at the project\'s ui.css, from the folder of the config page', () => {
- put('design/shtukbase-ui-react.md', '---\ntype: Page\ntarget: app/ui\ncss: design/shtukbase-ui-html/ui.css\n---\n');
- withHtml();
- place();
- expect(readFileSync(join(root, 'app/ui/styles/index.css'), 'utf8')).toContain('@import "../../../design/shtukbase-ui-html/ui.css";');
- });
- test('a second run changes nothing; an edited file is kept, --force replaces it', () => {
- withHtml();
- place();
- const edited = join(root, 'src/sb-ui/primitives/badge.tsx');
- writeFileSync(edited, `${readFileSync(edited, 'utf8')}// mine\n`);
- expect(place()).toContain('kept 1');
- expect(readFileSync(edited, 'utf8')).toContain('// mine');
- expect(place('--force')).toContain('written 1');
- expect(readFileSync(edited, 'utf8')).not.toContain('// mine');
- });
- });
- describe('SBR-R01 library', () => {
- test('passes on a placed library', () => {
- withHtml();
- place();
- expect(library(root)).toEqual([]);
- });
- test('fails on a missing file, a broken import and a missing CSS', () => {
- withHtml();
- place();
- rmSync(join(root, 'src/sb-ui/lib/controls.ts'));
- const out = library(root).join('\n');
- expect(out).toContain('lib/controls.ts: library file is missing');
- expect(out).toContain('import "../lib/controls" finds no file');
- rmSync(join(root, 'design/shtukbase-ui-html/ui.css'));
- expect(library(root).join('\n')).toContain('@import');
- });
- test('says that the library is not placed', () => {
- expect(library(root).join('\n')).toContain('the library is not placed');
- });
- });
- describe('SBR-R02 states', () => {
- test('passes on a placed library', () => {
- withHtml();
- place();
- expect(states(root)).toEqual([]);
- });
- test('fails on a component with no states file and on a states file with no meta', () => {
- withHtml();
- place();
- rmSync(join(root, 'src/sb-ui/primitives/kicker.states.tsx'));
- writeFileSync(join(root, 'src/sb-ui/primitives/chip.states.tsx'), 'export const nothing = 1;\n');
- const out = states(root).join('\n');
- expect(out).toContain('primitives/kicker.tsx: no states file');
- expect(out).toContain('primitives/chip.states.tsx: the states file has no export const meta');
- });
- });
- describe('SBR-R03 same components as the HTML library', () => {
- test('passes on a placed library next to the HTML library', () => {
- withHtml();
- place();
- expect(same(root)).toEqual([]);
- });
- test('fails on an id that is not the file name, a level in the wrong folder, a header with problems', () => {
- withHtml();
- place();
- const p = (rel: string, f: (t: string) => string) => writeFileSync(join(root, 'src/sb-ui', rel), f(readFileSync(join(root, 'src/sb-ui', rel), 'utf8')));
- p('primitives/kicker.tsx', (t) => t.replace('id: kicker', 'id: kickerx'));
- p('primitives/chip.tsx', (t) => t.replace('level: primitive', 'level: composite'));
- p('primitives/badge.tsx', (t) => t.replace('level: primitive', 'level: nonsense'));
- const out = same(root).join('\n');
- expect(out).toContain('the header id is "kickerx", the file is "kicker"');
- expect(out).toContain('level "composite" belongs in composites/, not primitives/');
- expect(out).toContain('level "nonsense" is not one of');
- });
- test.skipIf(!existsSync(HTML_COMPONENTS))('fails when the headers of the two libraries differ or a component is missing', () => {
- withHtml();
- place();
- const f = join(root, 'src/sb-ui/primitives/button.tsx');
- writeFileSync(f, readFileSync(f, 'utf8').replace('options: [sm, default, lg, icon]', 'options: [sm, default, lg]'));
- rmSync(join(root, 'src/sb-ui/blocks/area.tsx'));
- const out = same(root).join('\n');
- expect(out).toContain('button.tsx: header controls differs from the HTML library');
- expect(out).toContain('the HTML library has "area", the React library has not');
- });
- test('without an HTML library there is nothing to compare, the headers are still checked', () => {
- put('design/shtukbase-ui-html/ui.css', '.button{}\n');
- place();
- expect(same(root)).toEqual([]);
- });
- });
- describe('with design-components', () => {
- const DC = join(PKG, '..', 'design-components');
- test.skipIf(!existsSync(HTML_COMPONENTS) || !existsSync(join(DC, 'actions', 'sync.ts')))(
- 'place lists the target in sources: of the collection, and sync joins the React files to the HTML ones',
- () => {
- withHtml();
- put('design/components.md', readFileSync(join(DC, 'pages', 'design', 'components.md'), 'utf8'));
- expect(place()).toContain('added src/sb-ui to sources:');
- expect(readFileSync(join(root, 'design/components.md'), 'utf8')).toContain('sources: [src/sb-ui]');
- expect(place()).not.toContain('added src/sb-ui');
- const p = Bun.spawnSync(['bun', join(DC, 'actions', 'sync.ts')], { cwd: root, stdout: 'pipe', stderr: 'pipe' });
- expect(p.exitCode, p.stderr.toString() + p.stdout.toString()).toBe(0);
- expect(p.stdout.toString()).toMatch(/(\d+) components \(\1 html, \1 react\)/);
- expect(readFileSync(join(root, 'design/components/package-card.md'), 'utf8')).toContain('src/sb-ui/composites/package-card.tsx');
- },
- );
- });