Files
Agent-Workbench/tests/python-selection.test.mjs
T
2026-11-06 14:17:57 +08:00

87 lines
6.3 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { CheckRunner } from '../src/core/check-runner.mjs';
import { resolvePythonRuntime } from '../src/core/python-runtime.mjs';
import { validateToolArgs } from '../src/server/runtime.mjs';
import { fixture, launchWorkbench, compatibleFixture, completion, call, until } from './helpers.mjs';
function project(t) {
const f = fixture(t, { modernDefaults: true });
f.store.saveConfig({ approvalMode: 'full' });
fs.writeFileSync(path.join(f.projectRoot, 'pyproject.toml'), '[project]\nname="python-selection-fixture"\nversion="0.1.0"\n[tool.pytest.ini_options]\ntestpaths=["tests"]\n');
fs.mkdirSync(path.join(f.projectRoot, 'tests'));
fs.writeFileSync(path.join(f.projectRoot, 'tests/test_selection.py'), 'def test_first():\n assert 2+3 == 5\ndef test_second():\n assert "python".upper() == "PYTHON"\n');
const run = f.store.beginRun(f.session, 'Run the original Python tests with the requested interpreter.', false, f.store.config);
f.session.status = 'completed'; f.store.finishRun(f.session, run);
return f;
}
const hostOptions = () => process.env.AGENT_WORKBENCH_TEST_APP_ROOT ? { appRoot: process.env.AGENT_WORKBENCH_TEST_APP_ROOT } : {};
async function installed(f) {
const runtime = await resolvePythonRuntime(f.project, { executable: process.env.AGENT_WORKBENCH_TEST_PYTHON });
assert.equal(runtime.status, 'available', 'This test requires the existing Python 3.10+ / pytest environment.');
return runtime.logicalExecutable;
}
test('an explicit interpreter overrides an unavailable host default and is sealed for real pytest', async t => {
const f = project(t), executable = await installed(f);
const runner = new CheckRunner(f.store, { pythonExecutable: path.join(f.root, 'missing-host-python.exe') });
t.after(() => runner.stopAll());
const oldPlan = await runner.plan(f.session, ['python:test']);
assert.equal(oldPlan.checks[0].python.runtime.status, 'missing');
const plan = await runner.plan(f.session, ['python:test', 'python:compile'], { pythonExecutable: executable });
assert.ok(plan.checks.every(check => check.python.runtime.logicalExecutable === executable));
const job = await runner.start(f.session, plan.id).then(value => value.promise);
assert.equal(job.status, 'passed'); assert.equal(job.results[0].tests.passed, 2);
assert.equal(job.results[1].compilation.compiled, 1);
assert.equal(oldPlan.checks[0].python.runtime.status, 'missing');
const bad = await runner.plan(f.session, ['python:test'], { pythonExecutable: path.join(f.root, 'missing-requested-python.exe') });
assert.equal(bad.checks[0].python.runtime.status, 'missing', 'An invalid explicit choice must not silently fall back.');
});
test('HTTP carries the explicit interpreter without changing host configuration or old plans', { timeout: 45000 }, async t => {
const f = project(t), executable = await installed(f); f.store.close();
const host = await launchWorkbench(t, f.data, { AGENT_WORKBENCH_PYTHON_PATH: path.join(f.root, 'missing-default.exe') }, hostOptions());
const route = '/sessions/' + f.session.id;
const old = (await host.api(route + '/check-plans', { names: ['python:test'] })).execution.plans.at(-1);
assert.equal(old.checks[0].python.runtime.status, 'missing');
await assert.rejects(host.api(route + '/check-plans', { names: ['python:test'], pythonExecutable: true }), /文件路径/);
let state = await host.api(route + '/check-plans', { names: ['python:test'], pythonExecutable: executable });
const plan = state.execution.plans.at(-1); assert.equal(plan.checks[0].python.runtime.logicalExecutable, executable);
await host.api(route + '/check-jobs', { planId: plan.id });
await until(async () => { state = await host.api(route); return state.execution.checks.at(-1)?.finishedAt; }, 20000);
assert.equal(state.execution.checks.at(-1).status, 'passed');
assert.equal(state.execution.checks.at(-1).results[0].tests.passed, 2);
assert.equal(state.execution.plans.find(row => row.id === old.id).checks[0].python.runtime.status, 'missing');
assert.deepEqual((await host.api('/bootstrap')).config, host.bootstrap.config);
});
test('the real model tool surface can select Python and run checks under full access', { timeout: 45000 }, async t => {
const f = project(t), executable = await installed(f); f.store.close(); let requests = 0;
const model = await compatibleFixture(t, (_url, body) => {
requests++;
const planTool = body.tools.find(row => row.function.name === 'plan_checks');
assert.equal(planTool.function.parameters.properties.python_executable.type, 'string');
if (requests === 1) return completion([call('select-python', 'plan_checks', { names: ['python:test'], python_executable: executable })]);
if (requests === 2) {
const plan = JSON.parse(body.messages.findLast(row => row.role === 'tool').content);
assert.equal(plan.checks[0].python.runtime.logicalExecutable, executable);
return completion([call('run-selected-python', 'run_checks', { plan_id: plan.id })]);
}
assert.equal(requests, 3); return completion([], 'The selected Python tests passed.');
});
const host = await launchWorkbench(t, f.data, { AGENT_WORKBENCH_PYTHON_PATH: path.join(f.root, 'wrong-default.exe') }, hostOptions());
await host.api('/config', { mode: 'compatible', baseUrl: model.baseUrl, model: 'owned-python-selection', apiKey: 'owned-local-fixture-only' });
const route = '/sessions/' + f.session.id; await host.api(route + '/messages', { content: 'Run Python tests with ' + executable });
let state; await until(async () => { state = await host.api(route); return ['completed', 'failed', 'paused'].includes(state.status); }, 25000);
assert.equal(state.status, 'completed'); assert.equal(requests, 3);
assert.ok(state.approvals.every(row => row.status === 'approved' && row.approvalMode === 'full'));
assert.equal(state.execution.checks.at(-1).results[0].tests.passed, 2);
});
test('interpreter selection accepts paths and rejects malformed values before execution', () => {
validateToolArgs('plan_checks', { python_executable: 'C:/Python With Spaces/python.exe' });
for (const value of [true, [], '', ' ', 'python\ncommand', 'python\0.exe']) assert.throws(() => validateToolArgs('plan_checks', { python_executable: value }), /文件路径/);
});