Files
orca/src/main/providers/ssh-git-provider-diff.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

503 lines
16 KiB
TypeScript

import { describe, expect, it, beforeEach } from 'vitest'
import { SshGitProvider } from './ssh-git-provider'
import {
createMockMux,
waitForRequestCount,
type MockMultiplexer
} from './ssh-git-provider-test-harness'
function deferredValue<T>(value: T): { promise: Promise<T>; resolve: () => void } {
let resolve!: (value: T) => void
const promise = new Promise<T>((innerResolve) => {
resolve = innerResolve
})
return { promise, resolve: () => resolve(value) }
}
describe('SshGitProvider', () => {
let mux: MockMultiplexer
let provider: SshGitProvider
beforeEach(() => {
mux = createMockMux()
provider = new SshGitProvider('conn-1', mux as never)
})
it('getDiff sends git.diff request', async () => {
const diffResult = { kind: 'text', originalContent: '', modifiedContent: 'hello' }
mux.request.mockResolvedValue(diffResult)
const result = await provider.getDiff('/home/user/repo', 'src/index.ts', true)
expect(mux.request).toHaveBeenCalledWith('git.diff', {
worktreePath: '/home/user/repo',
filePath: 'src/index.ts',
staged: true,
// Why: opts into response streaming; a small result still comes back as a
// single frame (relay decides), and old relays ignore the flag.
__streamResponse: true
})
expect(result).toEqual(diffResult)
})
it('getBranchDiff sends git.branchDiff request', async () => {
const diffs = [{ kind: 'text', originalContent: '', modifiedContent: 'new' }]
mux.request.mockResolvedValue(diffs)
const result = await provider.getBranchDiff('/home/user/repo', 'main')
expect(mux.request).toHaveBeenCalledWith('git.branchDiff', {
worktreePath: '/home/user/repo',
baseRef: 'main',
__streamResponse: true
})
expect(result).toEqual(diffs)
})
it('getBranchDiff forwards a pinned head OID without adding it when omitted', async () => {
const headOid = 'a'.repeat(40)
mux.request.mockResolvedValue([])
await provider.getBranchDiff('/home/user/repo', 'main', {
includePatch: true,
filePath: 'src/file.ts',
headOid
})
await provider.getBranchDiff('/home/user/repo', 'main', {
includePatch: true,
filePath: 'src/other-file.ts',
headOid: undefined
})
expect(mux.request).toHaveBeenNthCalledWith(1, 'git.branchDiff', {
worktreePath: '/home/user/repo',
baseRef: 'main',
includePatch: true,
filePath: 'src/file.ts',
headOid,
__streamResponse: true
})
expect(mux.request).toHaveBeenNthCalledWith(2, 'git.branchDiff', {
worktreePath: '/home/user/repo',
baseRef: 'main',
includePatch: true,
filePath: 'src/other-file.ts',
__streamResponse: true
})
})
// Why: an unpinned compare snapshot reaches getBranchDiff as null despite the type.
it('omits a null head OID and shares the absent-head dedupe key', async () => {
const diffs = [{ kind: 'text', originalContent: 'old', modifiedContent: 'new' }]
const pendingDiff = deferredValue(diffs)
const headOid = 'a'.repeat(40)
mux.request.mockReturnValue(pendingDiff.promise)
const reads = [
provider.getBranchDiff('/home/user/repo', 'main', {
includePatch: true,
filePath: 'src/file.ts',
headOid: null as unknown as undefined
}),
provider.getBranchDiff('/home/user/repo', 'main', {
includePatch: true,
filePath: 'src/file.ts'
}),
provider.getBranchDiff('/home/user/repo', 'main', {
includePatch: true,
filePath: 'src/file.ts',
headOid
})
]
await waitForRequestCount(mux.request, 2)
expect(mux.request).toHaveBeenCalledTimes(2)
expect(mux.request).toHaveBeenNthCalledWith(1, 'git.branchDiff', {
worktreePath: '/home/user/repo',
baseRef: 'main',
includePatch: true,
filePath: 'src/file.ts',
__streamResponse: true
})
expect(mux.request).toHaveBeenNthCalledWith(2, 'git.branchDiff', {
worktreePath: '/home/user/repo',
baseRef: 'main',
includePatch: true,
filePath: 'src/file.ts',
headOid,
__streamResponse: true
})
pendingDiff.resolve()
await expect(Promise.all(reads)).resolves.toEqual(Array.from({ length: 3 }, () => diffs))
})
it('coalesces matching branch diff heads while keeping distinct and absent heads separate', async () => {
const diffs = [{ kind: 'text', originalContent: 'old', modifiedContent: 'new' }]
const pendingDiff = deferredValue(diffs)
const firstHeadOid = 'a'.repeat(40)
const secondHeadOid = 'b'.repeat(40)
mux.request.mockReturnValue(pendingDiff.promise)
const reads = [
provider.getBranchDiff('/home/user/repo', 'main', {
includePatch: true,
filePath: 'src/file.ts',
headOid: firstHeadOid
}),
provider.getBranchDiff('/home/user/repo', 'main', {
filePath: 'src/file.ts',
headOid: firstHeadOid,
includePatch: true
}),
provider.getBranchDiff('/home/user/repo', 'main', {
includePatch: true,
filePath: 'src/file.ts',
headOid: secondHeadOid
}),
provider.getBranchDiff('/home/user/repo', 'main', {
includePatch: true,
filePath: 'src/file.ts'
}),
provider.getBranchDiff('/home/user/repo', 'main', {
includePatch: true,
filePath: 'src/file.ts',
headOid: undefined
})
]
await waitForRequestCount(mux.request, 3)
expect(mux.request).toHaveBeenCalledTimes(3)
pendingDiff.resolve()
await expect(Promise.all(reads)).resolves.toEqual(Array.from({ length: 5 }, () => diffs))
})
it('coalesces concurrent identical diff RPCs while in flight', async () => {
const diff = {
kind: 'text',
originalContent: 'old',
modifiedContent: 'new',
originalIsBinary: false,
modifiedIsBinary: false
}
const pendingDiff = deferredValue(diff)
mux.request.mockReturnValue(pendingDiff.promise)
const reads = Array.from({ length: 8 }, () =>
provider.getDiff('/home/user/repo', 'src/file.ts', false, true)
)
await waitForRequestCount(mux.request, 1)
expect(mux.request).toHaveBeenCalledTimes(1)
pendingDiff.resolve()
await expect(Promise.all(reads)).resolves.toEqual(Array.from({ length: 8 }, () => diff))
mux.request.mockReset()
const branchDiffs = [diff]
const pendingBranchDiff = deferredValue(branchDiffs)
mux.request.mockReturnValue(pendingBranchDiff.promise)
const branchReads = Array.from({ length: 8 }, () =>
provider.getBranchDiff('/home/user/repo', 'main', {
includePatch: true,
filePath: 'src/file.ts'
})
)
await waitForRequestCount(mux.request, 1)
expect(mux.request).toHaveBeenCalledTimes(1)
pendingBranchDiff.resolve()
await expect(Promise.all(branchReads)).resolves.toEqual(
Array.from({ length: 8 }, () => branchDiffs)
)
mux.request.mockReset()
const pendingCommitDiff = deferredValue(diff)
mux.request.mockReturnValue(pendingCommitDiff.promise)
const commitReads = Array.from({ length: 8 }, () =>
provider.getCommitDiff('/home/user/repo', {
commitOid: 'c'.repeat(40),
parentOid: 'b'.repeat(40),
filePath: 'src/file.ts'
})
)
await waitForRequestCount(mux.request, 1)
expect(mux.request).toHaveBeenCalledTimes(1)
pendingCommitDiff.resolve()
await expect(Promise.all(commitReads)).resolves.toEqual(Array.from({ length: 8 }, () => diff))
})
it('retries diff RPCs after an in-flight rejection settles', async () => {
const failure = new Error('transient relay failure')
mux.request.mockRejectedValueOnce(failure)
const firstBurst = Array.from({ length: 8 }, () =>
provider.getDiff('/home/user/repo', 'src/file.ts', false, true)
)
await expect(Promise.all(firstBurst)).rejects.toThrow('transient relay failure')
expect(mux.request).toHaveBeenCalledTimes(1)
const diff = {
kind: 'text',
originalContent: 'old',
modifiedContent: 'new',
originalIsBinary: false,
modifiedIsBinary: false
}
mux.request.mockResolvedValueOnce(diff)
await expect(provider.getDiff('/home/user/repo', 'src/file.ts', false, true)).resolves.toBe(
diff
)
expect(mux.request).toHaveBeenCalledTimes(2)
})
it('clears pending diff RPCs when status runs', async () => {
const diff = {
kind: 'text',
originalContent: 'old',
modifiedContent: 'new',
originalIsBinary: false,
modifiedIsBinary: false
}
const pendingDiff = deferredValue(diff)
mux.request.mockReturnValueOnce(pendingDiff.promise)
const first = provider.getDiff('/home/user/repo', 'src/file.ts', false, true)
await waitForRequestCount(mux.request, 1)
mux.request.mockResolvedValueOnce({ entries: [], conflictOperation: 'unknown' })
await provider.getStatus('/home/user/repo')
mux.request.mockResolvedValueOnce(diff)
const second = provider.getDiff('/home/user/repo', 'src/file.ts', false, true)
pendingDiff.resolve()
await expect(Promise.all([first, second])).resolves.toEqual([diff, diff])
expect(mux.request).toHaveBeenCalledTimes(3)
})
it('clears pending diff RPCs when submodule status runs', async () => {
const diff = {
kind: 'text',
originalContent: 'old',
modifiedContent: 'new',
originalIsBinary: false,
modifiedIsBinary: false
}
const pendingDiff = deferredValue(diff)
mux.request.mockReturnValueOnce(pendingDiff.promise)
const first = provider.getDiff('/home/user/repo', 'src/file.ts', false, true)
await waitForRequestCount(mux.request, 1)
mux.request.mockResolvedValueOnce({ entries: [], conflictOperation: 'unknown' })
await provider.getSubmoduleStatus('/home/user/repo', 'vendor/lib')
mux.request.mockResolvedValueOnce(diff)
const second = provider.getDiff('/home/user/repo', 'src/file.ts', false, true)
pendingDiff.resolve()
await expect(Promise.all([first, second])).resolves.toEqual([diff, diff])
expect(mux.request).toHaveBeenCalledTimes(3)
})
it('clears pending diff RPCs when a mutation runs', async () => {
const diff = {
kind: 'text',
originalContent: 'old',
modifiedContent: 'new',
originalIsBinary: false,
modifiedIsBinary: false
}
const pendingDiff = deferredValue(diff)
mux.request.mockReturnValueOnce(pendingDiff.promise)
const first = provider.getDiff('/home/user/repo', 'src/file.ts', false, true)
await waitForRequestCount(mux.request, 1)
mux.request.mockResolvedValueOnce(undefined)
await provider.stageFile('/home/user/repo', 'src/file.ts')
mux.request.mockResolvedValueOnce(diff)
const second = provider.getDiff('/home/user/repo', 'src/file.ts', false, true)
pendingDiff.resolve()
await expect(Promise.all([first, second])).resolves.toEqual([diff, diff])
expect(mux.request).toHaveBeenCalledTimes(3)
})
it.each([
[
'clone',
(provider: SshGitProvider) =>
provider.clone(['clone', '--', 'https://example.com/repo.git', 'repo'], '/projects')
],
[
'mutating git.exec',
(provider: SshGitProvider) =>
provider.exec(['commit', '--allow-empty', '-m', 'initialize'], '/home/user/repo')
]
])('clears pending diff RPCs when %s runs', async (_name, mutate) => {
const diff = {
kind: 'text',
originalContent: 'old',
modifiedContent: 'new',
originalIsBinary: false,
modifiedIsBinary: false
}
const pendingDiff = deferredValue(diff)
mux.request.mockReturnValueOnce(pendingDiff.promise)
const first = provider.getDiff('/home/user/repo', 'src/file.ts', false, true)
await waitForRequestCount(mux.request, 1)
mux.request.mockResolvedValueOnce({ stdout: '', stderr: '' })
await mutate(provider)
mux.request.mockResolvedValueOnce(diff)
const second = provider.getDiff('/home/user/repo', 'src/file.ts', false, true)
pendingDiff.resolve()
await expect(Promise.all([first, second])).resolves.toEqual([diff, diff])
expect(mux.request).toHaveBeenCalledTimes(3)
})
it('clears pending branch diff RPCs when a ref-moving provider operation runs', async () => {
const diff = {
kind: 'text',
originalContent: 'old',
modifiedContent: 'new',
originalIsBinary: false,
modifiedIsBinary: false
}
const pendingDiff = deferredValue([diff])
mux.request.mockReturnValueOnce(pendingDiff.promise)
const first = provider.getBranchDiff('/home/user/repo', 'origin/main', {
includePatch: true,
filePath: 'src/file.ts'
})
await waitForRequestCount(mux.request, 1)
mux.request.mockResolvedValueOnce(undefined)
await provider.fetchRemoteTrackingRef(
'/home/user/repo',
'origin',
'main',
'refs/remotes/origin/main'
)
mux.request.mockResolvedValueOnce([diff])
const second = provider.getBranchDiff('/home/user/repo', 'origin/main', {
includePatch: true,
filePath: 'src/file.ts'
})
pendingDiff.resolve()
await expect(Promise.all([first, second])).resolves.toEqual([[diff], [diff]])
expect(mux.request).toHaveBeenCalledTimes(3)
})
it('coalesces logically identical branch and commit diff RPC args regardless of property order', async () => {
const diff = {
kind: 'text',
originalContent: 'old',
modifiedContent: 'new',
originalIsBinary: false,
modifiedIsBinary: false
}
mux.request.mockResolvedValue([diff])
await Promise.all([
provider.getBranchDiff('/home/user/repo', 'main', {
includePatch: true,
filePath: 'src/file.ts',
oldPath: 'src/old-file.ts'
}),
provider.getBranchDiff('/home/user/repo', 'main', {
oldPath: 'src/old-file.ts',
filePath: 'src/file.ts',
includePatch: true
})
])
expect(mux.request).toHaveBeenCalledTimes(1)
mux.request.mockReset()
mux.request.mockResolvedValue(diff)
await Promise.all([
provider.getCommitDiff('/home/user/repo', {
commitOid: 'c'.repeat(40),
parentOid: 'b'.repeat(40),
filePath: 'src/file.ts',
oldPath: 'src/old-file.ts'
}),
provider.getCommitDiff('/home/user/repo', {
oldPath: 'src/old-file.ts',
filePath: 'src/file.ts',
parentOid: 'b'.repeat(40),
commitOid: 'c'.repeat(40)
})
])
expect(mux.request).toHaveBeenCalledTimes(1)
})
it('keeps distinct diff RPC keys independent', async () => {
const diff = {
kind: 'text',
originalContent: 'old',
modifiedContent: 'new',
originalIsBinary: false,
modifiedIsBinary: false
}
mux.request.mockResolvedValue(diff)
await Promise.all([
provider.getDiff('/home/user/repo', 'src/file.ts', false, false),
provider.getDiff('/home/user/repo', 'src/file.ts', true, false),
provider.getDiff('/home/user/repo', 'src/file.ts', false, true),
provider.getBranchDiff('/home/user/repo', 'main', {
includePatch: true,
filePath: 'src/file.ts'
}),
provider.getBranchDiff('/home/user/repo', 'main', {
includePatch: false,
filePath: 'src/file.ts'
}),
provider.getBranchDiff('/home/user/repo', 'main', {
includePatch: true,
filePath: 'src/file.ts',
oldPath: 'src/old-file.ts'
}),
provider.getBranchDiff('/home/user/repo', 'develop', {
includePatch: true,
filePath: 'src/file.ts'
}),
provider.getCommitDiff('/home/user/repo', {
commitOid: 'c'.repeat(40),
parentOid: 'b'.repeat(40),
filePath: 'src/file.ts'
}),
provider.getCommitDiff('/home/user/repo', {
commitOid: 'c'.repeat(40),
parentOid: 'a'.repeat(40),
filePath: 'src/file.ts'
}),
provider.getCommitDiff('/home/user/repo', {
commitOid: 'c'.repeat(40),
parentOid: 'b'.repeat(40),
filePath: 'src/file.ts',
oldPath: 'src/old-file.ts'
})
])
expect(mux.request).toHaveBeenCalledTimes(10)
})
})