| 
									
										
										
										
											2025-05-07 23:04:54 +00:00
										 |  |  | import { tap, expect } from '@push.rocks/tapbundle'; | 
					
						
							|  |  |  | import * as plugins from '../ts/plugins.js'; | 
					
						
							|  |  |  | import {  | 
					
						
							|  |  |  |   DcRouter, | 
					
						
							| 
									
										
										
										
											2025-05-08 00:12:36 +00:00
										 |  |  |   type IDcRouterOptions, | 
					
						
							|  |  |  |   type IEmailConfig, | 
					
						
							|  |  |  |   type EmailProcessingMode, | 
					
						
							|  |  |  |   type IDomainRule | 
					
						
							| 
									
										
										
										
											2025-05-08 01:24:03 +00:00
										 |  |  | } from '../ts/classes.dcrouter.js'; | 
					
						
							| 
									
										
										
										
											2025-05-07 23:04:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | tap.test('DcRouter class - basic functionality', async () => { | 
					
						
							|  |  |  |   // Create a simple DcRouter instance
 | 
					
						
							|  |  |  |   const options: IDcRouterOptions = { | 
					
						
							|  |  |  |     tls: { | 
					
						
							|  |  |  |       contactEmail: 'test@example.com' | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const router = new DcRouter(options); | 
					
						
							|  |  |  |   expect(router).toBeTruthy(); | 
					
						
							|  |  |  |   expect(router instanceof DcRouter).toEqual(true); | 
					
						
							|  |  |  |   expect(router.options.tls.contactEmail).toEqual('test@example.com'); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-08 00:12:36 +00:00
										 |  |  | tap.test('DcRouter class - SmartProxy configuration', async () => { | 
					
						
							|  |  |  |   // Create SmartProxy configuration
 | 
					
						
							|  |  |  |   const smartProxyConfig: plugins.smartproxy.ISmartProxyOptions = { | 
					
						
							|  |  |  |     fromPort: 443, | 
					
						
							|  |  |  |     toPort: 8080, | 
					
						
							|  |  |  |     targetIP: '10.0.0.10', | 
					
						
							|  |  |  |     sniEnabled: true, | 
					
						
							|  |  |  |     acme: { | 
					
						
							|  |  |  |       port: 80, | 
					
						
							|  |  |  |       enabled: true, | 
					
						
							|  |  |  |       autoRenew: true, | 
					
						
							|  |  |  |       useProduction: false, | 
					
						
							|  |  |  |       renewThresholdDays: 30, | 
					
						
							|  |  |  |       accountEmail: 'admin@example.com' | 
					
						
							| 
									
										
										
										
											2025-05-07 23:04:54 +00:00
										 |  |  |     }, | 
					
						
							| 
									
										
										
										
											2025-05-08 00:12:36 +00:00
										 |  |  |     globalPortRanges: [ | 
					
						
							|  |  |  |       { from: 80, to: 80 }, | 
					
						
							|  |  |  |       { from: 443, to: 443 } | 
					
						
							|  |  |  |     ], | 
					
						
							|  |  |  |     domainConfigs: [ | 
					
						
							|  |  |  |       { | 
					
						
							|  |  |  |         domains: ['example.com', 'www.example.com'], | 
					
						
							|  |  |  |         allowedIPs: ['0.0.0.0/0'], | 
					
						
							|  |  |  |         targetIPs: ['10.0.0.10'], | 
					
						
							|  |  |  |         portRanges: [ | 
					
						
							|  |  |  |           { from: 80, to: 80 }, | 
					
						
							|  |  |  |           { from: 443, to: 443 } | 
					
						
							|  |  |  |         ] | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2025-05-07 23:04:54 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   const options: IDcRouterOptions = { | 
					
						
							| 
									
										
										
										
											2025-05-08 00:12:36 +00:00
										 |  |  |     smartProxyConfig, | 
					
						
							| 
									
										
										
										
											2025-05-07 23:04:54 +00:00
										 |  |  |     tls: { | 
					
						
							|  |  |  |       contactEmail: 'test@example.com' | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const router = new DcRouter(options); | 
					
						
							| 
									
										
										
										
											2025-05-08 00:12:36 +00:00
										 |  |  |   expect(router.options.smartProxyConfig).toBeTruthy(); | 
					
						
							|  |  |  |   expect(router.options.smartProxyConfig.domainConfigs.length).toEqual(1); | 
					
						
							|  |  |  |   expect(router.options.smartProxyConfig.domainConfigs[0].domains[0]).toEqual('example.com'); | 
					
						
							| 
									
										
										
										
											2025-05-07 23:04:54 +00:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-05-08 00:12:36 +00:00
										 |  |  | tap.test('DcRouter class - Email configuration', async () => { | 
					
						
							|  |  |  |   // Create consolidated email configuration
 | 
					
						
							|  |  |  |   const emailConfig: IEmailConfig = { | 
					
						
							| 
									
										
										
										
											2025-05-07 23:04:54 +00:00
										 |  |  |     ports: [25, 587, 465], | 
					
						
							| 
									
										
										
										
											2025-05-08 00:12:36 +00:00
										 |  |  |     hostname: 'mail.example.com', | 
					
						
							|  |  |  |     maxMessageSize: 50 * 1024 * 1024, // 50MB
 | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     defaultMode: 'forward' as EmailProcessingMode, | 
					
						
							|  |  |  |     defaultServer: 'fallback-mail.example.com', | 
					
						
							| 
									
										
										
										
											2025-05-07 23:04:54 +00:00
										 |  |  |     defaultPort: 25, | 
					
						
							| 
									
										
										
										
											2025-05-08 00:12:36 +00:00
										 |  |  |     defaultTls: true, | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     domainRules: [ | 
					
						
							| 
									
										
										
										
											2025-05-07 23:04:54 +00:00
										 |  |  |       { | 
					
						
							| 
									
										
										
										
											2025-05-08 00:12:36 +00:00
										 |  |  |         pattern: '*@example.com', | 
					
						
							|  |  |  |         mode: 'forward' as EmailProcessingMode, | 
					
						
							|  |  |  |         target: { | 
					
						
							|  |  |  |           server: 'mail1.example.com', | 
					
						
							|  |  |  |           port: 25, | 
					
						
							|  |  |  |           useTls: true | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-05-07 23:04:54 +00:00
										 |  |  |       }, | 
					
						
							|  |  |  |       { | 
					
						
							| 
									
										
										
										
											2025-05-08 00:12:36 +00:00
										 |  |  |         pattern: '*@example.org', | 
					
						
							|  |  |  |         mode: 'mta' as EmailProcessingMode, | 
					
						
							|  |  |  |         mtaOptions: { | 
					
						
							|  |  |  |           domain: 'example.org', | 
					
						
							|  |  |  |           allowLocalDelivery: true | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2025-05-07 23:04:54 +00:00
										 |  |  |       } | 
					
						
							|  |  |  |     ] | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const options: IDcRouterOptions = { | 
					
						
							| 
									
										
										
										
											2025-05-08 00:12:36 +00:00
										 |  |  |     emailConfig, | 
					
						
							| 
									
										
										
										
											2025-05-07 23:04:54 +00:00
										 |  |  |     tls: { | 
					
						
							|  |  |  |       contactEmail: 'test@example.com' | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const router = new DcRouter(options); | 
					
						
							| 
									
										
										
										
											2025-05-08 00:12:36 +00:00
										 |  |  |   expect(router.options.emailConfig).toBeTruthy(); | 
					
						
							|  |  |  |   expect(router.options.emailConfig.ports.length).toEqual(3); | 
					
						
							|  |  |  |   expect(router.options.emailConfig.domainRules.length).toEqual(2); | 
					
						
							|  |  |  |   expect(router.options.emailConfig.domainRules[0].pattern).toEqual('*@example.com'); | 
					
						
							|  |  |  |   expect(router.options.emailConfig.domainRules[1].pattern).toEqual('*@example.org'); | 
					
						
							| 
									
										
										
										
											2025-05-07 23:04:54 +00:00
										 |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | tap.test('DcRouter class - Domain pattern matching', async () => { | 
					
						
							|  |  |  |   const router = new DcRouter({}); | 
					
						
							|  |  |  |    | 
					
						
							|  |  |  |   // Use the internal method for testing if accessible
 | 
					
						
							|  |  |  |   // This requires knowledge of the implementation, so it's a bit brittle
 | 
					
						
							|  |  |  |   if (typeof router['isDomainMatch'] === 'function') { | 
					
						
							|  |  |  |     // Test exact match
 | 
					
						
							|  |  |  |     expect(router['isDomainMatch']('example.com', 'example.com')).toEqual(true); | 
					
						
							|  |  |  |     expect(router['isDomainMatch']('example.com', 'example.org')).toEqual(false); | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     // Test wildcard match
 | 
					
						
							|  |  |  |     expect(router['isDomainMatch']('sub.example.com', '*.example.com')).toEqual(true); | 
					
						
							|  |  |  |     expect(router['isDomainMatch']('sub.sub.example.com', '*.example.com')).toEqual(true); | 
					
						
							|  |  |  |     expect(router['isDomainMatch']('example.com', '*.example.com')).toEqual(false); | 
					
						
							|  |  |  |     expect(router['isDomainMatch']('sub.example.org', '*.example.com')).toEqual(false); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Final clean-up test
 | 
					
						
							|  |  |  | tap.test('clean up after tests', async () => { | 
					
						
							|  |  |  |   // No-op - just to make sure everything is cleaned up properly
 | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | tap.test('stop', async () => { | 
					
						
							|  |  |  |   await tap.stopForcefully(); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Export a function to run all tests
 | 
					
						
							|  |  |  | export default tap.start(); |