import { expect } from "e2e";
import { test, WEB } from "./t3.ts";

test("Large file counts follow German app language with an English browser", WEB, async ({ t3, app, browser, screen }) => {
  const row = Array.from({ length: 1000 }, () => "x").join(",");
  const csv = [row, ...Array.from({ length: 1250 }, () => row)].join("\n");
  const text = Array.from({ length: 1500 }, () => "Large text preview sample. ".repeat(1800)).join("\n");
  const project = await t3.project("sample-files", { "counts.csv": csv, "large.txt": text });
  const thread = await t3.thread({ project, title: "File count review", messages: ["Start"] });
  await t3.open("/settings/general");
  await screen.getByLabel("Language", { exact: true }).tap();
  await screen.getByText("Deutsch", { exact: true }).tap();
  await screen.getByLabel("Sprache", { exact: true }).waitFor();
  await t3.open(thread);
  await screen.getByRole("textbox", "Nachricht an File count review").waitFor();
  const openFile = async (name: string) => {
    await browser.keyboard.press("Control+p");
    await screen.getByPlaceholder("Dateien durchsuchen…").fill(name);
    await screen.getByRole("option", name, { exact: false }).tap();
  };
  await openFile("counts.csv");
  await screen.getByText(/Zeilen · .*Spalten ·/).waitFor({ timeout: 120000 });
  const csvCount = await browser.evaluate(() => Array.from(document.querySelectorAll("*" )).find(el => el.children.length === 0 && /Zeilen · .*Spalten ·/.test(el.textContent ?? ""))?.textContent);
  await app.screenshot("german-csv-counts");
  await openFile("large.txt");
  await screen.getByText(/Zeilen · .*MB/).waitFor({ timeout: 120000 });
  const textCount = await browser.evaluate(() => Array.from(document.querySelectorAll("*" )).find(el => el.children.length === 0 && /Zeilen · .*MB/.test(el.textContent ?? ""))?.textContent);
  await app.screenshot("german-text-line-count");
  const environment = await browser.evaluate(() => ({ language: navigator.language, app: document.documentElement.lang }));
  await expect({ csvCount, textCount, environment }).toEqual({ csvCount: expect.stringContaining("1.250 Zeilen · 1.000 Spalten"), textCount: expect.stringContaining("1.500 Zeilen"), environment: { language: "en-US", app: "de" } });
});
