Files
agents-marketplace/tests/unit/tx-reclaim.test.ts
jelisaveta-talbot cbab81efbd M1-F: end-to-end LLM marketplace lifecycle on Vector testnet
Bonded-escrow marketplace for prompt → response between buyers and
local-LLM suppliers, settling on Vector (UTxO) via Aiken Plutus V3
validators. Lifecycle PostEscrow → Claim → Submit → Accept proven on
Vector testnet 2026-05-06; AcceptEscrow tx 125e3cfe…7dc100d distributes
payment + bonds per the validator's distribution rule.

Workspace layout (pnpm + TypeScript):
  contracts/marketplace/      Aiken validators (advert + escrow) + plutus.json
  packages/shared/            Tx-builders (lucid-evolution), CBOR codecs,
                              chain providers (mock / read-only / live Ogmios)
  supplier/                   OpenAI-compatible HTTP node (Ollama by default;
                              vLLM / llama.cpp / OpenRouter / OpenClaw all
                              swap-in via OLLAMA_URL); 202+poll chat handler
  buyer/                      Express + React SPA; server-side runs the full
                              chain lifecycle via /v1/submit-prompt and
                              /v1/accept; SPA never holds the buyer key
  indexer/                    Chain-follower (extends apex-dashboard pattern),
                              SSE event stream + REST queries
  indexer-ui/                 Operator dashboard (events + suppliers + escrow
                              lookup)
  tests/                      1098 vitest unit + integration tests (10 skipped
                              under M1-F-D-defer for UPLC test plumbing)
  deploy/testnet/             Per-service docker-compose for the Hetzner stack
  docs/EXECUTIVE_SUMMARY.*    Marp deck (PDF + PPTX) — 11 slides, mermaid
                              diagrams pre-rendered for portable export
  docs/CLOUDFLARE_WAF_BYPASS.md  CF Skip-rule runbook for /v1/* paths
  docs/ARCHITECTURE.md        canonical spec

Live demo (testnet, 2026-05-06):
  https://mp-buyers.vector.testnet.apexfusion.org      (buyer SPA)
  https://mp-suppliers.vector.testnet.apexfusion.org   (supplier API)
  https://mp-indexer.vector.testnet.apexfusion.org     (indexer + UI)
2026-05-06 13:27:48 +02:00

181 lines
7.3 KiB
TypeScript

/**
* tx-reclaim.test.ts — RED phase tests for buildReclaimTx() (Open|Claimed → Reclaimed, terminal)
*
* Tests mirror escrow.ak:handle_reclaim invariants:
* - signed by buyer_pkh
* - old state ∈ {Open, Claimed}
* - buyer receives ≥ payment + buyer_bond + supplier_bond
* - validity lower-bound ≥ deliver_by
* - terminal: no continuing escrow output
*/
import { describe, it, expect, beforeEach } from "vitest";
import { MockChainProvider } from "../../packages/shared/src/chain/MockChainProvider.js";
import { buildReclaimTx } from "../../packages/shared/src/tx/escrow/reclaim.js";
import { TxConstructionError } from "../../packages/shared/src/tx/types.js";
import { buildBuyerWalletKey } from "../fixtures/buyer-side/wallet-keys.js";
import { buildSupplierWalletKey } from "../fixtures/supplier-side/wallet-keys.js";
import {
buildOpenEscrowUtxo,
buildClaimedEscrowUtxo,
buildSubmittedEscrowUtxo,
DELIVER_BY,
PAYMENT_LOVELACE,
BUYER_BOND,
SUPPLIER_BOND,
} from "../fixtures/buyer-side/sample-escrow-utxos.js";
// ─── Happy path — from Open ───────────────────────────────────────────────────
describe("buildReclaimTx() — happy path from Open state", () => {
let chain: MockChainProvider;
beforeEach(() => {
chain = new MockChainProvider();
// Tip is past deliver_by
chain.advanceSlot(Math.floor((DELIVER_BY + 60_000) / 1000));
chain.seed(buildOpenEscrowUtxo());
});
it("returns a non-empty txCborHex", async () => {
const buyer = buildBuyerWalletKey();
const utxo = buildOpenEscrowUtxo();
const result = await buildReclaimTx({ chain, buyerKey: buyer, escrowRef: utxo.ref });
expect(typeof result.txCborHex).toBe("string");
expect(result.txCborHex.length).toBeGreaterThan(0);
});
it("buyer receives >= payment + buyer_bond + supplier_bond", async () => {
const buyer = buildBuyerWalletKey();
const utxo = buildOpenEscrowUtxo();
await buildReclaimTx({ chain, buyerKey: buyer, escrowRef: utxo.ref });
const buyerUtxos = await chain.queryUtxosByAddress(buyer.address);
const total = buyerUtxos.reduce((sum, u) => sum + u.lovelace, 0n);
expect(total).toBeGreaterThanOrEqual(PAYMENT_LOVELACE + BUYER_BOND + SUPPLIER_BOND);
});
it("validity lower-bound is >= deliver_by (after the deadline)", async () => {
const buyer = buildBuyerWalletKey();
const utxo = buildOpenEscrowUtxo();
// No throw = builder constructed valid tx with correct time range
await expect(
buildReclaimTx({ chain, buyerKey: buyer, escrowRef: utxo.ref }),
).resolves.toBeDefined();
});
it("escrow UTxO is spent (not present after reclaim)", async () => {
const buyer = buildBuyerWalletKey();
const utxo = buildOpenEscrowUtxo();
await buildReclaimTx({ chain, buyerKey: buyer, escrowRef: utxo.ref });
const spent = await chain.queryUtxo(utxo.ref);
expect(spent).toBeNull();
});
});
// ─── Happy path — from Claimed ────────────────────────────────────────────────
describe("buildReclaimTx() — happy path from Claimed state", () => {
it("succeeds when state is Claimed (not just Open)", async () => {
const buyer = buildBuyerWalletKey();
const chain = new MockChainProvider();
chain.advanceSlot(Math.floor((DELIVER_BY + 60_000) / 1000));
const utxo = buildClaimedEscrowUtxo();
chain.seed(utxo);
await expect(
buildReclaimTx({ chain, buyerKey: buyer, escrowRef: utxo.ref }),
).resolves.toBeDefined();
});
});
// ─── Rejection: before deliver_by ────────────────────────────────────────────
describe("buildReclaimTx() — rejects before deliver_by", () => {
it("throws TxConstructionError when chain tip is before deliver_by", async () => {
const buyer = buildBuyerWalletKey();
const chain = new MockChainProvider();
// Tip is BEFORE deliver_by
chain.advanceSlot(Math.floor((DELIVER_BY - 60_000) / 1000));
chain.seed(buildOpenEscrowUtxo());
const utxo = buildOpenEscrowUtxo();
await expect(
buildReclaimTx({ chain, buyerKey: buyer, escrowRef: utxo.ref }),
).rejects.toThrow(TxConstructionError);
});
it("TxConstructionError.reason is 'reclaim before deliver_by'", async () => {
const buyer = buildBuyerWalletKey();
const chain = new MockChainProvider();
chain.advanceSlot(Math.floor((DELIVER_BY - 60_000) / 1000));
chain.seed(buildOpenEscrowUtxo());
const utxo = buildOpenEscrowUtxo();
let caught: unknown;
try {
await buildReclaimTx({ chain, buyerKey: buyer, escrowRef: utxo.ref });
} catch (e) {
caught = e;
}
expect(caught).toBeInstanceOf(TxConstructionError);
expect((caught as TxConstructionError).reason).toBe("reclaim before deliver_by");
});
});
// ─── Rejection: non-buyer signer ─────────────────────────────────────────────
describe("buildReclaimTx() — rejects non-buyer signer", () => {
it("throws TxConstructionError when signed by supplier", async () => {
const supplier = buildSupplierWalletKey();
const chain = new MockChainProvider();
chain.advanceSlot(Math.floor((DELIVER_BY + 60_000) / 1000));
chain.seed(buildOpenEscrowUtxo());
const utxo = buildOpenEscrowUtxo();
await expect(
buildReclaimTx({ chain, buyerKey: supplier, escrowRef: utxo.ref }),
).rejects.toThrow(TxConstructionError);
});
it("TxConstructionError.reason is 'buyer signature mismatch'", async () => {
const supplier = buildSupplierWalletKey();
const chain = new MockChainProvider();
chain.advanceSlot(Math.floor((DELIVER_BY + 60_000) / 1000));
chain.seed(buildOpenEscrowUtxo());
const utxo = buildOpenEscrowUtxo();
let caught: unknown;
try {
await buildReclaimTx({ chain, buyerKey: supplier, escrowRef: utxo.ref });
} catch (e) {
caught = e;
}
expect(caught).toBeInstanceOf(TxConstructionError);
expect((caught as TxConstructionError).reason).toBe("buyer signature mismatch");
});
});
// ─── Rejection: wrong state (Submitted) ──────────────────────────────────────
describe("buildReclaimTx() — rejects Submitted state", () => {
it("throws TxConstructionError when state is Submitted (not Open or Claimed)", async () => {
const buyer = buildBuyerWalletKey();
const chain = new MockChainProvider();
chain.advanceSlot(Math.floor((DELIVER_BY + 60_000) / 1000));
const utxo = buildSubmittedEscrowUtxo(); // Submitted — reclaim not allowed
chain.seed(utxo);
await expect(
buildReclaimTx({ chain, buyerKey: buyer, escrowRef: utxo.ref }),
).rejects.toThrow(TxConstructionError);
});
});
// ─── Distribution sanity ─────────────────────────────────────────────────────
describe("buildReclaimTx() — distribution sanity", () => {
it("buyer is owed payment + buyer_bond + supplier_bond = 4_000_000 lovelace", () => {
expect(PAYMENT_LOVELACE + BUYER_BOND + SUPPLIER_BOND).toBe(4_000_000n);
});
});