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

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 demo_api from "../workers/demo-api/handlers.js";
import worker_demo_api from "../workers/demo-api/index.js";
import * as demo_counter from "../workers/demo-counter/handlers.js";
import worker_demo_counter from "../workers/demo-counter/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_demo_api: any = {};
  const env_demo_counter: any = {};
  env_demo_api.DEMO_COUNTER = { fetch: (req: Request) => worker_demo_counter.fetch(req, env_demo_counter) } as ServiceBinding;
  env_demo_counter.COUNTER = makeIntegrationDoNamespace((state) => new demo_counter.Counter(state));
  const rootEnv: any = {};
  rootEnv.DEMO_API = { fetch: (req: Request) => worker_demo_api.fetch(req, env_demo_api) } as ServiceBinding;
  rootEnv.DEMO_COUNTER = { fetch: (req: Request) => worker_demo_counter.fetch(req, env_demo_counter) } as ServiceBinding;
  return { env: rootEnv };
}

async function test_two_ticks_on_the_same_id_accumulate_across_the_wire() {
  try {
    const deps = makeHarness();
    const a = await callService(deps.env.DEMO_API, "tick", "c1" as JsonValue, demo_api.deserialise_Result_Int_ApiError, "integration");
    const b = await callService(deps.env.DEMO_API, "tick", "c1" as JsonValue, demo_api.deserialise_Result_Int_ApiError, "integration");
    void (((__d) => {
        switch (__d.tag) {
          case "Ok": {
            const n = __d.value;
            return __bynkAssert((n === 2), "offset 239", 239, 245);
          }
          case "Err": {
            return __bynkAssert((false), "offset 269", 269, 274);
          }
        }
        throw new Error("non-exhaustive match");
      })(b));
    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_fresh_id_starts_from_zero_in_a_new_case() {
  try {
    const deps = makeHarness();
    const a = await callService(deps.env.DEMO_API, "tick", "c2" as JsonValue, demo_api.deserialise_Result_Int_ApiError, "integration");
    void (((__d) => {
        switch (__d.tag) {
          case "Ok": {
            const n = __d.value;
            return __bynkAssert((n === 1), "offset 409", 409, 415);
          }
          case "Err": {
            return __bynkAssert((false), "offset 439", 439, 444);
          }
        }
        throw new Error("non-exhaustive match");
      })(a));
    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: "two ticks on the same id accumulate across the wire", ...(await test_two_ticks_on_the_same_id_accumulate_across_the_wire()) });
  results.push({ name: "a fresh id starts from zero in a new case", ...(await test_a_fresh_id_starts_from_zero_in_a_new_case()) });
  return results;
}
