// 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("WEFAX entry exposes typed lifecycle handlers and renders decoder state", async () => {
  const status = { textContent: "", style: { color: "" } };
  const window = { ...createHost() };
  const context = vm.createContext({
    window,
    document: { getElementById: (id) => id === "wefax-status" ? status : null },
    Date,
    Number,
    String,
    Uint8Array,
    console,
  });
  const runtime = await bundleEntry(new URL("../src/plugin-runtime.ts", import.meta.url));
  const source = await bundleEntry(new URL("../src/plugins/wefax.ts", import.meta.url));
  new vm.Script(runtime).runInContext(context);
  new vm.Script(source).runInContext(context);

  assert.equal(window.onServerWefaxProgress, undefined);
  assert.equal(window.trxPluginRuntime.hasDecoder("wefax_progress"), true);
  window.trxPluginRuntime.dispatch("wefax_progress", { state: "Scanning 12 MHz" });
  assert.equal(status.textContent, "Scanning 12 MHz");
  assert.equal(status.style.color, "var(--text-accent)");
});
