Files
archived-orca/src/cli/index-worktree-create-agent.test.ts
T
Brennan BensonandOrca QA c18c358fce Keep child-worktree creation in the caller’s project (#35787)
* fix(cli): keep inferred worktree creation in the caller project

* Fix caller checkout ranking and creation guidance

* Share caller-host containment preparation across CLI selections

* Correct Windows CLI fixture syntax and snapshot command

* Match worktree fixture output to typed execution hosts

* Regenerate bundled workspace placement guidance

* Refuse ambiguous workspace ownership and preserve launch replay

* Scope runtime filesystem mocks to their aggregate tests

* Preserve launch proof semantics under strict test diagnostics

* Keep launch proof imports within their compiler project boundary

* Consolidate journal cleanup test imports

* Adapt launch fixtures to the current host settings contract

* Keep the CLI error formatter under the file-size limit

Move the selector-ambiguity workspace-choice check into selector-ambiguity-recovery.ts, which already owns that error's recovery text. Behavior is unchanged.

* Expect main's concise orchestration output in the workspace-choice CLI tests

Orchestration commands now print the one-line failure on stderr and the workspace choices and next steps on stdout (or compact JSON plus the stderr line with --json), and print concise success fields.

---------

Co-authored-by: Orca QA <orca-qa@users.noreply.github.com>
2026-11-01 13:35:47 -08:00

417 lines
12 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
const {
callMock,
runtimeClientConstructorMock,
serveOrcaAppMock,
getDefaultUserDataPathMock,
addEnvironmentFromPairingCodeMock,
listEnvironmentsMock,
spawnMock
} = vi.hoisted(() => ({
callMock: vi.fn(),
runtimeClientConstructorMock: vi.fn(),
serveOrcaAppMock: vi.fn(),
getDefaultUserDataPathMock: vi.fn(() => '/tmp/orca-user-data'),
addEnvironmentFromPairingCodeMock: vi.fn(),
listEnvironmentsMock: vi.fn(),
spawnMock: vi.fn()
}))
vi.mock('./runtime-client', async () => {
const { createRuntimeClientModuleMock } = await import('./index-test-harness.js')
return createRuntimeClientModuleMock({
callMock,
runtimeClientConstructorMock,
serveOrcaAppMock,
getDefaultUserDataPathMock
})
})
vi.mock('./runtime/environments', () => ({
addEnvironmentFromPairingCode: addEnvironmentFromPairingCodeMock,
listEnvironments: listEnvironmentsMock,
removeEnvironment: vi.fn(),
resolveEnvironment: vi.fn()
}))
vi.mock('child_process', async () => {
const { createChildProcessModuleMock } = await import('./index-test-harness.js')
return createChildProcessModuleMock(spawnMock)
})
import { main } from './index'
import { buildWorktree, okFixture, queueFixtures, worktreeListFixture } from './test-fixtures'
import { useWorktreeAwarenessEnvironment } from './index-test-harness'
import { agentLaunchFixture, agentLaunchHostFixture } from './agent-launch-test-fixtures'
describe('orca cli worktree awareness', () => {
useWorktreeAwarenessEnvironment({
callMock,
serveOrcaAppMock,
getDefaultUserDataPathMock,
addEnvironmentFromPairingCodeMock,
listEnvironmentsMock,
spawnMock
})
it('opts into setup and activation when worktree.create runs hooks', async () => {
queueFixtures(
callMock,
worktreeListFixture([buildWorktree('/tmp/repo', 'main', 'abc', 'repo-1')]),
okFixture('req_create', {
worktree: buildWorktree('/tmp/repo/feature', 'feature', 'abc', 'repo-1')
})
)
vi.spyOn(console, 'log').mockImplementation(() => {})
await main(
['worktree', 'create', '--repo', 'id:repo-1', '--name', 'feature', '--run-hooks', '--json'],
'/tmp/repo'
)
expect(callMock).toHaveBeenNthCalledWith(2, 'worktree.create', {
repo: 'id:repo-1',
name: 'feature',
displayName: 'feature',
displayNameKind: 'user',
baseBranch: undefined,
linkedIssue: undefined,
comment: undefined,
runHooks: true,
activate: true,
navigation: 'host',
parentWorktree: undefined,
cwdParentWorktree: 'id:repo-1::/tmp/repo',
noParent: false,
callerTerminalHandle: undefined,
cliProvenanceRequest: {}
})
})
it('starts an agent worktree through agent.launch, in the background unless activation is explicit', async () => {
process.env.ORCA_WORKTREE_ID = 'repo-1::/tmp/repo'
queueFixtures(
callMock,
worktreeListFixture([
buildWorktree('/tmp/repo', 'main', 'abc', 'repo-2'),
buildWorktree('/tmp/repo', 'main', 'abc', 'repo-1')
]),
agentLaunchHostFixture(),
agentLaunchFixture('repo-1::/tmp/repo/agent-task', 'The inferred parent contexts conflict.'),
okFixture('req_show', {
worktree: buildWorktree('/tmp/repo/agent-task', 'agent-task', 'abc', 'repo-1')
})
)
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
await main(
[
'worktree',
'create',
'--name',
'agent-task',
'--branch',
'DAT-111-agent-task',
'--agent',
'codex',
'--model',
'gpt-5.5',
'--effort',
'high',
'--prompt',
'hi',
'--setup',
'run',
'--json'
],
'/tmp/repo/src'
)
expect(callMock).toHaveBeenNthCalledWith(3, 'agent.launchReplay', {
target: {
kind: 'create-worktree',
create: {
repo: 'id:repo-1',
name: 'agent-task',
displayName: 'agent-task',
displayNameKind: 'user',
baseBranch: undefined,
branchNameOverride: 'DAT-111-agent-task',
linkedIssue: undefined,
comment: undefined,
runHooks: false,
activate: false,
setupDecision: 'run',
parentWorktree: undefined,
cwdParentWorktree: 'id:repo-1::/tmp/repo',
envParentWorkspace: 'worktree:repo-1::/tmp/repo',
noParent: false,
callerTerminalHandle: undefined,
cliProvenanceRequest: {}
}
},
presentation: 'background',
agent: 'codex',
prompt: { text: 'hi', delivery: 'submit' },
sessionOptions: { model: 'gpt-5.5', effort: 'high' },
launchSource: 'cli',
operationId: expect.any(String)
})
expect(callMock).toHaveBeenNthCalledWith(4, 'worktree.show', {
worktree: 'id:repo-1::/tmp/repo/agent-task'
})
const printed = JSON.parse(String(logSpy.mock.calls[0]?.[0]))
expect(printed.result.worktree.path).toBe('/tmp/repo/agent-task')
expect(printed.result.worktree.id).toBe('repo-1::/tmp/repo/agent-task')
expect(printed.result.launch.outcome).toEqual({
kind: 'structured',
sessionId: 'codex_1',
handle: 'agent_1'
})
expect(printed.result.launch.warning).toBe('The inferred parent contexts conflict.')
})
it('rejects an empty retry identity before creating a worktree', async () => {
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
const priorExitCode = process.exitCode
await main(
[
'worktree',
'create',
'--repo',
'id:repo-1',
'--name',
'task',
'--agent',
'codex',
'--retry-launch=',
'--json'
],
'/tmp/repo'
)
expect(process.exitCode).toBe(1)
process.exitCode = priorExitCode
expect(JSON.parse(String(logSpy.mock.calls[0]?.[0])).error).toMatchObject({
code: 'invalid_argument',
message: expect.stringContaining('--retry-launch')
})
expect(callMock).not.toHaveBeenCalled()
})
it.each([
{ capabilities: [] },
{ capabilities: ['agent.launch.v2'] },
{ capabilities: ['agent.launch.v2', 'agent.launch.local-presentation.v1'] }
])(
'falls back to the startup terminal when the host lacks a required launch capability: $capabilities',
async ({ capabilities }) => {
process.env.ORCA_WORKTREE_ID = 'repo-1::/tmp/repo'
queueFixtures(
callMock,
worktreeListFixture([
buildWorktree('/tmp/repo', 'main', 'abc', 'repo-2'),
buildWorktree('/tmp/repo', 'main', 'abc', 'repo-1')
]),
okFixture('req_status', { capabilities }),
okFixture('req_create', {
worktree: buildWorktree('/tmp/repo/agent-task', 'agent-task', 'abc', 'repo-1'),
lineage: null,
warnings: []
})
)
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
await main(
[
'worktree',
'create',
'--name',
'agent-task',
'--agent',
'codex',
'--prompt',
'hi',
'--activate',
'--json'
],
'/tmp/repo/src'
)
expect(callMock).toHaveBeenNthCalledWith(3, 'worktree.create', {
repo: 'id:repo-1',
name: 'agent-task',
displayName: 'agent-task',
displayNameKind: 'user',
baseBranch: undefined,
linkedIssue: undefined,
comment: undefined,
runHooks: false,
activate: true,
navigation: 'host',
parentWorktree: undefined,
cwdParentWorktree: 'id:repo-1::/tmp/repo',
envParentWorkspace: 'worktree:repo-1::/tmp/repo',
noParent: false,
callerTerminalHandle: undefined,
cliProvenanceRequest: {},
startupAgent: 'codex',
startupPrompt: 'hi',
launchSource: 'cli'
})
expect(JSON.parse(String(logSpy.mock.calls[0]?.[0])).result.worktree.id).toBe(
'repo-1::/tmp/repo/agent-task'
)
}
)
it('refuses inferred agent creation before capability lookup or either launch path when projects tie', async () => {
queueFixtures(
callMock,
worktreeListFixture([
buildWorktree('/tmp/repo', 'main', 'abc', 'repo-1'),
buildWorktree('/tmp/repo', 'main', 'abc', 'repo-2')
])
)
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
const priorExitCode = process.exitCode
try {
await main(
['worktree', 'create', '--name', 'task', '--agent', 'codex', '--json'],
'/tmp/repo/src'
)
expect(process.exitCode).toBe(1)
expect(JSON.parse(String(logSpy.mock.calls[0]?.[0])).error.code).toBe('selector_ambiguous')
expect(callMock.mock.calls.map(([method]) => method)).toEqual(['worktree.list'])
} finally {
process.exitCode = priorExitCode
}
})
it('rejects prompt without agent on worktree.create', async () => {
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
const errSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
const priorExitCode = process.exitCode
await main(
['worktree', 'create', '--repo', 'id:repo-1', '--name', 'child', '--prompt', 'hi', '--json'],
'/tmp/repo'
)
expect(callMock).not.toHaveBeenCalled()
expect([...logSpy.mock.calls, ...errSpy.mock.calls].flat().join('\n')).toContain(
'--prompt requires --agent'
)
expect(process.exitCode).toBe(1)
process.exitCode = priorExitCode
})
it('rejects unknown agents on worktree.create', async () => {
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
const priorExitCode = process.exitCode
await main(
['worktree', 'create', '--repo', 'id:repo-1', '--name', 'child', '--agent', 'wat', '--json'],
'/tmp/repo'
)
expect(callMock).not.toHaveBeenCalled()
expect(JSON.parse(String(logSpy.mock.calls[0]?.[0]))).toMatchObject({
ok: false,
error: {
code: 'invalid_argument',
message: 'Unknown TUI agent "wat"'
}
})
expect(process.exitCode).toBe(1)
process.exitCode = priorExitCode
})
it('rejects agent, prompt, and setup flags without values on worktree.create', async () => {
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
const errSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
const priorExitCode = process.exitCode
await main(
['worktree', 'create', '--repo', 'id:repo-1', '--name', 'child', '--agent'],
'/tmp/repo'
)
expect(callMock.mock.calls.some(([method]) => method === 'worktree.create')).toBe(false)
expect([...logSpy.mock.calls, ...errSpy.mock.calls].flat().join('\n')).toContain(
'Missing value for --agent'
)
callMock.mockClear()
logSpy.mockClear()
errSpy.mockClear()
process.exitCode = priorExitCode
await main(
[
'worktree',
'create',
'--repo',
'id:repo-1',
'--name',
'child',
'--agent',
'codex',
'--prompt'
],
'/tmp/repo'
)
expect(callMock.mock.calls.some(([method]) => method === 'worktree.create')).toBe(false)
expect([...logSpy.mock.calls, ...errSpy.mock.calls].flat().join('\n')).toContain(
'--prompt requires a value'
)
callMock.mockClear()
logSpy.mockClear()
errSpy.mockClear()
process.exitCode = priorExitCode
await main(
['worktree', 'create', '--repo', 'id:repo-1', '--name', 'child', '--setup'],
'/tmp/repo'
)
expect(callMock.mock.calls.some(([method]) => method === 'worktree.create')).toBe(false)
expect([...logSpy.mock.calls, ...errSpy.mock.calls].flat().join('\n')).toContain(
'Missing value for --setup'
)
process.exitCode = priorExitCode
})
it('rejects contradictory setup flags on worktree.create', async () => {
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
const errSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
const priorExitCode = process.exitCode
await main(
[
'worktree',
'create',
'--repo',
'id:repo-1',
'--name',
'child',
'--run-hooks',
'--setup',
'skip',
'--json'
],
'/tmp/repo'
)
expect(callMock).not.toHaveBeenCalled()
expect([...logSpy.mock.calls, ...errSpy.mock.calls].flat().join('\n')).toContain(
'Choose either --run-hooks or --setup run'
)
expect(process.exitCode).toBe(1)
process.exitCode = priorExitCode
})
})