Files
deepseek-harness/packages/experimental/forge-github/tests/gh.spec.ts
T
Yichen Jiang fc43a2d507 feat(forge): add the pull request panel and worktree Sessions
The forge panel shows the pull requests a Session created or pushed in a
right-sidebar tab, with a Session header button and branch label, and lets
a new Session start in its own Git worktree from the New Session options
row. The forge bundle ships the panel with the forge registry, the GitHub
provider, the Session bindings, and an isolated worktree service.

The panel listens to the agent-controller/admission stage. It holds the
first message of a Session with a worktree request while it creates and
enters the worktree as Agent maintenance, and refuses messages while the
worktree the Session works in is gone. A failed or stopped creation
returns the message to the composer: the card above the composer shows the
failure, sending again retries with a new branch name, and Continue
without a worktree withdraws the request. A removed worktree can be
checked out again or left for the source checkout. A message that enters
without admission still gets its worktree at the first step proposal.
2026-10-20 11:49:51 +08:00

83 lines
3.1 KiB
TypeScript

import { chmod, mkdtemp, rm, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { GhUnavailableError, ghRunner } from '../src/gh.ts'
let root = ''
beforeEach(async () => {
root = await mkdtemp(join(tmpdir(), 'dsh-forge-gh-'))
})
afterEach(async () => {
vi.unstubAllEnvs()
await rm(root, { recursive: true, force: true })
})
/** A POSIX stand-in for gh whose first argument selects its behavior. */
async function fakeGh(): Promise<string> {
const path = join(root, 'gh')
await writeFile(path, [
'#!/bin/sh',
'case "$1" in',
' echo) printf "%s|" "$@"; printf "\\nprompt=%s token=%s pager=[%s]\\n" "$GH_PROMPT_DISABLED" "${GH_TOKEN:-unset}" "$GH_PAGER" ;;',
' fail) echo oops >&2; exit 3 ;;',
' sleep) sleep 5 ;;',
' big) head -c 5000 /dev/zero | tr "\\0" x ;;',
'esac',
'',
].join('\n'))
await chmod(path, 0o755)
return path
}
describe.skipIf(process.platform === 'win32')('gh runner', () => {
it('runs gh with literal arguments and a scrubbed, non-interactive environment', async () => {
vi.stubEnv('GH_TOKEN', 'secret-token')
const run = ghRunner({ ghCommand: await fakeGh(), timeoutMs: 5_000, maxOutputBytes: 10_000 })
await expect(run(['echo', 'a b', '--flag'], new AbortController().signal)).resolves.toEqual({
exitCode: 0,
stdout: 'echo|a b|--flag|\nprompt=1 token=unset pager=[]',
stderr: '',
})
})
it('returns a nonzero exit code with its stderr', async () => {
const run = ghRunner({ ghCommand: await fakeGh(), timeoutMs: 5_000, maxOutputBytes: 10_000 })
await expect(run(['fail'], new AbortController().signal)).resolves.toEqual({ exitCode: 3, stdout: '', stderr: 'oops' })
})
it('rejects when gh outlives the timeout', async () => {
const run = ghRunner({ ghCommand: await fakeGh(), timeoutMs: 100, maxOutputBytes: 10_000 })
await expect(run(['sleep'], new AbortController().signal)).rejects.toThrow('gh sleep timed out after 100ms')
})
it('rejects with the abort reason when cancelled', async () => {
const run = ghRunner({ ghCommand: await fakeGh(), timeoutMs: 5_000, maxOutputBytes: 10_000 })
const controller = new AbortController()
setTimeout(() => {
controller.abort(new Error('stop requested'))
}, 50)
await expect(run(['sleep'], controller.signal)).rejects.toThrow('stop requested')
})
it('rejects output beyond maxOutputBytes', async () => {
const run = ghRunner({ ghCommand: await fakeGh(), timeoutMs: 5_000, maxOutputBytes: 1_000 })
await expect(run(['big'], new AbortController().signal)).rejects.toThrow('gh big output exceeds maxOutputBytes')
})
it('reports an executable that cannot start', async () => {
const run = ghRunner({ ghCommand: join(root, 'missing-gh'), timeoutMs: 5_000, maxOutputBytes: 1_000 })
const result = run([], new AbortController().signal)
await expect(result).rejects.toBeInstanceOf(GhUnavailableError)
await expect(result).rejects.toThrow(/^gh could not start: /)
})
})