/// <reference lib="dom" />
import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Literal file metadata distinguishes a file from its space-suffixed sibling directory", WEB, async ({ t3, app, browser, screen }) => {
  const project = await t3.project("Whitespace proof", { docs: "hello world", "docs /src/README.md": "directory entry\n" });
  await t3.open("/");
  await screen.getByRole("button", "New thread in Whitespace proof", { exact: true }).first().waitFor();
  const thread = await t3.thread({ project, title: "Literal filesystem paths", messages: ["Compare docs (11-byte file) and docs followed by one space (directory)."] });
  await t3.open(thread);
  const composer = screen.getByRole("textbox");
  await composer.pressSequentially("File without space: @docs");
  await screen.getByRole("option").filter({ has: browser.locator('[data-pierre-icon]') }).first().tap();
  await composer.pressSequentially(" Directory with trailing space: @docs");
  await screen.getByRole("option").filter({ has: browser.locator('.lucide-folder, [data-pierre-icon*="folder"]') }).first().tap();
  try {
    await expect.poll(() => browser.evaluate(() => {
      const editor = document.querySelector('[data-testid="composer-editor"]');
      return [...(editor?.querySelectorAll('[data-composer-mention-chip]') ?? [])].map(link => !!link.querySelector('.lucide-folder, [data-pierre-icon*="folder"]'));
    })).toEqual([false, true]);
  } finally {
    await app.screenshot("literal-sibling-metadata");
    await browser.locator('[data-composer-mention-chip]').first().hover();
    await browser.locator('[data-slot="tooltip-popup"]').waitFor();
    await browser.evaluate(async () => {
      await new Promise<void>(resolve => requestAnimationFrame(() => requestAnimationFrame(() => resolve())));
      await Promise.all(document.querySelector('[data-slot="tooltip-popup"]')?.getAnimations({subtree:true}).map(animation => animation.finished.catch(() => undefined)) ?? []);
      return null;
    });
    await app.screenshot("file-metadata-tooltip");
  }
});
