cred402/test/portfolio.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

56 lines
2.3 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 { cspr } from "../lib/core/units.js";
import { buildPortfolioReport } from "../lib/services/portfolio.js";
function setup() {
const ledger = new Ledger();
const econ = new Cred402Economy(ledger);
econ.bootstrap();
return { ledger, econ };
}
test("portfolio: empty book is diversified with zero HHI", () => {
const { ledger } = setup();
const r = buildPortfolioReport(ledger);
assert.equal(r.hhi, 0);
assert.equal(r.concentration_band, "diversified");
assert.equal(r.largest_borrower, null);
assert.equal(r.by_agent.length, 0);
});
test("portfolio: a single drawn line is 100% concentrated (HHI 10000)", () => {
const { ledger } = setup();
ledger.pool.deposit_liquidity(cspr(1000), "lp1");
const agentId = ledger.agents.list()[0]!.agent_id;
ledger.pool.open_credit_line({ agent_id: agentId, max_credit: cspr(100), interest_rate_bps: 800, origination_fee_bps: 50, term_seconds: 86_400 });
ledger.pool.draw(agentId, cspr(40));
const r = buildPortfolioReport(ledger);
assert.equal(r.hhi, 10000);
assert.equal(r.concentration_band, "concentrated");
assert.equal(r.largest_borrower!.key, agentId);
assert.equal(r.largest_borrower!.share_bps, 10000);
assert.equal(r.outstanding_motes, cspr(40).toString());
});
test("portfolio: spreading exposure across borrowers lowers HHI", () => {
const { ledger } = setup();
ledger.pool.deposit_liquidity(cspr(5000), "lp1");
const agents = ledger.agents.list().slice(0, 4);
assert.ok(agents.length >= 2, "need multiple agents to diversify");
for (const a of agents) {
ledger.pool.open_credit_line({ agent_id: a.agent_id, max_credit: cspr(100), interest_rate_bps: 800, origination_fee_bps: 50, term_seconds: 86_400 });
ledger.pool.draw(a.agent_id, cspr(40)); // equal $40 each → maximally diversified for N names
}
const r = buildPortfolioReport(ledger);
// N equal names → HHI = 10000 / N
const expected = Math.round(10000 / agents.length);
assert.ok(Math.abs(r.hhi - expected) <= agents.length, `HHI ${r.hhi} ~ ${expected}`);
assert.ok(r.hhi < 10000);
assert.equal(r.by_agent.length, agents.length);
});