Files
orca/src/cli/index.test.ts
T
Neil 9367169888 refactor(tests): split every oversized test file off the max-lines suppression list (#14728)
* refactor(tests): split oversized test files off the max-lines suppression list

Every `*.test.ts`/`*.spec.ts` that carried an `eslint/oxlint-disable max-lines`
directive is now split into focused, behavior-scoped suites that fit the 800-line
test budget, with shared setup extracted into co-located `*-test-harness.ts` /
`*-test-fixtures.ts` modules (300-line budget). 83 files became ~930; the largest
output is 797 effective lines. `orca-runtime.test.ts` is intentionally untouched.

Test bodies were moved by scripted line-range slicing rather than retyped, so
assertions are byte-identical. The only permitted body edits were mechanical
rebinding where a shared value moved into a harness (e.g. `tmpHome` ->
`homes.tmpHome`).

Registries that enumerate test files were updated in lockstep:
- config/max-lines-baseline.txt: pruned 341 -> 258 entries (all 83 removed).
- config/reliability-gates.jsonc: 33 gates repointed at the split files, with
  assertionRefs split per file where a gate's coverage now spans several.
- .github/workflows/pr.yml: the real-zsh lane now lists the 4 split files that
  actually exercise zsh, so they keep running in the dedicated shell lane.

Also renamed agent-hooks `server-test-fixtures.ts` to `server.test-fixtures.ts`
so the global-fetch call-site audit keeps skipping it, and added `.js` extensions
to the CLI suites' dynamic harness imports (node16 resolution) to unbreak
`build:cli`.

Verification: full suite 52,449 passing vs 52,448 at baseline with zero
assertions lost; `pnpm lint`, `pnpm typecheck`, and `pnpm build:cli` all exit 0;
the terminal-pane e2e spec runs 31/31 headless.

* refactor(tests): split hook-idle arbitration suite that oxfmt pushed over budget

The pre-commit oxfmt pass reflowed pty-connection-hook-idle-arbitration.test.ts
to 811 effective lines, 11 over the test budget. Split the hook-completion side
effect and replacement-agent veto cases into their own suite; both files now sit
well under the cap and the 15 tests are unchanged.

* test: port upstream test changes into the split files after rebase

Rebasing onto main surfaced 27 tests that main had added to files this branch
deleted, plus edits to tests that had already moved. Taking the deletion side of
those modify/delete conflicts would have dropped that coverage silently, so each
upstream change is ported into the split file that now owns the behavior — for
example main's six orchestration mailbox tests land across orchestration-runs,
-send, and -check.

Also repoints `orchestration.notification-mailbox-consistency`, a gate main added
after this branch's gate remap, at those same three split files, and re-prunes
the max-lines baseline against main's (257 entries).

Verified: all 27 upstream test titles present; full suite 52,761 passing with the
only diff vs baseline being 12 tests main itself removed and 3 that moved from
skipped to passing; lint and typecheck exit 0.

* fix(test): flush pending continuations before tearing down terminal test globals

CI shard 5/16 failed on both Node 24 and 26 with `ReferenceError: window is not
defined` from pty-connection.ts, surfacing through
pty-connection-daemon-snapshot-replay.test.ts.

The reattach/settle chains `await` a real promise and then touch `window.api`.
Under fake timers those continuations cannot run, so they only become schedulable
once restoreTerminalTestGlobals() switches back to real timers — which previously
happened immediately before `delete globalThis.window`, so a late continuation
threw and failed the whole file. Flush async ticks in that window instead.

This is latent in the source rather than new: the pre-split 25k-line file kept
running other tests after these, which gave the chains time to settle before
teardown. Splitting the file moved teardown directly behind them.

* fix(test): keep an inert window after terminal test teardown instead of deleting it

The async-tick flush was not enough: the reattach/settle chain can resolve after
teardown regardless of how long we drain, so CI shard 5/16 still failed with
`ReferenceError: window is not defined` from pty-connection.ts.

A real renderer never loses `window`, so deleting it was the artificial part.
Swap in an inert proxy whose properties resolve to callables and whose calls
resolve to undefined, making a late `window.api.pty.*` call a harmless no-op.
The next test replaces it wholesale via installTerminalTestGlobals(), and no test
asserts that `window` is absent.
2026-08-15 00:54:20 -07:00

504 lines
19 KiB
TypeScript

import { afterEach, beforeEach, 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 { COMMAND_SPECS, main } from './index'
import { GLOBAL_FLAGS, specPaths } from './args'
import { okFixture, queueFixtures } from './test-fixtures'
describe('COMMAND_SPECS collision check', () => {
it('has no duplicate command or alias paths', () => {
// Why: first-match resolution would silently shadow duplicate aliases.
const seen = new Set<string>()
for (const spec of COMMAND_SPECS) {
for (const path of specPaths(spec)) {
const key = path.join(' ')
expect(seen.has(key), `Duplicate command/alias path: "${key}"`).toBe(false)
seen.add(key)
}
}
})
it('allows every flag documented in command usage strings', () => {
const flagPattern = /--([a-zA-Z0-9-]+)/g
for (const spec of COMMAND_SPECS) {
const allowed = new Set([...GLOBAL_FLAGS, ...spec.allowedFlags])
for (const match of spec.usage.matchAll(flagPattern)) {
const flag = match[1]
expect(
allowed.has(flag),
`Documented flag --${flag} is not allowed for command: ${spec.path.join(' ')}`
).toBe(true)
}
}
})
})
describe('command aliases dispatch to the canonical handler', () => {
let logSpy: ReturnType<typeof vi.spyOn>
beforeEach(() => {
callMock.mockReset()
logSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
})
afterEach(() => {
callMock.mockReset()
// Why: restore console.log so a downstream describe's vi.spyOn starts from a
// clean spy — otherwise this block's --json output leaks into its calls[0].
logSpy.mockRestore()
})
it('runs `worktree remove` as the canonical `worktree rm` (the incident)', async () => {
queueFixtures(callMock, okFixture('req', { removed: true }))
await main(['worktree', 'remove', '--worktree', 'id:wt-1', '--force', '--json'], '/tmp/repo')
expect(callMock).toHaveBeenCalledWith(
'worktree.rm',
expect.objectContaining({ worktree: 'id:wt-1', force: true })
)
})
it('runs `worktree delete` as the canonical `worktree rm`', async () => {
queueFixtures(callMock, okFixture('req', { removed: true }))
await main(['worktree', 'delete', '--worktree', 'id:wt-1', '--json'], '/tmp/repo')
expect(callMock).toHaveBeenCalledWith(
'worktree.rm',
expect.objectContaining({ worktree: 'id:wt-1' })
)
})
it('still runs `terminal focus` after the handler de-duplication', async () => {
queueFixtures(callMock, okFixture('req', { focus: { ok: true } }))
await main(['terminal', 'focus', '--terminal', 'term_abc', '--json'], '/tmp/repo')
expect(callMock).toHaveBeenCalledWith(
'terminal.focus',
expect.objectContaining({ navigation: 'host' })
)
})
it('serves `agent-context --json` without contacting the runtime', async () => {
runtimeClientConstructorMock.mockClear()
await main(['agent-context', '--json'], '/tmp/repo')
// Why: pure local read — proves the SSH/offline property (no RPC).
expect(runtimeClientConstructorMock).not.toHaveBeenCalled()
expect(callMock).not.toHaveBeenCalled()
const schema = JSON.parse(String(logSpy.mock.calls.at(-1)?.[0]))
expect(schema.schemaVersion).toBe(1)
const rm = schema.commands.find(
(command: { command: string }) => command.command === 'worktree rm'
)
expect(rm.aliases).toContainEqual(['worktree', 'remove'])
})
it('keeps `agent-context` local when remote environment variables are set', async () => {
vi.stubEnv('ORCA_PAIRING_CODE', 'pairing-code')
vi.stubEnv('ORCA_ENVIRONMENT', 'stale-environment')
try {
await main(['agent-context', '--json'], '/tmp/repo')
expect(process.exitCode).not.toBe(1)
expect(callMock).not.toHaveBeenCalled()
} finally {
vi.unstubAllEnvs()
}
})
})
describe('artifact runtime routing', () => {
afterEach(() => {
vi.unstubAllEnvs()
vi.restoreAllMocks()
process.exitCode = 0
})
it('uses the desktop runtime despite remote-selection environment fallbacks', async () => {
vi.stubEnv('ORCA_ENVIRONMENT', 'remote-environment')
vi.stubEnv('ORCA_PAIRING_CODE', 'remote-pairing-code')
vi.spyOn(console, 'log').mockImplementation(() => undefined)
callMock.mockResolvedValue(okFixture('artifact-list', { status: 'ok', value: [] }))
runtimeClientConstructorMock.mockClear()
await main(['artifacts', 'list', '--json'], '/folder-workspace')
expect(process.exitCode).not.toBe(1)
expect(runtimeClientConstructorMock).toHaveBeenCalledWith(null, null)
expect(callMock).toHaveBeenCalledWith('artifacts.list', {})
})
})
describe('unknown command surfaces a suggestion', () => {
let errorSpy: ReturnType<typeof vi.spyOn>
beforeEach(() => {
callMock.mockReset()
errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
})
afterEach(() => {
errorSpy.mockRestore()
process.exitCode = 0
})
it('prints did-you-mean for a near-miss command and exits non-zero', async () => {
await main(['worktree', 'remov'], '/tmp/repo')
expect(process.exitCode).toBe(1)
const stderr = errorSpy.mock.calls.map((call) => String(call[0])).join('\n')
expect(stderr).toContain('Unknown command: worktree remov')
expect(stderr).toContain('orca worktree')
})
it('reports a mistyped pre-command flag without swallowing the command', async () => {
await main(['--jso', 'worktree', 'list'], '/tmp/repo')
expect(process.exitCode).toBe(1)
const stderr = errorSpy.mock.calls.map((call) => String(call[0])).join('\n')
expect(stderr).toContain('Unknown flag --jso for command: worktree list')
expect(stderr).toContain('--json')
})
it('reports a pre-command flag that belongs to another command', async () => {
await main(['--workspace', 'worktree', 'list'], '/tmp/repo')
expect(process.exitCode).toBe(1)
const stderr = errorSpy.mock.calls.map((call) => String(call[0])).join('\n')
expect(stderr).toContain('Unknown flag --workspace for command: worktree list')
})
it('reports a pre-command typo when a global flag splits the command path', async () => {
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
await main(['--jso', 'worktree', '--json', 'list'], '/tmp/repo')
expect(process.exitCode).toBe(1)
expect(logSpy.mock.calls.flat().join('\n')).toContain(
'Unknown flag --jso for command: worktree list'
)
expect(callMock).not.toHaveBeenCalled()
logSpy.mockRestore()
})
it.each(['environment', 'pairing-code'])(
'rejects --%s without a selector before runtime construction',
async (flag) => {
runtimeClientConstructorMock.mockClear()
await main([`--${flag}`, 'worktree', 'list'], '/tmp/repo')
expect(process.exitCode).toBe(1)
const stderr = errorSpy.mock.calls.map((call) => String(call[0])).join('\n')
expect(stderr).toContain(`Flag --${flag} requires a value.`)
expect(runtimeClientConstructorMock).not.toHaveBeenCalled()
expect(callMock).not.toHaveBeenCalled()
}
)
})
describe('unknown help command surfaces a suggestion', () => {
it.each([
['help prefix', ['help', 'worktree', 'remov']],
['help flag', ['worktree', 'remov', '--help']]
])('prints did-you-mean for the %s form', async (_label, argv) => {
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
await main(argv, '/tmp/repo')
expect(process.exitCode).toBe(1)
expect(logSpy.mock.calls.flat().join('\n')).toContain('Did you mean: orca worktree')
logSpy.mockRestore()
process.exitCode = 0
})
})
describe('orca root help', () => {
it('advertises machine-readable agent discovery', async () => {
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
await main([], '/tmp/repo')
expect(logSpy.mock.calls.flat().join('\n')).toContain('agent-context')
logSpy.mockRestore()
})
it('advertises host-local account management', async () => {
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
await main([], '/tmp/repo')
expect(logSpy.mock.calls.flat().join('\n')).toContain(
'account add Add a managed Claude or Codex account on this Orca host'
)
expect(logSpy.mock.calls.flat().join('\n')).toContain(
'account list List managed Claude and Codex accounts on this Orca host'
)
logSpy.mockRestore()
})
it('advertises computer-use capabilities discovery', async () => {
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
await main(['--help'], '/tmp/repo')
expect(logSpy.mock.calls[0][0]).toContain(
'computer capabilities Show computer-use provider capabilities'
)
expect(logSpy.mock.calls[0][0]).toContain(
'computer permissions Show or open computer-use permission setup'
)
expect(logSpy.mock.calls[0][0]).toContain(
'computer press-key Press a single key such as Return or Escape'
)
expect(logSpy.mock.calls[0][0]).toContain(
'project setup-existing-folder Make a project available on a host by importing an existing folder'
)
expect(logSpy.mock.calls[0][0]).toContain(
'project setup-create Create independent project host setup metadata'
)
expect(logSpy.mock.calls[0][0]).toContain(
'project setup-update Update project host setup metadata'
)
expect(logSpy.mock.calls[0][0]).toContain(
'project setup-delete Remove a project host setup'
)
expect(logSpy.mock.calls[0][0]).toContain('Agent Sessions And Worktrees:')
expect(logSpy.mock.calls[0][0]).toContain(
'`worktree create --agent` creates a new checkout with an agent.'
)
expect(logSpy.mock.calls[0][0]).toContain(
'orca terminal create --worktree active --command "codex"'
)
expect(logSpy.mock.calls[0][0]).toContain(
'orchestration worker-start Start a supervised worker locally or on a connected Orca server'
)
expect(logSpy.mock.calls[0][0]).toContain(
'orchestration ask Ask the coordinator a blocking question'
)
expect(logSpy.mock.calls[0][0]).toContain(
'orchestration worker-abandon Fence an uncertain worker without claiming it stopped'
)
expect(logSpy.mock.calls[0][0]).toContain(
"orchestration worker-release Release a settled worker's terminal after archiving its output"
)
expect(logSpy.mock.calls[0][0]).toContain(
'orchestration worker-retain Keep a worker terminal live for debugging'
)
expect(logSpy.mock.calls[0][0]).toContain(
'orchestration worker-list Report worker terminal resource accounting'
)
expect(callMock).not.toHaveBeenCalled()
})
it('progressively discloses Linear commands', async () => {
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
await main(['--help'], '/tmp/repo')
const rootHelp = String(logSpy.mock.calls[0][0])
expect(rootHelp).toContain('Linear:')
expect(rootHelp).toContain('linear Read Linear ticket context for agents')
expect(rootHelp).not.toContain('linear issue')
expect(rootHelp).not.toContain('linear search')
logSpy.mockClear()
await main(['linear', '--help'], '/tmp/repo')
const groupHelp = String(logSpy.mock.calls[0][0])
expect(groupHelp).toContain('orca linear')
expect(groupHelp).toContain('issue')
expect(groupHelp).toContain('search')
expect(groupHelp).not.toContain('--comments')
expect(groupHelp).not.toContain('--attachments')
logSpy.mockClear()
await main(['linear', 'issue', '--help'], '/tmp/repo')
const issueHelp = String(logSpy.mock.calls[0][0])
expect(issueHelp).toContain('orca linear issue [<id>]')
expect(issueHelp).toContain('--comments Include threaded Linear comments')
expect(issueHelp).toContain('--attachments Include attachment metadata and URLs')
expect(issueHelp).toContain('--activity Include issue field-change history')
expect(issueHelp).toContain('--workspace <id> Connected Linear workspace id')
expect(issueHelp).toContain('--id <id> Linear issue key, id, or URL')
logSpy.mockClear()
await main(['linear', 'search', '--help'], '/tmp/repo')
const searchHelp = String(logSpy.mock.calls[0][0])
expect(searchHelp).toContain('orca linear search <query>')
expect(searchHelp).toContain('--workspace <id|all> Connected Linear workspace id, or all')
expect(searchHelp).toContain('--query <text> Text to search across Linear issues')
logSpy.mockClear()
await main(['linear', 'list-issues', '--help'], '/tmp/repo')
const listIssuesHelp = String(logSpy.mock.calls[0][0])
expect(listIssuesHelp).toContain(
'--cursor <cursor> Opaque cursor returned by a previous list-issues page'
)
expect(listIssuesHelp).toContain('--workspace <id|all> Connected Linear workspace id, or all')
expect(listIssuesHelp).not.toContain('Line cursor from a previous read')
expect(callMock).not.toHaveBeenCalled()
})
it('documents the machine-readable terminal topology opt-in', async () => {
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
logSpy.mockClear()
await main(['terminal', 'list', '--help'], '/tmp/repo')
const help = String(logSpy.mock.calls[0][0])
expect(help).toContain('[--include-visual-layouts] [--json]')
expect(help).toContain('--include-visual-layouts Include tab and pane topology in JSON output')
expect(help).toContain('JSON omits visualLayouts by default')
expect(callMock).not.toHaveBeenCalled()
})
it('describes worker-read cursors as opaque', async () => {
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
logSpy.mockClear()
await main(['orchestration', 'worker-read', '--help'], '/tmp/repo')
const help = String(logSpy.mock.calls[0][0])
expect(help).toContain(
'--cursor <cursor> Opaque cursor returned by a previous worker-read page'
)
expect(help).not.toContain('Line cursor from a previous read')
expect(callMock).not.toHaveBeenCalled()
})
it('advertises Linear issue linking on worktree create and set help', async () => {
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
logSpy.mockClear()
await main(['worktree', 'create', '--help'], '/tmp/repo')
expect(String(logSpy.mock.calls[0][0])).toContain('--linear-issue <identifier-or-url>')
logSpy.mockClear()
await main(['worktree', 'set', '--help'], '/tmp/repo')
const setHelp = String(logSpy.mock.calls[0][0])
expect(setHelp).toContain('--linear-issue <identifier-or-url|null>')
expect(setHelp).toContain('--linear-issue <id|url|null> Linked Linear issue identifier or URL')
expect(callMock).not.toHaveBeenCalled()
})
it('advertises explicit orchestration task display labels', async () => {
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
logSpy.mockClear()
await main(['orchestration', 'task-create', '--help'], '/tmp/repo')
const help = String(logSpy.mock.calls[0][0])
expect(help).toContain('[--task-title <text>] [--display-name <text>]')
expect(help).toContain('--task-title <text> Concise title for the orchestration task')
expect(help).toContain('--display-name <text> UI label shown for dispatched worker rows')
expect(callMock).not.toHaveBeenCalled()
})
it('hides removed parent-workspace help and scopes create parent selectors', async () => {
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
logSpy.mockClear()
await main(['--help'], '/tmp/repo')
const rootHelp = String(logSpy.mock.calls[0][0])
expect(rootHelp).not.toContain('--parent-workspace')
expect(rootHelp).toContain('[--parent-worktree <selector>] [--no-parent]')
logSpy.mockClear()
await main(['worktree', 'create', '--help'], '/tmp/repo')
const createHelp = String(logSpy.mock.calls[0][0])
expect(createHelp).not.toContain('--parent-workspace')
expect(createHelp).not.toContain('checkout/workspace')
expect(createHelp).not.toContain('caller workspace')
expect(createHelp).not.toContain('current workspace')
expect(createHelp).not.toContain('active Orca workspace')
expect(createHelp).not.toContain('folderWorkspaceId')
expect(createHelp).toContain('folder:<id>')
expect(createHelp).toContain('folder:<folderId>')
expect(createHelp).toContain('worktree:<worktreeId>')
expect(createHelp).toContain(
'--no-parent only affects Orca lineage; omit --base-branch to use the repo default base'
)
logSpy.mockClear()
await main(['worktree', 'set', '--help'], '/tmp/repo')
const setHelp = String(logSpy.mock.calls[0][0])
expect(setHelp).not.toContain('--parent-workspace')
expect(setHelp).not.toContain('folder:<id>')
expect(setHelp).not.toContain('worktree:<id>')
expect(callMock).not.toHaveBeenCalled()
})
it('distinguishes new worktrees from fresh agent terminals in command help', async () => {
const logSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
logSpy.mockClear()
await main(['worktree', 'create', '--help'], '/tmp/repo')
expect(String(logSpy.mock.calls[0][0])).toContain('This creates a new checkout.')
expect(String(logSpy.mock.calls[0][0])).toContain(
'orca terminal create --worktree active --command "codex"'
)
logSpy.mockClear()
await main(['terminal', 'create', '--help'], '/tmp/repo')
const terminalHelp = String(logSpy.mock.calls[0][0])
expect(terminalHelp).toContain('Use this, not worktree create')
expect(terminalHelp).toContain(
'orca terminal create --worktree active --command "codex" --json'
)
expect(callMock).not.toHaveBeenCalled()
})
})