g1t/apps/status/src/probe.test.ts

40 lines2,170 bytesCodeBlame
1import assert from "node:assert/strict";
2import { test } from "node:test";
3
4import { combine, runCheck, step } from "./probe.ts";
5
6const answer = (status: number) => async () => new Response("x", { status });
7
8test("a step works on 2xx, or on exactly the status it expects", async () => {
9 assert.equal((await step(answer(200), { url: "https://a/" })).ok, true);
10 const refused = await step(answer(502), { url: "https://a/" });
11 assert.deepEqual([refused.ok, refused.error], [false, "HTTP 502"]);
12 assert.equal((await step(answer(401), { url: "https://a/", expect: 401 })).ok, true);
13 assert.equal((await step(answer(200), { url: "https://a/", expect: 401 })).ok, false);
14});
15
16test("a step that cannot connect, or takes too long, fails with why", async () => {
17 const broken = await step(async () => Promise.reject(new TypeError("boom")), { url: "https://a/" });
18 assert.equal(broken.error, "could not connect");
19 const slow = await step(
20 (_url, init) => new Promise((_, reject) => init.signal?.addEventListener("abort", () => reject(new Error("aborted")))),
21 { url: "https://a/" },
22 20,
23 );
24 assert.equal(slow.error, "timed out");
25});
26
27test("a check's steps: all must work, and it takes as long as the slowest", () => {
28 assert.deepEqual(combine([{ ok: true, ms: 10 }, { ok: true, ms: 30 }]), { ok: true, ms: 30 });
29 assert.deepEqual(combine([{ ok: true, ms: 10 }, { ok: false, ms: 5, error: "HTTP 500" }]), { ok: false, ms: 10, error: "HTTP 500" });
30});
31
32test("runCheck: billing without a binding, and a part with no check, are null", async () => {
33 const probers = { fetch: answer(200), billing: null };
34 assert.equal(await runCheck({ kind: "billing" }, probers), null);
35 assert.equal(await runCheck({ kind: "none" }, probers), null);
36 assert.equal((await runCheck({ kind: "billing" }, { ...probers, billing: async () => ({ version: 1 }) }))!.ok, true);
37 assert.equal((await runCheck({ kind: "billing" }, { ...probers, billing: async () => null }))!.error, "no price book");
38 const mixed = await runCheck({ kind: "http", steps: [{ url: "https://a/" }, { url: "https://b/", expect: 401 }] }, probers);
39 assert.equal(mixed!.ok, false);
40});