import { readFile, writeFile } from "node:fs/promises";
import { expect } from "e2e";
import { APP_DIR } from "../e2e.config.ts";
import { test, WEB } from "./t3.ts";

test("Windows PowerShell command row shows the inner command", WEB, async ({ t3, app, screen }) => {
  const file = `${APP_DIR}/apps/server/scripts/dev-replay/command_output_transcript.ndjson`;
  const original = await readFile(file, "utf8");
  const command = `"C:\\Program Files\\PowerShell\\7\\pwsh.exe" -Command "Get-Date -Format 'yyyy-MM-dd HH:mm:ss'"`;
  const recording = original.trim().split("\n").map(line => {
    const event = JSON.parse(line);
    const update = event.frame?.params?.update;
    if (update?.sessionUpdate === "tool_call") {
      update.title = command;
      update.rawInput = { command };
    }
    return JSON.stringify(event);
  }).join("\n") + "\n";
  await writeFile(file, recording);
  let thread;
  try {
    const project = await t3.project("Command display demo");
    thread = await t3.thread({ project, title: "Windows command display", model: "command-output", messages: ["Run Get-Date"] });
  } finally {
    await writeFile(file, original);
  }
  await t3.open(thread);
  await screen.getByRole("button", /^Worked for/).tap();
  await screen.getByText(/Syncing messages/).waitFor({ state: "hidden" });
  await app.screenshot("powershell-command-row");
  await expect(screen.getByText("Get-Date -Format 'yyyy-MM-dd HH:mm:ss'", { exact: true })).toBeVisible();
});
