feat: 啟動時前置驗證所有驗證相關設定
新增 app/preflight.js,在 action 啟動(Step1 之後、其餘步驟之前)集中 檢查必要環境變數、GITEA_TOKEN 讀 repo、GITEA_COMMENT_TOKEN、以及 LLM provider/API Key(多把只要一把通過即可,Ollama 改檢查 base URL 連線)。 任一項失敗即印出原因並 exit 1,避免分析到一半或發 comment 時才失敗。 main.js 在 Step1 後呼叫 runPreflight();新增 preflight.test.js 覆蓋 成功、缺環境變數、token 無效、所有 LLM key 失敗、Ollama 等情境。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,197 @@
|
||||
import { describe, it, afterEach, mock } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import axios from 'axios';
|
||||
import { checkRequiredEnv, verifyGiteaToken, verifyCommentToken, verifyLLM, runPreflight } from './preflight.js';
|
||||
|
||||
const LLM_ENV_KEYS = [
|
||||
'OPENAI_API_KEY', 'OPENAI_BASE_URL', 'OPENAI_MODEL',
|
||||
'CLAUDE_API_KEY', 'CLAUDE_BASE_URL', 'CLAUDE_MODEL',
|
||||
'GEMINI_API_KEY', 'GEMINI_BASE_URL', 'GEMINI_MODEL',
|
||||
'OLLAMA_BASE_URL', 'OLLAMA_MODEL',
|
||||
'AMAZONQ_API_KEY', 'AMAZONQ_BASE_URL', 'AMAZONQ_MODEL',
|
||||
];
|
||||
|
||||
function clearLLMEnv() {
|
||||
for (const k of LLM_ENV_KEYS) delete process.env[k];
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
mock.restoreAll();
|
||||
clearLLMEnv();
|
||||
});
|
||||
|
||||
describe('checkRequiredEnv', () => {
|
||||
it('reports all three missing when nothing provided', () => {
|
||||
const result = checkRequiredEnv({ token: '', repo: '', pr: '' });
|
||||
assert.equal(result.ok, false);
|
||||
assert.deepEqual(result.missing, ['GITEA_TOKEN', 'GITEA_REPOSITORY', 'PR_NUMBER']);
|
||||
});
|
||||
|
||||
it('reports only the missing ones', () => {
|
||||
const result = checkRequiredEnv({ token: 't', repo: '', pr: '5' });
|
||||
assert.equal(result.ok, false);
|
||||
assert.deepEqual(result.missing, ['GITEA_REPOSITORY']);
|
||||
});
|
||||
|
||||
it('ok when all provided', () => {
|
||||
const result = checkRequiredEnv({ token: 't', repo: 'owner/repo', pr: '5' });
|
||||
assert.equal(result.ok, true);
|
||||
assert.deepEqual(result.missing, []);
|
||||
});
|
||||
});
|
||||
|
||||
describe('verifyGiteaToken', () => {
|
||||
it('ok when repo endpoint returns successfully', async () => {
|
||||
let capturedUrl, capturedOpts;
|
||||
mock.method(axios, 'get', async (url, opts) => {
|
||||
capturedUrl = url;
|
||||
capturedOpts = opts;
|
||||
return { data: { full_name: 'owner/repo' } };
|
||||
});
|
||||
const result = await verifyGiteaToken('tok', 'owner/repo');
|
||||
assert.equal(result.ok, true);
|
||||
assert.ok(capturedUrl.includes('/api/v1/repos/owner/repo'));
|
||||
assert.equal(capturedOpts.headers['Authorization'], 'token tok');
|
||||
});
|
||||
|
||||
it('fails with HTTP status when token is invalid', async () => {
|
||||
mock.method(axios, 'get', async () => {
|
||||
const e = new Error('Unauthorized');
|
||||
e.response = { status: 401 };
|
||||
throw e;
|
||||
});
|
||||
const result = await verifyGiteaToken('bad', 'owner/repo');
|
||||
assert.equal(result.ok, false);
|
||||
assert.match(result.error, /HTTP 401/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('verifyCommentToken', () => {
|
||||
it('skips when no comment token provided', async () => {
|
||||
const result = await verifyCommentToken('');
|
||||
assert.deepEqual(result, { ok: true, skipped: true });
|
||||
});
|
||||
|
||||
it('ok when /user returns successfully', async () => {
|
||||
let capturedUrl, capturedOpts;
|
||||
mock.method(axios, 'get', async (url, opts) => {
|
||||
capturedUrl = url;
|
||||
capturedOpts = opts;
|
||||
return { data: { login: 'bot' } };
|
||||
});
|
||||
const result = await verifyCommentToken('ctok');
|
||||
assert.equal(result.ok, true);
|
||||
assert.ok(capturedUrl.endsWith('/api/v1/user'));
|
||||
assert.equal(capturedOpts.headers['Authorization'], 'token ctok');
|
||||
});
|
||||
|
||||
it('fails when comment token is invalid', async () => {
|
||||
mock.method(axios, 'get', async () => {
|
||||
const e = new Error('Unauthorized');
|
||||
e.response = { status: 401 };
|
||||
throw e;
|
||||
});
|
||||
const result = await verifyCommentToken('bad');
|
||||
assert.equal(result.ok, false);
|
||||
assert.match(result.error, /HTTP 401/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('verifyLLM', () => {
|
||||
it('fails when no provider/key configured', async () => {
|
||||
clearLLMEnv();
|
||||
const result = await verifyLLM();
|
||||
assert.equal(result.ok, false);
|
||||
assert.match(result.error, /未設定/);
|
||||
});
|
||||
|
||||
it('ok when an OpenAI-compatible key authenticates', async () => {
|
||||
clearLLMEnv();
|
||||
process.env.OPENAI_API_KEY = 'k1,k2';
|
||||
let capturedUrl, capturedPayload, capturedHeaders;
|
||||
mock.method(axios, 'post', async (url, payload, opts) => {
|
||||
capturedUrl = url;
|
||||
capturedPayload = payload;
|
||||
capturedHeaders = opts.headers;
|
||||
return { data: { choices: [{ message: { content: 'ok' } }] } };
|
||||
});
|
||||
const result = await verifyLLM();
|
||||
assert.equal(result.ok, true);
|
||||
assert.equal(result.provider, 'openai');
|
||||
assert.equal(result.keyIndex, 1);
|
||||
assert.equal(result.total, 2);
|
||||
assert.ok(capturedUrl.endsWith('/chat/completions'));
|
||||
assert.equal(capturedPayload.max_tokens, 1);
|
||||
assert.equal(capturedHeaders['Authorization'], 'Bearer k1');
|
||||
});
|
||||
|
||||
it('tries the next key when the first one fails', async () => {
|
||||
clearLLMEnv();
|
||||
process.env.OPENAI_API_KEY = 'bad,good';
|
||||
let calls = 0;
|
||||
mock.method(axios, 'post', async (_url, _payload, opts) => {
|
||||
calls += 1;
|
||||
if (opts.headers['Authorization'] === 'Bearer bad') throw new Error('401');
|
||||
return { data: { choices: [{ message: { content: 'ok' } }] } };
|
||||
});
|
||||
const result = await verifyLLM();
|
||||
assert.equal(result.ok, true);
|
||||
assert.equal(result.keyIndex, 2);
|
||||
assert.equal(calls, 2);
|
||||
});
|
||||
|
||||
it('fails when all keys fail', async () => {
|
||||
clearLLMEnv();
|
||||
process.env.OPENAI_API_KEY = 'k1,k2';
|
||||
mock.method(axios, 'post', async () => { throw new Error('401'); });
|
||||
const result = await verifyLLM();
|
||||
assert.equal(result.ok, false);
|
||||
assert.match(result.error, /所有 2 把 openai API Key 驗證失敗/);
|
||||
});
|
||||
|
||||
it('sets anthropic-version header for claude', async () => {
|
||||
clearLLMEnv();
|
||||
process.env.CLAUDE_API_KEY = 'ck';
|
||||
let capturedHeaders;
|
||||
mock.method(axios, 'post', async (_url, _payload, opts) => {
|
||||
capturedHeaders = opts.headers;
|
||||
return { data: { choices: [{ message: { content: 'ok' } }] } };
|
||||
});
|
||||
const result = await verifyLLM();
|
||||
assert.equal(result.ok, true);
|
||||
assert.equal(result.provider, 'claude');
|
||||
assert.equal(capturedHeaders['anthropic-version'], '2023-06-01');
|
||||
});
|
||||
|
||||
it('checks base URL connectivity for ollama (no key)', async () => {
|
||||
clearLLMEnv();
|
||||
process.env.OLLAMA_BASE_URL = 'http://ollama.local/v1';
|
||||
let capturedUrl;
|
||||
mock.method(axios, 'get', async (url) => {
|
||||
capturedUrl = url;
|
||||
return { data: { data: [] } };
|
||||
});
|
||||
const result = await verifyLLM();
|
||||
assert.equal(result.ok, true);
|
||||
assert.equal(result.provider, 'ollama');
|
||||
assert.ok(capturedUrl.endsWith('/models'));
|
||||
});
|
||||
|
||||
it('fails when ollama base URL is unreachable', async () => {
|
||||
clearLLMEnv();
|
||||
process.env.OLLAMA_BASE_URL = 'http://ollama.local/v1';
|
||||
mock.method(axios, 'get', async () => { throw new Error('ECONNREFUSED'); });
|
||||
const result = await verifyLLM();
|
||||
assert.equal(result.ok, false);
|
||||
assert.match(result.error, /無法連線/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('runPreflight', () => {
|
||||
it('returns false and stops early when required env is missing', async () => {
|
||||
// Config constants default to empty in the test environment, so the
|
||||
// required-env check fails before any network call is attempted.
|
||||
const result = await runPreflight();
|
||||
assert.equal(result, false);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user