-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-feedback.js
More file actions
154 lines (131 loc) Β· 5.86 KB
/
Copy pathtest-feedback.js
File metadata and controls
154 lines (131 loc) Β· 5.86 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
/**
* Test script to verify like/dislike functionality is working
* Tests both the feedback submission and user stats retrieval
*/
const BACKEND_URL = 'http://localhost:8004';
async function testFeedbackFlow() {
console.log('π§ͺ Testing Like/Dislike Functionality...\n');
const testUserId = 'test-user-' + Date.now();
const testItemId = 'item_123';
try {
// Test 1: Submit a LIKE feedback
console.log('1οΈβ£ Testing LIKE feedback submission...');
const likeResponse = await fetch(`${BACKEND_URL}/feedback`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
user_id: testUserId,
item_id: testItemId,
feedback: 'like'
})
});
if (!likeResponse.ok) {
throw new Error(`Like feedback failed: ${likeResponse.status} ${likeResponse.statusText}`);
}
const likeResult = await likeResponse.json();
console.log('β
LIKE feedback submitted successfully:');
console.log(' - Status:', likeResult.status);
console.log(' - Message:', likeResult.message);
console.log(' - User Stats:', likeResult.user_stats);
console.log(' - Total Feedback:', likeResult.user_stats.total_feedback);
console.log(' - Likes:', likeResult.user_stats.likes);
console.log(' - Dislikes:', likeResult.user_stats.dislikes);
console.log(' - Personalization Score:', likeResult.user_stats.personalization_score);
console.log();
// Test 2: Submit a DISLIKE feedback
console.log('2οΈβ£ Testing DISLIKE feedback submission...');
const dislikeResponse = await fetch(`${BACKEND_URL}/feedback`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
user_id: testUserId,
item_id: 'item_456',
feedback: 'dislike'
})
});
if (!dislikeResponse.ok) {
throw new Error(`Dislike feedback failed: ${dislikeResponse.status} ${dislikeResponse.statusText}`);
}
const dislikeResult = await dislikeResponse.json();
console.log('β
DISLIKE feedback submitted successfully:');
console.log(' - Status:', dislikeResult.status);
console.log(' - Message:', dislikeResult.message);
console.log(' - User Stats:', dislikeResult.user_stats);
console.log(' - Total Feedback:', dislikeResult.user_stats.total_feedback);
console.log(' - Likes:', dislikeResult.user_stats.likes);
console.log(' - Dislikes:', dislikeResult.user_stats.dislikes);
console.log(' - Personalization Score:', dislikeResult.user_stats.personalization_score);
console.log();
// Test 3: Get user stats
console.log('3οΈβ£ Testing user stats retrieval...');
const statsResponse = await fetch(`${BACKEND_URL}/users/${testUserId}/stats`);
if (!statsResponse.ok) {
throw new Error(`User stats failed: ${statsResponse.status} ${statsResponse.statusText}`);
}
const statsResult = await statsResponse.json();
console.log('β
User stats retrieved successfully:');
console.log(' - User ID:', statsResult.user_id);
console.log(' - Total Feedback:', statsResult.total_feedback);
console.log(' - Likes:', statsResult.likes);
console.log(' - Dislikes:', statsResult.dislikes);
console.log(' - Personalization Score:', statsResult.personalization_score);
console.log(' - Feedback History Length:', statsResult.feedback_history.length);
console.log();
// Test 4: Get feedback history
console.log('4οΈβ£ Testing feedback history retrieval...');
const historyResponse = await fetch(`${BACKEND_URL}/users/${testUserId}/feedback`);
if (!historyResponse.ok) {
throw new Error(`Feedback history failed: ${historyResponse.status} ${historyResponse.statusText}`);
}
const historyResult = await historyResponse.json();
console.log('β
Feedback history retrieved successfully:');
console.log(' - User ID:', historyResult.user_id);
console.log(' - Total Feedback:', historyResult.total_feedback);
console.log(' - History Items:', historyResult.feedback_history.length);
if (historyResult.feedback_history.length > 0) {
console.log(' - Recent Feedback:');
historyResult.feedback_history.forEach((item, index) => {
console.log(` ${index + 1}. Item: ${item.item_id}, Feedback: ${item.feedback}, Time: ${item.timestamp}`);
});
}
console.log();
// Test 5: Test personalization impact
console.log('5οΈβ£ Testing personalization impact...');
// Submit multiple likes to see if personalization score changes
for (let i = 0; i < 3; i++) {
const moreTestResponse = await fetch(`${BACKEND_URL}/feedback`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
user_id: testUserId,
item_id: `item_${1000 + i}`,
feedback: 'like'
})
});
if (moreTestResponse.ok) {
const moreResult = await moreTestResponse.json();
console.log(` - Feedback ${i + 1}: Personalization Score: ${moreResult.user_stats.personalization_score}`);
}
}
console.log();
console.log('π All feedback tests completed successfully!');
console.log('β
Like/Dislike functionality is working correctly');
console.log('β
User stats are being tracked properly');
console.log('β
Feedback history is being stored');
console.log('β
Personalization scores are updating');
} catch (error) {
console.error('β Test failed:', error.message);
if (error.message.includes('ECONNREFUSED')) {
console.error('π¨ Backend server is not running on http://localhost:8004');
console.error(' Please start the backend with: python adaptive_api.py');
}
}
}
// Run the test
testFeedbackFlow();