Files
Agent-Workbench/tests/process-environment.test.mjs

58 lines
4.4 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { fixture } from './helpers.mjs';
import { Provider } from '../src/server/provider.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { CheckRunner } from '../src/core/check-runner.mjs';
import { resolvePythonRuntime } from '../src/core/python-runtime.mjs';
const markers = ['AWB_QA_HOST_ONLY_SECRET', 'AWB_QA_UNRELATED_TOKEN'];
function hostMarkers(t) {
for (const name of markers) { assert.equal(Object.hasOwn(process.env, name), false); process.env[name] = 'owned-host-marker'; }
t.after(() => { for (const name of markers) delete process.env[name]; });
}
function task(f) {
const run = f.store.beginRun(f.session, 'Verify actual project execution environment', false, f.store.config);
f.session.status = 'completed'; f.store.finishRun(f.session, run);
}
async function checks(t, f, options = {}) {
task(f); const runner = new CheckRunner(f.store, options); f.onCleanup(() => runner.stopAll());
const plan = await runner.plan(f.session); return await (await runner.start(f.session, plan.id)).promise;
}
test('an actual shell command keeps Node and UTF-8 output while omitting host-only credentials', async t => {
const f = fixture(t); hostMarkers(t);
const filename = path.join(f.projectRoot, 'probe.mjs');
fs.writeFileSync(filename, 'import assert from "node:assert/strict"; for (const name of ' + JSON.stringify(markers) + ') assert.equal(process.env[name], undefined); console.log("工程终端可用");');
const quote = value => process.platform === 'win32' ? "'" + value.replaceAll("'", "''") + "'" : "'" + value.replaceAll("'", "'\\''") + "'";
const result = await new Runtime(f.store, new Provider()).executeCommand((process.platform === 'win32' ? '& ' : '')
+ quote(process.execPath) + ' ' + quote(filename), f.projectRoot, new AbortController().signal, () => {});
assert.equal(result.status, 'completed', result.stderr); assert.equal(result.exit_code, 0); assert.match(result.stdout, /工程终端可用/);
});
test('real npm tests and build do not inherit arbitrary host credentials or Node injection settings', async t => {
const f = fixture(t); hostMarkers(t);
fs.writeFileSync(path.join(f.projectRoot, 'package.json'), JSON.stringify({ type: 'module', scripts: { test: 'node --test case.test.mjs', build: 'node build.mjs' } }));
fs.writeFileSync(path.join(f.projectRoot, 'case.test.mjs'), 'import test from "node:test"; import assert from "node:assert/strict"; test("project receives no host credentials", () => { for (const name of '
+ JSON.stringify(markers) + ') assert.equal(process.env[name], undefined); assert.equal(process.env.CI, "1"); assert.equal(process.env.NODE_OPTIONS, undefined); });');
fs.writeFileSync(path.join(f.projectRoot, 'build.mjs'), 'import assert from "node:assert/strict"; for (const name of ' + JSON.stringify(markers) + ') assert.equal(process.env[name], undefined); console.log("actual build completed");');
const job = await checks(t, f); assert.equal(job.status, 'passed', job.reason);
assert.equal(job.results.length, 2); assert.equal(job.results[0].tests.total, 1); assert.equal(job.results[0].tests.passed, 1);
assert.equal(job.results[1].exitCode, 0);
});
test('real Python tests and compilation keep their sealed inputs without host credentials', async t => {
const f = fixture(t); hostMarkers(t);
const runtime = await resolvePythonRuntime(f.project, { executable: process.env.AGENT_WORKBENCH_TEST_PYTHON });
assert.equal(runtime.status, 'available', 'Use the independent QA Python interpreter.');
fs.writeFileSync(path.join(f.projectRoot, 'pyproject.toml'), '[project]\nname="owned-environment"\nversion="0.1.0"\n[tool.agent-workbench.python]\nframework="unittest"\ntest-paths=["tests"]\n');
fs.mkdirSync(path.join(f.projectRoot, 'tests'));
fs.writeFileSync(path.join(f.projectRoot, 'tests/test_environment.py'), 'import os, unittest\nclass Environment(unittest.TestCase):\n def test_host_credentials_absent(self):\n for name in '
+ JSON.stringify(markers) + ':\n self.assertNotIn(name, os.environ)\n self.assertEqual(os.environ.get("PYTEST_DISABLE_PLUGIN_AUTOLOAD"), "1")\n');
const job = await checks(t, f, { pythonExecutable: runtime.logicalExecutable });
assert.equal(job.status, 'passed', job.reason); assert.equal(job.results[0].tests.passed, 1);
assert.ok(job.results.every(result => result.evidenceValidated && result.exitCode === 0));
});