@drfrost/bods-js
Version:
JavaScript client for the UK's Bus Open Data Service (BODS) API
122 lines (97 loc) ⢠3.48 kB
text/typescript
/**
* Integration test for BODS API
*
* To run this test with a real API key:
* BODS_API_KEY=your-api-key bun run src/test/integration.test.ts
*/
import { BODSClient } from '../index.js';
const API_KEY = process.env.BODS_API_KEY;
if (!API_KEY) {
console.log('āļø Skipping integration tests - BODS_API_KEY not provided');
console.log('To run integration tests: BODS_API_KEY=your-api-key bun run src/test/integration.test.ts');
process.exit(0);
}
const client = new BODSClient({
apiKey: API_KEY,
timeout: 10000
});
async function testTimetables() {
console.log('š Testing Timetables API...');
try {
const timetables = await client.timetables.search({ limit: 5 });
console.log(`ā
Found ${timetables.count} timetables`);
if (timetables.results.length > 0) {
const first = timetables.results[0]!;
console.log(` - ${first.operatorName}: ${first.name}`);
const detailed = await client.timetables.getById(first.id);
console.log(`ā
Retrieved detailed timetable: ${detailed.name}`);
}
} catch (error) {
console.error('ā Timetables test failed:', error);
}
}
async function testFares() {
console.log('š Testing Fares API...');
try {
const fares = await client.fares.search({ limit: 3 });
console.log(`ā
Found ${fares.count} fare datasets`);
if (fares.results.length > 0) {
const first = fares.results[0]!;
console.log(` - ${first.operatorName}: ${first.numOfFareZones} zones`);
}
} catch (error) {
console.error('ā Fares test failed:', error);
}
}
async function testAVL() {
console.log('š Testing AVL API...');
try {
const vehicles = await client.avl.getSIRIVM({
boundingBox: [-2.930, 53.374, -3.085, 53.453] // Liverpool area
});
console.log(`ā
Retrieved SIRI-VM data: ${vehicles.xmlData.length} characters`);
const gtfsVehicles = await client.avl.getGTFSRT({
boundingBox: [-2.930, 53.374, -3.085, 53.453]
});
console.log(`ā
Retrieved GTFS-RT data: ${gtfsVehicles.protobufData.byteLength} bytes`);
} catch (error) {
console.error('ā AVL test failed:', error);
}
}
async function testDisruptions() {
console.log('š Testing Disruptions API...');
try {
const disruptions = await client.disruptions.getCurrent();
console.log(`ā
Retrieved disruptions: ${disruptions.xmlData.length} characters`);
const parsed = await client.disruptions.getCurrentParsed();
console.log(`ā
Parsed ${parsed.length} disruptions`);
if (parsed.length > 0) {
const first = parsed[0]!;
console.log(` - ${first.participantRef}: ${first.summary}`);
}
} catch (error) {
console.error('ā Disruptions test failed:', error);
}
}
async function testConnectivity() {
console.log('š Testing API connectivity...');
try {
const isConnected = await client.testConnection();
console.log(`ā
API connectivity: ${isConnected ? 'SUCCESS' : 'FAILED'}`);
} catch (error) {
console.error('ā Connectivity test failed:', error);
}
}
async function runIntegrationTests() {
console.log('š BODS Integration Tests');
console.log('='.repeat(30));
await testConnectivity();
await testTimetables();
await testFares();
await testAVL();
await testDisruptions();
console.log('\nā
Integration tests completed!');
}
if (import.meta.main) {
runIntegrationTests().catch(console.error);
}