Note: session.connect, session.disconnect, session.logout tests are skipped because they require full WhatsApp/whatsmeow initialization which is complex to set up in unit tests. The routing is tested via session.status. Manual/integration testing should be used for these methods.
(t *testing.T)
| 243 | // Manual/integration testing should be used for these methods. |
| 244 | |
| 245 | func TestChatSendText(t *testing.T) { |
| 246 | s := makeTestServer(t) |
| 247 | |
| 248 | // Create a user first |
| 249 | addRequest := newRequest("1", "admin.users.add", map[string]interface{}{ |
| 250 | "adminToken": "test-admin-token", |
| 251 | "name": "MessageUser", |
| 252 | "token": "message-token", |
| 253 | }).toJSON(t) |
| 254 | executeRequest(t, s, addRequest) |
| 255 | |
| 256 | // Try to send a message (will fail because no WhatsApp session, but tests routing) |
| 257 | sendRequest := newRequest("2", "chat.send.text", map[string]interface{}{ |
| 258 | "token": "message-token", |
| 259 | "Phone": "1234567890", |
| 260 | "Body": "Hello, World!", |
| 261 | }).toJSON(t) |
| 262 | sendResponse := executeRequest(t, s, sendRequest) |
| 263 | |
| 264 | // Should fail with "no session" error since we don't have WhatsApp initialized |
| 265 | expected := map[string]interface{}{ |
| 266 | "jsonrpc": "2.0", |
| 267 | "id": "2", |
| 268 | } |
| 269 | if diff := compareJSON(expected, sendResponse); diff != "" { |
| 270 | t.Errorf("Response mismatch:\n%s", diff) |
| 271 | } |
| 272 | |
| 273 | // Verify it's an error response |
| 274 | if sendResponse["error"] == nil { |
| 275 | t.Errorf("Expected error (no session), got success") |
| 276 | } |
| 277 | if sendResponse["result"] != nil { |
| 278 | t.Errorf("Expected no result on error, got: %v", sendResponse["result"]) |
| 279 | } |
| 280 | |
| 281 | errorObj := sendResponse["error"].(map[string]interface{}) |
| 282 | if errorObj["code"].(float64) != 500 { |
| 283 | t.Errorf("Expected error code 500 (no session), got %v", errorObj["code"]) |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | func TestChatHistory(t *testing.T) { |
| 288 | s := makeTestServer(t) |
nothing calls this directly
no test coverage detected