forked from Vero-protocol/vero-core-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpcHealth.test.ts
More file actions
62 lines (55 loc) · 2.1 KB
/
Copy pathrpcHealth.test.ts
File metadata and controls
62 lines (55 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { describe, it, expect, beforeEach, afterEach, vi } from "vitest";
import { isValidHttpUrl, probeEndpoint } from "./rpcHealth";
describe("isValidHttpUrl", () => {
it("accepts http and https URLs", () => {
expect(isValidHttpUrl("https://example.com")).toBe(true);
expect(isValidHttpUrl("http://example.com")).toBe(true);
expect(isValidHttpUrl("https://example.com:9000/path")).toBe(true);
});
it("rejects non-http schemes and garbage", () => {
expect(isValidHttpUrl("ftp://example.com")).toBe(false);
expect(isValidHttpUrl("ws://example.com")).toBe(false);
expect(isValidHttpUrl("")).toBe(false);
expect(isValidHttpUrl("not a url")).toBe(false);
});
});
describe("probeEndpoint", () => {
const originalFetch = globalThis.fetch;
beforeEach(() => {
vi.useFakeTimers();
});
afterEach(() => {
globalThis.fetch = originalFetch;
vi.useRealTimers();
});
it("returns unreachable for invalid URLs without calling fetch", () => {
const fetchSpy = vi.fn();
globalThis.fetch = fetchSpy;
return probeEndpoint("not a url").then((res) => {
expect(res.status).toBe("unreachable");
expect(fetchSpy).not.toHaveBeenCalled();
});
});
it("returns healthy on 2xx HEAD response", async () => {
globalThis.fetch = vi.fn().mockResolvedValue({ ok: true, status: 200 });
const res = await probeEndpoint("https://example.com");
expect(res.status).toBe("healthy");
expect(res.latencyMs).toBeGreaterThanOrEqual(0);
});
it("falls back to GET and returns healthy on 2xx", async () => {
globalThis.fetch = vi
.fn()
.mockResolvedValueOnce({ ok: false, status: 405 })
.mockResolvedValueOnce({ ok: true, status: 200 });
const res = await probeEndpoint("https://example.com");
expect(res.status).toBe("healthy");
});
it("returns unreachable when both probes fail", async () => {
globalThis.fetch = vi
.fn()
.mockResolvedValue({ ok: false, status: 500 });
const res = await probeEndpoint("https://example.com");
expect(res.status).toBe("unreachable");
expect(res.message).toMatch(/HTTP 500/);
});
});