wishforge-ide/packages/runtime/test/claude-ask.test.ts
wishforge dbf9fbedab
Some checks failed
CI / Format, types and tests (pull_request) Has been cancelled
CI / PostgreSQL migrations and integration tests (pull_request) Has been cancelled
🐛 Align ExecutorSettings across WishRefiner, Copilot, CodeWiki and Run resume
2026-11-11 17:34:05 -08:00

164 lines
6.2 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { existsSync } from 'node:fs';
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
import os from 'node:os';
import path from 'node:path';
import { askClaude, type Command } from '../src/index.js';
const input = {
binary: '/opt/claude',
home: '/nonexistent-home',
system: 'Rewrite the wish.',
prompt: '{"goal":"x"}',
schema: { type: 'object' },
};
describe('asking the local Claude CLI', () => {
it('runs it without any tool, settings, MCP server or saved session, in a directory of its own', async () => {
let seen: Command | undefined;
const answer = await askClaude(input, async command => {
seen = command;
expect(existsSync(command.cwd!)).toBe(true);
return {
exitCode: 0,
stdout: JSON.stringify({ type: 'result', is_error: false, structured_output: { goal: 'y' } }),
stderr: '',
durationMs: 1,
};
});
expect(answer).toEqual({ goal: 'y' });
const args = seen!.args;
expect(args[args.indexOf('--tools') + 1]).toBe('');
expect(args).toEqual(
expect.arrayContaining([
'--restricted',
'--safe-mode',
'--strict-mcp-config',
'--no-session-persistence',
'--output-format',
'json',
]),
);
expect(args[args.indexOf('--system-prompt') + 1]).toBe('Rewrite the wish.');
expect(JSON.parse(args[args.indexOf('--json-schema') + 1]!)).toEqual({ type: 'object' });
expect(seen!.env).toMatchObject({ HOME: '/nonexistent-home' });
expect(args).not.toContain('--model');
expect(seen!.env).not.toHaveProperty('WF_DEV_AUTH_TOKEN');
expect(existsSync(seen!.cwd!)).toBe(false);
});
it('carries the host egress proxy so a gateway behind it stays reachable', async () => {
const saved = { https: process.env.https_proxy, token: process.env.WF_DEV_AUTH_TOKEN };
process.env.https_proxy = 'http://127.0.0.1:7890';
process.env.WF_DEV_AUTH_TOKEN = 'must-not-leak';
try {
let seen: Command | undefined;
await askClaude(input, async command => {
seen = command;
return { exitCode: 0, stdout: JSON.stringify({ structured_output: {} }), stderr: '', durationMs: 1 };
});
expect(seen!.env).toMatchObject({ https_proxy: 'http://127.0.0.1:7890' });
expect(seen!.env).not.toHaveProperty('WF_DEV_AUTH_TOKEN');
} finally {
if (saved.https === undefined) delete process.env.https_proxy;
else process.env.https_proxy = saved.https;
if (saved.token === undefined) delete process.env.WF_DEV_AUTH_TOKEN;
else process.env.WF_DEV_AUTH_TOKEN = saved.token;
}
});
it('passes a named model and server-held credential environment to the CLI', async () => {
let seen: Command | undefined;
await askClaude(
{
...input,
model: 'deepseek-flash@latest',
credentialEnv: {
ANTHROPIC_API_KEY: 'server-fixture',
ANTHROPIC_BASE_URL: 'https://model.example',
ANTHROPIC_MODEL: 'stale-default',
ANTHROPIC_SMALL_FAST_MODEL: 'stale-small',
},
timeoutMs: 140_000,
},
async command => {
seen = command;
return {
exitCode: 0,
stdout: JSON.stringify({ is_error: false, structured_output: { goal: 'y' } }),
stderr: '',
durationMs: 1,
};
},
);
expect(seen!.args[seen!.args.indexOf('--model') + 1]).toBe('deepseek-flash@latest');
expect(seen!.env).toMatchObject({
ANTHROPIC_API_KEY: 'server-fixture',
ANTHROPIC_BASE_URL: 'https://model.example',
ANTHROPIC_MODEL: 'deepseek-flash@latest',
ANTHROPIC_SMALL_FAST_MODEL: 'deepseek-flash@latest',
});
expect(seen!.timeoutMs).toBe(140_000);
});
it('runs a credentialed call in a home of its own and never reads local settings', async () => {
// An operator's settings would point the CLI at another model and gateway; a call that carries
// its own credential must not see any of it.
const settingsHome = await mkdtemp(path.join(os.tmpdir(), 'wishforge-settings-'));
await mkdir(path.join(settingsHome, '.claude'), { recursive: true });
await writeFile(
path.join(settingsHome, '.claude/settings.json'),
JSON.stringify({ env: { ANTHROPIC_MODEL: 'opus', ANTHROPIC_BASE_URL: 'https://local.invalid' } }),
);
let seen: Command | undefined;
try {
await askClaude(
{
binary: '/opt/claude',
system: 'Answer.',
prompt: '{}',
schema: { type: 'object' },
model: 'deepseek-flash@latest',
credentialEnv: { ANTHROPIC_API_KEY: 'server-fixture', ANTHROPIC_BASE_URL: 'https://model.example' },
},
async command => {
seen = command;
expect(command.env!.HOME).not.toBe(settingsHome);
// The home is fresh for this call: nothing was copied from the operator's.
expect(existsSync(path.join(command.env!.HOME!, '.claude'))).toBe(false);
return {
exitCode: 0,
stdout: JSON.stringify({ is_error: false, structured_output: { goal: 'y' } }),
stderr: '',
durationMs: 1,
};
},
);
} finally {
await rm(settingsHome, { recursive: true, force: true });
}
expect(seen!.args[seen!.args.indexOf('--model') + 1]).toBe('deepseek-flash@latest');
expect(seen!.env).toMatchObject({
ANTHROPIC_API_KEY: 'server-fixture',
ANTHROPIC_BASE_URL: 'https://model.example',
ANTHROPIC_MODEL: 'deepseek-flash@latest',
ANTHROPIC_SMALL_FAST_MODEL: 'deepseek-flash@latest',
});
expect(seen!.env).not.toHaveProperty('CLAUDE_CODE_OAUTH_TOKEN');
});
it('takes the JSON object from a text answer, and fails on an error result', async () => {
const text = await askClaude(input, async () => ({
exitCode: 0,
stdout: JSON.stringify({ is_error: false, result: 'Here it is: {"goal":"z"} done' }),
stderr: '',
durationMs: 1,
}));
expect(text).toEqual({ goal: 'z' });
await expect(
askClaude(input, async () => ({
exitCode: 1,
stdout: JSON.stringify({ is_error: true, subtype: 'error_max_turns' }),
stderr: '',
durationMs: 1,
})),
).rejects.toThrow('error_max_turns');
});
});