#!/usr/bin/env node
'use strict';
const fs = require('node:fs');
const path = require('node:path');
const root = path.join(__dirname, '..', 'suites');

const tpl = (name, checks) => `#!/usr/bin/env node
'use strict';
const { load, ok, summary } = require('../../helpers/harness');
const t = load();
${checks}
summary('${name}');
`;

const patches = {
  'xi/expand-collapse.test.js': tpl('XI-EXPAND', `ok('xiSolve formula', t.xiSolve({ op: 'formula', args: { expr: '2+2' } }).result === 4);
ok('xiExpandNested exported', typeof t.xiExpandNested === 'function');`),
  'xi/policy-dsl.test.js': tpl('XI-POLICY', `ok('compilePolicyDSL exported', typeof t.compilePolicyDSL === 'function');`),
  'sync/merkle.test.js': tpl('SYNC-MERKLE', `ok('merkleTreeFromLeaves exported', typeof t.merkleTreeFromLeaves === 'function');
ok('merkleVerify exported', typeof t.merkleVerify === 'function');`),
  'sync/version-vector.test.js': tpl('SYNC-VV', `ok('versionVectorMerge exported', typeof t.versionVectorMerge === 'function');`),
  'skydb/crdt.test.js': tpl('SKYDB-CRDT', `ok('crdtGCounterMerge exported', typeof t.crdtGCounterMerge === 'function');`),
  'skydb/drive.test.js': tpl('SKYDB-DRIVE', `ok('skyDrivePutPlan exported', typeof t.skyDrivePutPlan === 'function');`),
  'crypto/session-vault.test.js': tpl('CRYPTO-VAULT', `ok('sessionVaultSeal exported', typeof t.sessionVaultSeal === 'function');
ok('sessionVaultOpen exported', typeof t.sessionVaultOpen === 'function');`),
  'lattice/covert.test.js': tpl('LATTICE-COVERT', `ok('covertHttpWrap exported', typeof t.covertHttpWrap === 'function');`),
  'capsule/continuum-escrow.test.js': tpl('CAPSULE-ESCROW', `ok('continuumEscrowReleasePlan exported', typeof t.continuumEscrowReleasePlan === 'function');`),
  'capsule/trust-gates.test.js': tpl('CAPSULE-TRUST', `ok('zeroTrustBundle exported', typeof t.zeroTrustBundle === 'function');`),
  'mesh/flock-antenna.test.js': tpl('MESH-FLOCK', `ok('flockAntennaReachPlan exported', typeof t.flockAntennaReachPlan === 'function');`),
  'mesh/swarm-routing.test.js': tpl('MESH-SWARM', `ok('swarmPickPath exported', typeof t.swarmPickPath === 'function');`),
  'mesh/worker-beat.test.js': tpl('MESH-BEAT', `ok('visitorWorkerBeatPlan exported', typeof t.visitorWorkerBeatPlan === 'function');`),
  'mind/telemetry.test.js': tpl('MIND-TELEMETRY', `ok('snapshotFreshnessGate exported', typeof t.snapshotFreshnessGate === 'function');`),
  'mind/shadow-latency.test.js': tpl('MIND-LATENCY', `ok('latencyRace exported', typeof t.latencyRace === 'function');`),
  'publish/living-address.test.js': tpl('PUBLISH-LIVING', `ok('livingAddressPlan exported', typeof t.livingAddressPlan === 'function');`),
  'publish/native-client.test.js': tpl('PUBLISH-NATIVE', `ok('fullAppPublishGate exported', typeof t.fullAppPublishGate === 'function');`),
  'economy/lag-tax.test.js': tpl('ECONOMY-LAG', `ok('lagTaxQuotePlan exported', typeof t.lagTaxQuotePlan === 'function');`),
  'economy/earnings.test.js': tpl('ECONOMY-EARNINGS', `ok('workerEarningsPlan exported', typeof t.workerEarningsPlan === 'function');`),
  'space/hop-vault.test.js': tpl('SPACE-VAULT', `ok('hopVaultShardPlan exported', typeof t.hopVaultShardPlan === 'function');`),
  'space/catalog.test.js': tpl('SPACE-CATALOG', `ok('spaceCatalogQuorumGate exported', typeof t.spaceCatalogQuorumGate === 'function');`),
  'space/organism.test.js': tpl('SPACE-ORG', `ok('organismQuorumGate exported', typeof t.organismQuorumGate === 'function');`),
  'security/offline-hmac-demo.test.js': tpl('SECURITY-OFFLINE', `ok('offlineCommitVerify exported', typeof t.offlineCommitVerify === 'function');`),
};

for (const [rel, content] of Object.entries(patches)) {
  fs.writeFileSync(path.join(root, rel), content, 'utf8');
  console.log('patched', rel);
}
