import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
import { CommandId, ThreadId } from "@t3tools/contracts";
import { expect } from "e2e";
import * as SecureRandom from "effect/crypto/SecureRandom";
import * as Effect from "effect/Effect";
import { MOBILE, MOBILE_T3_HOME } from "../e2e.config.ts";
import { IOS, test } from "./t3-mobile.ts";
import { rpcClient, runPromise } from "./t3.ts";

test("One hidden settled thread has a singular show-more announcement", IOS, async ({ t3, app, screen }) => {
  const project = await t3.project("Settled demo");
  const ids: string[] = [];
  for (let i = 1; i <= 11; i++) {
    ids.push((await t3.thread({ project, title: `Settled thread ${String(i).padStart(2, "0")}`, messages: ["first"] })).threadId);
  }
  await runPromise(Effect.gen(function* () {
    const client = yield* rpcClient(MOBILE_T3_HOME, `http://127.0.0.1:${MOBILE.serverPort}`);
    const random = yield* SecureRandom.SecureRandom;
    for (const id of ids) yield* client["orchestration.dispatchCommand"]({
      type: "thread.settle", commandId: CommandId.make(yield* random.uuidV4()), threadId: ThreadId.make(id),
    });
  }).pipe(Effect.scoped, Effect.provide(NodeCrypto.layer)));
  await t3.open();
  await screen.getByRole("button", "11 settled threads").tap({ timeout: 120_000 });
  const more = screen.getByRole("button", "Show 1 more settled thread", { exact: false });
  await more.waitFor();
  await app.screenshot("one-hidden-settled-thread");
  await expect(more).toHaveAccessibleName("Show 1 more settled thread");
});
