-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexecutor_test.go
More file actions
1309 lines (1058 loc) · 31.4 KB
/
executor_test.go
File metadata and controls
1309 lines (1058 loc) · 31.4 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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package toolfs
import (
"encoding/json"
"errors"
"fmt"
"strings"
"testing"
"time"
)
// ExampleSkill is a simple example skill implementation.
type ExampleSkill struct {
name string
version string
config map[string]interface{}
}
func (p *ExampleSkill) Name() string {
return p.name
}
func (p *ExampleSkill) Version() string {
return p.version
}
func (p *ExampleSkill) Init(config map[string]interface{}) error {
p.config = config
// Validate required configuration
if timeout, ok := config["timeout"].(float64); ok {
if timeout <= 0 {
return errors.New("timeout must be positive")
}
}
return nil
}
func (p *ExampleSkill) Execute(input []byte) ([]byte, error) {
var request SkillRequest
if err := json.Unmarshal(input, &request); err != nil {
return nil, err
}
response := SkillResponse{
Success: true,
Result: map[string]interface{}{
"code": p.name,
"version": p.version,
"operation": request.Operation,
"path": request.Path,
},
}
return json.Marshal(response)
}
// FileProcessorSkill is an example skill that processes files from ToolFS.
type FileProcessorSkill struct {
context *SkillContext
config map[string]interface{}
}
func (p *FileProcessorSkill) Name() string {
return "file-processor"
}
func (p *FileProcessorSkill) Version() string {
return "1.0.0"
}
func (p *FileProcessorSkill) Init(config map[string]interface{}) error {
p.config = config
// Validate allowed paths if provided
if paths, ok := config["allowed_paths"].([]interface{}); ok {
for _, path := range paths {
if _, ok := path.(string); !ok {
return errors.New("allowed_paths must contain strings")
}
}
}
return nil
}
func (p *FileProcessorSkill) Execute(input []byte) ([]byte, error) {
var request SkillRequest
if err := json.Unmarshal(input, &request); err != nil {
return nil, err
}
switch request.Operation {
case "read_and_process":
if request.Path == "" {
return nil, errors.New("path is required for read_and_process")
}
// Check allowed paths if configured
if allowedPaths, ok := p.config["allowed_paths"].([]interface{}); ok {
allowed := false
for _, ap := range allowedPaths {
if apStr, ok := ap.(string); ok && apStr == request.Path {
allowed = true
break
}
}
if !allowed {
return nil, errors.New("path not in allowed_paths")
}
}
// Read file using skill context
if p.context == nil {
return nil, errors.New("skill context not available")
}
data, err := p.context.ReadFile(request.Path)
if err != nil {
return nil, err
}
// Process the data (simple example: convert to uppercase)
processed := string(data)
if len(processed) > 100 {
processed = processed[:100] + "... (truncated)"
}
response := SkillResponse{
Success: true,
Result: map[string]interface{}{
"original_size": len(data),
"processed": processed,
"path": request.Path,
},
}
return json.Marshal(response)
case "list_files":
if request.Path == "" {
return nil, errors.New("path is required for list_files")
}
if p.context == nil {
return nil, errors.New("skill context not available")
}
entries, err := p.context.ListDir(request.Path)
if err != nil {
return nil, err
}
response := SkillResponse{
Success: true,
Result: map[string]interface{}{
"path": request.Path,
"entries": entries,
"count": len(entries),
},
}
return json.Marshal(response)
default:
return nil, fmt.Errorf("unknown operation: %s", request.Operation)
}
}
// ErrorSkill is an example skill that returns errors for testing.
type ErrorSkill struct {
initError error
executeError error
}
func (p *ErrorSkill) Name() string {
return "error-skill"
}
func (p *ErrorSkill) Version() string {
return "1.0.0"
}
func (p *ErrorSkill) Init(config map[string]interface{}) error {
return p.initError
}
func (p *ErrorSkill) Execute(input []byte) ([]byte, error) {
if p.executeError != nil {
return nil, p.executeError
}
response := SkillResponse{
Success: false,
Error: "skill execution error",
}
return json.Marshal(response)
}
func TestSkillExecutorInterface(t *testing.T) {
// Test that ExampleSkill implements SkillExecutor interface
var _ SkillExecutor = (*ExampleSkill)(nil)
skill := &ExampleSkill{
name: "test-skill",
version: "1.0.0",
}
// Test Name()
if skill.Name() != "test-skill" {
t.Errorf("Expected name 'test-skill', got '%s'", skill.Name())
}
// Test Version()
if skill.Version() != "1.0.0" {
t.Errorf("Expected version '1.0.0', got '%s'", skill.Version())
}
// Test Init()
err := skill.Init(map[string]interface{}{
"timeout": 30.0,
})
if err != nil {
t.Fatalf("Init failed: %v", err)
}
// Test Init() with invalid config
err = skill.Init(map[string]interface{}{
"timeout": -1.0,
})
if err == nil {
t.Error("Expected error for negative timeout")
}
// Test Execute()
request := SkillRequest{
Operation: "test",
Path: "/toolfs/data/test.txt",
}
input, _ := json.Marshal(request)
output, err := skill.Execute(input)
if err != nil {
t.Fatalf("Execute failed: %v", err)
}
var response SkillResponse
if err := json.Unmarshal(output, &response); err != nil {
t.Fatalf("Failed to unmarshal response: %v", err)
}
if !response.Success {
t.Error("Expected successful response")
}
result, ok := response.Result.(map[string]interface{})
if !ok {
t.Fatal("Result should be a map")
}
if result["operation"] != "test" {
t.Errorf("Expected operation 'test', got '%v'", result["operation"])
}
}
func TestSkillExecutorRegistry(t *testing.T) {
registry := NewSkillExecutorRegistry()
// Test Register()
skill1 := &ExampleSkill{name: "skill1", version: "1.0.0"}
err := registry.Register(skill1, nil)
if err != nil {
t.Fatalf("Register failed: %v", err)
}
// Test duplicate registration
err = registry.Register(skill1, nil)
if err == nil {
t.Error("Expected error for duplicate registration")
}
// Test Get()
retrieved, err := registry.Get("skill1")
if err != nil {
t.Fatalf("Get failed: %v", err)
}
if retrieved.Name() != "skill1" {
t.Errorf("Expected skill name 'skill1', got '%s'", retrieved.Name())
}
// Test Get() non-existent
_, err = registry.Get("nonexistent")
if err == nil {
t.Error("Expected error for non-existent skill")
}
// Test List()
skill2 := &ExampleSkill{name: "skill2", version: "1.0.0"}
registry.Register(skill2, nil)
list := registry.List()
if len(list) != 2 {
t.Errorf("Expected 2 skills, got %d", len(list))
}
// Test Unregister()
registry.Unregister("skill1")
list = registry.List()
if len(list) != 1 {
t.Errorf("Expected 1 skill after unregister, got %d", len(list))
}
if list[0] != "skill2" {
t.Errorf("Expected remaining skill 'skill2', got '%s'", list[0])
}
}
func TestSkillContext(t *testing.T) {
fs := NewToolFS("/toolfs")
tmpDir, cleanup := setupTestDir(t)
defer cleanup()
err := fs.MountLocal("/data", tmpDir, false)
if err != nil {
t.Fatalf("MountLocal failed: %v", err)
}
session, err := fs.NewSession("skill-session", []string{"/toolfs/data"})
if err != nil {
t.Fatalf("NewSession failed: %v", err)
}
ctx := NewSkillContext(fs, session)
// Test ReadFile()
data, err := ctx.ReadFile("/toolfs/data/test.txt")
if err != nil {
t.Fatalf("ReadFile failed: %v", err)
}
if string(data) != "Hello, ToolFS!" {
t.Errorf("Expected 'Hello, ToolFS!', got '%s'", string(data))
}
// Test WriteFile()
err = ctx.WriteFile("/toolfs/data/skill-test.txt", []byte("Skill test"))
if err != nil {
t.Fatalf("WriteFile failed: %v", err)
}
// Verify write
data, err = ctx.ReadFile("/toolfs/data/skill-test.txt")
if err != nil {
t.Fatalf("ReadFile after write failed: %v", err)
}
if string(data) != "Skill test" {
t.Errorf("Expected 'Skill test', got '%s'", string(data))
}
// Test ListDir()
entries, err := ctx.ListDir("/toolfs/data")
if err != nil {
t.Fatalf("ListDir failed: %v", err)
}
if len(entries) == 0 {
t.Error("Expected entries in directory")
}
// Test Stat()
info, err := ctx.Stat("/toolfs/data/test.txt")
if err != nil {
t.Fatalf("Stat failed: %v", err)
}
if info.IsDir {
t.Error("Expected file to not be a directory")
}
if info.Size == 0 {
t.Error("Expected non-zero file size")
}
}
func TestFileProcessorSkill(t *testing.T) {
fs := NewToolFS("/toolfs")
tmpDir, cleanup := setupTestDir(t)
defer cleanup()
err := fs.MountLocal("/data", tmpDir, false)
if err != nil {
t.Fatalf("MountLocal failed: %v", err)
}
session, err := fs.NewSession("processor-session", []string{"/toolfs/data"})
if err != nil {
t.Fatalf("NewSession failed: %v", err)
}
ctx := NewSkillContext(fs, session)
skill := &FileProcessorSkill{context: ctx}
// Test Init()
err = skill.Init(map[string]interface{}{
"allowed_paths": []interface{}{"/toolfs/data"},
})
if err != nil {
t.Fatalf("Init failed: %v", err)
}
// Test Execute - read_and_process (without allowed_paths restriction)
skillNoRestriction := &FileProcessorSkill{context: ctx}
skillNoRestriction.Init(nil) // No allowed_paths restriction
request := SkillRequest{
Operation: "read_and_process",
Path: "/toolfs/data/test.txt",
}
input, _ := json.Marshal(request)
output, err := skillNoRestriction.Execute(input)
if err != nil {
t.Fatalf("Execute failed: %v", err)
}
var response SkillResponse
if err := json.Unmarshal(output, &response); err != nil {
t.Fatalf("Failed to unmarshal response: %v", err)
}
if !response.Success {
t.Errorf("Expected success, got error: %s", response.Error)
}
result := response.Result.(map[string]interface{})
if result["path"] != "/toolfs/data/test.txt" {
t.Errorf("Expected path '/toolfs/data/test.txt', got '%v'", result["path"])
}
// Test Execute - list_files (with allowed_paths)
request = SkillRequest{
Operation: "list_files",
Path: "/toolfs/data",
}
input, _ = json.Marshal(request)
output, err = skill.Execute(input)
if err != nil {
t.Fatalf("Execute list_files failed: %v", err)
}
if err := json.Unmarshal(output, &response); err != nil {
t.Fatalf("Failed to unmarshal response: %v", err)
}
if !response.Success {
t.Errorf("Expected success, got error: %s", response.Error)
}
result = response.Result.(map[string]interface{})
entries, ok := result["entries"].([]interface{})
if !ok {
t.Fatal("Expected entries to be an array")
}
if len(entries) == 0 {
t.Error("Expected non-empty entries list")
}
// Test Execute - invalid operation
request = SkillRequest{
Operation: "invalid_operation",
}
input, _ = json.Marshal(request)
_, err = skill.Execute(input)
if err == nil {
t.Error("Expected error for invalid operation")
}
// Test Execute - path not in allowed_paths
// Create a new skill instance for this test to avoid state issues
skill2 := &FileProcessorSkill{context: ctx}
err = skill2.Init(map[string]interface{}{
"allowed_paths": []interface{}{"/toolfs/data/subdir"},
})
if err != nil {
t.Fatalf("Init failed: %v", err)
}
request = SkillRequest{
Operation: "read_and_process",
Path: "/toolfs/data/test.txt",
}
input, _ = json.Marshal(request)
_, err = skill2.Execute(input)
if err == nil {
t.Error("Expected error for path not in allowed_paths")
}
if err != nil && !strings.Contains(err.Error(), "allowed_paths") {
t.Errorf("Expected error about allowed_paths, got: %v", err)
}
}
func TestSkillExecutorRegistryExecuteSkill(t *testing.T) {
registry := NewSkillExecutorRegistry()
fs := NewToolFS("/toolfs")
tmpDir, cleanup := setupTestDir(t)
defer cleanup()
fs.MountLocal("/data", tmpDir, false)
session, _ := fs.NewSession("test-session", []string{"/toolfs/data"})
ctx := NewSkillContext(fs, session)
skill := &ExampleSkill{name: "test-skill", version: "1.0.0"}
skill.Init(nil)
registry.Register(skill, ctx)
// Test ExecuteSkill()
request := &SkillRequest{
Operation: "test",
Path: "/toolfs/data/test.txt",
}
response, err := registry.ExecuteSkill("test-skill", request)
if err != nil {
t.Fatalf("ExecuteSkill failed: %v", err)
}
if !response.Success {
t.Errorf("Expected success, got error: %s", response.Error)
}
result := response.Result.(map[string]interface{})
if result["code"] != "test-skill" {
t.Errorf("Expected skill 'test-skill', got '%v'", result["code"])
}
// Test ExecuteSkill - non-existent skill
_, err = registry.ExecuteSkill("nonexistent", request)
if err == nil {
t.Error("Expected error for non-existent skill")
}
}
func TestSkillExecutorRegistryInitSkill(t *testing.T) {
registry := NewSkillExecutorRegistry()
skill := &ExampleSkill{name: "test-skill", version: "1.0.0"}
registry.Register(skill, nil)
// Test InitSkill()
err := registry.InitSkill("test-skill", map[string]interface{}{
"timeout": 30.0,
})
if err != nil {
t.Fatalf("InitSkill failed: %v", err)
}
// Test InitSkill - non-existent skill
err = registry.InitSkill("nonexistent", nil)
if err == nil {
t.Error("Expected error for non-existent skill")
}
// Test InitSkill - invalid config
err = registry.InitSkill("test-skill", map[string]interface{}{
"timeout": -1.0,
})
if err == nil {
t.Error("Expected error for invalid config")
}
}
func TestSkillContextWithNilToolFS(t *testing.T) {
ctx := &SkillContext{fs: nil, session: nil}
// Test ReadFile with nil ToolFS
_, err := ctx.ReadFile("/test/path")
if err == nil {
t.Error("Expected error for nil ToolFS")
}
// Test WriteFile with nil ToolFS
err = ctx.WriteFile("/test/path", []byte("test"))
if err == nil {
t.Error("Expected error for nil ToolFS")
}
// Test ListDir with nil ToolFS
_, err = ctx.ListDir("/test/path")
if err == nil {
t.Error("Expected error for nil ToolFS")
}
// Test Stat with nil ToolFS
_, err = ctx.Stat("/test/path")
if err == nil {
t.Error("Expected error for nil ToolFS")
}
}
func TestSkillRequestResponseJSON(t *testing.T) {
// Test SkillRequest JSON encoding/decoding
request := SkillRequest{
Operation: "read_file",
Path: "/toolfs/data/test.txt",
Data: map[string]interface{}{
"encoding": "utf8",
},
Options: map[string]interface{}{
"timeout": 30,
},
}
data, err := json.Marshal(request)
if err != nil {
t.Fatalf("Failed to marshal request: %v", err)
}
var decoded SkillRequest
if err := json.Unmarshal(data, &decoded); err != nil {
t.Fatalf("Failed to unmarshal request: %v", err)
}
if decoded.Operation != "read_file" {
t.Errorf("Operation mismatch")
}
if decoded.Path != "/toolfs/data/test.txt" {
t.Errorf("Path mismatch")
}
// Test SkillResponse JSON encoding/decoding
response := SkillResponse{
Success: true,
Result: map[string]interface{}{
"content": "test content",
"size": 12,
},
Metadata: map[string]interface{}{
"timestamp": "2023-01-01T00:00:00Z",
},
}
data, err = json.Marshal(response)
if err != nil {
t.Fatalf("Failed to marshal response: %v", err)
}
var decodedResp SkillResponse
if err := json.Unmarshal(data, &decodedResp); err != nil {
t.Fatalf("Failed to unmarshal response: %v", err)
}
if !decodedResp.Success {
t.Error("Success mismatch")
}
result := decodedResp.Result.(map[string]interface{})
if result["size"].(float64) != 12 {
t.Errorf("Result size mismatch")
}
}
func TestFileProcessorSkillWithoutContext(t *testing.T) {
skill := &FileProcessorSkill{context: nil}
skill.Init(nil)
request := SkillRequest{
Operation: "read_and_process",
Path: "/toolfs/data/test.txt",
}
input, _ := json.Marshal(request)
_, err := skill.Execute(input)
if err == nil {
t.Error("Expected error when skill context is nil")
}
}
func TestErrorSkill(t *testing.T) {
// Test Init error
skill := &ErrorSkill{
initError: errors.New("initialization failed"),
}
err := skill.Init(nil)
if err == nil {
t.Error("Expected init error")
}
// Test Execute error
skill = &ErrorSkill{
executeError: errors.New("execution failed"),
}
skill.Init(nil)
input := []byte(`{"operation":"test"}`)
_, err = skill.Execute(input)
if err == nil {
t.Error("Expected execute error")
}
// Test Execute with no error (returns error response)
skill = &ErrorSkill{}
skill.Init(nil)
output, err := skill.Execute(input)
if err != nil {
t.Fatalf("Execute should not return error: %v", err)
}
var response SkillResponse
if err := json.Unmarshal(output, &response); err != nil {
t.Fatalf("Failed to unmarshal: %v", err)
}
if response.Success {
t.Error("Expected unsuccessful response")
}
}
// MockWASMLoader is a mock implementation for testing WASM loading.
type MockWASMLoader struct {
loadFunc func(string) ([]byte, error)
instantiateFunc func([]byte, *SkillContext) (SkillExecutor, error)
}
func (m *MockWASMLoader) LoadWASM(path string) ([]byte, error) {
if m.loadFunc != nil {
return m.loadFunc(path)
}
return []byte("mock wasm bytes"), nil
}
func (m *MockWASMLoader) Instantiate(wasmBytes []byte, context *SkillContext) (SkillExecutor, error) {
if m.instantiateFunc != nil {
return m.instantiateFunc(wasmBytes, context)
}
// Return a mock skill
return &ExampleSkill{name: "wasm-skill", version: "1.0.0"}, nil
}
func TestSkillExecutorManagerInjectSkill(t *testing.T) {
pm := NewSkillExecutorManager()
fs := NewToolFS("/toolfs")
session, _ := fs.NewSession("test-session", []string{})
ctx := NewSkillContext(fs, session)
skill := &ExampleSkill{name: "test-skill", version: "1.0.0"}
// Test InjectSkill
err := pm.InjectSkill(skill, ctx, nil)
if err != nil {
t.Fatalf("InjectSkill failed: %v", err)
}
// Verify skill is loaded
skills := pm.ListSkills()
if len(skills) != 1 {
t.Errorf("Expected 1 skill, got %d", len(skills))
}
if skills[0] != "test-skill" {
t.Errorf("Expected skill 'test-skill', got '%s'", skills[0])
}
// Test duplicate injection
err = pm.InjectSkill(skill, ctx, nil)
if err == nil {
t.Error("Expected error for duplicate skill injection")
}
// Test nil skill
err = pm.InjectSkill(nil, ctx, nil)
if err == nil {
t.Error("Expected error for nil skill")
}
// Test skill with empty name
emptySkill := &ExampleSkill{name: "", version: "1.0.0"}
err = pm.InjectSkill(emptySkill, ctx, nil)
if err == nil {
t.Error("Expected error for skill with empty name")
}
}
func TestSkillExecutorManagerListExecutors(t *testing.T) {
pm := NewSkillExecutorManager()
fs := NewToolFS("/toolfs")
session, _ := fs.NewSession("test-session", []string{})
ctx := NewSkillContext(fs, session)
// Initially empty
skills := pm.ListSkills()
if len(skills) != 0 {
t.Errorf("Expected 0 skills initially, got %d", len(skills))
}
// Inject multiple skills
pm.InjectSkill(&ExampleSkill{name: "skill1", version: "1.0.0"}, ctx, nil)
pm.InjectSkill(&ExampleSkill{name: "skill2", version: "1.0.0"}, ctx, nil)
pm.InjectSkill(&ExampleSkill{name: "skill3", version: "1.0.0"}, ctx, nil)
skills = pm.ListSkills()
if len(skills) != 3 {
t.Errorf("Expected 3 skills, got %d", len(skills))
}
// Verify all skills are listed
skillMap := make(map[string]bool)
for _, name := range skills {
skillMap[name] = true
}
if !skillMap["skill1"] || !skillMap["skill2"] || !skillMap["skill3"] {
t.Error("Expected all skills to be listed")
}
}
func TestSkillExecutorManagerExecuteSkill(t *testing.T) {
pm := NewSkillExecutorManager()
fs := NewToolFS("/toolfs")
session, _ := fs.NewSession("test-session", []string{})
ctx := NewSkillContext(fs, session)
skill := &ExampleSkill{name: "test-skill", version: "1.0.0"}
pm.InjectSkill(skill, ctx, nil)
// Test ExecuteSkill
request := SkillRequest{
Operation: "test",
Path: "/toolfs/data/test.txt",
}
input, _ := json.Marshal(request)
output, err := pm.ExecuteSkill("test-skill", input)
if err != nil {
t.Fatalf("ExecuteSkill failed: %v", err)
}
var response SkillResponse
if err := json.Unmarshal(output, &response); err != nil {
t.Fatalf("Failed to unmarshal response: %v", err)
}
if !response.Success {
t.Errorf("Expected success, got error: %s", response.Error)
}
// Test ExecuteSkill - non-existent skill
_, err = pm.ExecuteSkill("nonexistent", input)
if err == nil {
t.Error("Expected error for non-existent skill")
}
}
func TestSkillExecutorManagerExecuteSkillTimeout(t *testing.T) {
pm := NewSkillExecutorManager()
pm.SetTimeout(100 * time.Millisecond) // Short timeout
fs := NewToolFS("/toolfs")
session, _ := fs.NewSession("test-session", []string{})
ctx := NewSkillContext(fs, session)
// Create a slow skill
slowSkill := &SlowSkill{delay: 200 * time.Millisecond}
pm.InjectSkill(slowSkill, ctx, nil)
request := SkillRequest{Operation: "test"}
input, _ := json.Marshal(request)
// Should timeout
_, err := pm.ExecuteSkill("slow-skill", input)
if err == nil {
t.Error("Expected timeout error")
}
if !strings.Contains(err.Error(), "timeout") {
t.Errorf("Expected timeout error, got: %v", err)
}
}
// SlowSkill is a skill that delays execution for testing timeouts.
type SlowSkill struct {
delay time.Duration
}
func (p *SlowSkill) Name() string { return "slow-skill" }
func (p *SlowSkill) Version() string { return "1.0.0" }
func (p *SlowSkill) Init(config map[string]interface{}) error {
if delay, ok := config["delay"].(float64); ok {
p.delay = time.Duration(delay) * time.Millisecond
}
return nil
}
func (p *SlowSkill) Execute(input []byte) ([]byte, error) {
time.Sleep(p.delay)
response := SkillResponse{Success: true, Result: "delayed result"}
return json.Marshal(response)
}
func TestSkillExecutorManagerLoadSkill(t *testing.T) {
pm := NewSkillExecutorManager()
fs := NewToolFS("/toolfs")
session, _ := fs.NewSession("test-session", []string{})
ctx := NewSkillContext(fs, session)
// Test loading without WASM loader (should fail for .wasm files)
err := pm.LoadSkill("test.wasm", ctx, nil)
if err == nil {
t.Error("Expected error when loading WASM without loader")
}
if !strings.Contains(err.Error(), "WASM loader not configured") {
t.Errorf("Expected WASM loader error, got: %v", err)
}
// Test loading with mock WASM loader
mockLoader := &MockWASMLoader{}
pm.SetWASMLoader(mockLoader)
err = pm.LoadSkill("test.wasm", ctx, nil)
if err != nil {
t.Fatalf("LoadSkill with WASM loader failed: %v", err)
}
// Verify skill is loaded
skills := pm.ListSkills()
if len(skills) != 1 {
t.Errorf("Expected 1 skill after loading, got %d", len(skills))
}
// Test loading duplicate skill
err = pm.LoadSkill("test.wasm", ctx, nil)
if err == nil {
t.Error("Expected error for duplicate skill loading")
}
// Test loading native skill (not yet implemented)
err = pm.LoadSkill("test.so", ctx, nil)
if err == nil {
t.Error("Expected error for native skill (not implemented)")
}
if !strings.Contains(err.Error(), "not yet implemented") {
t.Errorf("Expected 'not yet implemented' error, got: %v", err)
}
}
func TestSkillExecutorManagerLoadSkillWithConfig(t *testing.T) {
pm := NewSkillExecutorManager()
fs := NewToolFS("/toolfs")
session, _ := fs.NewSession("test-session", []string{})
ctx := NewSkillContext(fs, session)
mockLoader := &MockWASMLoader{}
pm.SetWASMLoader(mockLoader)
config := map[string]interface{}{
"timeout": 60.0,
"memory_limit": 1024 * 1024,
}
err := pm.LoadSkill("config-test.wasm", ctx, config)
if err != nil {
t.Fatalf("LoadSkill with config failed: %v", err)
}
// Verify config was passed
info, err := pm.GetSkillInfo("wasm-skill")
if err != nil {
t.Fatalf("GetSkillInfo failed: %v", err)
}
if info.Config == nil {
t.Error("Expected config to be set")
}
}
func TestSkillExecutorManagerGetSkillInfo(t *testing.T) {
pm := NewSkillExecutorManager()
fs := NewToolFS("/toolfs")
session, _ := fs.NewSession("test-session", []string{})
ctx := NewSkillContext(fs, session)
skill := &ExampleSkill{name: "info-skill", version: "1.0.0"}
pm.InjectSkill(skill, ctx, map[string]interface{}{"test": "value"})
// Test GetSkillInfo