1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
|
package dashboard
import (
"fmt"
"regexp"
"strconv"
"strings"
"testing"
"ior/internal/statsengine"
common "ior/internal/tui/common"
"ior/internal/tui/eventstream"
"ior/internal/tui/messages"
tea "github.com/charmbracelet/bubbletea"
)
type fakeSnapshotSource struct {
snapshots int
snap *statsengine.Snapshot
}
func (f *fakeSnapshotSource) Snapshot() *statsengine.Snapshot {
f.snapshots++
return f.snap
}
func TestKeySwitchingChangesActiveTab(t *testing.T) {
m := NewModelWithConfig(nil, nil, 250, common.DefaultKeyMap())
next, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'2'}})
model := next.(Model)
if model.activeTab != TabSyscalls {
t.Fatalf("expected syscalls tab, got %v", model.activeTab)
}
next, _ = model.Update(tea.KeyMsg{Type: tea.KeyTab})
model = next.(Model)
if model.activeTab != TabFiles {
t.Fatalf("expected next tab to be files, got %v", model.activeTab)
}
next, _ = model.Update(tea.KeyMsg{Type: tea.KeyShiftTab})
model = next.(Model)
if model.activeTab != TabSyscalls {
t.Fatalf("expected previous tab to be syscalls, got %v", model.activeTab)
}
next, _ = model.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'7'}})
model = next.(Model)
if model.activeTab != TabStream {
t.Fatalf("expected stream tab on key 7, got %v", model.activeTab)
}
next, _ = model.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'6'}})
model = next.(Model)
if model.activeTab != TabStream {
t.Fatalf("expected stream tab on key 6, got %v", model.activeTab)
}
}
func TestArrowAndViKeysDoNotCycleTabs(t *testing.T) {
m := NewModelWithConfig(nil, nil, 250, common.DefaultKeyMap())
next, _ := m.Update(tea.KeyMsg{Type: tea.KeyRight})
model := next.(Model)
if model.activeTab != TabOverview {
t.Fatalf("expected right arrow not to change tabs, got %v", model.activeTab)
}
next, _ = model.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'l'}})
model = next.(Model)
if model.activeTab != TabOverview {
t.Fatalf("expected l not to change tabs, got %v", model.activeTab)
}
next, _ = model.Update(tea.KeyMsg{Type: tea.KeyLeft})
model = next.(Model)
if model.activeTab != TabOverview {
t.Fatalf("expected left arrow not to change tabs, got %v", model.activeTab)
}
next, _ = model.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'h'}})
model = next.(Model)
if model.activeTab != TabOverview {
t.Fatalf("expected h not to change tabs, got %v", model.activeTab)
}
}
func TestSyscallsTabScrollsWithJK(t *testing.T) {
m := NewModelWithConfig(nil, nil, 250, common.DefaultKeyMap())
m.activeTab = TabSyscalls
snap := statsengine.NewSnapshot(nil, nil, nil, []statsengine.SyscallSnapshot{{Name: "read", Count: 1}, {Name: "write", Count: 1}}, nil, nil, statsengine.HistogramSnapshot{}, statsengine.HistogramSnapshot{})
m.latest = &snap
next, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'j'}})
model := next.(Model)
if model.syscallsOffset != 1 {
t.Fatalf("expected offset 1 after j, got %d", model.syscallsOffset)
}
next, _ = model.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'k'}})
model = next.(Model)
if model.syscallsOffset != 0 {
t.Fatalf("expected offset 0 after k, got %d", model.syscallsOffset)
}
}
func TestProcessesTabScrollsWithJK(t *testing.T) {
m := NewModelWithConfig(nil, nil, 250, common.DefaultKeyMap())
m.activeTab = TabProcesses
snap := statsengine.NewSnapshot(nil, nil, nil, nil, nil, []statsengine.ProcessSnapshot{{PID: 1}, {PID: 2}}, statsengine.HistogramSnapshot{}, statsengine.HistogramSnapshot{})
m.latest = &snap
next, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'j'}})
model := next.(Model)
if model.processesOffset != 1 {
t.Fatalf("expected processes offset 1 after j, got %d", model.processesOffset)
}
next, _ = model.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'k'}})
model = next.(Model)
if model.processesOffset != 0 {
t.Fatalf("expected processes offset 0 after k, got %d", model.processesOffset)
}
}
func TestFilesTabScrollsWithJK(t *testing.T) {
m := NewModelWithConfig(nil, nil, 250, common.DefaultKeyMap())
m.activeTab = TabFiles
snap := statsengine.NewSnapshot(nil, nil, nil, nil, []statsengine.FileSnapshot{{Path: "/a"}, {Path: "/b"}}, nil, statsengine.HistogramSnapshot{}, statsengine.HistogramSnapshot{})
m.latest = &snap
next, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'j'}})
model := next.(Model)
if model.filesOffset != 1 {
t.Fatalf("expected files offset 1 after j, got %d", model.filesOffset)
}
next, _ = model.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'k'}})
model = next.(Model)
if model.filesOffset != 0 {
t.Fatalf("expected files offset 0 after k, got %d", model.filesOffset)
}
}
func TestFilesTabGroupedScrollUsesDirectoryOffset(t *testing.T) {
m := NewModelWithConfig(nil, nil, 250, common.DefaultKeyMap())
m.activeTab = TabFiles
m.filesDirGrouped = true
snap := statsengine.NewSnapshot(nil, nil, nil, nil, []statsengine.FileSnapshot{
{Path: "/a/f1"},
{Path: "/a/f2"},
{Path: "/b/f3"},
}, nil, statsengine.HistogramSnapshot{}, statsengine.HistogramSnapshot{})
m.latest = &snap
next, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'j'}})
model := next.(Model)
if model.filesDirOffset != 1 {
t.Fatalf("expected grouped dir offset 1 after j, got %d", model.filesDirOffset)
}
if model.filesOffset != 0 {
t.Fatalf("expected flat files offset unchanged, got %d", model.filesOffset)
}
}
func TestStreamSpaceUnpauseSchedulesStreamTick(t *testing.T) {
rb := eventstream.NewRingBuffer()
m := NewModelWithConfig(nil, rb, 250, common.DefaultKeyMap())
m.activeTab = TabStream
m.streamModel.HandleKey("space") // pause
next, cmd := m.Update(tea.KeyMsg{Type: tea.KeySpace})
_ = next
if cmd == nil {
t.Fatalf("expected stream tick command when unpausing stream")
}
}
func TestStreamPausedSupportsJKArrowsAndPageKeys(t *testing.T) {
rb := eventstream.NewRingBuffer()
for i := 0; i < 300; i++ {
rb.Push(eventstream.StreamEvent{
Seq: uint64(i + 1),
Syscall: "read",
Comm: "proc",
PID: 1000,
TID: uint32(2000 + i),
FileName: fmt.Sprintf("/tmp/file-%03d", i),
})
}
m := NewModelWithConfig(nil, rb, 250, common.DefaultKeyMap())
m.activeTab = TabStream
m.showHelp = true
next, _ := m.Update(tea.WindowSizeMsg{Width: 120, Height: 30})
m = next.(Model)
m.streamModel.Refresh()
_ = m.View()
next, _ = m.Update(tea.KeyMsg{Type: tea.KeySpace}) // pause
m = next.(Model)
before := rowFromStreamView(t, m.View())
next, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'k'}})
m = next.(Model)
afterK := rowFromStreamView(t, m.View())
if afterK >= before {
t.Fatalf("expected k to scroll up while paused: before=%d afterK=%d", before, afterK)
}
next, _ = m.Update(tea.KeyMsg{Type: tea.KeyDown})
m = next.(Model)
afterDown := rowFromStreamView(t, m.View())
if afterDown <= afterK {
t.Fatalf("expected down arrow to scroll down while paused: afterK=%d afterDown=%d", afterK, afterDown)
}
next, _ = m.Update(tea.KeyMsg{Type: tea.KeyPgUp})
m = next.(Model)
afterPgUp := rowFromStreamView(t, m.View())
if afterPgUp >= afterDown {
t.Fatalf("expected pgup to scroll up while paused: afterDown=%d afterPgUp=%d", afterDown, afterPgUp)
}
next, _ = m.Update(tea.KeyMsg{Type: tea.KeyPgDown})
m = next.(Model)
afterPgDown := rowFromStreamView(t, m.View())
if afterPgDown <= afterPgUp {
t.Fatalf("expected pgdown to scroll down while paused: afterPgUp=%d afterPgDown=%d", afterPgUp, afterPgDown)
}
}
func rowFromStreamView(t *testing.T, view string) int {
t.Helper()
re := regexp.MustCompile(`Row ([0-9]+)/([0-9]+)`)
m := re.FindStringSubmatch(view)
if len(m) != 3 {
t.Fatalf("stream row status not found in view")
}
row, err := strconv.Atoi(m[1])
if err != nil {
t.Fatalf("invalid row value %q: %v", m[1], err)
}
return row
}
func TestDirGroupKeyTogglesOnlyOnFilesTab(t *testing.T) {
m := NewModelWithConfig(nil, nil, 250, common.DefaultKeyMap())
m.activeTab = TabFiles
next, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'d'}})
model := next.(Model)
if !model.filesDirGrouped {
t.Fatalf("expected filesDirGrouped to toggle on files tab")
}
model.activeTab = TabOverview
next, _ = model.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'d'}})
model = next.(Model)
if !model.filesDirGrouped {
t.Fatalf("expected filesDirGrouped unchanged outside files tab")
}
}
func TestScrollOffsetDoesNotGrowUnbounded(t *testing.T) {
m := NewModelWithConfig(nil, nil, 250, common.DefaultKeyMap())
m.activeTab = TabSyscalls
snap := statsengine.NewSnapshot(nil, nil, nil, []statsengine.SyscallSnapshot{{Name: "read", Count: 1}, {Name: "write", Count: 1}}, nil, nil, statsengine.HistogramSnapshot{}, statsengine.HistogramSnapshot{})
m.latest = &snap
for i := 0; i < 50; i++ {
next, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'j'}})
m = next.(Model)
}
if m.syscallsOffset != 1 {
t.Fatalf("expected bounded offset 1, got %d", m.syscallsOffset)
}
}
func TestRefreshKeyEmitsRefreshTick(t *testing.T) {
snap := &statsengine.Snapshot{TotalSyscalls: 13}
engine := &fakeSnapshotSource{snap: snap}
m := NewModelWithConfig(engine, nil, 250, common.DefaultKeyMap())
next, cmd := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'r'}})
_ = next
if cmd == nil {
t.Fatalf("expected refresh command")
}
msg := cmd()
stats, ok := msg.(messages.StatsTickMsg)
if !ok {
t.Fatalf("expected StatsTickMsg from refresh key command, got %T", msg)
}
if stats.Snap != snap {
t.Fatalf("expected refreshed snapshot from engine")
}
}
func TestRefreshTickEmitsStatsTickMsg(t *testing.T) {
snap := &statsengine.Snapshot{TotalSyscalls: 9}
engine := &fakeSnapshotSource{snap: snap}
m := NewModelWithConfig(engine, nil, 100, common.DefaultKeyMap())
next, cmd := m.Update(refreshTickMsg{})
if cmd == nil {
t.Fatalf("expected tick command batch")
}
if engine.snapshots != 1 {
t.Fatalf("expected one snapshot call, got %d", engine.snapshots)
}
msg := cmd()
switch v := msg.(type) {
case tea.BatchMsg:
var sawStats bool
for _, c := range v {
out := c()
if stats, ok := out.(messages.StatsTickMsg); ok && stats.Snap == snap {
sawStats = true
}
}
if !sawStats {
t.Fatalf("expected StatsTickMsg in batch output")
}
default:
t.Fatalf("expected batch message, got %T", msg)
}
_ = next
}
func TestStatsTickMsgUpdatesLatestSnapshot(t *testing.T) {
snap := &statsengine.Snapshot{TotalSyscalls: 11}
m := NewModel(nil, nil)
next, _ := m.Update(messages.StatsTickMsg{Snap: snap})
model := next.(Model)
if model.latest != snap {
t.Fatalf("expected latest snapshot to be updated")
}
}
func TestStatsTickClampsGroupedFilesOffset(t *testing.T) {
snap := statsengine.NewSnapshot(
nil,
nil,
nil,
nil,
[]statsengine.FileSnapshot{{Path: "/a/f1"}, {Path: "/a/f2"}},
nil,
statsengine.HistogramSnapshot{},
statsengine.HistogramSnapshot{},
)
m := NewModel(nil, nil)
m.filesDirOffset = 10
next, _ := m.Update(messages.StatsTickMsg{Snap: &snap})
model := next.(Model)
if model.filesDirOffset != 0 {
t.Fatalf("expected grouped files offset clamped to 0, got %d", model.filesDirOffset)
}
}
func TestViewRendersTabBarAndHelp(t *testing.T) {
m := NewModelWithConfig(nil, nil, 1000, common.DefaultKeyMap())
out := m.View()
if !strings.Contains(out, "Overview") {
t.Fatalf("expected overview label in view")
}
if !strings.Contains(out, "press H for help") {
t.Fatalf("expected help hint text in view")
}
if strings.Contains(out, "tab next tab") {
t.Fatalf("did not expect expanded help bar by default")
}
}
func TestRenderActiveTabUsesDirectoryFilesViewWhenGrouped(t *testing.T) {
snap := statsengine.NewSnapshot(
nil, nil, nil, nil,
[]statsengine.FileSnapshot{{Path: "/tmp/a.log", Accesses: 2}},
nil,
statsengine.HistogramSnapshot{},
statsengine.HistogramSnapshot{},
)
out := renderActiveTab(TabFiles, &snap, nil, 120, 30, -1, 0, 0, true, 0, 0)
if !strings.Contains(out, "Directory") {
t.Fatalf("expected grouped directory files view header, got %q", out)
}
}
func TestStreamTabViewKeepsTabAndHelpChromeVisible(t *testing.T) {
rb := eventstream.NewRingBuffer()
for i := 0; i < 200; i++ {
rb.Push(eventstream.StreamEvent{Syscall: "read"})
}
m := NewModelWithConfig(nil, rb, 1000, common.DefaultKeyMap())
m.activeTab = TabStream
m.width = 120
m.height = 30
m.streamModel.SetSource(rb)
m.streamModel.Refresh()
out := m.View()
if !strings.Contains(out, "1:Overview") {
t.Fatalf("expected tab bar to remain visible in stream view")
}
if !strings.Contains(out, "press H for help") {
t.Fatalf("expected help hint to remain visible in stream view")
}
}
func TestHelpToggleWithH(t *testing.T) {
m := NewModelWithConfig(nil, nil, 1000, common.DefaultKeyMap())
out := m.View()
if !strings.Contains(out, "press H for help") {
t.Fatalf("expected default help hint")
}
next, _ := m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'H'}})
m = next.(Model)
out = m.View()
if !strings.Contains(out, "tab next tab") {
t.Fatalf("expected expanded help after pressing h")
}
next, _ = m.Update(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'H'}})
m = next.(Model)
out = m.View()
if !strings.Contains(out, "press H for help") {
t.Fatalf("expected help hint after pressing h again")
}
}
|