/// <reference lib="dom" />
import { ProjectSnapshot } from "@t3tools/contracts";
import { expect } from "e2e";
import * as Effect from "effect/Effect";
import { FetchHttpClient, HttpClient, HttpClientRequest, HttpClientResponse } from "effect/http";
import { T3_HOME } from "../e2e.config.ts";
import { environmentId, makeState, runPromise, test, WEB } from "./t3.ts";
import { run } from "./shell.ts";

test("Submitting a merge request title returns focus to Edit title", WEB, async ({t3,app,browser,screen}) => {
  const workspace = await t3.project("Title focus proof");
  await runPromise(run("git", ["remote", "add", "origin", "https://gitlab.com/julius54/title-focus-evidence-47380-2.git"], {cwd:workspace}));
  await runPromise(run("glab", ["api", "projects/89763404/merge_requests/1", "--method", "PUT", "-f", "title=Merge request title"]));
  const origin = app.baseUrl!;
  const environment = await runPromise(environmentId(origin));
  const project = await runPromise(Effect.gen(function*(){
    const issued = yield* makeState(T3_HOME).cli(["auth","session","issue","--token-only"]);
    const {projects} = yield* HttpClient.execute(HttpClientRequest.get(`${origin}/api/projects`).pipe(HttpClientRequest.bearerToken(issued.stdout.trim()))).pipe(Effect.flatMap(HttpClientResponse.filterStatusOk),Effect.flatMap(HttpClientResponse.schemaBodyJson(ProjectSnapshot)));
    return projects.find(p => p.workspaceRoot === workspace)!;
  }).pipe(Effect.provide(FetchHttpClient.layer)));
  await t3.open(`/pull-requests?${new URLSearchParams({repository:"julius54/title-focus-evidence-47380-2",number:"1",selectedProjectId:project.id,selectedEnvironmentId:environment,selectedHost:"gitlab.com"})}`);
  await screen.getByText("Authored",{exact:true}).waitFor();
  await screen.getByText("Loading pull requests…",{exact:true}).waitFor({state:"hidden"});
  await screen.getByText("No comments yet.",{exact:true}).waitFor();
  const edit = screen.getByRole("button","Edit title",{exact:true});
  const input = screen.getByRole("textbox","Merge request title",{exact:true});
  const results: Record<string,boolean> = {};
  const capture = async (label:string) => {
    await input.waitFor({state:"hidden"});
    await edit.waitFor();
    results[label] = await browser.evaluate(() => document.activeElement?.getAttribute("aria-label") === "Edit title");
    await app.screenshot(label);
  };
  await edit.tap();
  await browser.keyboard.press("Enter");
  await capture("unchanged-enter");
  await edit.tap();
  await browser.keyboard.press("Escape");
  await capture("escape-control");
  await browser.keyboard.press("Enter");
  await browser.keyboard.press("ControlOrMeta+a");
  await browser.keyboard.press("Backspace");
  await browser.keyboard.press("Enter");
  await capture("blank-enter");
  await edit.tap();
  await input.fill("Renamed title");
  await browser.keyboard.press("Enter");
  await screen.getByRole("heading","Renamed title",{exact:true}).waitFor();
  await capture("changed-enter");
  await edit.tap();
  await input.fill("Renamed again");
  await screen.getByRole("button","Save",{exact:true}).tap();
  await screen.getByRole("heading","Renamed again",{exact:true}).waitFor();
  await capture("changed-save");
  expect(results).toEqual({"unchanged-enter":true,"escape-control":true,"blank-enter":true,"changed-enter":true,"changed-save":true});
});
