-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-test-cli.js
More file actions
159 lines (139 loc) · 3.81 KB
/
api-test-cli.js
File metadata and controls
159 lines (139 loc) · 3.81 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/**
* Simple CLI for testing the wellness API endpoints
*/
import fetch from 'node-fetch';
import readline from 'readline';
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const API_URL = 'http://localhost:5173/api/v1';
const AUTH_TOKEN = 'test-auth-token';
// Helper function to make API requests
async function makeRequest(endpoint, method = 'GET', data = null) {
const options = {
method,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${AUTH_TOKEN}`
}
};
if (data && (method === 'POST' || method === 'PUT')) {
options.body = JSON.stringify(data);
}
try {
console.log(`\nRequesting ${method} ${API_URL}${endpoint}`);
const response = await fetch(`${API_URL}${endpoint}`, options);
console.log(`Response status: ${response.status}`);
const responseText = await response.text();
try {
if (responseText) {
const responseData = JSON.parse(responseText);
console.log('Response data:');
console.log(JSON.stringify(responseData, null, 2));
return { status: response.status, data: responseData };
} else {
console.log('Empty response');
return { status: response.status, data: null };
}
} catch (parseError) {
console.log(`Response is not JSON: ${responseText}`);
return { status: response.status, data: { raw: responseText } };
}
} catch (error) {
console.error(`Error making request:`, error.message);
return { status: 500, error: error.message };
}
}
// Test data
const testMoodEntry = {
userId: 'test-user-id',
rating: 8,
note: 'Feeling great today!',
tags: ['relaxed', 'happy', 'productive'],
createdAt: new Date().toISOString()
};
const testSymptomEntry = {
userId: 'test-user-id',
symptom: 'Headache',
severity: 5,
duration: '2 hours',
notes: 'Mild headache after working on computer',
tags: ['stress', 'work'],
createdAt: new Date().toISOString()
};
const testJournalEntry = {
userId: 'test-user-id',
title: 'Today\'s Reflections',
content: 'Had a productive day working on my wellness tracking features...',
tags: ['coding', 'wellness', 'productivity'],
mood: 7,
isPrivate: true,
createdAt: new Date().toISOString()
};
// Available tests
const tests = {
'health': async () => {
await makeRequest('/health');
},
'create-mood': async () => {
await makeRequest('/mood', 'POST', testMoodEntry);
},
'list-moods': async () => {
await makeRequest('/mood');
},
'create-symptom': async () => {
await makeRequest('/medical-symptoms', 'POST', testSymptomEntry);
},
'list-symptoms': async () => {
await makeRequest('/medical-symptoms');
},
'create-journal': async () => {
await makeRequest('/journal', 'POST', testJournalEntry);
},
'list-journals': async () => {
await makeRequest('/journal');
}
};
function showMenu() {
console.log('\n=== API Test CLI ===');
console.log('Available commands:');
Object.keys(tests).forEach(cmd => {
console.log(`- ${cmd}`);
});
console.log('- exit (quit the program)');
console.log('- help (show this menu)');
}
async function runCommand(cmd) {
if (cmd === 'exit') {
rl.close();
return;
}
if (cmd === 'help') {
showMenu();
return;
}
if (tests[cmd]) {
try {
await tests[cmd]();
} catch (error) {
console.error('Error executing command:', error);
}
} else {
console.log(`Unknown command: ${cmd}`);
console.log('Type "help" to see available commands');
}
}
// Start the CLI
console.log('Welcome to the Wellness API Test CLI');
showMenu();
rl.on('line', async (line) => {
const cmd = line.trim();
await runCommand(cmd);
rl.prompt();
});
rl.on('close', () => {
console.log('Exiting API Test CLI. Goodbye!');
process.exit(0);
});
rl.prompt();