MCPcopy Create free account
hub / github.com/astercloud/aster / TestAgentEventBus

Function TestAgentEventBus

pkg/agent/agent_test.go:83–132  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

81}
82
83func 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 创建测试依赖
135func setupTestDeps(t *testing.T) *Dependencies {

Callers

nothing calls this directly

Calls 6

setupTestDepsFunction · 0.85
CreateFunction · 0.85
EmitMonitorMethod · 0.80
CloseMethod · 0.65
SubscribeMethod · 0.65
ErrorMethod · 0.65

Tested by

no test coverage detected