Skip to content

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

TypeScript

Add unit tests for @xds/cli package (44 tests across 7 files) (#111)

Cindy Zhangc4dc29f74706
diff --git a/packages/cli/src/commands/agent-docs.mjs b/packages/cli/src/commands/agent-docs.mjs
index 36a6f545bf..bb2ae32b28 100644
--- a/packages/cli/src/commands/agent-docs.mjs
+++ b/packages/cli/src/commands/agent-docs.mjs
@@ -19,7 +19,7 @@ const XDS_MARKER_END = '<!-- XDS:END -->';
* Compressed index for AGENTS.md.
* Directs agents to use CLI commands for retrieval.
*/
-function generateCompressedIndex(version) {
+export function generateCompressedIndex(version) {
return `${XDS_MARKER_START}
[XDS v${version}]|IMPORTANT: Prefer retrieval-led reasoning. Run CLI to read docs before generating code.
|npx xds component <Name> --compact|--source Docs (props, usage) or source code
@@ -35,7 +35,7 @@ ${XDS_MARKER_END}`;
/**
* Get XDS version from core package.
*/
-function getXdsVersion(coreDir) {
+export function getXdsVersion(coreDir) {
if (coreDir) {
const pkgPath = path.join(coreDir, 'package.json');
if (fs.existsSync(pkgPath)) {
@@ -51,7 +51,7 @@ function getXdsVersion(coreDir) {
/**
* Inject or update XDS section in AGENTS.md.
*/
-function injectAgentsMd(targetDir, version) {
+export function injectAgentsMd(targetDir, version) {
const agentsPath = path.join(targetDir, AGENTS_MD);
const compressedIndex = generateCompressedIndex(version);
@@ -86,7 +86,7 @@ ${compressedIndex}
/**
* Remove XDS section from AGENTS.md.
*/
-function removeAgentDocs(targetDir) {
+export function removeAgentDocs(targetDir) {
const agentsPath = path.join(targetDir, AGENTS_MD);
if (fs.existsSync(agentsPath)) {
diff --git a/packages/cli/src/commands/agent-docs.test.mjs b/packages/cli/src/commands/agent-docs.test.mjs
new file mode 100644
index 0000000000..ba9a24d597
--- /dev/null
+++ b/packages/cli/src/commands/agent-docs.test.mjs
@@ -0,0 +1,134 @@
+import {describe, it, expect, beforeEach, afterEach, vi} from 'vitest';
+import * as fs from 'node:fs';
+import * as path from 'node:path';
+import * as os from 'node:os';
+import {
+ generateCompressedIndex,
+ getXdsVersion,
+ injectAgentsMd,
+ removeAgentDocs,
+} from './agent-docs.mjs';
+
+let tmpDir;
+
+beforeEach(() => {
+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'xds-agent-docs-test-'));
+});
+
+afterEach(() => {
+ fs.rmSync(tmpDir, {recursive: true, force: true});
+ vi.restoreAllMocks();
+});
+
+describe('generateCompressedIndex', () => {
+ it('includes the version number', () => {
+ const result = generateCompressedIndex('1.2.3');
+ expect(result).toContain('[XDS v1.2.3]');
+ expect(result).toContain('<!-- XDS:START -->');
+ expect(result).toContain('<!-- XDS:END -->');
+ });
+});
+
+describe('getXdsVersion', () => {
+ it('reads version from core package.json', () => {
+ const coreDir = path.join(tmpDir, 'core');
+ fs.mkdirSync(coreDir, {recursive: true});
+ fs.writeFileSync(
+ path.join(coreDir, 'package.json'),
+ JSON.stringify({version: '3.4.5'}),
+ );
+
+ expect(getXdsVersion(coreDir)).toBe('3.4.5');
+ });
+});
+
+describe('injectAgentsMd', () => {
+ it('creates new AGENTS.md when none exists', () => {
+ injectAgentsMd(tmpDir, '1.0.0');
+
+ const content = fs.readFileSync(path.join(tmpDir, 'AGENTS.md'), 'utf-8');
+ expect(content).toContain('# AGENTS.md');
+ expect(content).toContain('<!-- XDS:START -->');
+ expect(content).toContain('[XDS v1.0.0]');
+ expect(content).toContain('<!-- XDS:END -->');
+ });
+
+ it('updates existing AGENTS.md by replacing XDS markers', () => {
+ const existing = `# My Project
+
+Some content.
+
+<!-- XDS:START -->
+old content
+<!-- XDS:END -->
+
+More stuff.
+`;
+ fs.writeFileSync(path.join(tmpDir, 'AGENTS.md'), existing);
+
+ injectAgentsMd(tmpDir, '2.0.0');
+
+ const content = fs.readFileSync(path.join(tmpDir, 'AGENTS.md'), 'utf-8');
+ expect(content).toContain('[XDS v2.0.0]');
+ expect(content).not.toContain('old content');
+ expect(content).toContain('Some content.');
+ expect(content).toContain('More stuff.');
+ });
+
+ it('appends to existing AGENTS.md without markers', () => {
+ const existing = `# My Project
+
+Existing agent docs.
+`;
+ fs.writeFileSync(path.join(tmpDir, 'AGENTS.md'), existing);
+
+ injectAgentsMd(tmpDir, '1.0.0');
+
+ const content = fs.readFileSync(path.join(tmpDir, 'AGENTS.md'), 'utf-8');
+ expect(content).toContain('Existing agent docs.');
+ expect(content).toContain('<!-- XDS:START -->');
+ expect(content).toContain('[XDS v1.0.0]');
+ });
+});
+
+describe('removeAgentDocs', () => {
+ it('removes XDS section from AGENTS.md', () => {
+ const content = `# My Project
+
+Custom content here.
+
+<!-- XDS:START -->
+XDS index stuff
+<!-- XDS:END -->
+
+More custom content.
+`;
+ fs.writeFileSync(path.join(tmpDir, 'AGENTS.md'), content);
+ vi.spyOn(console, 'log').mockImplementation(() => {});
+
+ removeAgentDocs(tmpDir);
+
+ const result = fs.readFileSync(path.join(tmpDir, 'AGENTS.md'), 'utf-8');
+ expect(result).toContain('Custom content here.');
+ expect(result).toContain('More custom content.');
+ expect(result).not.toContain('<!-- XDS:START -->');
+ expect(result).not.toContain('XDS index stuff');
+ });
+
+ it('removes the file entirely when only XDS content remains', () => {
+ const content = `# AGENTS.md
+
+Project-specific guidance for AI coding agents.
+
+<!-- XDS:START -->
+XDS index stuff
+<!-- XDS:END -->
+`;
+ fs.writeFileSync(path.join(tmpDir, 'AGENTS.md'), content);
+ vi.spyOn(console, 'log').mockImplementation(() => {});
+
+ removeAgentDocs(tmpDir);
+
+ expect(fs.existsSync(path.join(tmpDir, 'AGENTS.md'))).toBe(false);
+ });
+});
diff --git a/packages/cli/src/commands/component.mjs b/packages/cli/src/commands/component.mjs
index 08004695e8..29ec43c2f3 100644
--- a/packages/cli/src/commands/component.mjs
+++ b/packages/cli/src/commands/component.mjs
@@ -46,7 +46,7 @@ const SKIP_DIRS = new Set(['theme', 'hooks', 'utils', '__tests__', 'node_modules
* Auto-discover components by scanning for XDS*.tsx files in core/src/.
* Groups them by category using the DIR_TO_CATEGORY mapping.
*/
-function discoverComponents(coreDir) {
+export function discoverComponents(coreDir) {
const srcDir = path.join(coreDir, 'src');
const categories = {};
@@ -105,7 +105,7 @@ function discoverComponents(coreDir) {
* Minimal cleanup for full docs (default mode).
* Strips SYNC comments, rewrites title, collapses blank lines.
*/
-function cleanReadme(content, componentName) {
+export function cleanReadme(content, componentName) {
const displayName = componentName.startsWith('XDS')
? componentName
: componentName.charAt(0).toUpperCase() + componentName.slice(1);
@@ -158,7 +158,7 @@ const MAX_EXAMPLES = 3;
* Keeps: title, description, Features, Usage (limited examples), Props, Implementation Notes.
* Drops: Files, RTL Support, Related, ASCII art, overflow code blocks.
*/
-function extractCompact(content, componentName) {
+export function extractCompact(content, componentName) {
const lines = content.split('\n');
const output = [];
let inSkipSection = false;
@@ -263,7 +263,7 @@ function extractCompact(content, componentName) {
* and nested directories. Falls back to finding the directory
* containing XDS{name}.tsx and returning its nearest README.
*/
-function findComponentReadme(coreDir, name) {
+export function findComponentReadme(coreDir, name) {
const srcDir = path.join(coreDir, 'src');
// Direct match: src/{name}/README.md
@@ -299,7 +299,7 @@ function findComponentReadme(coreDir, name) {
* For "Layout" finds src/Layout/XDSLayout/XDSLayout.tsx
* For "Card" finds src/Layout/Container/XDSCard.tsx (deep search fallback)
*/
-function findComponentSource(coreDir, name) {
+export function findComponentSource(coreDir, name) {
const srcDir = path.join(coreDir, 'src');
const xdsName = `XDS${name}.tsx`;
diff --git a/packages/cli/src/commands/component.test.mjs b/packages/cli/src/commands/component.test.mjs
new file mode 100644
index 0000000000..b1861b3b4f
--- /dev/null
+++ b/packages/cli/src/commands/component.test.mjs
@@ -0,0 +1,243 @@
+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 {
+ discoverComponents,
+ cleanReadme,
+ extractCompact,
+ findComponentReadme,
+ findComponentSource,
+} from './component.mjs';
+
+let tmpDir;
+
+beforeEach(() => {
+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'xds-component-test-'));
+});
+
+afterEach(() => {
+ fs.rmSync(tmpDir, {recursive: true, force: true});
+});
+
+describe('discoverComponents', () => {
+ it('groups XDS*.tsx files by category', () => {
+ const srcDir = path.join(tmpDir, 'src');
+ // Create Button dir with XDSButton.tsx
+ const buttonDir = path.join(srcDir, 'Button');
+ fs.mkdirSync(buttonDir, {recursive: true});
+ fs.writeFileSync(path.join(buttonDir, 'XDSButton.tsx'), '');
+
+ // Create Avatar dir with XDSAvatar.tsx
+ const avatarDir = path.join(srcDir, 'Avatar');
+ fs.mkdirSync(avatarDir, {recursive: true});
+ fs.writeFileSync(path.join(avatarDir, 'XDSAvatar.tsx'), '');
+
+ // Create Grid dir with XDSGrid.tsx
+ const gridDir = path.join(srcDir, 'Grid');
+ fs.mkdirSync(gridDir, {recursive: true});
+ fs.writeFileSync(path.join(gridDir, 'XDSGrid.tsx'), '');
+
+ // Skip dirs
+ const themeDir = path.join(srcDir, 'theme');
+ fs.mkdirSync(themeDir, {recursive: true});
+ fs.writeFileSync(path.join(themeDir, 'XDSTheme.tsx'), '');
+
+ const result = discoverComponents(tmpDir);
+
+ expect(result).toEqual({
+ Layout: ['Grid'],
+ Display: ['Avatar'],
+ Action: ['Button'],
+ });
+ });
+
+ it('skips test files', () => {
+ const srcDir = path.join(tmpDir, 'src');
+ const buttonDir = path.join(srcDir, 'Button');
+ fs.mkdirSync(buttonDir, {recursive: true});
+ fs.writeFileSync(path.join(buttonDir, 'XDSButton.tsx'), '');
+ fs.writeFileSync(path.join(buttonDir, 'XDSButton.test.tsx'), '');
+
+ const result = discoverComponents(tmpDir);
+ expect(result).toEqual({Action: ['Button']});
+ });
+
+ it('puts unknown directories under Other', () => {
+ const srcDir = path.join(tmpDir, 'src');
+ const customDir = path.join(srcDir, 'CustomWidget');
+ fs.mkdirSync(customDir, {recursive: true});
+ fs.writeFileSync(path.join(customDir, 'XDSCustomWidget.tsx'), '');
+
+ const result = discoverComponents(tmpDir);
+ expect(result).toEqual({Other: ['CustomWidget']});
+ });
+});
+
+describe('cleanReadme', () => {
+ it('strips SYNC comments, rewrites title, collapses blanks', () => {
+ const input = [
+ '# /packages/core/src/Button',
+ '<!-- SYNC: something -->',
+ '',
+ 'Description here.',
+ '',
+ '',
+ '',
+ 'More text.',
+ ].join('\n');
+
+ const result = cleanReadme(input, 'Button');
+ expect(result).toContain('# Button');
+ expect(result).not.toContain('SYNC');
+ // Should collapse consecutive blank lines
+ expect(result).not.toContain('\n\n\n');
+ });
+
+ it('preserves XDS prefix in display name', () => {
+ const input = '# /packages/core/src/XDSButton\n\nContent.\n';
+ const result = cleanReadme(input, 'XDSButton');
+ expect(result).toContain('# XDSButton');
+ });
+});
+
+describe('extractCompact', () => {
+ it('skips configured sections', () => {
+ const input = [
+ '# /packages/core/src/Button',
+ '',
+ '## Features',
+ 'Feature list.',
+ '',
+ '## Files',
+ 'This should be skipped.',
+ '',
+ '## RTL Support',
+ 'Also skipped.',
+ '',
+ '## Props',
+ 'Props content.',
+ ].join('\n');
+
+ const result = extractCompact(input, 'Button');
+ expect(result).toContain('# Button');
+ expect(result).toContain('Feature list.');
+ expect(result).not.toContain('This should be skipped.');
+ expect(result).not.toContain('Also skipped.');
+ expect(result).toContain('Props content.');
+ });
+
+ it('limits code blocks to MAX_EXAMPLES (3)', () => {
+ const blocks = [];
+ for (let i = 0; i < 5; i++) {
+ blocks.push(`\`\`\`tsx\ncode block ${i}\n\`\`\``);
+ }
+ const input = `# /Button\n\n## Usage\n\n${blocks.join('\n\n')}\n`;
+
+ const result = extractCompact(input, 'Button');
+ // Should include at most 3 code blocks
+ const codeBlockCount = (result.match(/```tsx/g) || []).length;
+ expect(codeBlockCount).toBeLessThanOrEqual(3);
+ });
+
+ it('strips ASCII art blocks', () => {
+ const input = [
+ '# /Button',
+ '',
+ '```',
+ '┌──────────────┐',
+ '│ ASCII art │',
+ '└──────────────┘',
+ '```',
+ '',
+ 'Regular text.',
+ ].join('\n');
+
+ const result = extractCompact(input, 'Button');
+ expect(result).not.toContain('┌──────────────┐');
+ expect(result).toContain('Regular text.');
+ });
+});
+
+describe('findComponentReadme', () => {
+ it('finds direct README: src/{name}/README.md', () => {
+ const srcDir = path.join(tmpDir, 'src');
+ const compDir = path.join(srcDir, 'Button');
+ fs.mkdirSync(compDir, {recursive: true});
+ fs.writeFileSync(path.join(compDir, 'README.md'), '# Button');
+
+ const result = findComponentReadme(tmpDir, 'Button');
+ expect(result).toBe(path.join(compDir, 'README.md'));
+ });
+
+ it('finds nested README: src/*/{name}/README.md', () => {
+ const srcDir = path.join(tmpDir, 'src');
+ const nestedDir = path.join(srcDir, 'Layout', 'Container');
+ fs.mkdirSync(nestedDir, {recursive: true});
+ fs.writeFileSync(path.join(nestedDir, 'README.md'), '# Container');
+
+ const result = findComponentReadme(tmpDir, 'Container');
+ expect(result).toBe(path.join(nestedDir, 'README.md'));
+ });
+
+ it('falls back to README near source file', () => {
+ const srcDir = path.join(tmpDir, 'src');
+ const deepDir = path.join(srcDir, 'Layout', 'Container', 'Card');
+ fs.mkdirSync(deepDir, {recursive: true});
+ fs.writeFileSync(path.join(deepDir, 'XDSCard.tsx'), '');
+ // README in Container (parent of Card)
+ fs.writeFileSync(
+ path.join(srcDir, 'Layout', 'Container', 'README.md'),
+ '# Container',
+ );
+
+ const result = findComponentReadme(tmpDir, 'Card');
+ expect(result).toBe(
+ path.join(srcDir, 'Layout', 'Container', 'README.md'),
+ );
+ });
+
+ it('returns null when no README found', () => {
+ const srcDir = path.join(tmpDir, 'src');
+ fs.mkdirSync(srcDir, {recursive: true});
+ expect(findComponentReadme(tmpDir, 'NonExistent')).toBeNull();
+ });
+});
+
+describe('findComponentSource', () => {
+ it('finds direct source: src/{name}/XDS{name}.tsx', () => {
+ const srcDir = path.join(tmpDir, 'src');
+ const compDir = path.join(srcDir, 'Button');
+ fs.mkdirSync(compDir, {recursive: true});
+ fs.writeFileSync(path.join(compDir, 'XDSButton.tsx'), '');
+
+ const result = findComponentSource(tmpDir, 'Button');
+ expect(result).toBe(path.join(compDir, 'XDSButton.tsx'));
+ });
+
+ it('finds nested source: src/{name}/XDS{name}/XDS{name}.tsx', () => {
+ const srcDir = path.join(tmpDir, 'src');
+ const nestedDir = path.join(srcDir, 'Layout', 'XDSLayout');
+ fs.mkdirSync(nestedDir, {recursive: true});
+ fs.writeFileSync(path.join(nestedDir, 'XDSLayout.tsx'), '');
+
+ const result = findComponentSource(tmpDir, 'Layout');
+ expect(result).toBe(path.join(nestedDir, 'XDSLayout.tsx'));
+ });
+
+ it('finds deep fallback: src/*/*/XDS{name}.tsx', () => {
+ const srcDir = path.join(tmpDir, 'src');
+ const deepDir = path.join(srcDir, 'Layout', 'Container');
+ fs.mkdirSync(deepDir, {recursive: true});
+ fs.writeFileSync(path.join(deepDir, 'XDSCard.tsx'), '');
+
+ const result = findComponentSource(tmpDir, 'Card');
+ expect(result).toBe(path.join(deepDir, 'XDSCard.tsx'));
+ });
+
+ it('returns null when source not found', () => {
+ const srcDir = path.join(tmpDir, 'src');
+ fs.mkdirSync(srcDir, {recursive: true});
+ expect(findComponentSource(tmpDir, 'NonExistent')).toBeNull();
+ });
+});
diff --git a/packages/cli/src/commands/docs.test.mjs b/packages/cli/src/commands/docs.test.mjs
new file mode 100644
index 0000000000..284de82c41
--- /dev/null
+++ b/packages/cli/src/commands/docs.test.mjs
@@ -0,0 +1,51 @@
+import {describe, it, expect, beforeEach, afterEach, vi} from 'vitest';
+import * as fs from 'node:fs';
+import * as path from 'node:path';
+import * as os from 'node:os';
+import {Command} from 'commander';
+import {registerDocs} from './docs.mjs';
+
+let tmpDir;
+
+beforeEach(() => {
+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'xds-docs-test-'));
+ vi.spyOn(console, 'log').mockImplementation(() => {});
+ vi.spyOn(console, 'error').mockImplementation(() => {});
+});
+
+afterEach(() => {
+ fs.rmSync(tmpDir, {recursive: true, force: true});
+ vi.restoreAllMocks();
+});
+
+function createProgram() {
+ const program = new Command();
+ program.exitOverride(); // Throw instead of calling process.exit
+ registerDocs(program);
+ return program;
+}
+
+describe('registerDocs', () => {
+ it('lists available topics when no topic given', async () => {
+ const program = createProgram();
+ await program.parseAsync(['node', 'xds', 'docs']);
+
+ const output = console.log.mock.calls.map(c => c[0]).join('\n');
+ expect(output).toContain('principles');
+ expect(output).toContain('tokens');
+ });
+
+ it('errors for unknown topic', async () => {
+ const program = createProgram();
+ vi.spyOn(process, 'exit').mockImplementation(code => {
+ throw new Error(`exit ${code}`);
+ });
+
+ await expect(
+ program.parseAsync(['node', 'xds', 'docs', 'nonexistent']),
+ ).rejects.toThrow('exit 1');
+
+ const errorOutput = console.error.mock.calls.map(c => c[0]).join('\n');
+ expect(errorOutput).toContain('Unknown topic');
+ });
+});
diff --git a/packages/cli/src/commands/swizzle.mjs b/packages/cli/src/commands/swizzle.mjs
index 4359407114..ef61d94f05 100644
--- a/packages/cli/src/commands/swizzle.mjs
+++ b/packages/cli/src/commands/swizzle.mjs
@@ -17,7 +17,7 @@ import {findCoreDir, listComponents} from '../utils/paths.mjs';
* e.g. '../theme/tokens.stylex' → '@xds/core/theme'
* '../utils/mergeProps' → '@xds/core/utils'
*/
-function rewriteImports(content) {
+export function rewriteImports(content) {
// Match import/export from statements with relative paths going up
return content.replace(
/(from\s+['"])(\.\.\/.+?)(['"])/g,
diff --git a/packages/cli/src/commands/swizzle.test.mjs b/packages/cli/src/commands/swizzle.test.mjs
new file mode 100644
index 0000000000..b6bd4ae206
--- /dev/null
+++ b/packages/cli/src/commands/swizzle.test.mjs
@@ -0,0 +1,51 @@
+import {describe, it, expect} from 'vitest';
+import {rewriteImports} from './swizzle.mjs';
+
+describe('rewriteImports', () => {
+ it('rewrites ../theme/tokens to @xds/core/theme', () => {
+ const input = `import { tokens } from '../theme/tokens.stylex';`;
+ const result = rewriteImports(input);
+ expect(result).toBe(`import { tokens } from '@xds/core/theme';`);
+ });
+
+ it('rewrites ../utils/mergeProps to @xds/core/utils', () => {
+ const input = `import { mergeProps } from '../utils/mergeProps';`;
+ const result = rewriteImports(input);
+ expect(result).toBe(`import { mergeProps } from '@xds/core/utils';`);
+ });
+
+ it('leaves same-level relative imports untouched', () => {
+ const input = `import { helper } from './helper';`;
+ const result = rewriteImports(input);
+ expect(result).toBe(`import { helper } from './helper';`);
+ });
+
+ it('rewrites export from statements', () => {
+ const input = `export { foo } from '../hooks/useLayout';`;
+ const result = rewriteImports(input);
+ expect(result).toBe(`export { foo } from '@xds/core/hooks';`);
+ });
+
+ it('handles double quotes', () => {
+ const input = `import { tokens } from "../theme/tokens.stylex";`;
+ const result = rewriteImports(input);
+ expect(result).toBe(`import { tokens } from "@xds/core/theme";`);
+ });
+
+ it('handles multiple imports in one file', () => {
+ const input = [
+ `import { tokens } from '../theme/tokens.stylex';`,
+ `import { mergeProps } from '../utils/mergeProps';`,
+ `import { helper } from './helper';`,
+ ].join('\n');
+
+ const result = rewriteImports(input);
+ expect(result).toBe(
+ [
+ `import { tokens } from '@xds/core/theme';`,
+ `import { mergeProps } from '@xds/core/utils';`,
+ `import { helper } from './helper';`,
+ ].join('\n'),
+ );
+ });
+});
diff --git a/packages/cli/src/commands/template.test.mjs b/packages/cli/src/commands/template.test.mjs
new file mode 100644
index 0000000000..e0c99bbcb9
--- /dev/null
+++ b/packages/cli/src/commands/template.test.mjs
@@ -0,0 +1,60 @@
+import {describe, it, expect, beforeEach, afterEach, vi} from 'vitest';
+import * as fs from 'node:fs';
+import * as path from 'node:path';
+import * as os from 'node:os';
+
+// We need to test listTemplates which depends on CLI_ROOT.
+// Since CLI_ROOT is computed from import.meta.url in paths.mjs,
+// we test listTemplates indirectly by mocking the module.
+
+let tmpDir;
+
+beforeEach(() => {
+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'xds-template-test-'));
+});
+
+afterEach(() => {
+ fs.rmSync(tmpDir, {recursive: true, force: true});
+ vi.restoreAllMocks();
+});
+
+describe('listTemplates', () => {
+ it('returns sorted template directory names', async () => {
+ // Create mock templates directory structure
+ const templatesDir = path.join(tmpDir, 'templates');
+ fs.mkdirSync(path.join(templatesDir, 'table'), {recursive: true});
+ fs.mkdirSync(path.join(templatesDir, 'blank'), {recursive: true});
+ fs.mkdirSync(path.join(templatesDir, 'login'), {recursive: true});
+ // Add a file (should be filtered out)
+ fs.writeFileSync(path.join(templatesDir, 'README.md'), '');
+
+ // listTemplates uses CLI_ROOT which is hardcoded from import.meta.url.
+ // Instead, we replicate its logic directly against our tmpDir.
+ const entries = fs
+ .readdirSync(templatesDir, {withFileTypes: true})
+ .filter(e => e.isDirectory())
+ .map(e => e.name)
+ .sort();
+
+ expect(entries).toEqual(['blank', 'login', 'table']);
+ });
+
+ it('returns empty array when templates dir is missing', async () => {
+ // No templates directory exists under tmpDir
+ const templatesDir = path.join(tmpDir, 'templates');
+ expect(fs.existsSync(templatesDir)).toBe(false);
+ // Replicate listTemplates logic
+ const result = fs.existsSync(templatesDir) ? [] : [];
+ expect(result).toEqual([]);
+ });
+});
+
+describe('listTemplates integration', () => {
+ it('can import listTemplates from the module', async () => {
+ const {listTemplates} = await import('./template.mjs');
+ // listTemplates returns based on CLI_ROOT/templates.
+ // It should return an array (possibly empty if templates dir doesn't exist).
+ const result = listTemplates();
+ expect(Array.isArray(result)).toBe(true);
+ });
+});
diff --git a/packages/cli/src/commands/theme.test.mjs b/packages/cli/src/commands/theme.test.mjs
new file mode 100644
index 0000000000..d1a53af20e
--- /dev/null
+++ b/packages/cli/src/commands/theme.test.mjs
@@ -0,0 +1,48 @@
+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 {discoverThemes, writeThemeConfig} from './theme.mjs';
+
+let tmpDir;
+
+beforeEach(() => {
+ tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'xds-theme-test-'));
+});
+
+afterEach(() => {
+ fs.rmSync(tmpDir, {recursive: true, force: true});
+});
+
+describe('discoverThemes', () => {
+ it('discovers *Theme.stylex.ts files and returns sorted array', () => {
+ const themeDir = path.join(tmpDir, 'src', 'theme');
+ fs.mkdirSync(themeDir, {recursive: true});
+ fs.writeFileSync(path.join(themeDir, 'neutralTheme.stylex.ts'), '');
+ fs.writeFileSync(path.join(themeDir, 'defaultTheme.stylex.ts'), '');
+ fs.writeFileSync(path.join(themeDir, 'tokens.stylex.ts'), ''); // not a theme file
+ fs.writeFileSync(path.join(themeDir, 'utils.ts'), ''); // not a theme file
+
+ const result = discoverThemes(tmpDir);
+ expect(result).toEqual([
+ {name: 'default', exportName: 'defaultTheme'},
+ {name: 'neutral', exportName: 'neutralTheme'},
+ ]);
+ });
+
+ it('returns empty array when theme dir is missing', () => {
+ expect(discoverThemes(tmpDir)).toEqual([]);
+ });
+});
+
+describe('writeThemeConfig', () => {
+ it('writes xds.config.mjs with correct content', () => {
+ const configPath = writeThemeConfig(tmpDir, 'neutralTheme');
+
+ expect(configPath).toBe(path.join(tmpDir, 'xds.config.mjs'));
+
+ const content = fs.readFileSync(configPath, 'utf-8');
+ expect(content).toContain("theme: 'neutralTheme'");
+ expect(content).toContain('XDS Configuration');
+ });
+});
diff --git a/packages/cli/src/utils/paths.test.mjs b/packages/cli/src/utils/paths.test.mjs
new file mode 100644
index 0000000000..aa4d616423
--- /dev/null
+++ b/packages/cli/src/utils/paths.test.mjs
@@ -0,0 +1,81 @@
+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 {findCoreDir, findProjectRoot, listComponents} from './paths.mjs';
+
+let tmpDir;
+
+function makeTmpDir() {
+ return fs.mkdtempSync(path.join(os.tmpdir(), 'xds-paths-test-'));
+}
+
+beforeEach(() => {
+ tmpDir = makeTmpDir();
+});
+
+afterEach(() => {
+ fs.rmSync(tmpDir, {recursive: true, force: true});
+});
+
+describe('findCoreDir', () => {
+ it('finds packages/core walking up directories', () => {
+ const coreDir = path.join(tmpDir, 'packages', 'core');
+ fs.mkdirSync(coreDir, {recursive: true});
+
+ const nested = path.join(tmpDir, 'sub', 'deep');
+ fs.mkdirSync(nested, {recursive: true});
+
+ expect(findCoreDir(nested)).toBe(coreDir);
+ });
+
+ it('finds node_modules/@xds/core fallback', () => {
+ const nmCore = path.join(tmpDir, 'node_modules', '@xds', 'core');
+ fs.mkdirSync(nmCore, {recursive: true});
+
+ expect(findCoreDir(tmpDir)).toBe(nmCore);
+ });
+
+ it('returns null when nothing found', () => {
+ expect(findCoreDir(tmpDir)).toBeNull();
+ });
+});
+
+describe('findProjectRoot', () => {
+ it('finds root with workspaces in package.json', () => {
+ fs.writeFileSync(
+ path.join(tmpDir, 'package.json'),
+ JSON.stringify({name: 'root', workspaces: ['packages/*']}),
+ );
+
+ const nested = path.join(tmpDir, 'packages', 'foo');
+ fs.mkdirSync(nested, {recursive: true});
+
+ expect(findProjectRoot(nested)).toBe(tmpDir);
+ });
+
+ it('returns null when no workspaces found', () => {
+ fs.writeFileSync(
+ path.join(tmpDir, 'package.json'),
+ JSON.stringify({name: 'no-workspaces'}),
+ );
+
+ expect(findProjectRoot(tmpDir)).toBeNull();
+ });
+});
+
+describe('listComponents', () => {
+ it('returns sorted component directory names, skipping hooks/theme/utils', () => {
+ const srcDir = path.join(tmpDir, 'src');
+ for (const dir of ['Button', 'Avatar', 'hooks', 'theme', 'utils', 'Zebra']) {
+ fs.mkdirSync(path.join(srcDir, dir), {recursive: true});
+ }
+
+ const result = listComponents(tmpDir);
+ expect(result).toEqual(['Avatar', 'Button', 'Zebra']);
+ });
+
+ it('returns empty array when src dir is missing', () => {
+ expect(listComponents(tmpDir)).toEqual([]);
+ });
+});
diff --git a/vitest.config.ts b/vitest.config.ts
index 2a0fc22437..40488e6cb1 100644
--- a/vitest.config.ts
+++ b/vitest.config.ts
@@ -35,7 +35,7 @@ export default defineConfig({
test: {
globals: true,
environment: 'jsdom',
- include: ['packages/**/src/**/*.test.{ts,tsx}'],
+ include: ['packages/**/src/**/*.test.{ts,tsx,mjs}'],
coverage: {
provider: 'v8',
reporter: ['text', 'json', 'html'],