|
1 | | -import { execSync } from 'node:child_process'; |
2 | | -import { existsSync, readFileSync } from 'node:fs'; |
| 1 | +import { readFileSync } from 'node:fs'; |
3 | 2 | import { dirname, join } from 'node:path'; |
4 | 3 | import { fileURLToPath } from 'node:url'; |
| 4 | +import type { Command } from 'commander'; |
5 | 5 | import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
6 | 6 |
|
7 | 7 | const __filename = fileURLToPath(import.meta.url); |
8 | 8 | const __dirname = dirname(__filename); |
9 | 9 |
|
10 | | -// Path to the compiled CLI |
11 | | -const cliPath = join(__dirname, '..', '..', 'dist', 'index.js'); |
12 | | - |
13 | | -// Helper function to execute CLI commands |
14 | | -function runCli(args: string[]): { stdout: string; stderr: string; exitCode: number } { |
15 | | - // Check if CLI binary exists before attempting to run |
16 | | - if (!existsSync(cliPath)) { |
17 | | - return { |
18 | | - stdout: '', |
19 | | - stderr: `CLI binary not found at ${cliPath}`, |
20 | | - exitCode: 1, |
21 | | - }; |
22 | | - } |
| 10 | +vi.mock('../commands/push', () => ({ pushCommand: vi.fn() })); |
| 11 | +vi.mock('../commands/add', () => ({ addCommand: vi.fn() })); |
| 12 | +vi.mock('../commands/config', () => ({ |
| 13 | + configGetCommand: vi.fn(), |
| 14 | + configSetCommand: vi.fn(), |
| 15 | + configListCommand: vi.fn(), |
| 16 | +})); |
| 17 | +vi.mock('../commands/dev', () => ({ devCommand: vi.fn() })); |
| 18 | +vi.mock('../commands/init', () => ({ initCommand: vi.fn() })); |
| 19 | +vi.mock('../commands/list-agents', () => ({ listAgentsCommand: vi.fn() })); |
| 20 | +vi.mock('../commands/login', () => ({ loginCommand: vi.fn() })); |
| 21 | +vi.mock('../commands/logout', () => ({ logoutCommand: vi.fn() })); |
| 22 | +vi.mock('../commands/profile', () => ({ |
| 23 | + profileAddCommand: vi.fn(), |
| 24 | + profileCurrentCommand: vi.fn(), |
| 25 | + profileListCommand: vi.fn(), |
| 26 | + profileRemoveCommand: vi.fn(), |
| 27 | + profileUseCommand: vi.fn(), |
| 28 | +})); |
| 29 | +vi.mock('../commands/pull-v4/introspect', () => ({ pullV4Command: vi.fn() })); |
| 30 | +vi.mock('../commands/status', () => ({ statusCommand: vi.fn() })); |
| 31 | +vi.mock('../commands/update', () => ({ updateCommand: vi.fn() })); |
| 32 | +vi.mock('../commands/whoami', () => ({ whoamiCommand: vi.fn() })); |
| 33 | + |
| 34 | +function parseArgs(program: Command, args: string[]): { stdout: string; stderr: string } { |
| 35 | + let stdout = ''; |
| 36 | + let stderr = ''; |
| 37 | + |
| 38 | + program.exitOverride(); |
| 39 | + program.configureOutput({ |
| 40 | + writeOut: (str) => { |
| 41 | + stdout += str; |
| 42 | + }, |
| 43 | + writeErr: (str) => { |
| 44 | + stderr += str; |
| 45 | + }, |
| 46 | + }); |
23 | 47 |
|
24 | 48 | try { |
25 | | - const stdout = execSync(`node ${cliPath} ${args.join(' ')}`, { |
26 | | - encoding: 'utf8', |
27 | | - stdio: ['pipe', 'pipe', 'pipe'], |
28 | | - timeout: 30000, // 30 second timeout for CI |
29 | | - killSignal: 'SIGTERM', // Use SIGTERM first for cleaner shutdown |
30 | | - windowsHide: true, // Hide windows on Windows |
31 | | - env: { |
32 | | - ...process.env, |
33 | | - // Test environment |
34 | | - CI: 'true', // Signal to CLI that it's running in CI |
35 | | - NODE_OPTIONS: '--max-old-space-size=256', // Limit memory usage |
36 | | - }, |
37 | | - }); |
38 | | - return { stdout, stderr: '', exitCode: 0 }; |
39 | | - } catch (error: any) { |
40 | | - // Handle timeout specifically - check for various timeout indicators |
41 | | - const isTimeout = |
42 | | - error.code === 'ETIMEDOUT' || |
43 | | - error.signal === 'SIGTERM' || |
44 | | - error.killed || |
45 | | - error.message?.toLowerCase().includes('timedout'); |
46 | | - |
47 | | - if (isTimeout) { |
48 | | - return { |
49 | | - stdout: error.stdout || '', |
50 | | - stderr: 'Command timed out', |
51 | | - exitCode: 124, // Standard timeout exit code |
52 | | - }; |
53 | | - } |
54 | | - |
55 | | - return { |
56 | | - stdout: error.stdout || '', |
57 | | - stderr: error.stderr || error.message || 'Unknown error', |
58 | | - exitCode: error.status || 1, |
59 | | - }; |
| 49 | + program.parse(['node', 'inkeep', ...args]); |
| 50 | + } catch { |
| 51 | + // Commander throws on --version, --help, and errors when exitOverride is set |
60 | 52 | } |
| 53 | + |
| 54 | + return { stdout, stderr }; |
61 | 55 | } |
62 | 56 |
|
63 | 57 | describe('Inkeep CLI', () => { |
64 | | - beforeEach(() => { |
65 | | - // Mock console methods to capture output during tests |
| 58 | + let createProgram: () => Command; |
| 59 | + |
| 60 | + beforeEach(async () => { |
66 | 61 | vi.spyOn(console, 'log').mockImplementation(() => {}); |
67 | 62 | vi.spyOn(console, 'error').mockImplementation(() => {}); |
| 63 | + ({ createProgram } = await import('../program')); |
68 | 64 | }); |
69 | 65 |
|
70 | | - afterEach(async () => { |
| 66 | + afterEach(() => { |
71 | 67 | vi.restoreAllMocks(); |
72 | | - // Small delay to allow processes to fully terminate |
73 | | - await new Promise((resolve) => setTimeout(resolve, 100)); |
74 | | - // Force garbage collection to clean up any hanging references |
75 | | - if (global.gc) { |
76 | | - global.gc(); |
77 | | - } |
78 | 68 | }); |
79 | 69 |
|
80 | 70 | describe('--version command', () => { |
81 | 71 | it('should display the version number', () => { |
82 | | - const result = runCli(['--version']); |
83 | | - |
84 | | - expect(result.exitCode).toBe(0); |
85 | | - expect(result.stdout.trim()).toMatch(/^\d+\.\d+\.\d+$/); |
| 72 | + const { stdout } = parseArgs(createProgram(), ['--version']); |
| 73 | + expect(stdout.trim()).toMatch(/^\d+\.\d+\.\d+$/); |
86 | 74 | }); |
87 | 75 |
|
88 | 76 | it('should match the version in package.json', () => { |
89 | 77 | const packageJsonPath = join(__dirname, '..', '..', 'package.json'); |
90 | 78 | const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf-8')); |
91 | | - const expectedVersion = packageJson.version; |
92 | 79 |
|
93 | | - const result = runCli(['--version']); |
94 | | - |
95 | | - expect(result.stdout.trim()).toBe(expectedVersion); |
| 80 | + const { stdout } = parseArgs(createProgram(), ['--version']); |
| 81 | + expect(stdout.trim()).toBe(packageJson.version); |
96 | 82 | }); |
97 | 83 | }); |
98 | 84 |
|
99 | | - describe('push command', () => { |
100 | | - it('should work without required arguments', () => { |
101 | | - const result = runCli(['push']); |
102 | | - |
103 | | - // Skip test if command timed out (CI performance issue, not CLI bug) |
104 | | - if (result.exitCode === 124) { |
105 | | - console.log('Skipping assertion: push command timed out in CI'); |
106 | | - return; |
107 | | - } |
108 | | - |
109 | | - // The push command now tries to detect project automatically |
110 | | - expect(result.exitCode).toBe(1); |
111 | | - // It should fail because configuration or project is missing in test environment |
112 | | - expect(result.stderr.toLowerCase()).toMatch(/tenant id|index\.ts|config/); |
113 | | - }); |
| 85 | + describe('--help command', () => { |
| 86 | + it('should display help information', () => { |
| 87 | + const { stdout } = parseArgs(createProgram(), ['--help']); |
114 | 88 |
|
115 | | - it('should accept --agents-api-url option', () => { |
116 | | - const result = runCli([ |
117 | | - 'push', |
118 | | - '--project', |
119 | | - 'non-existent', |
120 | | - '--agents-api-url', |
121 | | - 'http://example.com', |
122 | | - ]); |
123 | | - |
124 | | - // Will fail because project doesn't exist, but should accept the option |
125 | | - expect(result.exitCode).toBe(1); |
126 | | - // Should fail for project not found, not for invalid option |
127 | | - expect(result.stderr).not.toContain('unknown option'); |
| 89 | + expect(stdout).toContain('CLI tool for Inkeep Agent Framework'); |
| 90 | + expect(stdout).toContain('Commands:'); |
| 91 | + expect(stdout).toContain('push'); |
| 92 | + expect(stdout).toContain('config'); |
| 93 | + expect(stdout).toContain('list-agent'); |
128 | 94 | }); |
129 | | - }); |
130 | 95 |
|
131 | | - describe('list-agent command', () => { |
132 | | - it('should require --project option and accept --url option', () => { |
133 | | - const result = runCli(['list-agent', '--help']); |
| 96 | + it('should have push command with correct description', () => { |
| 97 | + const program = createProgram(); |
| 98 | + const pushCmd = program.commands.find((c) => c.name() === 'push'); |
134 | 99 |
|
135 | | - expect(result.exitCode).toBe(0); |
136 | | - expect(result.stdout).toContain('List all available agents for a specific project'); |
137 | | - expect(result.stdout).toContain('--project <project-id>'); |
138 | | - expect(result.stdout).toContain('--agents-api-url'); |
| 100 | + expect(pushCmd).toBeDefined(); |
| 101 | + expect(pushCmd?.description()).toContain('Push a project configuration'); |
139 | 102 | }); |
140 | 103 | }); |
141 | 104 |
|
142 | | - describe('--help command', () => { |
143 | | - it('should display help information', () => { |
144 | | - const result = runCli(['--help']); |
145 | | - |
146 | | - expect(result.exitCode).toBe(0); |
147 | | - expect(result.stdout).toContain('CLI tool for Inkeep Agent Framework'); |
148 | | - expect(result.stdout).toContain('Commands:'); |
149 | | - expect(result.stdout).toContain('push'); |
150 | | - expect(result.stdout).toContain('config'); |
151 | | - expect(result.stdout).toContain('list-agent'); |
| 105 | + describe('push command', () => { |
| 106 | + it('should accept --agents-api-url option', () => { |
| 107 | + const program = createProgram(); |
| 108 | + const pushCmd = program.commands.find((c) => c.name() === 'push'); |
| 109 | + |
| 110 | + expect(pushCmd).toBeDefined(); |
| 111 | + const optionNames = pushCmd?.options.map((o) => o.long); |
| 112 | + expect(optionNames).toContain('--agents-api-url'); |
| 113 | + expect(optionNames).toContain('--project'); |
| 114 | + expect(optionNames).toContain('--config'); |
| 115 | + expect(optionNames).toContain('--force'); |
152 | 116 | }); |
| 117 | + }); |
153 | 118 |
|
154 | | - it('should display help for push command', () => { |
155 | | - const result = runCli(['push', '--help']); |
156 | | - |
157 | | - expect(result.exitCode).toBe(0); |
158 | | - expect(result.stdout).toContain('Push a project configuration'); |
159 | | - expect(result.stdout).toContain('--agents-api-url'); |
| 119 | + describe('list-agent command', () => { |
| 120 | + it('should have expected description and options', () => { |
| 121 | + const program = createProgram(); |
| 122 | + const listAgentCmd = program.commands.find((c) => c.name() === 'list-agent'); |
| 123 | + |
| 124 | + expect(listAgentCmd).toBeDefined(); |
| 125 | + expect(listAgentCmd?.description()).toBe('List all available agents for a specific project'); |
| 126 | + const optionNames = listAgentCmd?.options.map((o) => o.long); |
| 127 | + expect(optionNames).toContain('--project'); |
| 128 | + expect(optionNames).toContain('--agents-api-url'); |
160 | 129 | }); |
161 | 130 | }); |
162 | 131 |
|
163 | 132 | describe('invalid commands', () => { |
164 | 133 | it('should show error for unknown command', () => { |
165 | | - const result = runCli(['unknown-command']); |
166 | | - |
167 | | - expect(result.exitCode).toBe(1); |
168 | | - expect(result.stderr).toContain('error: unknown command'); |
| 134 | + const { stderr } = parseArgs(createProgram(), ['unknown-command']); |
| 135 | + expect(stderr).toContain('unknown command'); |
169 | 136 | }); |
170 | 137 | }); |
171 | 138 |
|
172 | 139 | describe('CLI structure', () => { |
173 | 140 | it('should have correct CLI name', () => { |
174 | | - const result = runCli(['--help']); |
175 | | - |
176 | | - expect(result.stdout).toContain('inkeep'); |
| 141 | + const { stdout } = parseArgs(createProgram(), ['--help']); |
| 142 | + expect(stdout).toContain('inkeep'); |
177 | 143 | }); |
178 | 144 |
|
179 | | - it('should be executable', () => { |
180 | | - // This test ensures the CLI can be executed without throwing |
181 | | - const result = runCli(['--version']); |
182 | | - |
183 | | - expect(result.exitCode).toBe(0); |
| 145 | + it('should register all expected commands', () => { |
| 146 | + const program = createProgram(); |
| 147 | + const commandNames = program.commands.map((c) => c.name()); |
| 148 | + |
| 149 | + expect(commandNames).toContain('push'); |
| 150 | + expect(commandNames).toContain('pull'); |
| 151 | + expect(commandNames).toContain('list-agent'); |
| 152 | + expect(commandNames).toContain('config'); |
| 153 | + expect(commandNames).toContain('dev'); |
| 154 | + expect(commandNames).toContain('login'); |
| 155 | + expect(commandNames).toContain('logout'); |
| 156 | + expect(commandNames).toContain('status'); |
| 157 | + expect(commandNames).toContain('profile'); |
| 158 | + expect(commandNames).toContain('add'); |
| 159 | + expect(commandNames).toContain('init'); |
| 160 | + expect(commandNames).toContain('update'); |
| 161 | + expect(commandNames).toContain('whoami'); |
184 | 162 | }); |
185 | 163 | }); |
186 | 164 | }); |
0 commit comments