import * as fs from "node:fs/promises";
import { expect } from "e2e";
import { MOBILE_T3_HOME } from "../e2e.config.ts";
import { IOS, test } from "./t3-mobile.ts";

test("The no-model draft composer stays visible above the keyboard", IOS, async ({t3, app, screen, device}) => {
  await t3.project("Keyboard demo");
  const path = `${MOBILE_T3_HOME}/userdata/settings.json`;
  const settings = JSON.parse(await fs.readFile(path, "utf8"));
  for (const provider of Object.values(settings.providerInstances) as Array<{enabled:boolean}>) provider.enabled = false;
  await fs.writeFile(path, JSON.stringify(settings));
  await app.clearState().catch(() => {});
  await t3.open();
  await screen.getByRole("button", "New chat").tap({timeout:120000});
  await screen.getByRole("textbox", "Search projects").waitFor();
  await screen.getByRole("button", "Keyboard demo").tap();
  await device.alert("accept").catch(() => {});
  const composer = screen.getByRole("textbox", "Ask anything");
  await composer.fill("First line of the draft\nSecond line stays visible\nThird line and caret");
  await app.screenshot("no-model-keyboard");
  await expect(composer).toBeVisible();
  await screen.getByTestId("BackButton").tap();
  await app.screenshot("header-back");
});
