test(windows): close final native coverage gaps

This commit is contained in:
Tianyi Cui
2026-08-09 00:48:46 +08:00
parent d2928584e7
commit 9e724dcad7
11 changed files with 88 additions and 30 deletions

View File

@@ -174,18 +174,32 @@ describe('real @openai/codex 0.147.0 product', () => {
}, 60_000)
it('cancels a real app-server command approval without executing the command', async () => {
const { harness, fixture } = await realHarness([
const command = process.platform === 'win32'
? 'cmd /c type nul > approval-side-effect'
: 'touch approval-side-effect'
const commandCalls = [
{
kind: 'functionCall',
name: 'exec_command',
arguments: {
cmd: process.platform === 'win32'
? 'cmd /c type nul > approval-side-effect'
: 'touch approval-side-effect',
cmd: command,
sandbox_permissions: 'require_escalated',
justification: 'exercise the unattended approval boundary',
},
},
{
name: 'shell_command',
arguments: {
command,
sandbox_permissions: 'require_escalated',
justification: 'exercise the unattended approval boundary',
},
},
] as const
const { harness, fixture } = await realHarness([
{
kind: 'advertisedFunctionCall',
choices: commandCalls,
},
])
const sideEffect = join(harness.workspace, 'approval-side-effect')
const run = await harness.ctx.subagents.start('codex', {
@@ -202,9 +216,9 @@ describe('real @openai/codex 0.147.0 product', () => {
expect(existsSync(sideEffect)).toBe(false)
expect(fixture.requests).toHaveLength(1)
const tools = fixture.requests[0]!.body.tools as Array<Record<string, unknown>>
expect(tools).toEqual(expect.arrayContaining([
expect.objectContaining({ type: 'function', name: 'exec_command' }),
]))
expect(commandCalls.some(call => tools.some(tool => (
tool.type === 'function' && tool.name === call.name
)))).toBe(true)
expect(fixture.requests.every(requestEntry =>
requestEntry.headers.authorization === 'Bearer dsh-fake-openai-key',
)).toBe(true)

View File

@@ -22,6 +22,13 @@ export type ResponsesBehavior =
readonly name: string
readonly arguments: Record<string, unknown>
}
| {
readonly kind: 'advertisedFunctionCall'
readonly choices: readonly {
readonly name: string
readonly arguments: Record<string, unknown>
}[]
}
| { readonly kind: 'hold' }
/** Running package-private Responses fixture. */
@@ -218,6 +225,18 @@ function closeServer(server: Server): Promise<void> {
})
}
function advertisedFunctionNames(body: Record<string, unknown>): Set<string> {
if (!Array.isArray(body.tools)) return new Set()
return new Set(body.tools.flatMap((tool): string[] => (
tool !== null
&& typeof tool === 'object'
&& (tool as Record<string, unknown>).type === 'function'
&& typeof (tool as Record<string, unknown>).name === 'string'
? [(tool as Record<string, unknown>).name as string]
: []
)))
}
/**
* Start a loopback-only Responses SSE fixture.
* @param script - one behavior per expected Responses request.
@@ -234,11 +253,12 @@ export async function startResponsesFixture(
openResponses.add(response)
response.on('close', () => { openResponses.delete(response) })
void readRequest(request).then((body) => {
const parsedBody = JSON.parse(body) as Record<string, unknown>
requests.push({
method: request.method,
path: request.url,
headers: request.headers,
body: JSON.parse(body) as Record<string, unknown>,
body: parsedBody,
})
started.resolve(undefined)
const behavior = behaviors.shift()
@@ -247,6 +267,14 @@ export async function startResponsesFixture(
response.end(JSON.stringify({ error: { message: 'fixture script exhausted' } }))
return
}
const advertisedCall = behavior.kind === 'advertisedFunctionCall'
? behavior.choices.find(choice => advertisedFunctionNames(parsedBody).has(choice.name))
: undefined
if (behavior.kind === 'advertisedFunctionCall' && advertisedCall === undefined) {
response.writeHead(500, { 'content-type': 'application/json' })
response.end(JSON.stringify({ error: { message: 'none of the fixture function calls was advertised' } }))
return
}
response.writeHead(200, {
'content-type': 'text/event-stream',
'cache-control': 'no-cache',
@@ -254,9 +282,15 @@ export async function startResponsesFixture(
'x-request-id': 'req_fixture',
})
if (behavior.kind === 'hold') return
const events = behavior.kind === 'complete'
? completeResponsesEvents(behavior.text)
: functionCallEvents(behavior.name, behavior.arguments)
let events: Record<string, unknown>[]
if (behavior.kind === 'complete') {
events = completeResponsesEvents(behavior.text)
} else {
const call = behavior.kind === 'functionCall'
? behavior
: advertisedCall!
events = functionCallEvents(call.name, call.arguments)
}
for (const event of events) {
response.write(`data: ${JSON.stringify(event)}\n\n`)
}