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

test("virtual channels expose typed SSE and interception boundaries", async () => {
  const window = { ...createHost() };
  const context = vm.createContext({
    window,
    document: { getElementById: () => null },
    fetch: async () => ({ ok: true, json: async () => ({ connected_sessions: 1, released_sessions: 0 }) }),
    setInterval: () => 1,
    clearInterval() {},
    setTimeout,
    Date,
    Number,
    String,
    Array,
    Set,
    JSON,
    Error,
    console,
  });
  const source = await bundleEntry(new URL("../src/plugins/vchan.ts", import.meta.url));
  new vm.Script(source).runInContext(context);

  const service = window.trx.modules.vchan;
  assert.equal(typeof service.handleSession, "function");
  assert.equal(typeof service.handleChannels, "function");
  assert.equal(typeof service.applyCapabilities, "function");
  assert.equal(await service.interceptMode("USB"), false);
  assert.equal(await service.interceptBandwidth(2400), false);
  // No virtual channel is active, so tuning stays with the physical rig.
  assert.equal(service.interceptFrequency(14_074_000), false);
  assert.equal(window.vchanHandleSession, undefined);
  assert.equal(window.vchanInterceptBandwidth, undefined);
});
