[-] We test in prod

This commit is contained in:
2025-10-26 17:08:43 +08:00
parent 144e06d92b
commit 238f61d737
-105
View File
@@ -1,105 +0,0 @@
// --- Configuration ---
// Make sure this key matches your secrets.json!
const API_KEY = "meowmeow";
const SERVER_URL = "http://localhost:3000";
// A unique ID for this test upload
const TEST_ID = `test-${Math.floor(Math.random() * 10000)}`;
const TEST_OWNER_KEY = "1234";
const TEST_IMAGE_PATH = "./UWU01721.JPG"; // The dummy image you will create
/**
* Creates a dummy image file if it doesn't exist.
* This is just for testing; in a real client, you'd use a real file.
*/
async function ensureTestImageExists() {
const file = Bun.file(TEST_IMAGE_PATH);
if (!(await file.exists())) {
console.log(`Creating dummy file at: ${TEST_IMAGE_PATH}`);
// Create a tiny, simple text file as a placeholder.
// The server validation only checks MIME type based on extension,
// so for this test, a fake "jpg" is okay.
// A real image would be better for EXIF parsing.
await Bun.write(TEST_IMAGE_PATH, "This is a test image buffer");
}
}
/**
* Test 1: POST /upload
* Attempts to upload the test image with metadata.
*/
async function testUpload() {
console.log(`--- Testing POST /upload with ID: ${TEST_ID} ---`);
await ensureTestImageExists();
const testFile = Bun.file(TEST_IMAGE_PATH);
// 1. Create the multipart/form-data payload
const formData = new FormData();
formData.append("id", TEST_ID);
formData.append("owner_key", TEST_OWNER_KEY);
formData.append("photo", testFile);
formData.append("edited_photo", testFile);
try {
// 2. Send the request
const response = await fetch(`${SERVER_URL}/upload`, {
method: "POST",
body: formData,
headers: { "x-instant-key": API_KEY }
});
// 3. Log the response
const result = await response.json();
console.log(`Status: ${response.status}`);
console.log("Response:", result);
if (!response.ok) {
console.error("Upload test FAILED!");
} else {
console.log("Upload test PASSED!");
}
} catch (e) {
console.error("Error during /upload test:", e);
}
}
/**
* Test 2: GET /photos
* Fetches the public metadata.
*/
async function testGetPhotos() {
console.log("\n--- Testing GET /photos ---");
try {
const response = await fetch(`${SERVER_URL}/photos`);
const metadata = await response.json();
console.log(`Status: ${response.status}`);
console.log(`Found ${metadata.length} entries.`);
// Find our test upload
const ourEntry = metadata.find((e: any) => e.id === TEST_ID);
if (ourEntry) {
console.log("Found our test entry:", ourEntry);
if (ourEntry.owner_key) {
console.error("GET /photos test FAILED: 'owner_key' was exposed!");
} else {
console.log(
"GET /photos test PASSED: 'owner_key' was correctly removed.",
);
}
} else {
console.warn("Could not find our test entry in /photos response.");
}
} catch (e) {
console.error("Error during /photos test:", e);
}
}
// --- Run Tests ---
async function runTests() {
await testUpload();
await testGetPhotos();
}
runTests();