kog-fee-verify / tree / test/gate.test.ts ยท raw (this view has old GitHub links rewritten for the mirror; raw is byte-exact)
import { test } from "node:test";
import assert from "node:assert/strict";
import { createRegistry } from "./registry.js";
import { load, PYUSD, X402 } from "./fixtures.js";

const ROUTES = ["/offers", "/jobs/7c61c8b7/bids"];
function setup(amount = X402.amount) {
  const reg = createRegistry({ mint: PYUSD, payee: X402.payee });
  reg.quotes.set("q1", { amount, expires_at: X402.blockTime + 300 });
  return reg;
}

for (const path of ROUTES) {
  test(`A1 gate ${path}: refused without a fee`, () => {
    const reg = setup();
    for (const body of [{ item: {} }, { item: {}, fee: {} }, { item: {}, fee: { quote_id: "q1" } }, { item: {}, fee: { quote_id: "nope", tx: load(X402.name) } }]) {
      const r = reg.handle("POST", path, body);
      assert.equal(r.status, 402);
    }
    assert.equal(reg.offers.length + reg.bids.length, 0);
  });

  test(`A2 gate ${path}: refused with a short fee`, () => {
    const reg = setup("50000000");
    const r = reg.handle("POST", path, { item: {}, fee: { quote_id: "q1", tx: load(X402.name) } });
    assert.deepEqual([r.status, r.body.reason], [402, "short_fee"]);
  });

  test(`A3 gate ${path}: refused with a reused fee`, () => {
    const reg = setup();
    const tx = load(X402.name);
    assert.equal(reg.handle("POST", path, { item: { n: 1 }, fee: { quote_id: "q1", tx } }).status, 201);
    const again = reg.handle("POST", path, { item: { n: 2 }, fee: { quote_id: "q1", tx } });
    assert.deepEqual([again.status, again.body.reason], [402, "replay"]);
    // reuse across routes is refused too
    const other = ROUTES.find((p) => p !== path)!;
    assert.equal(reg.handle("POST", other, { item: {}, fee: { quote_id: "q1", tx } }).body.reason, "replay");
  });

  test(`A4 gate ${path}: accepted with a valid fee`, () => {
    const reg = setup();
    const r = reg.handle("POST", path, { item: { ok: true }, fee: { quote_id: "q1", tx: load(X402.name) } });
    assert.equal(r.status, 201);
    assert.equal(reg.seen.size, 1);
    assert.equal(path === "/offers" ? reg.offers.length : reg.bids.length, 1);
  });
}

agentgate-source: code mirror on Cloudflare Pages. All repos