-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.test.ts
More file actions
329 lines (289 loc) · 12.6 KB
/
Copy pathserver.test.ts
File metadata and controls
329 lines (289 loc) · 12.6 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
324
325
326
327
328
329
import { join, resolve } from "node:path";
import { mkdtemp, mkdir, readFile, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { buildServiceStartPlan, createServerApp, parseRequestDelayMs } from "../src/coordinator/server";
import { inject } from "./fixtures/inject";
import { startMockService } from "./fixtures/mock-service";
describe("coordinator server", () => {
it("returns config with TomTom key presence only", async () => {
const app = createServerApp({ env: { TOMTOM_API_KEY: "secret-value" } });
const response = await inject(app, "/api/config");
expect(response.statusCode).toBe(200);
expect(JSON.parse(response.body)).toEqual({ hasTomTomApiKey: true });
expect(response.body).not.toContain("secret-value");
});
it("returns the pinned fixture set", async () => {
const app = createServerApp({ env: {} });
const response = await inject(app, "/api/fixtures");
expect(response.statusCode).toBe(200);
expect(JSON.parse(response.body).seed).toBe(20260617);
});
it("returns expanded cases from the pinned fixture set", async () => {
const app = createServerApp({ env: { TOMTOM_API_KEY: "secret-value" } });
const response = await inject(app, "/api/cases?profile=Deep");
expect(response.statusCode).toBe(200);
expect(response.body).not.toContain("secret-value");
const cases = JSON.parse(response.body);
expect(cases.map((item: { id: string }) => item.id)).toContain("version-json");
expect(cases.some((item: { fixtureId?: string }) => item.fixtureId === "capital-nld-amsterdam")).toBe(true);
});
it("treats Custom as Fast because only Fast and Deep are supported profiles", async () => {
const app = createServerApp({ env: {} });
const customResponse = await inject(app, "/api/cases?profile=Custom");
const fastResponse = await inject(app, "/api/cases?profile=Fast");
expect(customResponse.statusCode).toBe(200);
expect(JSON.parse(customResponse.body).map((item: { id: string }) => item.id)).toEqual(
JSON.parse(fastResponse.body).map((item: { id: string }) => item.id)
);
});
it("returns profile-specific fixtures", async () => {
const app = createServerApp({ env: {} });
const fastResponse = await inject(app, "/api/fixtures?profile=Fast");
const deepResponse = await inject(app, "/api/fixtures?profile=Deep");
expect(fastResponse.statusCode).toBe(200);
expect(deepResponse.statusCode).toBe(200);
expect(JSON.parse(fastResponse.body).points.length).toBeLessThan(JSON.parse(deepResponse.body).points.length);
});
it("reports service status without auto-starting services", async () => {
const app = createServerApp({ env: {} });
const response = await inject(app, "/api/services");
expect(response.statusCode).toBe(200);
expect(JSON.parse(response.body)).toMatchObject({
production: {
label: "Production API",
mode: "manual",
baseUrl: "http://127.0.0.1:8081",
sourcePath: "../mapcode-rest-service"
},
candidate: {
label: "Candidate API",
mode: "manual",
baseUrl: "http://127.0.0.1:8082",
sourcePath: "../mapcode-rest-service-ts"
}
});
});
it("stores a source repo path with service configuration before the API is running", async () => {
const app = createServerApp({ env: {} });
const response = await inject(app, "/api/services/candidate/config", {
method: "POST",
body: { baseUrl: "http://127.0.0.1:9082", sourcePath: "/tmp/mapcode-rest-service-ts" }
});
const services = await inject(app, "/api/services");
expect(response.statusCode).toBe(200);
expect(JSON.parse(services.body).candidate).toMatchObject({
baseUrl: "http://127.0.0.1:9082",
sourcePath: "/tmp/mapcode-rest-service-ts",
availability: "unavailable"
});
});
it("checks service availability through /mapcode/status", async () => {
const app = createServerApp({ env: {} });
const service = await startMockService({ "/mapcode/status": { status: 200, body: "ok" } });
try {
const response = await inject(app, "/api/services/candidate/config", {
method: "POST",
body: { baseUrl: service.baseUrl, sourcePath: "/tmp/mapcode-rest-service-ts" }
});
expect(response.statusCode).toBe(200);
expect(JSON.parse(response.body)).toMatchObject({ availability: "available" });
expect(service.requests.map((request) => request.path)).toContain("/mapcode/status");
} finally {
await service.close();
}
});
it("preserves path-prefixed service URLs when saving and checking configuration", async () => {
const app = createServerApp({ env: {} });
const service = await startMockService({
"/mapcode-rest-service-ts/mapcode/status": { status: 200, body: "ok" }
});
try {
const baseUrl = `${service.baseUrl}/mapcode-rest-service-ts`;
const response = await inject(app, "/api/services/candidate/config", {
method: "POST",
body: { baseUrl, sourcePath: "/tmp/mapcode-rest-service-ts" }
});
expect(response.statusCode).toBe(200);
expect(JSON.parse(response.body)).toMatchObject({
baseUrl,
availability: "available"
});
expect(service.requests.map((request) => request.path)).toEqual(["/mapcode-rest-service-ts/mapcode/status"]);
} finally {
await service.close();
}
});
it("checks a draft path-prefixed service URL from the check payload", async () => {
const app = createServerApp({ env: {} });
const service = await startMockService({
"/mapcode-rest-service-ts/mapcode/status": { status: 200, body: "ok" }
});
try {
const baseUrl = `${service.baseUrl}/mapcode-rest-service-ts`;
const response = await inject(app, "/api/services/candidate/check", {
method: "POST",
body: { baseUrl }
});
expect(response.statusCode).toBe(200);
expect(JSON.parse(response.body)).toMatchObject({
baseUrl,
availability: "available"
});
expect(service.requests.map((request) => request.path)).toEqual(["/mapcode-rest-service-ts/mapcode/status"]);
} finally {
await service.close();
}
});
it("stops both managed APIs and reports them as unavailable", async () => {
const app = createServerApp({ env: {} });
const response = await inject(app, "/api/services/stop", { method: "POST" });
expect(response.statusCode).toBe(200);
expect(JSON.parse(response.body)).toMatchObject({
production: {
availability: "unavailable",
logs: ["Stopped Production API"]
},
candidate: {
availability: "unavailable",
logs: ["Stopped Candidate API"]
}
});
});
it("stops configured API ports even when the coordinator has no child process handles", async () => {
const stoppedBaseUrls: string[] = [];
const app = createServerApp({
env: {},
stopPortListener: async (baseUrl) => {
stoppedBaseUrls.push(baseUrl);
}
});
await inject(app, "/api/services/production/config", {
method: "POST",
body: { baseUrl: "http://127.0.0.1:19081", sourcePath: "../mapcode-rest-service" }
});
await inject(app, "/api/services/candidate/config", {
method: "POST",
body: { baseUrl: "http://127.0.0.1:19082", sourcePath: "../mapcode-rest-service-ts" }
});
const response = await inject(app, "/api/services/stop", { method: "POST" });
expect(response.statusCode).toBe(200);
expect(stoppedBaseUrls).toEqual(["http://127.0.0.1:19081", "http://127.0.0.1:19082"]);
});
it("saves active service URLs, source trees, and resolved versions in reports", async () => {
const app = createServerApp({ env: {} });
const production = await startMockService({
"/java/mapcode/status": { status: 200, body: "ok" },
"/java/mapcode/version": { status: 200, body: '{"version":"2.4.19.3"}' }
});
const candidate = await startMockService({
"/ts/mapcode/status": { status: 200, body: "ok" },
"/ts/mapcode/version": { status: 200, body: '{"version":"2.5.1"}' }
});
try {
const productionBaseUrl = `${production.baseUrl}/java`;
const candidateBaseUrl = `${candidate.baseUrl}/ts`;
await inject(app, "/api/services/production/config", {
method: "POST",
body: { baseUrl: productionBaseUrl, sourcePath: "/tmp/mapcode-rest-service" }
});
await inject(app, "/api/services/candidate/config", {
method: "POST",
body: { baseUrl: candidateBaseUrl, sourcePath: "/tmp/mapcode-rest-service-ts" }
});
const response = await inject(app, "/api/report/save", { method: "POST" });
const report = JSON.parse(response.body);
const json = JSON.parse(await readFile(report.jsonPath, "utf8"));
expect(response.statusCode).toBe(200);
expect(report.markdown).toContain("- Base URL: `" + productionBaseUrl + "`");
expect(report.markdown).toContain("- Source tree: `/tmp/mapcode-rest-service`");
expect(report.markdown).toContain("- Version: `2.4.19.3`");
expect(report.markdown).toContain("- Base URL: `" + candidateBaseUrl + "`");
expect(report.markdown).toContain("- Source tree: `/tmp/mapcode-rest-service-ts`");
expect(report.markdown).toContain("- Version: `2.5.1`");
expect(json.services.production).toMatchObject({
baseUrl: productionBaseUrl,
sourcePath: "/tmp/mapcode-rest-service",
version: "2.4.19.3"
});
expect(json.services.candidate).toMatchObject({
baseUrl: candidateBaseUrl,
sourcePath: "/tmp/mapcode-rest-service-ts",
version: "2.5.1"
});
expect(production.requests.map((request) => request.path)).toEqual(["/java/mapcode/status", "/java/mapcode/version"]);
expect(candidate.requests.map((request) => request.path)).toEqual(["/ts/mapcode/status", "/ts/mapcode/version"]);
} finally {
await production.close();
await candidate.close();
}
});
it("rejects automatic start when the source repo path does not exist", async () => {
const app = createServerApp({ env: {} });
const response = await inject(app, "/api/services/production/start", {
method: "POST",
body: { baseUrl: "http://127.0.0.1:9081", sourcePath: "/tmp/does-not-exist-mapcode-api-test" }
});
expect(response.statusCode).toBe(400);
expect(JSON.parse(response.body)).toMatchObject({
error: "Source path unavailable",
service: "Production API"
});
});
it("builds a Java repo by installing prod artifacts before running Jetty in deployment", async () => {
const dir = await mkdtemp(join(tmpdir(), "mapcode-java-repo-"));
await writeFile(join(dir, "pom.xml"), "<project />");
await mkdir(join(dir, "deployment"));
const plan = buildServiceStartPlan("candidate", "http://127.0.0.1:9081", dir);
expect(plan).toMatchObject([
{
command: "mvn",
args: ["install", "-Pprod"],
cwd: resolve(dir),
waitForExit: true
},
{
command: "mvn",
args: ["-Dmaven.httpserver.port=9081", "jetty:run"],
cwd: resolve(dir, "deployment"),
waitForExit: false
}
]);
});
it("starts a Node repo with npm regardless of service role", async () => {
const dir = await mkdtemp(join(tmpdir(), "mapcode-node-repo-"));
await writeFile(join(dir, "package.json"), "{}");
const plan = buildServiceStartPlan("production", "http://127.0.0.1:9082", dir);
expect(plan).toMatchObject([
{
command: "npm",
args: ["run", "dev"],
cwd: resolve(dir),
env: expect.objectContaining({ PORT: "9082" }),
waitForExit: false
}
]);
});
it("blocks run start and names APIs that are unavailable", async () => {
const app = createServerApp({ env: {} });
await inject(app, "/api/services/production/config", {
method: "POST",
body: { baseUrl: "http://127.0.0.1:19081", sourcePath: "../mapcode-rest-service" }
});
await inject(app, "/api/services/candidate/config", {
method: "POST",
body: { baseUrl: "http://127.0.0.1:19082", sourcePath: "../mapcode-rest-service-ts" }
});
const response = await inject(app, "/api/run/start", { method: "POST", body: { profile: "Fast" } });
expect(response.statusCode).toBe(409);
expect(JSON.parse(response.body)).toEqual({
error: "APIs unavailable",
unavailable: ["Production API", "Candidate API"]
});
});
it("clamps request delay seconds from run start payload", async () => {
expect(parseRequestDelayMs(undefined)).toBe(0);
expect(parseRequestDelayMs(-1)).toBe(0);
expect(parseRequestDelayMs(2.5)).toBe(2500);
expect(parseRequestDelayMs(10)).toBe(5000);
});
});