// Generated by bynkc — do not edit by hand.
// test target: demo.counter

import { Ok, Err, Some, None, makeTestState, type Result, type Option, type ValidationError } from "../runtime.js";
import * as demo_counter from "./../demo/counter.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 makeTestDeps() {
  return {  };
}

async function test_a_fresh_counter_reads_zero() {
  try {
    demo_counter.__resetAgents();
    const deps = makeTestDeps();
    const { Counter, CounterError, CounterId, __makeCounter, bump, read } = demo_counter as any;
    void (await (async (__d) => {
        switch (__d.tag) {
          case "Err": {
            return __bynkAssert((false), "offset 229", 229, 234);
          }
          case "Ok": {
            const id = __d.value;
            const total = await read.call(id, deps);
            switch (total.tag) {
              case "Ok": {
                const n = total.value;
                return __bynkAssert((n === 0), "offset 317", 317, 323);
              }
              case "Err": {
                return __bynkAssert((false), "offset 345", 345, 350);
              }
            }
            throw new Error("non-exhaustive match");
          }
        }
        throw new Error("non-exhaustive match");
      })(CounterId.of("c1")));
    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_bumping_twice_accumulates() {
  try {
    demo_counter.__resetAgents();
    const deps = makeTestDeps();
    const { Counter, CounterError, CounterId, __makeCounter, bump, read } = demo_counter as any;
    void (await (async (__d) => {
        switch (__d.tag) {
          case "Err": {
            return __bynkAssert((false), "offset 448", 448, 453);
          }
          case "Ok": {
            const id = __d.value;
            const __r0 = await bump.call(id, deps);
            const second = await bump.call(id, deps);
            switch (second.tag) {
              case "Ok": {
                const n = second.value;
                return __bynkAssert((n === 2), "offset 564", 564, 570);
              }
              case "Err": {
                return __bynkAssert((false), "offset 592", 592, 597);
              }
            }
            throw new Error("non-exhaustive match");
          }
        }
        throw new Error("non-exhaustive match");
      })(CounterId.of("c2")));
    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: "a fresh counter reads zero", ...(await test_a_fresh_counter_reads_zero()) });
  results.push({ name: "bumping twice accumulates", ...(await test_bumping_twice_accumulates()) });
  return results;
}
