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

import { Ok, Err, Some, None, makeTestState, type Result, type Option, type ValidationError } from "../runtime.js";
import * as shortener_links from "./../shortener/links.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); }
}

class MockAnalytics {
  async track(code: shortener_analytics.ShortCode): Promise<Result<number, shortener_analytics.AnalyticsError>> {
    const { AnalyticsError } = shortener_analytics as any;
    return Ok(1);
  }
}

class TestCodeGen {
  async next(): Promise<string> {
    const { CreateLinkRequest, CreatedView, LinkError, ResolveView } = shortener_links as any;
    return "test01";
  }
}

function makeTestDeps() {
  return { CodeGen: new TestCodeGen(), surface: { Analytics: new MockAnalytics() } };
}

async function test_a_fresh_Link_key_resolves_to_NotFound() {
  try {
    shortener_links.__resetAgents();
    const deps = makeTestDeps();
    const { CreateLinkRequest, CreatedView, Link, LinkError, ResolveView, __makeLink, api, create } = shortener_links as any;
    const { ShortCode, Url } = shortener_core as any;
    const { AnalyticsError } = shortener_analytics as any;
    const Analytics = (deps as any).surface?.Analytics;
    void (await (async (__d) => {
        switch (__d.tag) {
          case "Err": {
            return __bynkAssert((false), "offset 1664", 1664, 1669);
          }
          case "Ok": {
            const code = __d.value;
            const link = __makeLink(code);
            const outcome = await link.resolve(deps);
            switch (outcome.tag) {
              case "Ok": {
                return __bynkAssert((false), "offset 1783", 1783, 1788);
              }
              case "Err": {
                const linkError = outcome.error;
                return ((__d) => {
        switch (__d.tag) {
          case "NotFound": {
            return __bynkAssert((true), "offset 1858", 1858, 1862);
          }
          case "AlreadyExists": {
            return __bynkAssert((false), "offset 1892", 1892, 1897);
          }
        }
        throw new Error("non-exhaustive match");
      })(linkError);
              }
            }
            throw new Error("non-exhaustive match");
          }
        }
        throw new Error("non-exhaustive match");
      })(ShortCode.of("fresh2")));
    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_creating_a_link_stores_the_target_and_returns_its_code() {
  try {
    shortener_links.__resetAgents();
    const deps = makeTestDeps();
    const { CreateLinkRequest, CreatedView, Link, LinkError, ResolveView, __makeLink, api, create } = shortener_links as any;
    const { ShortCode, Url } = shortener_core as any;
    const { AnalyticsError } = shortener_analytics as any;
    const Analytics = (deps as any).surface?.Analytics;
    void (await (async (__d) => {
        switch (__d.tag) {
          case "Err": {
            return __bynkAssert((false), "offset 2034", 2034, 2039);
          }
          case "Ok": {
            const code = __d.value;
            return await (async (__d) => {
        switch (__d.tag) {
          case "Err": {
            return __bynkAssert((false), "offset 2112", 2112, 2117);
          }
          case "Ok": {
            const url = __d.value;
            const outcome = await create.call(code, url, deps);
            switch (outcome.tag) {
              case "Ok": {
                return __bynkAssert((true), "offset 2218", 2218, 2222);
              }
              case "Err": {
                return __bynkAssert((false), "offset 2245", 2245, 2250);
              }
            }
            throw new Error("non-exhaustive match");
          }
        }
        throw new Error("non-exhaustive match");
      })(Url.of("https://example.com"));
          }
        }
        throw new Error("non-exhaustive match");
      })(ShortCode.of("test01")));
    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_creating_the_same_code_twice_is_rejected() {
  try {
    shortener_links.__resetAgents();
    const deps = makeTestDeps();
    const { CreateLinkRequest, CreatedView, Link, LinkError, ResolveView, __makeLink, api, create } = shortener_links as any;
    const { ShortCode, Url } = shortener_core as any;
    const { AnalyticsError } = shortener_analytics as any;
    const Analytics = (deps as any).surface?.Analytics;
    void (await (async (__d) => {
        switch (__d.tag) {
          case "Err": {
            return __bynkAssert((false), "offset 2373", 2373, 2378);
          }
          case "Ok": {
            const code = __d.value;
            return await (async (__d) => {
        switch (__d.tag) {
          case "Err": {
            return __bynkAssert((false), "offset 2451", 2451, 2456);
          }
          case "Ok": {
            const url = __d.value;
            const __r0 = await create.call(code, url, deps);
            const second = await create.call(code, url, deps);
            switch (second.tag) {
              case "Ok": {
                return __bynkAssert((false), "offset 2590", 2590, 2595);
              }
              case "Err": {
                const linkError = second.error;
                return ((__d) => {
        switch (__d.tag) {
          case "AlreadyExists": {
            return __bynkAssert((true), "offset 2667", 2667, 2671);
          }
          case "NotFound": {
            return __bynkAssert((false), "offset 2702", 2702, 2707);
          }
        }
        throw new Error("non-exhaustive match");
      })(linkError);
              }
            }
            throw new Error("non-exhaustive match");
          }
        }
        throw new Error("non-exhaustive match");
      })(Url.of("https://example.com"));
          }
        }
        throw new Error("non-exhaustive match");
      })(ShortCode.of("dup001")));
    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_resolving_a_registered_code_returns_its_target() {
  try {
    shortener_links.__resetAgents();
    const deps = makeTestDeps();
    const { CreateLinkRequest, CreatedView, Link, LinkError, ResolveView, __makeLink, api, create } = shortener_links as any;
    const { ShortCode, Url } = shortener_core as any;
    const { AnalyticsError } = shortener_analytics as any;
    const Analytics = (deps as any).surface?.Analytics;
    void (await (async (__d) => {
        switch (__d.tag) {
          case "Err": {
            return __bynkAssert((false), "offset 2843", 2843, 2848);
          }
          case "Ok": {
            const code = __d.value;
            return await (async (__d) => {
        switch (__d.tag) {
          case "Err": {
            return __bynkAssert((false), "offset 2926", 2926, 2931);
          }
          case "Ok": {
            const url = __d.value;
            const __r0 = await create.call(code, url, deps);
            const link = __makeLink(code);
            const outcome = await link.resolve(deps);
            switch (outcome.tag) {
              case "Ok": {
                const target = outcome.value;
                return __bynkAssert((target === url), "offset 3090", 3090, 3103);
              }
              case "Err": {
                return __bynkAssert((false), "offset 3130", 3130, 3135);
              }
            }
            throw new Error("non-exhaustive match");
          }
        }
        throw new Error("non-exhaustive match");
      })(Url.of("https://example.com/page"));
          }
        }
        throw new Error("non-exhaustive match");
      })(ShortCode.of("res001")));
    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 Link key resolves to NotFound", ...(await test_a_fresh_Link_key_resolves_to_NotFound()) });
  results.push({ name: "creating a link stores the target and returns its code", ...(await test_creating_a_link_stores_the_target_and_returns_its_code()) });
  results.push({ name: "creating the same code twice is rejected", ...(await test_creating_the_same_code_twice_is_rejected()) });
  results.push({ name: "resolving a registered code returns its target", ...(await test_resolving_a_registered_code_returns_its_target()) });
  return results;
}
