import { expect, tap } from '@git.zone/tstest/tapbundle'; import * as smartpdf from '../ts/index.js'; tap.test('should create multiple SmartPdf instances with automatic port allocation', async () => { const instance1 = new smartpdf.SmartPdf(); const instance2 = new smartpdf.SmartPdf(); const instance3 = new smartpdf.SmartPdf(); // Start all instances await instance1.start(); await instance2.start(); await instance3.start(); // Verify all instances have different ports expect(instance1.serverPort).toBeGreaterThanOrEqual(20000); expect(instance1.serverPort).toBeLessThanOrEqual(30000); expect(instance2.serverPort).toBeGreaterThanOrEqual(20000); expect(instance2.serverPort).toBeLessThanOrEqual(30000); expect(instance3.serverPort).toBeGreaterThanOrEqual(20000); expect(instance3.serverPort).toBeLessThanOrEqual(30000); // Ensure all ports are different expect(instance1.serverPort).not.toEqual(instance2.serverPort); expect(instance1.serverPort).not.toEqual(instance3.serverPort); expect(instance2.serverPort).not.toEqual(instance3.serverPort); console.log(`Instance 1 port: ${instance1.serverPort}`); console.log(`Instance 2 port: ${instance2.serverPort}`); console.log(`Instance 3 port: ${instance3.serverPort}`); // Test that all instances work correctly const pdf1 = await instance1.getA4PdfResultForHtmlString('

Instance 1

'); const pdf2 = await instance2.getA4PdfResultForHtmlString('

Instance 2

'); const pdf3 = await instance3.getA4PdfResultForHtmlString('

Instance 3

'); expect(pdf1.buffer).toBeInstanceOf(Buffer); expect(pdf2.buffer).toBeInstanceOf(Buffer); expect(pdf3.buffer).toBeInstanceOf(Buffer); // Clean up await instance1.stop(); await instance2.stop(); await instance3.stop(); }); tap.test('should create SmartPdf instance with custom port range', async () => { const customInstance = new smartpdf.SmartPdf({ portRangeStart: 25000, portRangeEnd: 26000 }); await customInstance.start(); expect(customInstance.serverPort).toBeGreaterThanOrEqual(25000); expect(customInstance.serverPort).toBeLessThanOrEqual(26000); console.log(`Custom range instance port: ${customInstance.serverPort}`); await customInstance.stop(); }); tap.test('should create SmartPdf instance with specific port', async () => { const specificPortInstance = new smartpdf.SmartPdf({ port: 28888 }); await specificPortInstance.start(); expect(specificPortInstance.serverPort).toEqual(28888); console.log(`Specific port instance: ${specificPortInstance.serverPort}`); await specificPortInstance.stop(); }); tap.test('should throw error when specific port is already in use', async () => { const instance1 = new smartpdf.SmartPdf({ port: 29999 }); await instance1.start(); const instance2 = new smartpdf.SmartPdf({ port: 29999 }); let errorThrown = false; try { await instance2.start(); } catch (error) { errorThrown = true; expect(error.message).toInclude('already in use'); } expect(errorThrown).toBeTrue(); await instance1.stop(); }); export default tap.start();