forked from sqwu/helper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-api-endpoints.js
More file actions
125 lines (110 loc) · 3.69 KB
/
test-api-endpoints.js
File metadata and controls
125 lines (110 loc) · 3.69 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env node
/**
* Quick API endpoint testing script for SWR integration verification
* Tests the API endpoints that SWR hooks depend on
*/
import { apiClient } from "./lib/client.js";
// Mock basic test data structure that matches what SWR hooks expect
const testEndpoints = [
{
name: "Dashboard Events",
endpoint: "/mailbox/latest-events",
method: "GET",
expectedFields: ["id", "conversationSlug", "timestamp", "title", "type"]
},
{
name: "Dashboard Metrics",
endpoint: "/mailbox/dashboard-metrics",
method: "GET",
expectedFields: ["totalConversations", "openConversations", "resolvedToday", "averageResponseTime", "satisfaction"]
},
{
name: "Conversations List",
endpoint: "/mailbox/conversations",
method: "GET",
expectedFields: ["id", "slug", "subject", "status", "createdAt"]
},
{
name: "Conversation Messages",
endpoint: "/conversation/test-slug/messages",
method: "GET",
expectedFields: ["id", "content", "createdAt", "type"]
},
{
name: "Presence Data",
endpoint: "/presence/test-channel",
method: "GET",
expectedFields: ["id", "name"]
}
];
console.log("🧪 Starting API endpoint verification for SWR integration...\n");
async function testEndpoint(test) {
try {
console.log(`Testing ${test.name} (${test.method} ${test.endpoint})...`);
const response = await fetch(`http://localhost:3010/api${test.endpoint}`, {
method: test.method,
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer test-token' // Mock auth for testing
}
});
if (!response.ok) {
console.log(` ❌ HTTP ${response.status}: ${response.statusText}`);
return false;
}
const data = await response.json();
console.log(` ✅ Response received`);
// Verify response structure for arrays
if (Array.isArray(data)) {
if (data.length > 0) {
const firstItem = data[0];
const missingFields = test.expectedFields.filter(field => !(field in firstItem));
if (missingFields.length > 0) {
console.log(` ⚠️ Missing expected fields: ${missingFields.join(', ')}`);
} else {
console.log(` ✅ All expected fields present`);
}
} else {
console.log(` ✅ Empty array returned (expected for test data)`);
}
} else if (typeof data === 'object') {
// Verify response structure for objects
const missingFields = test.expectedFields.filter(field => !(field in data));
if (missingFields.length > 0) {
console.log(` ⚠️ Missing expected fields: ${missingFields.join(', ')}`);
} else {
console.log(` ✅ All expected fields present`);
}
}
return true;
} catch (error) {
console.log(` ❌ Error: ${error.message}`);
return false;
}
}
async function runTests() {
let passed = 0;
let total = testEndpoints.length;
for (const test of testEndpoints) {
const success = await testEndpoint(test);
if (success) passed++;
console.log(); // Empty line for readability
}
console.log(`📊 Test Results: ${passed}/${total} endpoints working`);
if (passed === total) {
console.log("🎉 All API endpoints are functional and compatible with SWR hooks!");
} else {
console.log(`⚠️ ${total - passed} endpoints need attention`);
}
}
// Check if server is running first
fetch('http://localhost:3010/api/health')
.then(() => {
console.log("✅ Development server is running\n");
runTests();
})
.catch(() => {
console.log("❌ Development server is not running on port 3010");
console.log("Please start the server with: npm run dev\n");
process.exit(1);
});