My agentic slop goes here. Not intended for anyone else!
at jsont 5.4 kB view raw
1(** Test program for the high-level email submission API *) 2 3open Printf 4 5let test_submission_api () = 6 printf "Testing JMAP Email Submission High-Level API\n"; 7 printf "=============================================\n\n"; 8 9 (* Initialize crypto *) 10 Mirage_crypto_rng_unix.use_default (); 11 12 Eio_main.run @@ fun env -> 13 14 (* Read API credentials *) 15 let api_key = 16 try 17 let ic = open_in ".api-key" in 18 let key = input_line ic in 19 close_in ic; 20 String.trim key 21 with 22 | Sys_error _ -> 23 eprintf "Error: Create a .api-key file with your JMAP bearer token\n"; 24 exit 1 25 in 26 27 try 28 (* Connect to JMAP server *) 29 printf "📡 Connecting to Fastmail JMAP server...\n"; 30 let client = Jmap_unix.create_client () in 31 let session_url = Uri.of_string "https://api.fastmail.com/jmap/session" in 32 let auth_method = Jmap_unix.Bearer api_key in 33 34 match Jmap_unix.(connect env client ~session_url ~host:"api.fastmail.com" ~port:443 ~use_tls:true ~auth_method ()) with 35 | Ok (ctx, session) -> 36 printf "✅ Connected successfully\n\n"; 37 38 (* Print session info *) 39 Jmap_unix.Session_utils.print_session_info session; 40 printf "\n"; 41 42 (* Test 1: Query pending submissions *) 43 printf "🔍 Test 1: Querying pending submissions...\n"; 44 (match Jmap_unix.Email_submission.query_pending_submissions env ctx with 45 | Ok submission_ids -> 46 printf " Found %d pending submission(s)\n" (List.length submission_ids); 47 List.iteri (fun i id -> 48 printf " [%d] %s\n" (i+1) (Jmap.Id.to_string id) 49 ) submission_ids 50 | Error err -> 51 Format.printf " ⚠️ Query failed: %a\n" Jmap.Error.pp err); 52 53 printf "\n"; 54 55 (* Test 2: Create a mock submission (would need real email/identity IDs) *) 56 printf "📧 Test 2: Mock submission creation...\n"; 57 printf " Note: This would require valid email and identity IDs\n"; 58 printf " Example usage:\n"; 59 printf " ```ocaml\n"; 60 printf " let result = Jmap_unix.Email_submission.submit_email env ctx\n"; 61 printf " ~email_id ~identity_id in\n"; 62 printf " ```\n\n"; 63 64 (* Test 3: Demonstrate envelope submission *) 65 printf "✉️ Test 3: Submission with custom envelope...\n"; 66 printf " Example usage:\n"; 67 printf " ```ocaml\n"; 68 printf " let result = Jmap_unix.Email_submission.submit_email_with_envelope env ctx\n"; 69 printf " ~email_id ~identity_id\n"; 70 printf " ~mail_from:\"sender@example.com\"\n"; 71 printf " ~rcpt_to:[\"recipient1@example.com\"; \"recipient2@example.com\"] in\n"; 72 printf " ```\n\n"; 73 74 (* Test 4: Cancel submission *) 75 printf "❌ Test 4: Cancelling submissions...\n"; 76 printf " Example usage:\n"; 77 printf " ```ocaml\n"; 78 printf " let result = Jmap_unix.Email_submission.cancel_submission env ctx\n"; 79 printf " ~submission_id in\n"; 80 printf " ```\n\n"; 81 82 (* Test 5: Check delivery status *) 83 printf "📊 Test 5: Checking delivery status...\n"; 84 (match Jmap_unix.Email_submission.query_pending_submissions env ctx with 85 | Ok [] -> 86 printf " No pending submissions to check\n" 87 | Ok (submission_id :: _) -> 88 printf " Checking status for: %s\n" (Jmap.Id.to_string submission_id); 89 (match Jmap_unix.Email_submission.get_delivery_status env ctx ~submission_id with 90 | Ok (Some status_tbl) -> 91 printf " Delivery status:\n"; 92 Hashtbl.iter (fun email status -> 93 let delivered = Jmap_email.Submission.DeliveryStatus.delivered status in 94 let delivered_str = match delivered with 95 | `Queued -> "Queued" 96 | `Yes -> "Delivered" 97 | `No -> "Failed" 98 | `Unknown -> "Unknown" 99 in 100 printf " %s: %s\n" email delivered_str 101 ) status_tbl 102 | Ok None -> 103 printf " No delivery status available\n" 104 | Error err -> 105 Format.printf " ⚠️ Status check failed: %a\n" Jmap.Error.pp err) 106 | Error _ -> ()); 107 108 printf "\n"; 109 110 (* Test 6: Batch cancel *) 111 printf "🚫 Test 6: Cancel all pending submissions...\n"; 112 (match Jmap_unix.Email_submission.cancel_all_pending env ctx with 113 | Ok count -> 114 printf " Cancelled %d submission(s)\n" count 115 | Error err -> 116 Format.printf " ⚠️ Batch cancel failed: %a\n" Jmap.Error.pp err); 117 118 printf "\n"; 119 120 (* Close connection *) 121 printf "🔌 Closing connection...\n"; 122 (match Jmap_unix.close ctx with 123 | Ok () -> printf "✅ Connection closed\n" 124 | Error error -> Format.printf "⚠️ Error closing: %a\n" Jmap.Error.pp error); 125 126 printf "\n✨ API tests completed successfully!\n" 127 128 | Error error -> 129 Format.printf "❌ Connection failed: %a\n" Jmap.Error.pp error; 130 exit 1 131 with 132 | exn -> 133 printf "❌ Unexpected error: %s\n" (Printexc.to_string exn); 134 exit 1 135 136let () = test_submission_api ()