1package content
2
3import (
4 "context"
5 "errors"
6 "testing"
7
8 "github.com/stretchr/testify/assert"
9
10 "github.com/bnema/dumber/internal/application/port/mocks"
11 "github.com/bnema/dumber/internal/domain/entity"
12)
13
14func TestHandleURIChanged_BlocksExternalSchemeWithoutLaunching(t *testing.T) {
15 t.Parallel()
16
17 ctx := context.Background()
18 paneID := entity.PaneID("pane-1")
19 wv := mocks.NewMockWebView(t)
20 wv.EXPECT().Stop(ctx).Return(nil).Once()
21 wv.EXPECT().CanGoBack().Return(false).Once()
22
23 launchCount := 0
24 c := &Coordinator{
25 onLaunchExternalURL: func(uri string) {
26 launchCount++
27 },
28 }
29
30 c.handleURIChanged(ctx, paneID, wv, "vscode://file/etc/passwd")
31
32 assert.Equal(t, 0, launchCount)
33}
34
35func TestHandleURIChanged_BlocksExternalSchemeRedirectAndRestoresPreviousPage(t *testing.T) {
36 t.Parallel()
37
38 ctx := context.Background()
39 paneID := entity.PaneID("pane-1")
40 wv := mocks.NewMockWebView(t)
41 wv.EXPECT().Stop(ctx).Return(nil).Once()
42 wv.EXPECT().CanGoBack().Return(true).Once()
43 wv.EXPECT().GoBack(ctx).Return(nil).Once()
44
45 launchCount := 0
46 c := &Coordinator{
47 onLaunchExternalURL: func(uri string) {
48 launchCount++
49 },
50 }
51
52 c.handleURIChanged(ctx, paneID, wv, "spotify://track/123")
53
54 assert.Equal(t, 0, launchCount)
55}
56
57func TestHandleURIChanged_HTTPURIStillRecordsSPANavigation(t *testing.T) {
58 t.Parallel()
59
60 ctx := context.Background()
61 paneID := entity.PaneID("pane-1")
62 wv := mocks.NewMockWebView(t)
63 wv.EXPECT().IsLoading().Return(false).Once()
64
65 var recordedPane entity.PaneID
66 var recordedURI string
67 c := &Coordinator{
68 onPaneURIUpdated: func(paneID entity.PaneID, uri string) {
69 recordedPane = paneID
70 recordedURI = uri
71 },
72 }
73
74 c.handleURIChanged(ctx, paneID, wv, "https://example.com/app#state")
75
76 assert.Equal(t, paneID, recordedPane)
77 assert.Equal(t, "https://example.com/app#state", recordedURI)
78}
79
80func TestHandleURIChanged_StopErrorDoesNotPanic(t *testing.T) {
81 t.Parallel()
82
83 ctx := context.Background()
84 paneID := entity.PaneID("pane-1")
85 wv := mocks.NewMockWebView(t)
86 wv.EXPECT().Stop(ctx).Return(errors.New("stop failed")).Once()
87 wv.EXPECT().CanGoBack().Return(false).Once()
88
89 launchCount := 0
90 c := &Coordinator{
91 onLaunchExternalURL: func(uri string) {
92 launchCount++
93 },
94 }
95
96 c.handleURIChanged(ctx, paneID, wv, "vscode://file/etc/passwd")
97
98 assert.Equal(t, 0, launchCount)
99}
100
101func TestHandleURIChanged_GoBackErrorDoesNotPanic(t *testing.T) {
102 t.Parallel()
103
104 ctx := context.Background()
105 paneID := entity.PaneID("pane-1")
106 wv := mocks.NewMockWebView(t)
107 wv.EXPECT().Stop(ctx).Return(nil).Once()
108 wv.EXPECT().CanGoBack().Return(true).Once()
109 wv.EXPECT().GoBack(ctx).Return(errors.New("goback failed")).Once()
110
111 launchCount := 0
112 c := &Coordinator{
113 onLaunchExternalURL: func(uri string) {
114 launchCount++
115 },
116 }
117
118 c.handleURIChanged(ctx, paneID, wv, "spotify://track/123")
119
120 assert.Equal(t, 0, launchCount)
121}