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

test("A composer mention uses metadata for its exact colon filename", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("mention-demo", { report: "x", "report:42": "x".repeat(2048) });
  await runPromise(Effect.gen(function* () {
    const client = yield* rpcClient(T3_HOME, app.baseUrl!);
    yield* client.subscribeServerConfig({}).pipe(
      Stream.filter((event) => {
        const providers = event.type === "snapshot" ? event.config.providers : event.type === "providerStatuses" ? event.payload.providers : [];
        return providers.some((provider) => provider.instanceId === "devReplay" && provider.models?.some((model) => model.slug === "multi-turn"));
      }),
      Stream.runHead,
    );
  }).pipe(Effect.scoped, Effect.timeout("30 seconds")));
  const thread = await t3.thread({ project, title: "Exact file metadata", messages: ["Hello"] });
  await t3.open(thread);
  await screen.getByRole("textbox").pressSequentially("@report");
  await screen.getByRole("option", "report:42", { exact: false }).tap();
  await screen.getByRole("button", "Preview report:42", { exact: true }).hover();
  try {
    await expect(browser.locator('[data-slot="tooltip-viewport"]:has-text("report:42")')).toContainText("2 KB");
  } finally {
    await browser.evaluate(async () => {
      await Promise.all(document.getAnimations().filter((animation) => animation.effect?.getComputedTiming().iterations !== Infinity).map((animation) => animation.finished.catch(() => {})));
    });
    await app.screenshot("exact-file-mention-tooltip");
  }
});
