import { describe, expect, it, vi } from "vitest";

vi.mock("@/workers/rpa/worker-state", () => ({
  listWorkerHeartbeats: vi.fn(),
}));

import { isAnyRpaWorkerHealthy } from "@/lib/rpa/worker-health";
import { listWorkerHeartbeats } from "@/workers/rpa/worker-state";

describe("isAnyRpaWorkerHealthy", () => {
  it("存在近期心跳时返回 true", async () => {
    vi.mocked(listWorkerHeartbeats).mockResolvedValue([
      { workerId: "w1", lastSeenMs: Date.now() - 5_000 },
    ]);
    await expect(isAnyRpaWorkerHealthy()).resolves.toBe(true);
  });

  it("无心跳或 Redis 异常时返回 false", async () => {
    vi.mocked(listWorkerHeartbeats).mockResolvedValue([]);
    await expect(isAnyRpaWorkerHealthy()).resolves.toBe(false);

    vi.mocked(listWorkerHeartbeats).mockRejectedValue(new Error("redis down"));
    await expect(isAnyRpaWorkerHealthy()).resolves.toBe(false);
  });
});
