// Generated by bynkc — do not edit by hand.
// test target: commerce.orders

import { Ok, Err, Some, None, type Result, type Option, type ValidationError } from "../runtime.js";
import * as commerce_orders from "./../commerce/orders.js";
import * as commerce_payment from "./../commerce/payment.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); }
}

class MockPayment {
  async authorise(amount: number): Promise<Result<commerce_payment.AuthId, commerce_payment.PaymentError>> {
    const { AuthId, PaymentError } = commerce_payment as any;
    return (amount > 0 ? Ok(AuthId.unsafe("MOCK-AUTH")) : Err(PaymentError.Declined));
  }
}

function makeTestDeps() {
  return { surface: { Payment: new MockPayment() } };
}

async function test_place_succeeds_for_positive_amounts() {
  try {
    const deps = makeTestDeps();
    const { OrderError, place } = commerce_orders as any;
    const { AuthId, PaymentError } = commerce_payment as any;
    const Payment = (deps as any).surface?.Payment;
    const r = await place.call(50, deps);
    if (!(r.tag === "Ok")) { throw __bynkAssertionFailure("tests/orders.test.bynk:14:12", 330, 340); }
    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_place_fails_for_zero() {
  try {
    const deps = makeTestDeps();
    const { OrderError, place } = commerce_orders as any;
    const { AuthId, PaymentError } = commerce_payment as any;
    const Payment = (deps as any).surface?.Payment;
    const r = await place.call(0, deps);
    if (!(r.tag === "Err")) { throw __bynkAssertionFailure("tests/orders.test.bynk:19:12", 416, 427); }
    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: "place succeeds for positive amounts", ...(await test_place_succeeds_for_positive_amounts()) });
  results.push({ name: "place fails for zero", ...(await test_place_fails_for_zero()) });
  return results;
}
