// 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, makeIntegrationDoNamespace, type DurableObjectState, type DurableObjectNamespace } 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 {
  constructor(public location: string, public start: number, public end: number) {
    super(`assertion failed at ${location}`);
  }
}
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;
  env_shop_orders.LEDGER = makeIntegrationDoNamespace((state) => new shop_orders.Ledger(state));
  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_successful_orders_accumulate_in_the_ledger() {
  try {
    const deps = makeHarness();
    const first = await callService(deps.env.SHOP_ORDERS, "place", { id: "acct-1" as JsonValue, cents: 100 as JsonValue }, shop_orders.deserialise_Result_Int_OrderError, "integration");
    const second = await callService(deps.env.SHOP_ORDERS, "place", { id: "acct-1" as JsonValue, cents: 200 as JsonValue }, shop_orders.deserialise_Result_Int_OrderError, "integration");
    void (((__d) => {
        switch (__d.tag) {
          case "Ok": {
            const n = __d.value;
            return __bynkAssert((n === 2), "offset 557", 557, 563);
          }
          case "Err": {
            return __bynkAssert((false), "offset 587", 587, 592);
          }
        }
        throw new Error("non-exhaustive match");
      })(second));
    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_a_declined_payment_is_rejected_and_does_not_record() {
  try {
    const deps = makeHarness();
    const r = await callService(deps.env.SHOP_ORDERS, "place", { id: "acct-2" as JsonValue, cents: 50000 as JsonValue }, shop_orders.deserialise_Result_Int_OrderError, "integration");
    void (((__d) => {
        switch (__d.tag) {
          case "Err": {
            const Rejected = __d.error;
            return __bynkAssert((true), "offset 758", 758, 762);
          }
          default: {
            return __bynkAssert((false), "offset 793", 793, 798);
          }
        }
        throw new Error("non-exhaustive match");
      })(r));
    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: "successful orders accumulate in the ledger", ...(await test_successful_orders_accumulate_in_the_ledger()) });
  results.push({ name: "a declined payment is rejected and does not record", ...(await test_a_declined_payment_is_rejected_and_does_not_record()) });
  return results;
}
