|
| 1 | +import { ApiRequestError } from "@posthog/api-client/fetcher"; |
| 2 | +import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; |
| 3 | +import { renderHook, waitFor } from "@testing-library/react"; |
| 4 | +import type { ReactNode } from "react"; |
| 5 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 6 | + |
| 7 | +const mockListAgentApplicationApprovals = vi.hoisted(() => vi.fn()); |
| 8 | +const mockClient = vi.hoisted(() => ({ |
| 9 | + listAgentApplicationApprovals: mockListAgentApplicationApprovals, |
| 10 | +})); |
| 11 | + |
| 12 | +vi.mock("@posthog/ui/features/auth/authClient", () => ({ |
| 13 | + useOptionalAuthenticatedClient: () => mockClient, |
| 14 | +})); |
| 15 | + |
| 16 | +vi.mock("../../auth/store", () => ({ |
| 17 | + useAuthStateValue: <T,>( |
| 18 | + selector: (state: { currentProjectId: number }) => T, |
| 19 | + ) => selector({ currentProjectId: 2 }), |
| 20 | +})); |
| 21 | + |
| 22 | +import { useAgentApplicationApprovals } from "./useAgentApplicationApprovals"; |
| 23 | + |
| 24 | +function renderApprovalsHook(idOrSlug: string) { |
| 25 | + const queryClient = new QueryClient({ |
| 26 | + // Zero out the backoff so the hook's own `retry` option (under test) |
| 27 | + // drives the call count without slowing the suite. |
| 28 | + defaultOptions: { queries: { retryDelay: 0 } }, |
| 29 | + }); |
| 30 | + const wrapper = ({ children }: { children: ReactNode }) => ( |
| 31 | + <QueryClientProvider client={queryClient}>{children}</QueryClientProvider> |
| 32 | + ); |
| 33 | + return renderHook(() => useAgentApplicationApprovals(idOrSlug), { wrapper }); |
| 34 | +} |
| 35 | + |
| 36 | +describe("useAgentApplicationApprovals", () => { |
| 37 | + beforeEach(() => { |
| 38 | + vi.clearAllMocks(); |
| 39 | + }); |
| 40 | + |
| 41 | + it("flags a 404 as a permission error without retrying", async () => { |
| 42 | + mockListAgentApplicationApprovals.mockRejectedValue( |
| 43 | + new ApiRequestError(404, '{"detail":"Not found"}'), |
| 44 | + ); |
| 45 | + const { result } = renderApprovalsHook("my-agent"); |
| 46 | + |
| 47 | + await waitFor(() => expect(result.current.isError).toBe(true)); |
| 48 | + expect(result.current.isPermissionError).toBe(true); |
| 49 | + // The admin gate never clears on retry, so the hook must not retry. |
| 50 | + expect(mockListAgentApplicationApprovals).toHaveBeenCalledTimes(1); |
| 51 | + }); |
| 52 | + |
| 53 | + it("treats non-404 failures as genuine errors and retries them", async () => { |
| 54 | + mockListAgentApplicationApprovals.mockRejectedValue( |
| 55 | + new ApiRequestError(500, '{"error":"boom"}'), |
| 56 | + ); |
| 57 | + const { result } = renderApprovalsHook("my-agent"); |
| 58 | + |
| 59 | + await waitFor(() => expect(result.current.isError).toBe(true)); |
| 60 | + expect(result.current.isPermissionError).toBe(false); |
| 61 | + expect(mockListAgentApplicationApprovals).toHaveBeenCalledTimes(4); |
| 62 | + }); |
| 63 | + |
| 64 | + it("returns approvals on success", async () => { |
| 65 | + mockListAgentApplicationApprovals.mockResolvedValue([{ id: "approval-1" }]); |
| 66 | + const { result } = renderApprovalsHook("my-agent"); |
| 67 | + |
| 68 | + await waitFor(() => expect(result.current.isSuccess).toBe(true)); |
| 69 | + expect(result.current.data).toEqual([{ id: "approval-1" }]); |
| 70 | + expect(result.current.isPermissionError).toBe(false); |
| 71 | + }); |
| 72 | + |
| 73 | + it("stays disabled and never fetches without an idOrSlug", async () => { |
| 74 | + mockListAgentApplicationApprovals.mockResolvedValue([]); |
| 75 | + const { result } = renderApprovalsHook(""); |
| 76 | + |
| 77 | + // enabled gates on !!idOrSlug — the query must stay pending with no fetch. |
| 78 | + await new Promise((resolve) => setTimeout(resolve, 50)); |
| 79 | + expect(mockListAgentApplicationApprovals).not.toHaveBeenCalled(); |
| 80 | + expect(result.current.isPending).toBe(true); |
| 81 | + expect(result.current.isPermissionError).toBe(false); |
| 82 | + }); |
| 83 | +}); |
0 commit comments