|
1 | | -const { system, filesystem } = require('gluegun') |
| 1 | +import { exec } from 'node:child_process' |
| 2 | +import { promisify } from 'node:util' |
| 3 | +import { resolve } from 'node:path' |
| 4 | +import { filesystem } from 'gluegun' |
2 | 5 |
|
3 | | -const src = filesystem.path(__dirname, '..') |
| 6 | +const execAsync = promisify(exec) |
4 | 7 |
|
5 | | -const cli = async (cmd: string) => |
6 | | - system.run('node ' + filesystem.path(src, 'bin', 'xs-dev') + ` ${cmd}`) |
| 8 | +const CLI_PATH = resolve(__dirname, '..', 'build', 'src', 'cli.js') |
7 | 9 |
|
8 | | -test('outputs version', async () => { |
9 | | - const output = await cli('--version') |
10 | | - expect(output).toContain('0.0.1') |
11 | | -}) |
| 10 | +/** |
| 11 | + * Execute xs-dev CLI command and return { stdout, stderr, code } |
| 12 | + */ |
| 13 | +const runCLI = async (command: string, options: { cwd?: string } = {}) => { |
| 14 | + try { |
| 15 | + const { stdout, stderr } = await execAsync( |
| 16 | + `node "${CLI_PATH}" ${command}`, |
| 17 | + { |
| 18 | + cwd: options.cwd || process.cwd(), |
| 19 | + timeout: 30000, // 30 second timeout |
| 20 | + }, |
| 21 | + ) |
| 22 | + return { stdout: stdout.trim(), stderr: stderr.trim(), code: 0 } |
| 23 | + } catch (error: any) { |
| 24 | + return { |
| 25 | + stdout: error.stdout?.trim() || '', |
| 26 | + stderr: error.stderr?.trim() || '', |
| 27 | + code: error.code || 1, |
| 28 | + } |
| 29 | + } |
| 30 | +} |
12 | 31 |
|
13 | | -test('outputs help', async () => { |
14 | | - const output = await cli('--help') |
15 | | - expect(output).toContain('0.0.1') |
16 | | -}) |
| 32 | +describe('CLI Integration Tests', () => { |
| 33 | + describe('Basic CLI functionality', () => { |
| 34 | + it('should output version information', async () => { |
| 35 | + const result = await runCLI('--version') |
| 36 | + |
| 37 | + expect(result.code).toBe(0) |
| 38 | + expect(result.stdout).toMatch(/\d+\.\d+\.\d+/) |
| 39 | + }, 10000) |
| 40 | + |
| 41 | + it('should display help information', async () => { |
| 42 | + const result = await runCLI('--help') |
| 43 | + |
| 44 | + expect(result.code).toBe(0) |
| 45 | + expect(result.stdout).toContain('xs-dev') |
| 46 | + expect(result.stdout).toContain('setup') |
| 47 | + expect(result.stdout).toContain('build') |
| 48 | + expect(result.stdout).toContain('run') |
| 49 | + }, 10000) |
| 50 | + |
| 51 | + it('should show command help', async () => { |
| 52 | + const result = await runCLI('setup --help') |
| 53 | + |
| 54 | + expect(result.code).toBe(0) |
| 55 | + expect(result.stdout).toContain('setup') |
| 56 | + expect(result.stdout).toContain('device') |
| 57 | + }, 10000) |
| 58 | + }) |
| 59 | + |
| 60 | + describe('Error handling', () => { |
| 61 | + it('should handle unknown commands gracefully', async () => { |
| 62 | + const result = await runCLI('nonexistent-command') |
| 63 | + |
| 64 | + expect(result.code).not.toBe(0) |
| 65 | + expect(result.stderr || result.stdout).toMatch( |
| 66 | + /no command registered|unknown|not found|invalid/i, |
| 67 | + ) |
| 68 | + }, 10000) |
| 69 | + |
| 70 | + it('should validate missing required arguments', async () => { |
| 71 | + const result = await runCLI('init') // init requires project name |
| 72 | + |
| 73 | + expect(result.code).not.toBe(0) |
| 74 | + expect(result.stderr || result.stdout).toMatch(/required|name/i) |
| 75 | + }, 10000) |
| 76 | + }) |
| 77 | + |
| 78 | + describe('Command functionality', () => { |
| 79 | + const tempDir = resolve(__dirname, '..', 'test-project') |
| 80 | + |
| 81 | + beforeEach(() => { |
| 82 | + // Clean up any previous test artifacts |
| 83 | + filesystem.remove(tempDir) |
| 84 | + }) |
| 85 | + |
| 86 | + afterEach(() => { |
| 87 | + // Clean up test artifacts |
| 88 | + filesystem.remove(tempDir) |
| 89 | + }) |
| 90 | + |
| 91 | + it('should handle init command without MODDABLE env var', async () => { |
| 92 | + const result = await runCLI(`init test-project --typescript`) |
| 93 | + |
| 94 | + expect(result.code).toBe(0) |
| 95 | + }, 15000) |
| 96 | + |
| 97 | + it('should handle include command outside of project directory', async () => { |
| 98 | + const result = await runCLI('include base/timer') |
| 99 | + expect(result.code).not.toBe(0) |
| 100 | + expect(result.stderr || result.stdout).toMatch( |
| 101 | + /manifest\.json.*not found|project directory/i, |
| 102 | + ) |
| 103 | + }, 10000) |
| 104 | + |
| 105 | + it('should handle remove command outside of project directory', async () => { |
| 106 | + const result = await runCLI('remove timer') |
| 107 | + |
| 108 | + expect(result.code).not.toBe(0) |
| 109 | + expect(result.stderr || result.stdout).toMatch( |
| 110 | + /manifest\.json.*not found|project directory/i, |
| 111 | + ) |
| 112 | + }, 10000) |
| 113 | + |
| 114 | + it('should handle setup command gracefully when dependencies missing', async () => { |
| 115 | + const result = await runCLI('setup --device wasm') |
| 116 | + |
| 117 | + // Should either succeed or provide helpful error messages |
| 118 | + // Since we don't have Moddable SDK installed in test environment, |
| 119 | + // it should fail gracefully with a helpful message |
| 120 | + expect(result.code).not.toBe(0) |
| 121 | + expect(result.stderr || result.stdout).toMatch( |
| 122 | + /moddable|required|setup|tooling/i, |
| 123 | + ) |
| 124 | + }, 20000) |
| 125 | + |
| 126 | + it('should show device help information', async () => { |
| 127 | + const result = await runCLI('setup --help') |
| 128 | + |
| 129 | + // Should mention device options in help |
| 130 | + expect(result.stdout || result.stderr).toMatch( |
| 131 | + /device|esp32|esp8266|wasm|pico/i, |
| 132 | + ) |
| 133 | + }, 10000) |
| 134 | + }) |
| 135 | + |
| 136 | + describe('Doctor command', () => { |
| 137 | + it('should run diagnostic checks', async () => { |
| 138 | + const result = await runCLI('doctor') |
| 139 | + |
| 140 | + expect(result.code).toBe(0) |
| 141 | + expect(result.stdout).toMatch(/node|system|platform/i) |
| 142 | + }, 15000) |
| 143 | + }) |
17 | 144 |
|
18 | | -test('generates file', async () => { |
19 | | - const output = await cli('generate foo') |
| 145 | + describe('Edge cases and error conditions', () => { |
| 146 | + it('should handle invalid flags gracefully', async () => { |
| 147 | + const result = await runCLI('setup --invalid-flag') |
20 | 148 |
|
21 | | - expect(output).toContain('Generated file at models/foo-model.ts') |
22 | | - const foomodel = filesystem.read('models/foo-model.ts') |
| 149 | + expect(result.code).not.toBe(0) |
| 150 | + }, 10000) |
23 | 151 |
|
24 | | - expect(foomodel).toContain(`module.exports = {`) |
25 | | - expect(foomodel).toContain(`name: 'foo'`) |
| 152 | + it('should handle empty arguments appropriately', async () => { |
| 153 | + const result = await runCLI('') |
26 | 154 |
|
27 | | - // cleanup artifact |
28 | | - filesystem.remove('models') |
| 155 | + // Should show help or error message |
| 156 | + expect(result.stdout || result.stderr).toMatch(/help|usage|command/i) |
| 157 | + }, 10000) |
| 158 | + }) |
29 | 159 | }) |
0 commit comments