Files
agents-marketplace/tests/unit/supplier-routes-status.test.ts
david 2f7b0194a5 feat(supplier,gateway,buyer): N-slot chat suppliers + entry-ticket settle mode
Two structural fixes for demo concurrency (3 single-slot suppliers thrashed
under Open WebUI's ~3 conversations per message):

Multi-slot suppliers (MAX_CHAT_SESSIONS, default 1):
- SupplierState is now capacity-aware: a Set of active escrow refs with
  free-until-full status semantics, ref-scoped release(ref) (also fixes the
  latent bug where handler catch-alls released another request's slot), and
  a walletMutex serializing every wallet-spending chain op — Claim, Submit,
  and wallet-health consolidation queue instead of racing the single
  working/collateral UTxO. /status gains active_sessions/max_sessions.

Entry-ticket settle mode (CHAT_SETTLE_MODE=ticket, default full, both sides):
- The buyer's escrow post at session open is the ONLY chain op. The supplier
  verifies the Open escrow as an entry ticket but never Claims/Submits
  (/v1/chat/start → {status:"ticket"}; /v1/chat/end → receipt-less
  {status:"ended"}); no hard-cap timer, so sessions may outlive deliver_by.
  Used tickets can't reopen sessions (records retained; 409 session_exists).
- Buyer SDK: startChat/endChat are settle-mode aware, driven by the
  supplier's response — mixed-mode rollout windows settle correctly.
- Gateway: ticket closes bill cost 0; the sweeper Reclaims Open tickets
  after deliver_by WITHOUT closing live sessions (the conversation and its
  affinity mirror survive their reclaimed ticket), gates reclaims on
  deliver_by, and keeps an in-process settled-refs memory so terminal
  indexer rows aren't retried every sweep forever.

Deployment (kimi-chat trio + gateway): ticket mode, 8 slots per supplier,
idle policy gateway 10 min / supplier 15 min (fixes the inversion where the
supplier idle-closed before the gateway janitor).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-09 20:48:52 +02:00

155 lines
5.7 KiB
TypeScript

/**
* supplier-routes-status.test.ts — RED phase tests for GET /status
*
* Uses supertest against createApp(deps) from supplier/src/server.ts.
* Injects a real SupplierState to control status transitions.
*
* Covers:
* - 200 in all states (free / working / offline)
* - Response shape: { status, last_seen }
* - current_escrow_ref only present when working
* - Never touches chain (MockChainProvider with no UTxOs)
*/
import { describe, it, expect } from "vitest";
import request from "supertest";
import type { Application } from "express";
import { MockChainProvider } from "../../packages/shared/src/chain/MockChainProvider.js";
import { SupplierState } from "../../supplier/src/state.js";
import { createApp } from "../../supplier/src/server.js";
import { buildSampleConfig } from "../fixtures/supplier-side/sample-config.js";
import { buildSupplierWalletKey } from "../fixtures/supplier-side/wallet-keys.js";
const ESCROW_REF = `${"f".repeat(64)}#0`;
function makeApp(state: SupplierState): Application {
return createApp({
chain: new MockChainProvider(), // empty — /status must NOT touch chain
state,
config: buildSampleConfig(),
supplierKey: buildSupplierWalletKey(),
});
}
// ─── Always 200 ──────────────────────────────────────────────────────────────
describe("GET /status — always 200", () => {
it("returns 200 when status is free", async () => {
const state = new SupplierState();
const res = await request(makeApp(state)).get("/status");
expect(res.status).toBe(200);
});
it("returns 200 when status is working", async () => {
const state = new SupplierState();
state.tryAcquire(ESCROW_REF);
const res = await request(makeApp(state)).get("/status");
expect(res.status).toBe(200);
});
it("returns 200 when status is offline", async () => {
const state = new SupplierState();
state.markOffline();
const res = await request(makeApp(state)).get("/status");
expect(res.status).toBe(200);
});
});
// ─── Response shape ──────────────────────────────────────────────────────────
describe("GET /status — response body when free", () => {
it("body.status is 'free'", async () => {
const state = new SupplierState();
const res = await request(makeApp(state)).get("/status");
expect(res.body.status).toBe("free");
});
it("body.last_seen is an ISO timestamp", async () => {
const state = new SupplierState();
const res = await request(makeApp(state)).get("/status");
expect(res.body.last_seen).toBeTruthy();
expect(new Date(res.body.last_seen).getTime()).not.toBeNaN();
});
it("body does NOT contain current_escrow_ref when free", async () => {
const state = new SupplierState();
const res = await request(makeApp(state)).get("/status");
expect(res.body.current_escrow_ref).toBeUndefined();
});
});
describe("GET /status — response body when working", () => {
it("body.status is 'working'", async () => {
const state = new SupplierState();
state.tryAcquire(ESCROW_REF);
const res = await request(makeApp(state)).get("/status");
expect(res.body.status).toBe("working");
});
it("body.current_escrow_ref matches the acquired escrow ref", async () => {
const state = new SupplierState();
state.tryAcquire(ESCROW_REF);
const res = await request(makeApp(state)).get("/status");
expect(res.body.current_escrow_ref).toBe(ESCROW_REF);
});
it("body.last_seen is present when working", async () => {
const state = new SupplierState();
state.tryAcquire(ESCROW_REF);
const res = await request(makeApp(state)).get("/status");
expect(new Date(res.body.last_seen).getTime()).not.toBeNaN();
});
});
describe("GET /status — response body when offline", () => {
it("body.status is 'offline'", async () => {
const state = new SupplierState();
state.markOffline();
const res = await request(makeApp(state)).get("/status");
expect(res.body.status).toBe("offline");
});
it("body does NOT contain current_escrow_ref when offline", async () => {
const state = new SupplierState();
state.markOffline();
const res = await request(makeApp(state)).get("/status");
expect(res.body.current_escrow_ref).toBeUndefined();
});
});
describe("GET /status — session slot fields", () => {
it("reports active_sessions/max_sessions when free", async () => {
const state = new SupplierState();
const res = await request(makeApp(state)).get("/status");
expect(res.body.active_sessions).toBe(0);
expect(res.body.max_sessions).toBe(1);
});
it("stays free with slots remaining and reports the active count", async () => {
const state = new SupplierState(3);
state.tryAcquire(ESCROW_REF);
const res = await request(makeApp(state)).get("/status");
expect(res.body.status).toBe("free");
expect(res.body.active_sessions).toBe(1);
expect(res.body.max_sessions).toBe(3);
expect(res.body.current_escrow_ref).toBeUndefined();
});
});
describe("GET /status — does not touch chain", () => {
it("returns 200 even when chain has no UTxOs seeded", async () => {
// If the handler incorrectly touches chain, it would throw because
// advert UTxO is absent — but /status must not query chain at all.
const state = new SupplierState();
const chain = new MockChainProvider(); // intentionally empty
const app = createApp({
chain,
state,
config: buildSampleConfig(),
supplierKey: buildSupplierWalletKey(),
});
const res = await request(app).get("/status");
expect(res.status).toBe(200);
});
});