mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
fix(site): only attempt to watch containers when agent connected (#18873)
This PR ensures we do not attempt to call `containers/watch` on the agent _before_ it is connected.
This commit is contained in:
@@ -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();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -31,6 +31,10 @@ export function useAgentContainers(
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
if (agent.status !== "connected") {
|
||||
return;
|
||||
}
|
||||
|
||||
const socket = watchAgentContainers(agent.id);
|
||||
|
||||
socket.addEventListener("message", (event) => {
|
||||
@@ -53,7 +57,7 @@ export function useAgentContainers(
|
||||
});
|
||||
|
||||
return () => socket.close();
|
||||
}, [agent.id, updateDevcontainersCache]);
|
||||
}, [agent.id, agent.status, updateDevcontainersCache]);
|
||||
|
||||
return devcontainers;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user