import assert from "node:assert/strict";
import { test } from "bun:test";

import { nodeWebObserveRunnerWorkbenchSource } from "./hwlab-node-web-observe-runner-workbench-source";

test("MDTODO observe commands recognize the rewritten workspace contracts", async () => {
  const source = nodeWebObserveRunnerWorkbenchSource();

  for (const testId of [
    "mdtodo-body-read",
    "mdtodo-body-editor",
    "mdtodo-body-save",
    "mdtodo-launch-workbench",
  ]) {
    assert.match(source, new RegExp(testId, "u"));
  }
  for (const legacyTestId of [
    "mdtodo-body-rendered",
    "mdtodo-edit-body",
    "mdtodo-edit-body-save",
    "mdtodo-workbench-launch",
  ]) {
    assert.match(source, new RegExp(legacyTestId, "u"));
  }

  const child = Bun.spawn(["node", "--input-type=module", "--check", "-"], {
    stdin: "pipe",
    stdout: "pipe",
    stderr: "pipe",
  });
  child.stdin.write(source);
  child.stdin.end();
  const [stderr, exitCode] = await Promise.all([
    new Response(child.stderr).text(),
    child.exited,
  ]);
  assert.equal(exitCode, 0, stderr);
});
