Repository navigation
Expand file tree
/
Copy pathobserve_array_test.go
More file actions
136 lines (125 loc) · 3.57 KB
/
Copy pathobserve_array_test.go
File metadata and controls
136 lines (125 loc) · 3.57 KB
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
package ygo_test
import (
"testing"
"github.com/Deln0r/ygo"
)
func collectArrayDeltas(a *ygo.Array) *[][]ygo.ArrayDeltaOp {
out := &[][]ygo.ArrayDeltaOp{}
a.Observe(func(e *ygo.ArrayEvent) { *out = append(*out, e.Delta) })
return out
}
// TestArrayObserve_Delta checks Array.Observe deltas against the yjs
// YArrayEvent semantics captured from yjs@13.6.31: insert / delete /
// retain ops and the dropped trailing retain.
func TestArrayObserve_Delta(t *testing.T) {
t.Run("insert head", func(t *testing.T) {
d := ygo.NewDoc()
a := ygo.NewArray(d, "a")
ev := collectArrayDeltas(a)
txn := d.WriteTxn()
a.Push(txn, "x", "y", "z")
txn.Commit()
assertDeltas(t, *ev, [][]ygo.ArrayDeltaOp{
{{Insert: []any{"x", "y", "z"}}},
})
})
t.Run("retain then insert middle", func(t *testing.T) {
d := ygo.NewDoc()
a := ygo.NewArray(d, "a")
txn := d.WriteTxn()
a.Push(txn, "a", "b", "c")
txn.Commit()
ev := collectArrayDeltas(a)
txn = d.WriteTxn()
a.Insert(txn, 1, "X")
txn.Commit()
assertDeltas(t, *ev, [][]ygo.ArrayDeltaOp{
{{Retain: 1}, {Insert: []any{"X"}}},
})
})
t.Run("retain then delete (trailing retain dropped)", func(t *testing.T) {
d := ygo.NewDoc()
a := ygo.NewArray(d, "a")
txn := d.WriteTxn()
a.Push(txn, "a", "b", "c", "d")
txn.Commit()
ev := collectArrayDeltas(a)
txn = d.WriteTxn()
a.Delete(txn, 1, 2) // remove b,c -> [a,d]; trailing retain on d dropped
txn.Commit()
assertDeltas(t, *ev, [][]ygo.ArrayDeltaOp{
{{Retain: 1}, {Delete: 2}},
})
})
t.Run("insert and delete one txn", func(t *testing.T) {
d := ygo.NewDoc()
a := ygo.NewArray(d, "a")
txn := d.WriteTxn()
a.Push(txn, "a", "b", "c")
txn.Commit()
ev := collectArrayDeltas(a)
txn = d.WriteTxn()
a.Insert(txn, 1, "X") // [a,X,b,c]
a.Delete(txn, 2, 1) // remove b -> [a,X,c]; c's trailing retain dropped
txn.Commit()
assertDeltas(t, *ev, [][]ygo.ArrayDeltaOp{
{{Retain: 1}, {Insert: []any{"X"}}, {Delete: 1}},
})
})
t.Run("delete head no trailing retain", func(t *testing.T) {
d := ygo.NewDoc()
a := ygo.NewArray(d, "a")
txn := d.WriteTxn()
a.Push(txn, "a", "b", "c")
txn.Commit()
ev := collectArrayDeltas(a)
txn = d.WriteTxn()
a.Delete(txn, 0, 1) // remove a -> [b,c]; trailing retain dropped
txn.Commit()
assertDeltas(t, *ev, [][]ygo.ArrayDeltaOp{
{{Delete: 1}},
})
})
}
// TestArrayObserve_RemoteUpdate confirms array observers fire on a
// remote update.
func TestArrayObserve_RemoteUpdate(t *testing.T) {
src := ygo.NewDoc()
sa := ygo.NewArray(src, "a")
txn := src.WriteTxn()
sa.Push(txn, 1, 2)
txn.Commit()
dst := ygo.NewDoc()
da := ygo.NewArray(dst, "a")
ev := collectArrayDeltas(da)
if err := ygo.ApplyUpdate(dst, ygo.EncodeStateAsUpdate(src)); err != nil {
t.Fatal(err)
}
assertDeltas(t, *ev, [][]ygo.ArrayDeltaOp{
{{Insert: []any{int64(1), int64(2)}}},
})
}
func assertDeltas(t *testing.T, got, want [][]ygo.ArrayDeltaOp) {
t.Helper()
if len(got) != len(want) {
t.Fatalf("got %d events, want %d: %+v", len(got), len(want), got)
}
for i := range want {
if len(got[i]) != len(want[i]) {
t.Errorf("event %d: got %d ops, want %d: %+v vs %+v", i, len(got[i]), len(want[i]), got[i], want[i])
continue
}
for j := range want[i] {
g, w := got[i][j], want[i][j]
if g.Delete != w.Delete || g.Retain != w.Retain || len(g.Insert) != len(w.Insert) {
t.Errorf("event %d op %d: got %+v, want %+v", i, j, g, w)
continue
}
for k := range w.Insert {
if g.Insert[k] != w.Insert[k] {
t.Errorf("event %d op %d insert[%d]: got %#v, want %#v", i, j, k, g.Insert[k], w.Insert[k])
}
}
}
}
}