import { expect } from "e2e";
import * as Effect from "effect/Effect";
import { T3_HOME } from "../e2e.config.ts";
import { rpcClient, runPromise, test, WEB } from "./t3.ts";

test("Workflows links truncate without splitting an emoji", WEB, async ({ t3, app, browser, screen }) => {
  await browser.setViewport({ width: 2800, height: 900 });
  await t3.project("demo");
  await runPromise(Effect.gen(function* () {
    const client = yield* rpcClient(T3_HOME, app.baseUrl ?? "");
    yield* client["server.updateSettings"]({ patch: { workflowsEnabled: true } });
  }).pipe(Effect.scoped));
  await t3.open(`/workflows?${new URLSearchParams({ env: "e".repeat(199) + "😀", run: "r".repeat(199) + "😀" })}`);
  await screen.getByText(/is not connected/).waitFor({ timeout: 60000 });
  const boundary = await browser.evaluate(() => ({ env: new URLSearchParams(location.search).get("env"), run: new URLSearchParams(location.search).get("run"), text: document.body.innerText }));
  await app.screenshot("environment-boundary");
  await t3.open(`/workflows?${new URLSearchParams({ env: "e".repeat(198) + "😀", run: "r".repeat(198) + "😀" })}`);
  await screen.getByText(/is not connected/).waitFor({ timeout: 60000 });
  const control = await browser.evaluate(() => ({ env: new URLSearchParams(location.search).get("env"), run: new URLSearchParams(location.search).get("run") }));
  await app.screenshot("intact-emoji-control");
  await t3.open(`/workflows?${new URLSearchParams({ definitions: "true", file: "c".repeat(199) + "😀" })}`);
  await screen.getByRole("textbox").first().waitFor({ timeout: 60000 });
  const file = await browser.evaluate(() => ({ search: location.search, labels: Array.from(document.querySelectorAll('[role="textbox"],textarea')).map(e => e.getAttribute('aria-label')) }));
  await app.screenshot("file-boundary");
  expect(new URLSearchParams(file.search).get("file")).toBe("c".repeat(199));
  expect(boundary.env).toBe("e".repeat(199));
  expect(boundary.run).toBe("r".repeat(199));
  expect(boundary.text.includes("�")).toBe(false);
  expect(control.env).toBe("e".repeat(198) + "😀");
  expect(control.run).toBe("r".repeat(198) + "😀");
});
