import { expect } from "e2e";
import { mkdir, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { execFileSync } from "node:child_process";
import { test, WEB } from "./t3.ts";
import { APP_DIR } from "../e2e.config.ts";

test("Windows drive links read existing files through the WSL mount", WEB, async ({ t3, app, screen, browser }) => {
  await browser.setViewport({ width: 1440, height: 1000 });
  const drive = "C:\\Users\\Public\\Documents\\drive-proof";
  const mount = execFileSync("wslpath", ["-u", drive], { encoding: "utf8" }).trim();
  await mkdir(mount, { recursive: true });
  const text = "# Windows drive proof\n\nThis file exists on the Windows C drive, outside the Linux workspace.\n\nREAD_ONLY_CONTENT_OK\n";
  for (const suffix of ["inline", "backslash", "slash", "uri", "control"]) await writeFile(join(mount, `notes-${suffix}.md`), text.replace("READ_ONLY_CONTENT_OK", `READ_ONLY_CONTENT_OK_${suffix}`));
  const reply = [
    "## Windows drive file links",
    `Inline code: \`${drive}\\notes-inline.md\``,
    `Backslashes: [notes-backslash.md](${drive}\\notes-backslash.md)`,
    "Forward slashes: [notes-slash.md](C:/Users/Public/Documents/drive-proof/notes-slash.md)",
    "File URI: [notes-uri.md](file:///C:/Users/Public/Documents/drive-proof/notes-uri.md)",
    `WSL mount control: [notes-control.md](${mount}/notes-control.md)`,
  ].join("\n\n");
  const frames = [
    { type: "expect_outbound", frame: { kind: "request", method: "session/prompt", params: "<any>" } },
    { type: "emit_inbound", frame: { kind: "notification", method: "session/update", params: { sessionId: "dev-replay-session", update: { sessionUpdate: "agent_message_chunk", content: { type: "text", text: reply } } } } },
    { type: "emit_inbound", frame: { kind: "response", method: "session/prompt", result: { stopReason: "end_turn" } } },
  ];
  await writeFile(join(APP_DIR, "apps/server/scripts/dev-replay/showcase_transcript.ndjson"), frames.map(x => JSON.stringify(x)).join("\n") + "\n");
  const project = await t3.project("Linux workspace", { "README.md": "Linux workspace; Windows files are outside this repository.\n" });
  const thread = await t3.thread({ project, title: "Windows drive path proof", messages: ["Show the Windows file links."], model: "showcase" });
  await t3.open(thread);
  await browser.reload();
  const results: Record<string, boolean> = {};
  for (const suffix of ["control", "inline", "backslash", "slash", "uri"]) {
    const chip = screen.getByRole("link", new RegExp(`notes-${suffix}\\.md`)).first();
    await chip.hover();
    await browser.locator('[data-slot="tooltip-popup"]').waitFor();
    await app.screenshot(`${suffix}-metadata`);
    await chip.tap();
    await screen.getByText(`READ_ONLY_CONTENT_OK_${suffix}`, { exact: false }).or(screen.getByRole("alert")).first().waitFor();
    results[suffix] = await screen.getByText(`READ_ONLY_CONTENT_OK_${suffix}`, { exact: false }).first().isVisible();
    await app.screenshot(`${suffix}-opened`);
  }
  expect(results).toEqual({ control: true, inline: true, backslash: true, slash: true, uri: true });
});
