import { jsx as _jsx } from "react/jsx-runtime";
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
import { renderHook, act } from "@testing-library/react";
import { create } from "@bufbuild/protobuf";
import { RunnerSchema, RunnerStatusSchema, } from "@stigmer/protos/ai/stigmer/agentic/runner/v1/api_pb";
import { RunnerPhase } from "@stigmer/protos/ai/stigmer/agentic/runner/v1/enum_pb";
import { ApiResourceMetadataSchema } from "@stigmer/protos/ai/stigmer/commons/apiresource/metadata_pb";
import { StigmerContext } from "../../context";
import { useRunnerList } from "../useRunnerList";
function makeRunner(id, name, phase) {
    const runner = create(RunnerSchema);
    runner.metadata = create(ApiResourceMetadataSchema);
    runner.metadata.id = id;
    runner.metadata.name = name;
    runner.status = create(RunnerStatusSchema);
    runner.status.phase = phase;
    return runner;
}
function buildMockClient(listMock) {
    return {
        runner: { list: listMock },
    };
}
function makeWrapper(client) {
    return ({ children }) => (_jsx(StigmerContext.Provider, { value: client, children: children }));
}
async function flush() {
    await act(async () => {
        await Promise.resolve();
    });
}
describe("useRunnerList — refetchInterval", () => {
    let listMock;
    let client;
    beforeEach(() => {
        vi.useFakeTimers();
        listMock = vi.fn();
        client = buildMockClient(listMock);
    });
    afterEach(() => {
        vi.useRealTimers();
    });
    it("polls when refetchInterval is set", async () => {
        const runners = [makeRunner("r1", "dev", RunnerPhase.PENDING)];
        listMock.mockResolvedValue({ items: runners });
        const { result } = renderHook(() => useRunnerList("acme", { refetchInterval: 2000 }), { wrapper: makeWrapper(client) });
        // Initial fetch.
        await flush();
        expect(listMock).toHaveBeenCalledTimes(1);
        expect(result.current.runners).toHaveLength(1);
        // Advance by one interval.
        await act(async () => {
            await vi.advanceTimersByTimeAsync(2000);
        });
        expect(listMock).toHaveBeenCalledTimes(2);
    });
    it("does not poll when refetchInterval is false", async () => {
        listMock.mockResolvedValue({ items: [] });
        renderHook(() => useRunnerList("acme", { refetchInterval: false }), { wrapper: makeWrapper(client) });
        await flush();
        expect(listMock).toHaveBeenCalledTimes(1);
        await act(async () => {
            await vi.advanceTimersByTimeAsync(10_000);
        });
        expect(listMock).toHaveBeenCalledTimes(1);
    });
});
//# sourceMappingURL=useRunnerList.test.js.map