|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/andyrewlee/amux/internal/data" |
| 8 | + "github.com/andyrewlee/amux/internal/git" |
| 9 | + "github.com/andyrewlee/amux/internal/messages" |
| 10 | + "github.com/andyrewlee/amux/internal/ui/dashboard" |
| 11 | +) |
| 12 | + |
| 13 | +type fileWatcherGitStatusStub struct { |
| 14 | + invalidateRoots []string |
| 15 | + refreshRoots []string |
| 16 | + refreshFastRoots []string |
| 17 | + cacheByRoot map[string]*git.StatusResult |
| 18 | +} |
| 19 | + |
| 20 | +func (s *fileWatcherGitStatusStub) Run(context.Context) error { return nil } |
| 21 | + |
| 22 | +func (s *fileWatcherGitStatusStub) GetCached(root string) *git.StatusResult { |
| 23 | + if s.cacheByRoot == nil { |
| 24 | + return nil |
| 25 | + } |
| 26 | + return s.cacheByRoot[root] |
| 27 | +} |
| 28 | + |
| 29 | +func (s *fileWatcherGitStatusStub) UpdateCache(root string, status *git.StatusResult) { |
| 30 | + if s.cacheByRoot == nil { |
| 31 | + s.cacheByRoot = make(map[string]*git.StatusResult) |
| 32 | + } |
| 33 | + s.cacheByRoot[root] = status |
| 34 | +} |
| 35 | + |
| 36 | +func (s *fileWatcherGitStatusStub) Invalidate(root string) { |
| 37 | + s.invalidateRoots = append(s.invalidateRoots, root) |
| 38 | +} |
| 39 | + |
| 40 | +func (s *fileWatcherGitStatusStub) Refresh(root string) (*git.StatusResult, error) { |
| 41 | + s.refreshRoots = append(s.refreshRoots, root) |
| 42 | + return &git.StatusResult{HasLineStats: true}, nil |
| 43 | +} |
| 44 | + |
| 45 | +func (s *fileWatcherGitStatusStub) RefreshFast(root string) (*git.StatusResult, error) { |
| 46 | + s.refreshFastRoots = append(s.refreshFastRoots, root) |
| 47 | + return &git.StatusResult{HasLineStats: false}, nil |
| 48 | +} |
| 49 | + |
| 50 | +func TestHandleFileWatcherEvent_ActiveWorkspaceRequestsFullStatus(t *testing.T) { |
| 51 | + active := &data.Workspace{ |
| 52 | + Repo: "/tmp/repo", |
| 53 | + Root: "/tmp/repo/ws-active", |
| 54 | + } |
| 55 | + stub := &fileWatcherGitStatusStub{} |
| 56 | + app := &App{ |
| 57 | + gitStatus: stub, |
| 58 | + dashboard: dashboard.New(), |
| 59 | + activeWorkspace: active, |
| 60 | + dirtyWorkspaces: make(map[string]bool), |
| 61 | + creatingWorkspaceIDs: make(map[string]bool), |
| 62 | + } |
| 63 | + |
| 64 | + cmds := app.handleFileWatcherEvent(messages.FileWatcherEvent{Root: active.Root}) |
| 65 | + if len(cmds) != 2 { |
| 66 | + t.Fatalf("expected 2 commands, got %d", len(cmds)) |
| 67 | + } |
| 68 | + if cmds[0] == nil { |
| 69 | + t.Fatal("expected status command") |
| 70 | + } |
| 71 | + msg := cmds[0]() |
| 72 | + result, ok := msg.(messages.GitStatusResult) |
| 73 | + if !ok { |
| 74 | + t.Fatalf("expected GitStatusResult, got %T", msg) |
| 75 | + } |
| 76 | + if result.Root != active.Root { |
| 77 | + t.Fatalf("expected root %q, got %q", active.Root, result.Root) |
| 78 | + } |
| 79 | + if result.Status == nil || !result.Status.HasLineStats { |
| 80 | + t.Fatalf("expected full status with line stats") |
| 81 | + } |
| 82 | + if len(stub.refreshRoots) != 1 { |
| 83 | + t.Fatalf("expected full refresh call, got %d", len(stub.refreshRoots)) |
| 84 | + } |
| 85 | + if len(stub.refreshFastRoots) != 0 { |
| 86 | + t.Fatalf("expected no fast refresh call, got %d", len(stub.refreshFastRoots)) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func TestHandleFileWatcherEvent_InactiveWorkspaceRequestsFastStatus(t *testing.T) { |
| 91 | + active := &data.Workspace{ |
| 92 | + Repo: "/tmp/repo", |
| 93 | + Root: "/tmp/repo/ws-active", |
| 94 | + } |
| 95 | + otherRoot := "/tmp/repo/ws-other" |
| 96 | + stub := &fileWatcherGitStatusStub{} |
| 97 | + app := &App{ |
| 98 | + gitStatus: stub, |
| 99 | + dashboard: dashboard.New(), |
| 100 | + activeWorkspace: active, |
| 101 | + dirtyWorkspaces: make(map[string]bool), |
| 102 | + creatingWorkspaceIDs: make(map[string]bool), |
| 103 | + } |
| 104 | + |
| 105 | + cmds := app.handleFileWatcherEvent(messages.FileWatcherEvent{Root: otherRoot}) |
| 106 | + if len(cmds) != 2 { |
| 107 | + t.Fatalf("expected 2 commands, got %d", len(cmds)) |
| 108 | + } |
| 109 | + if cmds[0] == nil { |
| 110 | + t.Fatal("expected status command") |
| 111 | + } |
| 112 | + msg := cmds[0]() |
| 113 | + result, ok := msg.(messages.GitStatusResult) |
| 114 | + if !ok { |
| 115 | + t.Fatalf("expected GitStatusResult, got %T", msg) |
| 116 | + } |
| 117 | + if result.Root != otherRoot { |
| 118 | + t.Fatalf("expected root %q, got %q", otherRoot, result.Root) |
| 119 | + } |
| 120 | + if result.Status == nil || result.Status.HasLineStats { |
| 121 | + t.Fatalf("expected fast status without line stats") |
| 122 | + } |
| 123 | + if len(stub.refreshRoots) != 0 { |
| 124 | + t.Fatalf("expected no full refresh call, got %d", len(stub.refreshRoots)) |
| 125 | + } |
| 126 | + if len(stub.refreshFastRoots) != 1 { |
| 127 | + t.Fatalf("expected one fast refresh call, got %d", len(stub.refreshFastRoots)) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func TestHandleGitStatusTick_ActiveWorkspaceCacheMissRequestsFullStatus(t *testing.T) { |
| 132 | + active := &data.Workspace{ |
| 133 | + Repo: "/tmp/repo", |
| 134 | + Root: "/tmp/repo/ws-active", |
| 135 | + } |
| 136 | + stub := &fileWatcherGitStatusStub{} |
| 137 | + app := &App{ |
| 138 | + gitStatus: stub, |
| 139 | + dashboard: dashboard.New(), |
| 140 | + activeWorkspace: active, |
| 141 | + dirtyWorkspaces: make(map[string]bool), |
| 142 | + creatingWorkspaceIDs: make(map[string]bool), |
| 143 | + } |
| 144 | + |
| 145 | + cmds := app.handleGitStatusTick() |
| 146 | + if len(cmds) != 2 { |
| 147 | + t.Fatalf("expected 2 commands, got %d", len(cmds)) |
| 148 | + } |
| 149 | + if cmds[0] == nil { |
| 150 | + t.Fatal("expected status command") |
| 151 | + } |
| 152 | + msg := cmds[0]() |
| 153 | + result, ok := msg.(messages.GitStatusResult) |
| 154 | + if !ok { |
| 155 | + t.Fatalf("expected GitStatusResult, got %T", msg) |
| 156 | + } |
| 157 | + if result.Root != active.Root { |
| 158 | + t.Fatalf("expected root %q, got %q", active.Root, result.Root) |
| 159 | + } |
| 160 | + if result.Status == nil || !result.Status.HasLineStats { |
| 161 | + t.Fatalf("expected full status with line stats on cache miss") |
| 162 | + } |
| 163 | + if len(stub.refreshRoots) != 1 { |
| 164 | + t.Fatalf("expected full refresh call, got %d", len(stub.refreshRoots)) |
| 165 | + } |
| 166 | + if len(stub.refreshFastRoots) != 0 { |
| 167 | + t.Fatalf("expected no fast refresh call, got %d", len(stub.refreshFastRoots)) |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +func TestHandleGitStatusTick_ActiveWorkspaceCachedStatusSkipsRefresh(t *testing.T) { |
| 172 | + active := &data.Workspace{ |
| 173 | + Repo: "/tmp/repo", |
| 174 | + Root: "/tmp/repo/ws-active", |
| 175 | + } |
| 176 | + stub := &fileWatcherGitStatusStub{ |
| 177 | + cacheByRoot: map[string]*git.StatusResult{ |
| 178 | + active.Root: {HasLineStats: true}, |
| 179 | + }, |
| 180 | + } |
| 181 | + app := &App{ |
| 182 | + gitStatus: stub, |
| 183 | + dashboard: dashboard.New(), |
| 184 | + activeWorkspace: active, |
| 185 | + dirtyWorkspaces: make(map[string]bool), |
| 186 | + creatingWorkspaceIDs: make(map[string]bool), |
| 187 | + } |
| 188 | + |
| 189 | + cmds := app.handleGitStatusTick() |
| 190 | + if len(cmds) != 2 { |
| 191 | + t.Fatalf("expected 2 commands, got %d", len(cmds)) |
| 192 | + } |
| 193 | + if cmds[0] == nil { |
| 194 | + t.Fatal("expected status command") |
| 195 | + } |
| 196 | + msg := cmds[0]() |
| 197 | + result, ok := msg.(messages.GitStatusResult) |
| 198 | + if !ok { |
| 199 | + t.Fatalf("expected GitStatusResult, got %T", msg) |
| 200 | + } |
| 201 | + if result.Status == nil || !result.Status.HasLineStats { |
| 202 | + t.Fatalf("expected cached status with line stats") |
| 203 | + } |
| 204 | + if len(stub.refreshRoots) != 0 { |
| 205 | + t.Fatalf("expected no full refresh call when cached, got %d", len(stub.refreshRoots)) |
| 206 | + } |
| 207 | + if len(stub.refreshFastRoots) != 0 { |
| 208 | + t.Fatalf("expected no fast refresh call when cached, got %d", len(stub.refreshFastRoots)) |
| 209 | + } |
| 210 | +} |
0 commit comments