Files
Elydora-Node-SDK/test/guard-template.test.mjs
雷电芽衣 90bea63eea 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
2026-09-02 22:22:45 -04:00

120 lines
3.7 KiB
JavaScript

import assert from 'node:assert/strict';
import { spawn } from 'node:child_process';
import { mkdtemp, mkdir, rm, writeFile } from 'node:fs/promises';
import http from 'node:http';
import os from 'node:os';
import path from 'node:path';
import test from 'node:test';
import { generateGuardScript } from '../dist/plugins/guard-template.js';
const AGENT_ID = 'agent-1';
async function createFixture(status) {
const homeDir = await mkdtemp(path.join(os.tmpdir(), 'elydora-guard-'));
const agentDir = path.join(homeDir, '.elydora', AGENT_ID);
await mkdir(agentDir, { recursive: true });
const server = http.createServer((_request, response) => {
response.writeHead(200, { 'Content-Type': 'application/json' });
response.end(JSON.stringify({ agent: { status } }));
});
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve));
const address = server.address();
assert(address && typeof address === 'object');
await writeFile(
path.join(agentDir, 'config.json'),
JSON.stringify({ agent_id: AGENT_ID, base_url: `http://127.0.0.1:${address.port}` }),
{ mode: 0o600 },
);
const scriptPath = path.join(homeDir, 'guard.cjs');
await writeFile(scriptPath, generateGuardScript('claudecode', AGENT_ID));
return {
homeDir,
scriptPath,
async close() {
await new Promise((resolve, reject) => {
server.close((error) => (error ? reject(error) : resolve()));
});
await rm(homeDir, { recursive: true, force: true });
},
};
}
function runGuard(scriptPath, homeDir, input = '{}') {
return new Promise((resolve, reject) => {
const child = spawn(process.execPath, [scriptPath], {
env: { ...process.env, HOME: homeDir, USERPROFILE: homeDir },
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);
});
}
test('frozen agents use the blocking exit code required by hook CLIs', async () => {
const fixture = await createFixture('frozen');
try {
const result = await runGuard(fixture.scriptPath, fixture.homeDir);
assert.equal(result.code, 2);
assert.match(result.stderr, /Tool execution blocked/);
assert.equal(result.stdout, '');
} finally {
await fixture.close();
}
});
test('active agents allow tool execution', async () => {
const fixture = await createFixture('active');
try {
const result = await runGuard(fixture.scriptPath, fixture.homeDir);
assert.equal(result.code, 0);
assert.equal(result.stderr, '');
assert.equal(result.stdout, '');
} finally {
await fixture.close();
}
});
test('cached frozen status keeps the blocking exit code', async () => {
const fixture = await createFixture('active');
try {
await writeFile(
path.join(fixture.homeDir, '.elydora', AGENT_ID, 'status-cache.json'),
JSON.stringify({ status: 'frozen', cached_at: Date.now() }),
{ mode: 0o600 },
);
const result = await runGuard(fixture.scriptPath, fixture.homeDir);
assert.equal(result.code, 2);
assert.match(result.stderr, /Tool execution blocked/);
assert.equal(result.stdout, '');
} finally {
await fixture.close();
}
});
test('fail-open guards report malformed hook input', async () => {
const fixture = await createFixture('active');
try {
const result = await runGuard(fixture.scriptPath, fixture.homeDir, '{ malformed');
assert.equal(result.code, 0);
assert.match(result.stderr, /invalid JSON.*fail-open/i);
assert.equal(result.stdout, '');
} finally {
await fixture.close();
}
});