x
This commit is contained in:
1 parent
70877882b0
commit
ce2f4cf0c8
93 files changed
+4200
-67
No files matched your search
@@ -0,0 +1,154 @@
|
||||
import { Elysia } from "elysia";
|
||||
import prisma from "./src/lib/prisma.js";
|
||||
import { menusRoute } from "./src/routes/menus.js";
|
||||
import { authRoute } from "./src/routes/auth.js";
|
||||
|
||||
const TEST_MENU_NAME = "test-image-menu";
|
||||
|
||||
async function main() {
|
||||
console.log("🚀 Starting Menu Image Integration Test...");
|
||||
|
||||
// 1. Direct Prisma cleanup
|
||||
console.log("🧹 Cleaning up old test menu...");
|
||||
await prisma.menu.deleteMany({
|
||||
where: { name: TEST_MENU_NAME }
|
||||
});
|
||||
|
||||
// 2. Initialize Elysia mock server
|
||||
console.log("⚙️ Setting up Elysia test app...");
|
||||
const app = new Elysia().use(authRoute).use(menusRoute);
|
||||
|
||||
// 3. Login to get token
|
||||
console.log("🔑 Logging in as admin...");
|
||||
const loginRes = await app.handle(
|
||||
new Request("http://localhost/auth/login", {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ username: "admin", password: "homaroma123" })
|
||||
})
|
||||
);
|
||||
|
||||
if (loginRes.status !== 200) {
|
||||
const errText = await loginRes.text();
|
||||
throw new Error(`Login failed with status ${loginRes.status}: ${errText}`);
|
||||
}
|
||||
|
||||
const { success, token } = (await loginRes.json()) as { success: boolean; token: string };
|
||||
if (!success || !token) {
|
||||
throw new Error("Failed to retrieve authentication token.");
|
||||
}
|
||||
console.log("✅ Authenticated successfully.");
|
||||
|
||||
// 4. POST: Create menu with rootImage, columnImage, and hoverImage
|
||||
console.log("➕ Creating new menu with image fields via POST...");
|
||||
const postRes = await app.handle(
|
||||
new Request("http://localhost/menus", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": `Bearer ${token}`
|
||||
},
|
||||
body: JSON.stringify({
|
||||
name: TEST_MENU_NAME,
|
||||
rootImage: "https://example.com/root.jpg",
|
||||
items: [
|
||||
{
|
||||
label: "Product 1",
|
||||
url: "/product1",
|
||||
order: 1,
|
||||
columnImage: "https://example.com/col1.jpg",
|
||||
hoverImage: "https://example.com/hov1.jpg"
|
||||
}
|
||||
]
|
||||
})
|
||||
})
|
||||
);
|
||||
|
||||
if (postRes.status !== 200 && postRes.status !== 201) {
|
||||
const errText = await postRes.text();
|
||||
throw new Error(`POST /menus failed with status ${postRes.status}: ${errText}`);
|
||||
}
|
||||
|
||||
const createdMenu = (await postRes.json()) as any;
|
||||
console.log("Created menu payload:", JSON.stringify(createdMenu, null, 2));
|
||||
|
||||
// Assertions
|
||||
if (createdMenu.name !== TEST_MENU_NAME) throw new Error("Incorrect menu name");
|
||||
if (createdMenu.rootImage !== "https://example.com/root.jpg") throw new Error("Incorrect rootImage");
|
||||
if (!createdMenu.items || createdMenu.items.length !== 1) throw new Error("Expected 1 menu item");
|
||||
if (createdMenu.items[0].columnImage !== "https://example.com/col1.jpg") throw new Error("Incorrect columnImage");
|
||||
if (createdMenu.items[0].hoverImage !== "https://example.com/hov1.jpg") throw new Error("Incorrect hoverImage");
|
||||
console.log("✅ POST verification successful!");
|
||||
|
||||
// 5. PUT: Update menu with new images
|
||||
console.log("🔄 Updating menu with new images via PUT...");
|
||||
const putRes = await app.handle(
|
||||
new Request(`http://localhost/menus/${TEST_MENU_NAME}`, {
|
||||
method: "PUT",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Authorization": `Bearer ${token}`
|
||||
},
|
||||
body: JSON.stringify({
|
||||
rootImage: "https://example.com/root-updated.jpg",
|
||||
items: [
|
||||
{
|
||||
label: "Product 1 Updated",
|
||||
url: "/product1-updated",
|
||||
order: 1,
|
||||
columnImage: "https://example.com/col1-updated.jpg",
|
||||
hoverImage: "https://example.com/hov1-updated.jpg"
|
||||
}
|
||||
]
|
||||
})
|
||||
})
|
||||
);
|
||||
|
||||
if (putRes.status !== 200) {
|
||||
const errText = await putRes.text();
|
||||
throw new Error(`PUT /menus/:name failed with status ${putRes.status}: ${errText}`);
|
||||
}
|
||||
|
||||
const updatedMenu = (await putRes.json()) as any;
|
||||
console.log("Updated menu payload:", JSON.stringify(updatedMenu, null, 2));
|
||||
|
||||
// Assertions
|
||||
if (updatedMenu.rootImage !== "https://example.com/root-updated.jpg") throw new Error("Incorrect rootImage update");
|
||||
if (!updatedMenu.items || updatedMenu.items.length !== 1) throw new Error("Expected 1 menu item after update");
|
||||
if (updatedMenu.items[0].columnImage !== "https://example.com/col1-updated.jpg") throw new Error("Incorrect columnImage update");
|
||||
if (updatedMenu.items[0].hoverImage !== "https://example.com/hov1-updated.jpg") throw new Error("Incorrect hoverImage update");
|
||||
console.log("✅ PUT verification successful!");
|
||||
|
||||
// 6. GET: Fetch the menu publicly
|
||||
console.log("🔍 Fetching menu via public GET route...");
|
||||
const getRes = await app.handle(
|
||||
new Request(`http://localhost/menus/${TEST_MENU_NAME}`)
|
||||
);
|
||||
|
||||
if (getRes.status !== 200) {
|
||||
const errText = await getRes.text();
|
||||
throw new Error(`GET /menus/:name failed with status ${getRes.status}: ${errText}`);
|
||||
}
|
||||
|
||||
const fetchedMenu = (await getRes.json()) as any;
|
||||
if (fetchedMenu.rootImage !== "https://example.com/root-updated.jpg") throw new Error("Incorrect fetched rootImage");
|
||||
if (fetchedMenu.items[0].columnImage !== "https://example.com/col1-updated.jpg") throw new Error("Incorrect fetched columnImage");
|
||||
if (fetchedMenu.items[0].hoverImage !== "https://example.com/hov1-updated.jpg") throw new Error("Incorrect fetched hoverImage");
|
||||
console.log("✅ GET verification successful!");
|
||||
|
||||
// 7. Cleanup
|
||||
console.log("🧹 Performing final database cleanup...");
|
||||
await prisma.menu.deleteMany({
|
||||
where: { name: TEST_MENU_NAME }
|
||||
});
|
||||
console.log("✨ All Menu Image Integration Tests Passed Successfully!");
|
||||
}
|
||||
|
||||
main()
|
||||
.catch((err) => {
|
||||
console.error("❌ Test failed:", err);
|
||||
process.exit(1);
|
||||
})
|
||||
.finally(async () => {
|
||||
await prisma.$disconnect();
|
||||
});
|
||||
Reference in new issue
Block a user