Files
shop-api/test-chatbot-socket.js
morteza 78d8795155
Build and Deploy / build_and_deploy (push) Has been cancelled
update : bycryptjs
2026-06-14 08:49:13 +03:30

99 lines
2.5 KiB
JavaScript

// Test script for ChatbotGateway Socket.IO connection
// Run with: node test-chatbot-socket.js
const io = require("socket.io-client");
// Connect to the chatbot gateway
// The path option matches the path configured in ChatbotGateway
const socket = io("http://localhost:4000", {
path: "/ws-chatbot",
transports: ["websocket"],
// Optional: Add token for authenticated connection
// query: {
// token: "your-jwt-token-here"
// }
});
socket.on("connect", () => {
console.log("✅ Connected to ChatbotGateway!");
console.log("Socket ID:", socket.id);
// Test: Create a session
console.log("\n📤 Creating session...");
socket.emit("create_session");
});
socket.on("authenticated", (data) => {
console.log("✅ Authenticated:", data);
});
socket.on("unauthorized", (data) => {
console.log("❌ Unauthorized:", data);
});
socket.on("session_created", (data) => {
console.log("✅ Session created:", JSON.stringify(data, null, 2));
// Test: Join the session
if (data.data && data.data.id) {
console.log("\n📤 Joining session:", data.data.id);
socket.emit("join_chat", data.data.id);
}
});
socket.on("chat_joined", (data) => {
console.log("✅ Joined chat:", JSON.stringify(data, null, 2));
// Test: Send a message
if (data.data && data.data.id) {
console.log("\n📤 Sending test message...");
socket.emit("send_message", {
sessionId: data.data.id,
content: "Hello, chatbot!",
});
}
});
socket.on("message_received", (data) => {
console.log("📨 Message received:", JSON.stringify(data, null, 2));
});
socket.on("bot_response_start", (data) => {
console.log("🤖 Bot response started:", JSON.stringify(data, null, 2));
});
socket.on("bot_response_chunk", (data) => {
process.stdout.write(data.data?.chunk || "");
});
socket.on("bot_response_end", (data) => {
console.log("\n✅ Bot response completed:", JSON.stringify(data, null, 2));
});
socket.on("bot_response", (data) => {
console.log("🤖 Bot response:", JSON.stringify(data, null, 2));
});
socket.on("error", (data) => {
console.log("❌ Error:", JSON.stringify(data, null, 2));
});
socket.on("session_error", (data) => {
console.log("❌ Session error:", JSON.stringify(data, null, 2));
});
socket.on("message_error", (data) => {
console.log("❌ Message error:", JSON.stringify(data, null, 2));
});
socket.on("disconnect", (reason) => {
console.log("❌ Disconnected:", reason);
});
socket.on("connect_error", (error) => {
console.log("❌ Connection error:", error.message);
});
// Keep the script running
process.stdin.resume();