"use strict";

const { getSubprocessImplForTest } = ChromeUtils.importESModule(
  "resource://gre/modules/Subprocess.sys.mjs"
);

let PYTHON;
const TEST_SCRIPT = do_get_file("data_test_script.py").path;

add_setup(async () => {
  PYTHON = await Subprocess.pathSearch(Services.env.get("PYTHON"));
});

// When the last process exits, we should stop polling.
// This is a regression test for bug 1982950
add_task(async function test_polling_only_when_process_is_running() {
  let worker = getSubprocessImplForTest().Process.getWorker();

  equal(
    await worker.call("getIsPolling", []),
    false,
    "Initially not polling before starting a program"
  );

  let proc = await Subprocess.call({
    command: await getRealPythonExecutable(PYTHON),
    arguments: ["-u", TEST_SCRIPT, "close_pipes_and_wait_for_stdin"],
  });

  equal(
    await worker.call("getIsPolling", []),
    true,
    "Is polling while process is active"
  );

  // Make sure that we have completed reading before exiting the program,
  // to rule out pending polls from open stdout pipe. Note that we expect
  // to be closed because the program closed stdout prematurely.
  equal(
    await proc.stdout.readString(),
    "",
    "Test program should have closed stdout prematurely without stdout"
  );

  equal(
    await worker.call("getIsPolling", []),
    true,
    "Is still polling while process is active"
  );

  info("Closing stdin to trigger exit");
  await proc.stdin.close();

  let { exitCode } = await proc.wait();
  equal(exitCode, 0, "Got expected exit code");

  // This part is the regression test for bug 1982950.
  equal(
    await worker.call("getIsPolling", []),
    false,
    "Not polling when last process has exited"
  );
});
