import { expect } from "e2e";
import { IOS, test } from "./t3-mobile.ts";

test("Typing before an image chip preserves iOS autocorrection and caret", IOS, async ({ t3, app, screen, device }) => {
  const project = await t3.project("Caret demo");
  await t3.thread({ project, title: "Caret reproduction", messages: [] });
  await device.setAppearance("light");
  await t3.open();
  await screen.getByText("Caret reproduction", { exact: false }).first().tap();
  const composer = screen.getByRole("textbox");
  await composer.tap();
  for (const key of "hello there") await screen.getByLabel(key === " " ? "space" : new RegExp(`^${key}$`, "i")).last().tap();
  await screen.getByRole("button", "Add attachment").tap();
  await screen.getByText("Photo Library").tap();
  await screen.getByRole("button", "Done").waitFor();
  await app.screenshot("photo-picker");
  await screen.getByRole("image", /^Photo,/).first().tap();
  await screen.getByRole("button", "Done").tap();
  await app.screenshot("before-typing");
  await composer.tap({ position: { x: 90, y: 15 } });
  for (const key of " im ok") await screen.getByLabel(key === " " ? "space" : new RegExp(`^${key}$`, "i")).last().tap();
  await app.screenshot("caret-after-autocorrection");
  await expect(composer).toHaveValue(/Hello there I’m ok/);
});
