1package coordinator
2
3import (
4 "context"
5 "testing"
6
7 "github.com/bnema/dumber/internal/application/usecase"
8 "github.com/bnema/dumber/internal/domain/entity"
9 "github.com/bnema/dumber/internal/ui/component"
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12)
13
14func TestWorkspaceCoordinator_ToggleSystemViewRightSplitsWithTargetURL(t *testing.T) {
15 ctx := context.Background()
16 ids := []string{"pane-2", "split-1"}
17 idx := 0
18 uc := usecase.NewManagePanesUseCase(func() string {
19 id := ids[idx]
20 idx++
21 return id
22 }, nil)
23
24 initialPane := entity.NewPane("pane-1")
25 initialPane.URI = "https://example.com"
26 ws := entity.NewWorkspace("ws-1", initialPane)
27
28 coord := NewWorkspaceCoordinator(ctx, WorkspaceCoordinatorConfig{
29 PanesUC: uc,
30 GetActiveWS: func() (*entity.Workspace, *component.WorkspaceView) {
31 return ws, nil
32 },
33 })
34
35 err := coord.ToggleSystemViewRight(ctx, "dumb://history")
36 require.NoError(t, err)
37
38 require.Equal(t, 2, ws.PaneCount())
39 active := ws.ActivePane()
40 require.NotNil(t, active)
41 require.NotNil(t, active.Pane)
42 assert.Equal(t, entity.PaneID("pane-2"), active.Pane.ID)
43 assert.Equal(t, "dumb://history", active.Pane.URI)
44}
45
46func TestWorkspaceCoordinator_ToggleSystemViewRightFocusesExistingPane(t *testing.T) {
47 ctx := context.Background()
48 left := testLeafNode("pane-1")
49 left.Pane.URI = "https://example.com"
50 right := testLeafNode("pane-2")
51 right.Pane.URI = "dumb://history"
52 root := testSplitNode("split-1", left, right)
53 ws := &entity.Workspace{ID: "ws-1", Root: root, ActivePaneID: left.Pane.ID}
54
55 coord := NewWorkspaceCoordinator(ctx, WorkspaceCoordinatorConfig{
56 PanesUC: usecase.NewManagePanesUseCase(func() string {
57 t.Fatal("PanesUC should not be invoked when focusing an existing systemview pane")
58 return "unused"
59 }, nil),
60 GetActiveWS: func() (*entity.Workspace, *component.WorkspaceView) {
61 return ws, nil
62 },
63 })
64
65 err := coord.ToggleSystemViewRight(ctx, "dumb://history")
66 require.NoError(t, err)
67
68 assert.Equal(t, entity.PaneID("pane-2"), ws.ActivePaneID)
69 assert.Equal(t, 2, ws.PaneCount())
70}
71
72func TestWorkspaceCoordinator_ToggleSystemViewRightClosesActivePane(t *testing.T) {
73 ctx := context.Background()
74 uc := usecase.NewManagePanesUseCase(func() string { return "unused" }, nil)
75 left := testLeafNode("pane-1")
76 left.Pane.URI = "https://example.com"
77 right := testLeafNode("pane-2")
78 right.Pane.URI = "dumb://history"
79 root := testSplitNode("split-1", left, right)
80 ws := &entity.Workspace{ID: "ws-1", Root: root, ActivePaneID: right.Pane.ID}
81
82 coord := NewWorkspaceCoordinator(ctx, WorkspaceCoordinatorConfig{
83 PanesUC: uc,
84 GetActiveWS: func() (*entity.Workspace, *component.WorkspaceView) {
85 return ws, nil
86 },
87 })
88
89 err := coord.ToggleSystemViewRight(ctx, "dumb://history")
90 require.NoError(t, err)
91
92 assert.Equal(t, 1, ws.PaneCount())
93 assert.Equal(t, entity.PaneID("pane-1"), ws.ActivePaneID)
94 active := ws.ActivePane()
95 require.NotNil(t, active)
96 assert.Equal(t, "https://example.com", active.Pane.URI)
97}
98
99func TestWorkspaceCoordinator_ToggleSystemViewRightUpdatesStackIndexForExistingPane(t *testing.T) {
100 ctx := context.Background()
101 first := testLeafNode("pane-1")
102 first.Pane.URI = "https://example.com"
103 second := testLeafNode("pane-2")
104 second.Pane.URI = "dumb://history"
105 stack := &entity.PaneNode{
106 ID: "stack-1",
107 IsStacked: true,
108 ActiveStackIndex: 0,
109 Children: []*entity.PaneNode{first, second},
110 }
111 first.Parent = stack
112 second.Parent = stack
113 ws := &entity.Workspace{ID: "ws-1", Root: stack, ActivePaneID: first.Pane.ID}
114
115 coord := NewWorkspaceCoordinator(ctx, WorkspaceCoordinatorConfig{
116 GetActiveWS: func() (*entity.Workspace, *component.WorkspaceView) {
117 return ws, nil
118 },
119 })
120
121 err := coord.ToggleSystemViewRight(ctx, "dumb://history")
122 require.NoError(t, err)
123
124 assert.Equal(t, entity.PaneID("pane-2"), ws.ActivePaneID)
125 assert.Equal(t, 1, stack.ActiveStackIndex)
126}