-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
390 lines (361 loc) · 11.4 KB
/
Copy pathtest.js
File metadata and controls
390 lines (361 loc) · 11.4 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// Test suite for Chomptron AI Recipe Generator
console.log("Running Chomptron tests...\n");
const fs = require("fs");
const http = require("http");
let passed = 0;
let failed = 0;
function assert(condition, message) {
if (condition) {
console.log(`✓ ${message}`);
passed++;
} else {
console.log(`✗ ${message}`);
failed++;
}
}
// Static file tests
console.log("File Structure Tests:");
assert(fs.existsSync("./server.js"), "server.js exists");
assert(fs.existsSync("./index.html"), "index.html exists");
assert(fs.existsSync("./Dockerfile"), "Dockerfile exists");
assert(fs.existsSync("./cloudbuild.yaml"), "cloudbuild.yaml exists");
// Package dependencies test
console.log("\nDependency Tests:");
const pkg = require("./package.json");
assert(pkg.dependencies.express, "express dependency exists");
assert(
pkg.dependencies["@google/generative-ai"],
"Gemini AI dependency exists",
);
assert(pkg.name === "chomptron", "package name is chomptron");
assert(
pkg.description.includes("recipe"),
"package description mentions recipes",
);
// Dockerfile validation
console.log("\nDocker Configuration Tests:");
const dockerfile = fs.readFileSync("./Dockerfile", "utf8");
assert(dockerfile.includes("FROM node"), "Dockerfile uses Node base image");
assert(dockerfile.includes("npm"), "Dockerfile installs dependencies");
assert(dockerfile.includes("EXPOSE 8080"), "Dockerfile exposes port 8080");
// Cloud Build configuration
console.log("\nCloud Build Tests:");
const cloudbuild = fs.readFileSync("./cloudbuild.yaml", "utf8");
assert(cloudbuild.includes("docker"), "cloudbuild.yaml has docker build step");
assert(cloudbuild.includes("push"), "cloudbuild.yaml has docker push step");
assert(
cloudbuild.includes("chomptron"),
"cloudbuild.yaml references chomptron",
);
// Server code validation
console.log("\nServer Code Tests:");
const serverCode = fs.readFileSync("./server.js", "utf8");
assert(
serverCode.includes("/api/generate-recipe"),
"server has recipe generation endpoint",
);
assert(serverCode.includes("/health"), "server has health check endpoint");
assert(serverCode.includes("/ready"), "server has readiness check endpoint");
assert(serverCode.includes("GoogleGenerativeAI"), "server uses Gemini AI");
// MCP tool exposure
console.log("\nMCP Server Tests:");
assert(
pkg.dependencies["@modelcontextprotocol/server"],
"MCP server dependency exists",
);
assert(
pkg.dependencies["@modelcontextprotocol/node"],
"MCP node transport dependency exists",
);
assert(serverCode.includes('app.post("/mcp"'), "server has MCP endpoint");
assert(
serverCode.includes('registerTool(\n "generate_recipe"') ||
serverCode.includes("generate_recipe"),
"MCP server registers generate_recipe tool",
);
assert(
serverCode.includes("async function generateRecipe("),
"recipe generation logic is a shared function, not duplicated",
);
assert(
serverCode.includes("MCP_DAILY_RECIPE_LIMIT"),
"MCP path has its own daily budget guard",
);
assert(
serverCode.includes("MCP_BUDGET_FILE") &&
serverCode.includes("REST_HOURLY_RECIPE_LIMIT"),
"MCP budget is file-persisted and the website has a server-side hourly cap",
);
assert(fs.existsSync("./server.json"), "MCP registry manifest exists");
// HTML content validation
console.log("\nFrontend Tests:");
const html = fs.readFileSync("./index.html", "utf8");
const css = fs.existsSync("./styles.css")
? fs.readFileSync("./styles.css", "utf8")
: "";
const js = fs.existsSync("./app.js") ? fs.readFileSync("./app.js", "utf8") : "";
assert(html.includes("Chomptron"), "HTML includes Chomptron branding");
assert(html.includes("recipe"), "HTML mentions recipes");
assert(
html.includes("styles.css") || css.length > 0,
"HTML links to styles.css or CSS is present",
);
assert(
html.includes("app.js") || js.length > 0,
"HTML links to app.js or JS is present",
);
assert(
html.includes("/api/generate-recipe") || js.includes("/api/generate-recipe"),
"App calls recipe API",
);
assert(html.includes("ingredients"), "HTML has ingredients input");
// Recipe History & Favorites feature tests
console.log("\nRecipe History & Favorites Tests:");
assert(
html.includes("RecipeManager") || js.includes("RecipeManager"),
"App includes RecipeManager object",
);
assert(
html.includes("chomptron_recipes") || js.includes("chomptron_recipes"),
"App defines recipe storage key",
);
assert(
html.includes("getRecipes") || js.includes("getRecipes"),
"App has getRecipes function",
);
assert(
html.includes("saveRecipe") || js.includes("saveRecipe"),
"App has saveRecipe function",
);
assert(
html.includes("toggleFavorite") || js.includes("toggleFavorite"),
"App has toggleFavorite function",
);
assert(
html.includes("exportToJSON") || js.includes("exportToJSON"),
"App has exportToJSON function",
);
assert(
html.includes("exportToText") || js.includes("exportToText"),
"App has exportToText function",
);
assert(
html.includes("clearAll") || js.includes("clearAll"),
"App has clearAll function",
);
assert(
html.includes("extractRecipeName") || js.includes("extractRecipeName"),
"App has recipe name extraction",
);
assert(
html.includes("renderHistory") || js.includes("renderHistory"),
"App has renderHistory function",
);
assert(
html.includes("filterHistory") || js.includes("filterHistory"),
"App has filterHistory function",
);
assert(
html.includes("showFavorites") || js.includes("showFavorites"),
"App has showFavorites function",
);
assert(
html.includes("showAllHistory") || js.includes("showAllHistory"),
"App has showAllHistory function",
);
assert(
html.includes("loadRecipe") || js.includes("loadRecipe"),
"App has loadRecipe function",
);
assert(
html.includes("updateFavoriteButton") || js.includes("updateFavoriteButton"),
"App has updateFavoriteButton function",
);
// UI Component tests
console.log("\nHistory UI Component Tests:");
assert(
html.includes('class="history-toggle"'),
"HTML has history toggle button",
);
assert(html.includes('id="historyPanel"'), "HTML has history panel element");
assert(html.includes('id="historySearch"'), "HTML has history search input");
assert(html.includes('id="historyList"'), "HTML has history list container");
assert(html.includes('id="favoriteBtn"'), "HTML has favorite toggle button");
assert(
html.includes("toggleHistory") || js.includes("toggleHistory"),
"App has toggleHistory function",
);
assert(
html.includes("exportJSON") || js.includes("exportJSON"),
"App has exportJSON function",
);
assert(
html.includes("clearHistory") || js.includes("clearHistory"),
"App has clearHistory function",
);
// CSS styling tests
console.log("\nHistory Styling Tests:");
assert(
html.includes(".history-toggle") || css.includes(".history-toggle"),
"App has history toggle button styles",
);
assert(
html.includes(".history-panel") || css.includes(".history-panel"),
"App has history panel styles",
);
assert(
html.includes(".recipe-item") || css.includes(".recipe-item"),
"App has recipe item styles",
);
assert(
html.includes(".favorite-btn") || css.includes(".favorite-btn"),
"App has favorite button styles",
);
assert(
html.includes(".empty-history") || css.includes(".empty-history"),
"App has empty state styles",
);
assert(
html.includes(".history-panel.open") || css.includes(".history-panel.open"),
"App has open panel state styles",
);
assert(
html.includes(".favorite-toggle-btn") || css.includes(".favorite-toggle-btn"),
"App has favorite toggle styles",
);
// Mobile responsive tests
console.log("\nMobile Responsive Tests:");
assert(
html.includes("@media (max-width: 768px)") ||
css.includes("@media (max-width: 768px)"),
"App has tablet breakpoint",
);
assert(
html.includes("@media (max-width: 600px)") ||
css.includes("@media (max-width: 600px)"),
"App has mobile breakpoint",
);
// localStorage integration tests
console.log("\nLocalStorage Integration Tests:");
assert(
html.includes("localStorage.getItem") || js.includes("localStorage.getItem"),
"App uses localStorage.getItem",
);
assert(
html.includes("localStorage.setItem") || js.includes("localStorage.setItem"),
"App uses localStorage.setItem",
);
assert(
html.includes("localStorage.removeItem") ||
js.includes("localStorage.removeItem"),
"App uses localStorage.removeItem",
);
assert(
html.includes("JSON.parse") || js.includes("JSON.parse"),
"App parses stored JSON",
);
assert(
html.includes("JSON.stringify") || js.includes("JSON.stringify"),
"App stringifies data for storage",
);
// Recipe save integration
console.log("\nRecipe Save Integration Tests:");
assert(
(html.includes("RecipeManager.saveRecipe") ||
js.includes("RecipeManager.saveRecipe")) &&
(html.includes("ingredients") || js.includes("ingredients")) &&
(html.includes("data.recipe") || js.includes("data.recipe")),
"App auto-saves recipes on generation",
);
assert(
html.includes("RecipeManager.currentRecipeId") ||
js.includes("RecipeManager.currentRecipeId"),
"App tracks current recipe ID",
);
assert(
html.match(/if\s*\(\s*recipes\.length\s*>\s*100\s*\)/) ||
js.match(/if\s*\(\s*recipes\.length\s*>\s*100\s*\)/),
"App limits history to 100 recipes",
);
// Node.js version check
console.log("\nEnvironment Tests:");
const nodeVersion = parseInt(process.version.slice(1).split(".")[0]);
assert(
nodeVersion >= 18,
`Node.js version >= 18 (current: ${process.version})`,
);
// Health check endpoint test
console.log("\nHealth Check Tests:");
const server = require("./server.js");
const port = 8080;
setTimeout(() => {
const options = {
hostname: "localhost",
port: port,
path: "/health",
method: "GET",
};
const healthReq = http.request(options, (res) => {
let data = "";
res.on("data", (chunk) => {
data += chunk;
});
res.on("end", () => {
try {
const json = JSON.parse(data);
assert(res.statusCode === 200, "/health endpoint returns 200");
assert(
json.status === "healthy",
"/health endpoint returns healthy status",
);
assert(
json.service === "chomptron",
"/health endpoint identifies as chomptron",
);
} catch {
assert(false, "/health endpoint returns valid JSON");
}
// Test readiness endpoint
const readyOptions = { ...options, path: "/ready" };
const readyReq = http.request(readyOptions, (res) => {
let data = "";
res.on("data", (chunk) => {
data += chunk;
});
res.on("end", () => {
try {
const json = JSON.parse(data);
assert(
[200, 503].includes(res.statusCode),
"/ready endpoint returns 200 or 503",
);
assert(json.status !== undefined, "/ready endpoint returns status");
} catch {
assert(false, "/ready endpoint returns valid JSON");
}
// Summary
console.log(`\n${"=".repeat(50)}`);
console.log(`✓ ${passed} passed | ✗ ${failed} failed`);
console.log(`${"=".repeat(50)}`);
server.close();
if (failed > 0) {
process.exit(1);
}
console.log("\n✓ All Chomptron tests passed!");
});
});
readyReq.on("error", () => {
assert(false, "/ready endpoint is accessible");
server.close();
process.exit(1);
});
readyReq.end();
});
});
healthReq.on("error", () => {
assert(false, "/health endpoint is accessible");
server.close();
process.exit(1);
});
healthReq.end();
}, 1000);