/*
 * Copyright 2024 Foster Hangdaan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import mod from "../mod.ts";
import { valibot as v } from "../deps.ts";
import { assertExists, assertInstanceOf } from "@std/assert";
import { assertSnapshot } from "@std/testing/snapshot";
import { getSite } from "./utils.ts";

Deno.test("Error thrown on invalid options", async (t) => {
  await t.step("Invalid server", () => {
    let error;
    try {
      mod({ server: "invalid domain" });
    } catch (err) {
      error = err;
    }
    assertExists(error);
    assertInstanceOf(error, v.ValiError);
  });

  await t.step("Invalid enabledDiagrams", () => {
    let error;
    try {
      mod({ enabledDiagrams: ["invalid diagram"] });
    } catch (err) {
      error = err;
    }
    assertExists(error);
    assertInstanceOf(error, v.ValiError);
  });

  await t.step("Invalid format", () => {
    let error;
    try {
      // @ts-ignore: This is supposed to be an invalid value.
      mod({ format: "invalid" });
    } catch (err) {
      error = err;
    }
    assertExists(error);
    assertInstanceOf(error, v.ValiError);
  });
});

Deno.test("Lume Site", async (t) => {
  const site = getSite();

  site.use(mod());

  // Don't save the site to disk.
  site.addEventListener("beforeSave", () => false);

  await site.build();

  site.pages.sort((a, b) => {
    if (a.outputPath > b.outputPath) {
      return 1;
    } else if (a.outputPath < b.outputPath) {
      return -1;
    } else {
      return 0;
    }
  });

  await assertSnapshot(t, {
    src: site.pages.map((page) => ({
      path: page.src.path,
      ext: page.src.ext,
    })),
    content: site.pages.map((page) => page.content),
  });
});