// 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 vm from "node:vm";
import { build } from "esbuild";

async function loadRoutes(window) {
  const result = await build({
    entryPoints: [new URL("../src/features/navigation/routes.ts", import.meta.url).pathname],
    bundle: true,
    format: "cjs",
    platform: "browser",
    target: "es2022",
    write: false,
  });
  const module = { exports: {} };
  vm.runInNewContext(result.outputFiles[0].text, { module, exports: module.exports, window });
  return module.exports;
}

test("all top-level tabs have stable round-trip routes", async () => {
  const calls = [];
  const window = {
    location: { pathname: "/", search: "?rig=one", hash: "#panel" },
    history: {
      pushState: (...args) => calls.push(["push", ...args]),
      replaceState: (...args) => calls.push(["replace", ...args]),
    },
  };
  const routes = await loadRoutes(window);
  for (const name of routes.TAB_ORDER) {
    assert.equal(routes.tabFromPath(routes.TAB_PATHS[name]), name);
  }
  routes.updateTabHistory("statistics");
  assert.equal(calls[0][0], "push");
  assert.equal(calls[0][3], "/statistics?rig=one#panel");
});
