UNPKG

@drfrost/bods-js

Version:

JavaScript client for the UK's Bus Open Data Service (BODS) API

122 lines (97 loc) • 3.48 kB
/** * 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); }