diff --git a/src/cli/mm.test.ts b/src/cli/mm.test.ts index 497b9e98..3a8bc4fb 100644 --- a/src/cli/mm.test.ts +++ b/src/cli/mm.test.ts @@ -4149,6 +4149,87 @@ describe('main', () => { process.argv = origArgv; }); + it.each([ + 'launch --help', + 'launch -h', + 'launch --platform ios --help', + 'stop --help', + 'cleanup --shutdown -h', + 'serve --background --help', + 'click --help', + 'status -h', + ])('prints help for `mm %s` without running it', async (command) => { + const { readDaemonState } = await import('../server/daemon-state.js'); + const { spawn } = await import('node:child_process'); + const fetchSpy = vi.spyOn(globalThis, 'fetch'); + + const origArgv = process.argv; + process.argv = ['node', 'mm', ...command.split(' ')]; + + try { + await expect(main()).rejects.toThrowError('process.exit'); + expect(exitSpy).toHaveBeenCalledWith(0); + expect(stdoutSpy).toHaveBeenCalledWith(expect.stringContaining('Usage:')); + expect(stderrSpy).not.toHaveBeenCalled(); + expect(fs.realpath).not.toHaveBeenCalled(); + expect(readDaemonState).not.toHaveBeenCalled(); + expect(spawn).not.toHaveBeenCalled(); + expect(fetchSpy).not.toHaveBeenCalled(); + } finally { + process.argv = origArgv; + } + }); + + it('types --help as text when it is not the first argument', async () => { + const { readDaemonState, isDaemonAlive, isDaemonVersionMatch } = + await import('../server/daemon-state.js'); + vi.mocked(readDaemonState).mockResolvedValueOnce({ + port: 3000, + pid: 123, + nonce: 'abc', + startedAt: '2024-01-01', + version: '1.0.0', + subPorts: {}, + }); + vi.mocked(isDaemonAlive).mockResolvedValueOnce(true); + vi.mocked(isDaemonVersionMatch).mockReturnValueOnce(true); + vi.spyOn(globalThis, 'fetch').mockResolvedValue({ + ok: true, + json: async () => ({ ok: true, result: {} }), + } as Response); + + const origArgv = process.argv; + process.argv = ['node', 'mm', 'type', 'e5', '--help']; + + try { + await main(); + } finally { + process.argv = origArgv; + } + + expect(globalThis.fetch).toHaveBeenCalledWith( + 'http://127.0.0.1:3000/tool/type', + expect.objectContaining({ + body: JSON.stringify({ a11yRef: 'e5', text: '--help' }), + }), + ); + }); + + it('rejects an unknown command before its --help flag', async () => { + const origArgv = process.argv; + process.argv = ['node', 'mm', 'frobnicate', '--help']; + + try { + await expect(main()).rejects.toThrowError('process.exit'); + expectFailure(expect.stringContaining("unknown command 'frobnicate'"), 2); + expect(stdoutSpy).not.toHaveBeenCalledWith( + expect.stringContaining('Usage:'), + ); + } finally { + process.argv = origArgv; + } + }); + it('routes stop command to handleStop', async () => { const { readDaemonState } = await import('../server/daemon-state.js'); vi.mocked(readDaemonState).mockResolvedValueOnce(null); diff --git a/src/cli/mm.ts b/src/cli/mm.ts index 7107652e..e755f6d8 100644 --- a/src/cli/mm.ts +++ b/src/cli/mm.ts @@ -121,6 +121,31 @@ function isKnownCommand( ); } +/** + * Whether the argument is `--help` or `-h`. + * + * @param arg - A CLI argument. + * @returns True for a help flag. + */ +function isHelpFlag(arg: string | undefined): boolean { + return arg === '--help' || arg === '-h'; +} + +/** + * Whether `mm ` asks for help: a help flag right after the + * command, or anywhere for a lifecycle command. Other commands take free + * text, so `mm type e5 --help` types `--help`. + * + * @param command - The command. + * @param args - The arguments after the command. + * @returns True when the command must print help and not run. + */ +function isHelpRequest(command: string, args: string[]): boolean { + return (LIFECYCLE_COMMANDS as readonly string[]).includes(command) + ? args.some(isHelpFlag) + : isHelpFlag(args[0]); +} + const DAEMON_POLL_INTERVAL_MS = 200; const DAEMON_START_TIMEOUT_MS = 30_000; const DAEMON_POLL_MAX_ATTEMPTS = @@ -495,16 +520,11 @@ export async function main(): Promise { resolvePretty(pretty, process.env.MM_PRETTY, process.stdout.isTTY), ); - if ( - remainingArgs.length === 0 || - remainingArgs[0] === '--help' || - remainingArgs[0] === '-h' - ) { + if (remainingArgs.length === 0 || isHelpFlag(remainingArgs[0])) { printHelp(); process.exit(0); } - const worktreeRoot = await resolveWorktreeRoot(projectPath); const args = remainingArgs; const command = args[0]; @@ -512,6 +532,13 @@ export async function main(): Promise { failUsage(`unknown command '${command}'. Run 'mm --help' for usage.`); } + if (isHelpRequest(command, args.slice(1))) { + printHelp(); + process.exit(0); + } + + const worktreeRoot = await resolveWorktreeRoot(projectPath); + if (command === 'serve') { const background = args.includes('--background'); await handleServe(worktreeRoot, background);