test(ai-code-review): 更新 CLI 呼叫與前置驗證測試

This commit is contained in:
2026-06-27 13:30:45 +00:00
parent 08a72a8c7f
commit 4fcb240208
3 changed files with 115 additions and 167 deletions
+36 -60
View File
@@ -1,21 +1,38 @@
import { describe, it, afterEach, mock } from 'node:test';
import assert from 'node:assert/strict';
import axios from 'axios';
import { mkdtemp, writeFile, chmod, rm } from 'fs/promises';
import { tmpdir } from 'os';
import { join } from 'path';
import { checkRequiredEnv, verifyGiteaToken, verifyCommentToken, verifyLLM, runPreflight } from '../preflight.js';
const LLM_ENV_KEYS = [
'OPENCODE_BASE_URL', 'OPENCODE_MODEL', 'OPENCODE_PROVIDER',
'AI_ASSISTANT_CLI', 'MODEL', 'OPENCODE_MODEL', 'PATH',
];
const ORIGINAL_PATH = process.env.PATH;
function clearLLMEnv() {
for (const k of LLM_ENV_KEYS) delete process.env[k];
}
afterEach(() => {
let tempDir;
afterEach(async () => {
mock.restoreAll();
clearLLMEnv();
process.env.PATH = ORIGINAL_PATH;
if (tempDir) await rm(tempDir, { recursive: true, force: true });
tempDir = null;
});
async function installFakeCLI(command = 'codex') {
tempDir = await mkdtemp(join(tmpdir(), 'preflight-cli-test-'));
const script = join(tempDir, command);
await writeFile(script, '#!/bin/sh\nexit 0\n');
await chmod(script, 0o755);
process.env.PATH = tempDir;
}
describe('checkRequiredEnv', () => {
it('reports all three missing when nothing provided', () => {
const result = checkRequiredEnv({ token: '', repo: '', pr: '' });
@@ -102,81 +119,40 @@ describe('verifyCommentToken', () => {
});
describe('verifyLLM', () => {
it('fails when OpenCode is not configured', async () => {
it('fails when no supported assistant CLI is detected', async () => {
clearLLMEnv();
process.env.AI_ASSISTANT_CLI = 'no-such-ai-cli';
process.env.PATH = '';
const result = await verifyLLM();
assert.equal(result.ok, false);
assert.match(result.error, /OPENCODE_BASE_URL/);
assert.match(result.error, /AI 助理 CLI/);
});
it('checks OpenCode server provider and model', async () => {
it('passes when a supported assistant CLI is detected', async () => {
clearLLMEnv();
process.env.OPENCODE_BASE_URL = 'http://opencode.local:4096';
process.env.OPENCODE_PROVIDER = 'google';
process.env.OPENCODE_MODEL = 'gemini-2.5-flash';
const urls = [];
mock.method(axios, 'get', async (url) => {
urls.push(url);
if (url.endsWith('/global/health')) return { data: { healthy: true, version: '1.17.7' } };
return { data: { providers: [{ id: 'google', models: { 'gemini-2.5-flash': { id: 'gemini-2.5-flash' } } }] } };
});
await installFakeCLI('codex');
process.env.AI_ASSISTANT_CLI = 'codex';
process.env.MODEL = 'gpt-5-mini';
const result = await verifyLLM();
assert.equal(result.ok, true);
assert.equal(result.provider, 'opencode');
assert.deepEqual(urls, ['http://opencode.local:4096/global/health', 'http://opencode.local:4096/config/providers']);
assert.equal(result.provider, 'codex');
assert.equal(result.command, 'codex');
assert.equal(result.model, 'gpt-5-mini');
});
it('fails when configured provider is missing', async () => {
it('fails when a requested CLI is not installed', async () => {
clearLLMEnv();
process.env.OPENCODE_BASE_URL = 'http://opencode.local:4096';
process.env.OPENCODE_PROVIDER = 'google';
mock.method(axios, 'get', async (url) => {
if (url.endsWith('/global/health')) return { data: { healthy: true } };
return { data: { providers: [{ id: 'anthropic', models: {} }] } };
});
process.env.AI_ASSISTANT_CLI = 'missing-cli';
process.env.PATH = '';
const result = await verifyLLM();
assert.equal(result.ok, false);
assert.match(result.error, /未設定 provider=google/);
});
it('fails when configured model is missing', async () => {
clearLLMEnv();
process.env.OPENCODE_BASE_URL = 'http://opencode.local:4096';
process.env.OPENCODE_PROVIDER = 'google';
process.env.OPENCODE_MODEL = 'gemini-2.5-pro';
mock.method(axios, 'get', async (url) => {
if (url.endsWith('/global/health')) return { data: { healthy: true } };
return { data: { providers: [{ id: 'google', models: { 'gemini-2.5-flash': { id: 'gemini-2.5-flash' } } }] } };
});
const result = await verifyLLM();
assert.equal(result.ok, false);
assert.match(result.error, /未列出 model=gemini-2.5-pro/);
});
it('passes an insecure https agent by default', async () => {
clearLLMEnv();
process.env.OPENCODE_BASE_URL = 'https://opencode.local:4096';
const agents = [];
mock.method(axios, 'get', async (url, opts) => {
agents.push(opts.httpsAgent);
if (url.endsWith('/global/health')) return { data: { healthy: true } };
return { data: { providers: [{ id: 'google', models: { 'gemini-2.5-flash': { id: 'gemini-2.5-flash' } } }] } };
});
const result = await verifyLLM();
assert.equal(result.ok, true);
assert.equal(agents.length, 2);
assert.equal(agents[0].options.rejectUnauthorized, false);
assert.equal(agents[1].options.rejectUnauthorized, false);
assert.match(result.error, /AI 助理 CLI/);
});
});
@@ -188,7 +164,7 @@ describe('runPreflight', () => {
verifyToken: async () => ({ ok: true }),
verifyComment: async () => ({ ok: true }),
verifyRemote: () => ({ ok: true }),
verifyLLMFn: async () => ({ ok: true, provider: 'opencode' }),
verifyLLMFn: async () => ({ ok: true, provider: 'codex' }),
...overrides,
};
}
@@ -239,7 +215,7 @@ describe('runPreflight', () => {
it('returns false when LLM verification fails', async () => {
const result = await runPreflight('/ws', makeDeps({
verifyLLMFn: async () => ({ ok: false, error: 'OpenCode server 驗證失敗' }),
verifyLLMFn: async () => ({ ok: false, error: 'AI 助理 CLI 驗證失敗' }),
}));
assert.equal(result, false);
});