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

test("RTF code pages 0 and 866 render in Files previews", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("RTF samples", {
    "ansicpg0.rtf": "{\\rtf1\\ansi\\ansicpg0 Hello world}",
    "ansicpg866.rtf": "{\\rtf1\\ansi\\ansicpg866 \\'8f\\'e0\\'a8\\'a2\\'a5\\'e2}",
    "control-1252.rtf": "{\\rtf1\\ansi\\ansicpg1252 Caf\\'e9}",
  });
  const thread = await t3.thread({ project, title: "RTF previews", messages: ["Start"] });
  await browser.setViewport({ width: 1440, height: 1000 });
  await t3.open(thread);
  await screen.getByRole("button", "Toggle right panel").tap();
  await screen.getByRole("button", "Files F").tap();
  const results: string[] = [];
  for (const file of ["ansicpg0.rtf", "ansicpg866.rtf", "control-1252.rtf"]) {
    await screen.getByRole("treeitem", file).tap();
    const body = browser.frameLocator(`iframe[title^="${file}"]`).locator("body");
    const error = screen.getByText("This rich text code page is not supported.", { exact: false });
    await expect.poll(async () => (await body.isVisible()) || (await error.isVisible())).toBe(true);
    results.push((await body.isVisible()) ? (await body.textContent())?.trim() ?? "" : "unsupported");
    await app.screenshot(file.replace(".rtf", ""));
  }
  expect(results).toEqual(["Hello world", "Привет", "Café"]);
});
