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

40 lines
1.8 KiB
TypeScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { Ledger } from "../lib/ledger/index.js";
import { simulateUnderwriting } from "../lib/services/credit_simulator.js";
test("simulator: more revenue → a larger credit line, monotonically", () => {
const ledger = new Ledger();
const low = simulateUnderwriting(ledger, { monthly_revenue_cspr: 1000, reputation: 80 });
const high = simulateUnderwriting(ledger, { monthly_revenue_cspr: 8000, reputation: 80 });
assert.ok(high.estimated_credit_line_cspr >= low.estimated_credit_line_cspr);
assert.ok(low.decision.credit_line >= 0n);
});
test("simulator: reputation below the governance floor is ineligible", () => {
const ledger = new Ledger();
const gov = ledger.governance.get();
const r = simulateUnderwriting(ledger, { monthly_revenue_cspr: 500, reputation: gov.min_reputation_to_draw - 5 });
assert.equal(r.eligible, false);
assert.match(r.ineligible_reason ?? "", /below minimum/);
});
test("simulator: the governance exposure cap bounds the estimate", () => {
const ledger = new Ledger();
const gov = ledger.governance.get();
const huge = simulateUnderwriting(ledger, { monthly_revenue_cspr: 10_000_000, stake_cspr: 1_000_000, reputation: 99, accuracy: 100 });
assert.equal(huge.decision.credit_line, gov.max_agent_exposure);
assert.equal(huge.governance_capped, true);
});
test("simulator: is read-only — no agent or credit line is created", () => {
const ledger = new Ledger();
const agentsBefore = ledger.agents.list().length;
const linesBefore = ledger.pool.list().length;
simulateUnderwriting(ledger, { monthly_revenue_cspr: 5000, reputation: 90 });
assert.equal(ledger.agents.list().length, agentsBefore);
assert.equal(ledger.pool.list().length, linesBefore);
assert.equal(ledger.agents.get("__simulation__"), undefined);
});