|
| 1 | +import { describe, it } from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | + |
| 4 | +import { |
| 5 | + KeeperStatusClient, |
| 6 | + StatusApiError, |
| 7 | + StatusJsonParseError, |
| 8 | +} from "./status-client.js"; |
| 9 | +import type { KeeperStatusResponse } from "./status.js"; |
| 10 | + |
| 11 | +const SAMPLE_STATUS: KeeperStatusResponse = { |
| 12 | + contractId: "C123", |
| 13 | + network: "testnet", |
| 14 | + uptimeSeconds: 42, |
| 15 | + rounds: [], |
| 16 | + health: { |
| 17 | + rpc: "ok", |
| 18 | + drand: "ok", |
| 19 | + checkedAt: "2026-01-01T00:00:00.000Z", |
| 20 | + }, |
| 21 | + now: "2026-01-01T00:00:00.000Z", |
| 22 | +}; |
| 23 | + |
| 24 | +function mockFetch(body: string, status = 200): typeof fetch { |
| 25 | + return async () => |
| 26 | + new Response(body, { |
| 27 | + status, |
| 28 | + headers: { "content-type": "application/json" }, |
| 29 | + }); |
| 30 | +} |
| 31 | + |
| 32 | +describe("KeeperStatusClient successful JSON parsing", () => { |
| 33 | + it("returns valid successful JSON unchanged", async () => { |
| 34 | + const client = new KeeperStatusClient({ |
| 35 | + baseURL: "http://keeper.test", |
| 36 | + fetchImpl: mockFetch(JSON.stringify(SAMPLE_STATUS)), |
| 37 | + }); |
| 38 | + const status = await client.getStatus(); |
| 39 | + assert.deepEqual(status, SAMPLE_STATUS); |
| 40 | + }); |
| 41 | + |
| 42 | + it("rejects empty successful JSON bodies", async () => { |
| 43 | + const client = new KeeperStatusClient({ |
| 44 | + baseURL: "http://keeper.test", |
| 45 | + fetchImpl: mockFetch(" "), |
| 46 | + }); |
| 47 | + await assert.rejects( |
| 48 | + () => client.getStatus(), |
| 49 | + (error: unknown) => { |
| 50 | + assert.ok(error instanceof StatusJsonParseError); |
| 51 | + assert.equal(error.status, 200); |
| 52 | + return true; |
| 53 | + }, |
| 54 | + ); |
| 55 | + }); |
| 56 | + |
| 57 | + it("rejects malformed successful JSON bodies", async () => { |
| 58 | + const client = new KeeperStatusClient({ |
| 59 | + baseURL: "http://keeper.test", |
| 60 | + fetchImpl: mockFetch("{not-json"), |
| 61 | + }); |
| 62 | + await assert.rejects( |
| 63 | + () => client.getStatus(), |
| 64 | + (error: unknown) => { |
| 65 | + assert.ok(error instanceof StatusJsonParseError); |
| 66 | + assert.match(error.message, /invalid JSON/i); |
| 67 | + return true; |
| 68 | + }, |
| 69 | + ); |
| 70 | + }); |
| 71 | +}); |
| 72 | + |
| 73 | +describe("KeeperStatusClient non-success responses", () => { |
| 74 | + it("preserves typed StatusApiError for non-2xx JSON errors", async () => { |
| 75 | + const client = new KeeperStatusClient({ |
| 76 | + baseURL: "http://keeper.test", |
| 77 | + fetchImpl: mockFetch(JSON.stringify({ error: "round not found" }), 404), |
| 78 | + }); |
| 79 | + await assert.rejects( |
| 80 | + () => client.getRound(7), |
| 81 | + (error: unknown) => { |
| 82 | + assert.ok(error instanceof StatusApiError); |
| 83 | + assert.equal(error.status, 404); |
| 84 | + assert.equal(error.data.error, "round not found"); |
| 85 | + return true; |
| 86 | + }, |
| 87 | + ); |
| 88 | + }); |
| 89 | + |
| 90 | + it("preserves StatusApiError when non-2xx bodies are malformed JSON", async () => { |
| 91 | + const client = new KeeperStatusClient({ |
| 92 | + baseURL: "http://keeper.test", |
| 93 | + fetchImpl: mockFetch("not-json", 503), |
| 94 | + }); |
| 95 | + await assert.rejects( |
| 96 | + () => client.getHealth(), |
| 97 | + (error: unknown) => { |
| 98 | + assert.ok(error instanceof StatusApiError); |
| 99 | + assert.equal(error.status, 503); |
| 100 | + assert.equal(error.data.error, "invalid JSON body"); |
| 101 | + return true; |
| 102 | + }, |
| 103 | + ); |
| 104 | + }); |
| 105 | +}); |
0 commit comments