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

+36 -147
View File
@@ -1,140 +1,60 @@
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 path from 'node:path';
import { pathToFileURL } from 'node:url'; import {
createFixtureRoot,
distUrl,
homeEnvironment,
installConfig as baseInstallConfig,
runPlugin as basePlugin,
runShell,
writeOptionalJson,
} from './harness.mjs';
export const VALID_PRIVATE_KEY = Buffer.alloc(32).toString('base64url'); export {
export const pluginModuleUrl = pathToFileURL(path.resolve('dist/plugins/augment.js')).href; VALID_PRIVATE_KEY,
export const registryModuleUrl = pathToFileURL(path.resolve('dist/plugins/registry.js')).href; cliPath,
export const ioModuleUrl = pathToFileURL(path.resolve('dist/plugins/augment-io.js')).href; managedInstallationModuleUrl,
export const installationModuleUrl = pathToFileURL( readSettings,
path.resolve('dist/plugins/augment-installation.js'), registryModuleUrl,
).href; runNode,
export const managedInstallationModuleUrl = pathToFileURL( runProcess,
path.resolve('dist/plugins/managed-installation.js'), startApiServer,
).href; writeJson,
export const cliPath = path.resolve('dist/cli.js'); } from './harness.mjs';
export const pluginModuleUrl = distUrl('plugins/augment.js');
export const ioModuleUrl = distUrl('plugins/augment-io.js');
export const installationModuleUrl = distUrl('plugins/augment-installation.js');
export const wrapperExtension = process.platform === 'win32' ? '.cmd' : '.sh'; export const wrapperExtension = process.platform === 'win32' ? '.cmd' : '.sh';
export const guardWrapperName = `augment-guard${wrapperExtension}`; export const guardWrapperName = `augment-guard${wrapperExtension}`;
export const auditWrapperName = `augment-hook${wrapperExtension}`; export const auditWrapperName = `augment-hook${wrapperExtension}`;
const PLUGIN = { exportName: 'augmentPlugin', moduleUrl: pluginModuleUrl };
export function runProcess(command, args, env, cwd, input = '') {
return new Promise((resolve, reject) => {
const child = spawn(command, args, {
cwd,
env: { ...process.env, ...env },
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 function runHandler(handler, input, fixture) { export function runHandler(handler, input, fixture) {
return new Promise((resolve, reject) => { return runShell(handler.command, homeEnvironment(fixture), fixture.projectDir, input);
const child = spawn(handler.command, {
cwd: fixture.projectDir,
env: { ...process.env, HOME: fixture.homeDir, USERPROFILE: fixture.homeDir },
shell: true,
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 async function writeJson(filePath, value) {
await mkdir(path.dirname(filePath), { recursive: true });
const contents = typeof value === 'string' ? value : `${JSON.stringify(value, null, 2)}\n`;
await writeFile(filePath, contents, { encoding: 'utf-8', mode: 0o600 });
} }
export function installConfig(fixture, overrides = {}) { export function installConfig(fixture, overrides = {}) {
return { return baseInstallConfig('augment', fixture, overrides);
agentName: 'augment',
orgId: 'org-1',
agentId: 'agent-1',
privateKey: VALID_PRIVATE_KEY,
kid: 'kid-1',
token: 'token-1',
baseUrl: fixture.baseUrl,
guardScriptPath: fixture.guardScriptPath,
hookScriptPath: fixture.hookScriptPath,
...overrides,
};
} }
export async function runPlugin(fixture, method, argument) { export function runPlugin(fixture, method, argument) {
const source = ` return basePlugin(PLUGIN, fixture, method, argument);
import { augmentPlugin } from ${JSON.stringify(pluginModuleUrl)};
const argument = JSON.parse(process.env.ELYDORA_TEST_ARGUMENT);
const result = await augmentPlugin[process.env.ELYDORA_TEST_METHOD](argument);
if (result !== undefined) console.log(JSON.stringify(result));
`;
return runNode(
['--input-type=module', '--eval', source],
{
HOME: fixture.homeDir,
USERPROFILE: fixture.homeDir,
ELYDORA_TEST_ARGUMENT: JSON.stringify(argument),
ELYDORA_TEST_METHOD: method,
},
fixture.projectDir,
);
} }
export async function createFixture({ baseUrl = 'http://127.0.0.1:9', settings } = {}) { export async function createFixture({ baseUrl = 'http://127.0.0.1:9', settings } = {}) {
const rootDir = await mkdtemp(path.join(os.tmpdir(), 'elydora-augment-')); const base = await createFixtureRoot('elydora-augment-', "home with spaces and 'quote %AUGGIE%");
const homeDir = path.join(rootDir, "home with spaces and 'quote %AUGGIE%"); const settingsPath = path.join(base.homeDir, '.augment', 'settings.json');
const projectDir = path.join(rootDir, 'project with spaces'); await writeOptionalJson(settingsPath, settings);
const settingsPath = path.join(homeDir, '.augment', 'settings.json'); return {
const agentDir = path.join(homeDir, '.elydora', 'agent-1'); ...base,
const guardScriptPath = path.join(agentDir, 'guard.js'); auditWrapperPath: path.join(base.agentDir, auditWrapperName),
const hookScriptPath = path.join(agentDir, 'hook.js');
await mkdir(projectDir, { recursive: true });
if (settings !== undefined) await writeJson(settingsPath, settings);
const fixture = {
agentDir,
auditWrapperPath: path.join(agentDir, auditWrapperName),
baseUrl, baseUrl,
guardScriptPath, guardWrapperPath: path.join(base.agentDir, guardWrapperName),
guardWrapperPath: path.join(agentDir, guardWrapperName),
homeDir,
hookScriptPath,
projectDir,
rootDir,
settingsPath, settingsPath,
install(overrides = {}) { install(overrides = {}) {
return runPlugin(this, 'install', installConfig(this, overrides)); return runPlugin(this, 'install', installConfig(this, overrides));
}, },
async close() {
await rm(rootDir, { recursive: true, force: true });
},
}; };
return fixture;
}
export async function readSettings(filePath) {
const raw = await readFile(filePath, 'utf-8');
return { raw, settings: JSON.parse(raw) };
} }
export function generatedCommand(wrapperPath) { export function generatedCommand(wrapperPath) {
@@ -150,34 +70,3 @@ export function managedHandler(settings, event, wrapperPath) {
} }
return undefined; return undefined;
} }
export async function startApiServer({ status = 'active', operationStatus = 201 } = {}) {
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({ headers: request.headers, method: request.method, url: request.url, raw });
if (request.method === 'GET') {
response.writeHead(200, { 'Content-Type': 'application/json' });
response.end(JSON.stringify({ agent: { status } }));
return;
}
response.writeHead(operationStatus, { 'Content-Type': 'application/json' });
response.end(operationStatus >= 200 && operationStatus < 300
? JSON.stringify({ operation: { accepted: true } })
: JSON.stringify({ error: { code: 'UPSTREAM_FAILURE', message: 'failed' } }));
});
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());
}),
};
}
+41 -131
View File
@@ -1,38 +1,32 @@
import assert from 'node:assert/strict'; import assert from 'node:assert/strict';
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 path from 'node:path';
import { pathToFileURL } from 'node:url'; import {
createFixtureRoot,
distUrl,
homeEnvironment,
installConfig as baseInstallConfig,
optionalEnvironment,
runPlugin as basePlugin,
runProcess as baseProcess,
writeOptionalJson,
} from './harness.mjs';
export const VALID_PRIVATE_KEY = Buffer.alloc(32).toString('base64url'); export {
export const pluginModuleUrl = pathToFileURL(path.resolve('dist/plugins/claudecode.js')).href; VALID_PRIVATE_KEY,
export const registryModuleUrl = pathToFileURL(path.resolve('dist/plugins/registry.js')).href; cliPath,
export const ioModuleUrl = pathToFileURL(path.resolve('dist/plugins/claudecode-io.js')).href; readSettings,
export const installationModuleUrl = pathToFileURL( registryModuleUrl,
path.resolve('dist/plugins/claudecode-installation.js'), startApiServer,
).href; writeJson,
export const cliPath = path.resolve('dist/cli.js'); } from './harness.mjs';
export const pluginModuleUrl = distUrl('plugins/claudecode.js');
export const ioModuleUrl = distUrl('plugins/claudecode-io.js');
export const installationModuleUrl = distUrl('plugins/claudecode-installation.js');
const PLUGIN = { exportName: 'claudecodePlugin', moduleUrl: pluginModuleUrl };
export function runProcess(command, args, env, cwd, input = '', unset = []) { export function runProcess(command, args, env, cwd, input = '', unset = []) {
return new Promise((resolve, reject) => { return baseProcess(command, args, env, cwd, input, { unset });
const childEnv = { ...process.env, ...env };
for (const key of unset) delete childEnv[key];
const child = spawn(command, args, {
cwd,
env: childEnv,
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 = '', unset = []) { export function runNode(args, env, cwd, input = '', unset = []) {
@@ -44,8 +38,7 @@ export function runClaudeHook(handler, input, fixture, environment = {}) {
handler.command, handler.command,
handler.args, handler.args,
{ {
HOME: fixture.homeDir, ...homeEnvironment(fixture),
USERPROFILE: fixture.homeDir,
CLAUDE_CONFIG_DIR: fixture.claudeConfigOverride ?? '', CLAUDE_CONFIG_DIR: fixture.claudeConfigOverride ?? '',
...environment, ...environment,
}, },
@@ -54,51 +47,16 @@ export function runClaudeHook(handler, input, fixture, environment = {}) {
); );
} }
export async function writeJson(filePath, value) {
await mkdir(path.dirname(filePath), { recursive: true });
const contents = typeof value === 'string' ? value : `${JSON.stringify(value, null, 2)}\n`;
await writeFile(filePath, contents, { encoding: 'utf-8', mode: 0o600 });
}
export function installConfig(fixture, overrides = {}) { export function installConfig(fixture, overrides = {}) {
return { return baseInstallConfig('claudecode', fixture, overrides);
agentName: 'claudecode',
orgId: 'org-1',
agentId: 'agent-1',
privateKey: VALID_PRIVATE_KEY,
kid: 'kid-1',
token: 'token-1',
baseUrl: fixture.baseUrl,
guardScriptPath: fixture.guardScriptPath,
hookScriptPath: fixture.hookScriptPath,
...overrides,
};
} }
export async function runPlugin(fixture, method, argument, environment = {}) { export function runPlugin(fixture, method, argument, environment = {}) {
const source = ` const configDir = optionalEnvironment('CLAUDE_CONFIG_DIR', fixture.claudeConfigOverride);
import { claudecodePlugin } from ${JSON.stringify(pluginModuleUrl)}; return basePlugin(PLUGIN, fixture, method, argument, {
const argument = JSON.parse(process.env.ELYDORA_TEST_ARGUMENT); env: { ...environment, ...configDir.env },
const result = await claudecodePlugin[process.env.ELYDORA_TEST_METHOD](argument); unset: configDir.unset,
if (result !== undefined) console.log(JSON.stringify(result)); });
`;
const env = {
HOME: fixture.homeDir,
USERPROFILE: fixture.homeDir,
ELYDORA_TEST_ARGUMENT: JSON.stringify(argument),
ELYDORA_TEST_METHOD: method,
...environment,
};
const unset = [];
if (fixture.claudeConfigOverride === undefined) unset.push('CLAUDE_CONFIG_DIR');
else env.CLAUDE_CONFIG_DIR = fixture.claudeConfigOverride;
return runNode(
['--input-type=module', '--eval', source],
env,
fixture.projectDir,
'',
unset,
);
} }
export async function createFixture({ export async function createFixture({
@@ -106,42 +64,25 @@ export async function createFixture({
settings, settings,
explicitClaudeConfig = true, explicitClaudeConfig = true,
} = {}) { } = {}) {
const rootDir = await mkdtemp(path.join(os.tmpdir(), 'elydora-claude-')); const base = await createFixtureRoot(
const homeDir = path.join(rootDir, "home with spaces and 'quote %CLAUDE_HOOK_EVENT%"); 'elydora-claude-',
const projectDir = path.join(rootDir, 'project with spaces'); "home with spaces and 'quote %CLAUDE_HOOK_EVENT%",
);
const claudeConfigDir = explicitClaudeConfig const claudeConfigDir = explicitClaudeConfig
? path.join(homeDir, 'custom claude') ? path.join(base.homeDir, 'custom claude')
: path.join(homeDir, '.claude'); : path.join(base.homeDir, '.claude');
const settingsPath = path.join(claudeConfigDir, 'settings.json'); const settingsPath = path.join(claudeConfigDir, 'settings.json');
const agentDir = path.join(homeDir, '.elydora', 'agent-1'); await writeOptionalJson(settingsPath, settings);
const guardScriptPath = path.join(agentDir, 'guard.js'); return {
const hookScriptPath = path.join(agentDir, 'hook.js'); ...base,
await mkdir(projectDir, { recursive: true });
if (settings !== undefined) await writeJson(settingsPath, settings);
const fixture = {
agentDir,
baseUrl, baseUrl,
claudeConfigDir, claudeConfigDir,
claudeConfigOverride: explicitClaudeConfig ? claudeConfigDir : undefined, claudeConfigOverride: explicitClaudeConfig ? claudeConfigDir : undefined,
guardScriptPath,
homeDir,
hookScriptPath,
projectDir,
rootDir,
settingsPath, settingsPath,
install(overrides = {}) { install(overrides = {}) {
return runPlugin(this, 'install', installConfig(this, overrides)); return runPlugin(this, 'install', installConfig(this, overrides));
}, },
async close() {
await rm(rootDir, { recursive: true, force: true });
},
}; };
return fixture;
}
export async function readSettings(filePath) {
const raw = await readFile(filePath, 'utf-8');
return { raw, settings: JSON.parse(raw) };
} }
export function managedHandler(settings, event) { export function managedHandler(settings, event) {
@@ -173,34 +114,3 @@ export function assertManagedHandler(handler, scriptPath, statusMessage) {
export function legacyHandler(scriptPath) { export function legacyHandler(scriptPath) {
return { type: 'command', command: `node "${scriptPath}"` }; return { type: 'command', command: `node "${scriptPath}"` };
} }
export async function startApiServer({ status = 'active', operationStatus = 201 } = {}) {
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({ headers: request.headers, method: request.method, url: request.url, raw });
if (request.method === 'GET') {
response.writeHead(200, { 'Content-Type': 'application/json' });
response.end(JSON.stringify({ agent: { status } }));
return;
}
response.writeHead(operationStatus, { 'Content-Type': 'application/json' });
response.end(operationStatus >= 200 && operationStatus < 300
? JSON.stringify({ operation: { accepted: true } })
: JSON.stringify({ error: { code: 'UPSTREAM_FAILURE', message: 'failed' } }));
});
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());
}),
};
}
+34 -122
View File
@@ -1,87 +1,39 @@
import { spawn } from 'node:child_process'; import { mkdir, writeFile } from 'node:fs/promises';
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 path from 'node:path';
import { pathToFileURL } from 'node:url'; import {
createFixtureRoot,
distUrl,
homeEnvironment,
installConfig as baseInstallConfig,
runPlugin as basePlugin,
} from './harness.mjs';
export const VALID_PRIVATE_KEY = Buffer.alloc(32, 7).toString('base64url'); export {
export const pluginModuleUrl = pathToFileURL(path.resolve('dist/plugins/cline.js')).href; VALID_PRIVATE_KEY,
export const contractModuleUrl = pathToFileURL( cliPath,
path.resolve('dist/plugins/cline-contract.js'), readJson,
).href; registryModuleUrl,
export const ioModuleUrl = pathToFileURL(path.resolve('dist/plugins/cline-io.js')).href; runNode,
export const installationModuleUrl = pathToFileURL( runProcess,
path.resolve('dist/plugins/cline-installation.js'), startApiServer,
).href; } from './harness.mjs';
export const registryModuleUrl = pathToFileURL(path.resolve('dist/plugins/registry.js')).href;
export const cliPath = path.resolve('dist/cli.js');
export function runProcess(command, args, env, cwd, input = '') { export const pluginModuleUrl = distUrl('plugins/cline.js');
return new Promise((resolve, reject) => { export const contractModuleUrl = distUrl('plugins/cline-contract.js');
const childEnvironment = { ...process.env, ...env }; export const ioModuleUrl = distUrl('plugins/cline-io.js');
for (const [name, value] of Object.entries(env)) { export const installationModuleUrl = distUrl('plugins/cline-installation.js');
if (value === undefined) delete childEnvironment[name]; const PLUGIN = { exportName: 'clinePlugin', moduleUrl: pluginModuleUrl };
}
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 function environment(fixture, clineDir = fixture.clineDir) { export function environment(fixture, clineDir = fixture.clineDir) {
return { return { ...homeEnvironment(fixture), CLINE_DIR: clineDir === null ? undefined : clineDir };
HOME: fixture.homeDir,
USERPROFILE: fixture.homeDir,
CLINE_DIR: clineDir === null ? undefined : clineDir,
};
} }
export function installConfig(fixture, overrides = {}) { export function installConfig(fixture, overrides = {}) {
return { return baseInstallConfig('cline', fixture, overrides);
agentName: 'cline',
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,
};
} }
export async function runPlugin(fixture, method, argument, clineDir = fixture.clineDir) { export function runPlugin(fixture, method, argument, clineDir = fixture.clineDir) {
const source = ` return basePlugin(PLUGIN, fixture, method, argument, { env: environment(fixture, clineDir) });
import { clinePlugin } from ${JSON.stringify(pluginModuleUrl)};
const argument = JSON.parse(process.env.ELYDORA_TEST_ARGUMENT);
const result = await clinePlugin[process.env.ELYDORA_TEST_METHOD](argument);
if (result !== undefined) console.log(JSON.stringify(result));
`;
return runNode(
['--input-type=module', '--eval', source],
{
...environment(fixture, clineDir),
ELYDORA_TEST_ARGUMENT: JSON.stringify(argument),
ELYDORA_TEST_METHOD: method,
},
fixture.projectDir,
);
} }
async function writeOptional(filePath, contents) { async function writeOptional(filePath, contents) {
@@ -96,66 +48,26 @@ export async function createFixture({
existingAudit, existingAudit,
existingGuard, existingGuard,
} = {}) { } = {}) {
const rootDir = await mkdtemp(path.join(os.tmpdir(), 'elydora-cline-')); const base = await createFixtureRoot(
const homeDir = path.join(rootDir, "home with spaces and 'quote %CLINE%"); 'elydora-cline-',
const projectDir = path.join(rootDir, 'project with spaces'); "home with spaces and 'quote %CLINE%",
const clineDir = path.join(rootDir, 'custom-cline-home'); { agentId },
);
const clineDir = path.join(base.rootDir, 'custom-cline-home');
const hooksDir = path.join(clineDir, 'hooks'); const hooksDir = path.join(clineDir, 'hooks');
const guardWrapperPath = path.join(hooksDir, 'PreToolUse.mjs'); const guardWrapperPath = path.join(hooksDir, 'PreToolUse.mjs');
const auditWrapperPath = path.join(hooksDir, 'PostToolUse.mjs'); const auditWrapperPath = path.join(hooksDir, 'PostToolUse.mjs');
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 writeOptional(guardWrapperPath, existingGuard); await writeOptional(guardWrapperPath, existingGuard);
await writeOptional(auditWrapperPath, existingAudit); await writeOptional(auditWrapperPath, existingAudit);
const fixture = { return {
agentDir, ...base,
agentId,
auditWrapperPath, auditWrapperPath,
baseUrl, baseUrl,
clineDir, clineDir,
guardScriptPath,
guardWrapperPath, guardWrapperPath,
homeDir,
hookScriptPath,
hooksDir, hooksDir,
projectDir,
rootDir,
install(overrides = {}, clineDirOverride = this.clineDir) { install(overrides = {}, clineDirOverride = this.clineDir) {
return runPlugin(this, 'install', installConfig(this, overrides), clineDirOverride); return runPlugin(this, 'install', installConfig(this, overrides), clineDirOverride);
}, },
async close() {
await rm(rootDir, { recursive: true, force: true });
},
};
return fixture;
}
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(201, { 'Content-Type': 'application/json' });
response.end('{"operation":{"accepted":true}}');
});
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());
}),
}; };
} }
+32 -149
View File
@@ -1,139 +1,58 @@
import assert from 'node:assert/strict'; import assert from 'node:assert/strict';
import { spawn } from 'node:child_process';
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
import http from 'node:http';
import os from 'node:os';
import path from 'node:path'; import path from 'node:path';
import { pathToFileURL } from 'node:url'; import {
createFixtureRoot,
distUrl,
homeEnvironment,
installConfig as baseInstallConfig,
runNode as baseNode,
runPlugin as basePlugin,
runProcess,
runShell,
writeOptionalJson,
} from './harness.mjs';
export const VALID_PRIVATE_KEY = Buffer.alloc(32).toString('base64url'); export { VALID_PRIVATE_KEY, registryModuleUrl, startApiServer, writeJson } from './harness.mjs';
export const pluginModuleUrl = pathToFileURL(path.resolve('dist/plugins/codex.js')).href;
export const registryModuleUrl = pathToFileURL(path.resolve('dist/plugins/registry.js')).href; export const pluginModuleUrl = distUrl('plugins/codex.js');
export const ioModuleUrl = pathToFileURL(path.resolve('dist/plugins/codex-io.js')).href; export const ioModuleUrl = distUrl('plugins/codex-io.js');
export const installationModuleUrl = pathToFileURL( export const installationModuleUrl = distUrl('plugins/codex-installation.js');
path.resolve('dist/plugins/codex-installation.js'), const PLUGIN = { exportName: 'codexPlugin', moduleUrl: pluginModuleUrl };
).href;
export function runNode(args, env, cwd, input = '') { export function runNode(args, env, cwd, input = '') {
return new Promise((resolve, reject) => { return baseNode(args, { CODEX_HOME: '', ...env }, cwd, input);
const child = spawn(process.execPath, args, {
cwd,
env: { ...process.env, CODEX_HOME: '', ...env },
stdio: ['pipe', 'pipe', 'pipe'],
});
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 runHook(handler, input, fixture, environment = {}) { export function runHook(handler, input, fixture, environment = {}) {
const command = process.platform === 'win32' ? handler.commandWindows : handler.command; const command = process.platform === 'win32' ? handler.commandWindows : handler.command;
return new Promise((resolve, reject) => { const env = { CODEX_HOME: '', ...homeEnvironment(fixture), ...environment };
const child = process.platform === 'win32' return process.platform === 'win32'
? spawn(command, { ? runShell(command, env, fixture.projectDir, input)
shell: true, : runProcess('/bin/sh', ['-c', command], env, fixture.projectDir, input);
cwd: fixture.projectDir,
env: {
...process.env,
CODEX_HOME: '',
HOME: fixture.homeDir,
USERPROFILE: fixture.homeDir,
...environment,
},
stdio: ['pipe', 'pipe', 'pipe'],
})
: spawn('/bin/sh', ['-c', command], {
cwd: fixture.projectDir,
env: {
...process.env,
CODEX_HOME: '',
HOME: fixture.homeDir,
USERPROFILE: fixture.homeDir,
...environment,
},
stdio: ['pipe', 'pipe', 'pipe'],
});
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 async function writeJson(filePath, value) {
await mkdir(path.dirname(filePath), { recursive: true });
const contents = typeof value === 'string' ? value : `${JSON.stringify(value, null, 2)}\n`;
await writeFile(filePath, contents, { encoding: 'utf-8', mode: 0o600 });
} }
export function installConfig(fixture, overrides = {}) { export function installConfig(fixture, overrides = {}) {
return { return baseInstallConfig('codex', fixture, overrides);
agentName: 'codex',
orgId: 'org-1',
agentId: 'agent-1',
privateKey: VALID_PRIVATE_KEY,
kid: 'kid-1',
token: 'token-1',
baseUrl: fixture.baseUrl,
guardScriptPath: fixture.guardScriptPath,
hookScriptPath: fixture.hookScriptPath,
...overrides,
};
} }
export async function runPlugin(fixture, method, argument, environment = {}) { export function runPlugin(fixture, method, argument, environment = {}) {
const source = ` return basePlugin(PLUGIN, fixture, method, argument, { env: { CODEX_HOME: '', ...environment } });
import { codexPlugin } from ${JSON.stringify(pluginModuleUrl)};
const argument = JSON.parse(process.env.ELYDORA_TEST_ARGUMENT);
const result = await codexPlugin[process.env.ELYDORA_TEST_METHOD](argument);
if (result !== undefined) console.log(JSON.stringify(result));
`;
return runNode(
['--input-type=module', '--eval', source],
{
HOME: fixture.homeDir,
USERPROFILE: fixture.homeDir,
ELYDORA_TEST_ARGUMENT: JSON.stringify(argument),
ELYDORA_TEST_METHOD: method,
...environment,
},
fixture.projectDir,
);
} }
export async function createFixture({ existingConfig, baseUrl = 'http://127.0.0.1:9' } = {}) { export async function createFixture({ existingConfig, baseUrl = 'http://127.0.0.1:9' } = {}) {
const rootDir = await mkdtemp(path.join(os.tmpdir(), 'elydora-codex-')); const base = await createFixtureRoot(
const homeDir = path.join(rootDir, "home with spaces and 'quote %ELYDORA_HOOK_PATH%"); 'elydora-codex-',
const projectDir = path.join(rootDir, 'project with spaces'); "home with spaces and 'quote %ELYDORA_HOOK_PATH%",
const configPath = path.join(homeDir, '.codex', 'hooks.json'); );
const agentDir = path.join(homeDir, '.elydora', 'agent-1'); const configPath = path.join(base.homeDir, '.codex', 'hooks.json');
const guardScriptPath = path.join(agentDir, 'guard.js'); await writeOptionalJson(configPath, existingConfig);
const hookScriptPath = path.join(agentDir, 'hook.js');
await mkdir(projectDir, { recursive: true });
if (existingConfig !== undefined) await writeJson(configPath, existingConfig);
return { return {
agentDir, ...base,
baseUrl, baseUrl,
configPath, configPath,
guardScriptPath,
homeDir,
hookScriptPath,
projectDir,
rootDir,
install(overrides = {}) { install(overrides = {}) {
return runPlugin(this, 'install', installConfig(this, overrides)); return runPlugin(this, 'install', installConfig(this, overrides));
}, },
async close() {
await rm(rootDir, { recursive: true, force: true });
},
}; };
} }
@@ -170,39 +89,3 @@ export function legacyHandler(scriptPath, statusMessage) {
statusMessage, statusMessage,
}; };
} }
export async function startApiServer({ status = 'active', operationStatus = 201 } = {}) {
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({
headers: request.headers,
method: request.method,
url: request.url,
raw,
});
if (request.method === 'GET') {
response.writeHead(200, { 'Content-Type': 'application/json' });
response.end(JSON.stringify({ agent: { status } }));
return;
}
response.writeHead(operationStatus, { 'Content-Type': 'application/json' });
response.end(operationStatus >= 200 && operationStatus < 300
? JSON.stringify({ operation: { accepted: true } })
: JSON.stringify({ error: { code: 'UPSTREAM_FAILURE', message: 'failed' } }));
});
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());
}),
};
}
+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 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 {
export const pluginModuleUrl = pathToFileURL(path.resolve('dist/plugins/copilot.js')).href; VALID_PRIVATE_KEY,
export const contractModuleUrl = pathToFileURL( cliPath,
path.resolve('dist/plugins/copilot-contract.js'), readJson,
).href; registryModuleUrl,
export const installationModuleUrl = pathToFileURL( runNode,
path.resolve('dist/plugins/copilot-installation.js'), runProcess,
).href; startApiServer,
export const ioModuleUrl = pathToFileURL(path.resolve('dist/plugins/copilot-io.js')).href; writeJson,
export const registryModuleUrl = pathToFileURL(path.resolve('dist/plugins/registry.js')).href; } from './harness.mjs';
export const cliPath = path.resolve('dist/cli.js');
export function runProcess(command, args, env, cwd, input = '') { export const pluginModuleUrl = distUrl('plugins/copilot.js');
return new Promise((resolve, reject) => { export const contractModuleUrl = distUrl('plugins/copilot-contract.js');
const childEnvironment = { ...process.env, ...env }; export const installationModuleUrl = distUrl('plugins/copilot-installation.js');
for (const [name, value] of Object.entries(env)) { export const ioModuleUrl = distUrl('plugins/copilot-io.js');
if (value === undefined) delete childEnvironment[name]; const PLUGIN = { exportName: 'copilotPlugin', moduleUrl: pluginModuleUrl };
}
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 function environment(fixture, copilotHome = fixture.copilotHome) { export function environment(fixture, copilotHome = fixture.copilotHome) {
return { return {
HOME: fixture.homeDir, ...homeEnvironment(fixture),
USERPROFILE: fixture.homeDir,
COPILOT_HOME: copilotHome === null ? undefined : copilotHome, COPILOT_HOME: copilotHome === null ? undefined : copilotHome,
}; };
} }
export function installConfig(fixture, overrides = {}) { export function installConfig(fixture, overrides = {}) {
return { return baseInstallConfig('copilot', fixture, overrides);
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,
};
} }
export async function runPlugin(fixture, method, argument, copilotHome = fixture.copilotHome) { export function runPlugin(fixture, method, argument, copilotHome = fixture.copilotHome) {
const source = ` return basePlugin(PLUGIN, fixture, method, argument, { env: environment(fixture, copilotHome) });
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 runHook(handler, fixture, input) { 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); 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({ export async function createFixture({
agentId = 'agent-1', agentId = 'agent-1',
baseUrl = 'http://127.0.0.1:9', baseUrl = 'http://127.0.0.1:9',
@@ -113,46 +60,36 @@ export async function createFixture({
repositorySettings, repositorySettings,
localSettings, localSettings,
} = {}) { } = {}) {
const rootDir = await mkdtemp(path.join(os.tmpdir(), 'elydora-copilot-')); const base = await createFixtureRoot(
const homeDir = path.join(rootDir, "home with spaces and 'quote %COPILOT%"); 'elydora-copilot-',
const projectDir = path.join(rootDir, 'project with spaces'); "home with spaces and 'quote %COPILOT%",
{ agentId },
);
const { projectDir, rootDir } = base;
const copilotHome = path.join(rootDir, "custom Copilot 'home"); const copilotHome = path.join(rootDir, "custom Copilot 'home");
const hooksDir = path.join(copilotHome, 'hooks'); const hooksDir = path.join(copilotHome, 'hooks');
const configPath = path.join(hooksDir, 'elydora-audit.json'); const configPath = path.join(hooksDir, 'elydora-audit.json');
const legacyPath = path.join(projectDir, '.github', 'hooks', 'hooks.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([ await Promise.all([
writeOptional(configPath, userConfig), writeOptionalJson(configPath, userConfig),
writeOptional(legacyPath, legacyConfig), writeOptionalJson(legacyPath, legacyConfig),
writeOptional(path.join(copilotHome, 'settings.json'), userSettings), writeOptionalJson(path.join(copilotHome, 'settings.json'), userSettings),
writeOptional(path.join(copilotHome, 'config.json'), legacyUserConfig), writeOptionalJson(path.join(copilotHome, 'config.json'), legacyUserConfig),
writeOptional(path.join(projectDir, '.claude', 'settings.json'), claudeSettings), writeOptionalJson(path.join(projectDir, '.claude', 'settings.json'), claudeSettings),
writeOptional(path.join(projectDir, '.claude', 'settings.local.json'), claudeLocalSettings), writeOptionalJson(path.join(projectDir, '.claude', 'settings.local.json'), claudeLocalSettings),
writeOptional(path.join(projectDir, '.github', 'copilot', 'settings.json'), repositorySettings), writeOptionalJson(path.join(projectDir, '.github', 'copilot', 'settings.json'), repositorySettings),
writeOptional(path.join(projectDir, '.github', 'copilot', 'settings.local.json'), localSettings), writeOptionalJson(path.join(projectDir, '.github', 'copilot', 'settings.local.json'), localSettings),
]); ]);
return { return {
agentDir, ...base,
agentId,
baseUrl, baseUrl,
configPath, configPath,
copilotHome, copilotHome,
guardScriptPath,
homeDir,
hookScriptPath,
hooksDir, hooksDir,
legacyPath, legacyPath,
projectDir,
rootDir,
install(overrides = {}, copilotHomeOverride = this.copilotHome) { install(overrides = {}, copilotHomeOverride = this.copilotHome) {
return runPlugin(this, 'install', installConfig(this, overrides), copilotHomeOverride); 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());
}),
};
}
+29 -131
View File
@@ -1,127 +1,56 @@
import assert from 'node:assert/strict'; import assert from 'node:assert/strict';
import { spawn } from 'node:child_process';
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
import http from 'node:http';
import os from 'node:os';
import path from 'node:path'; 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).toString('base64url'); export {
export const pluginModuleUrl = pathToFileURL(path.resolve('dist/plugins/cursor.js')).href; VALID_PRIVATE_KEY,
export const registryModuleUrl = pathToFileURL(path.resolve('dist/plugins/registry.js')).href; registryModuleUrl,
export const ioModuleUrl = pathToFileURL(path.resolve('dist/plugins/cursor-io.js')).href; runNode,
export const contractModuleUrl = pathToFileURL(path.resolve('dist/plugins/cursor-contract.js')).href; startApiServer,
export const installationModuleUrl = pathToFileURL( writeJson,
path.resolve('dist/plugins/cursor-installation.js'), } from './harness.mjs';
).href;
export function runNode(args, env, cwd, input = '') { export const pluginModuleUrl = distUrl('plugins/cursor.js');
return new Promise((resolve, reject) => { export const ioModuleUrl = distUrl('plugins/cursor-io.js');
const child = spawn(process.execPath, args, { export const contractModuleUrl = distUrl('plugins/cursor-contract.js');
cwd, export const installationModuleUrl = distUrl('plugins/cursor-installation.js');
env: { ...process.env, ...env }, const PLUGIN = { exportName: 'cursorPlugin', moduleUrl: pluginModuleUrl };
stdio: ['pipe', 'pipe', 'pipe'],
});
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 runHook(handler, input, fixture, environment = {}) { export function runHook(handler, input, fixture, environment = {}) {
const executable = process.platform === 'win32' ? 'powershell.exe' : '/bin/sh'; const executable = process.platform === 'win32' ? 'powershell.exe' : '/bin/sh';
const args = process.platform === 'win32' const args = process.platform === 'win32'
? ['-NoProfile', '-NonInteractive', '-Command', handler.command] ? ['-NoProfile', '-NonInteractive', '-Command', handler.command]
: ['-c', handler.command]; : ['-c', handler.command];
return new Promise((resolve, reject) => { return runProcess(executable, args, { ...homeEnvironment(fixture), ...environment }, undefined, input);
const child = spawn(executable, args, {
env: {
...process.env,
HOME: fixture.homeDir,
USERPROFILE: fixture.homeDir,
...environment,
},
stdio: ['pipe', 'pipe', 'pipe'],
});
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 async function writeJson(filePath, value) {
await mkdir(path.dirname(filePath), { recursive: true });
const contents = typeof value === 'string' ? value : `${JSON.stringify(value, null, 2)}\n`;
await writeFile(filePath, contents, { encoding: 'utf-8', mode: 0o600 });
} }
export function installConfig(fixture, overrides = {}) { export function installConfig(fixture, overrides = {}) {
return { return baseInstallConfig('cursor', fixture, overrides);
agentName: 'cursor',
orgId: 'org-1',
agentId: 'agent-1',
privateKey: VALID_PRIVATE_KEY,
kid: 'kid-1',
token: 'token-1',
baseUrl: fixture.baseUrl,
guardScriptPath: fixture.guardScriptPath,
hookScriptPath: fixture.hookScriptPath,
...overrides,
};
} }
export async function runPlugin(fixture, method, argument) { export function runPlugin(fixture, method, argument) {
const source = ` return basePlugin(PLUGIN, fixture, method, argument);
import { cursorPlugin } from ${JSON.stringify(pluginModuleUrl)};
const argument = JSON.parse(process.env.ELYDORA_TEST_ARGUMENT);
const result = await cursorPlugin[process.env.ELYDORA_TEST_METHOD](argument);
if (result !== undefined) console.log(JSON.stringify(result));
`;
return runNode(
['--input-type=module', '--eval', source],
{
HOME: fixture.homeDir,
USERPROFILE: fixture.homeDir,
ELYDORA_TEST_ARGUMENT: JSON.stringify(argument),
ELYDORA_TEST_METHOD: method,
},
fixture.projectDir,
);
} }
export async function createFixture({ existingConfig, baseUrl = 'http://127.0.0.1:9' } = {}) { export async function createFixture({ existingConfig, baseUrl = 'http://127.0.0.1:9' } = {}) {
const rootDir = await mkdtemp(path.join(os.tmpdir(), 'elydora-cursor-')); const base = await createFixtureRoot('elydora-cursor-', "home with spaces and 'quote");
const homeDir = path.join(rootDir, "home with spaces and 'quote"); const configPath = path.join(base.homeDir, '.cursor', 'hooks.json');
const projectDir = path.join(rootDir, 'project with spaces'); await writeOptionalJson(configPath, existingConfig);
const configPath = path.join(homeDir, '.cursor', 'hooks.json');
const agentDir = path.join(homeDir, '.elydora', 'agent-1');
const guardScriptPath = path.join(agentDir, 'guard.js');
const hookScriptPath = path.join(agentDir, 'hook.js');
await mkdir(projectDir, { recursive: true });
if (existingConfig !== undefined) await writeJson(configPath, existingConfig);
return { return {
agentDir, ...base,
baseUrl, baseUrl,
configPath, configPath,
guardScriptPath,
homeDir,
hookScriptPath,
projectDir,
rootDir,
install(overrides = {}) { install(overrides = {}) {
return runPlugin(this, 'install', installConfig(this, overrides)); return runPlugin(this, 'install', installConfig(this, overrides));
}, },
async close() {
await rm(rootDir, { recursive: true, force: true });
},
}; };
} }
@@ -145,34 +74,3 @@ export function assertNativeHandler(handler) {
export function legacyHandler(scriptPath) { export function legacyHandler(scriptPath) {
return { command: `node "${scriptPath}"` }; return { command: `node "${scriptPath}"` };
} }
export async function startApiServer({ status = 'active', operationStatus = 201 } = {}) {
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 });
if (request.method === 'GET') {
response.writeHead(200, { 'Content-Type': 'application/json' });
response.end(JSON.stringify({ agent: { status } }));
return;
}
response.writeHead(operationStatus, { 'Content-Type': 'application/json' });
response.end(operationStatus >= 200 && operationStatus < 300
? JSON.stringify({ operation: { accepted: true } })
: JSON.stringify({ error: { code: 'UPSTREAM_FAILURE', message: 'failed' } }));
});
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());
}),
};
}
+48 -129
View File
@@ -1,104 +1,58 @@
import assert from 'node:assert/strict'; import assert from 'node:assert/strict';
import { spawn } from 'node:child_process'; import { mkdir, readFile, readdir } from 'node:fs/promises';
import { mkdir, mkdtemp, readFile, readdir, rm, writeFile } from 'node:fs/promises';
import http from 'node:http';
import os from 'node:os';
import path from 'node:path'; import path from 'node:path';
import { pathToFileURL } from 'node:url';
import { parse } from 'jsonc-parser'; import { parse } from 'jsonc-parser';
import {
createFixtureRoot,
distUrl,
homeEnvironment,
installConfig as baseInstallConfig,
runPlugin as basePlugin,
runProcess as baseProcess,
writeOptionalJson,
} from './harness.mjs';
export const VALID_PRIVATE_KEY = Buffer.alloc(32, 13).toString('base64url'); export {
export const pluginModuleUrl = pathToFileURL(path.resolve('dist/plugins/droid.js')).href; VALID_PRIVATE_KEY,
export const configModuleUrl = pathToFileURL(path.resolve('dist/plugins/droid-config.js')).href; cliPath,
export const contractModuleUrl = pathToFileURL(path.resolve('dist/plugins/droid-contract.js')).href; homeEnvironment as environment,
export const installationModuleUrl = pathToFileURL( registryModuleUrl,
path.resolve('dist/plugins/droid-installation.js'), runNode,
).href; startApiServer,
export const ioModuleUrl = pathToFileURL(path.resolve('dist/plugins/droid-io.js')).href; writeJson as writeConfig,
export const registryModuleUrl = pathToFileURL(path.resolve('dist/plugins/registry.js')).href; } from './harness.mjs';
export const cliPath = path.resolve('dist/cli.js');
export const pluginModuleUrl = distUrl('plugins/droid.js');
export const configModuleUrl = distUrl('plugins/droid-config.js');
export const contractModuleUrl = distUrl('plugins/droid-contract.js');
export const installationModuleUrl = distUrl('plugins/droid-installation.js');
export const ioModuleUrl = distUrl('plugins/droid-io.js');
const PLUGIN = { exportName: 'droidPlugin', moduleUrl: pluginModuleUrl };
export function runProcess(command, args, env, cwd, input = '', shell = false) { export function runProcess(command, args, env, cwd, input = '', shell = false) {
return new Promise((resolve, reject) => { return baseProcess(command, args, env, cwd, input, { shell });
const child = spawn(command, args, {
cwd,
env: { ...process.env, ...env },
shell,
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 function environment(fixture) {
return { HOME: fixture.homeDir, USERPROFILE: fixture.homeDir };
} }
export function installConfig(fixture, overrides = {}) { export function installConfig(fixture, overrides = {}) {
return { return baseInstallConfig('droid', fixture, overrides);
agentName: 'droid',
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,
};
} }
export async function runPlugin(fixture, method, argument) { export function runPlugin(fixture, method, argument) {
const source = ` return basePlugin(PLUGIN, fixture, method, argument, { cwd: fixture.workspaceDir });
import { droidPlugin } from ${JSON.stringify(pluginModuleUrl)};
const argument = JSON.parse(process.env.ELYDORA_TEST_ARGUMENT);
const result = await droidPlugin[process.env.ELYDORA_TEST_METHOD](argument);
if (result !== undefined) console.log(JSON.stringify(result));
`;
return runNode(
['--input-type=module', '--eval', source],
{
...environment(fixture),
ELYDORA_TEST_ARGUMENT: JSON.stringify(argument),
ELYDORA_TEST_METHOD: method,
},
fixture.workspaceDir,
);
} }
export function runHook(command, fixture, input) { export function runHook(command, fixture, input) {
const env = homeEnvironment(fixture);
if (process.platform === 'win32') { if (process.platform === 'win32') {
return runProcess( return runProcess(
'powershell.exe', 'powershell.exe',
['-NoLogo', '-NoProfile', '-NonInteractive', '-Command', command], ['-NoLogo', '-NoProfile', '-NonInteractive', '-Command', command],
environment(fixture), env,
fixture.workspaceDir, fixture.workspaceDir,
input, input,
); );
} }
return runProcess('/bin/sh', ['-c', command], environment(fixture), fixture.workspaceDir, input); return runProcess('/bin/sh', ['-c', command], env, fixture.workspaceDir, input);
}
export async function writeConfig(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 writeConfig(filePath, value);
} }
export async function createFixture({ export async function createFixture({
@@ -111,51 +65,44 @@ export async function createFixture({
projectSettings, projectSettings,
projectLocalSettings, projectLocalSettings,
} = {}) { } = {}) {
const rootDir = await mkdtemp(path.join(os.tmpdir(), 'elydora-droid-')); const base = await createFixtureRoot(
const homeDir = path.join(rootDir, "home with spaces and 'quote %DROID%"); 'elydora-droid-',
const workspaceDir = path.join(rootDir, 'workspace with spaces'); "home with spaces and 'quote %DROID%",
const factoryDir = path.join(homeDir, '.factory'); { agentId, projectName: 'workspace with spaces' },
);
const workspaceDir = base.projectDir;
const factoryDir = path.join(base.homeDir, '.factory');
const rootPath = path.join(factoryDir, 'hooks.json'); const rootPath = path.join(factoryDir, 'hooks.json');
const legacyPath = path.join(factoryDir, 'hooks', 'hooks.json'); const legacyPath = path.join(factoryDir, 'hooks', 'hooks.json');
const settingsPath = path.join(factoryDir, 'settings.json'); const settingsPath = path.join(factoryDir, 'settings.json');
const localSettingsPath = path.join(factoryDir, 'settings.local.json'); const localSettingsPath = path.join(factoryDir, 'settings.local.json');
const agentDir = path.join(homeDir, '.elydora', agentId);
const guardScriptPath = path.join(agentDir, 'guard.js');
const hookScriptPath = path.join(agentDir, 'hook.js');
const projectFactoryDir = path.join(workspaceDir, '.factory'); const projectFactoryDir = path.join(workspaceDir, '.factory');
const projectSettingsPath = path.join(projectFactoryDir, 'settings.json'); const projectSettingsPath = path.join(projectFactoryDir, 'settings.json');
const projectLocalSettingsPath = path.join(projectFactoryDir, 'settings.local.json'); const projectLocalSettingsPath = path.join(projectFactoryDir, 'settings.local.json');
await mkdir(path.join(workspaceDir, '.git'), { recursive: true }); await mkdir(path.join(workspaceDir, '.git'), { recursive: true });
await Promise.all([ await Promise.all([
writeOptional(rootPath, rootConfig), writeOptionalJson(rootPath, rootConfig),
writeOptional(legacyPath, legacyConfig), writeOptionalJson(legacyPath, legacyConfig),
writeOptional(settingsPath, settings), writeOptionalJson(settingsPath, settings),
writeOptional(localSettingsPath, localSettings), writeOptionalJson(localSettingsPath, localSettings),
writeOptional(projectSettingsPath, projectSettings), writeOptionalJson(projectSettingsPath, projectSettings),
writeOptional(projectLocalSettingsPath, projectLocalSettings), writeOptionalJson(projectLocalSettingsPath, projectLocalSettings),
]); ]);
const { projectDir: _projectDir, ...fixture } = base;
return { return {
agentDir, ...fixture,
agentId,
baseUrl, baseUrl,
factoryDir, factoryDir,
guardScriptPath,
homeDir,
hookScriptPath,
legacyPath, legacyPath,
localSettingsPath, localSettingsPath,
projectLocalSettingsPath, projectLocalSettingsPath,
projectSettingsPath, projectSettingsPath,
rootDir,
rootPath, rootPath,
settingsPath, settingsPath,
workspaceDir, workspaceDir,
install(overrides = {}) { install(overrides = {}) {
return runPlugin(this, 'install', installConfig(this, overrides)); return runPlugin(this, 'install', installConfig(this, overrides));
}, },
async close() {
await rm(rootDir, { recursive: true, force: true });
},
}; };
} }
@@ -208,31 +155,3 @@ export async function assertNoTransactionFiles(fixture) {
const leaked = names.filter((name) => /\.(tmp|rollback)$/.test(name)); const leaked = names.filter((name) => /\.(tmp|rollback)$/.test(name));
if (leaked.length > 0) throw new Error(`Leaked transaction files: ${leaked.join(', ')}`); if (leaked.length > 0) throw new Error(`Leaked transaction files: ${leaked.join(', ')}`);
} }
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());
}),
};
}
+41 -153
View File
@@ -1,65 +1,49 @@
import assert from 'node:assert/strict'; import assert from 'node:assert/strict';
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 path from 'node:path';
import { pathToFileURL } from 'node:url'; import {
import { parse } from 'jsonc-parser'; createFixtureRoot,
distUrl,
homeEnvironment,
installConfig as baseInstallConfig,
optionalEnvironment,
runPlugin as basePlugin,
runProcess as baseProcess,
windowsPowerShellPath,
writeOptionalJson,
} from './harness.mjs';
export const VALID_PRIVATE_KEY = Buffer.alloc(32).toString('base64url'); export {
export const pluginModuleUrl = pathToFileURL(path.resolve('dist/plugins/gemini.js')).href; VALID_PRIVATE_KEY,
export const registryModuleUrl = pathToFileURL(path.resolve('dist/plugins/registry.js')).href; cliPath,
export const ioModuleUrl = pathToFileURL(path.resolve('dist/plugins/gemini-io.js')).href; parseJsonc as parseSettings,
export const installationModuleUrl = pathToFileURL( readJsoncSettings as readSettings,
path.resolve('dist/plugins/gemini-installation.js'), registryModuleUrl,
).href; startApiServer,
export const cliPath = path.resolve('dist/cli.js'); writeJson as writeSettings,
} from './harness.mjs';
export const pluginModuleUrl = distUrl('plugins/gemini.js');
export const ioModuleUrl = distUrl('plugins/gemini-io.js');
export const installationModuleUrl = distUrl('plugins/gemini-installation.js');
const PLUGIN = { exportName: 'geminiPlugin', moduleUrl: pluginModuleUrl };
export function runProcess(command, args, env, cwd, input = '', unset = []) { export function runProcess(command, args, env, cwd, input = '', unset = []) {
return new Promise((resolve, reject) => { return baseProcess(command, args, env, cwd, input, { unset });
const childEnv = { ...process.env, ...env };
for (const key of unset) delete childEnv[key];
const child = spawn(command, args, {
cwd,
env: childEnv,
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 = '', unset = []) { export function runNode(args, env, cwd, input = '', unset = []) {
return runProcess(process.execPath, args, env, cwd, input, unset); return runProcess(process.execPath, args, env, cwd, input, unset);
} }
function windowsPowerShell() {
return path.win32.join(
process.env.SystemRoot || 'C:\\Windows',
'System32',
'WindowsPowerShell',
'v1.0',
'powershell.exe',
);
}
export function runGeminiHook(handler, input, fixture, environment = {}) { export function runGeminiHook(handler, input, fixture, environment = {}) {
const env = { const env = {
HOME: fixture.homeDir, ...homeEnvironment(fixture),
USERPROFILE: fixture.homeDir,
GEMINI_CLI_HOME: fixture.geminiCliHomeOverride ?? '', GEMINI_CLI_HOME: fixture.geminiCliHomeOverride ?? '',
...environment, ...environment,
}; };
if (process.platform === 'win32') { if (process.platform === 'win32') {
return runProcess( return runProcess(
windowsPowerShell(), windowsPowerShellPath(),
[ [
'-NoLogo', '-NoLogo',
'-NoProfile', '-NoProfile',
@@ -75,66 +59,16 @@ export function runGeminiHook(handler, input, fixture, environment = {}) {
return runProcess('/bin/bash', ['-c', handler.command], env, fixture.projectDir, input); return runProcess('/bin/bash', ['-c', handler.command], env, fixture.projectDir, input);
} }
export async function writeSettings(filePath, value) {
await mkdir(path.dirname(filePath), { recursive: true });
const contents = typeof value === 'string' ? value : `${JSON.stringify(value, null, 2)}\n`;
await writeFile(filePath, contents, { encoding: 'utf-8', mode: 0o600 });
}
export function parseSettings(raw) {
const errors = [];
const value = parse(raw, errors, {
allowTrailingComma: false,
disallowComments: false,
});
assert.deepEqual(errors, []);
return value;
}
export async function readSettings(filePath) {
const raw = await readFile(filePath, 'utf-8');
return { raw, settings: parseSettings(raw) };
}
export function installConfig(fixture, overrides = {}) { export function installConfig(fixture, overrides = {}) {
return { return baseInstallConfig('gemini', fixture, overrides);
agentName: 'gemini',
orgId: 'org-1',
agentId: 'agent-1',
privateKey: VALID_PRIVATE_KEY,
kid: 'kid-1',
token: 'token-1',
baseUrl: fixture.baseUrl,
guardScriptPath: fixture.guardScriptPath,
hookScriptPath: fixture.hookScriptPath,
...overrides,
};
} }
export async function runPlugin(fixture, method, argument, environment = {}) { export function runPlugin(fixture, method, argument, environment = {}) {
const source = ` const home = optionalEnvironment('GEMINI_CLI_HOME', fixture.geminiCliHomeOverride);
import { geminiPlugin } from ${JSON.stringify(pluginModuleUrl)}; return basePlugin(PLUGIN, fixture, method, argument, {
const argument = JSON.parse(process.env.ELYDORA_TEST_ARGUMENT); env: { ...environment, ...home.env },
const result = await geminiPlugin[process.env.ELYDORA_TEST_METHOD](argument); unset: home.unset,
if (result !== undefined) console.log(JSON.stringify(result)); });
`;
const env = {
HOME: fixture.homeDir,
USERPROFILE: fixture.homeDir,
ELYDORA_TEST_ARGUMENT: JSON.stringify(argument),
ELYDORA_TEST_METHOD: method,
...environment,
};
const unset = [];
if (fixture.geminiCliHomeOverride === undefined) unset.push('GEMINI_CLI_HOME');
else env.GEMINI_CLI_HOME = fixture.geminiCliHomeOverride;
return runNode(
['--input-type=module', '--eval', source],
env,
fixture.projectDir,
'',
unset,
);
} }
export async function createFixture({ export async function createFixture({
@@ -142,38 +76,23 @@ export async function createFixture({
settings, settings,
explicitGeminiHome = true, explicitGeminiHome = true,
} = {}) { } = {}) {
const rootDir = await mkdtemp(path.join(os.tmpdir(), 'elydora-gemini-')); const base = await createFixtureRoot(
const homeDir = path.join( 'elydora-gemini-',
rootDir,
"home with spaces and 'quote $GEMINI_CWD %GEMINI_CWD%", "home with spaces and 'quote $GEMINI_CWD %GEMINI_CWD%",
); );
const projectDir = path.join(rootDir, 'project with spaces'); const geminiCliHome = base.homeDir;
const geminiCliHome = homeDir;
const settingsPath = path.join(geminiCliHome, '.gemini', 'settings.json'); const settingsPath = path.join(geminiCliHome, '.gemini', 'settings.json');
const agentDir = path.join(homeDir, '.elydora', 'agent-1'); await writeOptionalJson(settingsPath, settings);
const guardScriptPath = path.join(agentDir, 'guard.js'); return {
const hookScriptPath = path.join(agentDir, 'hook.js'); ...base,
await mkdir(projectDir, { recursive: true });
if (settings !== undefined) await writeSettings(settingsPath, settings);
const fixture = {
agentDir,
baseUrl, baseUrl,
geminiCliHome, geminiCliHome,
geminiCliHomeOverride: explicitGeminiHome ? geminiCliHome : undefined, geminiCliHomeOverride: explicitGeminiHome ? geminiCliHome : undefined,
guardScriptPath,
homeDir,
hookScriptPath,
projectDir,
rootDir,
settingsPath, settingsPath,
install(overrides = {}, environment = {}) { install(overrides = {}, environment = {}) {
return runPlugin(this, 'install', installConfig(this, overrides), environment); return runPlugin(this, 'install', installConfig(this, overrides), environment);
}, },
async close() {
await rm(rootDir, { recursive: true, force: true });
},
}; };
return fixture;
} }
export function managedHandler(settings, event, name) { export function managedHandler(settings, event, name) {
@@ -202,34 +121,3 @@ export function assertManagedHandler(handler, name) {
export function legacyHandler(scriptPath) { export function legacyHandler(scriptPath) {
return { type: 'command', command: `node "${scriptPath}"` }; return { type: 'command', command: `node "${scriptPath}"` };
} }
export async function startApiServer({ status = 'active', operationStatus = 201 } = {}) {
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({ headers: request.headers, method: request.method, url: request.url, raw });
if (request.method === 'GET') {
response.writeHead(200, { 'Content-Type': 'application/json' });
response.end(JSON.stringify({ agent: { status } }));
return;
}
response.writeHead(operationStatus, { 'Content-Type': 'application/json' });
response.end(operationStatus >= 200 && operationStatus < 300
? JSON.stringify({ operation: { accepted: true } })
: JSON.stringify({ error: { code: 'UPSTREAM_FAILURE', message: 'failed' } }));
});
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());
}),
};
}
+39 -139
View File
@@ -1,39 +1,27 @@
import assert from 'node:assert/strict'; import assert from 'node:assert/strict';
import { spawn } from 'node:child_process'; import { mkdir, readFile, writeFile } from 'node:fs/promises';
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 path from 'node:path';
import { pathToFileURL } from 'node:url'; import {
createFixtureRoot,
distUrl,
homeEnvironment,
installConfig as baseInstallConfig,
optionalEnvironment,
runPlugin as basePlugin,
runProcess as baseProcess,
runShell,
} from './harness.mjs';
export const VALID_PRIVATE_KEY = Buffer.alloc(32).toString('base64url'); export { VALID_PRIVATE_KEY, cliPath, registryModuleUrl, startApiServer } from './harness.mjs';
export const pluginModuleUrl = pathToFileURL(path.resolve('dist/plugins/grok.js')).href;
export const registryModuleUrl = pathToFileURL(path.resolve('dist/plugins/registry.js')).href; export const pluginModuleUrl = distUrl('plugins/grok.js');
export const contractModuleUrl = pathToFileURL(path.resolve('dist/plugins/grok-contract.js')).href; export const contractModuleUrl = distUrl('plugins/grok-contract.js');
export const ioModuleUrl = pathToFileURL(path.resolve('dist/plugins/grok-io.js')).href; export const ioModuleUrl = distUrl('plugins/grok-io.js');
export const installationModuleUrl = pathToFileURL( export const installationModuleUrl = distUrl('plugins/grok-installation.js');
path.resolve('dist/plugins/grok-installation.js'), const PLUGIN = { exportName: 'grokPlugin', moduleUrl: pluginModuleUrl };
).href;
export const cliPath = path.resolve('dist/cli.js');
export function runProcess(command, args, env, cwd, input = '', unset = []) { export function runProcess(command, args, env, cwd, input = '', unset = []) {
return new Promise((resolve, reject) => { return baseProcess(command, args, env, cwd, input, { unset });
const childEnv = { ...process.env, ...env };
for (const key of unset) delete childEnv[key];
const child = spawn(command, args, {
cwd,
env: childEnv,
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 = '', unset = []) { export function runNode(args, env, cwd, input = '', unset = []) {
@@ -41,68 +29,23 @@ export function runNode(args, env, cwd, input = '', unset = []) {
} }
export function runGrokHook(command, input, fixture, environment = {}) { export function runGrokHook(command, input, fixture, environment = {}) {
return new Promise((resolve, reject) => { return runShell(command, {
const child = spawn(command, { ...homeEnvironment(fixture),
shell: true, GROK_HOME: fixture.grokHomeOverride ?? '',
cwd: fixture.projectDir, ...environment,
env: { }, fixture.projectDir, input);
...process.env,
HOME: fixture.homeDir,
USERPROFILE: fixture.homeDir,
GROK_HOME: fixture.grokHomeOverride ?? '',
...environment,
},
stdio: ['pipe', 'pipe', 'pipe'],
});
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 installConfig(fixture, overrides = {}) { export function installConfig(fixture, overrides = {}) {
return { return baseInstallConfig('grok', fixture, overrides);
agentName: 'grok',
orgId: 'org-1',
agentId: 'agent-1',
privateKey: VALID_PRIVATE_KEY,
kid: 'kid-1',
token: 'token-1',
baseUrl: fixture.baseUrl,
guardScriptPath: fixture.guardScriptPath,
hookScriptPath: fixture.hookScriptPath,
...overrides,
};
} }
export async function runPlugin(fixture, method, argument, environment = {}) { export function runPlugin(fixture, method, argument, environment = {}) {
const source = ` const home = optionalEnvironment('GROK_HOME', fixture.grokHomeOverride);
import { grokPlugin } from ${JSON.stringify(pluginModuleUrl)}; return basePlugin(PLUGIN, fixture, method, argument, {
const argument = JSON.parse(process.env.ELYDORA_TEST_ARGUMENT); env: { ...environment, ...home.env },
const result = await grokPlugin[process.env.ELYDORA_TEST_METHOD](argument); unset: home.unset,
if (result !== undefined) console.log(JSON.stringify(result)); });
`;
const env = {
HOME: fixture.homeDir,
USERPROFILE: fixture.homeDir,
ELYDORA_TEST_ARGUMENT: JSON.stringify(argument),
ELYDORA_TEST_METHOD: method,
...environment,
};
const unset = [];
if (fixture.grokHomeOverride === undefined) unset.push('GROK_HOME');
else env.GROK_HOME = fixture.grokHomeOverride;
return runNode(
['--input-type=module', '--eval', source],
env,
fixture.projectDir,
'',
unset,
);
} }
async function writeOptional(filePath, contents) { async function writeOptional(filePath, contents) {
@@ -116,37 +59,25 @@ export async function createFixture({
config, config,
explicitGrokHome = true, explicitGrokHome = true,
} = {}) { } = {}) {
const rootDir = await mkdtemp(path.join(os.tmpdir(), 'elydora-grok-')); const base = await createFixtureRoot(
const homeDir = path.join(rootDir, "home with spaces and 'quote %GROK_HOOK_EVENT%"); 'elydora-grok-',
const projectDir = path.join(rootDir, 'project with spaces'); "home with spaces and 'quote %GROK_HOOK_EVENT%",
);
const grokHome = explicitGrokHome const grokHome = explicitGrokHome
? path.join(homeDir, 'custom grok') ? path.join(base.homeDir, 'custom grok')
: path.join(homeDir, '.grok'); : path.join(base.homeDir, '.grok');
const configPath = path.join(grokHome, 'hooks', 'elydora-audit.json'); const configPath = path.join(grokHome, 'hooks', 'elydora-audit.json');
const agentDir = path.join(homeDir, '.elydora', 'agent-1');
const guardScriptPath = path.join(agentDir, 'guard.js');
const hookScriptPath = path.join(agentDir, 'hook.js');
await mkdir(projectDir, { recursive: true });
await writeOptional(configPath, config); await writeOptional(configPath, config);
const fixture = { return {
agentDir, ...base,
baseUrl, baseUrl,
configPath, configPath,
grokHome, grokHome,
grokHomeOverride: explicitGrokHome ? grokHome : undefined, grokHomeOverride: explicitGrokHome ? grokHome : undefined,
guardScriptPath,
homeDir,
hookScriptPath,
projectDir,
rootDir,
install(overrides = {}) { install(overrides = {}) {
return runPlugin(this, 'install', installConfig(this, overrides)); return runPlugin(this, 'install', installConfig(this, overrides));
}, },
async close() {
await rm(rootDir, { recursive: true, force: true });
},
}; };
return fixture;
} }
export async function readGrokConfig(filePath) { export async function readGrokConfig(filePath) {
@@ -184,34 +115,3 @@ export function legacyCommand(scriptPath) {
const quote = (value) => `'${value.replaceAll("'", `'"'"'`)}'`; const quote = (value) => `'${value.replaceAll("'", `'"'"'`)}'`;
return `${quote(process.execPath)} ${quote(scriptPath)}`; return `${quote(process.execPath)} ${quote(scriptPath)}`;
} }
export async function startApiServer({ status = 'active', operationStatus = 201 } = {}) {
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({ headers: request.headers, method: request.method, url: request.url, raw });
if (request.method === 'GET') {
response.writeHead(200, { 'Content-Type': 'application/json' });
response.end(JSON.stringify({ agent: { status } }));
return;
}
response.writeHead(operationStatus, { 'Content-Type': 'application/json' });
response.end(operationStatus >= 200 && operationStatus < 300
? JSON.stringify({ operation: { accepted: true } })
: JSON.stringify({ error: { code: 'UPSTREAM_FAILURE', message: 'failed' } }));
});
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());
}),
};
}
+202
View File
@@ -0,0 +1,202 @@
import assert from 'node:assert/strict';
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 { parse } from 'jsonc-parser';
export const VALID_PRIVATE_KEY = Buffer.alloc(32).toString('base64url');
export const cliPath = path.resolve('dist/cli.js');
export function distUrl(relativePath) {
return pathToFileURL(path.resolve('dist', relativePath)).href;
}
export const registryModuleUrl = distUrl('plugins/registry.js');
export const managedInstallationModuleUrl = distUrl('plugins/managed-installation.js');
export function runProcess(command, args, env, cwd, input = '', { unset = [], shell = false } = {}) {
return new Promise((resolve, reject) => {
const childEnvironment = { ...process.env, ...env };
for (const [name, value] of Object.entries(env)) {
if (value === undefined) delete childEnvironment[name];
}
for (const name of unset) delete childEnvironment[name];
const child = spawn(command, args, {
cwd,
env: childEnvironment,
shell,
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 = '', options = {}) {
return runProcess(process.execPath, args, env, cwd, input, options);
}
export function runShell(command, env, cwd, input = '') {
return runProcess(command, [], env, cwd, input, { shell: true });
}
export function windowsPowerShellPath() {
return path.win32.join(
process.env.SystemRoot || 'C:\\Windows',
'System32',
'WindowsPowerShell',
'v1.0',
'powershell.exe',
);
}
export function homeEnvironment(fixture) {
return { HOME: fixture.homeDir, USERPROFILE: fixture.homeDir };
}
// Sets NAME when a value is configured, otherwise removes it from the child environment.
export function optionalEnvironment(name, value) {
return value === undefined ? { env: {}, unset: [name] } : { env: { [name]: value }, unset: [] };
}
export function installConfig(agentName, fixture, overrides = {}) {
return {
agentName,
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,
};
}
export function runPlugin(
plugin,
fixture,
method,
argument,
{ env = {}, unset = [], cwd = fixture.projectDir } = {},
) {
const source = `
import { ${plugin.exportName} } from ${JSON.stringify(plugin.moduleUrl)};
const argument = JSON.parse(process.env.ELYDORA_TEST_ARGUMENT);
const result = await ${plugin.exportName}[process.env.ELYDORA_TEST_METHOD](argument);
if (result !== undefined) console.log(JSON.stringify(result));
`;
return runNode(
['--input-type=module', '--eval', source],
{
...homeEnvironment(fixture),
ELYDORA_TEST_ARGUMENT: JSON.stringify(argument),
ELYDORA_TEST_METHOD: method,
...env,
},
cwd,
'',
{ unset },
);
}
export async function createFixtureRoot(
prefix,
homeName,
{ agentId = 'agent-1', projectName = 'project with spaces', createHome = false } = {},
) {
const rootDir = await mkdtemp(path.join(os.tmpdir(), prefix));
const homeDir = path.join(rootDir, homeName);
const projectDir = path.join(rootDir, projectName);
const agentDir = path.join(homeDir, '.elydora', agentId);
if (createHome) await mkdir(homeDir, { recursive: true });
await mkdir(projectDir, { recursive: true });
return {
agentDir,
agentId,
guardScriptPath: path.join(agentDir, 'guard.js'),
homeDir,
hookScriptPath: path.join(agentDir, 'hook.js'),
projectDir,
rootDir,
async close() {
await rm(rootDir, { recursive: true, force: true });
},
};
}
export async function writeJson(filePath, value) {
await mkdir(path.dirname(filePath), { recursive: true });
const contents = typeof value === 'string' ? value : `${JSON.stringify(value, null, 2)}\n`;
await writeFile(filePath, contents, { encoding: 'utf-8', mode: 0o600 });
}
export async function writeOptionalJson(filePath, value) {
if (value !== undefined) await writeJson(filePath, value);
}
export async function readJson(filePath) {
return JSON.parse(await readFile(filePath, 'utf-8'));
}
export async function readSettings(filePath) {
const raw = await readFile(filePath, 'utf-8');
return { raw, settings: JSON.parse(raw) };
}
export function parseJsonc(raw) {
const errors = [];
const value = parse(raw, errors, { allowTrailingComma: false, disallowComments: false });
assert.deepEqual(errors, []);
return value;
}
export async function readJsoncSettings(filePath) {
const raw = await readFile(filePath, 'utf-8');
return { raw, settings: parseJsonc(raw) };
}
export async function startApiServer({ status: initialStatus = 'active', operationStatus: initialOperationStatus = 201 } = {}) {
const requests = [];
let status = initialStatus;
let operationStatus = initialOperationStatus;
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({ headers: request.headers, method: request.method, url: request.url, raw });
if (request.method === 'GET') {
response.writeHead(200, { 'Content-Type': 'application/json' });
response.end(JSON.stringify({ agent: { status } }));
return;
}
response.writeHead(operationStatus, { 'Content-Type': 'application/json' });
response.end(operationStatus >= 200 && operationStatus < 300
? JSON.stringify({ operation: { accepted: true } })
: JSON.stringify({ error: { code: 'UPSTREAM_FAILURE', message: 'failed' } }));
});
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,
setStatus(value) { status = value; },
setOperationStatus(value) { operationStatus = value; },
close: () => new Promise((resolve, reject) => {
server.close((error) => error ? reject(error) : resolve());
}),
};
}
+43 -148
View File
@@ -1,104 +1,48 @@
import assert from 'node:assert/strict'; import assert from 'node:assert/strict';
import { spawn } from 'node:child_process'; import { mkdir, readFile, writeFile } from 'node:fs/promises';
import { mkdir, mkdtemp, rm, writeFile } from 'node:fs/promises';
import http from 'node:http';
import os from 'node:os';
import path from 'node:path'; import path from 'node:path';
import { pathToFileURL } from 'node:url';
import { parse as parseToml } from '@decimalturn/toml-patch'; import { parse as parseToml } from '@decimalturn/toml-patch';
import {
createFixtureRoot,
distUrl,
homeEnvironment,
installConfig as baseInstallConfig,
optionalEnvironment,
runNode as baseNode,
runPlugin as basePlugin,
runShell,
} from './harness.mjs';
export const VALID_PRIVATE_KEY = Buffer.alloc(32).toString('base64url'); export { VALID_PRIVATE_KEY, cliPath, registryModuleUrl, startApiServer } from './harness.mjs';
export const pluginModuleUrl = pathToFileURL(path.resolve('dist/plugins/kimi.js')).href;
export const registryModuleUrl = pathToFileURL(path.resolve('dist/plugins/registry.js')).href; export const pluginModuleUrl = distUrl('plugins/kimi.js');
export const contractModuleUrl = pathToFileURL(path.resolve('dist/plugins/kimi-contract.js')).href; export const contractModuleUrl = distUrl('plugins/kimi-contract.js');
export const ioModuleUrl = pathToFileURL(path.resolve('dist/plugins/kimi-io.js')).href; export const ioModuleUrl = distUrl('plugins/kimi-io.js');
export const installationModuleUrl = pathToFileURL( export const installationModuleUrl = distUrl('plugins/kimi-installation.js');
path.resolve('dist/plugins/kimi-installation.js'), const PLUGIN = { exportName: 'kimiPlugin', moduleUrl: pluginModuleUrl };
).href;
export const cliPath = path.resolve('dist/cli.js');
export function runNode(args, env, cwd, input = '', unset = []) { export function runNode(args, env, cwd, input = '', unset = []) {
return new Promise((resolve, reject) => { return baseNode(args, env, cwd, input, { unset });
const childEnv = { ...process.env, ...env };
for (const key of unset) delete childEnv[key];
const child = spawn(process.execPath, args, {
cwd,
env: childEnv,
stdio: ['pipe', 'pipe', 'pipe'],
});
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 runKimiHook(command, input, fixture, environment = {}) { export function runKimiHook(command, input, fixture, environment = {}) {
return new Promise((resolve, reject) => { return runShell(command, {
const child = spawn(command, { ...homeEnvironment(fixture),
shell: true, KIMI_CODE_HOME: fixture.kimiHomeOverride ?? '',
cwd: fixture.projectDir, ...environment,
env: { }, fixture.projectDir, input);
...process.env,
HOME: fixture.homeDir,
USERPROFILE: fixture.homeDir,
KIMI_CODE_HOME: fixture.kimiHomeOverride ?? '',
...environment,
},
stdio: ['pipe', 'pipe', 'pipe'],
});
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 installConfig(fixture, overrides = {}) { export function installConfig(fixture, overrides = {}) {
return { return baseInstallConfig('kimi', fixture, overrides);
agentName: 'kimi',
orgId: 'org-1',
agentId: 'agent-1',
privateKey: VALID_PRIVATE_KEY,
kid: 'kid-1',
token: 'token-1',
baseUrl: fixture.baseUrl,
guardScriptPath: fixture.guardScriptPath,
hookScriptPath: fixture.hookScriptPath,
...overrides,
};
} }
export async function runPlugin(fixture, method, argument, environment = {}) { export function runPlugin(fixture, method, argument, environment = {}) {
const source = ` const home = optionalEnvironment('KIMI_CODE_HOME', fixture.kimiHomeOverride);
import { kimiPlugin } from ${JSON.stringify(pluginModuleUrl)}; return basePlugin(PLUGIN, fixture, method, argument, {
const argument = JSON.parse(process.env.ELYDORA_TEST_ARGUMENT); env: { ...environment, ...home.env },
const result = await kimiPlugin[process.env.ELYDORA_TEST_METHOD](argument); unset: home.unset,
if (result !== undefined) console.log(JSON.stringify(result)); });
`;
const env = {
HOME: fixture.homeDir,
USERPROFILE: fixture.homeDir,
ELYDORA_TEST_ARGUMENT: JSON.stringify(argument),
ELYDORA_TEST_METHOD: method,
...environment,
};
const unset = [];
if (fixture.kimiHomeOverride === undefined) unset.push('KIMI_CODE_HOME');
else env.KIMI_CODE_HOME = fixture.kimiHomeOverride;
return runNode(
['--input-type=module', '--eval', source],
env,
fixture.projectDir,
'',
unset,
);
} }
async function writeOptional(filePath, contents) { async function writeOptional(filePath, contents) {
@@ -115,50 +59,37 @@ export async function createFixture({
legacyDetected = true, legacyDetected = true,
explicitKimiHome = true, explicitKimiHome = true,
} = {}) { } = {}) {
const rootDir = await mkdtemp(path.join(os.tmpdir(), 'elydora-kimi-')); const base = await createFixtureRoot(
const homeDir = path.join(rootDir, "home with spaces and 'quote %ELYDORA_HOOK_PATH%"); 'elydora-kimi-',
const projectDir = path.join(rootDir, 'project with spaces'); "home with spaces and 'quote %ELYDORA_HOOK_PATH%",
);
const kimiHome = explicitKimiHome const kimiHome = explicitKimiHome
? path.join(homeDir, 'custom kimi-code') ? path.join(base.homeDir, 'custom kimi-code')
: path.join(homeDir, '.kimi-code'); : path.join(base.homeDir, '.kimi-code');
const stablePath = path.join(kimiHome, 'config.toml'); const stablePath = path.join(kimiHome, 'config.toml');
const legacyHome = path.join(homeDir, '.kimi'); const legacyHome = path.join(base.homeDir, '.kimi');
const legacyPath = path.join(legacyHome, 'config.toml'); const legacyPath = path.join(legacyHome, 'config.toml');
const agentDir = path.join(homeDir, '.elydora', 'agent-1');
const guardScriptPath = path.join(agentDir, 'guard.js');
const hookScriptPath = path.join(agentDir, 'hook.js');
await mkdir(projectDir, { recursive: true });
if (stableDetected && !explicitKimiHome) await mkdir(kimiHome, { recursive: true }); if (stableDetected && !explicitKimiHome) await mkdir(kimiHome, { recursive: true });
if (legacyDetected) await mkdir(legacyHome, { recursive: true }); if (legacyDetected) await mkdir(legacyHome, { recursive: true });
await writeOptional(stablePath, stableConfig); await writeOptional(stablePath, stableConfig);
await writeOptional(legacyPath, legacyConfig); await writeOptional(legacyPath, legacyConfig);
const fixture = { return {
agentDir, ...base,
baseUrl, baseUrl,
guardScriptPath,
homeDir,
hookScriptPath,
kimiHome, kimiHome,
kimiHomeOverride: explicitKimiHome ? kimiHome : undefined, kimiHomeOverride: explicitKimiHome ? kimiHome : undefined,
legacyHome, legacyHome,
legacyPath, legacyPath,
projectDir,
rootDir,
stablePath, stablePath,
install(overrides = {}) { install(overrides = {}) {
return runPlugin(this, 'install', installConfig(this, overrides)); return runPlugin(this, 'install', installConfig(this, overrides));
}, },
async close() {
await rm(rootDir, { recursive: true, force: true });
},
}; };
return fixture;
} }
export function readKimiConfig(filePath) { export async function readKimiConfig(filePath) {
return import('node:fs/promises') const raw = await readFile(filePath, 'utf-8');
.then(({ readFile }) => readFile(filePath, 'utf-8')) return { raw, config: parseToml(raw) };
.then((raw) => ({ raw, config: parseToml(raw) }));
} }
export function managedHook(config, event) { export function managedHook(config, event) {
@@ -182,39 +113,3 @@ export function legacyCommand(scriptPath) {
const quote = (value) => `'${value.replaceAll("'", `'"'"'`)}'`; const quote = (value) => `'${value.replaceAll("'", `'"'"'`)}'`;
return `${quote(process.execPath)} ${quote(scriptPath)}`; return `${quote(process.execPath)} ${quote(scriptPath)}`;
} }
export async function startApiServer({ status = 'active', operationStatus = 201 } = {}) {
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({
headers: request.headers,
method: request.method,
url: request.url,
raw,
});
if (request.method === 'GET') {
response.writeHead(200, { 'Content-Type': 'application/json' });
response.end(JSON.stringify({ agent: { status } }));
return;
}
response.writeHead(operationStatus, { 'Content-Type': 'application/json' });
response.end(operationStatus >= 200 && operationStatus < 300
? JSON.stringify({ operation: { accepted: true } })
: JSON.stringify({ error: { code: 'UPSTREAM_FAILURE', message: 'failed' } }));
});
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());
}),
};
}
+39 -141
View File
@@ -1,98 +1,41 @@
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 path from 'node:path';
import { pathToFileURL } from 'node:url'; import {
createFixtureRoot,
distUrl,
installConfig as baseInstallConfig,
runPlugin as basePlugin,
runProcess as baseProcess,
writeOptionalJson,
} from './harness.mjs';
export const VALID_PRIVATE_KEY = Buffer.alloc(32, 17).toString('base64url'); export {
export const pluginModuleUrl = pathToFileURL(path.resolve('dist/plugins/kirocli.js')).href; VALID_PRIVATE_KEY,
export const contractModuleUrl = pathToFileURL( cliPath,
path.resolve('dist/plugins/kirocli-contract.js'), homeEnvironment as environment,
).href; readJson,
export const installationModuleUrl = pathToFileURL( registryModuleUrl,
path.resolve('dist/plugins/kirocli-installation.js'), runNode,
).href; runShell,
export const ioModuleUrl = pathToFileURL(path.resolve('dist/plugins/kirocli-io.js')).href; startApiServer,
export const kiroV1ModuleUrl = pathToFileURL( } from './harness.mjs';
path.resolve('dist/plugins/kiroide-contract.js'),
).href; export const pluginModuleUrl = distUrl('plugins/kirocli.js');
export const registryModuleUrl = pathToFileURL(path.resolve('dist/plugins/registry.js')).href; export const contractModuleUrl = distUrl('plugins/kirocli-contract.js');
export const cliPath = path.resolve('dist/cli.js'); export const installationModuleUrl = distUrl('plugins/kirocli-installation.js');
export const ioModuleUrl = distUrl('plugins/kirocli-io.js');
export const kiroV1ModuleUrl = distUrl('plugins/kiroide-contract.js');
const PLUGIN = { exportName: 'kirocliPlugin', moduleUrl: pluginModuleUrl };
export function runProcess(command, args, env, cwd, input = '', shell = false) { export function runProcess(command, args, env, cwd, input = '', shell = false) {
return new Promise((resolve, reject) => { return baseProcess(command, args, env, cwd, input, { shell });
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,
shell,
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 function runShell(command, env, cwd, input = '') {
return runProcess(command, [], env, cwd, input, true);
}
export function environment(fixture) {
return { HOME: fixture.homeDir, USERPROFILE: fixture.homeDir };
} }
export function installConfig(fixture, overrides = {}) { export function installConfig(fixture, overrides = {}) {
return { return baseInstallConfig('kirocli', fixture, overrides);
agentName: 'kirocli',
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,
};
} }
export async function runPlugin(fixture, method, argument) { export function runPlugin(fixture, method, argument) {
const source = ` return basePlugin(PLUGIN, fixture, method, argument);
import { kirocliPlugin } from ${JSON.stringify(pluginModuleUrl)};
const argument = JSON.parse(process.env.ELYDORA_TEST_ARGUMENT);
const result = await kirocliPlugin[process.env.ELYDORA_TEST_METHOD](argument);
if (result !== undefined) console.log(JSON.stringify(result));
`;
return runNode(
['--input-type=module', '--eval', source],
{
...environment(fixture),
ELYDORA_TEST_ARGUMENT: JSON.stringify(argument),
ELYDORA_TEST_METHOD: method,
},
fixture.projectDir,
);
}
async function writeOptional(filePath, value) {
if (value === undefined) return;
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 });
} }
export function managedV2Document(overrides = {}) { export function managedV2Document(overrides = {}) {
@@ -112,71 +55,26 @@ export async function createFixture({
existingV2, existingV2,
existingV3, existingV3,
} = {}) { } = {}) {
const rootDir = await mkdtemp(path.join(os.tmpdir(), 'elydora-kirocli-')); const base = await createFixtureRoot(
const homeDir = path.join(rootDir, "home with spaces and 'quote %KIRO%"); 'elydora-kirocli-',
const projectDir = path.join(rootDir, 'workspace with spaces'); "home with spaces and 'quote %KIRO%",
const v2Path = path.join(homeDir, '.kiro', 'agents', 'elydora-audit.json'); { agentId, projectName: 'workspace with spaces', createHome: true },
const v3Path = path.join(homeDir, '.kiro', 'hooks', 'elydora-audit.json'); );
const agentDir = path.join(homeDir, '.elydora', agentId); const v2Path = path.join(base.homeDir, '.kiro', 'agents', 'elydora-audit.json');
const guardScriptPath = path.join(agentDir, 'guard.js'); const v3Path = path.join(base.homeDir, '.kiro', 'hooks', 'elydora-audit.json');
const hookScriptPath = path.join(agentDir, 'hook.js'); await writeOptionalJson(v2Path, existingV2);
await mkdir(homeDir, { recursive: true }); await writeOptionalJson(v3Path, existingV3);
await mkdir(projectDir, { recursive: true });
await writeOptional(v2Path, existingV2);
await writeOptional(v3Path, existingV3);
return { return {
agentDir, ...base,
agentId,
baseUrl, baseUrl,
guardScriptPath,
homeDir,
hookScriptPath,
projectDir,
rootDir,
v2Path, v2Path,
v3Path, v3Path,
install(overrides = {}) { install(overrides = {}) {
return runPlugin(this, 'install', installConfig(this, overrides)); return runPlugin(this, 'install', installConfig(this, overrides));
}, },
async close() {
await rm(rootDir, { recursive: true, force: true });
},
}; };
} }
export async function readJson(filePath) {
return JSON.parse(await readFile(filePath, 'utf-8'));
}
export function findV3Hook(document, name) { export function findV3Hook(document, name) {
return document.hooks.find((hook) => hook.name === name); return document.hooks.find((hook) => hook.name === name);
} }
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 });
if (request.method === 'GET') {
response.writeHead(200, { 'Content-Type': 'application/json' });
response.end('{"agent":{"status":"active"}}');
return;
}
response.writeHead(201, { 'Content-Type': 'application/json' });
response.end('{"operation":{"accepted":true}}');
});
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());
}),
};
}
+39 -139
View File
@@ -1,95 +1,40 @@
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 path from 'node:path';
import { pathToFileURL } from 'node:url'; import {
createFixtureRoot,
distUrl,
installConfig as baseInstallConfig,
runPlugin as basePlugin,
runProcess as baseProcess,
writeOptionalJson,
} from './harness.mjs';
export const VALID_PRIVATE_KEY = Buffer.alloc(32, 11).toString('base64url'); export {
export const pluginModuleUrl = pathToFileURL(path.resolve('dist/plugins/kiroide.js')).href; VALID_PRIVATE_KEY,
export const contractModuleUrl = pathToFileURL( cliPath,
path.resolve('dist/plugins/kiroide-contract.js'), homeEnvironment as environment,
).href; readJson,
export const installationModuleUrl = pathToFileURL( registryModuleUrl,
path.resolve('dist/plugins/kiroide-installation.js'), runNode,
).href; runShell,
export const ioModuleUrl = pathToFileURL(path.resolve('dist/plugins/kiroide-io.js')).href; startApiServer,
export const registryModuleUrl = pathToFileURL(path.resolve('dist/plugins/registry.js')).href; } from './harness.mjs';
export const cliPath = path.resolve('dist/cli.js');
export const pluginModuleUrl = distUrl('plugins/kiroide.js');
export const contractModuleUrl = distUrl('plugins/kiroide-contract.js');
export const installationModuleUrl = distUrl('plugins/kiroide-installation.js');
export const ioModuleUrl = distUrl('plugins/kiroide-io.js');
const PLUGIN = { exportName: 'kiroidePlugin', moduleUrl: pluginModuleUrl };
export function runProcess(command, args, env, cwd, input = '', shell = false) { export function runProcess(command, args, env, cwd, input = '', shell = false) {
return new Promise((resolve, reject) => { return baseProcess(command, args, env, cwd, input, { shell });
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,
shell,
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 function runShell(command, env, cwd, input = '') {
return runProcess(command, [], env, cwd, input, true);
}
export function environment(fixture) {
return { HOME: fixture.homeDir, USERPROFILE: fixture.homeDir };
} }
export function installConfig(fixture, overrides = {}) { export function installConfig(fixture, overrides = {}) {
return { return baseInstallConfig('kiroide', fixture, overrides);
agentName: 'kiroide',
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,
};
} }
export async function runPlugin(fixture, method, argument) { export function runPlugin(fixture, method, argument) {
const source = ` return basePlugin(PLUGIN, fixture, method, argument);
import { kiroidePlugin } from ${JSON.stringify(pluginModuleUrl)};
const argument = JSON.parse(process.env.ELYDORA_TEST_ARGUMENT);
const result = await kiroidePlugin[process.env.ELYDORA_TEST_METHOD](argument);
if (result !== undefined) console.log(JSON.stringify(result));
`;
return runNode(
['--input-type=module', '--eval', source],
{
...environment(fixture),
ELYDORA_TEST_ARGUMENT: JSON.stringify(argument),
ELYDORA_TEST_METHOD: method,
},
fixture.projectDir,
);
}
async function writeOptional(filePath, value) {
if (value === undefined) return;
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 });
} }
export async function createFixture({ export async function createFixture({
@@ -98,73 +43,28 @@ export async function createFixture({
existingConfig, existingConfig,
existingLegacy, existingLegacy,
} = {}) { } = {}) {
const rootDir = await mkdtemp(path.join(os.tmpdir(), 'elydora-kiroide-')); const base = await createFixtureRoot(
const homeDir = path.join(rootDir, "home with spaces and 'quote %KIRO%"); 'elydora-kiroide-',
const projectDir = path.join(rootDir, 'workspace with spaces'); "home with spaces and 'quote %KIRO%",
const hooksDir = path.join(projectDir, '.kiro', 'hooks'); { agentId, projectName: 'workspace with spaces' },
);
const hooksDir = path.join(base.projectDir, '.kiro', 'hooks');
const configPath = path.join(hooksDir, 'elydora-audit.json'); const configPath = path.join(hooksDir, 'elydora-audit.json');
const legacyConfigPath = path.join(homeDir, '.kiro', 'hooks', 'elydora-audit.kiro.hook'); const legacyConfigPath = path.join(base.homeDir, '.kiro', 'hooks', 'elydora-audit.kiro.hook');
const agentDir = path.join(homeDir, '.elydora', agentId); await writeOptionalJson(configPath, existingConfig);
const guardScriptPath = path.join(agentDir, 'guard.js'); await writeOptionalJson(legacyConfigPath, existingLegacy);
const hookScriptPath = path.join(agentDir, 'hook.js'); return {
await mkdir(projectDir, { recursive: true }); ...base,
await writeOptional(configPath, existingConfig);
await writeOptional(legacyConfigPath, existingLegacy);
const fixture = {
agentDir,
agentId,
baseUrl, baseUrl,
configPath, configPath,
guardScriptPath,
homeDir,
hookScriptPath,
hooksDir, hooksDir,
legacyConfigPath, legacyConfigPath,
projectDir,
rootDir,
install(overrides = {}) { install(overrides = {}) {
return runPlugin(this, 'install', installConfig(this, overrides)); return runPlugin(this, 'install', installConfig(this, overrides));
}, },
async close() {
await rm(rootDir, { recursive: true, force: true });
},
}; };
return fixture;
}
export async function readJson(filePath) {
return JSON.parse(await readFile(filePath, 'utf-8'));
} }
export function findHook(document, name) { export function findHook(document, name) {
return document.hooks.find((hook) => hook.name === name); return document.hooks.find((hook) => hook.name === name);
} }
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 });
if (request.method === 'GET') {
response.writeHead(200, { 'Content-Type': 'application/json' });
response.end('{"agent":{"status":"active"}}');
return;
}
response.writeHead(201, { 'Content-Type': 'application/json' });
response.end('{"operation":{"accepted":true}}');
});
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());
}),
};
}
+41 -146
View File
@@ -1,92 +1,38 @@
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 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, 23).toString('base64url'); export {
export const pluginModuleUrl = pathToFileURL(path.resolve('dist/plugins/letta.js')).href; VALID_PRIVATE_KEY,
export const configModuleUrl = pathToFileURL(path.resolve('dist/plugins/letta-config.js')).href; cliPath,
export const contractModuleUrl = pathToFileURL(path.resolve('dist/plugins/letta-contract.js')).href; homeEnvironment as environment,
export const installationModuleUrl = pathToFileURL( readJson as readSettings,
path.resolve('dist/plugins/letta-installation.js'), registryModuleUrl,
).href; runNode,
export const sourcesModuleUrl = pathToFileURL(path.resolve('dist/plugins/letta-sources.js')).href; runProcess,
export const registryModuleUrl = pathToFileURL(path.resolve('dist/plugins/registry.js')).href; startApiServer,
export const cliPath = path.resolve('dist/cli.js'); } from './harness.mjs';
export function runProcess(command, args, env, cwd, input = '') { export const pluginModuleUrl = distUrl('plugins/letta.js');
return new Promise((resolve, reject) => { export const configModuleUrl = distUrl('plugins/letta-config.js');
const childEnvironment = { ...process.env, ...env }; export const contractModuleUrl = distUrl('plugins/letta-contract.js');
for (const [name, value] of Object.entries(env)) { export const installationModuleUrl = distUrl('plugins/letta-installation.js');
if (value === undefined) delete childEnvironment[name]; export const sourcesModuleUrl = distUrl('plugins/letta-sources.js');
} const PLUGIN = { exportName: 'lettaPlugin', moduleUrl: pluginModuleUrl };
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 function environment(fixture) {
return {
HOME: fixture.homeDir,
USERPROFILE: fixture.homeDir,
};
}
export function installConfig(fixture, overrides = {}) { export function installConfig(fixture, overrides = {}) {
return { return baseInstallConfig('letta', fixture, overrides);
agentName: 'letta',
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,
};
} }
export async function runPlugin(fixture, method, argument) { export function runPlugin(fixture, method, argument) {
const source = ` return basePlugin(PLUGIN, fixture, method, argument);
import { lettaPlugin } from ${JSON.stringify(pluginModuleUrl)};
const argument = JSON.parse(process.env.ELYDORA_TEST_ARGUMENT);
const result = await lettaPlugin[process.env.ELYDORA_TEST_METHOD](argument);
if (result !== undefined) console.log(JSON.stringify(result));
`;
return runNode(
['--input-type=module', '--eval', source],
{
...environment(fixture),
ELYDORA_TEST_ARGUMENT: JSON.stringify(argument),
ELYDORA_TEST_METHOD: method,
},
fixture.projectDir,
);
}
async function writeOptional(filePath, value) {
if (value === undefined) return;
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 });
} }
export async function createFixture({ export async function createFixture({
@@ -96,45 +42,29 @@ export async function createFixture({
projectSettings, projectSettings,
localSettings, localSettings,
} = {}) { } = {}) {
const rootDir = await mkdtemp(path.join(os.tmpdir(), 'elydora-letta-')); const base = await createFixtureRoot(
const homeDir = path.join(rootDir, "home with spaces and 'quote %HOME%"); 'elydora-letta-',
const projectDir = path.join(rootDir, 'workspace with spaces'); "home with spaces and 'quote %HOME%",
const globalPath = path.join(homeDir, '.letta', 'settings.json'); { agentId, projectName: 'workspace with spaces', createHome: true },
const projectPath = path.join(projectDir, '.letta', 'settings.json'); );
const localPath = path.join(projectDir, '.letta', 'settings.local.json'); const globalPath = path.join(base.homeDir, '.letta', 'settings.json');
const agentDir = path.join(homeDir, '.elydora', agentId); const projectPath = path.join(base.projectDir, '.letta', 'settings.json');
const guardScriptPath = path.join(agentDir, 'guard.js'); const localPath = path.join(base.projectDir, '.letta', 'settings.local.json');
const hookScriptPath = path.join(agentDir, 'hook.js'); await writeOptionalJson(globalPath, globalSettings);
await mkdir(homeDir, { recursive: true }); await writeOptionalJson(projectPath, projectSettings);
await mkdir(projectDir, { recursive: true }); await writeOptionalJson(localPath, localSettings);
await writeOptional(globalPath, globalSettings);
await writeOptional(projectPath, projectSettings);
await writeOptional(localPath, localSettings);
return { return {
agentDir, ...base,
agentId,
baseUrl, baseUrl,
globalPath, globalPath,
guardScriptPath,
homeDir,
hookScriptPath,
localPath, localPath,
projectDir,
projectPath, projectPath,
rootDir,
install(overrides = {}) { install(overrides = {}) {
return runPlugin(this, 'install', installConfig(this, overrides)); return runPlugin(this, 'install', installConfig(this, overrides));
}, },
async close() {
await rm(rootDir, { recursive: true, force: true });
},
}; };
} }
export async function readSettings(filePath) {
return JSON.parse(await readFile(filePath, 'utf-8'));
}
export function managedHandler(settings, event) { export function managedHandler(settings, event) {
return settings.hooks[event].find((group) => ( return settings.hooks[event].find((group) => (
group.matcher === '*' group.matcher === '*'
@@ -148,40 +78,5 @@ export function runLettaHook(handler, input, fixture) {
const args = process.platform === 'win32' const args = process.platform === 'win32'
? ['-NoLogo', '-NoProfile', '-NonInteractive', '-Command', handler.command] ? ['-NoLogo', '-NoProfile', '-NonInteractive', '-Command', handler.command]
: ['-c', handler.command]; : ['-c', handler.command];
return runProcess(shell, args, environment(fixture), fixture.projectDir, input); return runProcess(shell, args, homeEnvironment(fixture), fixture.projectDir, input);
}
export async function startApiServer(initialStatus = 'active') {
const requests = [];
let status = initialStatus;
let operationStatus = 201;
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 });
if (request.method === 'GET') {
response.writeHead(200, { 'Content-Type': 'application/json' });
response.end(JSON.stringify({ agent: { status } }));
return;
}
response.writeHead(operationStatus, { 'Content-Type': 'application/json' });
response.end(operationStatus < 400
? '{"operation":{"accepted":true}}'
: '{"error":{"code":"AUDIT_UNAVAILABLE"}}');
});
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,
setStatus(value) { status = value; },
setOperationStatus(value) { operationStatus = value; },
close: () => new Promise((resolve, reject) => {
server.close((error) => error ? reject(error) : resolve());
}),
};
} }
+33 -135
View File
@@ -1,87 +1,40 @@
import { spawn } from 'node:child_process'; import { mkdir, writeFile } from 'node:fs/promises';
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 path from 'node:path';
import { pathToFileURL } from 'node:url'; import { pathToFileURL } from 'node:url';
import {
createFixtureRoot,
distUrl,
homeEnvironment,
installConfig as baseInstallConfig,
runPlugin as basePlugin,
} from './harness.mjs';
export const VALID_PRIVATE_KEY = Buffer.alloc(32, 19).toString('base64url'); export {
export const pluginModuleUrl = pathToFileURL(path.resolve('dist/plugins/opencode.js')).href; VALID_PRIVATE_KEY,
export const contractModuleUrl = pathToFileURL( cliPath,
path.resolve('dist/plugins/opencode-contract.js'), readJson,
).href; registryModuleUrl,
export const installationModuleUrl = pathToFileURL( runNode,
path.resolve('dist/plugins/opencode-installation.js'), runProcess,
).href; startApiServer,
export const ioModuleUrl = pathToFileURL(path.resolve('dist/plugins/opencode-io.js')).href; } from './harness.mjs';
export const registryModuleUrl = pathToFileURL(path.resolve('dist/plugins/registry.js')).href;
export const cliPath = path.resolve('dist/cli.js');
export function runProcess(command, args, env, cwd, input = '') { export const pluginModuleUrl = distUrl('plugins/opencode.js');
return new Promise((resolve, reject) => { export const contractModuleUrl = distUrl('plugins/opencode-contract.js');
const childEnvironment = { ...process.env, ...env }; export const installationModuleUrl = distUrl('plugins/opencode-installation.js');
for (const [name, value] of Object.entries(env)) { export const ioModuleUrl = distUrl('plugins/opencode-io.js');
if (value === undefined) delete childEnvironment[name]; const PLUGIN = { exportName: 'opencodePlugin', moduleUrl: pluginModuleUrl };
}
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 function environment(fixture) { export function environment(fixture) {
return { return { ...homeEnvironment(fixture), XDG_CONFIG_HOME: fixture.configRoot };
HOME: fixture.homeDir,
USERPROFILE: fixture.homeDir,
XDG_CONFIG_HOME: fixture.configRoot,
};
} }
export function installConfig(fixture, overrides = {}) { export function installConfig(fixture, overrides = {}) {
return { return baseInstallConfig('opencode', fixture, overrides);
agentName: 'opencode',
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,
};
} }
export async function runPlugin(fixture, method, argument) { export function runPlugin(fixture, method, argument) {
const source = ` return basePlugin(PLUGIN, fixture, method, argument, { env: environment(fixture) });
import { opencodePlugin } from ${JSON.stringify(pluginModuleUrl)};
const argument = JSON.parse(process.env.ELYDORA_TEST_ARGUMENT);
const result = await opencodePlugin[process.env.ELYDORA_TEST_METHOD](argument);
if (result !== undefined) console.log(JSON.stringify(result));
`;
return runNode(
['--input-type=module', '--eval', source],
{
...environment(fixture),
ELYDORA_TEST_ARGUMENT: JSON.stringify(argument),
ELYDORA_TEST_METHOD: method,
},
fixture.projectDir,
);
} }
async function writeOptional(filePath, value) { async function writeOptional(filePath, value) {
@@ -96,33 +49,24 @@ export async function createFixture({
existingPlugin, existingPlugin,
existingLegacy, existingLegacy,
} = {}) { } = {}) {
const rootDir = await mkdtemp(path.join(os.tmpdir(), 'elydora-opencode-')); const base = await createFixtureRoot(
const homeDir = path.join(rootDir, "home with spaces and 'quote %OPEN%"); 'elydora-opencode-',
const configRoot = path.join(rootDir, 'xdg config'); "home with spaces and 'quote %OPEN%",
const projectDir = path.join(rootDir, 'workspace with spaces'); { agentId, projectName: 'workspace with spaces', createHome: true },
);
const configRoot = path.join(base.rootDir, 'xdg config');
const pluginsDir = path.join(configRoot, 'opencode', 'plugins'); const pluginsDir = path.join(configRoot, 'opencode', 'plugins');
const pluginPath = path.join(pluginsDir, 'elydora-audit.js'); const pluginPath = path.join(pluginsDir, 'elydora-audit.js');
const legacyPluginPath = path.join(pluginsDir, 'elydora-audit.mjs'); const legacyPluginPath = path.join(pluginsDir, 'elydora-audit.mjs');
const agentDir = path.join(homeDir, '.elydora', agentId);
const guardScriptPath = path.join(agentDir, 'guard.js');
const hookScriptPath = path.join(agentDir, 'hook.js');
await mkdir(homeDir, { recursive: true });
await mkdir(projectDir, { recursive: true });
await writeOptional(pluginPath, existingPlugin); await writeOptional(pluginPath, existingPlugin);
await writeOptional(legacyPluginPath, existingLegacy); await writeOptional(legacyPluginPath, existingLegacy);
return { return {
agentDir, ...base,
agentId,
baseUrl, baseUrl,
configRoot, configRoot,
guardScriptPath,
homeDir,
hookScriptPath,
legacyPluginPath, legacyPluginPath,
pluginPath, pluginPath,
pluginsDir, pluginsDir,
projectDir,
rootDir,
install(overrides = {}) { install(overrides = {}) {
return runPlugin(this, 'install', installConfig(this, overrides)); return runPlugin(this, 'install', installConfig(this, overrides));
}, },
@@ -130,51 +74,5 @@ export async function createFixture({
const module = await import(`${pathToFileURL(this.pluginPath).href}?test=${Date.now()}`); const module = await import(`${pathToFileURL(this.pluginPath).href}?test=${Date.now()}`);
return module.ElydoraAuditPlugin({}); return module.ElydoraAuditPlugin({});
}, },
async close() {
await rm(rootDir, { recursive: true, force: true });
},
};
}
export async function readJson(filePath) {
return JSON.parse(await readFile(filePath, 'utf-8'));
}
export async function startApiServer(initialStatus = 'active') {
const requests = [];
let status = initialStatus;
let operationStatus = 201;
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 });
if (request.method === 'GET') {
response.writeHead(200, { 'Content-Type': 'application/json' });
response.end(JSON.stringify({ agent: { status } }));
return;
}
response.writeHead(operationStatus, { 'Content-Type': 'application/json' });
response.end(operationStatus < 400
? '{"operation":{"accepted":true}}'
: '{"error":{"code":"AUDIT_UNAVAILABLE"}}');
});
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,
setStatus(value) {
status = value;
},
setOperationStatus(value) {
operationStatus = value;
},
close: () => new Promise((resolve, reject) => {
server.close((error) => error ? reject(error) : resolve());
}),
}; };
} }
+40 -170
View File
@@ -1,28 +1,32 @@
import assert from 'node:assert/strict'; import assert from 'node:assert/strict';
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 path from 'node:path';
import { pathToFileURL } from 'node:url'; import {
import { parse } from 'jsonc-parser'; createFixtureRoot,
distUrl,
homeEnvironment,
installConfig as baseInstallConfig,
runPlugin as basePlugin,
runProcess as baseProcess,
windowsPowerShellPath,
writeOptionalJson,
} from './harness.mjs';
export const VALID_PRIVATE_KEY = Buffer.alloc(32).toString('base64url'); export {
export const pluginModuleUrl = pathToFileURL(path.resolve('dist/plugins/qwen.js')).href; VALID_PRIVATE_KEY,
export const registryModuleUrl = pathToFileURL(path.resolve('dist/plugins/registry.js')).href; cliPath,
export const sourcesModuleUrl = pathToFileURL( parseJsonc as parseSettings,
path.resolve('dist/plugins/qwen-sources.js'), readJsoncSettings as readSettings,
).href; registryModuleUrl,
export const installationModuleUrl = pathToFileURL( startApiServer,
path.resolve('dist/plugins/qwen-installation.js'), writeJson as writeSettings,
).href; } from './harness.mjs';
export const configModuleUrl = pathToFileURL(
path.resolve('dist/plugins/qwen-config.js'), export const pluginModuleUrl = distUrl('plugins/qwen.js');
).href; export const sourcesModuleUrl = distUrl('plugins/qwen-sources.js');
export const contractModuleUrl = pathToFileURL( export const installationModuleUrl = distUrl('plugins/qwen-installation.js');
path.resolve('dist/plugins/qwen-contract.js'), export const configModuleUrl = distUrl('plugins/qwen-config.js');
).href; export const contractModuleUrl = distUrl('plugins/qwen-contract.js');
export const cliPath = path.resolve('dist/cli.js'); const PLUGIN = { exportName: 'qwenPlugin', moduleUrl: pluginModuleUrl };
const QWEN_ENV_KEYS = [ const QWEN_ENV_KEYS = [
'QWEN_HOME', 'QWEN_HOME',
@@ -33,48 +37,18 @@ const QWEN_ENV_KEYS = [
]; ];
export function runProcess(command, args, env, cwd, input = '', unset = []) { export function runProcess(command, args, env, cwd, input = '', unset = []) {
return new Promise((resolve, reject) => { return baseProcess(command, args, env, cwd, input, { unset });
const childEnv = { ...process.env, ...env };
for (const key of unset) delete childEnv[key];
const child = spawn(command, args, {
cwd,
env: childEnv,
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 = '', unset = []) { export function runNode(args, env, cwd, input = '', unset = []) {
return runProcess(process.execPath, args, env, cwd, input, unset); return runProcess(process.execPath, args, env, cwd, input, unset);
} }
function windowsPowerShell() {
return path.win32.join(
process.env.SystemRoot || 'C:\\Windows',
'System32',
'WindowsPowerShell',
'v1.0',
'powershell.exe',
);
}
export function runQwenHook(handler, input, fixture, environment = {}) { export function runQwenHook(handler, input, fixture, environment = {}) {
const env = { const env = { ...homeEnvironment(fixture), ...environment };
HOME: fixture.homeDir,
USERPROFILE: fixture.homeDir,
...environment,
};
if (process.platform === 'win32') { if (process.platform === 'win32') {
return runProcess( return runProcess(
windowsPowerShell(), windowsPowerShellPath(),
['-NoLogo', '-NoProfile', '-NonInteractive', '-Command', handler.command], ['-NoLogo', '-NoProfile', '-NonInteractive', '-Command', handler.command],
env, env,
fixture.projectDir, fixture.projectDir,
@@ -84,109 +58,36 @@ export function runQwenHook(handler, input, fixture, environment = {}) {
return runProcess('/bin/bash', ['-c', handler.command], env, fixture.projectDir, input); return runProcess('/bin/bash', ['-c', handler.command], env, fixture.projectDir, input);
} }
export async function writeSettings(filePath, value) {
await mkdir(path.dirname(filePath), { recursive: true });
const contents = typeof value === 'string' ? value : `${JSON.stringify(value, null, 2)}\n`;
await writeFile(filePath, contents, { encoding: 'utf-8', mode: 0o600 });
}
export function parseSettings(raw) {
const errors = [];
const value = parse(raw, errors, {
allowTrailingComma: false,
disallowComments: false,
});
assert.deepEqual(errors, []);
return value;
}
export async function readSettings(filePath) {
const raw = await readFile(filePath, 'utf-8');
return { raw, settings: parseSettings(raw) };
}
export function installConfig(fixture, overrides = {}) { export function installConfig(fixture, overrides = {}) {
return { return baseInstallConfig('qwen', fixture, overrides);
agentName: 'qwen',
orgId: 'org-1',
agentId: 'agent-1',
privateKey: VALID_PRIVATE_KEY,
kid: 'kid-1',
token: 'token-1',
baseUrl: fixture.baseUrl,
guardScriptPath: fixture.guardScriptPath,
hookScriptPath: fixture.hookScriptPath,
...overrides,
};
} }
function qwenEnvironment(fixture, environment) { export function runPlugin(fixture, method, argument, environment = {}) {
const env = { const env = {
HOME: fixture.homeDir,
USERPROFILE: fixture.homeDir,
QWEN_CODE_SYSTEM_DEFAULTS_PATH: path.join(fixture.rootDir, 'system-defaults.json'), QWEN_CODE_SYSTEM_DEFAULTS_PATH: path.join(fixture.rootDir, 'system-defaults.json'),
QWEN_CODE_SYSTEM_SETTINGS_PATH: path.join(fixture.rootDir, 'system-settings.json'), QWEN_CODE_SYSTEM_SETTINGS_PATH: path.join(fixture.rootDir, 'system-settings.json'),
...environment, ...environment,
}; };
const unset = QWEN_ENV_KEYS.filter((key) => !Object.hasOwn(env, key)); const unset = QWEN_ENV_KEYS.filter((key) => !Object.hasOwn(env, key));
return { env, unset }; return basePlugin(PLUGIN, fixture, method, argument, { env, unset });
} }
export async function runPlugin(fixture, method, argument, environment = {}) { export async function createFixture({ baseUrl = 'http://127.0.0.1:9', settings } = {}) {
const source = ` const base = await createFixtureRoot(
import { qwenPlugin } from ${JSON.stringify(pluginModuleUrl)}; 'elydora-qwen-',
const argument = JSON.parse(process.env.ELYDORA_TEST_ARGUMENT);
const result = await qwenPlugin[process.env.ELYDORA_TEST_METHOD](argument);
if (result !== undefined) console.log(JSON.stringify(result));
`;
const configured = qwenEnvironment(fixture, {
ELYDORA_TEST_ARGUMENT: JSON.stringify(argument),
ELYDORA_TEST_METHOD: method,
...environment,
});
return runNode(
['--input-type=module', '--eval', source],
configured.env,
fixture.projectDir,
'',
configured.unset,
);
}
export async function createFixture({
baseUrl = 'http://127.0.0.1:9',
settings,
} = {}) {
const rootDir = await mkdtemp(path.join(os.tmpdir(), 'elydora-qwen-'));
const homeDir = path.join(
rootDir,
"home with spaces and 'quote $QWEN_PROJECT_DIR %QWEN_PROJECT_DIR%", "home with spaces and 'quote $QWEN_PROJECT_DIR %QWEN_PROJECT_DIR%",
{ createHome: true },
); );
const projectDir = path.join(rootDir, 'project with spaces'); const settingsPath = path.join(base.homeDir, '.qwen', 'settings.json');
const settingsPath = path.join(homeDir, '.qwen', 'settings.json'); await writeOptionalJson(settingsPath, settings);
const agentDir = path.join(homeDir, '.elydora', 'agent-1'); return {
const guardScriptPath = path.join(agentDir, 'guard.js'); ...base,
const hookScriptPath = path.join(agentDir, 'hook.js');
await mkdir(homeDir, { recursive: true });
await mkdir(projectDir, { recursive: true });
if (settings !== undefined) await writeSettings(settingsPath, settings);
const fixture = {
agentDir,
baseUrl, baseUrl,
guardScriptPath,
homeDir,
hookScriptPath,
projectDir,
rootDir,
settingsPath, settingsPath,
install(overrides = {}, environment = {}) { install(overrides = {}, environment = {}) {
return runPlugin(this, 'install', installConfig(this, overrides), environment); return runPlugin(this, 'install', installConfig(this, overrides), environment);
}, },
async close() {
await rm(rootDir, { recursive: true, force: true });
},
}; };
return fixture;
} }
export function managedHandler(settings, event, name) { export function managedHandler(settings, event, name) {
@@ -243,34 +144,3 @@ export function legacyGroup(scriptPath) {
}], }],
}; };
} }
export async function startApiServer({ status = 'active', operationStatus = 201 } = {}) {
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({ headers: request.headers, method: request.method, url: request.url, raw });
if (request.method === 'GET') {
response.writeHead(200, { 'Content-Type': 'application/json' });
response.end(JSON.stringify({ agent: { status } }));
return;
}
response.writeHead(operationStatus, { 'Content-Type': 'application/json' });
response.end(operationStatus >= 200 && operationStatus < 300
? JSON.stringify({ operation: { accepted: true } })
: JSON.stringify({ error: { code: 'UPSTREAM_FAILURE', message: 'failed' } }));
});
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());
}),
};
}
+1
View File
@@ -18,6 +18,7 @@ async function runCli(args, homeDir) {
...process.env, ...process.env,
HOME: homeDir, HOME: homeDir,
USERPROFILE: homeDir, USERPROFILE: homeDir,
XDG_CONFIG_HOME: path.join(homeDir, '.config'),
}, },
stdio: ['ignore', 'pipe', 'pipe'], stdio: ['ignore', 'pipe', 'pipe'],
}); });
+2
View File
@@ -26,6 +26,7 @@ async function createFixture(status) {
await writeFile( await writeFile(
path.join(agentDir, 'config.json'), path.join(agentDir, 'config.json'),
JSON.stringify({ agent_id: AGENT_ID, base_url: `http://127.0.0.1:${address.port}` }), JSON.stringify({ agent_id: AGENT_ID, base_url: `http://127.0.0.1:${address.port}` }),
{ mode: 0o600 },
); );
const scriptPath = path.join(homeDir, 'guard.cjs'); const scriptPath = path.join(homeDir, 'guard.cjs');
@@ -91,6 +92,7 @@ test('cached frozen status keeps the blocking exit code', async () => {
await writeFile( await writeFile(
path.join(fixture.homeDir, '.elydora', AGENT_ID, 'status-cache.json'), path.join(fixture.homeDir, '.elydora', AGENT_ID, 'status-cache.json'),
JSON.stringify({ status: 'frozen', cached_at: Date.now() }), JSON.stringify({ status: 'frozen', cached_at: Date.now() }),
{ mode: 0o600 },
); );
const result = await runGuard(fixture.scriptPath, fixture.homeDir); const result = await runGuard(fixture.scriptPath, fixture.homeDir);
+2 -2
View File
@@ -60,7 +60,7 @@ async function installedHandlers(fixture) {
} }
test('Letta guard accepts active agents with native exit semantics', async () => { test('Letta guard accepts active agents with native exit semantics', async () => {
const api = await startApiServer('active'); const api = await startApiServer({ status: 'active' });
const fixture = await createFixture({ baseUrl: api.baseUrl }); const fixture = await createFixture({ baseUrl: api.baseUrl });
try { try {
assert.equal((await fixture.install()).code, 0); assert.equal((await fixture.install()).code, 0);
@@ -82,7 +82,7 @@ test('Letta guard accepts active agents with native exit semantics', async () =>
test('Letta guard propagates frozen and revoked states through exit code 2', async (t) => { test('Letta guard propagates frozen and revoked states through exit code 2', async (t) => {
for (const status of ['frozen', 'revoked']) { for (const status of ['frozen', 'revoked']) {
await t.test(status, async () => { await t.test(status, async () => {
const api = await startApiServer(status); const api = await startApiServer({ status });
const fixture = await createFixture({ baseUrl: api.baseUrl }); const fixture = await createFixture({ baseUrl: api.baseUrl });
try { try {
assert.equal((await fixture.install()).code, 0); assert.equal((await fixture.install()).code, 0);