/// <reference lib="dom" />
import { expect } from "e2e";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import { T3_HOME } from "../e2e.config.ts";
import { runPromise, test, WEB } from "./t3.ts";
const argsFile = `${T3_HOME}/editor-args.txt`;
const read = (file: string) => runPromise(Effect.gen(function* () {
  const fs = yield* FileSystem.FileSystem;
  return (yield* fs.exists(file)) ? yield* fs.readFileString(file) : "";
}));
test("Gradle quoted build path opens at the following line", WEB, async ({t3, app, browser, screen}) => {
  const project = await t3.project("gradleproj", {
    "build.gradle": 'plugins { id "java" }\n\nfoo()\n',
    "settings.gradle": "rootProject.name='x'\n",
    "reproduce.sh": `export PS1='$ '
clear
gradle --offline --no-daemon help > gradle.log 2>&1
sed -n '/FAILURE:/,/What went wrong:/p' gradle.log | sed "s@$PWD/@./@g" > diagnostic.log
for i in $(seq 1 100); do sed -n "/Build file/p" diagnostic.log; done
echo ready > printed
`,
  });
  const thread = await t3.thread({project, title: "Gradle build diagnostic", messages:["Open the build failure at its reported line."]});
  await t3.open(thread);
  await screen.getByRole("button", "Toggle terminal drawer").tap();
  const input = screen.getByRole("textbox", "Terminal input");
  await expect(input).toHaveAttribute("data-workspace-focus-default", "");
  await input.focus();
  await browser.keyboard.type("unset PROMPT_COMMAND; PS1=\"$ \"; bash reproduce.sh\n");
  await expect.poll(() => read(`${project}/printed`), {timeout: 90_000}).toEqual("ready\n");
  await expect.poll(() => read(`${project}/diagnostic.log`), {timeout: 90_000}).toContain("line: 3");
  await app.screenshot("gradle-diagnostic");
  const position = await browser.evaluate(() => {
    const input = document.querySelector('[aria-label="Terminal input"]');
    const box = input?.parentElement?.querySelector("canvas")?.getBoundingClientRect();
    return box ? {x:box.x,y:box.y,width:box.width,height:box.height} : null;
  });
  if (!position) throw new Error("Terminal canvas missing");
  // The same real Gradle diagnostic fills the visible rows.
  await browser.mouse.move(position.x + 130, position.y + position.height / 2);
  await app.screenshot("hover-link");
  await runPromise(Effect.flatMap(FileSystem.FileSystem, fs => fs.remove(argsFile, {force:true})));
  await browser.keyboard.down("Control");
  await browser.mouse.down(); await browser.mouse.up();
  await browser.keyboard.up("Control");
  await expect.poll(() => read(argsFile), {timeout:15_000}).toContain("build.gradle");
  const actual = await read(argsFile);
  await runPromise(Effect.flatMap(FileSystem.FileSystem, fs => fs.writeFileString(`${project}/editor-result.txt`, actual.replaceAll(project + "/", "./"))));
  await input.focus();
  await browser.keyboard.type(`echo EDITOR_INVOCATION; cat editor-result.txt\n`);
  await app.screenshot("editor-invocation");
  await expect.poll(() => read(argsFile), {timeout:15_000}).toEqual(`--classic\n--goto\n${project}/build.gradle:3\n`);
});
