// Generated by bynkc — do not edit by hand.
// test target: shortener.analytics

import { Ok, Err, Some, None, makeTestState, type Result, type Option, type ValidationError } from "../runtime.js";
import * as shortener_analytics from "./../shortener/analytics.js";
import * as shortener_core from "./../shortener/core.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_Hits_key_reads_count_as_0() {
  try {
    shortener_analytics.__resetAgents();
    const deps = makeTestDeps();
    const { AnalyticsError, Hits, __makeHits, count, track } = shortener_analytics as any;
    const { ShortCode, Url } = shortener_core as any;
    void (await (async (__d) => {
        switch (__d.tag) {
          case "Err": {
            return __bynkAssert((false), "offset 1357", 1357, 1362);
          }
          case "Ok": {
            const code = __d.value;
            const reading = await count.call(code, deps);
            switch (reading.tag) {
              case "Ok": {
                const total = reading.value;
                return __bynkAssert((total === 0), "offset 1457", 1457, 1467);
              }
              case "Err": {
                return __bynkAssert((false), "offset 1492", 1492, 1497);
              }
            }
            throw new Error("non-exhaustive match");
          }
        }
        throw new Error("non-exhaustive match");
      })(ShortCode.of("fresh1")));
    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_recording_a_hit_returns_a_running_total() {
  try {
    shortener_analytics.__resetAgents();
    const deps = makeTestDeps();
    const { AnalyticsError, Hits, __makeHits, count, track } = shortener_analytics as any;
    const { ShortCode, Url } = shortener_core as any;
    void (await (async (__d) => {
        switch (__d.tag) {
          case "Err": {
            return __bynkAssert((false), "offset 1613", 1613, 1618);
          }
          case "Ok": {
            const code = __d.value;
            const result = await track.call(code, deps);
            switch (result.tag) {
              case "Ok": {
                const total = result.value;
                return __bynkAssert((total === 1), "offset 1711", 1711, 1721);
              }
              case "Err": {
                return __bynkAssert((false), "offset 1746", 1746, 1751);
              }
            }
            throw new Error("non-exhaustive match");
          }
        }
        throw new Error("non-exhaustive match");
      })(ShortCode.of("abc123")));
    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_two_hits_on_the_same_code_accumulate() {
  try {
    shortener_analytics.__resetAgents();
    const deps = makeTestDeps();
    const { AnalyticsError, Hits, __makeHits, count, track } = shortener_analytics as any;
    const { ShortCode, Url } = shortener_core as any;
    void (await (async (__d) => {
        switch (__d.tag) {
          case "Err": {
            return __bynkAssert((false), "offset 1864", 1864, 1869);
          }
          case "Ok": {
            const code = __d.value;
            const __r0 = await track.call(code, deps);
            const second = await track.call(code, deps);
            switch (second.tag) {
              case "Ok": {
                const total = second.value;
                return __bynkAssert((total === 2), "offset 1991", 1991, 2001);
              }
              case "Err": {
                return __bynkAssert((false), "offset 2026", 2026, 2031);
              }
            }
            throw new Error("non-exhaustive match");
          }
        }
        throw new Error("non-exhaustive match");
      })(ShortCode.of("abc123")));
    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_count_reads_back_the_current_total_without_incrementing() {
  try {
    shortener_analytics.__resetAgents();
    const deps = makeTestDeps();
    const { AnalyticsError, Hits, __makeHits, count, track } = shortener_analytics as any;
    const { ShortCode, Url } = shortener_core as any;
    void (await (async (__d) => {
        switch (__d.tag) {
          case "Err": {
            return __bynkAssert((false), "offset 2163", 2163, 2168);
          }
          case "Ok": {
            const code = __d.value;
            const __r0 = await track.call(code, deps);
            const reading = await count.call(code, deps);
            switch (reading.tag) {
              case "Ok": {
                const total = reading.value;
                return __bynkAssert((total === 1), "offset 2292", 2292, 2302);
              }
              case "Err": {
                return __bynkAssert((false), "offset 2327", 2327, 2332);
              }
            }
            throw new Error("non-exhaustive match");
          }
        }
        throw new Error("non-exhaustive match");
      })(ShortCode.of("zzz999")));
    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 Hits key reads count as 0", ...(await test_a_fresh_Hits_key_reads_count_as_0()) });
  results.push({ name: "recording a hit returns a running total", ...(await test_recording_a_hit_returns_a_running_total()) });
  results.push({ name: "two hits on the same code accumulate", ...(await test_two_hits_on_the_same_code_accumulate()) });
  results.push({ name: "count reads back the current total without incrementing", ...(await test_count_reads_back_the_current_total_without_incrementing()) });
  return results;
}
