Files
smartnetwork/test/test.ipintelligence.ts

71 lines
2.6 KiB
TypeScript

import { expect, tap } from '@git.zone/tstest/tapbundle';
import * as smartnetwork from '../ts/index.js';
let testSmartNetwork: smartnetwork.SmartNetwork;
tap.test('should create a SmartNetwork instance', async () => {
testSmartNetwork = new smartnetwork.SmartNetwork();
expect(testSmartNetwork).toBeInstanceOf(smartnetwork.SmartNetwork);
});
tap.test('should get IP intelligence for 8.8.8.8 (Google)', async () => {
const result = await testSmartNetwork.getIpIntelligence('8.8.8.8');
console.log('IP Intelligence for 8.8.8.8:', JSON.stringify(result, null, 2));
// Google's ASN is 15169
expect(result.asn).toEqual(15169);
expect(result.asnOrg).toBeTruthy();
// Geolocation
expect(result.countryCode).toEqual('US');
expect(result.latitude).not.toBeNull();
expect(result.longitude).not.toBeNull();
// RDAP registration
expect(result.registrantOrg).toBeTruthy();
expect(result.networkRange).toBeTruthy();
});
tap.test('should get IP intelligence for 1.1.1.1 (Cloudflare)', async () => {
const result = await testSmartNetwork.getIpIntelligence('1.1.1.1');
console.log('IP Intelligence for 1.1.1.1:', JSON.stringify(result, null, 2));
// ASN should be Cloudflare's 13335
expect(result.asn).toEqual(13335);
expect(result.asnOrg).toBeTruthy();
// RDAP registration data should be present
expect(result.networkRange).toBeTruthy();
expect(result.registrantCountry).toBeTruthy();
// Note: 1.1.1.1 is anycast — city-level geo may be null in GeoLite2
});
tap.test('should get IP intelligence for own public IP', async () => {
const ips = await testSmartNetwork.getPublicIps();
if (ips.v4) {
const result = await testSmartNetwork.getIpIntelligence(ips.v4);
console.log(`IP Intelligence for own IP (${ips.v4}):`, JSON.stringify(result, null, 2));
expect(result.asn).toBeTypeofNumber();
expect(result.countryCode).toBeTruthy();
}
});
tap.test('should handle invalid IP gracefully', async () => {
const result = await testSmartNetwork.getIpIntelligence('999.999.999.999');
console.log('IP Intelligence for invalid IP:', JSON.stringify(result, null, 2));
// Should return nulls without throwing
expect(result).toBeTruthy();
});
tap.test('should use cache when cacheTtl is set', async () => {
const cached = new smartnetwork.SmartNetwork({ cacheTtl: 60000 });
const r1 = await cached.getIpIntelligence('8.8.8.8');
const r2 = await cached.getIpIntelligence('8.8.8.8');
// Second call should return the same cached result
expect(r1.asn).toEqual(r2.asn);
expect(r1.countryCode).toEqual(r2.countryCode);
});
export default tap.start();