Files
agents-marketplace/tests/unit/lucid-context.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

127 lines
5.2 KiB
TypeScript

/**
* lucid-context.test.ts — RED phase tests for createLucidContext()
*
* Verifies that the factory returns a usable LucidEvolution instance with the
* wallet selected from the private key and the correct network configuration.
*
* All Ogmios calls are mocked via injected fetch so no real node is required.
*
* M1-F-4 RED — tests fail until Catherine implements createLucidContext.
*/
import { describe, it, expect, vi, beforeEach } from "vitest";
import {
OgmiosLucidProvider,
} from "../../packages/shared/src/chain/OgmiosLucidProvider.js";
import { createLucidContext } from "../../packages/shared/src/tx/internal/lucidContext.js";
import { VECTOR_TESTNET } from "../../packages/shared/src/network.js";
import {
buildBuyerWalletKey,
BUYER_PRIVATE_KEY_HEX,
} from "../fixtures/buyer-side/wallet-keys.js";
// ─── Mock fetch for protocol-params bootstrap ─────────────────────────────────
const mockFetch = vi.fn();
beforeEach(() => {
mockFetch.mockReset();
// Protocol-params call that Lucid() issues on construction
mockFetch.mockResolvedValue({
ok: true,
json: async () => ({
jsonrpc: "2.0",
result: {
minFeeCoefficient: 44,
minFeeConstant: { ada: { lovelace: 155381 } },
maxTransactionSize: { bytes: 16384 },
maxValueSize: { bytes: 5000 },
stakeCredentialDeposit: { ada: { lovelace: 2000000 } },
stakePoolDeposit: { ada: { lovelace: 500000000 } },
prices: { memory: "0.0577", steps: "0.0000721" },
maxExecutionUnitsPerTransaction: { memory: 14000000, cpu: 10000000000 },
coinsPerUtxoByte: { ada: { lovelace: 4310 } },
collateralPercentage: 150,
maxCollateralInputs: 3,
plutusCostModels: { "plutus:v1": {}, "plutus:v2": {}, "plutus:v3": {} },
monetaryExpansion: "0.003",
treasuryExpansion: "0.2",
minStakePoolCost: { ada: { lovelace: 340000000 } },
minFeeReferenceScripts: { base: 15 },
governanceActionDeposit: { ada: { lovelace: 100000000000 } },
delegateRepresentativeDeposit: { ada: { lovelace: 500000000 } },
},
}),
});
});
// ─── Tests ────────────────────────────────────────────────────────────────────
describe("createLucidContext()", () => {
it("returns an object with lucid and networkParams keys", async () => {
const provider = new OgmiosLucidProvider({ ogmiosUrl: "http://localhost:1337", fetch: mockFetch });
const buyer = buildBuyerWalletKey();
const ctx = await createLucidContext(provider, buyer, VECTOR_TESTNET);
expect(ctx).toHaveProperty("lucid");
expect(ctx).toHaveProperty("networkParams");
});
it("lucid.newTx() returns a TxBuilder (not null/undefined)", async () => {
const provider = new OgmiosLucidProvider({ ogmiosUrl: "http://localhost:1337", fetch: mockFetch });
const buyer = buildBuyerWalletKey();
const { lucid } = await createLucidContext(provider, buyer, VECTOR_TESTNET);
expect(lucid.newTx).toBeDefined();
expect(typeof lucid.newTx).toBe("function");
const txBuilder = lucid.newTx();
expect(txBuilder).toBeDefined();
});
it("wallet is selected (lucid.wallet() does not throw)", async () => {
const provider = new OgmiosLucidProvider({ ogmiosUrl: "http://localhost:1337", fetch: mockFetch });
const buyer = buildBuyerWalletKey();
const { lucid } = await createLucidContext(provider, buyer, VECTOR_TESTNET);
// After fromPrivateKey, wallet() should be accessible
expect(() => lucid.wallet()).not.toThrow();
});
it("returns networkParams matching the input networkParams", async () => {
const provider = new OgmiosLucidProvider({ ogmiosUrl: "http://localhost:1337", fetch: mockFetch });
const buyer = buildBuyerWalletKey();
const ctx = await createLucidContext(provider, buyer, VECTOR_TESTNET);
expect(ctx.networkParams).toBe(VECTOR_TESTNET);
});
it("throws when privateKeyHex is not 64 hex chars (invalid length)", async () => {
const provider = new OgmiosLucidProvider({ ogmiosUrl: "http://localhost:1337", fetch: mockFetch });
const badKey = { ...buildBuyerWalletKey(), privateKeyHex: "deadbeef" };
await expect(
createLucidContext(provider, badKey, VECTOR_TESTNET),
).rejects.toThrow();
});
});
describe("createLucidContext() — network ID mapping", () => {
it("Vector testnet (networkId=0) maps to Mainnet for lucid (as per genesis)", async () => {
// ARCHITECTURE decision: Vector testnet uses Cardano mainnet network ID 0.
// Lucid's "Mainnet" corresponds to networkId=0 in CIP-0019 address encoding.
// This test verifies the factory wires the correct lucid network string.
const provider = new OgmiosLucidProvider({ ogmiosUrl: "http://localhost:1337", fetch: mockFetch });
const buyer = buildBuyerWalletKey();
const { lucid } = await createLucidContext(provider, buyer, VECTOR_TESTNET);
// lucid.config() exposes the network string; "Mainnet" = networkId 0
const cfg = lucid.config();
expect(cfg.network).toBe("Mainnet");
});
});