// SPDX-FileCopyrightText: 2026 Stan Grams <sjg@haxx.space>
//
// SPDX-License-Identifier: GPL-2.0-or-later

import assert from "node:assert/strict";
import test from "node:test";
import { readFile } from "node:fs/promises";

const indexPath = new URL("../../assets/web/index.html", import.meta.url);
const pluginLoaderPath = new URL("../src/plugin-loader.ts", import.meta.url);
const mapCorePath = new URL("../src/map-core.ts", import.meta.url);

test("index loads one first-party application bootstrap", async () => {
  const html = await readFile(indexPath, "utf8");
  assert.equal((html.match(/<script[^>]+src="\/(?!vendor\/)[^"]+\.js"/g) ?? []).length, 1);
  assert.match(html, /src="\/app\.js"/);
  assert.doesNotMatch(html, /src="\/(?:ui-core|plugin-runtime|plugin-loader|webgl-renderer|leaflet-ais-tracksymbol)\.js"/);
  assert.equal(html.includes("var pluginScripts"), false);
});

test("startup has no remote script or stylesheet dependencies", async () => {
  const html = await readFile(indexPath, "utf8");
  const assetReferences = Array.from(
    html.matchAll(/<(?:script|link)\b[^>]+(?:src|href)="([^"]+)"/g),
    (match) => match[1],
  );
  assert.equal(
    assetReferences.some((reference) => /^https?:\/\//i.test(reference)),
    false,
  );
});

test("lazy frontend features use modules and local map symbols", async () => {
  const [loader, map] = await Promise.all([
    readFile(pluginLoaderPath, "utf8"),
    readFile(mapCorePath, "utf8"),
  ]);
  assert.equal(loader.includes("createElement(\"script\")"), false);
  assert.match(loader, /import\(path\)/);
  assert.match(map, /aprs-symbol-local/);
  assert.equal(map.includes("raw.githubusercontent.com"), false);
});
