231 lines
7.1 KiB
JavaScript
231 lines
7.1 KiB
JavaScript
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 = [
|
|
'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];
|
|
}
|
|
|
|
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: '' });
|
|
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 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, /AI 助理 CLI/);
|
|
});
|
|
|
|
it('passes when a supported assistant CLI is detected', async () => {
|
|
clearLLMEnv();
|
|
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, 'codex');
|
|
assert.equal(result.command, 'codex');
|
|
assert.equal(result.model, 'gpt-5-mini');
|
|
});
|
|
|
|
it('fails when a requested CLI is not installed', async () => {
|
|
clearLLMEnv();
|
|
process.env.AI_ASSISTANT_CLI = 'missing-cli';
|
|
process.env.PATH = '';
|
|
|
|
const result = await verifyLLM();
|
|
|
|
assert.equal(result.ok, false);
|
|
assert.match(result.error, /AI 助理 CLI/);
|
|
});
|
|
|
|
});
|
|
|
|
describe('runPreflight', () => {
|
|
function makeDeps(overrides = {}) {
|
|
return {
|
|
checkEnv: () => ({ ok: true, missing: [] }),
|
|
verifyToken: async () => ({ ok: true }),
|
|
verifyComment: async () => ({ ok: true }),
|
|
verifyRemote: () => ({ ok: true }),
|
|
verifyLLMFn: async () => ({ ok: true, provider: 'codex' }),
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
it('returns false and stops early when required env is missing', async () => {
|
|
const result = await runPreflight();
|
|
assert.equal(result, false);
|
|
});
|
|
|
|
it('returns true when every verification step succeeds', async () => {
|
|
const result = await runPreflight('/ws', makeDeps());
|
|
assert.equal(result, true);
|
|
});
|
|
|
|
it('returns true when the comment token check is skipped', async () => {
|
|
const result = await runPreflight('/ws', makeDeps({
|
|
verifyComment: async () => ({ ok: true, skipped: true }),
|
|
}));
|
|
assert.equal(result, true);
|
|
});
|
|
|
|
it('returns false when the Gitea token check fails', async () => {
|
|
let remoteCalled = false;
|
|
const result = await runPreflight('/ws', makeDeps({
|
|
verifyToken: async () => ({ ok: false, error: 'HTTP 401' }),
|
|
verifyRemote: () => { remoteCalled = true; return { ok: true }; },
|
|
}));
|
|
assert.equal(result, false);
|
|
assert.equal(remoteCalled, false, 'should stop before later checks');
|
|
});
|
|
|
|
it('returns false when the comment token check fails', async () => {
|
|
const result = await runPreflight('/ws', makeDeps({
|
|
verifyComment: async () => ({ ok: false, error: 'HTTP 401' }),
|
|
}));
|
|
assert.equal(result, false);
|
|
});
|
|
|
|
it('returns false when git remote access fails', async () => {
|
|
let llmCalled = false;
|
|
const result = await runPreflight('/ws', makeDeps({
|
|
verifyRemote: () => ({ ok: false, error: 'auth failed' }),
|
|
verifyLLMFn: async () => { llmCalled = true; return { ok: true }; },
|
|
}));
|
|
assert.equal(result, false);
|
|
assert.equal(llmCalled, false, 'should stop before the LLM check');
|
|
});
|
|
|
|
it('returns false when LLM verification fails', async () => {
|
|
const result = await runPreflight('/ws', makeDeps({
|
|
verifyLLMFn: async () => ({ ok: false, error: 'AI 助理 CLI 驗證失敗' }),
|
|
}));
|
|
assert.equal(result, false);
|
|
});
|
|
|
|
it('passes the workspace through to the remote-access check', async () => {
|
|
let captured;
|
|
await runPreflight('/custom/ws', makeDeps({
|
|
verifyRemote: (ws) => { captured = ws; return { ok: true }; },
|
|
}));
|
|
assert.equal(captured, '/custom/ws');
|
|
});
|
|
});
|