1package cef
2
3import (
4 "context"
5 "errors"
6 "testing"
7
8 "github.com/bnema/dumber/internal/application/dto"
9 "github.com/bnema/dumber/internal/application/port"
10 "github.com/stretchr/testify/require"
11)
12
13type stubAccentHandler struct{}
14
15func (stubAccentHandler) OnKeyPressed(context.Context, rune, bool) bool { return true }
16func (stubAccentHandler) OnKeyReleased(context.Context, rune) {}
17
18func TestEngineRegisterHandlers_UsesInjectedRegistrar(t *testing.T) {
19 called := false
20 eng := &Engine{
21 messageRouter: NewMessageRouter(context.Background()),
22 registerHandlers: func(context.Context, port.WebUIHandlerRouter, port.HandlerDependencies) error {
23 called = true
24 return nil
25 },
26 }
27 require.NoError(t, eng.RegisterHandlers(context.Background(), port.HandlerDependencies{}))
28 require.True(t, called)
29}
30
31func TestEngineRegisterAccentHandlers_UsesInjectedRegistrar(t *testing.T) {
32 called := false
33 eng := &Engine{
34 messageRouter: NewMessageRouter(context.Background()),
35 registerAccentHandlers: func(context.Context, port.WebUIHandlerRouter, port.AccentKeyHandler) error {
36 called = true
37 return nil
38 },
39 }
40 require.NoError(t, eng.RegisterAccentHandlers(context.Background(), stubAccentHandler{}))
41 require.True(t, called)
42}
43
44func TestEngineRegisterHandlers_PropagatesRegistrarError(t *testing.T) {
45 want := errors.New("boom")
46 eng := &Engine{
47 messageRouter: NewMessageRouter(context.Background()),
48 registerHandlers: func(context.Context, port.WebUIHandlerRouter, port.HandlerDependencies) error {
49 return want
50 },
51 }
52 err := eng.RegisterHandlers(context.Background(), port.HandlerDependencies{})
53 require.ErrorIs(t, err, want)
54}
55
56func TestEngineRegisterHandlers_StoresClipboardWiring(t *testing.T) {
57 orchestrator := &recordingClipboardTextOrchestrator{}
58 onCopied := func(int) {}
59 eng := &Engine{
60 messageRouter: NewMessageRouter(context.Background()),
61 registerHandlers: func(context.Context, port.WebUIHandlerRouter, port.HandlerDependencies) error {
62 return nil
63 },
64 }
65
66 require.NoError(t, eng.RegisterHandlers(context.Background(), port.HandlerDependencies{
67 ClipboardTextOrchestrator: orchestrator,
68 OnClipboardCopied: onCopied,
69 }))
70 require.Same(t, orchestrator, eng.clipboardTextOrchestrator)
71 require.NotNil(t, eng.onClipboardCopied)
72}
73
74func TestEngineClipboardSelectionUpdate_ForwardsViewID(t *testing.T) {
75 orchestrator := &recordingClipboardTextOrchestrator{}
76 eng := &Engine{clipboardTextOrchestrator: orchestrator}
77
78 eng.handleClipboardSelectionUpdate(99, "selected")
79
80 require.Equal(t, dto.SelectionClipboardInput{Text: "selected", SourceEngine: dto.ClipboardSourceCEF, ViewID: 99}, orchestrator.selection)
81}
82
83func TestEngineExplicitClipboardCopy_ForwardsViewID(t *testing.T) {
84 orchestrator := &recordingClipboardTextOrchestrator{}
85 eng := &Engine{clipboardTextOrchestrator: orchestrator}
86
87 eng.handleExplicitClipboardBridgeText(77, "copy", "copied")
88
89 require.Equal(t, dto.ExplicitClipboardInput{Text: "copied", SourceEngine: dto.ClipboardSourceCEF, ViewID: 77, Action: "copy", NativeHandled: false}, orchestrator.explicit)
90}
91
92type recordingClipboardTextOrchestrator struct {
93 selection dto.SelectionClipboardInput
94 explicit dto.ExplicitClipboardInput
95}
96
97func (r *recordingClipboardTextOrchestrator) HandleSelectionUpdate(_ context.Context, input dto.SelectionClipboardInput) error {
98 r.selection = input
99 return nil
100}
101
102func (r *recordingClipboardTextOrchestrator) HandleExplicitCopy(_ context.Context, input dto.ExplicitClipboardInput) error {
103 r.explicit = input
104 return nil
105}