-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaltechauthors_api_test.ts
More file actions
189 lines (172 loc) · 5.83 KB
/
Copy pathcaltechauthors_api_test.ts
File metadata and controls
189 lines (172 loc) · 5.83 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
* caltechauthors_api_test.ts
*
* Unit tests for fetchAllRecords() run without any network access. All HTTP
* calls are stubbed via the injectable fetchFn/sleepFn options, per
* caltechauthors_api_pagination.md.
*
* Run:
* deno test caltechauthors_api_test.ts
*/
import { assertEquals, assertRejects } from "@std/assert";
import { fetchAllRecords } from "./caltechauthors_api.ts";
// ---------------------------------------------------------------------------
// Test helpers
// ---------------------------------------------------------------------------
interface MockPage {
status: number;
hits?: unknown[];
next?: string;
headers?: Record<string, string>;
}
function mockResponse(page: MockPage): Response {
const body = {
hits: { hits: page.hits ?? [], total: page.hits?.length ?? 0 },
links: page.next ? { next: page.next } : {},
};
const headers = new Headers(page.headers ?? {});
return {
ok: page.status >= 200 && page.status < 300,
status: page.status,
headers,
json: () => Promise.resolve(body),
text: () => Promise.resolve(JSON.stringify(body)),
// deno-lint-ignore no-explicit-any
} as any;
}
/** Builds a fetchFn that returns queued responses in order, one per call,
* and records every URL it was called with. */
function queuedFetch(pages: MockPage[]) {
const calls: string[] = [];
let i = 0;
const fetchFn = (input: string | URL | Request) => {
calls.push(String(input));
const page = pages[i];
i++;
if (page === undefined) {
throw new Error(`queuedFetch: no page queued for call ${i}`);
}
return Promise.resolve(mockResponse(page));
};
return { fetchFn, calls };
}
/** A sleepFn that resolves immediately but records how long it was asked to wait. */
function fakeSleep() {
const waits: number[] = [];
const sleepFn = (ms: number) => {
waits.push(ms);
return Promise.resolve();
};
return { sleepFn, waits };
}
// ---------------------------------------------------------------------------
// Single page
// ---------------------------------------------------------------------------
Deno.test("fetchAllRecords - single page with no links.next returns its hits", async () => {
const { fetchFn, calls } = queuedFetch([
{ status: 200, hits: [{ id: "a" }, { id: "b" }] },
]);
const records = await fetchAllRecords("https://example.org/api/records?q=x", {
fetchFn,
});
assertEquals(records, [{ id: "a" }, { id: "b" }]);
assertEquals(calls.length, 1);
});
// ---------------------------------------------------------------------------
// Multi-page follow
// ---------------------------------------------------------------------------
Deno.test("fetchAllRecords - follows links.next across multiple pages in order", async () => {
const { fetchFn, calls } = queuedFetch([
{ status: 200, hits: [{ id: 1 }, { id: 2 }], next: "https://example.org/page2" },
{ status: 200, hits: [{ id: 3 }, { id: 4 }], next: "https://example.org/page3" },
{ status: 200, hits: [{ id: 5 }, { id: 6 }] },
]);
const records = await fetchAllRecords("https://example.org/page1", { fetchFn });
assertEquals(records, [
{ id: 1 },
{ id: 2 },
{ id: 3 },
{ id: 4 },
{ id: 5 },
{ id: 6 },
]);
assertEquals(calls, [
"https://example.org/page1",
"https://example.org/page2",
"https://example.org/page3",
]);
});
// ---------------------------------------------------------------------------
// Page ceiling (safety net)
// ---------------------------------------------------------------------------
Deno.test("fetchAllRecords - stops at maxPages and warns instead of looping forever", async () => {
const { fetchFn, calls } = queuedFetch([
{ status: 200, hits: [{ id: 1 }], next: "https://example.org/page2" },
{ status: 200, hits: [{ id: 2 }], next: "https://example.org/page3" },
{ status: 200, hits: [{ id: 3 }] },
]);
const originalError = console.error;
let warned = false;
console.error = (...args: unknown[]) => {
warned = true;
originalError(...args);
};
try {
const records = await fetchAllRecords("https://example.org/page1", {
fetchFn,
maxPages: 2,
});
assertEquals(records, [{ id: 1 }, { id: 2 }]);
assertEquals(calls.length, 2);
assertEquals(warned, true);
} finally {
console.error = originalError;
}
});
// ---------------------------------------------------------------------------
// 429 retry/backoff
// ---------------------------------------------------------------------------
Deno.test("fetchAllRecords - retries once on 429 then succeeds", async () => {
const { fetchFn, calls } = queuedFetch([
{ status: 429, headers: { "Retry-After": "1" } },
{ status: 200, hits: [{ id: "a" }] },
]);
const { sleepFn, waits } = fakeSleep();
const records = await fetchAllRecords("https://example.org/page1", {
fetchFn,
sleepFn,
});
assertEquals(records, [{ id: "a" }]);
assertEquals(calls.length, 2);
assertEquals(waits.length, 1);
});
Deno.test("fetchAllRecords - throws once retries are exhausted on repeated 429s", async () => {
const { fetchFn } = queuedFetch([
{ status: 429 },
{ status: 429 },
{ status: 429 },
]);
const { sleepFn } = fakeSleep();
await assertRejects(
() =>
fetchAllRecords("https://example.org/page1", {
fetchFn,
sleepFn,
maxRetries: 3,
}),
Error,
);
});
// ---------------------------------------------------------------------------
// Non-429 errors fail fast, no retry
// ---------------------------------------------------------------------------
Deno.test("fetchAllRecords - throws immediately on a non-429 error without retrying", async () => {
const { fetchFn, calls } = queuedFetch([
{ status: 500 },
]);
await assertRejects(
() => fetchAllRecords("https://example.org/page1", { fetchFn }),
Error,
);
assertEquals(calls.length, 1);
});