// 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("WSPR entry forwards decoded locators at their absolute frequency", async () => {
  const forwarded = [];
  const window = {
    ...createHost(),
    ft8BaseHz: 14_095_600,
    trxUi: { confirm: async () => true },
    mapAddLocator: (...args) => { forwarded.push(args); },
  };
  const context = vm.createContext({
    window,
    document: { getElementById: () => null, createDocumentFragment: () => ({ appendChild() {} }) },
    setInterval() { return 1; },
    Date,
    Number,
    String,
    Set,
    Element: class Element {},
    console,
  });
  const runtime = await bundleEntry(new URL("../src/plugin-runtime.ts", import.meta.url));
  const source = await bundleEntry(new URL("../src/plugins/wspr.ts", import.meta.url));
  new vm.Script(runtime).runInContext(context);
  new vm.Script(source).runInContext(context);

  window.trxPluginRuntime.dispatch("wspr", { message: "SP0ABC JO91 37", freq_hz: 1_420, ts_ms: Date.now() });
  assert.equal(forwarded.length, 1);
  assert.equal(forwarded[0][1][0], "JO91");
  assert.equal(forwarded[0][4].freq_hz, 14_097_020);
});
