import { expect } from "e2e";
import { setTimeout } from "node:timers/promises";
import { MOBILE } from "../e2e.config.ts";
import { environmentId, runPromise } from "./t3.ts";
import { IOS, test } from "./t3-mobile.ts";

test("Cold launching a thread keeps the iOS dock off the composer", IOS, async ({ t3, app, screen, device }) => {
  const project = await t3.project("Dock proof");
  const { threadId } = await t3.thread({ project, title: "Dock visibility proof", messages: ["Hello"] });
  await t3.open();
  const row = screen.getByText("Dock visibility proof", { exact: false }).first();
  await row.waitFor({ timeout: 120_000 });
  await app.screenshot("home");
  await row.tap();
  await screen.getByText("Ask anything", { exact: false }).first().waitFor();
  await app.screenshot("normal-push");
  const environment = await runPromise(environmentId(`http://${MOBILE.host}:${MOBILE.serverPort}`));
  await device.closeApp();
  await device.openLink(`t3code-dev://threads/${environment}/${threadId}`, { app: "com.t3tools.t3code.dev" });
  await device.alert("accept").catch(() => {});
  // The development launcher holds the notification link until the Metro bundle is selected.
  const bundle = screen.getByText(`http://${MOBILE.host}:${MOBILE.metroPort}`, { exact: false }).first();
  if (await bundle.isVisible()) await bundle.tap();
  await screen.getByText("Ask anything", { exact: false }).first().waitFor({ timeout: 120_000 });
  // The regression arrives after inactive tab prewarming, rather than at the initial appearance.
  await setTimeout(20_000);
  await app.screenshot("cold-thread-after-prewarm");
  await expect(screen.getByText("Chats", { exact: true })).not.toBeVisible();
});
