-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-api.js
More file actions
109 lines (93 loc) Β· 3.01 KB
/
test-api.js
File metadata and controls
109 lines (93 loc) Β· 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const fetch = require('node-fetch');
const BASE_URL = 'http://localhost:3000/api';
async function testEndpoint(name, url, expectedFields = []) {
try {
console.log(`\nπ§ͺ Testing ${name}...`);
const response = await fetch(url);
const data = await response.json();
if (data.success) {
console.log(`β
${name} - Success`);
if (expectedFields.length > 0) {
const hasAllFields = expectedFields.every(field => {
const hasField = data.data && data.data[field] !== undefined;
if (!hasField) console.log(` β οΈ Missing field: ${field}`);
return hasField;
});
if (hasAllFields) {
console.log(` β
All expected fields present`);
}
}
console.log(` π Response: ${JSON.stringify(data.data).substring(0, 100)}...`);
} else {
console.log(`β ${name} - Failed: ${data.error}`);
}
} catch (error) {
console.log(`β ${name} - Error: ${error.message}`);
}
}
async function runTests() {
console.log('π Starting BC Transit API Tests');
console.log('=====================================');
// Wait for server to be ready
console.log('β³ Waiting for server to start...');
await new Promise(resolve => setTimeout(resolve, 5000));
// Test health endpoint
await testEndpoint(
'Health Check',
`${BASE_URL}/health`,
['status', 'uptime', 'version']
);
// Test API documentation
await testEndpoint(
'API Documentation',
`${BASE_URL}`,
['title', 'version', 'endpoints']
);
// Test nearby stops (Victoria downtown coordinates)
await testEndpoint(
'Nearby Stops',
`${BASE_URL}/stops/nearby?lat=48.4284&lon=-123.3656&radius=1000`,
['location', 'radius', 'stopCount', 'stops']
);
// Test routes list
await testEndpoint(
'Routes List',
`${BASE_URL}/routes?page=1&limit=5`,
[]
);
// Test vehicle positions
await testEndpoint(
'Vehicle Positions',
`${BASE_URL}/vehicles`,
['vehicleCount', 'vehicles']
);
// Test service alerts
await testEndpoint(
'Service Alerts',
`${BASE_URL}/alerts`,
['alertCount', 'alerts']
);
// Test with a common BC Transit stop ID (if it exists)
await testEndpoint(
'Bus Arrivals (Test Stop)',
`${BASE_URL}/arrivals/12345`,
['stopId', 'arrivalCount', 'arrivals']
);
// Test stop details
await testEndpoint(
'Stop Details (Test Stop)',
`${BASE_URL}/stops/12345`
);
console.log('\nπ Tests completed!');
console.log('=====================================');
console.log('π‘ Tips:');
console.log(' β’ Check the server logs for GTFS data loading status');
console.log(' β’ Some endpoints may return empty data if no real-time info is available');
console.log(' β’ Try different stop IDs from the BC Transit system');
console.log(' β’ Use nearby stops to find valid stop IDs in your area');
}
// Run tests if this file is executed directly
if (require.main === module) {
runTests().catch(console.error);
}
module.exports = { runTests, testEndpoint };