import { describe, expect, it } from "vitest";
import {
  flockLocatorCount,
  flockPageClosedMessage,
  isFlockPageAlive,
  isFlockPageClosedError,
} from "@/workers/rpa/flock/page-alive";

describe("flock page-alive", () => {
  it("识别 Target closed / locator.count 关窗错误", () => {
    expect(
      isFlockPageClosedError(
        new Error("locator.count: Target page, context or browser has been closed"),
      ),
    ).toBe(true);
    expect(isFlockPageClosedError(new Error("other"))).toBe(false);
  });

  it("关窗文案可读", () => {
    expect(flockPageClosedMessage("填表")).toContain("BROWSER_CLOSED");
    expect(flockPageClosedMessage("填表")).toContain("填表");
  });

  it("isFlockPageAlive null 为 false", () => {
    expect(isFlockPageAlive(null)).toBe(false);
  });

  it("flockLocatorCount 在 page.isClosed 时返回 0", async () => {
    const loc = {
      page: () => ({ isClosed: () => true }),
      count: async () => {
        throw new Error("should not call");
      },
    };
    expect(await flockLocatorCount(loc as never)).toBe(0);
  });
});
