-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagentize_test.go
More file actions
668 lines (563 loc) · 17.8 KB
/
agentize_test.go
File metadata and controls
668 lines (563 loc) · 17.8 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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
package agentize
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestNew(t *testing.T) {
// Create temporary knowledge tree
tmpDir := createTestKnowledgeTree(t)
defer os.RemoveAll(tmpDir)
// Create Agentize instance
ag, err := New(tmpDir)
if err != nil {
t.Fatalf("Failed to create Agentize: %v", err)
}
// Check root node
root := ag.GetRoot()
if root == nil {
t.Fatal("Root node should not be nil")
}
if root.Path != "root" {
t.Errorf("Expected root path 'root', got '%s'", root.Path)
}
// Check all nodes are loaded
allNodes := ag.GetAllNodes()
if len(allNodes) < 1 {
t.Fatal("Should have at least root node loaded")
}
// Check node paths
paths := ag.GetNodePaths()
if len(paths) < 1 {
t.Fatal("Should have at least one path")
}
if paths[0] != "root" {
t.Errorf("First path should be 'root', got '%s'", paths[0])
}
}
func TestNewWithOptions(t *testing.T) {
tmpDir := createTestKnowledgeTree(t)
defer os.RemoveAll(tmpDir)
opts := &Options{}
ag, err := NewWithOptions(tmpDir, opts)
if err != nil {
t.Fatalf("Failed to create Agentize with options: %v", err)
}
if ag == nil {
t.Error("Expected Agentize instance, got nil")
}
}
func TestGetNode(t *testing.T) {
tmpDir := createTestKnowledgeTree(t)
defer os.RemoveAll(tmpDir)
ag, err := New(tmpDir)
if err != nil {
t.Fatalf("Failed to create Agentize: %v", err)
}
// Get root node
node, err := ag.GetNode("root")
if err != nil {
t.Fatalf("Failed to get root node: %v", err)
}
if node.Path != "root" {
t.Errorf("Expected path 'root', got '%s'", node.Path)
}
// Try to get non-existent node
_, err = ag.GetNode("nonexistent")
if err == nil {
t.Error("Expected error for nonexistent node")
}
}
func TestReload(t *testing.T) {
tmpDir := createTestKnowledgeTree(t)
defer os.RemoveAll(tmpDir)
ag, err := New(tmpDir)
if err != nil {
t.Fatalf("Failed to create Agentize: %v", err)
}
initialCount := len(ag.GetAllNodes())
// Reload
if err := ag.Reload(); err != nil {
t.Fatalf("Failed to reload: %v", err)
}
reloadedCount := len(ag.GetAllNodes())
if reloadedCount != initialCount {
t.Errorf("Node count should remain the same after reload, got %d vs %d", reloadedCount, initialCount)
}
}
func createTestKnowledgeTree(t *testing.T) string {
tmpDir, err := os.MkdirTemp("", "agentize-test-*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
// Create root node
rootPath := filepath.Join(tmpDir, "root")
os.MkdirAll(rootPath, 0755)
yamlContent := `id: "root"
title: "Test Root"
description: "Test description"
auth:
users:
- user_id: "test"
can_edit: true
can_read: true
can_access_next: true
can_see: true
visible_in_docs: true
visible_in_graph: true
routing:
mode: "sequential"
`
os.WriteFile(filepath.Join(rootPath, "node.yaml"), []byte(yamlContent), 0644)
os.WriteFile(filepath.Join(rootPath, "node.md"), []byte("# Root\n\nRoot content."), 0644)
os.WriteFile(filepath.Join(rootPath, "tools.json"), []byte(`{"tools": []}`), 0644)
// Create next node
nextPath := filepath.Join(rootPath, "next")
os.MkdirAll(nextPath, 0755)
os.WriteFile(filepath.Join(nextPath, "node.yaml"), []byte(`id: "next"`), 0644)
os.WriteFile(filepath.Join(nextPath, "node.md"), []byte("# Next\n\nNext content."), 0644)
return tmpDir
}
// createInfraAgentLikeKnowledgeTree creates a knowledge tree structure similar to InfraAgent
// but without any private/sensitive information - suitable for testing
func createInfraAgentLikeKnowledgeTree(t *testing.T) string {
tmpDir, err := os.MkdirTemp("", "agentize-infra-test-*")
if err != nil {
t.Fatalf("Failed to create temp dir: %v", err)
}
// ===== ROOT NODE =====
rootPath := filepath.Join(tmpDir, "root")
os.MkdirAll(rootPath, 0755)
rootYAML := `id: "root"
title: "Infrastructure Assistant"
description: "AI Infrastructure Operations Assistant for Kubernetes and Monitoring"
auth:
default:
visible_docs: true
visible_graph: true
`
os.WriteFile(filepath.Join(rootPath, "node.yaml"), []byte(rootYAML), 0644)
rootMD := `# Infrastructure Operations Assistant
## Role
* You are an **AI Infrastructure Operations Assistant** specialized in managing infrastructure.
* Your mission is to help DevOps engineers and SREs monitor, diagnose, and manage resources.
* Always prioritize **safety** and **best practices** when performing operations.
## Navigation
You can navigate to specialized areas:
- **kubernetes**: Kubernetes cluster operations and resources
- **monitoring**: Monitoring and observability tools
- **documents**: Documentation and reference materials
`
os.WriteFile(filepath.Join(rootPath, "node.md"), []byte(rootMD), 0644)
rootTools := `{
"tools": [
{
"name": "send_message",
"description": "Send message to the user",
"input_schema": {
"type": "object",
"properties": {
"statement": {
"type": "string",
"description": "The text message you want to send"
}
},
"required": ["statement"]
}
}
]
}`
os.WriteFile(filepath.Join(rootPath, "tools.json"), []byte(rootTools), 0644)
// ===== KUBERNETES NODE =====
kubernetesPath := filepath.Join(rootPath, "kubernetes")
os.MkdirAll(kubernetesPath, 0755)
kubernetesYAML := `id: "kubernetes"
title: "Kubernetes Operations"
description: "Kubernetes cluster management and operations"
`
os.WriteFile(filepath.Join(kubernetesPath, "node.yaml"), []byte(kubernetesYAML), 0644)
kubernetesMD := `# Kubernetes Operations
Kubernetes cluster management and operations.
## Available Operations
- Pod management
- Deployment operations
- Service management
- Namespace operations
`
os.WriteFile(filepath.Join(kubernetesPath, "node.md"), []byte(kubernetesMD), 0644)
kubernetesTools := `{
"tools": [
{
"name": "get_namespaces",
"description": "Get all namespaces in the cluster",
"input_schema": {
"type": "object",
"properties": {},
"required": []
}
},
{
"name": "get_namespace_pods",
"description": "Get all pods in a specific namespace",
"input_schema": {
"type": "object",
"properties": {
"namespace": {
"type": "string",
"description": "The namespace to get pods from"
}
},
"required": ["namespace"]
}
},
{
"name": "get_deployments",
"description": "Get all deployments in a specific namespace",
"input_schema": {
"type": "object",
"properties": {
"namespace": {
"type": "string",
"description": "The namespace to get deployments from"
}
},
"required": ["namespace"]
}
}
]
}`
os.WriteFile(filepath.Join(kubernetesPath, "tools.json"), []byte(kubernetesTools), 0644)
// ===== KUBERNETES/DEPLOYMENTS NODE =====
deploymentsPath := filepath.Join(kubernetesPath, "deployments")
os.MkdirAll(deploymentsPath, 0755)
deploymentsYAML := `id: "deployments"
title: "Kubernetes Deployments"
description: "Deployment management and operations"
`
os.WriteFile(filepath.Join(deploymentsPath, "node.yaml"), []byte(deploymentsYAML), 0644)
deploymentsMD := `# Kubernetes Deployments
Deployment management and operations.
## Operations
- List deployments
- Scale deployments
- Update deployment images
- Rollout restarts
`
os.WriteFile(filepath.Join(deploymentsPath, "node.md"), []byte(deploymentsMD), 0644)
// ===== KUBERNETES/PODS NODE =====
podsPath := filepath.Join(kubernetesPath, "pods")
os.MkdirAll(podsPath, 0755)
podsYAML := `id: "pods"
title: "Kubernetes Pods"
description: "Pod management and operations"
`
os.WriteFile(filepath.Join(podsPath, "node.yaml"), []byte(podsYAML), 0644)
podsMD := `# Kubernetes Pods
Pod management and operations.
## Operations
- Get pod status
- View pod logs
- Restart pods
- Delete pods
`
os.WriteFile(filepath.Join(podsPath, "node.md"), []byte(podsMD), 0644)
// ===== MONITORING NODE =====
monitoringPath := filepath.Join(rootPath, "monitoring")
os.MkdirAll(monitoringPath, 0755)
monitoringYAML := `id: "monitoring"
title: "Monitoring and Observability"
description: "Monitoring tools and metrics"
`
os.WriteFile(filepath.Join(monitoringPath, "node.yaml"), []byte(monitoringYAML), 0644)
monitoringMD := `# Monitoring and Observability
Monitoring tools and metrics collection.
## Available Tools
- Prometheus metrics
- Service level indicators (SLI)
- Health checks
`
os.WriteFile(filepath.Join(monitoringPath, "node.md"), []byte(monitoringMD), 0644)
monitoringTools := `{
"tools": [
{
"name": "get_metrics",
"description": "Get metrics from monitoring system",
"input_schema": {
"type": "object",
"properties": {
"query": {
"type": "string",
"description": "Prometheus query"
}
},
"required": ["query"]
}
},
{
"name": "get_sli",
"description": "Get service level indicators",
"input_schema": {
"type": "object",
"properties": {
"service": {
"type": "string",
"description": "Service name"
}
},
"required": ["service"]
}
}
]
}`
os.WriteFile(filepath.Join(monitoringPath, "tools.json"), []byte(monitoringTools), 0644)
// ===== MONITORING/PROMETHEUS NODE =====
prometheusPath := filepath.Join(monitoringPath, "prometheus")
os.MkdirAll(prometheusPath, 0755)
prometheusYAML := `id: "prometheus"
title: "Prometheus"
description: "Prometheus metrics and queries"
`
os.WriteFile(filepath.Join(prometheusPath, "node.yaml"), []byte(prometheusYAML), 0644)
prometheusMD := `# Prometheus
Prometheus metrics and querying.
## Features
- Query metrics
- View dashboards
- Alert management
`
os.WriteFile(filepath.Join(prometheusPath, "node.md"), []byte(prometheusMD), 0644)
// ===== DOCUMENTS NODE =====
documentsPath := filepath.Join(rootPath, "documents")
os.MkdirAll(documentsPath, 0755)
documentsYAML := `id: "documents"
title: "Documentation"
description: "Documentation and reference materials"
`
os.WriteFile(filepath.Join(documentsPath, "node.yaml"), []byte(documentsYAML), 0644)
documentsMD := `# Documentation
Documentation and reference materials.
## Sections
- API documentation
- Service documentation
- Architecture guides
`
os.WriteFile(filepath.Join(documentsPath, "node.md"), []byte(documentsMD), 0644)
// ===== DOCUMENTS/API-GATEWAY NODE =====
apiGatewayPath := filepath.Join(documentsPath, "api-gateway")
os.MkdirAll(apiGatewayPath, 0755)
apiGatewayYAML := `id: "api-gateway"
title: "API Gateway"
description: "API Gateway service documentation"
`
os.WriteFile(filepath.Join(apiGatewayPath, "node.yaml"), []byte(apiGatewayYAML), 0644)
apiGatewayMD := `# API Gateway
API Gateway service documentation.
## Overview
The API Gateway handles routing and authentication for all API requests.
`
os.WriteFile(filepath.Join(apiGatewayPath, "node.md"), []byte(apiGatewayMD), 0644)
return tmpDir
}
// TestScenario_TBD is a test scenario written in TDD approach
// This test will be developed incrementally based on requirements
//
// TDD Workflow:
// 1. Write failing test (RED)
// 2. Write minimal code to pass (GREEN)
// 3. Refactor (REFACTOR)
// 4. Repeat
func TestScenario_TBD(t *testing.T) {
// ============================================
// SETUP PHASE
// ============================================
// Create a knowledge tree similar to InfraAgent structure
knowledgePath := createInfraAgentLikeKnowledgeTree(t)
defer os.RemoveAll(knowledgePath)
// ============================================
// TEST SCENARIO STEPS
// ============================================
// Step 1: Load the knowledge tree
t.Run("Step 1: Load Knowledge Tree", func(t *testing.T) {
// Initialize Agentize instance with the knowledge tree
ag, err := New(knowledgePath)
if err != nil {
t.Fatalf("Failed to create Agentize instance: %v", err)
}
// Verify Agentize instance is created
if ag == nil {
t.Fatal("Agentize instance should not be nil")
}
// Verify root node exists
root := ag.GetRoot()
if root == nil {
t.Fatal("Root node should not be nil")
}
if root.Path != "root" {
t.Errorf("Expected root path 'root', got '%s'", root.Path)
}
if root.ID != "root" {
t.Errorf("Expected root ID 'root', got '%s'", root.ID)
}
if root.Title != "Infrastructure Assistant" {
t.Errorf("Expected root title 'Infrastructure Assistant', got '%s'", root.Title)
}
// Verify root node has content
if len(root.Content) == 0 {
t.Error("Root node should have markdown content")
}
// Verify all expected nodes are loaded
allNodes := ag.GetAllNodes()
expectedNodePaths := []string{
"root",
"root/kubernetes",
"root/kubernetes/deployments",
"root/kubernetes/pods",
"root/monitoring",
"root/monitoring/prometheus",
"root/documents",
"root/documents/api-gateway",
}
if len(allNodes) != len(expectedNodePaths) {
t.Errorf("Expected %d nodes, got %d. Nodes: %v", len(expectedNodePaths), len(allNodes), ag.GetNodePaths())
}
// Verify each expected node exists
for _, expectedPath := range expectedNodePaths {
node, err := ag.GetNode(expectedPath)
if err != nil {
t.Errorf("Failed to get node '%s': %v", expectedPath, err)
continue
}
if node.Path != expectedPath {
t.Errorf("Node path mismatch: expected '%s', got '%s'", expectedPath, node.Path)
}
}
// Verify node paths are returned in correct order
paths := ag.GetNodePaths()
if len(paths) != len(expectedNodePaths) {
t.Errorf("Expected %d paths, got %d", len(expectedNodePaths), len(paths))
}
// Verify root has tools loaded
if len(root.Tools) == 0 {
t.Error("Root node should have tools loaded from tools.json")
}
// Verify kubernetes node has tools
kubernetesNode, err := ag.GetNode("root/kubernetes")
if err != nil {
t.Fatalf("Failed to get kubernetes node: %v", err)
}
if len(kubernetesNode.Tools) == 0 {
t.Error("Kubernetes node should have tools loaded")
}
// Verify monitoring node has tools
monitoringNode, err := ag.GetNode("root/monitoring")
if err != nil {
t.Fatalf("Failed to get monitoring node: %v", err)
}
if len(monitoringNode.Tools) == 0 {
t.Error("Monitoring node should have tools loaded")
}
t.Logf("Successfully loaded knowledge tree with %d nodes", len(allNodes))
})
// Step 2: Verify node content and structure
t.Run("Step 2: Verify Node Content and Structure", func(t *testing.T) {
ag, err := New(knowledgePath)
if err != nil {
t.Fatalf("Failed to create Agentize instance: %v", err)
}
// Verify root node content
root, err := ag.GetNode("root")
if err != nil {
t.Fatalf("Failed to get root node: %v", err)
}
if !strings.Contains(root.Content, "Infrastructure Operations Assistant") {
t.Error("Root content should contain 'Infrastructure Operations Assistant'")
}
// Verify kubernetes node
kubernetesNode, err := ag.GetNode("root/kubernetes")
if err != nil {
t.Fatalf("Failed to get kubernetes node: %v", err)
}
if kubernetesNode.Title != "Kubernetes Operations" {
t.Errorf("Expected kubernetes title 'Kubernetes Operations', got '%s'", kubernetesNode.Title)
}
if len(kubernetesNode.Content) == 0 {
t.Error("Kubernetes node should have markdown content")
}
// Verify deployments node
deploymentsNode, err := ag.GetNode("root/kubernetes/deployments")
if err != nil {
t.Fatalf("Failed to get deployments node: %v", err)
}
if deploymentsNode.Title != "Kubernetes Deployments" {
t.Errorf("Expected deployments title 'Kubernetes Deployments', got '%s'", deploymentsNode.Title)
}
// Verify monitoring node
monitoringNode, err := ag.GetNode("root/monitoring")
if err != nil {
t.Fatalf("Failed to get monitoring node: %v", err)
}
if monitoringNode.Title != "Monitoring and Observability" {
t.Errorf("Expected monitoring title 'Monitoring and Observability', got '%s'", monitoringNode.Title)
}
t.Log("Node content and structure verified successfully")
})
// Step 3: Verify tools aggregation
t.Run("Step 3: Verify Tools Loading", func(t *testing.T) {
ag, err := New(knowledgePath)
if err != nil {
t.Fatalf("Failed to create Agentize instance: %v", err)
}
// Verify root tools
root, err := ag.GetNode("root")
if err != nil {
t.Fatalf("Failed to get root node: %v", err)
}
if len(root.Tools) < 1 {
t.Errorf("Root should have at least 1 tool, got %d", len(root.Tools))
}
// Check for expected root tools
toolNames := make(map[string]bool)
for _, tool := range root.Tools {
toolNames[tool.Name] = true
}
if !toolNames["send_message"] {
t.Error("Root should have 'send_message' tool")
}
// Verify kubernetes tools
kubernetesNode, err := ag.GetNode("root/kubernetes")
if err != nil {
t.Fatalf("Failed to get kubernetes node: %v", err)
}
if len(kubernetesNode.Tools) < 3 {
t.Errorf("Kubernetes should have at least 3 tools, got %d", len(kubernetesNode.Tools))
}
// Check for expected kubernetes tools
k8sToolNames := make(map[string]bool)
for _, tool := range kubernetesNode.Tools {
k8sToolNames[tool.Name] = true
}
if !k8sToolNames["get_namespaces"] {
t.Error("Kubernetes should have 'get_namespaces' tool")
}
if !k8sToolNames["get_namespace_pods"] {
t.Error("Kubernetes should have 'get_namespace_pods' tool")
}
if !k8sToolNames["get_deployments"] {
t.Error("Kubernetes should have 'get_deployments' tool")
}
// Verify monitoring tools
monitoringNode, err := ag.GetNode("root/monitoring")
if err != nil {
t.Fatalf("Failed to get monitoring node: %v", err)
}
if len(monitoringNode.Tools) < 2 {
t.Errorf("Monitoring should have at least 2 tools, got %d", len(monitoringNode.Tools))
}
t.Log("Tools loading verified successfully")
})
// ============================================
// CLEANUP PHASE (handled by defer)
// ============================================
t.Log("Knowledge tree loading test completed successfully")
}