|
| 1 | +/** |
| 2 | + * Unit tests for the CLI command adapter. |
| 3 | + */ |
| 4 | + |
| 5 | +import { describe, it, expect, vi } from 'vitest'; |
| 6 | +import { buildYargsCommand } from './cli-command-adapter.js'; |
| 7 | +import { defineCommand, type CommandDefinition } from '../../commands/framework/define-command.js'; |
| 8 | +import { arg } from '../../commands/framework/arg-builder.js'; |
| 9 | + |
| 10 | +// --------------------------------------------------------------------------- |
| 11 | +// Helpers |
| 12 | +// --------------------------------------------------------------------------- |
| 13 | + |
| 14 | +function sessionCommand( |
| 15 | + overrides: Partial<{ name: string; safety: 'read' | 'mutate' | 'none' }> = {}, |
| 16 | +): CommandDefinition { |
| 17 | + return defineCommand({ |
| 18 | + group: 'console', |
| 19 | + name: overrides.name ?? 'exec', |
| 20 | + description: 'Execute code', |
| 21 | + category: 'execution', |
| 22 | + safety: overrides.safety ?? 'mutate', |
| 23 | + scope: 'session', |
| 24 | + args: { |
| 25 | + code: arg.positional({ description: 'Luau source code' }), |
| 26 | + timeout: arg.option({ description: 'Timeout', type: 'number', alias: 'T' }), |
| 27 | + }, |
| 28 | + handler: async (_session, _args) => ({ success: true }), |
| 29 | + }); |
| 30 | +} |
| 31 | + |
| 32 | +function standaloneCommand(): CommandDefinition { |
| 33 | + return defineCommand({ |
| 34 | + group: null, |
| 35 | + name: 'serve', |
| 36 | + description: 'Start the bridge server', |
| 37 | + category: 'infrastructure', |
| 38 | + safety: 'none', |
| 39 | + scope: 'standalone', |
| 40 | + args: { |
| 41 | + port: arg.option({ description: 'Port number', type: 'number', default: 38741 }), |
| 42 | + }, |
| 43 | + handler: async () => ({ started: true }), |
| 44 | + }); |
| 45 | +} |
| 46 | + |
| 47 | +function connectionCommand(): CommandDefinition { |
| 48 | + return defineCommand({ |
| 49 | + group: 'process', |
| 50 | + name: 'list', |
| 51 | + description: 'List sessions', |
| 52 | + category: 'infrastructure', |
| 53 | + safety: 'read', |
| 54 | + scope: 'connection', |
| 55 | + args: {}, |
| 56 | + handler: async () => ({ sessions: [] }), |
| 57 | + }); |
| 58 | +} |
| 59 | + |
| 60 | +/** Mock yargs Argv object that records calls. */ |
| 61 | +function createMockYargs() { |
| 62 | + const positionals: Record<string, any> = {}; |
| 63 | + const options: Record<string, any> = {}; |
| 64 | + |
| 65 | + const mock: any = { |
| 66 | + positionals, |
| 67 | + options, |
| 68 | + positional: vi.fn((name: string, opts: any) => { |
| 69 | + positionals[name] = opts; |
| 70 | + return mock; |
| 71 | + }), |
| 72 | + option: vi.fn((name: string, opts: any) => { |
| 73 | + options[name] = opts; |
| 74 | + return mock; |
| 75 | + }), |
| 76 | + demandCommand: vi.fn(() => mock), |
| 77 | + }; |
| 78 | + |
| 79 | + return mock; |
| 80 | +} |
| 81 | + |
| 82 | +// --------------------------------------------------------------------------- |
| 83 | +// Tests |
| 84 | +// --------------------------------------------------------------------------- |
| 85 | + |
| 86 | +describe('buildYargsCommand', () => { |
| 87 | + describe('command string', () => { |
| 88 | + it('includes positional args in command string', () => { |
| 89 | + const module = buildYargsCommand(sessionCommand()); |
| 90 | + expect(module.command).toBe('exec <code>'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('produces simple command string with no positionals', () => { |
| 94 | + const module = buildYargsCommand(standaloneCommand()); |
| 95 | + expect(module.command).toBe('serve'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('uses command description', () => { |
| 99 | + const module = buildYargsCommand(sessionCommand()); |
| 100 | + expect(module.describe).toBe('Execute code'); |
| 101 | + }); |
| 102 | + }); |
| 103 | + |
| 104 | + describe('builder — arg registration', () => { |
| 105 | + it('registers positional args', () => { |
| 106 | + const module = buildYargsCommand(sessionCommand()); |
| 107 | + const yargs = createMockYargs(); |
| 108 | + (module.builder as any)(yargs); |
| 109 | + |
| 110 | + expect(yargs.positionals.code).toBeDefined(); |
| 111 | + expect(yargs.positionals.code.describe).toBe('Luau source code'); |
| 112 | + expect(yargs.positionals.code.type).toBe('string'); |
| 113 | + expect(yargs.positionals.code.demandOption).toBe(true); |
| 114 | + }); |
| 115 | + |
| 116 | + it('registers command-specific options', () => { |
| 117 | + const module = buildYargsCommand(sessionCommand()); |
| 118 | + const yargs = createMockYargs(); |
| 119 | + (module.builder as any)(yargs); |
| 120 | + |
| 121 | + expect(yargs.options.timeout).toBeDefined(); |
| 122 | + expect(yargs.options.timeout.describe).toBe('Timeout'); |
| 123 | + expect(yargs.options.timeout.type).toBe('number'); |
| 124 | + expect(yargs.options.timeout.alias).toBe('T'); |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + describe('builder — universal args', () => { |
| 129 | + it('injects --target and --context for session-scoped commands', () => { |
| 130 | + const module = buildYargsCommand(sessionCommand()); |
| 131 | + const yargs = createMockYargs(); |
| 132 | + (module.builder as any)(yargs); |
| 133 | + |
| 134 | + expect(yargs.options.target).toBeDefined(); |
| 135 | + expect(yargs.options.target.alias).toBe('t'); |
| 136 | + expect(yargs.options.context).toBeDefined(); |
| 137 | + }); |
| 138 | + |
| 139 | + it('injects --target and --context for connection-scoped commands', () => { |
| 140 | + const module = buildYargsCommand(connectionCommand()); |
| 141 | + const yargs = createMockYargs(); |
| 142 | + (module.builder as any)(yargs); |
| 143 | + |
| 144 | + expect(yargs.options.target).toBeDefined(); |
| 145 | + expect(yargs.options.context).toBeDefined(); |
| 146 | + }); |
| 147 | + |
| 148 | + it('does not inject --target for standalone commands', () => { |
| 149 | + const module = buildYargsCommand(standaloneCommand()); |
| 150 | + const yargs = createMockYargs(); |
| 151 | + (module.builder as any)(yargs); |
| 152 | + |
| 153 | + expect(yargs.options.target).toBeUndefined(); |
| 154 | + expect(yargs.options.context).toBeUndefined(); |
| 155 | + }); |
| 156 | + |
| 157 | + it('always injects --format, --output, --open', () => { |
| 158 | + const module = buildYargsCommand(standaloneCommand()); |
| 159 | + const yargs = createMockYargs(); |
| 160 | + (module.builder as any)(yargs); |
| 161 | + |
| 162 | + expect(yargs.options.format).toBeDefined(); |
| 163 | + expect(yargs.options.output).toBeDefined(); |
| 164 | + expect(yargs.options.open).toBeDefined(); |
| 165 | + }); |
| 166 | + |
| 167 | + it('injects --watch and --interval for read-safety commands', () => { |
| 168 | + const module = buildYargsCommand(connectionCommand()); // safety: 'read' |
| 169 | + const yargs = createMockYargs(); |
| 170 | + (module.builder as any)(yargs); |
| 171 | + |
| 172 | + expect(yargs.options.watch).toBeDefined(); |
| 173 | + expect(yargs.options.watch.alias).toBe('w'); |
| 174 | + expect(yargs.options.interval).toBeDefined(); |
| 175 | + expect(yargs.options.interval.default).toBe(1000); |
| 176 | + }); |
| 177 | + |
| 178 | + it('does not inject --watch for mutate-safety commands', () => { |
| 179 | + const module = buildYargsCommand(sessionCommand({ safety: 'mutate' })); |
| 180 | + const yargs = createMockYargs(); |
| 181 | + (module.builder as any)(yargs); |
| 182 | + |
| 183 | + expect(yargs.options.watch).toBeUndefined(); |
| 184 | + expect(yargs.options.interval).toBeUndefined(); |
| 185 | + }); |
| 186 | + }); |
| 187 | + |
| 188 | + describe('handler — standalone', () => { |
| 189 | + it('calls standalone handler with extracted args', async () => { |
| 190 | + const handler = vi.fn().mockResolvedValue({ started: true }); |
| 191 | + const cmd = defineCommand({ |
| 192 | + group: null, |
| 193 | + name: 'serve', |
| 194 | + description: 'Start server', |
| 195 | + category: 'infrastructure', |
| 196 | + safety: 'none', |
| 197 | + scope: 'standalone', |
| 198 | + args: { |
| 199 | + port: arg.option({ description: 'Port', type: 'number', default: 38741 }), |
| 200 | + }, |
| 201 | + handler, |
| 202 | + }); |
| 203 | + |
| 204 | + const module = buildYargsCommand(cmd); |
| 205 | + await (module.handler as any)({ port: 9999, verbose: false, timeout: 120000 }); |
| 206 | + |
| 207 | + expect(handler).toHaveBeenCalledWith({ port: 9999 }); |
| 208 | + }); |
| 209 | + }); |
| 210 | +}); |
0 commit comments