import test from 'node:test'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; import { spawnSync } from 'node:child_process'; const packageRoot = path.resolve(import.meta.dirname, '..'); const unexpectedPackedPathMatchers = [ /\.tgz$/, /^state\//, /^docs\//, /^node_modules\//, ]; function run(command, args, { cwd, env = {} } = {}) { const result = spawnSync(command, args, { cwd, encoding: 'utf8', env: { ...process.env, ...env, }, }); assert.equal( result.status, 0, [ `command failed: ${command} ${args.join(' ')}`, `cwd: ${cwd}`, result.stdout && `stdout:\n${result.stdout.trim()}`, result.stderr && `stderr:\n${result.stderr.trim()}`, ].filter(Boolean).join('\n\n') ); return result; } test('packed tarball excludes nested tarballs and obvious repo junk', () => { const packResult = run('npm', ['pack', '--json', '--dry-run'], { cwd: packageRoot }); const packPayload = JSON.parse(packResult.stdout.trim()); const files = packPayload.at(-1)?.files?.map((entry) => entry.path) ?? []; assert.ok(files.length > 0, 'npm pack --dry-run should report packed files'); for (const packedPath of files) { for (const matcher of unexpectedPackedPathMatchers) { assert.equal( matcher.test(packedPath), false, `packed tarball should not include ${packedPath}` ); } } }); test('packed tarball installs into clean consumer and works via public exports/bin only', () => { const root = fs.mkdtempSync(path.join(os.tmpdir(), 'reporting-governance-packed-consumer-')); try { const packResult = run('npm', ['pack', '--json'], { cwd: packageRoot }); const packPayload = JSON.parse(packResult.stdout.trim()); const tarballName = packPayload.at(-1)?.filename; assert.ok(tarballName, 'npm pack should return tarball filename'); const tarballPath = path.join(packageRoot, tarballName); assert.equal(fs.existsSync(tarballPath), true, 'tarball should exist after npm pack'); const consumerRoot = path.join(root, 'consumer'); fs.mkdirSync(consumerRoot, { recursive: true }); run('npm', ['init', '-y'], { cwd: consumerRoot }); run('npm', ['install', tarballPath], { cwd: consumerRoot }); const exportProbe = run(process.execPath, ['--input-type=module', '--eval', ` import * as plugin from '@openclaw/plugin-reporting-governance'; import { runOrchestratorAdapter } from '@openclaw/plugin-reporting-governance/adapters/orchestrator'; process.stdout.write(JSON.stringify({ packageName: plugin.packageName, hasRunWatchdogChain: typeof plugin.runWatchdogChain, hasRunOrchestratorAdapter: typeof runOrchestratorAdapter, })); `], { cwd: consumerRoot }); const exportPayload = JSON.parse(exportProbe.stdout.trim()); assert.equal(exportPayload.packageName, '@openclaw/plugin-reporting-governance'); assert.equal(exportPayload.hasRunWatchdogChain, 'function'); assert.equal(exportPayload.hasRunOrchestratorAdapter, 'function'); const smokeResult = run( path.join(consumerRoot, 'node_modules', '.bin', 'reporting-governance-package-smoke'), ['--compact'], { cwd: consumerRoot } ); const smokePayload = JSON.parse(smokeResult.stdout.trim()); assert.equal(smokePayload.ok, true); assert.equal(smokePayload.tool, 'reporting-governance-package-smoke'); assert.equal(smokePayload.orchestrator.ok, true); assert.equal(smokePayload.orchestrator.dispatchedCount, 1); assert.equal(smokePayload.orchestrator.pendingCount, 1); assert.equal(smokePayload.orchestrator.notificationCount, 1); } finally { fs.rmSync(root, { recursive: true, force: true }); } });