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

test("An attached .cts file with application/typescript MIME previews its source", WEB, async ({ t3, app, browser, screen }) => {
  await t3.project("demo");
  await t3.open();
  const input = '[data-chat-composer-form] input[type="file"]';
  await browser.locator(input).waitFor({ state: "attached" });
  // Supply the exact File metadata Chromium reports with the Linux .cts MIME mapping.
  await browser.evaluate((selector) => {
    const picker = document.querySelector<HTMLInputElement>(selector)!;
    const files = new DataTransfer();
    files.items.add(new File(["export const answer = 42;\n"], "config.cts", { type: "application/typescript" }));
    picker.files = files.files;
    picker.dispatchEvent(new Event("change", { bubbles: true }));
    return null;
  }, input);
  await screen.getByRole("button", "File attachment, config.cts", { exact: false }).tap();
  await browser.locator('[role="dialog"]').filter({ hasText: /No preview for this file|export const answer/ }).waitFor();
  await app.screenshot("cts-source-preview");
  await expect(await browser.evaluate(() => document.querySelector('[role="dialog"] diffs-container')?.shadowRoot?.querySelector("[data-line]")?.textContent ?? "")).toContain("export const answer = 42;");
});
