mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-04-20 15:36:16 -04:00
tests: Factor out USER_CHANGES/ACCEPT_COMMIT helpers
This will make it possible for other tests to reuse the code.
This commit is contained in:
parent
674a0ccedc
commit
02d1b90d30
2 changed files with 90 additions and 36 deletions
|
@ -1,6 +1,8 @@
|
|||
'use strict';
|
||||
|
||||
const AttributePool = require('../../static/js/AttributePool');
|
||||
const apiHandler = require('../../node/handler/APIHandler');
|
||||
const assert = require('assert').strict;
|
||||
const io = require('socket.io-client');
|
||||
const log4js = require('log4js');
|
||||
const process = require('process');
|
||||
|
@ -185,6 +187,58 @@ exports.handshake = async (socket, padId) => {
|
|||
return msg;
|
||||
};
|
||||
|
||||
/**
|
||||
* Convenience wrapper around `socket.send()` that waits for acknowledgement.
|
||||
*/
|
||||
exports.sendMessage = async (socket, message) => await new Promise((resolve, reject) => {
|
||||
socket.send(message, (errInfo) => {
|
||||
if (errInfo != null) {
|
||||
const {name, message} = errInfo;
|
||||
const err = new Error(message);
|
||||
err.name = name;
|
||||
reject(err);
|
||||
return;
|
||||
}
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
|
||||
/**
|
||||
* Convenience function to send a USER_CHANGES message. Waits for acknowledgement.
|
||||
*/
|
||||
exports.sendUserChanges = async (socket, data) => await exports.sendMessage(socket, {
|
||||
type: 'COLLABROOM',
|
||||
component: 'pad',
|
||||
data: {
|
||||
type: 'USER_CHANGES',
|
||||
apool: new AttributePool(),
|
||||
...data,
|
||||
},
|
||||
});
|
||||
|
||||
/**
|
||||
* Convenience function that waits for an ACCEPT_COMMIT message. Asserts that the new revision
|
||||
* matches the expected revision.
|
||||
*
|
||||
* Note: To avoid a race condition, this should be called before the USER_CHANGES message is sent.
|
||||
* For example:
|
||||
*
|
||||
* await Promise.all([
|
||||
* common.waitForAcceptCommit(socket, rev + 1),
|
||||
* common.sendUserChanges(socket, {baseRev: rev, changeset}),
|
||||
* ]);
|
||||
*/
|
||||
exports.waitForAcceptCommit = async (socket, wantRev) => {
|
||||
const msg = await exports.waitForSocketEvent(socket, 'message');
|
||||
assert.deepEqual(msg, {
|
||||
type: 'COLLABROOM',
|
||||
data: {
|
||||
type: 'ACCEPT_COMMIT',
|
||||
newRev: wantRev,
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
const alphabet = 'abcdefghijklmnopqrstuvwxyz';
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue