cred402/test/reputation_breakdown.test.ts
caelum0x 5cf86893e3 feat(contracts): deploy Cred402 Odra contracts to Casper Testnet (Casper 2.0)
Migrate the contract suite to Odra 2.x (builds on the Casper 2.0 ABI; fixes the
1.x nightly/edition2024 toolchain deadlock) and deploy 9 contracts live to
casper-test via the Odra livenet deployer:

  AgentRegistry · X402ReceiptRegistry · RWAEvidenceRegistry · RWAAssetRegistry
  ReputationEngine · AgentCreditPool · RiskPolicyManager · AgentPassport · DisputeCourt

- contracts: odra "2", no_std headers, build.rs (odra-build), per-crate Odra.toml +
  build_contract/livenet bins; pinned nightly-2025-01-15 for reproducible wasm.
- tooling: scaffold_livenet.py, deploy_all_testnet.sh, deploy_one.mjs.
- deploys.testnet.json: live manifest; README: on-chain contract hashes.
- Deployer key kept out of git (.secrets/ gitignored); wasm artifacts gitignored.

Verified on-chain: AgentRegistry ContractPackage present in global state.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-17 02:34:49 +03:00

49 lines
2.2 KiB
TypeScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { Cred402Economy } from "../agents/economy.js";
import { Ledger } from "../lib/ledger/index.js";
import { buildReputationBreakdown } from "../lib/services/reputation_breakdown.js";
test("reputation breakdown: dimensions, weights and contributions reconcile", async () => {
const econ = new Cred402Economy(new Ledger());
econ.bootstrap();
econ.createJob();
const { reports } = await econ.runEvidencePurchases();
await econ.runWatchdogAudit(reports);
const id = econ.seller.agent_id;
const b = buildReputationBreakdown(econ.ledger, id);
assert.ok(!("error" in b));
if ("error" in b) return;
// the weighted dimensions (excluding the penalty) define the composite
const weighted = b.dimensions.filter((d) => d.dimension !== "collusion_penalty");
const penalty = b.dimensions.find((d) => d.dimension === "collusion_penalty")!;
const sum = weighted.reduce((s, d) => s + d.contribution, 0) + penalty.contribution;
assert.ok(Math.abs(sum - b.composite_score) <= 1, `weighted sum ${sum} ≈ composite ${b.composite_score}`);
// weights of the scored dimensions sum to 1.0
const weightSum = weighted.reduce((s, d) => s + d.weight, 0);
assert.ok(Math.abs(weightSum - 1) < 1e-9);
// sorted by contribution descending
for (let i = 1; i < b.dimensions.length; i++) {
assert.ok(b.dimensions[i - 1]!.contribution >= b.dimensions[i]!.contribution);
}
});
test("reputation breakdown: an open dispute applies a negative penalty term", () => {
const l = new Ledger();
l.agents.register_agent({ agent_id: "A", owner_public_key: "01", agent_public_key: "01", service_type: "monitoring" });
l.disputes.open({ dispute_type: "bad_evidence", complainant: "W", respondent_agent: "A", note: "x", evidence_hash: "0x1" });
const b = buildReputationBreakdown(l, "A");
if ("error" in b) return assert.fail("expected breakdown");
const penalty = b.dimensions.find((d) => d.dimension === "collusion_penalty")!;
assert.ok(penalty.contribution < 0);
});
test("reputation breakdown: unknown agent → error", () => {
const l = new Ledger();
assert.ok("error" in buildReputationBreakdown(l, "Ghost"));
});