// 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("satellite entry registers lifecycle callbacks and forwards georeferenced images", async () => {
  const overlays = [];
  const window = {
    ...createHost(),
    trxUi: { confirm: async () => true },
    addSatMapOverlay: (image) => { overlays.push(image); },
  };
  const context = vm.createContext({
    window,
    document: { getElementById: () => null, createDocumentFragment: () => ({ appendChild() {} }) },
    fetch: async () => ({ ok: true, json: async () => ({ satellite_count: 0, passes: [] }) }),
    setInterval: () => 1,
    clearInterval() {},
    Date,
    Number,
    String,
    Array,
    Error,
    console,
  });
  const runtime = await bundleEntry(new URL("../src/plugin-runtime.ts", import.meta.url));
  const source = await bundleEntry(new URL("../src/plugins/sat.ts", import.meta.url));
  new vm.Script(runtime).runInContext(context);
  new vm.Script(source).runInContext(context);

  window.trxPluginRuntime.dispatch("lrpt_image", { path: "/images/lrpt.png", geo_bounds: [50, 10, 55, 20], mcu_count: 100 });
  assert.equal(overlays.length, 1);
  assert.deepEqual(overlays[0].geo_bounds, [50, 10, 55, 20]);
  assert.equal(typeof window.updateSatLiveState, "function");
  assert.equal(window.onServerLrptImage, undefined);
});
