// 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";

test("plugin runtime queues lazy decoder traffic and flushes in order on registration", async () => {
  const window = {};
  const context = vm.createContext({ window, Map, Error });
  const source = await bundleEntry(new URL("../src/plugin-runtime.ts", import.meta.url));
  new vm.Script(source).runInContext(context);

  assert.equal(window.trxPluginRuntime.dispatch("late", { sequence: 1 }), false);
  assert.equal(window.trxPluginRuntime.restore("late", [{ sequence: 2 }]), false);
  const received = [];
  window.trxPluginRuntime.registerDecoder({
    id: "late",
    onMessage: (message) => { received.push(["message", message.sequence]); },
    restore: (messages) => { received.push(["restore", messages[0].sequence]); },
  });
  assert.deepEqual(received, [["message", 1], ["restore", 2]]);
});
