// Generated by bynkc — do not edit by hand.
// integration test: checkout

import { Ok, Err, Some, None, callService, type Result, type Option, type ValidationError, type JsonError, type JsonValue, type BoundaryError, type ServiceBinding } from "../runtime.js";
import * as shop_orders from "../workers/shop-orders/handlers.js";
import worker_shop_orders from "../workers/shop-orders/index.js";
import * as shop_payment from "../workers/shop-payment/handlers.js";
import worker_shop_payment from "../workers/shop-payment/index.js";

class AssertionError extends Error {
  location: string;
  start: number;
  end: number;
  constructor(location: string, start: number, end: number) {
    super(`assertion failed at ${location}`);
    this.location = location;
    this.start = start;
    this.end = end;
  }
}
function __bynkAssertionFailure(location: string, start: number, end: number) {
  return new AssertionError(location, start, end);
}
function __bynkAssert(cond: boolean, location: string, start: number, end: number): void {
  if (!cond) { throw __bynkAssertionFailure(location, start, end); }
}

function makeHarness() {
  const env_shop_orders: any = {};
  const env_shop_payment: any = {};
  env_shop_orders.SHOP_PAYMENT = { fetch: (req: Request) => worker_shop_payment.fetch(req, env_shop_payment) } as ServiceBinding;
  const rootEnv: any = {};
  rootEnv.SHOP_ORDERS = { fetch: (req: Request) => worker_shop_orders.fetch(req, env_shop_orders) } as ServiceBinding;
  rootEnv.SHOP_PAYMENT = { fetch: (req: Request) => worker_shop_payment.fetch(req, env_shop_payment) } as ServiceBinding;
  return { env: rootEnv };
}

async function test_small_order_authorises_across_the_wire() {
  try {
    const deps = makeHarness();
    const r = await callService(deps.env.SHOP_ORDERS, "place", 100 as JsonValue, shop_orders.deserialise_Result_Int_OrderError, "integration");
    if (!(r.tag === "Ok")) { throw __bynkAssertionFailure("checkout.bynk:11:12", 358, 368); }
    return { pass: true };
  } catch (e) {
    if (e instanceof AssertionError) {
      return { pass: false, error: { message: e.message, location: e.location } };
    }
    return { pass: false, error: { message: String(e), location: "unknown" } };
  }
}

async function test_large_order_is_rejected_end_to_end() {
  try {
    const deps = makeHarness();
    const r = await callService(deps.env.SHOP_ORDERS, "place", 50000 as JsonValue, shop_orders.deserialise_Result_Int_OrderError, "integration");
    if (!(r.tag === "Err")) { throw __bynkAssertionFailure("checkout.bynk:16:12", 469, 480); }
    return { pass: true };
  } catch (e) {
    if (e instanceof AssertionError) {
      return { pass: false, error: { message: e.message, location: e.location } };
    }
    return { pass: false, error: { message: String(e), location: "unknown" } };
  }
}

export async function run() {
  const results = [];
  results.push({ name: "small order authorises across the wire", ...(await test_small_order_authorises_across_the_wire()) });
  results.push({ name: "large order is rejected end to end", ...(await test_large_order_is_rejected_end_to_end()) });
  return results;
}
