Something went wrong. Try again.
Go bindings to the Rust Yrs library that implements the Yjs CRDT
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225package sync
import ( "bytes" "testing")
func TestWriteVarUint(t *testing.T) { tests := []struct { name string value uint64 expected []byte }{ {"zero", 0, []byte{0x00}}, {"one", 1, []byte{0x01}}, {"127 (max single byte)", 127, []byte{0x7f}}, {"128 (two bytes)", 128, []byte{0x80, 0x01}}, {"300 (two bytes)", 300, []byte{0xac, 0x02}}, {"16383 (max two bytes)", 16383, []byte{0xff, 0x7f}}, {"16384 (three bytes)", 16384, []byte{0x80, 0x80, 0x01}}, {"max uint64", 0xffffffffffffffff, []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01}}, }
for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { buf := make([]byte, 20) n := writeVarUint(buf, tt.value) if !bytes.Equal(buf[:n], tt.expected) { t.Errorf("writeVarUint(%d) = %v, want %v", tt.value, buf[:n], tt.expected) } }) }}
func TestReadVarUint(t *testing.T) { tests := []struct { name string data []byte expected uint64 wantErr bool }{ {"zero", []byte{0x00}, 0, false}, {"one", []byte{0x01}, 1, false}, {"127", []byte{0x7f}, 127, false}, {"128", []byte{0x80, 0x01}, 128, false}, {"300", []byte{0xac, 0x02}, 300, false}, {"16383", []byte{0xff, 0x7f}, 16383, false}, {"16384", []byte{0x80, 0x80, 0x01}, 16384, false}, {"max uint64", []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01}, 0xffffffffffffffff, false}, {"empty input", []byte{}, 0, true}, {"incomplete continuation", []byte{0x80}, 0, true}, }
for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { val, n, err := readVarUint(tt.data) if (err != nil) != tt.wantErr { t.Errorf("readVarUint(%v) error = %v, wantErr %v", tt.data, err, tt.wantErr) return } if !tt.wantErr && val != tt.expected { t.Errorf("readVarUint(%v) = %d, want %d", tt.data, val, tt.expected) } if !tt.wantErr && n == 0 { t.Error("readVarUint should return non-zero bytes read") } }) }}
func TestEncodeVarByteArray(t *testing.T) { tests := []struct { name string data []byte expected []byte }{ {"empty", []byte{}, []byte{0}}, {"single byte", []byte{0x01}, []byte{1, 0x01}}, {"multiple bytes", []byte{0x01, 0x02, 0x03}, []byte{3, 0x01, 0x02, 0x03}}, {"longer data", []byte(bytes.Repeat([]byte{0xab}, 100)), append([]byte{100}, bytes.Repeat([]byte{0xab}, 100)...)}, }
for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { result := encodeVarByteArray(tt.data) if !bytes.Equal(result, tt.expected) { t.Errorf("encodeVarByteArray(%v) = %v, want %v", tt.data, result, tt.expected) } }) }}
func TestEncodeMessage(t *testing.T) { tests := []struct { name string msgType uint8 payload []byte expected []byte }{ {"SyncStep1 with empty payload", SyncStep1, []byte{}, []byte{0, 0}}, {"SyncStep2 with payload", SyncStep2, []byte{0x01, 0x02}, []byte{1, 2, 0x01, 0x02}}, {"Update with empty payload", Update, []byte{}, []byte{2, 0}}, {"Update with payload", Update, []byte{0xff, 0xff}, []byte{2, 2, 0xff, 0xff}}, {"large payload", SyncStep1, []byte(bytes.Repeat([]byte{0xaa}, 50)), func() []byte { buf := []byte{0, 50} return append(buf, bytes.Repeat([]byte{0xaa}, 50)...) }()}, }
for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { result := encodeMessage(tt.msgType, tt.payload) if !bytes.Equal(result, tt.expected) { t.Errorf("encodeMessage(%d, %v) = %v, want %v", tt.msgType, tt.payload, result, tt.expected) } }) }}
func TestMessageTypeConstants(t *testing.T) { if SyncStep1 != 0 { t.Errorf("expected SyncStep1=0, got %d", SyncStep1) } if SyncStep2 != 1 { t.Errorf("expected SyncStep2=1, got %d", SyncStep2) } if Update != 2 { t.Errorf("expected Update=2, got %d", Update) }}
func TestWriteReadVarUintRoundTrip(t *testing.T) { values := []uint64{0, 1, 127, 128, 300, 16383, 16384, 1000, 100000, 0xffffffff} for _, v := range values { buf := make([]byte, 20) n := writeVarUint(buf, v) val, read, err := readVarUint(buf[:n]) if err != nil { t.Errorf("round trip failed for %d: %v", v, err) } if val != v { t.Errorf("round trip: wrote %d, read %d", v, val) } if read != n { t.Errorf("round trip: wrote %d bytes, read %d bytes", n, read) } }}
func TestReadVarUintOverflow(t *testing.T) { overflowData := make([]byte, 11) for i := 0; i < 10; i++ { overflowData[i] = 0x80 } overflowData[10] = 0x01
_, _, err := readVarUint(overflowData) if err == nil { t.Error("expected error for varint overflow") }}
func TestAppendVarUint(t *testing.T) { tests := []struct { name string value uint64 }{ {"zero", 0}, {"one", 1}, {"127", 127}, {"128", 128}, {"large", 0xFFFFFFFF}, }
for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { result := appendVarUint(nil, tt.value) val, n, err := readVarUint(result) if err != nil { t.Errorf("appendVarUint failed: %v", err) } if val != tt.value { t.Errorf("appendVarUint(%d) = %d, want %d", tt.value, val, tt.value) } if n != len(result) { t.Errorf("appendVarUint length mismatch: wrote %d, read %d", len(result), n) } }) }}
func TestAppendVarByteArray(t *testing.T) { tests := []struct { name string data []byte }{ {"empty", []byte{}}, {"single byte", []byte{0x01}}, {"multiple bytes", []byte{0x01, 0x02, 0x03}}, }
for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { result := appendVarByteArray(nil, tt.data) if len(result) < 1 { t.Error("encoded result too short") }
length, n, err := readVarUint(result) if err != nil { t.Errorf("failed to read length: %v", err) }
if length != uint64(len(tt.data)) { t.Errorf("length mismatch: got %d, want %d", length, len(tt.data)) }
payload := result[n:] if string(payload) != string(tt.data) { t.Errorf("payload mismatch: got %v, want %v", payload, tt.data) } }) }}