mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-11-16 19:36:54 +00:00
Every non-zero gh exit collapsed to a generic "is gh installed and authenticated?" — a lie whenever gh was fine and the real failure was "no commits between main and feature", a missing upstream, or a refused push (#87731). The user had to drop to a terminal to learn what gh already printed. - apps/desktop git-review-ops.ts runGh() now resolves {ok, stdout, stderr} (execFile's err.stderr carries the exit's own stderr), and reviewCreatePr() prefixes the surfaced message with gh's reason, keeping the generic text only when gh reported nothing. - hermes_cli web_git.py _gh() keeps (ok, stdout, stderr) through the same collapse — _run already captured stderr; it was discarded at the tuple boundary — and review_create_pr() surfaces a bounded stderr tail (400 chars) plus the gh context. Tests pin the contract with unique stderr markers and non-zero exits on both wrappers; successful-path return contracts unchanged. Electron half salvaged from PR #87751 (author preserved); CLI half added per the issue's acceptance scope. Fixes #87731
149 lines
4.9 KiB
TypeScript
149 lines
4.9 KiB
TypeScript
import assert from 'node:assert/strict'
|
|
import { execFileSync } from 'node:child_process'
|
|
import fs from 'node:fs'
|
|
import os from 'node:os'
|
|
import path from 'node:path'
|
|
|
|
import { afterEach, test, vi } from 'vitest'
|
|
|
|
import { gitFor, repoStatus, resolveRenamePath, REVIEW_FILE_CAP, reviewCreatePr, reviewList } from './git-review-ops'
|
|
|
|
// `runGh` shells to the `gh` CLI via execFile. Mock it so reviewCreatePr's gh
|
|
// invocation is controllable (real `gh` may be absent or slow in CI) while the
|
|
// repo setup below still uses the real execFileSync.
|
|
vi.mock('node:child_process', async importOriginal => {
|
|
const actual = await importOriginal<{ execFile: unknown; execFileSync: unknown }>()
|
|
|
|
return { ...actual, execFile: vi.fn() }
|
|
})
|
|
|
|
const tempDirs: string[] = []
|
|
|
|
afterEach(() => {
|
|
for (const dir of tempDirs.splice(0)) {
|
|
fs.rmSync(dir, { force: true, recursive: true })
|
|
}
|
|
})
|
|
|
|
function makeRepo() {
|
|
const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'hermes-desktop-git-status-'))
|
|
|
|
tempDirs.push(dir)
|
|
execFileSync('git', ['init', '-q'], { cwd: dir })
|
|
execFileSync('git', ['config', 'user.email', '[email protected]'], { cwd: dir })
|
|
execFileSync('git', ['config', 'user.name', 'Hermes Test'], { cwd: dir })
|
|
fs.writeFileSync(path.join(dir, 'tracked.txt'), 'tracked\n')
|
|
execFileSync('git', ['add', 'tracked.txt'], { cwd: dir })
|
|
execFileSync('git', ['commit', '-qm', 'initial'], { cwd: dir })
|
|
|
|
return dir
|
|
}
|
|
|
|
test('resolveRenamePath: plain path is unchanged', () => {
|
|
assert.equal(resolveRenamePath('src/a.ts'), 'src/a.ts')
|
|
})
|
|
|
|
test('gitFor accepts an internally resolved git binary path containing spaces', () => {
|
|
assert.doesNotThrow(() => gitFor(process.cwd(), 'C:\\Program Files\\Git\\cmd\\git.exe'))
|
|
})
|
|
|
|
test('resolveRenamePath: simple rename resolves to the new path', () => {
|
|
assert.equal(resolveRenamePath('old.ts => new.ts'), 'new.ts')
|
|
})
|
|
|
|
test('resolveRenamePath: brace rename resolves to the new path', () => {
|
|
assert.equal(resolveRenamePath('src/{old => new}/file.ts'), 'src/new/file.ts')
|
|
})
|
|
|
|
test('resolveRenamePath: brace rename collapsing a segment', () => {
|
|
assert.equal(resolveRenamePath('src/{lib => }/file.ts'), 'src/file.ts')
|
|
})
|
|
|
|
test('repoStatus reports an untracked directory without recursively listing its contents', async () => {
|
|
const dir = makeRepo()
|
|
const nested = path.join(dir, 'generated', 'deep')
|
|
|
|
fs.mkdirSync(nested, { recursive: true })
|
|
fs.writeFileSync(path.join(nested, 'large-output.txt'), 'generated\n')
|
|
|
|
const status = await repoStatus(dir, 'git')
|
|
|
|
assert.ok(status)
|
|
assert.equal(status.untracked, 1)
|
|
assert.equal(status.changed, 1)
|
|
assert.deepEqual(
|
|
status.files.map(file => file.path),
|
|
['generated/']
|
|
)
|
|
})
|
|
|
|
test('reviewList reports an untracked directory without recursively listing its contents', async () => {
|
|
const dir = makeRepo()
|
|
const nested = path.join(dir, 'browser-profile', 'Default', 'Cache')
|
|
|
|
fs.mkdirSync(nested, { recursive: true })
|
|
|
|
for (let i = 0; i < 20; i++) {
|
|
fs.writeFileSync(path.join(nested, `cache-${i}.bin`), 'generated\n')
|
|
}
|
|
|
|
const result = await reviewList(dir, 'uncommitted', null, 'git')
|
|
|
|
assert.deepEqual(
|
|
result.files.map(file => file.path),
|
|
['browser-profile/']
|
|
)
|
|
})
|
|
|
|
test('reviewList caps the file payload returned to the renderer', async () => {
|
|
const dir = makeRepo()
|
|
|
|
for (let i = 0; i < REVIEW_FILE_CAP + 10; i++) {
|
|
fs.writeFileSync(path.join(dir, `untracked-${String(i).padStart(4, '0')}.txt`), 'generated\n')
|
|
}
|
|
|
|
const result = await reviewList(dir, 'uncommitted', null, 'git')
|
|
|
|
assert.equal(result.files.length, REVIEW_FILE_CAP)
|
|
})
|
|
|
|
const mockExecFile = vi.mocked(await import('node:child_process')).execFile
|
|
|
|
type ExecFileCallback = (error: Error | null, stdout?: string, stderr?: string) => void
|
|
|
|
// `execFile` has overloaded declarations returning ChildProcess; the mock
|
|
// implementation only needs to drive its callback, so view it as a plain
|
|
// callable and set the implementation through the Mock typing.
|
|
function failGh(stderr: string): void {
|
|
;(
|
|
mockExecFile as unknown as {
|
|
mockImplementation: (
|
|
impl: (file: string, args: string[], options: object, callback: ExecFileCallback) => void
|
|
) => unknown
|
|
}
|
|
).mockImplementation((_bin: string, _args: string[], _opts: object, callback: ExecFileCallback) => {
|
|
const error = new Error('command failed')
|
|
|
|
if (stderr) {
|
|
;(error as Error & { stderr?: string }).stderr = stderr
|
|
}
|
|
|
|
callback(error, '', stderr)
|
|
})
|
|
}
|
|
|
|
test('reviewCreatePr surfaces gh stderr when pr create fails', async () => {
|
|
const dir = makeRepo()
|
|
|
|
failGh('no commits between main and feature')
|
|
|
|
await assert.rejects(reviewCreatePr(dir, 'git', 'gh'), /no commits between main and feature/)
|
|
})
|
|
|
|
test('reviewCreatePr falls back to the generic message when gh reports no stderr', async () => {
|
|
const dir = makeRepo()
|
|
|
|
failGh('')
|
|
|
|
await assert.rejects(reviewCreatePr(dir, 'git', 'gh'), /is gh installed and authenticated\?/)
|
|
})
|