import * as fs from "node:fs/promises";
import * as path from "node:path";
import { expect } from "e2e";
import { APP_DIR } from "../e2e.config.ts";
import { test, WEB, runPromise } from "./t3.ts";
import { run } from "./shell.ts";

test("PR comment hover previews use the selected German language", WEB, async ({ t3, app, screen }) => {
  // Only the replay's response is synthetic; the app fetches and renders the real GitHub comment.
  const transcript = path.join(APP_DIR, "apps/server/scripts/dev-replay/command_output_transcript.ndjson");
  const original = await fs.readFile(transcript, "utf8");
  const frames = [
    { type: "expect_outbound", frame: { kind: "request", method: "session/prompt", params: "<any>" } },
    { type: "emit_inbound", frame: { kind: "notification", method: "session/update", params: { sessionId: "replay", update: { sessionUpdate: "agent_message_chunk", content: { type: "text", text: "See [this comment](https://github.com/pingdotgg/t3code/pull/43312#issuecomment-7186438221)." } } } } },
    { type: "emit_inbound", frame: { kind: "response", method: "session/prompt", result: { stopReason: "end_turn" } } },
  ];
  await fs.writeFile(transcript, frames.map(frame => JSON.stringify(frame)).join("\n") + "\n");
  try {
    const project = await t3.project("Comment preview");
    await runPromise(run("git", ["remote", "add", "origin", "https://github.com/pingdotgg/t3code.git"], { cwd: project }));
    const thread = await t3.thread({ project, title: "PR comment language", model: "command-output", messages: ["Show the PR comment link."] });
    await t3.open("/settings/general");
    await screen.getByLabel("Language", { exact: true }).tap();
    await screen.getByText("Deutsch", { exact: true }).tap();
    await screen.getByLabel("Sprache", { exact: true }).waitFor();
    await t3.open(thread);
    await screen.getByRole("link", "this comment", { exact: true }).hover();
    await screen.getByText("juliusmarminge", { exact: true }).waitFor();
    await app.screenshot("german-comment-preview");
    await expect(screen.getByText("hat kommentiert", { exact: true })).toBeVisible();
  } finally {
    await fs.writeFile(transcript, original);
  }
});
