mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
fix: handle non-ok HTTP responses in connection store checkHealth
checkHealth previously ignored non-ok responses (e.g. 503), silently doing nothing when the server reported unhealthy. Add else branch to transition connected → disconnected on non-ok status. Add tests for connected → disconnected on fetch failure and offline → reconnected on health check success.
This commit is contained in:
@@ -51,6 +51,10 @@ export const useConnectionStore = create<ConnectionState>((set, get) => ({
|
||||
} else {
|
||||
set({ lastHealthCheck: Date.now() });
|
||||
}
|
||||
} else {
|
||||
if (get().status === "connected") {
|
||||
get().setDisconnected();
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
if (get().status === "connected") {
|
||||
|
||||
@@ -81,6 +81,24 @@ describe("connection-store", () => {
|
||||
expect(fetchMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("checkHealth transitions connected → disconnected on fetch failure", async () => {
|
||||
fetchMock.mockImplementation(failHealth);
|
||||
expect(useConnectionStore.getState().status).toBe("connected");
|
||||
await useConnectionStore.getState().checkHealth();
|
||||
expect(useConnectionStore.getState().status).toBe("disconnected");
|
||||
expect(useConnectionStore.getState().failedSince).toBeTypeOf("number");
|
||||
});
|
||||
|
||||
it("checkHealth transitions offline → reconnected on success", async () => {
|
||||
fetchMock.mockImplementation(okHealth);
|
||||
useConnectionStore.getState().setOffline();
|
||||
expect(useConnectionStore.getState().status).toBe("offline");
|
||||
await useConnectionStore.getState().checkHealth();
|
||||
expect(useConnectionStore.getState().status).toBe("reconnected");
|
||||
expect(useConnectionStore.getState().failedSince).toBeNull();
|
||||
expect(useConnectionStore.getState().lastHealthCheck).toBeTypeOf("number");
|
||||
});
|
||||
|
||||
it("startPolling is idempotent", () => {
|
||||
useConnectionStore.getState().setDisconnected();
|
||||
useConnectionStore.getState().startPolling();
|
||||
|
||||
Reference in New Issue
Block a user