This repository was archived by the owner on Aug 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilders_test.go
More file actions
630 lines (537 loc) · 16.7 KB
/
Copy pathbuilders_test.go
File metadata and controls
630 lines (537 loc) · 16.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
package sdk
import (
"context"
"errors"
"testing"
"time"
"github.com/xraph/ai-sdk/testhelpers"
)
// --- AgentBuilder Tests ---
func TestAgentBuilder(t *testing.T) {
mockLLM := testhelpers.NewMockLLM()
mockLogger := testhelpers.NewMockLogger()
mockMetrics := testhelpers.NewMockMetrics()
mockStateStore := &mockBuilderStateStore{}
t.Run("build basic agent", func(t *testing.T) {
agent, err := NewAgentBuilder().
WithID("test-agent").
WithName("Test Agent").
WithLLMManager(mockLLM).
WithStateStore(mockStateStore).
WithLogger(mockLogger).
WithMetrics(mockMetrics).
Build()
if err != nil {
t.Fatalf("failed to build agent: %v", err)
}
if agent.ID != "test-agent" {
t.Errorf("expected ID 'test-agent', got '%s'", agent.ID)
}
if agent.Name != "Test Agent" {
t.Errorf("expected name 'Test Agent', got '%s'", agent.Name)
}
})
t.Run("build agent with description", func(t *testing.T) {
agent, err := NewAgentBuilder().
WithID("test-agent").
WithName("Test Agent").
WithDescription("A test agent for testing").
WithLLMManager(mockLLM).
WithStateStore(mockStateStore).
Build()
if err != nil {
t.Fatalf("failed to build agent: %v", err)
}
if agent.Description != "A test agent for testing" {
t.Errorf("expected description to be set, got '%s'", agent.Description)
}
})
t.Run("build agent with model and provider", func(t *testing.T) {
agent, err := NewAgentBuilder().
WithID("test-agent").
WithName("Test Agent").
WithModel("gpt-4").
WithProvider("openai").
WithLLMManager(mockLLM).
WithStateStore(mockStateStore).
Build()
if err != nil {
t.Fatalf("failed to build agent: %v", err)
}
if agent.Model != "gpt-4" {
t.Errorf("expected model 'gpt-4', got '%s'", agent.Model)
}
if agent.Provider != "openai" {
t.Errorf("expected provider 'openai', got '%s'", agent.Provider)
}
})
t.Run("build agent with tools", func(t *testing.T) {
tool1 := Tool{Name: "tool1", Description: "Tool 1"}
tool2 := Tool{Name: "tool2", Description: "Tool 2"}
agent, err := NewAgentBuilder().
WithID("test-agent").
WithName("Test Agent").
WithLLMManager(mockLLM).
WithStateStore(mockStateStore).
WithTool(tool1).
WithTools(tool2).
Build()
if err != nil {
t.Fatalf("failed to build agent: %v", err)
}
if len(agent.tools) != 2 {
t.Errorf("expected 2 tools, got %d", len(agent.tools))
}
})
t.Run("build agent with system prompt", func(t *testing.T) {
agent, err := NewAgentBuilder().
WithID("test-agent").
WithName("Test Agent").
WithSystemPrompt("You are a helpful assistant").
WithLLMManager(mockLLM).
WithStateStore(mockStateStore).
Build()
if err != nil {
t.Fatalf("failed to build agent: %v", err)
}
if agent.systemPrompt != "You are a helpful assistant" {
t.Errorf("expected system prompt to be set")
}
})
t.Run("build agent with max iterations and temperature", func(t *testing.T) {
agent, err := NewAgentBuilder().
WithID("test-agent").
WithName("Test Agent").
WithMaxIterations(5).
WithTemperature(0.5).
WithLLMManager(mockLLM).
WithStateStore(mockStateStore).
Build()
if err != nil {
t.Fatalf("failed to build agent: %v", err)
}
if agent.maxIterations != 5 {
t.Errorf("expected maxIterations 5, got %d", agent.maxIterations)
}
if agent.temperature != 0.5 {
t.Errorf("expected temperature 0.5, got %f", agent.temperature)
}
})
t.Run("build agent with callbacks", func(t *testing.T) {
var startCalled, errorCalled, completeCalled bool
agent, err := NewAgentBuilder().
WithID("test-agent").
WithName("Test Agent").
WithLLMManager(mockLLM).
WithStateStore(mockStateStore).
OnStart(func(ctx context.Context) error {
startCalled = true
return nil
}).
OnError(func(err error) {
errorCalled = true
}).
OnComplete(func(state *AgentState) {
completeCalled = true
}).
Build()
if err != nil {
t.Fatalf("failed to build agent: %v", err)
}
// Verify callbacks are set
if agent.callbacks.OnStart == nil {
t.Error("OnStart callback not set")
}
if agent.callbacks.OnError == nil {
t.Error("OnError callback not set")
}
if agent.callbacks.OnComplete == nil {
t.Error("OnComplete callback not set")
}
// Suppress unused variable warnings
_ = startCalled
_ = errorCalled
_ = completeCalled
})
t.Run("build fails without ID", func(t *testing.T) {
_, err := NewAgentBuilder().
WithName("Test Agent").
WithLLMManager(mockLLM).
WithStateStore(mockStateStore).
Build()
if !errors.Is(err, ErrAgentIDRequired) {
t.Errorf("expected ErrAgentIDRequired, got %v", err)
}
})
t.Run("build fails without LLM manager", func(t *testing.T) {
_, err := NewAgentBuilder().
WithID("test-agent").
WithName("Test Agent").
WithStateStore(mockStateStore).
Build()
if err == nil {
t.Error("expected error for missing LLM manager, got nil")
}
})
t.Run("build fails without state store", func(t *testing.T) {
_, err := NewAgentBuilder().
WithID("test-agent").
WithName("Test Agent").
WithLLMManager(mockLLM).
Build()
if err == nil {
t.Error("expected error for missing state store, got nil")
}
})
t.Run("name defaults to ID", func(t *testing.T) {
agent, err := NewAgentBuilder().
WithID("test-agent").
WithLLMManager(mockLLM).
WithStateStore(mockStateStore).
Build()
if err != nil {
t.Fatalf("failed to build agent: %v", err)
}
if agent.Name != "test-agent" {
t.Errorf("expected name to default to ID 'test-agent', got '%s'", agent.Name)
}
})
t.Run("build with guardrails", func(t *testing.T) {
guardrails := NewGuardrailManager(mockLogger, mockMetrics, nil)
agent, err := NewAgentBuilder().
WithID("test-agent").
WithName("Test Agent").
WithLLMManager(mockLLM).
WithStateStore(mockStateStore).
WithGuardrails(guardrails).
Build()
if err != nil {
t.Fatalf("failed to build agent: %v", err)
}
if agent.guardrails == nil {
t.Error("expected guardrails to be set")
}
})
}
func TestAgentBuilderWithHandoff(t *testing.T) {
mockLLM := testhelpers.NewMockLLM()
mockLogger := testhelpers.NewMockLogger()
mockMetrics := testhelpers.NewMockMetrics()
mockStateStore := &mockBuilderStateStore{}
t.Run("build with handoff manager fails without manager", func(t *testing.T) {
_, err := NewAgentBuilder().
WithID("test-agent").
WithName("Test Agent").
WithLLMManager(mockLLM).
WithStateStore(mockStateStore).
BuildWithHandoff()
if err == nil {
t.Error("expected error for missing handoff manager, got nil")
}
})
t.Run("build with handoff manager succeeds", func(t *testing.T) {
registry := NewAgentRegistry(mockLogger, mockMetrics)
router := NewDefaultAgentRouter(registry, "", mockLLM, mockLogger, mockMetrics)
handoffManager := NewHandoffManager(registry, router, mockLogger, mockMetrics, nil)
agent, err := NewAgentBuilder().
WithID("test-agent").
WithName("Test Agent").
WithLLMManager(mockLLM).
WithStateStore(mockStateStore).
WithHandoffManager(handoffManager).
BuildWithHandoff()
if err != nil {
t.Fatalf("failed to build agent with handoff: %v", err)
}
if agent == nil {
t.Fatal("expected agent with handoff, got nil")
}
})
}
// --- WorkflowBuilder Tests ---
func TestWorkflowBuilder(t *testing.T) {
mockLogger := testhelpers.NewMockLogger()
mockMetrics := testhelpers.NewMockMetrics()
t.Run("build basic workflow", func(t *testing.T) {
workflow, err := NewWorkflowBuilder().
WithID("test-workflow").
WithName("Test Workflow").
WithLogger(mockLogger).
WithMetrics(mockMetrics).
AddNode(&WorkflowNode{
ID: "node1",
Type: NodeTypeTool,
Name: "Node 1",
}).
SetStartNode("node1").
Build()
if err != nil {
t.Fatalf("failed to build workflow: %v", err)
}
if workflow.ID != "test-workflow" {
t.Errorf("expected ID 'test-workflow', got '%s'", workflow.ID)
}
if workflow.Name != "Test Workflow" {
t.Errorf("expected name 'Test Workflow', got '%s'", workflow.Name)
}
})
t.Run("build workflow with description", func(t *testing.T) {
workflow, err := NewWorkflowBuilder().
WithID("test-workflow").
WithName("Test Workflow").
WithDescription("A test workflow").
AddNode(&WorkflowNode{ID: "node1", Type: NodeTypeTool, Name: "Node 1"}).
SetStartNode("node1").
Build()
if err != nil {
t.Fatalf("failed to build workflow: %v", err)
}
if workflow.Description != "A test workflow" {
t.Errorf("expected description 'A test workflow', got '%s'", workflow.Description)
}
})
t.Run("build workflow with version", func(t *testing.T) {
workflow, err := NewWorkflowBuilder().
WithID("test-workflow").
WithName("Test Workflow").
WithVersion("2.0.0").
AddNode(&WorkflowNode{ID: "node1", Type: NodeTypeTool, Name: "Node 1"}).
SetStartNode("node1").
Build()
if err != nil {
t.Fatalf("failed to build workflow: %v", err)
}
if workflow.Version != "2.0.0" {
t.Errorf("expected version '2.0.0', got '%s'", workflow.Version)
}
})
t.Run("build workflow with edges", func(t *testing.T) {
workflow, err := NewWorkflowBuilder().
WithID("test-workflow").
WithName("Test Workflow").
AddNode(&WorkflowNode{ID: "node1", Type: NodeTypeTool, Name: "Node 1"}).
AddNode(&WorkflowNode{ID: "node2", Type: NodeTypeTool, Name: "Node 2"}).
AddEdge("node1", "node2").
SetStartNode("node1").
Build()
if err != nil {
t.Fatalf("failed to build workflow: %v", err)
}
if len(workflow.Edges["node1"]) != 1 {
t.Errorf("expected 1 edge from node1, got %d", len(workflow.Edges["node1"]))
}
if workflow.Edges["node1"][0] != "node2" {
t.Errorf("expected edge to node2, got %s", workflow.Edges["node1"][0])
}
})
t.Run("build workflow with sequence", func(t *testing.T) {
workflow, err := NewWorkflowBuilder().
WithID("test-workflow").
WithName("Test Workflow").
AddNode(&WorkflowNode{ID: "node1", Type: NodeTypeTool, Name: "Node 1"}).
AddNode(&WorkflowNode{ID: "node2", Type: NodeTypeTool, Name: "Node 2"}).
AddNode(&WorkflowNode{ID: "node3", Type: NodeTypeTool, Name: "Node 3"}).
AddSequence("node1", "node2", "node3").
SetStartNode("node1").
Build()
if err != nil {
t.Fatalf("failed to build workflow: %v", err)
}
// Verify edges
if len(workflow.Edges["node1"]) != 1 || workflow.Edges["node1"][0] != "node2" {
t.Error("expected edge node1 -> node2")
}
if len(workflow.Edges["node2"]) != 1 || workflow.Edges["node2"][0] != "node3" {
t.Error("expected edge node2 -> node3")
}
})
t.Run("build fails without ID", func(t *testing.T) {
_, err := NewWorkflowBuilder().
WithName("Test Workflow").
AddNode(&WorkflowNode{ID: "node1", Type: NodeTypeTool, Name: "Node 1"}).
SetStartNode("node1").
Build()
if err == nil {
t.Error("expected error for missing ID, got nil")
}
})
t.Run("build fails without nodes", func(t *testing.T) {
_, err := NewWorkflowBuilder().
WithID("test-workflow").
WithName("Test Workflow").
Build()
if err == nil {
t.Error("expected error for missing nodes, got nil")
}
})
t.Run("build fails without start nodes", func(t *testing.T) {
_, err := NewWorkflowBuilder().
WithID("test-workflow").
WithName("Test Workflow").
AddNode(&WorkflowNode{ID: "node1", Type: NodeTypeTool, Name: "Node 1"}).
Build()
if !errors.Is(err, ErrWorkflowNoEntryPoint) {
t.Errorf("expected ErrWorkflowNoEntryPoint, got %v", err)
}
})
t.Run("name defaults to ID", func(t *testing.T) {
workflow, err := NewWorkflowBuilder().
WithID("test-workflow").
AddNode(&WorkflowNode{ID: "node1", Type: NodeTypeTool, Name: "Node 1"}).
SetStartNode("node1").
Build()
if err != nil {
t.Fatalf("failed to build workflow: %v", err)
}
if workflow.Name != "test-workflow" {
t.Errorf("expected name to default to ID, got '%s'", workflow.Name)
}
})
t.Run("build workflow with multiple start nodes", func(t *testing.T) {
workflow, err := NewWorkflowBuilder().
WithID("test-workflow").
WithName("Test Workflow").
AddNode(&WorkflowNode{ID: "node1", Type: NodeTypeTool, Name: "Node 1"}).
AddNode(&WorkflowNode{ID: "node2", Type: NodeTypeTool, Name: "Node 2"}).
SetStartNodes("node1", "node2").
Build()
if err != nil {
t.Fatalf("failed to build workflow: %v", err)
}
if len(workflow.StartNodes) != 2 {
t.Errorf("expected 2 start nodes, got %d", len(workflow.StartNodes))
}
})
}
func TestWorkflowBuilderNodeTypes(t *testing.T) {
mockLogger := testhelpers.NewMockLogger()
mockMetrics := testhelpers.NewMockMetrics()
mockStateStore := &mockBuilderStateStore{}
t.Run("add agent node", func(t *testing.T) {
mockLLM := testhelpers.NewMockLLM()
agent, _ := NewAgent("agent1", "Agent 1", mockLLM, mockStateStore, mockLogger, mockMetrics, nil)
workflow, err := NewWorkflowBuilder().
WithID("test-workflow").
AddAgentNode("node1", "Agent Node", agent).
SetStartNode("node1").
Build()
if err != nil {
t.Fatalf("failed to build workflow: %v", err)
}
node, _ := workflow.GetNode("node1")
if node.Type != NodeTypeAgent {
t.Errorf("expected NodeTypeAgent, got %s", node.Type)
}
if node.AgentID != "agent1" {
t.Errorf("expected AgentID 'agent1', got '%s'", node.AgentID)
}
})
t.Run("add tool node", func(t *testing.T) {
tool := Tool{Name: "test_tool", Description: "A test tool"}
workflow, err := NewWorkflowBuilder().
WithID("test-workflow").
AddToolNode("node1", "Tool Node", tool).
SetStartNode("node1").
Build()
if err != nil {
t.Fatalf("failed to build workflow: %v", err)
}
node, _ := workflow.GetNode("node1")
if node.Type != NodeTypeTool {
t.Errorf("expected NodeTypeTool, got %s", node.Type)
}
if node.ToolName != "test_tool" {
t.Errorf("expected ToolName 'test_tool', got '%s'", node.ToolName)
}
})
t.Run("add condition node", func(t *testing.T) {
workflow, err := NewWorkflowBuilder().
WithID("test-workflow").
AddConditionNode("node1", "Condition Node", "result > 10").
SetStartNode("node1").
Build()
if err != nil {
t.Fatalf("failed to build workflow: %v", err)
}
node, _ := workflow.GetNode("node1")
if node.Type != NodeTypeCondition {
t.Errorf("expected NodeTypeCondition, got %s", node.Type)
}
if node.Condition != "result > 10" {
t.Errorf("expected Condition 'result > 10', got '%s'", node.Condition)
}
})
t.Run("add transform node", func(t *testing.T) {
workflow, err := NewWorkflowBuilder().
WithID("test-workflow").
AddTransformNode("node1", "Transform Node", "value * 2").
SetStartNode("node1").
Build()
if err != nil {
t.Fatalf("failed to build workflow: %v", err)
}
node, _ := workflow.GetNode("node1")
if node.Type != NodeTypeTransform {
t.Errorf("expected NodeTypeTransform, got %s", node.Type)
}
if node.Transform != "value * 2" {
t.Errorf("expected Transform 'value * 2', got '%s'", node.Transform)
}
})
t.Run("add wait node", func(t *testing.T) {
workflow, err := NewWorkflowBuilder().
WithID("test-workflow").
AddWaitNode("node1", "Wait Node", 5*time.Second).
SetStartNode("node1").
Build()
if err != nil {
t.Fatalf("failed to build workflow: %v", err)
}
node, _ := workflow.GetNode("node1")
if node.Type != NodeTypeWait {
t.Errorf("expected NodeTypeWait, got %s", node.Type)
}
if node.Config["duration"] != 5*time.Second {
t.Errorf("expected duration 5s, got %v", node.Config["duration"])
}
})
}
func TestWorkflowBuilderWithRegistries(t *testing.T) {
mockLogger := testhelpers.NewMockLogger()
mockMetrics := testhelpers.NewMockMetrics()
t.Run("build with registries", func(t *testing.T) {
toolRegistry := NewToolRegistry(mockLogger, mockMetrics)
agentRegistry := NewAgentRegistry(mockLogger, mockMetrics)
result, err := NewWorkflowBuilder().
WithID("test-workflow").
WithName("Test Workflow").
WithToolRegistry(toolRegistry).
WithAgentRegistry(agentRegistry).
AddNode(&WorkflowNode{ID: "node1", Type: NodeTypeTool, Name: "Node 1"}).
SetStartNode("node1").
BuildWithRegistries()
if err != nil {
t.Fatalf("failed to build workflow with registries: %v", err)
}
if result.ToolRegistry == nil {
t.Error("expected tool registry to be set")
}
if result.AgentRegistry == nil {
t.Error("expected agent registry to be set")
}
})
}
// --- Mock StateStore for testing ---
type mockBuilderStateStore struct{}
func (m *mockBuilderStateStore) Save(ctx context.Context, state *AgentState) error {
return nil
}
func (m *mockBuilderStateStore) Load(ctx context.Context, agentID, sessionID string) (*AgentState, error) {
return nil, ErrStateNotFound
}
func (m *mockBuilderStateStore) Delete(ctx context.Context, agentID, sessionID string) error {
return nil
}
func (m *mockBuilderStateStore) List(ctx context.Context, agentID string) ([]string, error) {
return []string{}, nil
}