mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
feat: detect network errors in API client and trigger disconnected state
This commit is contained in:
@@ -741,6 +741,40 @@ describe("API lib", () => {
|
||||
});
|
||||
});
|
||||
|
||||
// -- api network error → connection store ---------------------------------
|
||||
|
||||
describe("api network error → connection store", () => {
|
||||
it("triggers disconnected state on TypeError from fetch", async () => {
|
||||
const { useConnectionStore } = await import("@/stores/connection-store");
|
||||
useConnectionStore.setState({
|
||||
status: "connected",
|
||||
failedSince: null,
|
||||
lastHealthCheck: null,
|
||||
});
|
||||
|
||||
fetchMock.mockRejectedValueOnce(new TypeError("Failed to fetch"));
|
||||
await expect(apiGet("/v1/test")).rejects.toThrow("Failed to fetch");
|
||||
|
||||
expect(useConnectionStore.getState().status).toBe("disconnected");
|
||||
});
|
||||
|
||||
it("does NOT trigger disconnected on HTTP errors", async () => {
|
||||
const { useConnectionStore } = await import("@/stores/connection-store");
|
||||
useConnectionStore.setState({
|
||||
status: "connected",
|
||||
failedSince: null,
|
||||
lastHealthCheck: null,
|
||||
});
|
||||
|
||||
fetchMock.mockResolvedValueOnce(
|
||||
new Response(JSON.stringify({ error: "Not found" }), { status: 404 }),
|
||||
);
|
||||
await expect(apiGet("/v1/test")).rejects.toThrow();
|
||||
|
||||
expect(useConnectionStore.getState().status).toBe("connected");
|
||||
});
|
||||
});
|
||||
|
||||
// -- Cross-cutting: token is read fresh on every call --------------------
|
||||
|
||||
describe("token freshness", () => {
|
||||
|
||||
Reference in New Issue
Block a user