fix(detector): Improve test coverage and adjust detection result handling
This commit is contained in:
118
test/test.ts
118
test/test.ts
@@ -3,14 +3,16 @@ import * as detector from '../ts/index.js';
|
||||
|
||||
let testDetector: detector.Detector;
|
||||
|
||||
tap.test('first test', async () => {
|
||||
tap.test('should create a detector instance', async () => {
|
||||
testDetector = new detector.Detector();
|
||||
expect(testDetector).toBeInstanceOf(detector.Detector);
|
||||
});
|
||||
|
||||
tap.test('should detect an closed port on a local domain', async () => {
|
||||
// Basic port detection tests
|
||||
tap.test('should detect a closed port on localhost', async () => {
|
||||
const result = await testDetector.isActive('http://localhost:3008');
|
||||
expect(result.isActive).toBeFalse();
|
||||
expect(result.serviceType).toBeUndefined();
|
||||
});
|
||||
|
||||
tap.test('should detect an open port on a remote domain', async () => {
|
||||
@@ -18,28 +20,130 @@ tap.test('should detect an open port on a remote domain', async () => {
|
||||
expect(result.isActive).toBeTrue();
|
||||
});
|
||||
|
||||
tap.test('should detect service type for HTTP', async () => {
|
||||
// Backward compatibility tests
|
||||
tap.test('should support backward compatibility with isActiveSimple', async () => {
|
||||
const result = await testDetector.isActiveSimple('https://example.com');
|
||||
expect(result).toBeTypeofBoolean();
|
||||
expect(result).toBeTrue();
|
||||
});
|
||||
|
||||
tap.test('should return false for closed port with isActiveSimple', async () => {
|
||||
const result = await testDetector.isActiveSimple('http://localhost:3008');
|
||||
expect(result).toBeFalse();
|
||||
});
|
||||
|
||||
// Service detection tests
|
||||
tap.test('should detect HTTP service on port 80', async () => {
|
||||
const result = await testDetector.isActive('http://example.com', { detectServiceType: true });
|
||||
expect(result.isActive).toBeTrue();
|
||||
expect(result.serviceType).toEqual(detector.ServiceType.HTTP);
|
||||
});
|
||||
|
||||
tap.test('should detect service type for HTTPS', async () => {
|
||||
tap.test('should detect HTTPS service on port 443', async () => {
|
||||
const result = await testDetector.isActive('https://example.com', { detectServiceType: true });
|
||||
expect(result.isActive).toBeTrue();
|
||||
expect(result.serviceType).toEqual(detector.ServiceType.HTTPS);
|
||||
});
|
||||
|
||||
tap.test('should detect SSH service', async () => {
|
||||
tap.test('should detect SSH service on GitHub', async () => {
|
||||
const sshType = await testDetector.detectType('ssh://github.com:22');
|
||||
expect(sshType).toEqual(detector.ServiceType.SSH);
|
||||
});
|
||||
|
||||
tap.test('should return unknown for non-standard services', async () => {
|
||||
tap.test('should detect HTTPS on non-standard port', async () => {
|
||||
const result = await testDetector.isActive('https://lossless.com:443', { detectServiceType: true });
|
||||
if (result.isActive) {
|
||||
expect(result.serviceType).toEqual(detector.ServiceType.HTTPS);
|
||||
}
|
||||
});
|
||||
|
||||
// Direct detectType tests
|
||||
tap.test('should detect common services by port number', async () => {
|
||||
// Test FTP port
|
||||
const ftpType = await testDetector.detectType('ftp://localhost:21');
|
||||
// Since localhost:21 is likely not running, it will try detection
|
||||
expect(ftpType).toBeTypeofString();
|
||||
});
|
||||
|
||||
tap.test('should return UNKNOWN for non-standard ports', async () => {
|
||||
const result = await testDetector.isActive('http://localhost:9999', { detectServiceType: true });
|
||||
if (result.isActive) {
|
||||
expect(result.serviceType).toEqual(detector.ServiceType.UNKNOWN);
|
||||
}
|
||||
});
|
||||
|
||||
export default tap.start();
|
||||
// Edge cases
|
||||
tap.test('should handle invalid URLs gracefully', async () => {
|
||||
try {
|
||||
await testDetector.isActive('not-a-valid-url');
|
||||
} catch (error) {
|
||||
expect(error).toBeInstanceOf(Error);
|
||||
}
|
||||
});
|
||||
|
||||
tap.test('should handle localhost with detectServiceType', async () => {
|
||||
const result = await testDetector.isActive('http://localhost:8080', { detectServiceType: true });
|
||||
expect(result).toHaveProperty('isActive');
|
||||
expect(result).toHaveProperty('serviceType');
|
||||
});
|
||||
|
||||
// Multiple service checks
|
||||
tap.test('should correctly identify multiple HTTPS services', async () => {
|
||||
const services = [
|
||||
{ url: 'https://google.com', expected: detector.ServiceType.HTTPS },
|
||||
{ url: 'https://github.com', expected: detector.ServiceType.HTTPS },
|
||||
];
|
||||
|
||||
for (const service of services) {
|
||||
const result = await testDetector.isActive(service.url, { detectServiceType: true });
|
||||
if (result.isActive) {
|
||||
expect(result.serviceType).toEqual(service.expected);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Performance test
|
||||
tap.test('should complete detection within reasonable time', async () => {
|
||||
const startTime = Date.now();
|
||||
await testDetector.isActive('https://example.com', { detectServiceType: true });
|
||||
const duration = Date.now() - startTime;
|
||||
// Should complete within 10 seconds
|
||||
expect(duration).toBeLessThan(10000);
|
||||
});
|
||||
|
||||
// Test without service detection
|
||||
tap.test('should work without service detection option', async () => {
|
||||
const result = await testDetector.isActive('https://example.com');
|
||||
expect(result.isActive).toBeTrue();
|
||||
expect(result.serviceType).toBeUndefined();
|
||||
});
|
||||
|
||||
// Test specific ports
|
||||
tap.test('should handle MySQL default port', async () => {
|
||||
const mysqlType = await testDetector.detectType('mysql://localhost:3306');
|
||||
// Will return MYSQL based on port, but actual detection depends on service running
|
||||
expect([detector.ServiceType.MYSQL, detector.ServiceType.UNKNOWN]).toContain(mysqlType);
|
||||
});
|
||||
|
||||
tap.test('should handle Redis default port', async () => {
|
||||
const redisType = await testDetector.detectType('redis://localhost:6379');
|
||||
expect([detector.ServiceType.REDIS, detector.ServiceType.UNKNOWN]).toContain(redisType);
|
||||
});
|
||||
|
||||
// Local port tests
|
||||
tap.test('should detect commonly used local development ports', async () => {
|
||||
const localPorts = [
|
||||
{ url: 'http://localhost:3000', name: 'Node.js dev server' },
|
||||
{ url: 'http://localhost:4200', name: 'Angular dev server' },
|
||||
{ url: 'http://localhost:8080', name: 'Common web server' },
|
||||
];
|
||||
|
||||
for (const port of localPorts) {
|
||||
const result = await testDetector.isActive(port.url);
|
||||
// Just check that it returns a valid result structure
|
||||
expect(result).toHaveProperty('isActive');
|
||||
expect(result.isActive).toBeTypeofBoolean();
|
||||
}
|
||||
});
|
||||
|
||||
export default tap.start();
|
||||
Reference in New Issue
Block a user