cred402/test/discovery.test.ts
caelum0x 2d43618725 feat: real integrations (Casper Testnet, x402/EIP-712, MCP, RealFi, cross-chain), wallet sign-in, branding
Replace the design-note docs (p1–p7) with the implemented "make it real" phase
docs (p8–p10) and wire production-grade, env-gated real integrations:

- p8 Casper Testnet: byte-exact deploys + WASM install via casper-js-sdk; live
  node reads (node.testnet.casper.network); CRED402_CHAIN=sim|testnet transport.
- p9 x402 + data + MCP: real EIP-712 typed-data digests (@casper-ecosystem/
  casper-eip-712); casper-x402 facilitator client (x402 V2); live Open-Meteo GHI;
  official @modelcontextprotocol/sdk server; casper-sidecar SSE event feed.
- p10 RealFi + cross-chain: real Stripe test-mode webhooks (HMAC-verified) and
  Plaid sandbox → privacy-preserving on-chain envelopes; Base Sepolia EVM deploy
  (Foundry); reachable via /api/realfi/* and /api/x402/facilitator routes.
- Sign-in with Casper Wallet: real extension provider + challenge/ed25519/session
  flow (/v1/auth/wallet/*), console connect + sign-in button.
- Branding: Cred402 logo mark, favicon, OG image; Vercel config for the console.

All env-gated with sim/free-API fallbacks; full suite green, all toolchains
(TS/Rust/Go/Solidity) build.

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

61 lines
2.5 KiB
TypeScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { Ledger } from "../lib/ledger/index.js";
import { Cred402Economy } from "../agents/economy.js";
import { AttestationGraph } from "../lib/services/attestation_graph.js";
import { discoverAgents } from "../lib/services/discovery.js";
function setup() {
const ledger = new Ledger();
const econ = new Cred402Economy(ledger);
econ.bootstrap();
const attestations = new AttestationGraph(ledger);
return { ledger, econ, attestations };
}
test("discovery: ranks agents by composite score, clamped 0..100, sorted desc", () => {
const { ledger, attestations } = setup();
const res = discoverAgents(ledger, attestations, {});
assert.ok(res.results.length > 0);
// ranks are dense and 1-based
res.results.forEach((r, i) => assert.equal(r.rank, i + 1));
// scores in range and monotonically non-increasing
for (let i = 1; i < res.results.length; i++) {
const prev = res.results[i - 1]!;
const cur = res.results[i]!;
assert.ok(cur.score >= 0 && cur.score <= 100);
assert.ok(prev.score >= cur.score);
}
});
test("discovery: a vouch lifts the target's trust component and score", () => {
const { ledger, attestations } = setup();
const agents = ledger.agents.list();
// pick a high-rep agent as attester and a different agent as target
const attester = agents.find((a) => a.reputation_score >= 60);
const target = agents.find((a) => a.agent_id !== attester?.agent_id);
assert.ok(attester && target, "need two agents");
const before = discoverAgents(ledger, attestations, {}).results.find((r) => r.agent_id === target!.agent_id)!;
attestations.attest(attester!.agent_id, target!.agent_id, "vouch");
const after = discoverAgents(ledger, attestations, {}).results.find((r) => r.agent_id === target!.agent_id)!;
assert.ok(after.trust_score > before.trust_score);
assert.ok(after.vouches > before.vouches);
assert.ok(after.score >= before.score);
});
test("discovery: service_type and min_reputation filters apply", () => {
const { ledger, attestations } = setup();
const sample = ledger.agents.list()[0]!;
const filtered = discoverAgents(ledger, attestations, { service_type: sample.service_type });
assert.ok(filtered.results.every((r) => r.service_type === sample.service_type));
const high = discoverAgents(ledger, attestations, { min_reputation: 200 });
assert.equal(high.count, 0); // nobody has reputation >= 200
const limited = discoverAgents(ledger, attestations, { limit: 1 });
assert.ok(limited.results.length <= 1);
});