diff --git a/internal/cli/app.go b/internal/cli/app.go index fbed5d6..3e0bb80 100644 --- a/internal/cli/app.go +++ b/internal/cli/app.go @@ -626,8 +626,14 @@ func (a *App) cmdWindow(args string) { a.mu.Lock() nick := a.nick + connected := a.connected a.mu.Unlock() + connStatus := "disconnected" + if connected { + connStatus = "connected" + } + // Read the name before switching. A message from the server can // add a buffer at any time, but only at the end, so bufIndex // still names this buffer when SwitchBuffer runs. @@ -641,14 +647,14 @@ func (a *App) cmdWindow(args string) { a.mu.Unlock() a.ui.SetStatus( - nick, name, "connected", + nick, name, connStatus, ) } else { a.mu.Lock() a.target = "" a.mu.Unlock() - a.ui.SetStatus(nick, "", "connected") + a.ui.SetStatus(nick, "", connStatus) } } } diff --git a/internal/cli/app_internal_test.go b/internal/cli/app_internal_test.go index 11ddd9a..9d097fa 100644 --- a/internal/cli/app_internal_test.go +++ b/internal/cli/app_internal_test.go @@ -297,6 +297,69 @@ func TestAltDigit(t *testing.T) { } } +// TestWindowConnectionState checks that typing /window 0, or +// pressing Alt and 0, leaves the status bar showing [connected] +// only while the client is connected, and [disconnected] otherwise. +func TestWindowConnectionState(t *testing.T) { + t.Parallel() + + const line = "/window 0" + + typeLine := func(ui *UI) { + for _, r := range line { + ui.app.QueueEvent(tcell.NewEventKey(tcell.KeyRune, r, tcell.ModNone)) + } + + ui.app.QueueEvent(tcell.NewEventKey(tcell.KeyEnter, 0, tcell.ModNone)) + } + + pressAltZero := func(ui *UI) { + ui.app.QueueEvent(tcell.NewEventKey(tcell.KeyRune, '0', tcell.ModAlt)) + } + + tests := []struct { + name string + send func(ui *UI) + connected bool + want string + }{ + { + name: "typed line, not connected", send: typeLine, + connected: false, want: "[disconnected]", + }, + { + name: "Alt and 0, not connected", send: pressAltZero, + connected: false, want: "[disconnected]", + }, + { + name: "typed line, connected", send: typeLine, + connected: true, want: "[connected]", + }, + { + name: "Alt and 0, connected", send: pressAltZero, + connected: true, want: "[connected]", + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + t.Parallel() + + app := &App{ui: NewUI(), nick: testNick, connected: test.connected} + handled := handledLines(app) + runUI(t, app.ui) + + test.send(app.ui) + waitHandled(t, handled, line) + + status := statusBarText(app.ui) + if !strings.Contains(status, test.want) { + t.Fatalf("status bar is %q, want it to show %s", status, test.want) + } + }) + } +} + // TestEventLoopKeepsRunning sends Alt and 1, then a typed line and // Enter, to a running UI. It checks that both are handled, so the // event loop did not stop on either.