TestRoom_SendTo 测试点对点消息
(t *testing.T)
| 345 | |
| 346 | // TestRoom_SendTo 测试点对点消息 |
| 347 | func TestRoom_SendTo(t *testing.T) { |
| 348 | deps := createTestDeps(t) |
| 349 | pool := NewPool(&PoolOptions{ |
| 350 | Dependencies: deps, |
| 351 | MaxAgents: 10, |
| 352 | }) |
| 353 | defer func() { |
| 354 | if err := pool.Shutdown(); err != nil { |
| 355 | t.Errorf("Shutdown failed: %v", err) |
| 356 | } |
| 357 | }() |
| 358 | |
| 359 | room := NewRoom(pool) |
| 360 | ctx := context.Background() |
| 361 | |
| 362 | // 创建 Agent |
| 363 | config1 := createTestConfig("agent-1") |
| 364 | _, err := pool.Create(ctx, config1) |
| 365 | if err != nil { |
| 366 | t.Fatalf("Failed to create agent-1: %v", err) |
| 367 | } |
| 368 | |
| 369 | config2 := createTestConfig("agent-2") |
| 370 | _, err = pool.Create(ctx, config2) |
| 371 | if err != nil { |
| 372 | t.Fatalf("Failed to create agent-2: %v", err) |
| 373 | } |
| 374 | |
| 375 | // 加入 Room |
| 376 | if err := room.Join("alice", "agent-1"); err != nil { |
| 377 | t.Fatalf("Failed to join room: %v", err) |
| 378 | } |
| 379 | if err := room.Join("bob", "agent-2"); err != nil { |
| 380 | t.Fatalf("Failed to join room: %v", err) |
| 381 | } |
| 382 | |
| 383 | // 发送点对点消息 |
| 384 | err = room.SendTo(ctx, "alice", "bob", "Private message") |
| 385 | if err != nil { |
| 386 | t.Fatalf("Failed to send private message: %v", err) |
| 387 | } |
| 388 | |
| 389 | // 检查历史 |
| 390 | history := room.GetHistory() |
| 391 | if len(history) != 1 { |
| 392 | t.Errorf("Expected 1 message in history, got %d", len(history)) |
| 393 | } |
| 394 | |
| 395 | if len(history) > 0 { |
| 396 | msg := history[0] |
| 397 | if msg.From != "alice" { |
| 398 | t.Errorf("Expected message from alice, got %s", msg.From) |
| 399 | } |
| 400 | if len(msg.To) != 1 || msg.To[0] != "bob" { |
| 401 | t.Errorf("Expected message to bob, got %v", msg.To) |
| 402 | } |
| 403 | } |
| 404 | } |
nothing calls this directly
no test coverage detected