Files
reporting-governance-plugin/plugins/reporting-governance/test/exports-boundary.integration.test.mjs

230 lines
8.6 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import os from 'node:os';
import { spawnSync } from 'node:child_process';
const packageRoot = path.resolve(import.meta.dirname, '..');
function createFixtureRoot() {
return fs.mkdtempSync(path.join(os.tmpdir(), 'reporting-governance-exports-'));
}
function installPackageAlias(root) {
const packageDir = path.join(root, 'node_modules', '@openclaw', 'plugin-reporting-governance');
fs.mkdirSync(path.dirname(packageDir), { recursive: true });
fs.symlinkSync(packageRoot, packageDir, 'dir');
return packageDir;
}
function writeState(root) {
const statePath = path.join(root, 'watchdog-state.json');
fs.writeFileSync(statePath, `${JSON.stringify({
version: 1,
watchdogs: [
{
id: 'reporting-governance-plugin-watchdog',
task: 'reporting-governance plugin spec development',
status: 'active',
ownerSessionKey: 'agent:coder:main',
reportChannel: 'telegram',
reportTarget: '864811879',
intervalMinutes: 10,
lastMilestoneAt: '2026-05-07T08:00:00.000Z',
lastAlertAt: null
}
]
}, null, 2)}\n`, 'utf8');
return statePath;
}
function runNodeEval(root, source, env = {}) {
return spawnSync(process.execPath, ['--input-type=module', '--eval', source], {
cwd: root,
encoding: 'utf8',
env: {
...process.env,
...env,
},
});
}
function runJsonEval(root, source, env = {}) {
const result = runNodeEval(root, source, env);
if (result.status !== 0) {
throw new Error(`node eval failed: ${(result.stderr ?? '').trim() || '(no stderr)'}`);
}
return JSON.parse((result.stdout ?? '').trim());
}
test('package root export resolves public package surface only', () => {
const root = createFixtureRoot();
try {
installPackageAlias(root);
const result = runJsonEval(root, `
import * as plugin from '@openclaw/plugin-reporting-governance';
process.stdout.write(JSON.stringify({
packageName: plugin.packageName,
artifactKinds: plugin.artifactKinds,
hasRunWatchdogChain: typeof plugin.runWatchdogChain,
hasPlanDecisionExecution: typeof plugin.planDecisionExecution,
hasExecuteGovernanceContract: typeof plugin.executeGovernanceContract,
hasExecuteRuntimeIntegratedGovernance: typeof plugin.executeRuntimeIntegratedGovernance,
hasCreateRuntimeBinding: typeof plugin.createRuntimeBinding,
hasCreateDecisionRecordArtifact: typeof plugin.createDecisionRecordArtifact,
hasCreateFileDecisionStore: typeof plugin.createFileDecisionStore,
}));
`);
assert.equal(result.packageName, '@openclaw/plugin-reporting-governance');
assert.deepEqual(result.artifactKinds, {
deploymentProfile: 'DeploymentProfileArtifact',
decisionRecord: 'DecisionRecordArtifact',
});
assert.equal(result.hasRunWatchdogChain, 'function');
assert.equal(result.hasPlanDecisionExecution, 'function');
assert.equal(result.hasExecuteGovernanceContract, 'function');
assert.equal(result.hasExecuteRuntimeIntegratedGovernance, 'function');
assert.equal(result.hasCreateRuntimeBinding, 'function');
assert.equal(result.hasCreateDecisionRecordArtifact, 'function');
assert.equal(result.hasCreateFileDecisionStore, 'function');
} finally {
fs.rmSync(root, { recursive: true, force: true });
}
});
test('adapters subpath export resolves package-owned adapter entrypoints only', () => {
const root = createFixtureRoot();
try {
installPackageAlias(root);
const result = runJsonEval(root, `
import * as adapters from '@openclaw/plugin-reporting-governance/adapters';
process.stdout.write(JSON.stringify({
adapterKeys: Object.keys(adapters).sort(),
}));
`);
assert.deepEqual(result.adapterKeys, [
'runBridgeSupervisorAdapter',
'runDispatcherAdapter',
'runOrchestratorAdapter',
'runSenderBindingAdapter',
'runWatchdogAdapter',
]);
} finally {
fs.rmSync(root, { recursive: true, force: true });
}
});
test('storage/profile-artifact deep subpaths stay outside package exports boundary', () => {
const root = createFixtureRoot();
try {
installPackageAlias(root);
const result = runNodeEval(root, `
import('@openclaw/plugin-reporting-governance/src/storage/profile-artifact.mjs')
.then(() => {
process.stdout.write(JSON.stringify({ ok: true }));
})
.catch((error) => {
process.stdout.write(JSON.stringify({
ok: false,
code: error?.code ?? null,
name: error?.name ?? null,
message: error?.message ?? null,
}));
});
`);
assert.equal(result.status, 0, result.stderr);
const payload = JSON.parse((result.stdout ?? '').trim());
assert.equal(payload.ok, false);
assert.equal(payload.code, 'ERR_PACKAGE_PATH_NOT_EXPORTED');
assert.equal(payload.name, 'Error');
assert.match(payload.message ?? '', /Package subpath '\.\/src\/storage\/profile-artifact\.mjs' is not defined by "exports"/);
} finally {
fs.rmSync(root, { recursive: true, force: true });
}
});
test('deep runtime-binding subpath stays outside package exports boundary', () => {
const root = createFixtureRoot();
try {
installPackageAlias(root);
const result = runNodeEval(root, `
import('@openclaw/plugin-reporting-governance/src/adapters/runtime-binding.mjs')
.then(() => {
process.stdout.write(JSON.stringify({ ok: true }));
})
.catch((error) => {
process.stdout.write(JSON.stringify({
ok: false,
code: error?.code ?? null,
name: error?.name ?? null,
message: error?.message ?? null,
}));
});
`);
assert.equal(result.status, 0, result.stderr);
const payload = JSON.parse((result.stdout ?? '').trim());
assert.equal(payload.ok, false);
assert.equal(payload.code, 'ERR_PACKAGE_PATH_NOT_EXPORTED');
assert.equal(payload.name, 'Error');
assert.match(payload.message ?? '', /Package subpath '\.\/src\/adapters\/runtime-binding\.mjs' is not defined by "exports"/);
} finally {
fs.rmSync(root, { recursive: true, force: true });
}
});
test('leaf subpath export resolves and can execute through injected runtime binding', () => {
const root = createFixtureRoot();
try {
installPackageAlias(root);
const statePath = writeState(root);
fs.mkdirSync(path.join(root, 'evidence'), { recursive: true });
fs.mkdirSync(path.join(root, 'events'), { recursive: true });
fs.mkdirSync(path.join(root, 'queue'), { recursive: true });
fs.mkdirSync(path.join(root, 'spool'), { recursive: true });
fs.mkdirSync(path.join(root, 'receipts'), { recursive: true });
const result = runJsonEval(root, `
import { runOrchestratorAdapter } from '@openclaw/plugin-reporting-governance/adapters/orchestrator';
const payload = runOrchestratorAdapter({
runtimeBinding: {
cwd: ${JSON.stringify(path.resolve(packageRoot))},
scripts: {
orchestrator: ${JSON.stringify(path.resolve(packageRoot, 'scripts', 'watchdog_auto_notify_orchestrator.mjs'))},
watchdog: ${JSON.stringify(path.resolve(packageRoot, 'scripts', 'long_task_watchdog.mjs'))},
dispatcher: ${JSON.stringify(path.resolve(packageRoot, 'scripts', 'operator_notify_dispatcher.mjs'))},
bridgeSupervisor: ${JSON.stringify(path.resolve(packageRoot, 'scripts', 'operator_notify_bridge_supervisor.mjs'))},
senderBinding: ${JSON.stringify(path.resolve(packageRoot, 'scripts', 'operator_notify_sender_binding.mjs'))},
},
},
state: ${JSON.stringify(statePath)},
evidenceDir: ${JSON.stringify(path.join(root, 'evidence'))},
eventDir: ${JSON.stringify(path.join(root, 'events'))},
queueDir: ${JSON.stringify(path.join(root, 'queue'))},
spoolDir: ${JSON.stringify(path.join(root, 'spool'))},
receiptDir: ${JSON.stringify(path.join(root, 'receipts'))},
writeState: true,
dryRun: true,
now: '2026-05-07T08:20:00.000Z',
});
process.stdout.write(JSON.stringify({
ok: payload.ok,
dispatchedCount: payload.result.dispatcher.dispatchedCount,
pendingCount: payload.result.supervisor.pendingCount,
}));
`, {
RG_REPO_ROOT: path.resolve(packageRoot, '..', '..'),
});
assert.equal(result.ok, true);
assert.equal(result.dispatchedCount, 1);
assert.equal(result.pendingCount, 1);
} finally {
fs.rmSync(root, { recursive: true, force: true });
}
});