mirror of
https://github.com/stablyai/orca.git
synced 2026-09-22 16:02:32 +00:00
* 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.
406 lines
15 KiB
TypeScript
406 lines
15 KiB
TypeScript
/**
|
|
* GitHandler working-tree change reporting and mutation: status entries,
|
|
* ignored-path checks, stage/unstage, and discard (single + bulk).
|
|
*/
|
|
import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'
|
|
import * as fs from 'node:fs/promises'
|
|
import * as path from 'node:path'
|
|
import { mkdirSync, mkdtempSync, symlinkSync, writeFileSync } from 'node:fs'
|
|
import { tmpdir } from 'node:os'
|
|
import { execFileSync } from 'node:child_process'
|
|
import type { GitHandler } from './git-handler'
|
|
import { gitInit, gitCommit, type MockDispatcher } from './git-handler-test-setup'
|
|
import {
|
|
createGitHandlerRelay,
|
|
createGitTempDir,
|
|
removeGitTempDir
|
|
} from './git-handler-test-harness'
|
|
|
|
describe('GitHandler', () => {
|
|
let dispatcher: MockDispatcher
|
|
let handler: GitHandler
|
|
let tmpDir: string
|
|
|
|
beforeEach(() => {
|
|
tmpDir = createGitTempDir()
|
|
;({ dispatcher, handler } = createGitHandlerRelay())
|
|
})
|
|
|
|
afterEach(async () => {
|
|
await removeGitTempDir(tmpDir)
|
|
})
|
|
|
|
describe('status', () => {
|
|
it('returns empty entries for clean repo', async () => {
|
|
gitInit(tmpDir)
|
|
writeFileSync(path.join(tmpDir, 'file.txt'), 'hello')
|
|
gitCommit(tmpDir, 'initial')
|
|
|
|
const result = (await dispatcher.callRequest('git.status', { worktreePath: tmpDir })) as {
|
|
entries: Record<string, unknown>[]
|
|
conflictOperation: string
|
|
head?: string
|
|
branch?: string
|
|
}
|
|
expect(result.entries).toEqual([])
|
|
expect(result.conflictOperation).toBe('unknown')
|
|
expect(result.branch).toMatch(/^refs\/heads\//)
|
|
expect(typeof result.head).toBe('string')
|
|
})
|
|
|
|
it('detects untracked files', async () => {
|
|
gitInit(tmpDir)
|
|
writeFileSync(path.join(tmpDir, 'tracked.txt'), 'tracked')
|
|
gitCommit(tmpDir, 'initial')
|
|
writeFileSync(path.join(tmpDir, 'new.txt'), 'new')
|
|
|
|
const result = (await dispatcher.callRequest('git.status', { worktreePath: tmpDir })) as {
|
|
entries: {
|
|
path?: unknown
|
|
status?: unknown
|
|
area?: unknown
|
|
added?: unknown
|
|
removed?: unknown
|
|
}[]
|
|
}
|
|
const untracked = result.entries.find((e) => e.path === 'new.txt')
|
|
expect(untracked).toBeDefined()
|
|
expect(untracked!.status).toBe('untracked')
|
|
expect(untracked!.area).toBe('untracked')
|
|
expect(untracked!.added).toBe(1)
|
|
expect(untracked!.removed).toBeUndefined()
|
|
})
|
|
|
|
it('returns ignored paths only when requested', async () => {
|
|
gitInit(tmpDir)
|
|
writeFileSync(path.join(tmpDir, '.gitignore'), 'dist/\n.env\n')
|
|
gitCommit(tmpDir, 'initial')
|
|
mkdirSync(path.join(tmpDir, 'dist'), { recursive: true })
|
|
writeFileSync(path.join(tmpDir, 'dist', 'bundle.js'), 'compiled')
|
|
writeFileSync(path.join(tmpDir, '.env'), 'TOKEN=secret')
|
|
|
|
const defaultResult = (await dispatcher.callRequest('git.status', {
|
|
worktreePath: tmpDir
|
|
})) as {
|
|
ignoredPaths?: string[]
|
|
}
|
|
const ignoredResult = (await dispatcher.callRequest('git.status', {
|
|
worktreePath: tmpDir,
|
|
includeIgnored: true
|
|
})) as {
|
|
ignoredPaths?: string[]
|
|
}
|
|
|
|
expect('ignoredPaths' in defaultResult).toBe(false)
|
|
expect(ignoredResult.ignoredPaths).toEqual(expect.arrayContaining(['dist/', '.env']))
|
|
})
|
|
|
|
it('checks ignored status for selected paths', async () => {
|
|
gitInit(tmpDir)
|
|
writeFileSync(path.join(tmpDir, '.gitignore'), 'dist/\n.env\n')
|
|
gitCommit(tmpDir, 'initial')
|
|
mkdirSync(path.join(tmpDir, 'dist'), { recursive: true })
|
|
writeFileSync(path.join(tmpDir, 'dist', 'bundle.js'), 'compiled')
|
|
writeFileSync(path.join(tmpDir, '.env'), 'TOKEN=secret')
|
|
|
|
const result = (await dispatcher.callRequest('git.checkIgnored', {
|
|
worktreePath: tmpDir,
|
|
paths: ['dist/bundle.js', 'src/index.ts', '.env']
|
|
})) as string[]
|
|
|
|
expect(result).toEqual(expect.arrayContaining(['dist/bundle.js', '.env']))
|
|
expect(result).not.toContain('src/index.ts')
|
|
})
|
|
|
|
it('detects modified files', async () => {
|
|
gitInit(tmpDir)
|
|
writeFileSync(path.join(tmpDir, 'file.txt'), 'original')
|
|
gitCommit(tmpDir, 'initial')
|
|
writeFileSync(path.join(tmpDir, 'file.txt'), 'modified')
|
|
|
|
const result = (await dispatcher.callRequest('git.status', { worktreePath: tmpDir })) as {
|
|
entries: {
|
|
path?: unknown
|
|
status?: unknown
|
|
area?: unknown
|
|
added?: unknown
|
|
removed?: unknown
|
|
}[]
|
|
}
|
|
const modified = result.entries.find((e) => e.path === 'file.txt')
|
|
expect(modified).toBeDefined()
|
|
expect(modified!.status).toBe('modified')
|
|
expect(modified!.area).toBe('unstaged')
|
|
expect(modified!.added).toBe(1)
|
|
expect(modified!.removed).toBe(1)
|
|
})
|
|
|
|
it('detects staged files', async () => {
|
|
gitInit(tmpDir)
|
|
writeFileSync(path.join(tmpDir, 'file.txt'), 'original')
|
|
gitCommit(tmpDir, 'initial')
|
|
writeFileSync(path.join(tmpDir, 'file.txt'), 'changed')
|
|
execFileSync('git', ['add', 'file.txt'], { cwd: tmpDir, stdio: 'pipe' })
|
|
|
|
const result = (await dispatcher.callRequest('git.status', { worktreePath: tmpDir })) as {
|
|
entries: {
|
|
path?: unknown
|
|
status?: unknown
|
|
area?: unknown
|
|
added?: unknown
|
|
removed?: unknown
|
|
}[]
|
|
}
|
|
const staged = result.entries.find((e) => e.area === 'staged')
|
|
expect(staged).toBeDefined()
|
|
expect(staged!.status).toBe('modified')
|
|
expect(staged!.added).toBe(1)
|
|
expect(staged!.removed).toBe(1)
|
|
})
|
|
|
|
// Why: regression for #1503 — default core.quotePath=true octal-escapes non-ASCII paths (breaks sidebar + blob reads).
|
|
it('preserves UTF-8 paths in status output', async () => {
|
|
gitInit(tmpDir)
|
|
const utf8Dir = path.join(tmpDir, 'docs', '日本語')
|
|
mkdirSync(utf8Dir, { recursive: true })
|
|
writeFileSync(path.join(utf8Dir, 'sample.md'), 'hello')
|
|
|
|
const result = (await dispatcher.callRequest('git.status', { worktreePath: tmpDir })) as {
|
|
entries: Record<string, unknown>[]
|
|
}
|
|
const entry = result.entries.find((e) =>
|
|
typeof e.path === 'string' ? e.path.endsWith('sample.md') : false
|
|
)
|
|
expect(entry).toBeDefined()
|
|
expect(entry!.path).toBe('docs/日本語/sample.md')
|
|
})
|
|
|
|
// Why: regression for #1503 on the porcelain v2 type-1 (tracked+modified) parser branch, which the untracked '?' test misses.
|
|
it('preserves UTF-8 paths for tracked-modified entries', async () => {
|
|
gitInit(tmpDir)
|
|
const utf8Dir = path.join(tmpDir, 'docs', '日本語')
|
|
mkdirSync(utf8Dir, { recursive: true })
|
|
const utf8File = path.join(utf8Dir, 'sample.md')
|
|
writeFileSync(utf8File, 'original')
|
|
gitCommit(tmpDir, 'initial')
|
|
writeFileSync(utf8File, 'modified')
|
|
|
|
const result = (await dispatcher.callRequest('git.status', { worktreePath: tmpDir })) as {
|
|
entries: Record<string, unknown>[]
|
|
}
|
|
const entry = result.entries.find((e) =>
|
|
typeof e.path === 'string' ? e.path.endsWith('sample.md') : false
|
|
)
|
|
expect(entry).toBeDefined()
|
|
expect(entry!.path).toBe('docs/日本語/sample.md')
|
|
expect(entry!.status).toBe('modified')
|
|
expect(entry!.area).toBe('unstaged')
|
|
})
|
|
})
|
|
|
|
describe('stage and unstage', () => {
|
|
it('stages a file', async () => {
|
|
gitInit(tmpDir)
|
|
writeFileSync(path.join(tmpDir, 'file.txt'), 'content')
|
|
gitCommit(tmpDir, 'initial')
|
|
writeFileSync(path.join(tmpDir, 'file.txt'), 'changed')
|
|
|
|
await dispatcher.callRequest('git.stage', { worktreePath: tmpDir, filePath: 'file.txt' })
|
|
|
|
const output = execFileSync('git', ['diff', '--cached', '--name-only'], {
|
|
cwd: tmpDir,
|
|
encoding: 'utf-8'
|
|
})
|
|
expect(output.trim()).toBe('file.txt')
|
|
})
|
|
|
|
it('unstages a file', async () => {
|
|
gitInit(tmpDir)
|
|
writeFileSync(path.join(tmpDir, 'file.txt'), 'content')
|
|
gitCommit(tmpDir, 'initial')
|
|
writeFileSync(path.join(tmpDir, 'file.txt'), 'changed')
|
|
execFileSync('git', ['add', 'file.txt'], { cwd: tmpDir, stdio: 'pipe' })
|
|
|
|
await dispatcher.callRequest('git.unstage', { worktreePath: tmpDir, filePath: 'file.txt' })
|
|
|
|
const output = execFileSync('git', ['diff', '--cached', '--name-only'], {
|
|
cwd: tmpDir,
|
|
encoding: 'utf-8'
|
|
})
|
|
expect(output.trim()).toBe('')
|
|
})
|
|
})
|
|
|
|
describe('discard', () => {
|
|
it('discards changes to tracked file', async () => {
|
|
gitInit(tmpDir)
|
|
writeFileSync(path.join(tmpDir, 'file.txt'), 'original')
|
|
gitCommit(tmpDir, 'initial')
|
|
writeFileSync(path.join(tmpDir, 'file.txt'), 'modified')
|
|
|
|
await dispatcher.callRequest('git.discard', { worktreePath: tmpDir, filePath: 'file.txt' })
|
|
|
|
const content = await fs.readFile(path.join(tmpDir, 'file.txt'), 'utf-8')
|
|
expect(content).toBe('original')
|
|
})
|
|
|
|
it('deletes untracked file on discard', async () => {
|
|
gitInit(tmpDir)
|
|
gitCommit(tmpDir, 'initial')
|
|
writeFileSync(path.join(tmpDir, 'new.txt'), 'untracked')
|
|
|
|
await dispatcher.callRequest('git.discard', { worktreePath: tmpDir, filePath: 'new.txt' })
|
|
await expect(fs.access(path.join(tmpDir, 'new.txt'))).rejects.toThrow()
|
|
})
|
|
|
|
it('treats untracked discard paths with Git glob characters as literal paths', async () => {
|
|
gitInit(tmpDir)
|
|
writeFileSync(path.join(tmpDir, '.gitignore'), 'ignored.log\n')
|
|
gitCommit(tmpDir, 'initial')
|
|
writeFileSync(path.join(tmpDir, '[k]eep.log'), 'selected')
|
|
writeFileSync(path.join(tmpDir, 'keep.log'), 'unrelated')
|
|
writeFileSync(path.join(tmpDir, 'ignored.log'), 'ignored')
|
|
|
|
await dispatcher.callRequest('git.discard', { worktreePath: tmpDir, filePath: '[k]eep.log' })
|
|
|
|
await expect(fs.access(path.join(tmpDir, '[k]eep.log'))).rejects.toThrow()
|
|
await expect(fs.access(path.join(tmpDir, 'keep.log'))).resolves.toBeUndefined()
|
|
await expect(fs.access(path.join(tmpDir, 'ignored.log'))).resolves.toBeUndefined()
|
|
})
|
|
|
|
it('treats tracked discard paths with Git glob characters as literal paths', async () => {
|
|
gitInit(tmpDir)
|
|
writeFileSync(path.join(tmpDir, '[k]eep.log'), 'selected')
|
|
writeFileSync(path.join(tmpDir, 'keep.log'), 'keep')
|
|
gitCommit(tmpDir, 'track log fixtures')
|
|
writeFileSync(path.join(tmpDir, '[k]eep.log'), 'selected modified')
|
|
writeFileSync(path.join(tmpDir, 'keep.log'), 'keep modified')
|
|
|
|
await dispatcher.callRequest('git.discard', { worktreePath: tmpDir, filePath: '[k]eep.log' })
|
|
|
|
await expect(fs.readFile(path.join(tmpDir, '[k]eep.log'), 'utf-8')).resolves.toBe('selected')
|
|
await expect(fs.readFile(path.join(tmpDir, 'keep.log'), 'utf-8')).resolves.toBe(
|
|
'keep modified'
|
|
)
|
|
})
|
|
|
|
it('bulk discards tracked and untracked files', async () => {
|
|
gitInit(tmpDir)
|
|
writeFileSync(path.join(tmpDir, 'a.txt'), 'a')
|
|
writeFileSync(path.join(tmpDir, 'b.txt'), 'b')
|
|
gitCommit(tmpDir, 'initial')
|
|
writeFileSync(path.join(tmpDir, 'a.txt'), 'a-modified')
|
|
writeFileSync(path.join(tmpDir, 'b.txt'), 'b-modified')
|
|
writeFileSync(path.join(tmpDir, 'new.txt'), 'untracked')
|
|
|
|
await dispatcher.callRequest('git.bulkDiscard', {
|
|
worktreePath: tmpDir,
|
|
filePaths: ['a.txt', 'b.txt', 'new.txt']
|
|
})
|
|
|
|
await expect(fs.readFile(path.join(tmpDir, 'a.txt'), 'utf-8')).resolves.toBe('a')
|
|
await expect(fs.readFile(path.join(tmpDir, 'b.txt'), 'utf-8')).resolves.toBe('b')
|
|
await expect(fs.access(path.join(tmpDir, 'new.txt'))).rejects.toThrow()
|
|
})
|
|
|
|
it('handles large tracked path lists during bulk discard classification', async () => {
|
|
const trackedStdout = Array.from({ length: 150_000 }, (_, index) => `docs/file-${index}.ts`)
|
|
.join('\0')
|
|
.concat('\0')
|
|
const gitMock = vi
|
|
.spyOn(
|
|
handler as unknown as {
|
|
git: (args: string[], cwd: string) => Promise<{ stdout: string; stderr: string }>
|
|
},
|
|
'git'
|
|
)
|
|
.mockResolvedValueOnce({ stdout: trackedStdout, stderr: '' })
|
|
.mockResolvedValueOnce({ stdout: '', stderr: '' })
|
|
|
|
await dispatcher.callRequest('git.bulkDiscard', {
|
|
worktreePath: tmpDir,
|
|
filePaths: ['docs']
|
|
})
|
|
|
|
expect(gitMock).toHaveBeenNthCalledWith(
|
|
2,
|
|
['restore', '--worktree', '--source=HEAD', '--', ':(literal)docs'],
|
|
tmpDir
|
|
)
|
|
})
|
|
|
|
it('rejects path traversal', async () => {
|
|
gitInit(tmpDir)
|
|
await expect(
|
|
dispatcher.callRequest('git.discard', {
|
|
worktreePath: tmpDir,
|
|
filePath: '../../../etc/passwd'
|
|
})
|
|
).rejects.toThrow('outside the worktree')
|
|
})
|
|
|
|
it('rejects bulk discard path traversal', async () => {
|
|
gitInit(tmpDir)
|
|
await expect(
|
|
dispatcher.callRequest('git.bulkDiscard', {
|
|
worktreePath: tmpDir,
|
|
filePaths: ['file.txt', '../../../etc/passwd']
|
|
})
|
|
).rejects.toThrow('outside the worktree')
|
|
})
|
|
|
|
it('rejects untracked child paths through symlinked parents', async () => {
|
|
gitInit(tmpDir)
|
|
gitCommit(tmpDir, 'initial')
|
|
const outsideDir = mkdtempSync(path.join(tmpdir(), 'relay-git-outside-'))
|
|
const outsideFile = path.join(outsideDir, 'keep.txt')
|
|
writeFileSync(outsideFile, 'outside')
|
|
symlinkSync(
|
|
outsideDir,
|
|
path.join(tmpDir, 'link'),
|
|
process.platform === 'win32' ? 'junction' : 'dir'
|
|
)
|
|
|
|
try {
|
|
await expect(
|
|
dispatcher.callRequest('git.discard', {
|
|
worktreePath: tmpDir,
|
|
filePath: 'link/keep.txt'
|
|
})
|
|
).rejects.toThrow('outside the worktree')
|
|
await expect(fs.access(outsideFile)).resolves.toBeUndefined()
|
|
} finally {
|
|
await fs.rm(outsideDir, { recursive: true, force: true })
|
|
}
|
|
})
|
|
|
|
it('rejects bulk untracked child paths through symlinked parents before deleting anything', async () => {
|
|
gitInit(tmpDir)
|
|
gitCommit(tmpDir, 'initial')
|
|
const outsideDir = mkdtempSync(path.join(tmpdir(), 'relay-git-outside-'))
|
|
const outsideFile = path.join(outsideDir, 'keep.txt')
|
|
const untrackedFile = path.join(tmpDir, 'new.txt')
|
|
writeFileSync(outsideFile, 'outside')
|
|
writeFileSync(untrackedFile, 'untracked')
|
|
symlinkSync(
|
|
outsideDir,
|
|
path.join(tmpDir, 'link'),
|
|
process.platform === 'win32' ? 'junction' : 'dir'
|
|
)
|
|
|
|
try {
|
|
await expect(
|
|
dispatcher.callRequest('git.bulkDiscard', {
|
|
worktreePath: tmpDir,
|
|
filePaths: ['new.txt', 'link/keep.txt']
|
|
})
|
|
).rejects.toThrow('outside the worktree')
|
|
await expect(fs.access(outsideFile)).resolves.toBeUndefined()
|
|
await expect(fs.access(untrackedFile)).resolves.toBeUndefined()
|
|
} finally {
|
|
await fs.rm(outsideDir, { recursive: true, force: true })
|
|
}
|
|
})
|
|
})
|
|
})
|