2021-12-12 09:46:58 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const AttributePool = require('../../../static/js/AttributePool');
|
|
|
|
const assert = require('assert').strict;
|
|
|
|
const common = require('../common');
|
|
|
|
const padManager = require('../../../node/db/PadManager');
|
|
|
|
|
|
|
|
describe(__filename, function () {
|
|
|
|
let agent;
|
|
|
|
let pad;
|
|
|
|
let padId;
|
|
|
|
let rev;
|
|
|
|
let socket;
|
|
|
|
|
|
|
|
before(async function () {
|
|
|
|
agent = await common.init();
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(async function () {
|
|
|
|
padId = common.randomString();
|
|
|
|
assert(!await padManager.doesPadExist(padId));
|
|
|
|
pad = await padManager.getPad(padId, '');
|
|
|
|
assert.equal(pad.text(), '\n');
|
|
|
|
const res = await agent.get(`/p/${padId}`).expect(200);
|
|
|
|
socket = await common.connect(res);
|
|
|
|
const {type, data: clientVars} = await common.handshake(socket, padId);
|
|
|
|
assert.equal(type, 'CLIENT_VARS');
|
|
|
|
rev = clientVars.collab_client_vars.rev;
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async function () {
|
|
|
|
if (socket != null) socket.close();
|
|
|
|
socket = null;
|
|
|
|
if (pad != null) await pad.remove();
|
|
|
|
pad = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('USER_CHANGES', function () {
|
2021-12-19 22:47:45 +01:00
|
|
|
const sendUserChanges =
|
|
|
|
async (changeset) => await common.sendUserChanges(socket, {baseRev: rev, changeset});
|
2021-12-12 09:46:58 +01:00
|
|
|
const assertAccepted = async (wantRev) => {
|
2021-12-19 22:47:45 +01:00
|
|
|
await common.waitForAcceptCommit(socket, wantRev);
|
2021-12-12 09:46:58 +01:00
|
|
|
rev = wantRev;
|
|
|
|
};
|
|
|
|
const assertRejected = async () => {
|
|
|
|
const msg = await common.waitForSocketEvent(socket, 'message');
|
|
|
|
assert.deepEqual(msg, {disconnect: 'badChangeset'});
|
|
|
|
};
|
|
|
|
|
|
|
|
it('changes are applied', async function () {
|
2021-12-19 22:47:45 +01:00
|
|
|
await Promise.all([
|
|
|
|
assertAccepted(rev + 1),
|
|
|
|
sendUserChanges('Z:1>5+5$hello'),
|
|
|
|
]);
|
2021-12-12 09:46:58 +01:00
|
|
|
assert.equal(pad.text(), 'hello\n');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('bad changeset is rejected', async function () {
|
2021-12-19 22:47:45 +01:00
|
|
|
await Promise.all([
|
|
|
|
assertRejected(),
|
|
|
|
sendUserChanges('this is not a valid changeset'),
|
|
|
|
]);
|
2021-12-12 09:46:58 +01:00
|
|
|
});
|
|
|
|
|
2021-12-13 00:19:36 +01:00
|
|
|
it('retransmission is accepted, has no effect', async function () {
|
2021-12-19 22:47:45 +01:00
|
|
|
const cs = 'Z:1>5+5$hello';
|
|
|
|
await Promise.all([
|
|
|
|
assertAccepted(rev + 1),
|
|
|
|
sendUserChanges(cs),
|
|
|
|
]);
|
2021-12-12 09:46:58 +01:00
|
|
|
--rev;
|
2021-12-19 22:47:45 +01:00
|
|
|
await Promise.all([
|
|
|
|
assertAccepted(rev + 1),
|
|
|
|
sendUserChanges(cs),
|
|
|
|
]);
|
2021-12-12 09:46:58 +01:00
|
|
|
assert.equal(pad.text(), 'hello\n');
|
|
|
|
});
|
|
|
|
|
2021-12-12 02:03:35 +01:00
|
|
|
it('identity changeset is accepted, has no effect', async function () {
|
2021-12-19 22:47:45 +01:00
|
|
|
await Promise.all([
|
|
|
|
assertAccepted(rev + 1),
|
|
|
|
sendUserChanges('Z:1>5+5$hello'),
|
|
|
|
]);
|
|
|
|
await Promise.all([
|
|
|
|
assertAccepted(rev),
|
|
|
|
sendUserChanges('Z:6>0$'),
|
|
|
|
]);
|
2021-12-12 09:46:58 +01:00
|
|
|
assert.equal(pad.text(), 'hello\n');
|
|
|
|
});
|
|
|
|
|
2021-12-12 02:03:35 +01:00
|
|
|
it('non-identity changeset with no net change is accepted, has no effect', async function () {
|
2021-12-19 22:47:45 +01:00
|
|
|
await Promise.all([
|
|
|
|
assertAccepted(rev + 1),
|
|
|
|
sendUserChanges('Z:1>5+5$hello'),
|
|
|
|
]);
|
|
|
|
await Promise.all([
|
|
|
|
assertAccepted(rev),
|
|
|
|
sendUserChanges('Z:6>0-5+5$hello'),
|
|
|
|
]);
|
2021-12-12 09:46:58 +01:00
|
|
|
assert.equal(pad.text(), 'hello\n');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|