Skip to content

An open source design system that's fully customizable and agent ready

TypeScript
4.7 KB
Raw Blame
// Copyright (c) Meta Platforms, Inc. and affiliates.

/**
 * @file Direct-API tests for `themeBuild`'s `iconsSpecifier` option (#4620).
 *
 * The CLI surface of `--icons-specifier` is pinned in
 * clients/cli/commands/build-theme.icons-specifier.test.mjs; these tests pin
 * the programmatic surface that watch mode, editor tooling, and build scripts
 * call directly: the option reaches the emitted module, its absence leaves the
 * scraped specifier byte-for-byte alone, and `check` mode compares the
 * specifier-bearing text like any other generated byte — outputs built with a
 * different specifier than the one being checked against are stale, not clean.
 */

import {describe, it, expect, beforeEach, afterEach} from 'vitest';
import * as fs from 'node:fs';
import * as path from 'node:path';
import * as os from 'node:os';
import {themeBuild} from './build.mjs';

let tmpDir;
beforeEach(() => {
  tmpDir = fs.mkdtempSync(
    path.join(os.tmpdir(), 'astryx-api-icons-specifier-'),
  );
});
afterEach(() => {
  fs.rmSync(tmpDir, {recursive: true, force: true});
});

/**
 * Write a theme source (plus a loadable icons source beside it) under
 * `<tmpDir>/src/`. The registry is a plain object — the emit path only
 * re-exports it, so no React is needed.
 */
function writeIconTheme({withIcons = true, name = 'icotheme'} = {}) {
  const srcDir = path.join(tmpDir, 'src');
  fs.mkdirSync(srcDir, {recursive: true});
  fs.writeFileSync(
    path.join(srcDir, 'icons.ts'),
    `export const myIcons = { close: 'x' };\n`,
  );
  const iconLines = withIcons ? [`import { myIcons } from './icons';`] : [];
  fs.writeFileSync(
    path.join(srcDir, `${name}.ts`),
    [
      ...iconLines,
      `export default { name: '${name}', tokens: { '--color-bg': '#fff' }${
        withIcons ? ', icons: myIcons' : ''
      } };`,
      '',
    ].join('\n'),
  );
  return `src/${name}.ts`;
}

function builtModule(name = 'icotheme') {
  return fs.readFileSync(path.join(tmpDir, 'dist', `${name}.js`), 'utf8');
}

describe('themeBuild({iconsSpecifier}) — direct API', () => {
  it('emits the declared specifier into the generated module', async () => {
    const file = writeIconTheme();

    const result = await themeBuild(
      file,
      {out: 'dist/theme.css', iconsSpecifier: './icons.mjs'},
      {cwd: tmpDir},
    );

    expect(result?.type).toBe('theme.build');
    const js = builtModule();
    expect(js).toContain(`import { myIcons } from "./icons.mjs";`);
    // The registry re-export survives the override.
    expect(js).toContain('export { myIcons }');
  });

  it('emits the scraped specifier unchanged when the option is omitted', async () => {
    const file = writeIconTheme();

    const result = await themeBuild(
      file,
      {out: 'dist/theme.css'},
      {cwd: tmpDir},
    );

    expect(result?.type).toBe('theme.build');
    expect(builtModule()).toContain(`import { myIcons } from './icons';`);
  });

  it('is inert for a theme with no icons field', async () => {
    const file = writeIconTheme({withIcons: false});

    const result = await themeBuild(
      file,
      {out: 'dist/theme.css', iconsSpecifier: './icons.mjs'},
      {cwd: tmpDir},
    );

    expect(result?.type).toBe('theme.build');
    // The header comment's usage example mentions imports; only a real
    // statement (line-leading `import`) would be a leak.
    const js = builtModule();
    expect(js).not.toMatch(/^import /m);
    expect(js).not.toContain('icons.mjs');
  });

  it('check mode is clean against outputs built with the same specifier', async () => {
    const file = writeIconTheme();
    await themeBuild(
      file,
      {out: 'dist/theme.css', iconsSpecifier: './icons.mjs'},
      {cwd: tmpDir},
    );

    const result = await themeBuild(
      file,
      {out: 'dist/theme.css', check: true, iconsSpecifier: './icons.mjs'},
      {cwd: tmpDir},
    );

    expect(result?.type).toBe('theme.build.check');
    expect(result?.data.upToDate).toBe(true);
    expect(result?.data.stale).toEqual([]);
  });

  it('check mode reports outputs stale when the specifier differs', async () => {
    const file = writeIconTheme();
    await themeBuild(
      file,
      {out: 'dist/theme.css', iconsSpecifier: './icons.mjs'},
      {cwd: tmpDir},
    );

    // Checking without the option regenerates with the scraped './icons' —
    // different bytes than the on-disk module, so the check must flag it.
    const result = await themeBuild(
      file,
      {out: 'dist/theme.css', check: true},
      {cwd: tmpDir},
    );

    expect(result?.type).toBe('theme.build.check');
    expect(result?.data.upToDate).toBe(false);
    expect(result?.data.stale.some(entry => entry.reason === 'outdated')).toBe(
      true,
    );
  });
});