cred402/test/x402_stats.test.ts
caelum0x 51a56a66c7 feat(deploy): Render Blueprint for the API + Vercel rewrites to it
- render.yaml: always-on web service `cred402-api` (node runtime, installs dev
  deps so tsx is present, health check /v1/health) — Render runs the long-lived
  node:http + SSE server the serverless Vercel deploy can't.
- api/server.ts: honor the platform-provided PORT (Render) before CRED402_PORT.
- frontend/vercel.json: rewrite /api/*, /v1/*, /graphql → https://cred402-api.onrender.com
  so the deployed console reaches the backend same-origin (no CORS, no code change).

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

48 lines
1.6 KiB
TypeScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { Ledger } from "../lib/ledger/index.js";
import { cspr } from "../lib/core/units.js";
import { buildX402Stats } from "../lib/services/x402_stats.js";
function pay(l: Ledger, payer: string, seller: string, amount: bigint, n: number) {
l.receipts.record_receipt({
payer_agent: payer,
seller_agent: seller,
service_type: "weather_risk",
amount,
rwa_reference_hash: "0xr",
result_hash: `0x${n}`,
payment_proof_hash: `0xp${n}`,
nonce: `n${n}`,
expires_at: l.clock.now() + 300,
});
}
test("x402 stats: empty network is zeroed", () => {
const s = buildX402Stats(new Ledger());
assert.equal(s.total_receipts, 0);
assert.equal(s.total_volume_motes, "0");
assert.equal(s.finalization_rate, 0);
});
test("x402 stats: aggregates volume, top counterparties and per-service", () => {
const l = new Ledger();
pay(l, "Buyer", "TopSeller", cspr(5), 1);
pay(l, "Buyer", "TopSeller", cspr(3), 2);
pay(l, "Buyer2", "SmallSeller", cspr(1), 3);
const s = buildX402Stats(l);
assert.equal(s.total_receipts, 3);
assert.equal(s.total_volume_motes, cspr(9).toString());
assert.equal(s.avg_receipt_motes, cspr(3).toString());
// top seller by volume
assert.equal(s.top_sellers[0]!.agent_id, "TopSeller");
assert.equal(s.top_sellers[0]!.volume_motes, cspr(8).toString());
// top payer
assert.equal(s.top_payers[0]!.agent_id, "Buyer");
// by service
const weather = s.by_service.find((x) => x.service_type === "weather_risk")!;
assert.equal(weather.receipts, 3);
});