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

import { hwlabNodeWebProbeHelp, hwlabNodeWebProbeScreenshotHelp } from "../hwlab-node-help";
import { parseNodeWebProbeOptions } from "./web-probe";

test("web-probe help promotes reusable Workbench debug and product Trace commands", () => {
  const help = hwlabNodeWebProbeHelp();
  const examples = Array.isArray(help.examples) ? help.examples.join("\n") : "";
  const notes = Array.isArray(help.notes) ? help.notes.join("\n") : "";
  assert.match(examples, /--type validateWorkbenchKafkaDebugReplay/u);
  assert.match(examples, /--type validateWorkbenchTraceReadability/u);
  assert.match(notes, /validateWorkbenchTraceReadability/u);
  assert.match(notes, /排除隔离调试面板/u);
});

test("web-probe screenshot scoped help explains semantic, custom and local URL usage", () => {
  const help = hwlabNodeWebProbeScreenshotHelp();
  const usage = Array.isArray(help.usage) ? help.usage.join("\n") : "";
  const notes = Array.isArray(help.notes) ? help.notes.join("\n") : "";
  assert.match(usage, /--origin internal --path \/workbench/u);
  assert.match(usage, /--url 'https:\/\/custom\.example\/workbench'/u);
  assert.match(usage, /--url 'http:\/\/127\.0\.0\.1:4173\/workbench'/u);
  assert.match(notes, /--url.*不要再传 --path/u);
  assert.match(notes, /持续 SSE.*domcontentloaded/u);
  assert.equal(help.mutation, false);
});

test("web-probe screenshot defaults to DOM readiness and keeps network idle explicit", () => {
  const defaults = parseNodeWebProbeOptions(["screenshot", "--node", "NC01", "--lane", "v03", "--origin", "native"]);
  assert.equal(defaults.action, "screenshot");
  if (defaults.action !== "screenshot") throw new Error("expected screenshot options");
  assert.equal(defaults.waitUntil, "domcontentloaded");

  const explicit = parseNodeWebProbeOptions(["screenshot", "--node", "NC01", "--lane", "v03", "--origin", "native", "--wait-until", "networkidle"]);
  assert.equal(explicit.action, "screenshot");
  if (explicit.action !== "screenshot") throw new Error("expected screenshot options");
  assert.equal(explicit.waitUntil, "networkidle");
});
