1package vt
2
3import (
4 "testing"
5
6 "github.com/bnema/vev-vt/core"
7 "github.com/stretchr/testify/require"
8)
9
10func TestHistoryViewReusesUnchangedTail(t *testing.T) {
11 h := NewHistory(HistoryConfig{MaxRows: 8, ChunkRows: 4})
12 require.NoError(t, h.Append([]core.Cell{{Rune: 'a', Style: core.DefaultStyle()}}, LineBound{End: 1}))
13 first := h.View()
14 require.Same(t, first.chunks[0], h.View().chunks[0])
15 require.LessOrEqual(t, testing.AllocsPerRun(20, func() { _ = h.View() }), float64(1))
16 require.Same(t, first.chunks[0], h.SealAndView().chunks[0])
17}
18
19func TestHistoryTailCacheInvalidationPreservesRetainedViews(t *testing.T) {
20 for _, tt := range []struct {
21 name string
22 mutate func(*History) error
23 want int
24 }{
25 {"append", func(h *History) error {
26 return h.Append([]core.Cell{{Rune: 'c', Style: core.DefaultStyle()}}, LineBound{End: 1})
27 }, 3},
28 {"byte eviction", func(h *History) error {
29 return h.SetLimits(HistoryConfig{MaxBytes: core.StoredCellLogicalBytes + core.RowDescriptorLogicalBytes + core.StyleRecordLogicalBytes, ChunkRows: 4})
30 }, 1},
31 {"disable", func(h *History) error { return h.SetLimits(HistoryConfig{}) }, 0},
32 {"seal and row eviction", func(h *History) error { return h.SetLimits(HistoryConfig{MaxRows: 1, ChunkRows: 1}) }, 1},
33 } {
34 t.Run(tt.name, func(t *testing.T) {
35 h := NewHistory(HistoryConfig{MaxRows: 8, ChunkRows: 4})
36 for _, r := range "ab" {
37 require.NoError(t, h.Append([]core.Cell{{Rune: r, Style: core.DefaultStyle()}}, LineBound{End: 1}))
38 }
39 before := h.View()
40 snapshot := h.SnapshotView()
41 require.NoError(t, tt.mutate(h))
42 require.Equal(t, tt.want, h.View().Len())
43 require.Equal(t, 2, before.Len())
44 require.Equal(t, 'a', before.Cell(0, 0).Rune)
45 require.Equal(t, 'b', before.Cell(0, 1).Rune)
46 require.Equal(t, 'a', snapshot.Tail().Cell(0, 0).Rune)
47 require.Equal(t, 'b', snapshot.Tail().Cell(0, 1).Rune)
48 })
49 }
50}