import { describe, expect, it } from 'vitest' import recordings from './fixtures/source-control-launch-recordings.json' import { normalizeSourceControlActionLaunchPreferences } from './source-control-ai-actions' import { sourceControlActionLaunchOptions } from './source-control-action-launch-preferences' import type { SessionOptionValue } from './native-chat/input/native-chat-session-options' import { resolveAgentLaunchCommand, terminalSessionOptionInputs } from './tui-agent-launch-command' import { tokenizeStartupCommand } from './tui-agent-startup-shell' import type { TuiAgent } from './tui-agent' describe.each(['posix', 'powershell', 'cmd'] as const)('argument values on %s', (shell) => { function launchAgent( agent: TuiAgent, sessionOptions: Record, agentArgs: string, sessionOptionsOverrideAgentArgs = false ) { const result = resolveAgentLaunchCommand({ agent, cmdOverrides: {}, platform: shell === 'posix' ? 'linux' : 'win32', shell, sessionOptions, agentArgs, sessionOptionsOverrideAgentArgs }) if (!result.ok) { throw new Error(result.error) } return { ...result, argv: tokenizeStartupCommand(result.command, shell) } } function launch(agentArgs: string, sessionOptionsOverrideAgentArgs = false) { return launchAgent( 'claude', { model: 'opus', effort: 'xhigh' }, agentArgs, sessionOptionsOverrideAgentArgs ) } it('compiles the saved Cursor Thinking choice to the recorded real child arguments', () => { const capture = recordings.cursor const options = sourceControlActionLaunchOptions( { agentId: 'cursor', launchPreferences: normalizeSourceControlActionLaunchPreferences(capture.launchPreferences) }, 'cursor' ) const startup = terminalSessionOptionInputs({ agent: 'cursor', sessionOptions: options.sessionOptions, yieldToAgentArgs: options.sessionOptionsYieldToAgentArgs, agentArgs: capture.agentArgs, shell }) const result = launchAgent( 'cursor', startup.sessionOptions ?? {}, capture.agentArgs, startup.sessionOptionsOverrideAgentArgs ) expect(result.argv).toMatchObject({ ok: true, tokens: ['cursor-agent', ...capture.argv] }) }) it('replaces real worker flags while preserving flag-like prompt text', () => { const result = launch( '--append-system-prompt "--model=haiku" --system-prompt "--effort=low" --model sonnet --effort low', true ) expect(result.argv).toMatchObject({ ok: true, tokens: [ 'claude', '--append-system-prompt', '--model=haiku', '--system-prompt', '--effort=low', '--model', 'opus', '--effort', 'xhigh' ] }) expect(result.appliedSessionOptions).toEqual({ model: 'opus', effort: 'xhigh' }) }) it('records policy without defeated selectors, protecting values and the terminator', () => { const result = launchAgent( 'claude', { model: 'opus', permissionMode: 'ask' }, '--append-system-prompt "--dangerously-skip-permissions" --model haiku --dangerously-skip-permissions -- literal', true ) const tokens = [ '--append-system-prompt', '--dangerously-skip-permissions', '--dangerously-skip-permissions', '--', 'literal' ] expect(tokenizeStartupCommand(result.resumeLaunch.agentCommand, shell)).toMatchObject({ ok: true, tokens: ['claude', ...tokens] }) expect(tokenizeStartupCommand(result.resumeLaunch.agentArgs ?? '', shell)).toMatchObject({ ok: true, tokens }) }) it('recognizes a real model flag after a prompt whose value is --', () => { const result = launch('--append-system-prompt "--" --model haiku') expect(result.argv).toMatchObject({ ok: true, tokens: ['claude', '--append-system-prompt', '--', '--model', 'haiku'] }) expect(result.appliedSessionOptions).toEqual({}) }) it('inserts worker picks after a -- prompt value and before the real terminator', () => { const result = launch('--append-system-prompt "--" --model haiku -- literal', true) expect(result.argv).toMatchObject({ ok: true, tokens: [ 'claude', '--append-system-prompt', '--', '--model', 'opus', '--effort', 'xhigh', '--', 'literal' ] }) expect(result.appliedSessionOptions).toEqual({ model: 'opus', effort: 'xhigh' }) }) it('recognizes a real effort flag after flag-like prompt text', () => { const result = launch('--system-prompt "--effort=low" --effort max') expect(result.argv).toMatchObject({ ok: true, tokens: ['claude', '--model', 'opus', '--system-prompt', '--effort=low', '--effort', 'max'] }) expect(result.appliedSessionOptions).toEqual({ model: 'opus' }) }) it('recognizes a model flag after an option with an inline value', () => { const result = launch('--append-system-prompt=literal --model haiku') expect(result.argv).toMatchObject({ ok: true, tokens: ['claude', '--append-system-prompt=literal', '--model', 'haiku'] }) expect(result.appliedSessionOptions).toEqual({}) }) it('keeps the Cursor model when its API key starts with -m', () => { const result = launchAgent('cursor', { model: 'auto' }, '--api-key "-my-test-key"') expect(result.argv).toMatchObject({ ok: true, tokens: ['cursor-agent', '--model', 'auto', '--api-key', '-my-test-key'] }) expect(result.appliedSessionOptions).toEqual({ model: 'auto' }) }) })