-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-test-dev.js
More file actions
190 lines (158 loc) · 5.74 KB
/
api-test-dev.js
File metadata and controls
190 lines (158 loc) · 5.74 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* API Test Script for Development Environment
*/
import fetch from 'node-fetch';
const API_URL = 'http://localhost:4001/api/v1';
// Helper function to make API requests with mock auth
async function makeRequest(endpoint, method = 'GET', data = null) {
const options = {
method,
headers: {
'Content-Type': 'application/json',
// Include both headers for proper development auth
'Authorization': 'Bearer mock-token-for-development',
'X-Mock-User-Type': 'test'
}
};
if (data && (method === 'POST' || method === 'PUT')) {
options.body = JSON.stringify(data);
}
try {
console.log(`\n[${method}] ${endpoint}`);
const response = await fetch(`${API_URL}${endpoint}`, options);
console.log(`Status: ${response.status}`);
let responseData = null;
const responseText = await response.text();
try {
if (responseText.trim()) {
responseData = JSON.parse(responseText);
console.log(JSON.stringify(responseData, null, 2));
} else {
console.log('Empty response body');
}
} catch (e) {
console.log(`Non-JSON response: ${responseText}`);
responseData = { raw: responseText };
}
return { status: response.status, data: responseData };
} catch (error) {
console.error(`Error making request to ${endpoint}:`, error.message);
return { status: 500, error: error.message };
}
}
// Test data for creating entries
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()
};
// Test all endpoints
async function runTests() {
console.log('=== Testing API Endpoints (Development Environment) ===');
// 1. Test health endpoint
console.log('\n--- Testing Health Endpoint ---');
await makeRequest('/health');
// 2. Test mood endpoints
console.log('\n--- Testing Mood Endpoints ---');
// Create a mood entry
console.log('\nCreating mood entry...');
const moodResponse = await makeRequest('/mood', 'POST', testMoodEntry);
const moodId = moodResponse.data?.data?.id;
if (moodId) {
// Get all mood entries
console.log('\nFetching all mood entries...');
await makeRequest('/mood');
// Get specific mood entry
console.log(`\nFetching mood entry ${moodId}...`);
await makeRequest(`/mood/${moodId}`);
// Update mood entry
console.log(`\nUpdating mood entry ${moodId}...`);
await makeRequest(`/mood/${moodId}`, 'PUT', {
...testMoodEntry,
rating: 9,
note: 'Feeling even better after some relaxation!'
});
// Delete mood entry
console.log(`\nDeleting mood entry ${moodId}...`);
await makeRequest(`/mood/${moodId}`, 'DELETE');
} else {
console.log('Could not create mood entry, skipping related tests');
}
// 3. Test medical symptom endpoints
console.log('\n--- Testing Medical Symptom Endpoints ---');
// Create a symptom entry
console.log('\nCreating symptom entry...');
const symptomResponse = await makeRequest('/medical-symptoms', 'POST', testSymptomEntry);
const symptomId = symptomResponse.data?.data?.id;
if (symptomId) {
// Get all symptom entries
console.log('\nFetching all symptom entries...');
await makeRequest('/medical-symptoms');
// Get specific symptom entry
console.log(`\nFetching symptom entry ${symptomId}...`);
await makeRequest(`/medical-symptoms/${symptomId}`);
// Update symptom entry
console.log(`\nUpdating symptom entry ${symptomId}...`);
await makeRequest(`/medical-symptoms/${symptomId}`, 'PUT', {
...testSymptomEntry,
severity: 3,
notes: 'Headache getting better after taking a break'
});
// Delete symptom entry
console.log(`\nDeleting symptom entry ${symptomId}...`);
await makeRequest(`/medical-symptoms/${symptomId}`, 'DELETE');
} else {
console.log('Could not create symptom entry, skipping related tests');
}
// 4. Test journal endpoints
console.log('\n--- Testing Journal Endpoints ---');
// Create a journal entry
console.log('\nCreating journal entry...');
const journalResponse = await makeRequest('/journal', 'POST', testJournalEntry);
const journalId = journalResponse.data?.data?.id;
if (journalId) {
// Get all journal entries
console.log('\nFetching all journal entries...');
await makeRequest('/journal');
// Get specific journal entry
console.log(`\nFetching journal entry ${journalId}...`);
await makeRequest(`/journal/${journalId}`);
// Update journal entry
console.log(`\nUpdating journal entry ${journalId}...`);
await makeRequest(`/journal/${journalId}`, 'PUT', {
...testJournalEntry,
title: 'Updated Reflections',
content: 'Added more details to my journal entry...'
});
// Delete journal entry
console.log(`\nDeleting journal entry ${journalId}...`);
await makeRequest(`/journal/${journalId}`, 'DELETE');
} else {
console.log('Could not create journal entry, skipping related tests');
}
console.log('\n=== Testing Complete ===');
}
// Run the tests
runTests()
.then(() => console.log('Tests finished running'))
.catch(err => console.error('Fatal error running tests:', err));