-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinteractive_test.go
More file actions
95 lines (77 loc) · 2.08 KB
/
interactive_test.go
File metadata and controls
95 lines (77 loc) · 2.08 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
package lifecycle
import (
"context"
"strings"
"testing"
"time"
"github.com/aretw0/lifecycle/pkg/events"
)
func TestInteractiveRouter_Passthrough(t *testing.T) {
// Create a pipe to simulate stdin
r := strings.NewReader("Hello\nquit\n")
received := make(chan string, 1)
// Handler to capture passthrough
handler := events.HandlerFunc(func(ctx context.Context, e events.Event) error {
if le, ok := e.(events.LineEvent); ok {
received <- le.Line
}
return nil
})
router := NewInteractiveRouter(
WithDefaultHandler(handler),
WithDefaultMappings(), // Enable standard commands
WithInputOptions(events.WithInputReader(r)),
)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Start router
done := make(chan error)
go func() {
done <- router.Start(ctx)
}()
select {
case line := <-received:
if line != "Hello" {
t.Errorf("Expected 'Hello', got '%s'", line)
}
case <-time.After(2 * time.Second):
t.Error("Timeout waiting for input")
}
cancel()
<-done
}
func TestInteractiveRouter_UnknownCommand(t *testing.T) {
// Create a pipe to simulate stdin
r := strings.NewReader("weird_command\nquit\n")
received := make(chan events.UnknownCommandEvent, 1)
// Intercept the unknown command event to verify it was emitted
unknownHandler := events.HandlerFunc(func(ctx context.Context, e events.Event) error {
if ue, ok := e.(events.UnknownCommandEvent); ok {
received <- ue
}
return nil
})
router := NewInteractiveRouter(
WithDefaultMappings(), // Enable standard commands
WithInputOptions(events.WithInputReader(r)),
)
// Register our verification handler for the topic
router.Handle("input/unknown", unknownHandler)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Start router
done := make(chan error)
go func() {
done <- router.Start(ctx)
}()
select {
case ue := <-received:
if ue.Command != "weird_command" {
t.Errorf("Expected 'weird_command', got '%s'", ue.Command)
}
case <-time.After(2 * time.Second):
t.Error("Timeout waiting for unknown command event")
}
cancel()
<-done
}