/**
 * Tests for the web-shim route resolution (Fix #2 — no-extension web app).
 * The shim is a browser IIFE that installs window.chrome + window.NEXUS_WEB_SHIM;
 * the repo is type:module, so we seed globalThis.window then dynamic-import it.
 *
 * Run: node --test chrome-extension/web-shim.test.js
 */

import test from "node:test";
import assert from "node:assert/strict";
import { pathToFileURL } from "node:url";
import path from "node:path";

const dir = path.dirname(new URL(import.meta.url).pathname.replace(/^\/([A-Za-z]:)/, "$1"));
globalThis.window = {};
await import(pathToFileURL(path.join(dir, "web-shim.js")).href);
const SHIM = globalThis.window.NEXUS_WEB_SHIM;

test("shim installed + exposes ROUTES + resolveRoute", () => {
  assert.ok(SHIM && SHIM.ROUTES && typeof SHIM.resolveRoute === "function");
  // It must install a chrome stub so the dashboard's api() works.
  assert.ok(
    globalThis.window.chrome && typeof globalThis.window.chrome.runtime.sendMessage === "function",
  );
});

test("CRM-as-a-source routes (the Composer flows) resolve correctly", () => {
  assert.deepEqual(
    SHIM.resolveRoute("crm-segment", { queryString: "?platform=telegram&limit=3" }),
    { method: "GET", path: "/api/crm/segment?platform=telegram&limit=3" },
  );
  assert.deepEqual(SHIM.resolveRoute("crm-segment-options", { queryString: "?platform=email" }), {
    method: "GET",
    path: "/api/crm/segment/options?platform=email",
  });
  assert.deepEqual(SHIM.resolveRoute("crm-segment-ids", { queryString: "?platform=email" }), {
    method: "GET",
    path: "/api/crm/segment/ids?platform=email",
  });
  assert.deepEqual(
    SHIM.resolveRoute("crm-bulk-campaign", { body: { channel: "telegram", contactIds: [1, 2] } }),
    {
      method: "POST",
      path: "/api/crm/contacts/bulk-campaign",
      body: { channel: "telegram", contactIds: [1, 2] },
    },
  );
});

test("account + shell routes resolve", () => {
  assert.deepEqual(SHIM.resolveRoute("accounts-all", {}), {
    method: "GET",
    path: "/api/accounts/all",
  });
  assert.deepEqual(SHIM.resolveRoute("li-budget", {}), {
    method: "GET",
    path: "/api/linkedin/limits-check",
  });
  assert.deepEqual(SHIM.resolveRoute("settings-get", {}), { method: "GET", path: "/api/settings" });
});

test("missing queryString degrades to a clean path", () => {
  assert.deepEqual(SHIM.resolveRoute("crm-segment", {}), {
    method: "GET",
    path: "/api/crm/segment",
  });
});

test("unmapped actions resolve to null (caller degrades to {})", () => {
  assert.equal(SHIM.resolveRoute("li-start-accept-and-dm-batch-bg", {}), null);
  assert.equal(SHIM.resolveRoute("totally-unknown", {}), null);
});

test("every route entry returns a method + path", () => {
  for (const action of Object.keys(SHIM.ROUTES)) {
    const r = SHIM.resolveRoute(action, { queryString: "", body: {} });
    assert.ok(
      r && typeof r.method === "string" && typeof r.path === "string",
      `${action}: bad route shape`,
    );
    assert.ok(r.path.startsWith("/api/"), `${action}: path must hit /api/`);
  }
});
