-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.integration.test.ts
More file actions
323 lines (282 loc) · 12.1 KB
/
Copy pathapi.integration.test.ts
File metadata and controls
323 lines (282 loc) · 12.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import { describe, expect, it } from "vitest";
const BASE_URL = "https://elyos-interview-907656039105.europe-west2.run.app";
const API_KEY = process.env["ELYOS_API_KEY"] ?? "";
const REQUEST_TIMEOUT_MS = 30_000;
// Helper: raw fetch with full control over headers and params
const get = async (
path: string,
params: Record<string, string> = {},
headers: Record<string, string> = { "X-API-Key": API_KEY },
): Promise<{ status: number; body: string; ms: number }> => {
const url = new URL(path, BASE_URL);
for (const [k, v] of Object.entries(params)) url.searchParams.set(k, v);
const start = Date.now();
const res = await fetch(url.toString(), {
headers,
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
});
const body = await res.text();
return { status: res.status, body, ms: Date.now() - start };
};
// ---------------------------------------------------------------------------
// Authentication
// ---------------------------------------------------------------------------
describe("authentication", () => {
it("weather: missing API key returns non-200 status", async () => {
const { status } = await get("/weather", { location: "London" }, {});
expect(status).not.toBe(200);
});
it("weather: invalid API key returns non-200 status", async () => {
const { status } = await get(
"/weather",
{ location: "London" },
{ "X-API-Key": "invalid-key-xyz" },
);
expect(status).not.toBe(200);
});
it("research: missing API key returns non-200 status", async () => {
const { status } = await get("/research", { topic: "solar energy" }, {});
expect(status).not.toBe(200);
}, 15_000);
it("research: invalid API key returns non-200 status", async () => {
const { status } = await get(
"/research",
{ topic: "solar energy" },
{ "X-API-Key": "invalid-key-xyz" },
);
expect(status).not.toBe(200);
}, 15_000);
});
// ---------------------------------------------------------------------------
// Weather endpoint – happy path
// ---------------------------------------------------------------------------
describe("GET /weather – happy path", () => {
it("returns 200 for a valid city", async () => {
const { status, body } = await get("/weather", { location: "London" });
expect(status).toBe(200);
expect(body.trim()).not.toBe("");
});
it("response is not an empty JSON object {}", async () => {
const { body } = await get("/weather", { location: "London" });
// cli.ts retries on empty `{}` – confirm it doesn't happen in normal usage
expect(body.trim()).not.toBe("{}");
});
it("response body can be parsed as JSON", async () => {
const { body } = await get("/weather", { location: "London" });
expect(() => JSON.parse(body)).not.toThrow();
});
it("response contains weather-related data for the requested city", async () => {
const { body } = await get("/weather", { location: "London" });
const lower = body.toLowerCase();
// Expect at least one of: temperature, weather, humidity, wind, degrees
const weatherKeywords = [
"temperature",
"weather",
"humidity",
"wind",
"celsius",
"fahrenheit",
"cloud",
"rain",
"sunny",
"forecast",
"°",
];
const hasWeatherData = weatherKeywords.some((kw) => lower.includes(kw));
expect(hasWeatherData).toBe(true);
});
it("response references the requested city (London)", async () => {
const { body } = await get("/weather", { location: "London" });
expect(body.toLowerCase()).toContain("london");
});
it("city with spaces (New York) returns 200 with data", async () => {
const { status, body } = await get("/weather", {
location: "New York",
});
expect(status).toBe(200);
expect(body.trim()).not.toBe("");
});
it("response references the multi-word city (New York)", async () => {
const { body } = await get("/weather", { location: "New York" });
// Expect the response to mention New York, not a different city
const lower = body.toLowerCase();
expect(lower).toContain("new york");
});
});
// ---------------------------------------------------------------------------
// Weather endpoint – case sensitivity
// ---------------------------------------------------------------------------
describe("GET /weather – case sensitivity", () => {
it("lowercase and mixed-case city produce the same result", async () => {
const [r1, r2] = await Promise.all([
get("/weather", { location: "london" }),
get("/weather", { location: "London" }),
]);
expect(r1.status).toBe(200);
expect(r2.status).toBe(200);
// Both should be non-empty and contain the same essential data
expect(r1.body.toLowerCase()).toContain("london");
expect(r2.body.toLowerCase()).toContain("london");
});
it("all-uppercase city returns 200", async () => {
const { status } = await get("/weather", { location: "LONDON" });
expect(status).toBe(200);
});
});
// ---------------------------------------------------------------------------
// Weather endpoint – edge cases / quirks
// ---------------------------------------------------------------------------
describe("GET /weather – edge cases", () => {
it("non-existent city returns a clear error or non-200 status (not silent success)", async () => {
const { status, body } = await get("/weather", {
location: "Qxzplorf99999",
});
// Either status should indicate failure, or body should mention error/not found
const isErrorStatus = status >= 400;
const isErrorBody = /error|not found|unknown|invalid/i.test(body);
expect(isErrorStatus || isErrorBody).toBe(true);
});
it("empty location string does not return weather data as if it were valid", async () => {
const { status, body } = await get("/weather", { location: "" });
const isErrorStatus = status >= 400;
const isErrorBody =
/error|not found|unknown|invalid|required|missing/i.test(body);
// Empty location should result in an error (not a 200 with data)
expect(isErrorStatus || isErrorBody).toBe(true);
});
it("missing location parameter returns a non-200 status or error body", async () => {
const { status, body } = await get("/weather");
const isErrorStatus = status >= 400;
const isErrorBody = /error|missing|required|parameter/i.test(body);
expect(isErrorStatus || isErrorBody).toBe(true);
});
it("numeric string as location returns an error or empty data (not real weather)", async () => {
const { status, body } = await get("/weather", { location: "12345" });
// A numeric string is not a valid city name
const isErrorStatus = status >= 400;
const isErrorBody = /error|not found|unknown|invalid/i.test(body);
expect(isErrorStatus || isErrorBody).toBe(true);
});
it("SQL injection attempt in location does not cause server error (500)", async () => {
const { status } = await get("/weather", {
location: "'; DROP TABLE locations; --",
});
expect(status).not.toBe(500);
});
it("consecutive calls for same city return consistent (non-empty) responses", async () => {
const r1 = await get("/weather", { location: "Tokyo" });
const r2 = await get("/weather", { location: "Tokyo" });
expect(r1.body.trim()).not.toBe("");
expect(r2.body.trim()).not.toBe("");
// Core data (at minimum both should reference the city)
expect(r1.body.toLowerCase()).toContain("tokyo");
expect(r2.body.toLowerCase()).toContain("tokyo");
});
it("non-ASCII city name (Zürich) returns 200 or graceful error", async () => {
const { status } = await get("/weather", { location: "Zürich" });
// Should either work (200) or return a clean 4xx, not a 500
expect(status).not.toBe(500);
});
});
// ---------------------------------------------------------------------------
// Research endpoint – happy path
// ---------------------------------------------------------------------------
describe("GET /research – happy path", () => {
it("returns 200 for a valid topic", async () => {
const { status, body } = await get("/research", {
topic: "solar energy",
});
expect(status).toBe(200);
expect(body.trim()).not.toBe("");
}, 15_000);
it("response is not an empty JSON object {}", async () => {
const { body } = await get("/research", { topic: "solar energy" });
expect(body.trim()).not.toBe("{}");
}, 15_000);
it("response body can be parsed as JSON", async () => {
const { body } = await get("/research", { topic: "climate change" });
expect(() => JSON.parse(body)).not.toThrow();
}, 15_000);
it("response contains content relevant to the researched topic", async () => {
const { body } = await get("/research", { topic: "solar energy" });
const lower = body.toLowerCase();
// Solar energy research should mention relevant keywords
const relevantKeywords = [
"solar",
"energy",
"sun",
"panel",
"photovoltaic",
"renewable",
"electricity",
"power",
];
const hasRelevantContent = relevantKeywords.some((kw) =>
lower.includes(kw),
);
expect(hasRelevantContent).toBe(true);
}, 15_000);
});
// ---------------------------------------------------------------------------
// Research endpoint – response timing
// ---------------------------------------------------------------------------
describe("GET /research – response timing", () => {
it("response time is at least 3 seconds (as documented)", async () => {
const { ms } = await get("/research", { topic: "quantum computing" });
expect(ms).toBeGreaterThanOrEqual(3_000);
}, 20_000);
it("response time does not exceed 8 seconds (as documented)", async () => {
const { ms } = await get("/research", { topic: "machine learning" });
// Documented maximum is 8 seconds; allow 2s of network overhead
expect(ms).toBeLessThanOrEqual(10_000);
}, 20_000);
});
// ---------------------------------------------------------------------------
// Research endpoint – edge cases / quirks
// ---------------------------------------------------------------------------
describe("GET /research – edge cases", () => {
it("empty topic string does not return research data as if valid", async () => {
const { status, body } = await get("/research", { topic: "" });
const isErrorStatus = status >= 400;
const isErrorBody = /error|not found|invalid|required|missing/i.test(body);
expect(isErrorStatus || isErrorBody).toBe(true);
}, 15_000);
it("missing topic parameter returns a non-200 status or error body", async () => {
const { status, body } = await get("/research");
const isErrorStatus = status >= 400;
const isErrorBody = /error|missing|required|parameter/i.test(body);
expect(isErrorStatus || isErrorBody).toBe(true);
}, 15_000);
it("two requests for the same topic return consistent (non-empty) responses", async () => {
const [r1, r2] = await Promise.all([
get("/research", { topic: "solar energy" }),
get("/research", { topic: "solar energy" }),
]);
expect(r1.body.trim()).not.toBe("");
expect(r2.body.trim()).not.toBe("");
// Both results should be about solar energy
expect(r1.body.toLowerCase()).toMatch(/solar|energy|renewable/);
expect(r2.body.toLowerCase()).toMatch(/solar|energy|renewable/);
}, 20_000);
it("different topics return different content", async () => {
const [r1, r2] = await Promise.all([
get("/research", { topic: "solar energy" }),
get("/research", { topic: "ocean biology" }),
]);
// Responses should not be identical for unrelated topics
expect(r1.body).not.toBe(r2.body);
}, 20_000);
it("research on an obscure topic returns a meaningful (non-empty) response", async () => {
const { status, body } = await get("/research", {
topic: "knot theory in topology",
});
expect(status).toBe(200);
expect(body.trim().length).toBeGreaterThan(50);
}, 15_000);
it("SQL injection attempt in topic does not cause server error (500)", async () => {
const { status } = await get("/research", {
topic: "'; DROP TABLE research; --",
});
expect(status).not.toBe(500);
}, 15_000);
});