Skip to content

Commit ec05f33

Browse files
committed
Unit tests for readSyncMessage
1 parent e074cf4 commit ec05f33

File tree

1 file changed

+103
-1
lines changed

1 file changed

+103
-1
lines changed

test/shareddoc.test.js

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,7 @@ describe('Collab Test Suite', () => {
887887
}
888888
});
889889

890-
it('Test message listener Sync', () => {
890+
it('Test Sync Step1', () => {
891891
const connSent = []
892892
const conn = {
893893
readyState: 0, // wsReadyState
@@ -910,6 +910,108 @@ describe('Collab Test Suite', () => {
910910
}
911911
});
912912

913+
it('Test Sync Step1 readonly connection', () => {
914+
const connSent = []
915+
const conn = {
916+
readyState: 0, // wsReadyState
917+
send(m, r) { connSent.push({m, r}); },
918+
readOnly: true,
919+
};
920+
921+
const emitted = []
922+
const doc = new Y.Doc();
923+
doc.emit = (t, e) => emitted.push({t, e});
924+
doc.getMap('foo').set('bar', 'hello');
925+
926+
const message = [0, 0, 1, 0];
927+
928+
messageListener(conn, doc, new Uint8Array(message));
929+
assert.equal(1, connSent.length, "Readonly connection should still call sync step 1");
930+
assert(isSubArray(connSent[0].m, new Uint8Array(getAsciiChars('hello'))));
931+
932+
for (let i = 0; i < emitted.length; i++) {
933+
assert(emitted[i].t !== 'error');
934+
}
935+
});
936+
937+
const testSyncStep2 = async (doc, readonly) => {
938+
const ss2Called = [];
939+
const mockSS2 = (dec, doc) => {
940+
ss2Called.push({dec, doc});
941+
}
942+
943+
const shd = await esmock(
944+
'../src/shareddoc.js', {
945+
'y-protocols/sync.js': {
946+
readSyncStep2: mockSS2
947+
}
948+
});
949+
950+
const conn = {};
951+
if (readonly) {
952+
conn.readOnly = true;
953+
}
954+
955+
const message = [0, 1, 1, 0];
956+
957+
assert.equal(ss2Called.length, 0, 'Precondition');
958+
shd.messageListener(conn, doc, new Uint8Array(message));
959+
return ss2Called;
960+
};
961+
962+
it('Test Sync Step2', async () => {
963+
const doc = new Y.Doc();
964+
const ss2Called = await testSyncStep2(doc, false);
965+
assert.equal(ss2Called.length, 1);
966+
assert(ss2Called[0].dec);
967+
assert(ss2Called[0].doc === doc);
968+
});
969+
970+
it('Test Sync Step2 readonly connection', async () => {
971+
const doc = new Y.Doc();
972+
const ss2Called = await testSyncStep2(doc, true);
973+
assert.equal(ss2Called.length, 0, 'Sync step 2 should not be called for a readonly connection');
974+
});
975+
976+
const testYjsUpdate = async (doc, readonly) => {
977+
const updCalled = [];
978+
const mockUpd = (dec, doc) => {
979+
updCalled.push({dec, doc});
980+
}
981+
982+
const shd = await esmock(
983+
'../src/shareddoc.js', {
984+
'y-protocols/sync.js': {
985+
readUpdate: mockUpd
986+
}
987+
});
988+
989+
const conn = {};
990+
if (readonly) {
991+
conn.readOnly = true;
992+
}
993+
994+
const message = [0, 2, 1, 0];
995+
996+
assert.equal(updCalled.length, 0, 'Precondition');
997+
shd.messageListener(conn, doc, new Uint8Array(message));
998+
return updCalled;
999+
};
1000+
1001+
it('Test YJS Update', async () => {
1002+
const doc = new Y.Doc();
1003+
const updCalled = await testYjsUpdate(doc, false);
1004+
assert.equal(updCalled.length, 1);
1005+
assert(updCalled[0].dec);
1006+
assert(updCalled[0].doc === doc);
1007+
});
1008+
1009+
it('Test YJS Update readonly connection', async () => {
1010+
const doc = new Y.Doc();
1011+
const updCalled = await testYjsUpdate(doc, true);
1012+
assert.equal(updCalled.length, 0, 'YJS update should not be called for a readonly connection');
1013+
});
1014+
9131015
it('Test message listener awareness', () => {
9141016
// A fabricated message
9151017
const message = [

0 commit comments

Comments
 (0)