/*
 * render-format-extension.test.ts
 *
 * Copyright (C) 2020-2022 Posit Software, PBC
 */

// TODO re-enable the ACM tests once the template has been updated

// This test file focuses on rendering documents with journal extensions.
// It tests HTML formats, PDF formats, and format variants (e.g., elsevier-pdf+foobar).
// Note: extension-render-journals.test.ts tests template scaffolding (quarto use template).
// Both files serve different purposes and should remain separate.

import { safeRemoveSync } from "../../../src/core/path.ts";
import { docs } from "../../utils.ts";
import { quarto } from "../../../src/quarto.ts";

import { testRender } from "./render.ts";

// Update extensions to latest versions before testing
// Falls back to committed versions on network failure
//
// Maintenance note: Committed extension files serve as fallback for offline/CI
// failure scenarios. They don't need frequent updates since tests always fetch
// latest versions. When extension-related issues occur, consider updating the
// committed files to document the working version (see commits c3149a9ba and
// b8421caf9 for example).
const updateExtensions = async () => {
  try {
    console.log("Updating quarto-journals extensions to latest versions...");
    const wd = Deno.cwd();
    Deno.chdir(docs("extensions/format/academic"));

    for (const repo of ["acs", "elsevier"]) {
      await quarto([
        "update",
        "extension",
        `quarto-journals/${repo}`,
        "--no-prompt",
      ]);
    }

    Deno.chdir(wd);
    console.log("Extensions updated successfully");
  } catch (error) {
    const message = error instanceof Error ? error.message : String(error);
    console.warn(`Failed to update extensions: ${message}`);
    console.warn("Falling back to committed extension versions");
  }
};

// Some HTML tests
testRender(docs("extensions/format/academic/document.qmd"), "html", false);
// testRender(docs("extensions/format/academic/document.qmd"), "acm-html", false);
testRender(
  docs("extensions/format/academic/document.qmd"),
  "acs-html",
  false,
  [],
  { setup: updateExtensions },
);
testRender(
  docs("extensions/format/academic/document.qmd"),
  "elsevier-html",
  false,
  [],
  { setup: updateExtensions },
);

// some PDF tests
testRender(docs("extensions/format/academic/document.qmd"), "pdf", true);
// testRender(
//   docs("extensions/format/academic/document.qmd"),
//   "acm-pdf",
//   true,
//   [],
//   {
//     teardown: async () => {
//       await Deno.remove(docs("extensions/format/academic/sensys-abstract.cls"));
//       await Deno.remove(
//         docs("extensions/format/academic/acm_proc_article-sp.cls"),
//       );
//     },
//   },
// );
testRender(
  docs("extensions/format/academic/document.qmd"),
  "acs-pdf",
  true,
  [],
  {
    setup: updateExtensions,
    // deno-lint-ignore require-await
    teardown: async () => {
      // Clean up files generated by the acs extension
      safeRemoveSync(docs("extensions/format/academic/acs-document.bib"));
      safeRemoveSync(docs("extensions/format/academic/achemso.bst"));
    },
  },
);
testRender(
  docs("extensions/format/academic/document.qmd"),
  "elsevier-pdf",
  true,
  [],
  {
    setup: updateExtensions,
    // deno-lint-ignore require-await
    teardown: async () => {
      // Clean up files generated by the elsevier extension
      safeRemoveSync(docs("extensions/format/academic/document.spl"));
      safeRemoveSync(docs("extensions/format/academic/elsarticle.cls"));
      safeRemoveSync(docs("extensions/format/academic/elsarticle-num.bst"));
    },
  },
);

// Funky format string test
testRender(
  docs("extensions/format/academic/document.qmd"),
  "elsevier-pdf+foobar",
  true,
  [],
  {
    setup: updateExtensions,
    // deno-lint-ignore require-await
    teardown: async () => {
      // Clean up files generated by the elsevier extension
      safeRemoveSync(docs("extensions/format/academic/document+foobar.spl"));
      safeRemoveSync(docs("extensions/format/academic/elsarticle.cls"));
      safeRemoveSync(docs("extensions/format/academic/elsarticle-num.bst"));
    },
  },
);
