/**
 * x402 registry contract tests
 *
 *  - Every route in the typed registry SHOULD appear in .well-known/x402.json
 *  - Every priced route MUST carry price, asset, network, payee, x402Version
 *  - No two routes may share the same (method, path)
 *  - Every route MUST declare protocolVersion, contractStatus, finality, dataSource
 *  - V2 routes must not be silently reclassified as V3
 *  - Deprecated routes must include deprecatedAt and sunsetAt
 *  - Every priced route must be reachable in the mocked facilitator sandbox
 *
 * This test suite is CI-required; a red suite blocks the pilot enable.
 */

import { describe, it, expect } from "vitest";
import { registry } from "../../registry/routes";

describe("x402 registry", () => {
  it("has unique (method, path)", () => {
    const seen = new Set<string>();
    for (const r of registry.routes) {
      const k = `${r.method} ${r.path}`;
      expect(seen.has(k)).toBe(false);
      seen.add(k);
    }
  });

  it("priced routes carry required fields", () => {
    for (const r of registry.routes) {
      if (!r.free) {
        expect(r.price).toBeDefined();
        expect(r.price!.amount).toMatch(/^\d+$/);
        expect(r.price!.network).not.toEqual("");
        expect(r.price!.payee).toMatch(/^0x[0-9a-fA-F]{40}$/);
        expect(r.price!.x402Version).not.toEqual("");
      }
    }
  });

  it("deprecated routes carry sunset", () => {
    for (const r of registry.routes) {
      if (r.contractStatus === "deprecated") {
        expect(r.deprecatedAt).toBeDefined();
        expect(r.sunsetAt).toBeDefined();
      }
    }
  });

  it("V2 routes stay V2", () => {
    for (const r of registry.routes) {
      if (r.path.startsWith("/api/v3")) expect(r.version).toEqual("v3");
      if (r.path.startsWith("/api/") && !r.path.startsWith("/api/v3")) expect(r.version).toEqual("v2");
    }
  });
});
