import fs from 'fs'; import path from 'path'; async function testPhase2() { console.log("1. Creating dummy image file..."); const dummyFilePath = path.join(__dirname, 'dummy_rx.jpg'); fs.writeFileSync(dummyFilePath, 'dummy image content'); console.log("2. Requesting Token..."); const tokenResponse = await fetch("http://localhost:8080/v1/auth/token", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ grant_type: "client_credentials", client_id: "jayam_web_ai", client_secret: "my_secret_key" }) }); const tokenData = await tokenResponse.json(); const token = tokenData.access_token; if (!token) { console.error("No token received", tokenData); return; } console.log("3. Uploading Prescription (POST /v1/prescriptions/upload)..."); // Use fetch with FormData to upload file const formData = new FormData(); const fileBlob = new Blob([fs.readFileSync(dummyFilePath)], { type: 'image/jpeg' }); formData.append('image', fileBlob, 'dummy_rx.jpg'); const uploadResponse = await fetch("http://localhost:8080/v1/prescriptions/upload", { method: "POST", headers: { "Authorization": `Bearer ${token}`, "X-Actor-Id": "usr_ph_admin" }, body: formData as any }); const uploadData = await uploadResponse.json(); console.log("Upload Status:", uploadResponse.status); console.log("Upload Data:", uploadData); const rxId = uploadData.id; if (uploadResponse.status !== 202) { console.error("Upload failed"); return; } console.log("\n4. Polling for Extraction Results (waiting 6 seconds)..."); await new Promise(resolve => setTimeout(resolve, 6000)); const pollResponse = await fetch(`http://localhost:8080/v1/prescriptions/${rxId}`, { method: "GET", headers: { "Authorization": `Bearer ${token}`, "X-Actor-Id": "usr_ph_admin" } }); const pollData = await pollResponse.json(); console.log("Poll Status:", pollResponse.status); console.log("Image Path (S3 URL):", pollData.image_path); console.log("Extracted Data:", JSON.stringify(pollData.extracted_data, null, 2)); console.log("\n5. Testing Confirmation - Human Gate 1"); console.log("Attempting to confirm without explicit selection on low-confidence item..."); const badConfirmPayload = { items: [ { explicitly_confirmed: true }, { explicitly_confirmed: false } // Fails Gate 1 ] }; const badConfirmResponse = await fetch(`http://localhost:8080/v1/prescriptions/${rxId}/confirm`, { method: "POST", headers: { "Content-Type": "application/json", "Authorization": `Bearer ${token}`, "X-Actor-Id": "usr_ph_admin" }, body: JSON.stringify(badConfirmPayload) }); console.log("Bad Confirm Status (Expect 422):", badConfirmResponse.status); console.log("Bad Confirm Response:", await badConfirmResponse.json()); console.log("\nAttempting to confirm properly..."); const goodConfirmPayload = { items: [ { explicitly_confirmed: true }, { explicitly_confirmed: true, parsed: { brand: "Hydralazine 25mg" } } ] }; const goodConfirmResponse = await fetch(`http://localhost:8080/v1/prescriptions/${rxId}/confirm`, { method: "POST", headers: { "Content-Type": "application/json", "Authorization": `Bearer ${token}`, "X-Actor-Id": "usr_ph_admin" }, body: JSON.stringify(goodConfirmPayload) }); console.log("Good Confirm Status (Expect 200):", goodConfirmResponse.status); console.log("Good Confirm Response:", await goodConfirmResponse.json()); process.exit(0); } testPhase2();