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

test("Retry now enables a switched-off environment and loads review", IOS, async ({ t3, app, screen, device }) => {
  const project = await t3.project("Reconnect demo");
  const thread = await t3.thread({ project, title: "Reconnect demo", messages: ["Hello"] });
  const { environmentId } = await (await fetch(`http://127.0.0.1:${MOBILE.serverPort}/.well-known/t3/environment`)).json();
  await device.setAppearance("dark");
  await t3.open();
  const open = async (path: string) => {
    await device.openLink(`t3code-dev://${path}`, { app: "com.t3tools.t3code.dev" });
    await device.alert("accept").catch(() => {});
  };
  await open(`threads/${environmentId}/${thread.threadId}`);
  await open("settings/environments");
  await screen.getByRole("switch").tap();
  await open(`threads/${environmentId}/${thread.threadId}/review`);
  await screen.getByRole("button", "Retry now").tap();
  try {
    await expect(screen.getByText("Reconnect the environment to load the review.")).not.toBeVisible({ timeout: 20000 });
  } finally {
    await app.screenshot("review-after-retry");
  }
  await open("settings/environments");
  await app.screenshot("environment-after-retry");
  await expect(screen.getByText("Connected", { exact: true })).toBeVisible();
});
