// 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 { bundleEntry } from "./bundle-entry.mjs";
import { createHost } from "./host-fixture.mjs";

test("APRS entry normalizes positioned packets without remote symbol assets", async () => {
  const forwarded = [];
  const window = {
    ...createHost(),
    trxUi: { confirm: async () => true },
    aprsMapAddStation: (...args) => { forwarded.push(args); },
  };
  const context = vm.createContext({
    window,
    document: { getElementById: () => null, querySelectorAll: () => [] },
    navigator: {},
    Date,
    Number,
    String,
    Array,
    Set,
    Reflect,
    console,
  });
  const runtime = await bundleEntry(new URL("../src/plugin-runtime.ts", import.meta.url));
  const source = await bundleEntry(new URL("../src/plugins/aprs.ts", import.meta.url));
  assert.equal(source.includes("raw.githubusercontent.com"), false);
  new vm.Script(runtime).runInContext(context);
  new vm.Script(source).runInContext(context);

  window.trxPluginRuntime.dispatch("aprs", { src_call: "SP0ABC", dest_call: "APRS", crc_ok: true, lat: 54.5, lon: 18.5, symbol_table: "/", symbol_code: ">" });
  assert.equal(window.onServerAprs, undefined);
  assert.equal(forwarded.length, 1);
  assert.equal(forwarded[0][0], "SP0ABC");
  assert.equal(forwarded[0][1], 54.5);
  assert.equal(forwarded[0][6].rig_id, null);
});

// Distance is computed from the receiver position and the great-circle helper,
// both of which the entry reads through the host contract.  These used to be
// bare window properties that the module graph stopped publishing, so the
// distance column rendered empty for every positioned packet.
test("APRS distance text uses the receiver position from the host contract", async () => {
  const distanceArgs = [];
  const host = createHost({
    state: { serverLat: 54.0, serverLon: 18.0 },
    core: {
      haversineKm: (lat1, lon1, lat2, lon2) => {
        distanceArgs.push([lat1, lon1, lat2, lon2]);
        return 61.7;
      },
    },
  });
  const window = { ...host, trxUi: { confirm: async () => true }, aprsMapAddStation: () => {} };
  const node = () => ({
    innerHTML: "", textContent: "", className: "", style: {}, dataset: {},
    appendChild(child) { return child; }, addEventListener() {}, replaceChildren() {},
    querySelector: () => null, querySelectorAll: () => [], setAttribute() {},
    classList: { add() {}, toggle() {} },
  });
  const context = vm.createContext({
    window,
    document: {
      getElementById: (id) => (id === "aprs-packets" ? node() : null),
      querySelectorAll: () => [],
      createElement: node,
      createDocumentFragment: node,
    },
    navigator: {},
    Date, Number, String, Array, Set, Reflect, console,
  });
  const runtime = await bundleEntry(new URL("../src/plugin-runtime.ts", import.meta.url));
  const source = await bundleEntry(new URL("../src/plugins/aprs.ts", import.meta.url));
  new vm.Script(runtime).runInContext(context);
  new vm.Script(source).runInContext(context);

  window.trxPluginRuntime.dispatch("aprs", {
    src_call: "SP0ABC", dest_call: "APRS", crc_ok: true, lat: 54.5, lon: 18.5,
    symbol_table: "/", symbol_code: ">",
  });

  assert.deepEqual(distanceArgs, [[54.0, 18.0, 54.5, 18.5]]);
});
