<script>
  import { onMount } from 'svelte';
  import Layout from '$/admin/Layout.svelte';
  import api from "$/client/api.js";
  import assert from "$/client/assert.js";
  import { random_numbers, defer } from "$/client/helpers.js";
  import { log } from "$/client/logging.js";

  // just get some random numbers to run that function
  const r = random_numbers(2);
  assert(r.length == 2, "Whoops, random_numbers failed.");

  // this is a mock on purpose to test the mock code
  api.mock({
    "/fake/test": {
      "get": [500, {"message": "Failure."}],
    }
  });

  const assert_promise = defer("assert promise");
  const bad_promise = defer("bad promise");

  onMount(async () => {
    const [status, data] = await api.get("/fake/test");

    bad_promise.reject(new Error("Bad worked."));

    try {
      assert(status === 200, "Fake test call failed (it should).");
    } catch(error) {
      log.error(error, "Assert worked!");
      assert_promise.resolve("resolve-message");
    }
  });
</script>

<Layout testid="page-admin-test" centered={ true }>
  <h1>Additional Tests</h1>

  <callout class="info">
  <span>
    <b>NOTE:</b> This page basically does nothing except provide access to
   things for the unit test in tests/ui/admin_tests.js.  It has a mock
   on purpose to test that they keep working, and does asserts as well.
   You can delete this if you also delete the test.
  </span>
  </callout>

  {#await assert_promise}
    <p data-testid="no-assert">Waiting for assert.</p>
  {:then msg}
    <p data-testid="assert-works">Assert is working. Good job! Message: { msg }</p>
  {/await}

  {#await bad_promise}
    <p data-testid="bad-await">This should error.</p>
  {:then}
    <p data-testid="bad-failed">OOPS! This shouldn't run.</p>
  {:catch error}
    <p data-testid="bad-worked">Defer failure worked: { error }</p>
  {/await}

</Layout>
