-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpocket_test.go
More file actions
892 lines (799 loc) · 21.7 KB
/
Copy pathpocket_test.go
File metadata and controls
892 lines (799 loc) · 21.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
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
package pocket_test
import (
"context"
"errors"
"fmt"
"strings"
"testing"
"time"
"github.com/agentstation/pocket"
)
const (
testResult = "result"
)
func TestNodeLifecycle(t *testing.T) {
tests := []struct {
name string
input any
want any
wantErr bool
}{
{
name: "string transformation",
input: "hello",
want: "HELLO",
},
{
name: "number doubling",
input: 5,
want: 10,
},
{
name: "nil input",
input: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var node pocket.Node
switch tt.name {
case "string transformation":
node = pocket.NewNode[any, any]("transform",
pocket.Steps{
Prep: func(ctx context.Context, store pocket.StoreReader, input any) (any, error) {
s, ok := input.(string)
if !ok {
return nil, fmt.Errorf("expected string")
}
return s, nil
},
Exec: func(ctx context.Context, s any) (any, error) {
return strings.ToUpper(s.(string)), nil
},
Post: func(ctx context.Context, store pocket.StoreWriter, input, prep, result any) (any, string, error) {
return result, doneRoute, nil
},
},
)
case "number doubling":
node = pocket.NewNode[any, any]("double",
pocket.Steps{
Prep: func(ctx context.Context, store pocket.StoreReader, input any) (any, error) {
n, ok := input.(int)
if !ok {
return nil, fmt.Errorf("expected int")
}
return n, nil
},
Exec: func(ctx context.Context, n any) (any, error) {
return n.(int) * 2, nil
},
Post: func(ctx context.Context, store pocket.StoreWriter, input, prep, result any) (any, string, error) {
return result, doneRoute, nil
},
},
)
case "nil input":
node = pocket.NewNode[any, any]("nilcheck",
pocket.Steps{
Prep: func(ctx context.Context, store pocket.StoreReader, input any) (any, error) {
if input == nil {
return nil, errors.New("nil input")
}
return input, nil
},
},
)
}
store := pocket.NewStore()
graph := pocket.NewGraph(node, store)
got, err := graph.Run(context.Background(), tt.input)
if (err != nil) != tt.wantErr {
t.Errorf("Run() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !tt.wantErr && got != tt.want {
t.Errorf("Run() = %v, want %v", got, tt.want)
}
})
}
}
func TestNodeConnections(t *testing.T) {
// Create test nodes using lifecycle
start := pocket.NewNode[any, any]("start",
pocket.Steps{
Exec: func(ctx context.Context, input any) (any, error) {
return "processed", nil
},
Post: func(ctx context.Context, store pocket.StoreWriter, input, prep, result any) (any, string, error) {
return result, "next", nil
},
},
)
middle := pocket.NewNode[any, any]("middle",
pocket.Steps{
Exec: func(ctx context.Context, input any) (any, error) {
return input.(string) + "-middle", nil
},
Post: func(ctx context.Context, store pocket.StoreWriter, input, prep, result any) (any, string, error) {
return result, "default", nil
},
},
)
end := pocket.NewNode[any, any]("end",
pocket.Steps{
Exec: func(ctx context.Context, input any) (any, error) {
return input.(string) + "-end", nil
},
},
)
// Test connections
start.Connect("next", middle)
middle.Connect("default", end)
store := pocket.NewStore()
graph := pocket.NewGraph(start, store)
result, err := graph.Run(context.Background(), "input")
if err != nil {
t.Fatalf("Graph execution failed: %v", err)
}
expected := "processed-middle-end"
if result != expected {
t.Errorf("Graph result = %v, want %v", result, expected)
}
}
func TestGraphExecution(t *testing.T) {
tests := []struct {
name string
setupGraph func() (*pocket.Graph, pocket.Store)
input any
want any
wantErr error
}{
{
name: "simple graph",
setupGraph: func() (*pocket.Graph, pocket.Store) {
node := pocket.NewNode[any, any]("simple",
pocket.Steps{
Exec: func(ctx context.Context, input any) (any, error) {
return testResult, nil
},
},
)
store := pocket.NewStore()
return pocket.NewGraph(node, store), store
},
input: "test",
want: testResult,
},
{
name: "graph with routing",
setupGraph: func() (*pocket.Graph, pocket.Store) {
router := pocket.NewNode[any, any]("router",
pocket.Steps{
Exec: func(ctx context.Context, input any) (any, error) {
return input, nil
},
Post: func(ctx context.Context, store pocket.StoreWriter, input, prep, result any) (any, string, error) {
if result.(int) > 10 {
return result, "big", nil
}
return result, "small", nil
},
},
)
big := pocket.NewNode[any, any]("big",
pocket.Steps{
Exec: func(ctx context.Context, input any) (any, error) {
return "big number", nil
},
},
)
small := pocket.NewNode[any, any]("small",
pocket.Steps{
Exec: func(ctx context.Context, input any) (any, error) {
return "small number", nil
},
},
)
router.Connect("big", big)
router.Connect("small", small)
store := pocket.NewStore()
return pocket.NewGraph(router, store), store
},
input: 15,
want: "big number",
},
{
name: "graph with error",
setupGraph: func() (*pocket.Graph, pocket.Store) {
node := pocket.NewNode[any, any]("error",
pocket.Steps{
Exec: func(ctx context.Context, input any) (any, error) {
return nil, errors.New("process error")
},
},
)
store := pocket.NewStore()
return pocket.NewGraph(node, store), store
},
input: "test",
wantErr: errors.New("node error: exec failed: process error"),
},
{
name: "no start node",
setupGraph: func() (*pocket.Graph, pocket.Store) {
store := pocket.NewStore()
return pocket.NewGraph(nil, store), store
},
input: "test",
wantErr: pocket.ErrNoStartNode,
},
{
name: "prep step error",
setupGraph: func() (*pocket.Graph, pocket.Store) {
node := pocket.NewNode[any, any]("prep-error",
pocket.Steps{
Prep: func(ctx context.Context, store pocket.StoreReader, input any) (any, error) {
return nil, errors.New("prep failed")
},
},
)
store := pocket.NewStore()
return pocket.NewGraph(node, store), store
},
input: "test",
wantErr: errors.New("node prep-error: prep failed: prep failed"),
},
{
name: "post step error",
setupGraph: func() (*pocket.Graph, pocket.Store) {
node := pocket.NewNode[any, any]("post-error",
pocket.Steps{
Exec: func(ctx context.Context, input any) (any, error) {
return "result", nil
},
Post: func(ctx context.Context, store pocket.StoreWriter, input, prep, result any) (any, string, error) {
return nil, "", errors.New("post failed")
},
},
)
store := pocket.NewStore()
return pocket.NewGraph(node, store), store
},
input: "test",
wantErr: errors.New("node post-error: post failed: post failed"),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
graph, _ := tt.setupGraph()
got, err := graph.Run(context.Background(), tt.input)
if tt.wantErr != nil {
if err == nil {
t.Errorf("Run() error = nil, wantErr %v", tt.wantErr)
} else if tt.wantErr == pocket.ErrNoStartNode && err != pocket.ErrNoStartNode {
t.Errorf("Run() error = %v, wantErr %v", err, tt.wantErr)
}
return
}
if err != nil {
t.Errorf("Run() unexpected error = %v", err)
return
}
if got != tt.want {
t.Errorf("Run() = %v, want %v", got, tt.want)
}
})
}
}
func TestStore(t *testing.T) {
store := pocket.NewStore()
ctx := context.Background()
tests := []struct {
name string
op func()
check func(t *testing.T)
}{
{
name: "set and get",
op: func() {
_ = store.Set(ctx, "key1", "value1")
_ = store.Set(ctx, "key2", 42)
},
check: func(t *testing.T) {
val1, ok := store.Get(ctx, "key1")
if !ok || val1 != "value1" {
t.Errorf("Get(key1) = %v, %v; want value1, true", val1, ok)
}
val2, ok := store.Get(ctx, "key2")
if !ok || val2 != 42 {
t.Errorf("Get(key2) = %v, %v; want 42, true", val2, ok)
}
},
},
{
name: "get missing key",
op: func() {},
check: func(t *testing.T) {
_, ok := store.Get(ctx, "missing")
if ok {
t.Error("Get(missing) returned true, want false")
}
},
},
{
name: "delete",
op: func() {
_ = store.Set(ctx, "temp", "data")
_ = store.Delete(ctx, "temp")
},
check: func(t *testing.T) {
_, ok := store.Get(ctx, "temp")
if ok {
t.Error("Get(temp) after Delete returned true, want false")
}
},
},
{
name: "scoped store",
op: func() {
userStore := store.Scope("user")
userStore.Set(ctx, "123", "Alice")
},
check: func(t *testing.T) {
// Should be accessible via scoped store
userStore := store.Scope("user")
val, ok := userStore.Get(ctx, "123")
if !ok || val != "Alice" {
t.Errorf("Scoped Get(123) = %v, %v; want Alice, true", val, ok)
}
// Should be accessible via main store with prefix
val2, ok := store.Get(ctx, "user:123")
if !ok || val2 != "Alice" {
t.Errorf("Get(user:123) = %v, %v; want Alice, true", val2, ok)
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.op()
tt.check(t)
})
}
}
func TestWithRetry(t *testing.T) {
attempts := 0
ctx := context.Background()
node := pocket.NewNode[any, any]("retry",
pocket.Steps{
Exec: func(ctx context.Context, input any) (any, error) {
attempts++
if attempts < 3 {
return nil, errors.New("temporary error")
}
return successResult, nil
},
},
pocket.WithRetry(3, 10*time.Millisecond),
)
store := pocket.NewStore()
graph := pocket.NewGraph(node, store)
start := time.Now()
result, err := graph.Run(ctx, nil)
duration := time.Since(start)
if err != nil {
t.Fatalf("Expected success after retries, got error: %v", err)
}
if result != successResult {
t.Errorf("Expected 'success', got %v", result)
}
if attempts != 3 {
t.Errorf("Expected 3 attempts, got %d", attempts)
}
// Check that retry delays were applied (2 retries * 10ms)
if duration < 20*time.Millisecond {
t.Errorf("Expected duration >= 20ms, got %v", duration)
}
}
func TestWithTimeout(t *testing.T) {
node := pocket.NewNode[any, any]("slow",
pocket.Steps{
Exec: func(ctx context.Context, input any) (any, error) {
select {
case <-time.After(100 * time.Millisecond):
return "completed", nil
case <-ctx.Done():
return nil, ctx.Err()
}
},
},
pocket.WithTimeout(10*time.Millisecond),
)
store := pocket.NewStore()
graph := pocket.NewGraph(node, store)
_, err := graph.Run(context.Background(), nil)
if err == nil {
t.Error("Expected timeout error, got nil")
}
// The error will be wrapped, so check if it contains deadline exceeded
if !strings.Contains(err.Error(), "context deadline exceeded") &&
!strings.Contains(err.Error(), "failed after 1 attempts") {
t.Errorf("Expected timeout error, got %v", err)
}
}
func TestBuilder(t *testing.T) {
store := pocket.NewStore()
node1 := pocket.NewNode[any, any]("node1",
pocket.Steps{
Exec: func(ctx context.Context, input any) (any, error) {
return "from-node1", nil
},
Post: func(ctx context.Context, store pocket.StoreWriter, input, prep, result any) (any, string, error) {
return result, "default", nil
},
},
)
node2 := pocket.NewNode[any, any]("node2",
pocket.Steps{
Exec: func(ctx context.Context, input any) (any, error) {
return input.(string) + "-node2", nil
},
},
)
tests := []struct {
name string
build func() (*pocket.Graph, error)
wantErr bool
}{
{
name: "successful build",
build: func() (*pocket.Graph, error) {
return pocket.NewBuilder(store).
Add(node1).
Add(node2).
Connect("node1", "default", "node2").
Start("node1").
Build()
},
wantErr: false,
},
{
name: "no start node",
build: func() (*pocket.Graph, error) {
return pocket.NewBuilder(store).
Build()
},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
graph, err := tt.build()
if (err != nil) != tt.wantErr {
t.Errorf("Build() error = %v, wantErr %v", err, tt.wantErr)
}
if !tt.wantErr && graph == nil {
t.Error("Build() returned nil graph without error")
}
})
}
}
func TestTypedNode(t *testing.T) {
type User struct {
Name string
}
type Greeting struct {
Message string
}
// Create a typed node with lifecycle
node := pocket.NewNode[User, Greeting]("greet",
pocket.Steps{
Prep: func(ctx context.Context, store pocket.StoreReader, input any) (any, error) {
user, ok := input.(User)
if !ok {
return nil, fmt.Errorf("expected User type")
}
if user.Name == "" {
return nil, fmt.Errorf("name required")
}
return user, nil
},
Exec: func(ctx context.Context, prepData any) (any, error) {
user := prepData.(User)
return Greeting{Message: fmt.Sprintf("Hello, %s!", user.Name)}, nil
},
Post: func(ctx context.Context, store pocket.StoreWriter, input, prep, result any) (any, string, error) {
return result, doneRoute, nil
},
},
)
// Verify types are set
if node.InputType() == nil {
t.Error("NewNode did not set InputType")
}
if node.OutputType() == nil {
t.Error("NewNode did not set OutputType")
}
// Execute the node
store := pocket.NewStore()
graph := pocket.NewGraph(node, store)
result, err := graph.Run(context.Background(), User{Name: "Alice"})
if err != nil {
t.Fatalf("Run() error = %v", err)
}
greeting, ok := result.(Greeting)
if !ok {
t.Fatalf("Result is not Greeting type, got %T", result)
}
if greeting.Message != "Hello, Alice!" {
t.Errorf("Greeting message = %v, want %v", greeting.Message, "Hello, Alice!")
}
}
func TestValidateGraph(t *testing.T) {
type Input struct{ Value int }
type Output struct{ Result string }
type Different struct{ Data float64 }
tests := []struct {
name string
setupGraph func() pocket.Node
wantErr bool
errMsg string
}{
{
name: "valid graph with matching types",
setupGraph: func() pocket.Node {
// Create nodes with matching input/output types
node1 := pocket.NewNode[Input, Output]("node1",
pocket.Steps{
Exec: func(ctx context.Context, input any) (any, error) {
return Output{Result: "processed"}, nil
},
},
)
node2 := pocket.NewNode[Output, Different]("node2",
pocket.Steps{
Exec: func(ctx context.Context, input any) (any, error) {
return Different{Data: 3.14}, nil
},
},
)
node1.Connect("default", node2)
return node1
},
wantErr: false,
},
{
name: "invalid graph with type mismatch",
setupGraph: func() pocket.Node {
// Create nodes with mismatched types
node1 := pocket.NewNode[Input, Output]("node1",
pocket.Steps{
Exec: func(ctx context.Context, input any) (any, error) {
return Output{Result: "processed"}, nil
},
},
)
// node2 expects Different but node1 outputs Output
node2 := pocket.NewNode[Different, Input]("node2",
pocket.Steps{
Exec: func(ctx context.Context, input any) (any, error) {
return Input{Value: 42}, nil
},
},
)
node1.Connect("default", node2)
return node1
},
wantErr: true,
errMsg: "type mismatch",
},
{
name: "graph with untyped nodes (should pass)",
setupGraph: func() pocket.Node {
// Mix of typed and untyped nodes
typedNode := pocket.NewNode[Input, Output]("typed",
pocket.Steps{
Exec: func(ctx context.Context, input any) (any, error) {
return Output{Result: "processed"}, nil
},
},
)
// Untyped node - no validation performed
untypedNode := pocket.NewNode[any, any]("untyped",
pocket.Steps{
Exec: func(ctx context.Context, input any) (any, error) {
return testResult, nil
},
},
)
typedNode.Connect("default", untypedNode)
return typedNode
},
wantErr: false,
},
{
name: "cyclic graph validation",
setupGraph: func() pocket.Node {
node1 := pocket.NewNode[Input, Output]("node1",
pocket.Steps{
Exec: func(ctx context.Context, input any) (any, error) {
return Output{Result: "processed"}, nil
},
Post: func(ctx context.Context, store pocket.StoreWriter, input, prep, result any) (any, string, error) {
return result, defaultRoute, nil
},
},
)
node2 := pocket.NewNode[Output, Input]("node2",
pocket.Steps{
Exec: func(ctx context.Context, input any) (any, error) {
return Input{Value: 42}, nil
},
Post: func(ctx context.Context, store pocket.StoreWriter, input, prep, result any) (any, string, error) {
return result, defaultRoute, nil
},
},
)
// Create a cycle
node1.Connect("default", node2)
node2.Connect("default", node1)
return node1
},
wantErr: false, // Cycles are allowed, validation handles visited nodes
},
{
name: "interface type compatibility",
setupGraph: func() pocket.Node {
// With any,any nodes, type checking is not enforced
// This test validates that nodes can be connected regardless of type
node1 := pocket.NewNode[any, any]("producer",
pocket.Steps{
Exec: func(ctx context.Context, input any) (any, error) {
return struct{ Value string }{Value: "test"}, nil
},
},
)
node2 := pocket.NewNode[any, any]("consumer",
pocket.Steps{
Exec: func(ctx context.Context, input any) (any, error) {
return testResult, nil
},
},
)
node1.Connect("default", node2)
return node1
},
wantErr: false, // With any,any nodes, all types are compatible
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
startNode := tt.setupGraph()
err := pocket.ValidateGraph(startNode)
if (err != nil) != tt.wantErr {
t.Errorf("ValidateGraph() error = %v, wantErr %v", err, tt.wantErr)
}
if err != nil && tt.errMsg != "" && !strings.Contains(err.Error(), tt.errMsg) {
t.Errorf("ValidateGraph() error = %v, want error containing %v", err, tt.errMsg)
}
})
}
}
func TestLifecycleSteps(t *testing.T) {
ctx := context.Background()
store := pocket.NewStore()
// Track execution order
var executionOrder []string
node := pocket.NewNode[any, any]("lifecycle",
pocket.Steps{
Prep: func(ctx context.Context, store pocket.StoreReader, input any) (any, error) {
executionOrder = append(executionOrder, "prep")
return input.(string) + "-prepped", nil
},
Exec: func(ctx context.Context, prepResult any) (any, error) {
executionOrder = append(executionOrder, "exec")
return prepResult.(string) + "-executed", nil
},
Post: func(ctx context.Context, store pocket.StoreWriter, input, prepResult, execResult any) (any, string, error) {
executionOrder = append(executionOrder, "post")
// Verify all values are available
if input.(string) != "input" {
t.Errorf("Post got wrong input: %v", input)
}
if prepResult.(string) != "input-prepped" {
t.Errorf("Post got wrong prepResult: %v", prepResult)
}
if execResult.(string) != "input-prepped-executed" {
t.Errorf("Post got wrong execResult: %v", execResult)
}
return execResult.(string) + "-posted", doneRoute, nil
},
},
)
graph := pocket.NewGraph(node, store)
result, err := graph.Run(ctx, "input")
if err != nil {
t.Fatalf("Graph failed: %v", err)
}
// Check execution order
expectedOrder := []string{"prep", "exec", "post"}
if len(executionOrder) != len(expectedOrder) {
t.Fatalf("Wrong execution order length: %v", executionOrder)
}
for i, step := range expectedOrder {
if executionOrder[i] != step {
t.Errorf("Step %d: got %s, want %s", i, executionOrder[i], step)
}
}
// Check final result
if result != "input-prepped-executed-posted" {
t.Errorf("Wrong final result: %v", result)
}
}
func TestRetryPerStep(t *testing.T) {
ctx := context.Background()
store := pocket.NewStore()
prepAttempts := 0
execAttempts := 0
node := pocket.NewNode[any, any]("retry-steps",
pocket.Steps{
Prep: func(ctx context.Context, store pocket.StoreReader, input any) (any, error) {
prepAttempts++
if prepAttempts < 2 {
return nil, errors.New("prep retry")
}
return "prep-success", nil
},
Exec: func(ctx context.Context, prepResult any) (any, error) {
execAttempts++
if execAttempts < 2 {
return nil, errors.New("exec retry")
}
return "exec-success", nil
},
},
pocket.WithRetry(3, 10*time.Millisecond),
)
graph := pocket.NewGraph(node, store)
result, err := graph.Run(ctx, "input")
if err != nil {
t.Fatalf("Graph failed: %v", err)
}
if prepAttempts != 2 {
t.Errorf("Prep attempts = %d, want 2", prepAttempts)
}
if execAttempts != 2 {
t.Errorf("Exec attempts = %d, want 2", execAttempts)
}
if result != "exec-success" {
t.Errorf("Result = %v, want exec-success", result)
}
}
func TestErrorHandler(t *testing.T) {
ctx := context.Background()
store := pocket.NewStore()
var capturedError error
node := pocket.NewNode[any, any]("error-handler",
pocket.Steps{
Exec: func(ctx context.Context, input any) (any, error) {
return nil, errors.New("test error")
},
},
pocket.WithErrorHandler(func(err error) {
capturedError = err
}),
)
graph := pocket.NewGraph(node, store)
_, err := graph.Run(ctx, "input")
if err == nil {
t.Fatal("Expected error, got nil")
}
if capturedError == nil {
t.Fatal("Error handler not called")
}
if !strings.Contains(capturedError.Error(), "exec failed") {
t.Errorf("Wrong error captured: %v", capturedError)
}
}