Skip to content

Commit

Permalink
feat: fix more tests
Browse files Browse the repository at this point in the history
darkskygit committed Jul 10, 2024

Verified

This commit was signed with the committer’s verified signature.
cdavernas Charles d'Avernas
1 parent b123a45 commit b60c673
Showing 2 changed files with 39 additions and 40 deletions.
51 changes: 25 additions & 26 deletions y-octo-node/tests/yjs/testHelper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as t from "lib0/testing";
import assert, { deepEqual } from "node:assert";
import * as prng from "lib0/prng";
import * as encoding from "lib0/encoding";
import * as decoding from "lib0/decoding";
@@ -75,14 +75,14 @@ export class TestYOctoInstance extends Y.Doc {
testConnector.allConns.add(this);
this.updates = [];
// set up observe on local model
this.onUpdate((update) => {
if (origin !== testConnector) {
const encoder = encoding.createEncoder();
syncProtocol.writeUpdate(encoder, update);
broadcastMessage(this, encoding.toUint8Array(encoder));
}
this.updates.push(update);
});
// this.onUpdate((update) => {
// // if (origin !== testConnector) {
// // const encoder = encoding.createEncoder();
// // syncProtocol.writeUpdate(encoder, update);
// // broadcastMessage(this, encoding.toUint8Array(encoder));
// // }
// this.updates.push(update);
// });
this.connect();
}

