8 "github.com/stretchr/testify/require"
11 func TestViewMergesInTheOrderOfTaelinsPush(t *testing.T) {
15 for tick := range 20001 {
16 states = push(tick, states)
17 starts := states.starts()
18 view.high = len(starts)
19 view.low = len(starts)
22 lines := make([]int, len(view.lines))
23 for n, line := range view.lines {
24 lines[n] = line.index << line.level
27 require.Equal(t, starts, lines, "t=%d", tick)
31 func TestViewMergesInOneBatchFromHighToLow(t *testing.T) {
33 view := newView(10, 5)
34 sizes := make([]int, 0, 20)
37 sizes = append(sizes, view.Size(tree))
40 require.Equal(t, []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 5, 6, 7, 8, 9, 10, 5, 6, 7, 8}, sizes)
43 func TestViewMergesOnlyBuiltParents(t *testing.T) {
45 sizes: map[nodeKey]int{
60 require.Equal(t, []nodeKey{{0, 0}, {0, 1}, {0, 2}, {0, 3}}, view.lines)
61 require.True(t, view.merging)
63 tree.sizes[nodeKey{1, 0}] = 1
65 require.Equal(t, []nodeKey{{1, 0}, {0, 2}, {0, 3}, {0, 4}}, view.lines)
66 require.True(t, view.merging)
68 tree.sizes[nodeKey{1, 1}] = 1
69 tree.sizes[nodeKey{1, 2}] = 1
70 tree.sizes[nodeKey{2, 0}] = 1
72 require.Equal(t, []nodeKey{{2, 0}, {1, 2}}, view.lines)
73 require.False(t, view.merging)
76 func TestViewRendersPendingLinesWithoutTheirMessage(t *testing.T) {
78 sizes: map[nodeKey]int{
85 view, err := restoreView(viewState{
86 Lines: [][2]int{{1, 0}, {0, 2}},
87 }, chatHigh, chatLow, tree)
88 require.NoError(t, err)
89 require.Equal(t, "<chat>\n0+2|aaa aaa\n2+1|"+pending+"\n</chat>", view.Render(tree))
90 require.Equal(t, 7+len(pending), view.Size(tree))
93 func TestRestoreViewRejectsAGap(t *testing.T) {
94 _, err := restoreView(viewState{
95 Lines: [][2]int{{0, 0}, {0, 2}},
96 }, chatHigh, chatLow, builtTree{})
97 require.ErrorContains(t, err, "does not start at message 1")
100 // pushEntry is one entry of Taelin's rollback state list, newest first. Under
101 // push alone, life stays 0, so the entry leaves it out.
102 type pushEntry struct {
108 // push is the push of rollback_state_list.js.
109 func push(state int, states *pushEntry) *pushEntry {
126 older: push(states.state, states.older),
130 // starts returns the states of the list, oldest first.
131 func (e *pushEntry) starts() []int {
133 for entry := e; entry != nil; entry = entry.older {
134 starts = append(starts, entry.state)
137 slices.Reverse(starts)
142 // builtTree is a tree with each node built, with 1 byte of text.
143 type builtTree struct{}
145 func (builtTree) Node(level, index int) (Node, bool) {
146 return testNode(level, index, "a"), true
149 // partTree is a tree with the built nodes of its sizes.
150 type partTree struct {
151 sizes map[nodeKey]int
154 func (p partTree) Node(level, index int) (Node, bool) {
155 size, built := p.sizes[nodeKey{level, index}]
160 text := strings.Repeat("a", size)
165 return testNode(level, index, text), true