import { tap, expect } from '@git.zone/tstest/tapbundle'; import { SmartProxy } from '../ts/index.js'; tap.test('should set update routes callback on certificate manager', async () => { // Create a simple proxy with a route requiring certificates const proxy = new SmartProxy({ acme: { email: 'test@local.dev', useProduction: false, port: 8080 // Use non-privileged port for ACME challenges globally }, routes: [{ name: 'test-route', match: { ports: [8443], domains: ['test.local'] }, action: { type: 'forward', target: { host: 'localhost', port: 3000 }, tls: { mode: 'terminate', certificate: 'auto', acme: { email: 'test@local.dev', useProduction: false } } } }] }); // Track callback setting let callbackSet = false; // Override createCertificateManager to track callback setting (proxy as any).createCertificateManager = async function( routes: any, certStore: string, acmeOptions?: any, initialState?: any ) { // Create a mock certificate manager const mockCertManager = { setUpdateRoutesCallback: function(callback: any) { callbackSet = true; }, setHttpProxy: function(proxy: any) {}, setGlobalAcmeDefaults: function(defaults: any) {}, setAcmeStateManager: function(manager: any) {}, initialize: async function() {}, provisionAllCertificates: async function() {}, stop: async function() {}, getAcmeOptions: function() { return acmeOptions || {}; }, getState: function() { return initialState || { challengeRouteActive: false }; } }; // Mimic the real createCertificateManager behavior // Always set up the route update callback for ACME challenges mockCertManager.setUpdateRoutesCallback(async (routes) => { await this.updateRoutes(routes); }); // Connect with HttpProxy if available (mimic real behavior) if ((this as any).httpProxyBridge.getHttpProxy()) { mockCertManager.setHttpProxy((this as any).httpProxyBridge.getHttpProxy()); } // Set the ACME state manager mockCertManager.setAcmeStateManager((this as any).acmeStateManager); // Pass down the global ACME config if available if ((this as any).settings.acme) { mockCertManager.setGlobalAcmeDefaults((this as any).settings.acme); } await mockCertManager.initialize(); return mockCertManager; }; await proxy.start(); // The callback should have been set during initialization expect(callbackSet).toEqual(true); // Reset tracking callbackSet = false; // Update routes - this should recreate the certificate manager await proxy.updateRoutes([{ name: 'new-route', match: { ports: [8444], domains: ['new.local'] }, action: { type: 'forward', target: { host: 'localhost', port: 3001 }, tls: { mode: 'terminate', certificate: 'auto', acme: { email: 'test@local.dev', useProduction: false } } } }]); // The callback should have been set again after update expect(callbackSet).toEqual(true); await proxy.stop(); }); tap.start();