// 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("VDES entry normalizes and forwards positioned server messages", async () => {
  const forwarded = [];
  const window = {
    ...createHost(),
    trxUi: { confirm: async () => true },
    vdesMapAddPoint: (message) => { forwarded.push(message); },
  };
  const context = vm.createContext({
    window,
    document: { getElementById: () => null },
    Date,
    Number,
    String,
    Array,
    console,
  });
  const runtime = await bundleEntry(new URL("../src/plugin-runtime.ts", import.meta.url));
  const source = await bundleEntry(new URL("../src/plugins/vdes.ts", import.meta.url));
  new vm.Script(runtime).runInContext(context);
  new vm.Script(source).runInContext(context);

  window.trxPluginRuntime.dispatch("vdes", { callsign: "SP0ABC", lat: 54.5, lon: 18.5, bit_len: 120, ts_ms: Date.now() });
  assert.equal(window.onServerVdes, undefined);
  assert.equal(forwarded.length, 1);
  assert.equal(forwarded[0].callsign, "SP0ABC");
  assert.equal(forwarded[0].rig_id, null);
  assert.equal(typeof forwarded[0]._tsMs, "number");
});
