Skip to content

Commit

Permalink
Test that exercises a yjs class (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
bosschaert authored Feb 13, 2024
1 parent 106bc38 commit d27139c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 8 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"lint": "npm run lint:js",
"dev": "wrangler dev src/edge.js",
"deploy": "wrangler deploy src/edge.js",
"test": "c8 mocha"

"test": "c8 mocha --exit # --exit is needed because some test code triggers async listeners"
},
"mocha": {
"reporter": "mocha-multi-reporters",
Expand Down
2 changes: 1 addition & 1 deletion src/edge.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export const updateHandler = (update, _origin, doc) => {
doc.conns.forEach((_, conn) => send(doc, conn, message));
};

class WSSharedDoc extends Y.Doc {
export class WSSharedDoc extends Y.Doc {
constructor(name) {
super({ gc: gcEnabled });
this.name = name;
Expand Down
62 changes: 56 additions & 6 deletions test/edge.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,32 @@
* governing permissions and limitations under the License.
*/
import assert from 'assert';
import { updateHandler } from '../src/edge.js';
import { updateHandler, WSSharedDoc } from '../src/edge.js';

function isSubArray(full, sub) {
if (sub.length === 0) {
return true;
}

const candidateIdxs = [];
for (let i = 0; i < full.length; i++) {
if (full[i] === sub[0]) {
candidateIdxs.push(i);
}
}

nextCandidate:
for (let i = 0; i < candidateIdxs.length; i++) {
for (let j = 0; j < sub.length; j++) {
if (sub[j] !== full[candidateIdxs[i] + j]) {
break nextCandidate;
}
}
return true;
}

return false;
}

describe('Collab Test Suite', () => {
it('Test updateHandler', () => {
Expand All @@ -29,13 +54,11 @@ describe('Collab Test Suite', () => {
},
};

const fe = (func) => {
func(null, conn);
};

const deleted = [];
const conns = {
forEach: fe,
forEach(f) {
f(null, conn);
},
has(c) {
return c === conn;
},
Expand Down Expand Up @@ -97,4 +120,31 @@ describe('Collab Test Suite', () => {
assert.deepStrictEqual(update, conn1.message.slice(-4));
assert.deepStrictEqual(update, conn2.message.slice(-4));
});

it('Test WSSharedDoc', () => {
const doc = new WSSharedDoc('hello');
assert.equal(doc.name, 'hello');
assert.equal(doc.awareness.getLocalState(), null);

const conn = {
isClosed: false,
message: null,
readyState: 1, // wsReadyStateOpen
has() {
return true;
},
close() {
this.isClosed = true;
},
send(m) {
this.message = m;
},
};

doc.conns.set(conn, 'conn1');
doc.awareness.setLocalState('foo');
assert(conn.isClosed === false);
const fooAsUint8Arr = new Uint8Array(['f'.charCodeAt(0), 'o'.charCodeAt(0), 'o'.charCodeAt(0)]);
assert(isSubArray(conn.message, fooAsUint8Arr));
});
});

0 comments on commit d27139c

Please sign in to comment.