chore(node-sdk): audit test-support and test

test-support/harness.mjs owns process, server, JSON, fixture, and plugin-runner helpers; provider helper files keep their APIs as thin wrappers. Guard fixtures write 0600 files and the CLI security test isolates XDG_CONFIG_HOME, so the suite passes on Linux.

Claude-Session: https://claude.ai/code/session_01Agoc9bRVE9KL7mmWtey9Vq
This commit is contained in:
雷电芽衣 committed 2026-09-02 22:22:45 -04:00
1 parent 25bfa39fcf
commit 90bea63eea
19 files changed
+785 -2120

No files matched your search

+43 -138
View File
@@ -1,87 +1,44 @@
import { spawn } from 'node:child_process';
import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises';
import http from 'node:http';
import os from 'node:os';
import path from 'node:path';
import { pathToFileURL } from 'node:url';
import {
createFixtureRoot,
distUrl,
homeEnvironment,
installConfig as baseInstallConfig,
runPlugin as basePlugin,
runProcess,
writeOptionalJson,
} from './harness.mjs';
export const VALID_PRIVATE_KEY = Buffer.alloc(32, 11).toString('base64url');
export const pluginModuleUrl = pathToFileURL(path.resolve('dist/plugins/copilot.js')).href;
export const contractModuleUrl = pathToFileURL(
path.resolve('dist/plugins/copilot-contract.js'),
).href;
export const installationModuleUrl = pathToFileURL(
path.resolve('dist/plugins/copilot-installation.js'),
).href;
export const ioModuleUrl = pathToFileURL(path.resolve('dist/plugins/copilot-io.js')).href;
export const registryModuleUrl = pathToFileURL(path.resolve('dist/plugins/registry.js')).href;
export const cliPath = path.resolve('dist/cli.js');
export {
VALID_PRIVATE_KEY,
cliPath,
readJson,
registryModuleUrl,
runNode,
runProcess,
startApiServer,
writeJson,
} from './harness.mjs';
export function runProcess(command, args, env, cwd, input = '') {
return new Promise((resolve, reject) => {
const childEnvironment = { ...process.env, ...env };
for (const [name, value] of Object.entries(env)) {
if (value === undefined) delete childEnvironment[name];
}
const child = spawn(command, args, {
cwd,
env: childEnvironment,
stdio: ['pipe', 'pipe', 'pipe'],
windowsHide: true,
});
let stdout = '';
let stderr = '';
child.stdout.on('data', (chunk) => { stdout += chunk; });
child.stderr.on('data', (chunk) => { stderr += chunk; });
child.once('error', reject);
child.once('close', (code) => resolve({ code, stdout, stderr }));
child.stdin.end(input);
});
}
export function runNode(args, env, cwd, input = '') {
return runProcess(process.execPath, args, env, cwd, input);
}
export const pluginModuleUrl = distUrl('plugins/copilot.js');
export const contractModuleUrl = distUrl('plugins/copilot-contract.js');
export const installationModuleUrl = distUrl('plugins/copilot-installation.js');
export const ioModuleUrl = distUrl('plugins/copilot-io.js');
const PLUGIN = { exportName: 'copilotPlugin', moduleUrl: pluginModuleUrl };
export function environment(fixture, copilotHome = fixture.copilotHome) {
return {
HOME: fixture.homeDir,
USERPROFILE: fixture.homeDir,
...homeEnvironment(fixture),
COPILOT_HOME: copilotHome === null ? undefined : copilotHome,
};
}
export function installConfig(fixture, overrides = {}) {
return {
agentName: 'copilot',
orgId: 'org-1',
agentId: fixture.agentId,
privateKey: VALID_PRIVATE_KEY,
kid: 'kid-1',
token: 'token-1',
baseUrl: fixture.baseUrl,
guardScriptPath: fixture.guardScriptPath,
hookScriptPath: fixture.hookScriptPath,
...overrides,
};
return baseInstallConfig('copilot', fixture, overrides);
}
export async function runPlugin(fixture, method, argument, copilotHome = fixture.copilotHome) {
const source = `
import { copilotPlugin } from ${JSON.stringify(pluginModuleUrl)};
const argument = JSON.parse(process.env.ELYDORA_TEST_ARGUMENT);
const result = await copilotPlugin[process.env.ELYDORA_TEST_METHOD](argument);
if (result !== undefined) console.log(JSON.stringify(result));
`;
return runNode(
['--input-type=module', '--eval', source],
{
...environment(fixture, copilotHome),
ELYDORA_TEST_ARGUMENT: JSON.stringify(argument),
ELYDORA_TEST_METHOD: method,
},
fixture.projectDir,
);
export function runPlugin(fixture, method, argument, copilotHome = fixture.copilotHome) {
return basePlugin(PLUGIN, fixture, method, argument, { env: environment(fixture, copilotHome) });
}
export function runHook(handler, fixture, input) {
@@ -91,16 +48,6 @@ export function runHook(handler, fixture, input) {
return runProcess(command[0], command[1], environment(fixture), fixture.projectDir, input);
}
export async function writeJson(filePath, value) {
await mkdir(path.dirname(filePath), { recursive: true });
const source = typeof value === 'string' ? value : `${JSON.stringify(value, null, 2)}\n`;
await writeFile(filePath, source, { mode: 0o600 });
}
async function writeOptional(filePath, value) {
if (value !== undefined) await writeJson(filePath, value);
}
export async function createFixture({
agentId = 'agent-1',
baseUrl = 'http://127.0.0.1:9',
@@ -113,46 +60,36 @@ export async function createFixture({
repositorySettings,
localSettings,
} = {}) {
const rootDir = await mkdtemp(path.join(os.tmpdir(), 'elydora-copilot-'));
const homeDir = path.join(rootDir, "home with spaces and 'quote %COPILOT%");
const projectDir = path.join(rootDir, 'project with spaces');
const base = await createFixtureRoot(
'elydora-copilot-',
"home with spaces and 'quote %COPILOT%",
{ agentId },
);
const { projectDir, rootDir } = base;
const copilotHome = path.join(rootDir, "custom Copilot 'home");
const hooksDir = path.join(copilotHome, 'hooks');
const configPath = path.join(hooksDir, 'elydora-audit.json');
const legacyPath = path.join(projectDir, '.github', 'hooks', 'hooks.json');
const agentDir = path.join(homeDir, '.elydora', agentId);
const guardScriptPath = path.join(agentDir, 'guard.js');
const hookScriptPath = path.join(agentDir, 'hook.js');
await mkdir(projectDir, { recursive: true });
await Promise.all([
writeOptional(configPath, userConfig),
writeOptional(legacyPath, legacyConfig),
writeOptional(path.join(copilotHome, 'settings.json'), userSettings),
writeOptional(path.join(copilotHome, 'config.json'), legacyUserConfig),
writeOptional(path.join(projectDir, '.claude', 'settings.json'), claudeSettings),
writeOptional(path.join(projectDir, '.claude', 'settings.local.json'), claudeLocalSettings),
writeOptional(path.join(projectDir, '.github', 'copilot', 'settings.json'), repositorySettings),
writeOptional(path.join(projectDir, '.github', 'copilot', 'settings.local.json'), localSettings),
writeOptionalJson(configPath, userConfig),
writeOptionalJson(legacyPath, legacyConfig),
writeOptionalJson(path.join(copilotHome, 'settings.json'), userSettings),
writeOptionalJson(path.join(copilotHome, 'config.json'), legacyUserConfig),
writeOptionalJson(path.join(projectDir, '.claude', 'settings.json'), claudeSettings),
writeOptionalJson(path.join(projectDir, '.claude', 'settings.local.json'), claudeLocalSettings),
writeOptionalJson(path.join(projectDir, '.github', 'copilot', 'settings.json'), repositorySettings),
writeOptionalJson(path.join(projectDir, '.github', 'copilot', 'settings.local.json'), localSettings),
]);
return {
agentDir,
agentId,
...base,
baseUrl,
configPath,
copilotHome,
guardScriptPath,
homeDir,
hookScriptPath,
hooksDir,
legacyPath,
projectDir,
rootDir,
install(overrides = {}, copilotHomeOverride = this.copilotHome) {
return runPlugin(this, 'install', installConfig(this, overrides), copilotHomeOverride);
},
async close() {
await rm(rootDir, { recursive: true, force: true });
},
};
}
@@ -193,35 +130,3 @@ export function legacyManagedConfig(fixture, extraHooks = {}) {
},
};
}
export async function readJson(filePath) {
return JSON.parse(await readFile(filePath, 'utf-8'));
}
export async function startApiServer() {
const requests = [];
const server = http.createServer(async (request, response) => {
const chunks = [];
for await (const chunk of request) chunks.push(chunk);
const raw = Buffer.concat(chunks).toString('utf-8');
requests.push({ method: request.method, url: request.url, raw });
response.writeHead(request.method === 'POST' ? 201 : 200, {
'Content-Type': 'application/json',
});
response.end(request.method === 'POST'
? '{"operation":{"accepted":true}}'
: '{"agent":{"status":"active"}}');
});
await new Promise((resolve, reject) => {
server.once('error', reject);
server.listen(0, '127.0.0.1', resolve);
});
const address = server.address();
return {
baseUrl: `http://127.0.0.1:${address.port}`,
requests,
close: () => new Promise((resolve, reject) => {
server.close((error) => error ? reject(error) : resolve());
}),
};
}