-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.test.ts
More file actions
299 lines (250 loc) · 9.38 KB
/
Copy pathserver.test.ts
File metadata and controls
299 lines (250 loc) · 9.38 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
// src/__tests__/server.test.ts
import { describe, it, expect, vi } from "vitest";
import request from "supertest";
import type { Runtime } from "../types.js";
vi.mock("../mcp.js", () => ({
createMcpServer: vi.fn().mockReturnValue({
connect: vi.fn(),
}),
}));
vi.mock("../store.js", async () => {
const actual = await vi.importActual<typeof import("../store.js")>("../store.js");
return {
...actual,
saveState: vi.fn(),
};
});
import { createApp } from "../server.js";
const now = new Date().toISOString();
const botHeaders = { "x-spawndock-bot-secret": "spawndock-dev-bot-secret" };
process.env.TELEGRAM_BOT_USERNAME ??= "rustgpt_bot";
process.env.TELEGRAM_MINI_APP_SHORT_NAME ??= "tma";
function createRuntime(): Runtime {
return {
state: { projects: [], pairingTokens: [], deviceCredentials: [], tunnelSessions: [] },
connectionsBySlug: new Map(),
pendingResponses: new Map(),
};
}
function createAuthorizedRuntime(): Runtime {
return {
...createRuntime(),
state: {
projects: [],
pairingTokens: [],
deviceCredentials: [{
id: "device_1",
projectId: "project_1",
label: "Test Device",
secret: "secret_1",
mcpApiKey: "mcp_test_key",
createdAt: now,
revokedAt: null,
}],
tunnelSessions: [],
},
};
}
const authorizedHeaders = {
authorization: "Bearer mcp_test_key",
};
const mockRuntime: Runtime = {
state: { projects: [], pairingTokens: [], deviceCredentials: [], tunnelSessions: [] },
connectionsBySlug: new Map(),
pendingResponses: new Map(),
};
describe("Express server", () => {
it("GET /health returns 200 with status ok", async () => {
const app = createApp(mockRuntime);
const res = await request(app).get("/health");
expect(res.status).toBe(200);
expect(res.body.status).toBe("ok");
});
it("GET /mcp/health returns 200 with status ok", async () => {
const app = createApp(mockRuntime);
const res = await request(app).get("/mcp/health");
expect(res.status).toBe(200);
expect(res.body.status).toBe("ok");
});
it("GET /mcp/sse requires MCP API key", async () => {
const app = createApp(createRuntime());
const res = await request(app).get("/mcp/sse");
expect(res.status).toBe(401);
expect(res.body.error).toBe("mcp_unauthorized");
});
it("GET /mcp/sse accepts valid MCP API key before route handling", async () => {
const app = createApp(createAuthorizedRuntime());
const res = await request(app)
.get("/mcp/sse")
.set(authorizedHeaders);
expect(res.status).toBe(405);
});
it("POST /messages with unknown session returns 400", async () => {
const app = createApp(createAuthorizedRuntime());
const res = await request(app)
.post("/messages?sessionId=nonexistent")
.set(authorizedHeaders)
.send({});
expect(res.status).toBe(400);
expect(res.body.error).toBe("Unknown session");
});
it("POST /mcp/messages with unknown session returns 400", async () => {
const app = createApp(createAuthorizedRuntime());
const res = await request(app)
.post("/mcp/messages?sessionId=nonexistent")
.set(authorizedHeaders)
.send({});
expect(res.status).toBe(400);
expect(res.body.error).toBe("Unknown session");
});
it("POST /v1/bootstrap/claim returns flat bootstrap fields", async () => {
const app = createApp({
state: { projects: [], pairingTokens: [], deviceCredentials: [], tunnelSessions: [] },
connectionsBySlug: new Map(),
pendingResponses: new Map(),
});
const created = await request(app)
.post("/api/projects")
.set(botHeaders)
.send({ title: "Test App", ownerTelegramId: 42 });
expect(created.status).toBe(201);
expect(created.body.pairingToken.token).toBeTruthy();
const claimed = await request(app)
.post("/v1/bootstrap/claim")
.send({ token: created.body.pairingToken.token });
expect(claimed.status).toBe(201);
expect(claimed.body.projectId).toBeTruthy();
expect(claimed.body.projectSlug).toBeTruthy();
expect(claimed.body.deviceSecret).toBeTruthy();
expect(claimed.body.mcpApiKey).toBeTruthy();
expect(claimed.body.controlPlaneUrl).toBeTruthy();
expect(claimed.body.launchUrl).toBeTruthy();
});
it("POST /api/pairing/inspect returns project metadata without consuming the token", async () => {
const app = createApp(createRuntime());
const created = await request(app)
.post("/api/projects")
.set(botHeaders)
.send({ title: "Test App", ownerTelegramId: 42 });
expect(created.status).toBe(201);
expect(created.body.pairingToken.token).toBeTruthy();
const inspected = await request(app)
.post("/api/pairing/inspect")
.send({ token: created.body.pairingToken.token });
expect(inspected.status).toBe(200);
expect(inspected.body.projectId).toBe(created.body.project.id);
expect(inspected.body.projectSlug).toBe(created.body.project.slug);
expect(inspected.body.expiresAt).toBe(created.body.pairingToken.expiresAt);
const claimed = await request(app)
.post("/v1/bootstrap/claim")
.send({ token: created.body.pairingToken.token });
expect(claimed.status).toBe(201);
expect(claimed.body.projectSlug).toBe(created.body.project.slug);
});
it("POST /v1/bootstrap/claim replays the same credential when the token was already claimed", async () => {
const app = createApp({
state: { projects: [], pairingTokens: [], deviceCredentials: [], tunnelSessions: [] },
connectionsBySlug: new Map(),
pendingResponses: new Map(),
});
const created = await request(app)
.post("/api/projects")
.set(botHeaders)
.send({ title: "Replay App", ownerTelegramId: 42 });
const firstClaim = await request(app)
.post("/v1/bootstrap/claim")
.send({ token: created.body.pairingToken.token });
const secondClaim = await request(app)
.post("/v1/bootstrap/claim")
.send({ token: created.body.pairingToken.token });
expect(firstClaim.status).toBe(201);
expect(secondClaim.status).toBe(201);
expect(secondClaim.body.projectId).toBe(firstClaim.body.projectId);
expect(secondClaim.body.projectSlug).toBe(firstClaim.body.projectSlug);
expect(secondClaim.body.deviceSecret).toBe(firstClaim.body.deviceSecret);
expect(secondClaim.body.mcpApiKey).toBe(firstClaim.body.mcpApiKey);
});
it("POST /api/projects requires bot authorization", async () => {
const app = createApp(createRuntime());
const res = await request(app)
.post("/api/projects")
.send({ title: "Test App" });
expect(res.status).toBe(401);
expect(res.body.error).toBe("bot_unauthorized");
});
it("POST /projects accepts bot-authorized creation without /api prefix", async () => {
const app = createApp(createRuntime());
const res = await request(app)
.post("/projects")
.set(botHeaders)
.send({ title: "Test App", ownerTelegramId: 42 });
expect(res.status).toBe(201);
expect(res.body.project.slug).toBe("test-app");
});
it("GET /api/projects/:slug/launch-url requires owner and bot authorization", async () => {
const runtime = createRuntime();
runtime.state.projects.push({
id: "project_1",
slug: "demo-app",
title: "Demo App",
templateId: "nextjs-template",
ownerTelegramId: 42,
createdAt: now,
status: "draft",
});
const app = createApp(runtime);
const unauthorized = await request(app).get("/api/projects/demo-app/launch-url");
expect(unauthorized.status).toBe(401);
const wrongOwner = await request(app)
.get("/api/projects/demo-app/launch-url?ownerTelegramId=7")
.set(botHeaders);
expect(wrongOwner.status).toBe(404);
const owner = await request(app)
.get("/api/projects/demo-app/launch-url?ownerTelegramId=42")
.set(botHeaders);
expect(owner.status).toBe(200);
expect(owner.body.slug).toBe("demo-app");
expect(owner.body.status).toBe("offline");
});
it("GET /tma redirects to preview preserving query string", async () => {
const app = createApp(mockRuntime);
const res = await request(app)
.get("/tma?tgWebAppStartParam=demo-app&foo=bar")
.redirects(0);
expect(res.status).toBe(302);
expect(res.headers.location).toBe("https://spawn-dock.w3voice.net/preview/demo-app?tgWebAppStartParam=demo-app&foo=bar");
});
it("GET /tma returns 400 when project slug is missing", async () => {
const app = createApp(mockRuntime);
const res = await request(app).get("/tma");
expect(res.status).toBe(400);
expect(res.body.error).toBe("missing_project");
});
it("does not rate-limit preview requests", async () => {
const runtime: Runtime = {
state: {
projects: [
{
id: "project_1",
slug: "demo-app",
title: "Demo App",
templateId: "nextjs-template",
ownerTelegramId: null,
createdAt: new Date().toISOString(),
status: "active",
},
],
pairingTokens: [],
deviceCredentials: [],
tunnelSessions: [],
},
connectionsBySlug: new Map(),
pendingResponses: new Map(),
};
const app = createApp(runtime);
const responses = await Promise.all(
Array.from({ length: 12 }, () => request(app).get("/preview/demo-app")),
);
expect(responses.every((response) => response.status !== 429)).toBe(true);
});
});