|
| 1 | +import { renderHook, waitFor } from "@testing-library/react"; |
| 2 | +import { http, HttpResponse } from "msw"; |
| 3 | +import type { FC, PropsWithChildren } from "react"; |
| 4 | +import { QueryClient, QueryClientProvider } from "react-query"; |
| 5 | +import { |
| 6 | + MockWorkspaceAgent, |
| 7 | + MockWorkspaceAgentDevcontainer, |
| 8 | +} from "testHelpers/entities"; |
| 9 | +import { server } from "testHelpers/server"; |
| 10 | +import { useAgentContainers } from "./useAgentContainers"; |
| 11 | + |
| 12 | +const createWrapper = (): FC<PropsWithChildren> => { |
| 13 | + const queryClient = new QueryClient({ |
| 14 | + defaultOptions: { |
| 15 | + queries: { |
| 16 | + retry: false, |
| 17 | + }, |
| 18 | + }, |
| 19 | + }); |
| 20 | + return ({ children }) => ( |
| 21 | + <QueryClientProvider client={queryClient}>{children}</QueryClientProvider> |
| 22 | + ); |
| 23 | +}; |
| 24 | + |
| 25 | +describe("useAgentContainers", () => { |
| 26 | + it("returns containers when agent is connected", async () => { |
| 27 | + server.use( |
| 28 | + http.get( |
| 29 | + `/api/v2/workspaceagents/${MockWorkspaceAgent.id}/containers`, |
| 30 | + () => { |
| 31 | + return HttpResponse.json({ |
| 32 | + devcontainers: [MockWorkspaceAgentDevcontainer], |
| 33 | + containers: [], |
| 34 | + }); |
| 35 | + }, |
| 36 | + ), |
| 37 | + ); |
| 38 | + |
| 39 | + const { result } = renderHook( |
| 40 | + () => useAgentContainers(MockWorkspaceAgent), |
| 41 | + { |
| 42 | + wrapper: createWrapper(), |
| 43 | + }, |
| 44 | + ); |
| 45 | + |
| 46 | + await waitFor(() => { |
| 47 | + expect(result.current).toEqual([MockWorkspaceAgentDevcontainer]); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + it("returns undefined when agent is not connected", () => { |
| 52 | + const disconnectedAgent = { |
| 53 | + ...MockWorkspaceAgent, |
| 54 | + status: "disconnected" as const, |
| 55 | + }; |
| 56 | + |
| 57 | + const { result } = renderHook(() => useAgentContainers(disconnectedAgent), { |
| 58 | + wrapper: createWrapper(), |
| 59 | + }); |
| 60 | + |
| 61 | + expect(result.current).toBeUndefined(); |
| 62 | + }); |
| 63 | + |
| 64 | + it("handles API errors gracefully", async () => { |
| 65 | + server.use( |
| 66 | + http.get( |
| 67 | + `/api/v2/workspaceagents/${MockWorkspaceAgent.id}/containers`, |
| 68 | + () => { |
| 69 | + return HttpResponse.error(); |
| 70 | + }, |
| 71 | + ), |
| 72 | + ); |
| 73 | + |
| 74 | + const { result } = renderHook( |
| 75 | + () => useAgentContainers(MockWorkspaceAgent), |
| 76 | + { |
| 77 | + wrapper: createWrapper(), |
| 78 | + }, |
| 79 | + ); |
| 80 | + |
| 81 | + await waitFor(() => { |
| 82 | + expect(result.current).toBeUndefined(); |
| 83 | + }); |
| 84 | + }); |
| 85 | +}); |
0 commit comments