@@ -154,6 +154,7 @@ export class TestConnector {
* If this function was unable to flush a message, because there are no more messages to flush, it returns false. true otherwise.
*/
flushRandomMessage(): boolean {
return false;
const gen = this.prng;
const conns = Array.from(this.onlineConns).filter(
(conn) => conn.receiving.size > 0,
@@ -201,13 +202,11 @@ export class TestConnector {
}

reconnectAll() {
this.allConns.forEach((conn: { connect: () => any }) => conn.connect());
this.allConns.forEach((conn) => conn.connect());
}

disconnectAll() {
this.allConns.forEach((conn: { disconnect: () => any }) =>
conn.disconnect(),
);
this.allConns.forEach((conn) => conn.disconnect());
}

syncAll() {
@@ -330,32 +329,32 @@ export const compare = (users: TestYOctoInstance[]) => {
// t.assert(u.store.pendingStructs === null);
// }
// Test Array iterator
t.compare(
deepEqual(
users[0].getOrCreateArray("array").toArray(),
Array.from(users[0].getOrCreateArray("array").iter()),
);
// Test Map iterator
const ymapkeys: any[] = Array.from(users[0].getOrCreateMap("map").keys());
t.assert(ymapkeys.length === Object.keys(userMapValues[0]).length);
assert(ymapkeys.length === Object.keys(userMapValues[0]).length);
ymapkeys.forEach((key) =>
t.assert(object.hasProperty(userMapValues[0], key)),
assert(object.hasProperty(userMapValues[0], key)),
);

const mapRes: Record<string, any> = {};
for (const [k, v] of users[0].getOrCreateMap("map").entries()) {
mapRes[k] = Y.isAbstractType(v) ? v.toJSON() : v;
}
t.compare(userMapValues[0], mapRes);
deepEqual(userMapValues[0], mapRes);
// Compare all users
for (let i = 0; i < users.length - 1; i++) {
t.compare(
deepEqual(
userArrayValues[i].length,
users[i].getOrCreateArray("array").length,
);
t.compare(userArrayValues[i], userArrayValues[i + 1]);
t.compare(userMapValues[i], userMapValues[i + 1]);
// t.compare(userXmlValues[i], userXmlValues[i + 1]);
// t.compare(
deepEqual(userArrayValues[i], userArrayValues[i + 1]);
deepEqual(userMapValues[i], userMapValues[i + 1]);
// deepEqual(userXmlValues[i], userXmlValues[i + 1]);
// deepEqual(
// userTextValues[i]
// .map(
// /** @param {any} a */ (a: { insert: any }) =>
@@ -364,26 +363,26 @@ export const compare = (users: TestYOctoInstance[]) => {
// .join("").length,
// users[i].getOrCreateText("text").length,
// );
// t.compare(
// deepEqual(
// userTextValues[i],
// userTextValues[i + 1],
// "",
// (_constructor, a, b) => {
// if (Y.isAbstractType(a)) {
// t.compare(a.toJSON(), b.toJSON());
// deepEqual(a.toJSON(), b.toJSON());
// } else if (a !== b) {
// t.fail("Deltas dont match");
// }
// return true;
// },
// );
t.compare(Y.encodeStateVector(users[i]), Y.encodeStateVector(users[i + 1]));
deepEqual(Y.encodeStateVector(users[i]), Y.encodeStateVector(users[i + 1]));
Y.equalDeleteSets(
Y.createDeleteSetFromStructStore(users[i].store),
Y.createDeleteSetFromStructStore(users[i + 1].store),
);
Y.compareStructStores(users[i].store, users[i + 1].store);
t.compare(
deepEqual(
Y.encodeSnapshot(Y.snapshot(users[i])),
Y.encodeSnapshot(Y.snapshot(users[i + 1])),
);
28 changes: 14 additions & 14 deletions y-octo-node/tests/yjs/y-map.spec.ts
Original file line number Diff line number Diff line change
@@ -14,14 +14,16 @@ test.beforeEach(() => {
gen = prng.create(randomInt(0, 0xffffffff));
});

test.skip("testIterators", (t) => {
test("testIterators", (t) => {
const ydoc = new Y.Doc();
const ymap = ydoc.createMap();
// we are only checking if the type assumptions are correct
const vals = Array.from(ymap.values());
const entries = Array.from(ymap.entries());
const keys = Array.from(ymap.keys());
console.log(vals, entries, keys);
t.is(vals.length, 0);
t.is(entries.length, 0);
t.is(keys.length, 0);
});

/**
@@ -30,22 +32,20 @@ test.skip("testIterators", (t) => {
test.skip("testMapEventError", (t) => {
const doc = new Y.Doc();
const ymap = doc.createMap();
/**
* @type {any}
*/

let event: any = null;
ymap.observe((e) => {
event = e;
});
t.fails(() => {
t.throws(() => {
t.info(event.keys);
});
t.fails(() => {
t.throws(() => {
t.info(event.keys);
});
});

test.skip("testMapHavingIterableAsConstructorParamTests", (t) => {
test("testMapHavingIterableAsConstructorParamTests", (t) => {
const { users, map0 } = init(gen, { users: 1 });

const m1 = users[0].createMap(Object.entries({ number: 1, string: "hello" }));
@@ -58,14 +58,14 @@ test.skip("testMapHavingIterableAsConstructorParamTests", (t) => {
["boolean", true],
]);
map0.set("m2", m2);
t.assert(m2.get("object").x === 1);
t.assert(m2.get<any>("object").x === 1);
t.assert(m2.get("boolean") === true);

const m3 = users[0].createMap([...m1, ...m2]);
const m3 = users[0].createMap([...m1.entries(), ...m2.entries()]);
map0.set("m3", m3);
t.assert(m3.get("number") === 1);
t.assert(m3.get("string") === "hello");
t.assert(m3.get("object").x === 1);
t.assert(m3.get<any>("object").x === 1);
t.assert(m3.get("boolean") === true);
});

@@ -195,8 +195,8 @@ test.skip("testGetAndSetOfMapProperty", (t) => {
test.skip("testYmapSetsYmap", (t) => {
const { users, map0 } = init(gen, { users: 2 });

const map = map0.set("Map", new Y.Map());
t.assert(map0.get("Map") === map);
const map = map0.set("Map", users[0].createMap());
t.assert(Y.compareIds(map0.get<Y.Map>("Map").itemId, map.itemId));
map.set("one", 1);
t.deepEqual(map.get("one"), 1);
compare(users);
@@ -239,7 +239,7 @@ test.skip("testGetAndSetOfMapPropertyWithConflict", (t) => {
compare(users);
});

test.skip("testSizeAndDeleteOfMapProperty", (t) => {
test("testSizeAndDeleteOfMapProperty", (t) => {
const { map0 } = init(gen, { users: 1 });

map0.set("stuff", "c0");

0 comments on commit b60c673

Please sign in to comment.