-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.spec.ts
42 lines (34 loc) · 1.34 KB
/
api.spec.ts
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
import {bufferCount} from "rxjs/operators";
import {lastValueFrom} from "rxjs";
import test, {ExecutionContext} from "ava";
import {API_IMPLEMENTATION} from "./";
const apiActionTests: Record<keyof typeof API_IMPLEMENTATION, (t: ExecutionContext) => void> = {
evaluateMathExpression: async (t) => {
t.is(25, await API_IMPLEMENTATION.evaluateMathExpression("12 * 2 + 1"));
},
httpPing: async (t) => {
const entries = [
{address: "google.com", attempts: 1},
{address: "github.com"},
{address: "1.1.1.1"},
];
const results = await lastValueFrom(
API_IMPLEMENTATION
.httpPing(entries)
.pipe(bufferCount(entries.length)),
);
// type checking like assertions implemented below are not really needed since TypeScript handles the type checking
t.is(results.length, entries.length);
for (const result of results) {
if ("time" in result) {
t.true(typeof result.time === "number");
t.false("error" in result);
continue;
}
t.true("error" in result && typeof result.error === "string");
}
},
};
for (const [apiMethodName, apiMethodTest] of Object.entries(apiActionTests)) {
test(`API: ${apiMethodName}`, apiMethodTest);
}