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

test("GB18030 RTF documents preview Chinese and Unicode text", WEB, async ({ t3, app, screen, browser }) => {
  const project = await t3.project("rtf-demo", {
    "notes.rtf": String.raw`{\rtf1\ansi\ansicpg54936 Quarterly notes: \'d6\'d0\'ce\'c4 \'95\'32\'82\'36 \'82\'35\'8f\'33 \uc2\u20320\'c4\'e3 done\par}`,
    "fonttable.rtf": String.raw`{\rtf1\ansi{\fonttbl{\f0\fnil\fcharset0\cpg54936 SimSun;}}\f0 \'d6\'d0\'ce\'c4}`,
  });
  const thread = await t3.thread({ project, title: "Review rich text", messages: ["Start"] });
  await t3.open(thread);
  await expect(screen.getByText("fixture turn complete", { exact: false })).toBeVisible();
  await browser.keyboard.press("Control+p");
  await screen.getByPlaceholder("Search files…").fill("fonttable.rtf");
  await screen.getByRole("option", "fonttable.rtf", { exact: false }).tap();
  await expect(screen.getByText("1 page", { exact: false })).toBeVisible({ timeout: 120_000 });
  await app.screenshot("font-table");
  const fontText = await browser.frameLocator('iframe[title^="fonttable.rtf"]').locator("body").textContent();
  await browser.keyboard.press("Control+p");
  await screen.getByPlaceholder("Search files…").fill("notes.rtf");
  await screen.getByRole("option", "notes.rtf", { exact: false }).tap();
  await expect(screen.getByText(/1 page|No preview for this file/).first()).toBeVisible({ timeout: 120_000 });
  await app.screenshot("quarterly-notes");
  expect(fontText?.trim()).toBe("中文");
  await expect(browser.frameLocator('iframe[title^="notes.rtf"]').locator("body")).toHaveText("Quarterly notes: 中文 𠀀 龦 你 done");
});
