(t *testing.T)
| 81 | } |
| 82 | |
| 83 | func TestAgentEventBus(t *testing.T) { |
| 84 | deps := setupTestDeps(t) |
| 85 | |
| 86 | config := &types.AgentConfig{ |
| 87 | TemplateID: "test-template", |
| 88 | ModelConfig: &types.ModelConfig{ |
| 89 | Provider: "anthropic", |
| 90 | Model: "claude-sonnet-4-5", |
| 91 | APIKey: "test-key", |
| 92 | }, |
| 93 | Sandbox: &types.SandboxConfig{ |
| 94 | Kind: types.SandboxKindMock, |
| 95 | WorkDir: "/tmp/test", |
| 96 | }, |
| 97 | } |
| 98 | |
| 99 | ag, err := Create(context.Background(), config, deps) |
| 100 | if err != nil { |
| 101 | t.Fatalf("Failed to create agent: %v", err) |
| 102 | } |
| 103 | defer func() { _ = ag.Close() }() |
| 104 | |
| 105 | // 订阅事件 |
| 106 | eventCh := ag.Subscribe([]types.AgentChannel{types.ChannelMonitor}, nil) |
| 107 | |
| 108 | // 发送一个测试事件 |
| 109 | go func() { |
| 110 | time.Sleep(100 * time.Millisecond) |
| 111 | ag.eventBus.EmitMonitor(&types.MonitorStateChangedEvent{ |
| 112 | State: types.AgentStateWorking, |
| 113 | }) |
| 114 | }() |
| 115 | |
| 116 | // 等待事件 |
| 117 | select { |
| 118 | case envelope := <-eventCh: |
| 119 | if envelope.Event == nil { |
| 120 | t.Error("Event should not be nil") |
| 121 | } |
| 122 | if evt, ok := envelope.Event.(*types.MonitorStateChangedEvent); ok { |
| 123 | if evt.State != types.AgentStateWorking { |
| 124 | t.Errorf("Expected state Working, got %s", evt.State) |
| 125 | } |
| 126 | } else { |
| 127 | t.Error("Event should be MonitorStateChangedEvent") |
| 128 | } |
| 129 | case <-time.After(1 * time.Second): |
| 130 | t.Error("Timeout waiting for event") |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // setupTestDeps 创建测试依赖 |
| 135 | func setupTestDeps(t *testing.T) *Dependencies { |
nothing calls this directly
no test coverage detected