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

test("Mobile Send now explains a real steering rejection", { ...IOS, video: "off" }, async ({ t3, app, screen }) => {
  const project = await t3.project("mobile-queue-demo");
  const thread = await t3.thread({ project, title: "Mobile queued message", messages: ["Ask me a question"], model: "question" });
  await t3.open();
  await screen.getByText("Mobile queued message", { exact: false }).first().tap();
  const { cli } = makeState(MOBILE_T3_HOME, { T3CODE_PORT: String(MOBILE.serverPort) });
  await runPromise(cli(["thread", "send", thread.threadId, "/compact"]));
  await app.startRecording({ name: "send-now" });
  await screen.getByRole("button", "Open queue, 1 messages").tap();
  await app.screenshot("mobile-queued");
  await screen.getByRole("button", "Send now, queued message 1").tap();
  await new Promise((resolve) => setTimeout(resolve, 700));
  await app.screenshot("mobile-rejection");
  await expect.soft(screen.getByText("Could not send queued message", { exact: true })).toBeVisible();
  if (await screen.getByText("Could not send queued message", { exact: true }).isVisible()) {
    await screen.getByRole("button", "OK", { exact: true }).tap();
  }
  await app.screenshot("mobile-queue-retained");
  await screen.getByRole("button", "Close", { exact: true }).tap();
  await screen.getByRole("button", "Stop", { exact: true }).tap();
  await screen.getByRole("button", "Stop", { exact: true }).waitFor({ state: "hidden" });
  await screen.getByRole("button", "Open queue, 1 messages").tap();
  await screen.getByRole("button", "Send now, queued message 1").tap();
  await screen.getByRole("button", "Send now, queued message 1").waitFor({ state: "hidden" });
  await app.screenshot("mobile-retry-accepted");
  await app.stopRecording();
});
