-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-test-mock.js
More file actions
144 lines (125 loc) · 3.73 KB
/
api-test-mock.js
File metadata and controls
144 lines (125 loc) · 3.73 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
/**
* API Test Script for Development Environment using mock auth
*/
import fetch from 'node-fetch';
// Use the confirmed port from Vite logs
const API_URL = 'http://localhost:4001/api/v1';
// Helper function to make API requests with mock auth only
async function makeRequest(endpoint, method = 'GET', data = null) {
const options = {
method,
headers: {
'Content-Type': 'application/json',
// Use ONLY mock auth header for testing
'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 one endpoint at a time
async function testHealthEndpoint() {
console.log('=== Testing Health Endpoint ===');
await makeRequest('/health');
}
async function testMoodEndpoint() {
console.log('=== Testing Mood Endpoint ===');
await makeRequest('/mood');
}
async function testCreateMood() {
console.log('=== Testing Create Mood ===');
await makeRequest('/mood', 'POST', testMoodEntry);
}
async function testMedicalSymptomsEndpoint() {
console.log('=== Testing Medical Symptoms Endpoint ===');
await makeRequest('/medical-symptoms');
}
async function testCreateSymptom() {
console.log('=== Testing Create Symptom ===');
await makeRequest('/medical-symptoms', 'POST', testSymptomEntry);
}
async function testJournalEndpoint() {
console.log('=== Testing Journal Endpoint ===');
await makeRequest('/journal');
}
async function testCreateJournal() {
console.log('=== Testing Create Journal ===');
await makeRequest('/journal', 'POST', testJournalEntry);
}
// Execute the selected test
const args = process.argv.slice(2);
const testToRun = args[0] || 'health';
switch (testToRun) {
case 'health':
testHealthEndpoint();
break;
case 'mood':
testMoodEndpoint();
break;
case 'create-mood':
testCreateMood();
break;
case 'symptoms':
testMedicalSymptomsEndpoint();
break;
case 'create-symptom':
testCreateSymptom();
break;
case 'journal':
testJournalEndpoint();
break;
case 'create-journal':
testCreateJournal();
break;
default:
console.log('Unknown test. Available tests: health, mood, create-mood, symptoms, create-symptom, journal, create-journal');
}