forked from shreyan001/backend-socials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-server-responses.js
More file actions
143 lines (124 loc) · 4.56 KB
/
test-server-responses.js
File metadata and controls
143 lines (124 loc) · 4.56 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
const { HumanMessage, AIMessage } = require('@langchain/core/messages');
// Mock the graph module since we can't import it directly in this test
const mockGetActualLangGraph = () => {
return {
async *stream(initialState, options) {
// Simulate initial_node response
yield {
'initial_node': {
messages: ['I understand you want to create a wager. Let me help you with that.']
}
};
// Simulate extraction node asking for missing info
if (!initialState.input.includes('prediction')) {
yield {
'wager_info_extraction_node': {
messages: ['What is your prediction for this event?']
}
};
}
// If we have all info, simulate validation node
if (initialState.input.includes('prediction') && initialState.input.includes('vs')) {
yield {
'wager_validation_node': {
messages: ['[OBJ]{"event":"India vs England","playerA":{"name":"You","address":"0x123","isDeposited":false},"playerB":{"name":"Friend","address":"0x456","isDeposited":false},"prediction":"India will win","amount":"10","token":"USDC","outcome":null}[/OBJ]']
}
};
}
}
};
};
// Test different scenarios
async function testServerResponse(input, expectedMessageCount, description) {
console.log(`\n=== Testing: ${description} ===`);
console.log(`Input: "${input}"`);
try {
const mockGraph = mockGetActualLangGraph();
const initialState = {
input,
chat_history: [],
playerA: {
name: "You",
address: "0x123",
isDeposited: false
}
};
const stream = await mockGraph.stream(initialState, {
streamMode: "updates",
recursionLimit: 100,
});
const messages = [];
for await (const value of stream) {
for (const [nodeName, nodeOutput] of Object.entries(value)) {
const output = nodeOutput;
if (output?.messages?.[0]) {
const message = output.messages[0];
// Check if this is a wager object message
const wagerObjectMatch = message.match(/\[OBJ\](.*?)\[\/OBJ\]/);
if (wagerObjectMatch && nodeName === "wager_validation_node") {
try {
const wagerObject = JSON.parse(wagerObjectMatch[1]);
messages.push({
type: "wager",
content: "Wager created successfully!",
wager: wagerObject
});
} catch (error) {
messages.push({
type: "message",
content: "Error creating wager object",
wager: null
});
}
} else {
messages.push({
type: "message",
content: message,
wager: null
});
}
}
}
}
console.log(`Expected ${expectedMessageCount} messages, got ${messages.length}`);
messages.forEach((msg, i) => {
console.log(` ${i + 1}. [${msg.type}] ${msg.content}`);
if (msg.wager) {
console.log(` Wager: ${JSON.stringify(msg.wager, null, 2)}`);
}
});
if (messages.length >= expectedMessageCount) {
console.log(`✅ PASS: Got at least ${expectedMessageCount} message(s)`);
} else {
console.log(`❌ FAIL: Expected at least ${expectedMessageCount} messages, got ${messages.length}`);
}
} catch (error) {
console.log(`❌ ERROR: ${error.message}`);
}
}
async function runTests() {
console.log('Testing server response behavior...\n');
// Test 1: Incomplete wager (missing prediction)
await testServerResponse(
"I want to bet on India vs England match",
2, // Should get initial response + ask for prediction
"Incomplete wager - missing prediction"
);
// Test 2: Complete wager
await testServerResponse(
"I want to bet on India vs England match, my prediction is India will win",
2, // Should get initial response + wager object
"Complete wager - all info provided"
);
// Test 3: Very incomplete wager
await testServerResponse(
"I want to make a bet",
2, // Should get initial response + ask for more info
"Very incomplete wager - minimal info"
);
console.log('\n=== Test Summary ===');
console.log('✅ All tests check that the backend ALWAYS sends at least one message');
console.log('✅ The server should never leave the frontend hanging without a response');
console.log('✅ Use "end" event type (not "streamEnd") for frontend compatibility');
}
runTests().catch(console.error);