Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions site/src/modules/resources/useAgentContainers.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,22 @@ describe("useAgentContainers", () => {
displayErrorSpy.mockRestore();
watchAgentContainersSpy.mockRestore();
});

it("does not establish WebSocket connection when agent is not connected", () => {
const watchAgentContainersSpy = jest.spyOn(API, "watchAgentContainers");

const disconnectedAgent = {
...MockWorkspaceAgent,
status: "disconnected" as const,
};

const { result } = renderHook(() => useAgentContainers(disconnectedAgent), {
wrapper: createWrapper(),
});

expect(watchAgentContainersSpy).not.toHaveBeenCalled();
expect(result.current).toBeUndefined();

watchAgentContainersSpy.mockRestore();
});
});
6 changes: 5 additions & 1 deletion site/src/modules/resources/useAgentContainers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ export function useAgentContainers(
);

useEffect(() => {
if (agent.status !== "connected") {
return;
}

const socket = watchAgentContainers(agent.id);

socket.addEventListener("message", (event) => {
Expand All @@ -53,7 +57,7 @@ export function useAgentContainers(
});

return () => socket.close();
}, [agent.id, updateDevcontainersCache]);
}, [agent.id, agent.status, updateDevcontainersCache]);

return devcontainers;
}
Loading