/// <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";

test("Raw HTML attribute backticks do not split inline command examples", WEB, async ({t3, app, browser, screen}) => {
    const workspace = await t3.project("Command example", {".git/config": '[remote "origin"]\nurl = https://gitlab.com/julius54/proof-48860-20261204.git\nfetch = +refs/heads/*:refs/remotes/origin/*\n'});
    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((candidate) => candidate.workspaceRoot === workspace)!;
      }).pipe(Effect.provide(FetchHttpClient.layer)),
    );

    await browser.setViewport({width: 1280, height: 800});
    const query = new URLSearchParams({repository: "julius54/proof-48860-20261204", number: "1", selectedProjectId: project.id, selectedEnvironmentId: environment, selectedHost: "gitlab.com"});
    await t3.open(`/pull-requests?${query}`);
    await screen.getByText("End of description.", {exact: true}).waitFor({timeout: 60000});
    await screen.getByRole("status", "Loading pull requests…", {exact: true}).waitFor({state: "hidden", timeout: 60000});
    await screen.getByRole("status", "Loading merge request conversation", {exact: true}).waitFor({state: "hidden", timeout: 60000});
    await app.screenshot("inline-command-after-raw-html");
    const codes = await browser.locator("code").allTextContents();
    expect(codes).toContain("fetch https://example.com/release-demo.mp4 --output clip");
    expect(await browser.locator('video[aria-label="Pull request video"]').count()).toBe(0);
});
