Files
homaroma/apps/backend/test_category_image.ts
T
2026-06-28 20:18:07 -04:00

70 lines
2.1 KiB
TypeScript

import prisma from "./src/lib/prisma";
import { categoriesRoute } from "./src/routes/categories";
async function test() {
console.log("Creating/Finding test category...");
// Let's find or create a category
let category = await prisma.category.findFirst({
where: {
name: "TestCategoryTemp"
}
});
if (!category) {
category = await prisma.category.create({
data: {
name: "TestCategoryTemp",
translations: { create: { langCode: "th", name: "TestCategoryTemp" } }
}
});
}
const id = category.id;
console.log(`Using category ID: ${id}, current imageUrl: ${category.imageUrl}`);
// Test updating image URL to a new value
console.log("Updating imageUrl to 'https://example.com/test.png'...");
const updateRes = await categoriesRoute.handle(
new Request(`http://localhost/categories/${id}/image`, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ imageUrl: "https://example.com/test.png" })
})
);
console.log("PATCH status:", updateRes.status);
const updatedCategory = await updateRes.json();
console.log("PATCH response:", updatedCategory);
if (updatedCategory.imageUrl !== "https://example.com/test.png") {
throw new Error("imageUrl was not updated correctly!");
}
// Test setting imageUrl to null
console.log("Updating imageUrl to null...");
const nullRes = await categoriesRoute.handle(
new Request(`http://localhost/categories/${id}/image`, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ imageUrl: null })
})
);
console.log("PATCH null status:", nullRes.status);
const nulledCategory = await nullRes.json();
console.log("PATCH null response:", nulledCategory);
if (nulledCategory.imageUrl !== null) {
throw new Error("imageUrl was not nulled correctly!");
}
// Clean up if we created the temp category
if (category.name === "TestCategoryTemp") {
await prisma.category.delete({ where: { id } });
console.log("Cleaned up temp category.");
}
console.log("All PATCH tests passed successfully!");
}
test().catch(console.error);