| 
									
										
										
										
											2025-10-13 20:20:24 +00:00
										 |  |  | import { expect, tap } from '@push.rocks/tapbundle'; | 
					
						
							|  |  |  | import { Qenv } from '@push.rocks/qenv'; | 
					
						
							|  |  |  | import * as smartmail from '@push.rocks/smartmail'; | 
					
						
							| 
									
										
										
										
											2019-10-28 15:55:04 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | const testQenv = new Qenv('./', './.nogit'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-07 16:31:56 +02:00
										 |  |  | import * as mailgun from '../ts/index.js'; | 
					
						
							| 
									
										
										
										
											2025-10-13 20:20:24 +00:00
										 |  |  | import { type IMailgunMessage } from '../ts/index.js'; | 
					
						
							| 
									
										
										
										
											2019-10-23 19:27:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-28 15:55:04 +01:00
										 |  |  | let testMailgunAccount: mailgun.MailgunAccount; | 
					
						
							| 
									
										
										
										
											2020-01-21 10:04:28 +00:00
										 |  |  | let testSmartmail: smartmail.Smartmail<IMailgunMessage>; | 
					
						
							| 
									
										
										
										
											2019-10-28 15:55:04 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-21 10:04:28 +00:00
										 |  |  | tap.test('should create a mailgun account', async () => { | 
					
						
							| 
									
										
										
										
											2022-08-07 16:31:56 +02:00
										 |  |  |   testMailgunAccount = new mailgun.MailgunAccount({ | 
					
						
							| 
									
										
										
										
											2025-10-13 20:20:24 +00:00
										 |  |  |     apiToken: await testQenv.getEnvVarOnDemand('MAILGUN_API_TOKEN'), | 
					
						
							| 
									
										
										
										
											2022-08-07 19:07:01 +02:00
										 |  |  |     region: 'eu', | 
					
						
							| 
									
										
										
										
											2022-08-07 16:31:56 +02:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2022-08-07 19:07:01 +02:00
										 |  |  |   await testMailgunAccount.addSmtpCredentials( | 
					
						
							| 
									
										
										
										
											2025-10-13 20:20:24 +00:00
										 |  |  |     await testQenv.getEnvVarOnDemand('MAILGUN_SMTP_CREDENTIALS'), | 
					
						
							| 
									
										
										
										
											2022-08-07 19:07:01 +02:00
										 |  |  |   ); | 
					
						
							| 
									
										
										
										
											2022-08-07 16:31:56 +02:00
										 |  |  |   expect(testMailgunAccount).toBeInstanceOf(mailgun.MailgunAccount); | 
					
						
							| 
									
										
										
										
											2019-10-28 15:55:04 +01:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | tap.test('should create a smartmail', async () => { | 
					
						
							|  |  |  |   testSmartmail = new smartmail.Smartmail({ | 
					
						
							|  |  |  |     body: 'hi there. This is the body.', | 
					
						
							| 
									
										
										
										
											2020-04-26 23:24:03 +00:00
										 |  |  |     from: 'Lossless GmbH <noreply@mail.lossless.com>', | 
					
						
							| 
									
										
										
										
											2020-08-13 01:37:26 +00:00
										 |  |  |     subject: 'TestMessage from @mojoio/mailgun test', | 
					
						
							| 
									
										
										
										
											2019-10-28 15:55:04 +01:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2022-08-07 16:31:56 +02:00
										 |  |  |   expect(testSmartmail).toBeInstanceOf(smartmail.Smartmail); | 
					
						
							| 
									
										
										
										
											2019-10-28 15:55:04 +01:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | tap.test('should send a smartmail', async () => { | 
					
						
							| 
									
										
										
										
											2025-10-13 20:20:24 +00:00
										 |  |  |   await testMailgunAccount.sendSmartMail( | 
					
						
							|  |  |  |     testSmartmail, | 
					
						
							|  |  |  |     'Sandbox Team <sandbox@mail.git.zone>', | 
					
						
							|  |  |  |   ); | 
					
						
							| 
									
										
										
										
											2019-10-23 19:27:42 +02:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-11 14:47:25 +00:00
										 |  |  | tap.test('should send a smartmail with empty body', async () => { | 
					
						
							|  |  |  |   const emptyBodySmartmail = new smartmail.Smartmail<IMailgunMessage>({ | 
					
						
							|  |  |  |     body: '', | 
					
						
							| 
									
										
										
										
											2020-08-13 00:32:05 +00:00
										 |  |  |     from: 'Lossless GmbH <noreply@mail.lossless.one>', | 
					
						
							| 
									
										
										
										
											2020-08-13 01:37:26 +00:00
										 |  |  |     subject: 'A message with no body from @mojoio/mailgun test', | 
					
						
							| 
									
										
										
										
											2020-08-11 14:47:25 +00:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-08-13 01:37:26 +00:00
										 |  |  |   await testMailgunAccount.sendSmartMail( | 
					
						
							|  |  |  |     emptyBodySmartmail, | 
					
						
							| 
									
										
										
										
											2025-10-13 20:20:24 +00:00
										 |  |  |     'Sandbox Team <sandbox@mail.git.zone>', | 
					
						
							| 
									
										
										
										
											2020-08-13 01:37:26 +00:00
										 |  |  |   ); | 
					
						
							| 
									
										
										
										
											2020-08-11 14:47:25 +00:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-13 08:09:37 +00:00
										 |  |  | tap.test('should retrieve a mail using a retrieval url', async () => { | 
					
						
							| 
									
										
										
										
											2025-10-17 10:23:57 +00:00
										 |  |  |   // Note: This test requires a valid message storage URL from a recently sent email
 | 
					
						
							|  |  |  |   // Storage URLs are available for 3 days after sending via Events API or Mailgun dashboard
 | 
					
						
							| 
									
										
										
										
											2020-06-18 22:00:00 +00:00
										 |  |  |   const result = await testMailgunAccount.retrieveSmartMailFromMessageUrl( | 
					
						
							| 
									
										
										
										
											2025-10-17 10:23:57 +00:00
										 |  |  |     'https://api.eu.mailgun.net/v3/domains/mail.lossless.one/messages/AgMFnnnAKC8xp_dDa79LyoxhloxtaVmnRA==', | 
					
						
							| 
									
										
										
										
											2020-06-18 22:00:00 +00:00
										 |  |  |   ); | 
					
						
							| 
									
										
										
										
											2022-08-07 16:31:56 +02:00
										 |  |  |   console.log(result); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // TODO handle empty body
 | 
					
						
							|  |  |  |   if (false) { | 
					
						
							| 
									
										
										
										
											2020-01-21 10:04:28 +00:00
										 |  |  |     result.options.subject = 'hi there. This is a testmail with attachment'; | 
					
						
							|  |  |  |     result.options.from = 'noreply@mail.lossless.com'; | 
					
						
							| 
									
										
										
										
											2025-10-13 20:20:24 +00:00
										 |  |  |     await testMailgunAccount.sendSmartMail( | 
					
						
							|  |  |  |       result, | 
					
						
							|  |  |  |       'Sandbox Team <sandbox@mail.git.zone>', | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2020-01-21 10:04:28 +00:00
										 |  |  |   } | 
					
						
							| 
									
										
										
										
											2020-01-13 08:09:37 +00:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-10-17 08:48:22 +00:00
										 |  |  | export default tap.start(); |