floating_session_registry_test.go RawHistoryFolder
1package ui
2
3import (
4 "maps"
5 "testing"
6
7 "github.com/stretchr/testify/assert"
8 "github.com/stretchr/testify/require"
9
10 "github.com/bnema/dumber/internal/domain/entity"
11)
12
13func TestFloatingSessionRegistry(t *testing.T) {
14 tab1, tab2 := entity.TabID("tab-1"), entity.TabID("tab-2")
15 gmail := newFloatingPaneSession(tab1, "profile:gmail")
16 blank := newFloatingPaneSession(tab1, "profile:blank")
17 other := newFloatingPaneSession(tab2, "profile:gmail")
18 blank.pane.Show()
19
20 r := floatingSessionRegistry{
21 floatingSessionMapKey(tab1, "profile:gmail"): gmail,
22 floatingSessionMapKey(tab1, "profile:blank"): blank,
23 floatingSessionMapKey(tab2, "profile:gmail"): other,
24 floatingSessionMapKey(tab2, "profile:nil"): nil,
25 }
26
27 t.Run("forTab skips other tabs and nil sessions", func(t *testing.T) {
28 assert.Len(t, r.forTabSnapshot(tab1), 2)
29 assert.Len(t, r.forTabSnapshot(tab2), 1)
30 assert.Len(t, maps.Collect(r.all()), 3, "all skips nil sessions")
31 })
32
33 t.Run("forTab does not allocate", func(t *testing.T) {
34 allocs := testing.AllocsPerRun(100, func() {
35 for range r.forTab(tab1) {
36 }
37 })
38 assert.Zero(t, allocs)
39 })
40
41 t.Run("byPaneID maps the pane to its owning key", func(t *testing.T) {
42 key, session, ok := r.byPaneID(other.paneID)
43 require.True(t, ok)
44 assert.Same(t, other, session)
45 assert.Equal(t, tab2, key.tabID)
46
47 _, _, ok = r.byPaneID("")
48 assert.False(t, ok)
49 })
50
51 t.Run("visibleForTab returns only the shown session", func(t *testing.T) {
52 _, session, ok := r.visibleForTab(tab1)
53 require.True(t, ok)
54 assert.Same(t, blank, session)
55
56 key, _, ok := r.visibleForTab(tab2)
57 assert.False(t, ok)
58 assert.Equal(t, tab2, key.tabID, "miss still reports the tab")
59 })
60
61 t.Run("tabIDs is distinct and sorted", func(t *testing.T) {
62 assert.Equal(t, []entity.TabID{tab1, tab2}, r.tabIDs())
63 })
64}