optimus-init
Version:
Initialization utility for Optimus Security
154 lines (130 loc) • 5.61 kB
JavaScript
/**
* Tests for optimus-init CLI
*/
const { execSync } = require('child_process');
const path = require('path');
const fs = require('fs');
const CLI_PATH = path.join(__dirname, '../dist/cli.js');
describe('optimus-init CLI', () => {
describe('Version command', () => {
test('should display version', () => {
const output = execSync(`node ${CLI_PATH} --version`).toString();
expect(output.trim()).toBe('1.0.9');
});
});
describe('Help command', () => {
test('should display help when no arguments', () => {
const output = execSync(`node ${CLI_PATH} --help`).toString();
expect(output).toContain('Optimus Security Initialization Tool');
expect(output).toContain('get');
expect(output).toContain('post');
expect(output).toContain('download');
expect(output).toContain('init');
});
});
describe('Get command options', () => {
test('should accept --user flag', () => {
const output = execSync(`node ${CLI_PATH} get --help`).toString();
expect(output).toContain('--user');
});
test('should accept --repo flag', () => {
const output = execSync(`node ${CLI_PATH} get --help`).toString();
expect(output).toContain('--repo');
});
test('should accept --branch flag', () => {
const output = execSync(`node ${CLI_PATH} get --help`).toString();
expect(output).toContain('--branch');
});
test('should accept --agent flag', () => {
const output = execSync(`node ${CLI_PATH} get --help`).toString();
expect(output).toContain('--agent');
});
test('should accept --env flag', () => {
const output = execSync(`node ${CLI_PATH} get --help`).toString();
expect(output).toContain('--env');
});
test('should accept --params flag', () => {
const output = execSync(`node ${CLI_PATH} get --help`).toString();
expect(output).toContain('--params');
});
});
describe('Post command options', () => {
test('should accept --data flag', () => {
const output = execSync(`node ${CLI_PATH} post --help`).toString();
expect(output).toContain('--data');
});
test('should accept --env flag', () => {
const output = execSync(`node ${CLI_PATH} post --help`).toString();
expect(output).toContain('--env');
});
});
describe('Download command options', () => {
test('should accept --params flag', () => {
const output = execSync(`node ${CLI_PATH} download --help`).toString();
expect(output).toContain('--params');
});
test('should accept --env flag', () => {
const output = execSync(`node ${CLI_PATH} download --help`).toString();
expect(output).toContain('--env');
});
});
describe('Init command options', () => {
test('should accept --dir flag', () => {
const output = execSync(`node ${CLI_PATH} init --help`).toString();
expect(output).toContain('--dir');
});
test('should accept --env flag', () => {
const output = execSync(`node ${CLI_PATH} init --help`).toString();
expect(output).toContain('--env');
});
});
describe('is-unix command', () => {
test('should return exit code 0 or 1', () => {
try {
execSync(`node ${CLI_PATH} is-unix`);
// If it succeeds, exit code is 0 (tr available)
expect(true).toBe(true);
} catch (error) {
// If it fails, exit code is 1 (tr not available)
expect(error.status).toBe(1);
}
});
});
describe('Parameter validation', () => {
test('should accept all parameter flags together', () => {
const output = execSync(`node ${CLI_PATH} get --help`).toString();
expect(output).toContain('--user');
expect(output).toContain('--repo');
expect(output).toContain('--branch');
expect(output).toContain('--agent');
});
test('should show correct help text for branch parameter', () => {
const output = execSync(`node ${CLI_PATH} get --help`).toString();
expect(output).toContain('--branch <branch>');
expect(output).toContain('Git branch name');
});
test('should include branch parameter in help options order', () => {
const output = execSync(`node ${CLI_PATH} get --help`).toString();
const lines = output.split('\n');
const userLineIndex = lines.findIndex(line => line.includes('--user <user>'));
const repoLineIndex = lines.findIndex(line => line.includes('--repo <repo>'));
const branchLineIndex = lines.findIndex(line => line.includes('--branch <branch>'));
const agentLineIndex = lines.findIndex(line => line.includes('--agent <agent>'));
// Verify that branch comes after repo and before agent
expect(branchLineIndex).toBeGreaterThan(repoLineIndex);
expect(branchLineIndex).toBeLessThan(agentLineIndex);
});
test('should handle branch parameter in command line parsing', () => {
// This test verifies that the CLI can parse the branch parameter without errors
// We can't easily test the actual HTTP request without mocking, but we can test parsing
try {
// This should not throw an error even though it will fail due to missing auth
execSync(`node ${CLI_PATH} get /api/mcp/optimus_init.md --user test --repo test --branch main --agent Cursor`, { stdio: 'pipe' });
} catch (error) {
// We expect this to fail due to authentication, but not due to parsing errors
expect(error.message).not.toContain('Unknown option');
expect(error.message).not.toContain('Invalid option');
}
});
});
});