types_test.go

  1package sync
  2
  3import (
  4	"testing"
  5)
  6
  7type mockTransaction struct{}
  8
  9func (m *mockTransaction) ApplyUpdate(data []byte) error {
 10	return nil
 11}
 12
 13func (m *mockTransaction) GetStateVector() []byte {
 14	return []byte{}
 15}
 16
 17func TestDocInterface(t *testing.T) {
 18	doc := &mockDoc{
 19		readTx: func(fn func(Transaction) error) error {
 20			return fn(&mockTransaction{})
 21		},
 22		writeTx: func(fn func(Transaction) error) error {
 23			return fn(&mockTransaction{})
 24		},
 25	}
 26
 27	err := doc.WithReadTransaction(func(txn Transaction) error {
 28		return nil
 29	})
 30	if err != nil {
 31		t.Errorf("unexpected error: %v", err)
 32	}
 33
 34	err = doc.WithWriteTransaction(func(txn Transaction) error {
 35		return nil
 36	})
 37	if err != nil {
 38		t.Errorf("unexpected error: %v", err)
 39	}
 40}
 41
 42func TestTransactionInterface(t *testing.T) {
 43	txn := &mockTransaction{}
 44
 45	err := txn.ApplyUpdate([]byte("test"))
 46	if err != nil {
 47		t.Errorf("unexpected error: %v", err)
 48	}
 49
 50	sv := txn.GetStateVector()
 51	if sv == nil {
 52		t.Error("expected non-nil state vector")
 53	}
 54}
 55
 56func TestUpdateData(t *testing.T) {
 57	data := UpdateData("test data")
 58
 59	if string(data) != "test data" {
 60		t.Errorf("expected 'test data', got '%s'", string(data))
 61	}
 62}
 63
 64func TestStatusConstants(t *testing.T) {
 65	if StatusDisconnected != 0 {
 66		t.Errorf("expected StatusDisconnected to be 0, got %d", StatusDisconnected)
 67	}
 68	if StatusConnecting != 1 {
 69		t.Errorf("expected StatusConnecting to be 1, got %d", StatusConnecting)
 70	}
 71	if StatusConnected != 2 {
 72		t.Errorf("expected StatusConnected to be 2, got %d", StatusConnected)
 73	}
 74	if StatusDisconnecting != 3 {
 75		t.Errorf("expected StatusDisconnecting to be 3, got %d", StatusDisconnecting)
 76	}
 77}
 78
 79func TestOptions(t *testing.T) {
 80	opts := Options{
 81		Endpoint:  "ws://localhost:8080",
 82		AuthToken: "test-token",
 83		OnUpdate:  func(doc DocInterface) error { return nil },
 84	}
 85
 86	if opts.Endpoint != "ws://localhost:8080" {
 87		t.Errorf("expected Endpoint ws://localhost:8080, got %s", opts.Endpoint)
 88	}
 89	if opts.AuthToken != "test-token" {
 90		t.Errorf("expected AuthToken test-token, got %s", opts.AuthToken)
 91	}
 92	if opts.OnUpdate == nil {
 93		t.Error("OnUpdate should not be nil")
 94	}
 95}
 96
 97func TestDefaultOptions(t *testing.T) {
 98	opts := DefaultOptions()
 99
100	if opts.Endpoint != "" {
101		t.Errorf("expected empty Endpoint, got %s", opts.Endpoint)
102	}
103	if opts.AuthToken != "" {
104		t.Errorf("expected empty AuthToken, got %s", opts.AuthToken)
105	}
106	if opts.OnUpdate != nil {
107		t.Error("OnUpdate should be nil for default options")
108	}
109}
110
111func TestWithEndpoint(t *testing.T) {
112	opts := Options{}
113	WithEndpoint("ws://test")(&opts)
114
115	if opts.Endpoint != "ws://test" {
116		t.Errorf("expected Endpoint ws://test, got %s", opts.Endpoint)
117	}
118}
119
120func TestWithAuthToken(t *testing.T) {
121	opts := Options{}
122	WithAuthToken("mytoken")(&opts)
123
124	if opts.AuthToken != "mytoken" {
125		t.Errorf("expected AuthToken mytoken, got %s", opts.AuthToken)
126	}
127}
128
129func TestWithOnUpdate(t *testing.T) {
130	called := false
131	fn := func(doc DocInterface) error {
132		called = true
133		return nil
134	}
135	opts := Options{}
136	WithOnUpdate(fn)(&opts)
137
138	opts.OnUpdate(nil)
139	if !called {
140		t.Error("OnUpdate callback was not called")
141	}
142}