|
1 | 1 | var Backend = require('../../lib/backend');
|
2 | 2 | var expect = require('chai').expect;
|
3 | 3 | var async = require('async');
|
| 4 | +var json0 = require('ot-json0').type; |
| 5 | +var richText = require('rich-text').type; |
| 6 | +var ShareDBError = require('../../lib/error'); |
4 | 7 | var errorHandler = require('../util').errorHandler;
|
5 | 8 |
|
6 | 9 | describe('Doc', function() {
|
@@ -407,6 +410,43 @@ describe('Doc', function() {
|
407 | 410 | ], done);
|
408 | 411 | });
|
409 | 412 |
|
| 413 | + it('rolls the doc back even if the op is not invertible', function(done) { |
| 414 | + var backend = this.backend; |
| 415 | + |
| 416 | + async.series([ |
| 417 | + function(next) { |
| 418 | + // Register the rich text type, which can't be inverted |
| 419 | + json0.registerSubtype(richText); |
| 420 | + |
| 421 | + var validOp = {p: ['richName'], oi: {ops: [{insert: 'Scooby\n'}]}}; |
| 422 | + doc.submitOp(validOp, function(error) { |
| 423 | + expect(error).to.not.exist; |
| 424 | + next(); |
| 425 | + }); |
| 426 | + }, |
| 427 | + function(next) { |
| 428 | + // Make the server reject this insertion |
| 429 | + backend.use('submit', function(_context, backendNext) { |
| 430 | + backendNext(new ShareDBError(ShareDBError.CODES.ERR_UNKNOWN_ERROR, 'Custom unknown error')); |
| 431 | + }); |
| 432 | + var nonInvertibleOp = {p: ['richName'], t: 'rich-text', o: [{insert: 'e'}]}; |
| 433 | + |
| 434 | + // The server error should get all the way back to our handler |
| 435 | + doc.submitOp(nonInvertibleOp, function(error) { |
| 436 | + expect(error.message).to.eql('Custom unknown error'); |
| 437 | + next(); |
| 438 | + }); |
| 439 | + }, |
| 440 | + doc.whenNothingPending.bind(doc), |
| 441 | + function(next) { |
| 442 | + // The doc should have been reverted successfully |
| 443 | + expect(doc.data).to.eql({name: 'Scooby', richName: {ops: [{insert: 'Scooby\n'}]}}); |
| 444 | + next(); |
| 445 | + } |
| 446 | + ], done); |
| 447 | + }); |
| 448 | + |
| 449 | + |
410 | 450 | it('rescues an irreversible op collision', function(done) {
|
411 | 451 | // This test case attempts to reconstruct the following corner case, with
|
412 | 452 | // two independent references to the same document. We submit two simultaneous, but
|
|
0 commit comments