| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079 |
- 'use strict'
- const _ = require('lodash')
- const { expect } = require('chai')
- const ot = require('..')
- const StringFileData = require('../lib/file_data/string_file_data')
- const File = ot.File
- const AddFileOperation = ot.AddFileOperation
- const MoveFileOperation = ot.MoveFileOperation
- const EditFileOperation = ot.EditFileOperation
- const NoOperation = ot.NoOperation
- const Operation = ot.Operation
- const TextOperation = ot.TextOperation
- const AddCommentOperation = ot.AddCommentOperation
- const DeleteCommentOperation = ot.DeleteCommentOperation
- const SetCommentStateOperation = ot.SetCommentStateOperation
- const Snapshot = ot.Snapshot
- describe('Operation', function () {
- function makeEmptySnapshot() {
- return new Snapshot()
- }
- function makeOneFileSnapshot() {
- const snapshot = makeEmptySnapshot()
- snapshot.addFile('foo', File.fromString(''))
- return snapshot
- }
- function makeTwoFileSnapshot() {
- const snapshot = makeOneFileSnapshot()
- snapshot.addFile('bar', File.fromString('a'))
- return snapshot
- }
- function addFile(pathname, content) {
- return new AddFileOperation(pathname, File.fromString(content))
- }
- function roundTripOperation(operation) {
- return Operation.fromRaw(operation.toRaw())
- }
- function deepCopySnapshot(snapshot) {
- return Snapshot.fromRaw(snapshot.toRaw())
- }
- function runConcurrently(operation0, operation1, snapshot) {
- const operations = [
- // make sure they survive serialization
- roundTripOperation(operation0),
- roundTripOperation(operation1),
- ]
- const primeOperations = Operation.transform(operation0, operation1)
- const originalSnapshot = snapshot || makeEmptySnapshot()
- const snapshotA = deepCopySnapshot(originalSnapshot)
- const snapshotB = deepCopySnapshot(originalSnapshot)
- operations[0].applyTo(snapshotA)
- operations[1].applyTo(snapshotB)
- primeOperations[0].applyTo(snapshotB)
- primeOperations[1].applyTo(snapshotA)
- expect(snapshotA).to.eql(snapshotB)
- return {
- snapshot: snapshotA,
- operations,
- primeOperations,
- log() {
- console.log(this)
- return this
- },
- expectNoTransform() {
- expect(this.operations).to.deep.eql(this.primeOperations)
- return this
- },
- expectTransform() {
- expect(this.operations).to.not.deep.eql(this.primeOperations)
- return this
- },
- swap() {
- return runConcurrently(operation1, operation0, originalSnapshot)
- },
- expectFiles(files) {
- this.expectedFiles = files
- expect(this.snapshot.countFiles()).to.equal(_.size(files))
- _.forOwn(files, (expectedFile, pathname) => {
- if (_.isString(expectedFile)) {
- expectedFile = { content: expectedFile, metadata: {}, comments: [] }
- }
- const file = this.snapshot.getFile(pathname)
- expect(file.getContent()).to.equal(expectedFile.content)
- expect(file.getMetadata()).to.eql(expectedFile.metadata)
- expect(file.getComments()).to.deep.equal(expectedFile.comments)
- })
- return this
- },
- expectSymmetry() {
- if (!this.expectedFiles) {
- throw new Error('must call expectFiles before expectSymmetry')
- }
- this.swap().expectFiles(this.expectedFiles)
- return this
- },
- expectPrime(index, klass) {
- expect(this.primeOperations[index]).to.be.an.instanceof(klass)
- return this
- },
- tap(fn) {
- fn.call(this)
- return this
- },
- }
- }
- // shorthand for creating an edit operation
- function edit(pathname, textOperationOps) {
- return Operation.editFile(
- pathname,
- TextOperation.fromJSON({ textOperation: textOperationOps })
- )
- }
- it('transforms AddFile-AddFile with different names', function () {
- runConcurrently(addFile('foo', ''), addFile('bar', 'a'))
- .expectNoTransform()
- .expectFiles({ bar: 'a', foo: '' })
- .expectSymmetry()
- })
- it('transforms AddFile-AddFile with same name', function () {
- // the second file 'wins'
- runConcurrently(addFile('foo', ''), addFile('foo', 'a'))
- .expectFiles({ foo: 'a' })
- // if the first add was committed first, the second add overwrites it
- .expectPrime(1, AddFileOperation)
- // if the second add was committed first, the first add becomes a no-op
- .expectPrime(0, NoOperation)
- .swap()
- .expectFiles({ foo: '' })
- })
- it('transforms AddFile-MoveFile with no conflict', function () {
- runConcurrently(
- Operation.moveFile('foo', 'baz'),
- addFile('bar', 'a'),
- makeOneFileSnapshot()
- )
- .expectNoTransform()
- .expectFiles({ bar: 'a', baz: '' })
- .expectSymmetry()
- })
- it('transforms AddFile-MoveFile with move from new file', function () {
- runConcurrently(
- Operation.moveFile('foo', 'baz'),
- addFile('foo', 'a'),
- makeOneFileSnapshot()
- )
- .expectFiles({ baz: 'a' })
- // if the move was committed first, the add overwrites it
- .expectPrime(1, AddFileOperation)
- // if the add was committed first, the move appears in the history
- .expectPrime(0, MoveFileOperation)
- .expectSymmetry()
- })
- it('transforms AddFile-MoveFile with move to new file', function () {
- runConcurrently(
- Operation.moveFile('foo', 'baz'),
- addFile('baz', 'a'),
- makeOneFileSnapshot()
- )
- .expectFiles({ baz: 'a' })
- // if the move was committed first, the add overwrites it
- .expectPrime(1, AddFileOperation)
- // if the add was committed first, the move becomes a delete
- .expectPrime(0, MoveFileOperation)
- .tap(function () {
- expect(this.primeOperations[0].isRemoveFile()).to.be.true
- })
- .expectSymmetry()
- })
- it('transforms AddFile-RemoveFile with no conflict', function () {
- runConcurrently(
- Operation.removeFile('foo'),
- addFile('bar', 'a'),
- makeOneFileSnapshot()
- )
- .expectNoTransform()
- .expectFiles({ bar: 'a' })
- .expectSymmetry()
- })
- it('transforms AddFile-RemoveFile that removes added file', function () {
- runConcurrently(
- Operation.removeFile('foo'),
- addFile('foo', 'a'),
- makeOneFileSnapshot()
- )
- .expectFiles({ foo: 'a' })
- // if the move was committed first, the add overwrites it
- .expectPrime(1, AddFileOperation)
- // if the add was committed first, the move gets dropped
- .expectPrime(0, NoOperation)
- .expectSymmetry()
- })
- it('transforms AddFile-EditFile with no conflict', function () {
- runConcurrently(
- edit('foo', ['x']),
- addFile('bar', 'a'),
- makeOneFileSnapshot()
- )
- .expectNoTransform()
- .expectFiles({ bar: 'a', foo: 'x' })
- .expectSymmetry()
- })
- it('transforms AddFile-EditFile when new file is edited', function () {
- runConcurrently(
- edit('foo', ['x']),
- addFile('foo', 'a'),
- makeOneFileSnapshot()
- )
- .expectFiles({ foo: 'a' })
- // if the edit was committed first, the add overwrites it
- .expectPrime(1, AddFileOperation)
- // if the add was committed first, the edit gets dropped
- .expectPrime(0, NoOperation)
- .expectSymmetry()
- })
- it('transforms AddFile-SetFileMetadata with no conflict', function () {
- const testMetadata = { baz: 1 }
- runConcurrently(
- addFile('bar', 'a'),
- Operation.setFileMetadata('foo', testMetadata),
- makeOneFileSnapshot()
- )
- .expectNoTransform()
- .expectFiles({
- foo: { content: '', metadata: testMetadata, comments: [] },
- bar: 'a',
- })
- .expectSymmetry()
- })
- it('transforms AddFile-SetFileMetadata with same name', function () {
- const testMetadata = { baz: 1 }
- runConcurrently(
- addFile('foo', 'x'),
- Operation.setFileMetadata('foo', testMetadata),
- makeEmptySnapshot()
- )
- .expectFiles({
- foo: { content: 'x', metadata: testMetadata, comments: [] },
- })
- .expectSymmetry()
- })
- it('transforms MoveFile-MoveFile with no conflict', function () {
- runConcurrently(
- Operation.moveFile('foo', 'baz'),
- Operation.moveFile('bar', 'bat'),
- makeTwoFileSnapshot()
- )
- .expectFiles({ bat: 'a', baz: '' })
- .expectNoTransform()
- .expectSymmetry()
- })
- it('transforms MoveFile-MoveFile same move foo->foo, foo->foo', function () {
- runConcurrently(
- Operation.moveFile('foo', 'foo'),
- Operation.moveFile('foo', 'foo'),
- makeOneFileSnapshot()
- )
- .expectFiles({ foo: '' })
- .expectPrime(1, NoOperation)
- .expectPrime(0, NoOperation)
- .expectSymmetry()
- })
- it('transforms MoveFile-MoveFile no-op foo->foo, foo->bar', function () {
- runConcurrently(
- Operation.moveFile('foo', 'foo'),
- Operation.moveFile('foo', 'bar'),
- makeOneFileSnapshot()
- )
- .expectFiles({ bar: '' })
- .expectPrime(1, MoveFileOperation)
- .expectPrime(0, NoOperation)
- .expectSymmetry()
- })
- it('transforms MoveFile-MoveFile no-op foo->foo, bar->foo', function () {
- runConcurrently(
- Operation.moveFile('foo', 'foo'),
- Operation.moveFile('foo', 'bar'),
- makeTwoFileSnapshot()
- )
- .expectFiles({ bar: '' })
- .expectPrime(1, MoveFileOperation)
- .expectPrime(0, NoOperation)
- .expectSymmetry()
- })
- it('transforms MoveFile-MoveFile no-op foo->foo, bar->bar', function () {
- runConcurrently(
- Operation.moveFile('foo', 'foo'),
- Operation.moveFile('bar', 'bar'),
- makeTwoFileSnapshot()
- )
- .expectFiles({ bar: 'a', foo: '' })
- .expectPrime(1, NoOperation)
- .expectPrime(0, NoOperation)
- .expectSymmetry()
- })
- it('transforms MoveFile-MoveFile same move foo->bar, foo->bar', function () {
- runConcurrently(
- Operation.moveFile('foo', 'bar'),
- Operation.moveFile('foo', 'bar'),
- makeOneFileSnapshot()
- )
- .expectFiles({ bar: '' })
- .expectPrime(1, NoOperation)
- .expectPrime(0, NoOperation)
- .expectSymmetry()
- })
- it('transforms MoveFile-MoveFile opposite foo->bar, bar->foo', function () {
- runConcurrently(
- Operation.moveFile('foo', 'bar'),
- Operation.moveFile('bar', 'foo'),
- makeTwoFileSnapshot()
- )
- .expectFiles([])
- .expectPrime(1, MoveFileOperation)
- .expectPrime(0, MoveFileOperation)
- .tap(function () {
- expect(this.primeOperations[1].isRemoveFile()).to.be.true
- expect(this.primeOperations[1].getPathname()).to.equal('bar')
- expect(this.primeOperations[0].isRemoveFile()).to.be.true
- expect(this.primeOperations[0].getPathname()).to.equal('foo')
- })
- .expectSymmetry()
- })
- it('transforms MoveFile-MoveFile no-op foo->foo, bar->baz', function () {
- runConcurrently(
- Operation.moveFile('foo', 'foo'),
- Operation.moveFile('bar', 'baz'),
- makeTwoFileSnapshot()
- )
- .expectFiles({ baz: 'a', foo: '' })
- .expectPrime(1, MoveFileOperation)
- .expectPrime(0, NoOperation)
- .expectSymmetry()
- })
- it('transforms MoveFile-MoveFile diverge foo->bar, foo->baz', function () {
- runConcurrently(
- Operation.moveFile('foo', 'bar'),
- Operation.moveFile('foo', 'baz'),
- makeOneFileSnapshot()
- )
- .expectFiles({ baz: '' })
- // if foo->bar was committed first, the second move becomes bar->baz
- .expectPrime(1, MoveFileOperation)
- // if foo->baz was committed first, the second move becomes a no-op
- .expectPrime(0, NoOperation)
- .tap(function () {
- expect(this.primeOperations[1].getPathname()).to.equal('bar')
- expect(this.primeOperations[1].getNewPathname()).to.equal('baz')
- })
- .swap()
- .expectFiles({ bar: '' })
- })
- it('transforms MoveFile-MoveFile transitive foo->baz, bar->foo', function () {
- runConcurrently(
- Operation.moveFile('foo', 'baz'),
- Operation.moveFile('bar', 'foo'),
- makeTwoFileSnapshot()
- )
- .expectFiles({ baz: 'a' })
- .expectPrime(1, MoveFileOperation)
- .expectPrime(0, MoveFileOperation)
- .expectSymmetry()
- })
- it('transforms MoveFile-MoveFile transitive foo->bar, bar->baz', function () {
- runConcurrently(
- Operation.moveFile('foo', 'bar'),
- Operation.moveFile('bar', 'baz'),
- makeTwoFileSnapshot()
- )
- .expectFiles({ baz: '' })
- .expectPrime(1, MoveFileOperation)
- .expectPrime(0, MoveFileOperation)
- .expectSymmetry()
- })
- it('transforms MoveFile-MoveFile converge foo->baz, bar->baz', function () {
- runConcurrently(
- Operation.moveFile('foo', 'baz'),
- Operation.moveFile('bar', 'baz'),
- makeTwoFileSnapshot()
- )
- .expectFiles({ baz: 'a' })
- .expectPrime(1, MoveFileOperation)
- .expectPrime(0, MoveFileOperation)
- .tap(function () {
- // if foo->baz was committed first, we just apply the move
- expect(this.primeOperations[1]).to.eql(this.operations[1])
- // if bar->baz was committed first, the other move becomes a remove
- expect(this.primeOperations[0].isRemoveFile()).to.be.true
- expect(this.primeOperations[0].getPathname()).to.equal('foo')
- })
- .swap()
- .expectFiles({ baz: '' })
- })
- it('transforms MoveFile-RemoveFile no-op foo->foo, foo->', function () {
- runConcurrently(
- Operation.moveFile('foo', 'foo'),
- Operation.removeFile('foo'),
- makeOneFileSnapshot()
- )
- .expectFiles([])
- .expectPrime(1, MoveFileOperation)
- .expectPrime(0, NoOperation)
- .tap(function () {
- expect(this.primeOperations[1].isRemoveFile()).to.be.true
- })
- .expectSymmetry()
- })
- it('transforms MoveFile-RemoveFile same move foo->, foo->', function () {
- runConcurrently(
- Operation.removeFile('foo'),
- Operation.removeFile('foo'),
- makeOneFileSnapshot()
- )
- .expectFiles([])
- .expectPrime(1, NoOperation)
- .expectPrime(0, NoOperation)
- .expectSymmetry()
- })
- it('transforms MoveFile-RemoveFile no conflict foo->, bar->', function () {
- runConcurrently(
- Operation.removeFile('foo'),
- Operation.removeFile('bar'),
- makeTwoFileSnapshot()
- )
- .expectFiles([])
- .expectNoTransform()
- .expectSymmetry()
- })
- it('transforms MoveFile-RemoveFile no conflict foo->foo, bar->', function () {
- runConcurrently(
- Operation.moveFile('foo', 'foo'),
- Operation.removeFile('bar'),
- makeTwoFileSnapshot()
- )
- .expectFiles({ foo: '' })
- .expectPrime(1, MoveFileOperation)
- .expectPrime(0, NoOperation)
- .tap(function () {
- expect(this.primeOperations[1].isRemoveFile()).to.be.true
- })
- .expectSymmetry()
- })
- it('transforms MoveFile-RemoveFile transitive foo->, bar->foo', function () {
- runConcurrently(
- Operation.removeFile('foo'),
- Operation.moveFile('bar', 'foo'),
- makeTwoFileSnapshot()
- )
- .expectFiles([])
- .expectPrime(1, MoveFileOperation)
- .expectPrime(0, MoveFileOperation)
- .tap(function () {
- expect(this.primeOperations[1].isRemoveFile()).to.be.true
- expect(this.primeOperations[1].getPathname()).to.equal('bar')
- expect(this.primeOperations[0].isRemoveFile()).to.be.true
- expect(this.primeOperations[0].getPathname()).to.equal('foo')
- })
- .expectSymmetry()
- })
- it('transforms MoveFile-RemoveFile transitive foo->bar, bar->', function () {
- runConcurrently(
- Operation.moveFile('foo', 'bar'),
- Operation.removeFile('bar'),
- makeTwoFileSnapshot()
- )
- .expectFiles({})
- .expectPrime(1, MoveFileOperation)
- .expectPrime(0, MoveFileOperation)
- .tap(function () {
- expect(this.primeOperations[1].isRemoveFile()).to.be.true
- expect(this.primeOperations[1].getPathname()).to.equal('bar')
- expect(this.primeOperations[0].isRemoveFile()).to.be.true
- expect(this.primeOperations[0].getPathname()).to.equal('foo')
- })
- .expectSymmetry()
- })
- it('transforms MoveFile-EditFile with no conflict', function () {
- runConcurrently(
- Operation.moveFile('bar', 'baz'),
- edit('foo', ['x']),
- makeTwoFileSnapshot()
- )
- .expectFiles({ baz: 'a', foo: 'x' })
- .expectNoTransform()
- .expectSymmetry()
- })
- it('transforms MoveFile-EditFile with edit on pathname', function () {
- runConcurrently(
- Operation.moveFile('foo', 'bar'),
- edit('foo', ['x']),
- makeOneFileSnapshot()
- )
- .expectFiles({ bar: 'x' })
- .expectPrime(1, EditFileOperation)
- .expectPrime(0, MoveFileOperation)
- .tap(function () {
- expect(this.primeOperations[1].getPathname()).to.equal('bar')
- expect(this.primeOperations[0].getPathname()).to.equal('foo')
- expect(this.primeOperations[0].getNewPathname()).to.equal('bar')
- })
- .expectSymmetry()
- })
- it('transforms MoveFile-EditFile with edit on new pathname', function () {
- runConcurrently(
- Operation.moveFile('bar', 'foo'),
- edit('foo', ['x']),
- makeTwoFileSnapshot()
- )
- .expectFiles({ foo: 'a' })
- .expectPrime(1, NoOperation)
- .tap(function () {
- expect(this.primeOperations[0]).to.eql(this.operations[0])
- })
- .expectSymmetry()
- })
- it('transforms MoveFile-EditFile with no-op move', function () {
- runConcurrently(
- Operation.moveFile('foo', 'foo'),
- edit('foo', ['x']),
- makeOneFileSnapshot()
- )
- .expectFiles({ foo: 'x' })
- .expectNoTransform()
- .expectSymmetry()
- })
- it('transforms MoveFile-SetFileMetadata with no conflict', function () {
- const testMetadata = { baz: 1 }
- runConcurrently(
- Operation.moveFile('foo', 'baz'),
- Operation.setFileMetadata('bar', testMetadata),
- makeTwoFileSnapshot()
- )
- .expectNoTransform()
- .expectFiles({
- bar: { content: 'a', metadata: testMetadata, comments: [] },
- baz: '',
- })
- .expectSymmetry()
- })
- it('transforms MoveFile-SetFileMetadata with set on pathname', function () {
- const testMetadata = { baz: 1 }
- runConcurrently(
- Operation.moveFile('foo', 'bar'),
- Operation.setFileMetadata('foo', testMetadata),
- makeOneFileSnapshot()
- )
- .expectFiles({
- bar: { content: '', metadata: testMetadata, comments: [] },
- })
- .expectSymmetry()
- })
- it('transforms MoveFile-SetFileMetadata w/ set on new pathname', function () {
- const testMetadata = { baz: 1 }
- runConcurrently(
- Operation.moveFile('foo', 'bar'),
- Operation.setFileMetadata('bar', testMetadata),
- makeTwoFileSnapshot()
- )
- // move wins
- .expectFiles({ bar: { content: '', metadata: {}, comments: [] } })
- .expectSymmetry()
- })
- it('transforms MoveFile-SetFileMetadata with no-op move', function () {
- const testMetadata = { baz: 1 }
- runConcurrently(
- Operation.moveFile('foo', 'foo'),
- Operation.setFileMetadata('foo', testMetadata),
- makeOneFileSnapshot()
- )
- .expectFiles({
- foo: { content: '', metadata: testMetadata, comments: [] },
- })
- .expectSymmetry()
- })
- it('transforms EditFile-EditFile with no conflict', function () {
- runConcurrently(
- edit('foo', ['x']),
- edit('bar', [1, 'x']),
- makeTwoFileSnapshot()
- )
- .expectFiles({ bar: 'ax', foo: 'x' })
- .expectNoTransform()
- .expectSymmetry()
- })
- it('transforms EditFile-EditFile on same file', function () {
- runConcurrently(
- edit('foo', ['x']),
- edit('foo', ['y']),
- makeOneFileSnapshot()
- )
- .expectFiles({ foo: 'xy' })
- .expectPrime(1, EditFileOperation)
- .expectPrime(0, EditFileOperation)
- .tap(function () {
- expect(this.primeOperations[1].getOperation().toJSON()).to.eql({
- textOperation: [1, 'y'],
- })
- expect(this.primeOperations[0].getOperation().toJSON()).to.eql({
- textOperation: ['x', 1],
- })
- })
- .swap()
- .expectFiles({ foo: 'yx' })
- })
- it('transforms EditFile-RemoveFile with no conflict', function () {
- runConcurrently(
- edit('foo', ['x']),
- Operation.removeFile('bar'),
- makeTwoFileSnapshot()
- )
- .expectFiles({ foo: 'x' })
- .expectNoTransform()
- .expectSymmetry()
- })
- it('transforms EditFile-RemoveFile on same file', function () {
- runConcurrently(
- edit('foo', ['x']),
- Operation.removeFile('foo'),
- makeOneFileSnapshot()
- )
- .expectFiles({})
- .expectSymmetry()
- })
- it('transforms EditFile-SetFileMetadata with no conflict', function () {
- const testMetadata = { baz: 1 }
- runConcurrently(
- edit('foo', ['x']),
- Operation.setFileMetadata('bar', testMetadata),
- makeTwoFileSnapshot()
- )
- .expectNoTransform()
- .expectFiles({
- foo: { content: 'x', metadata: {}, comments: [] },
- bar: { content: 'a', metadata: testMetadata, comments: [] },
- })
- .expectSymmetry()
- })
- it('transforms EditFile-SetFileMetadata on same file', function () {
- const testMetadata = { baz: 1 }
- runConcurrently(
- edit('foo', ['x']),
- Operation.setFileMetadata('foo', testMetadata),
- makeOneFileSnapshot()
- )
- .expectNoTransform()
- .expectFiles({
- foo: { content: 'x', metadata: testMetadata, comments: [] },
- })
- .expectSymmetry()
- })
- it('transforms SetFileMetadata-SetFileMetadata w/ no conflict', function () {
- runConcurrently(
- Operation.setFileMetadata('foo', { baz: 1 }),
- Operation.setFileMetadata('bar', { baz: 2 }),
- makeTwoFileSnapshot()
- )
- .expectNoTransform()
- .expectFiles({
- foo: { content: '', metadata: { baz: 1 }, comments: [] },
- bar: { content: 'a', metadata: { baz: 2 }, comments: [] },
- })
- .expectSymmetry()
- })
- it('transforms SetFileMetadata-SetFileMetadata on same file', function () {
- runConcurrently(
- Operation.setFileMetadata('foo', { baz: 1 }),
- Operation.setFileMetadata('foo', { baz: 2 }),
- makeOneFileSnapshot()
- )
- // second op wins
- .expectFiles({ foo: { content: '', metadata: { baz: 2 }, comments: [] } })
- .swap()
- // first op wins
- .expectFiles({ foo: { content: '', metadata: { baz: 1 }, comments: [] } })
- })
- it('transforms SetFileMetadata-RemoveFile with no conflict', function () {
- const testMetadata = { baz: 1 }
- runConcurrently(
- Operation.setFileMetadata('foo', testMetadata),
- Operation.removeFile('bar'),
- makeTwoFileSnapshot()
- )
- .expectNoTransform()
- .expectFiles({
- foo: { content: '', metadata: testMetadata, comments: [] },
- })
- .expectSymmetry()
- })
- it('transforms SetFileMetadata-RemoveFile on same file', function () {
- const testMetadata = { baz: 1 }
- runConcurrently(
- Operation.setFileMetadata('foo', testMetadata),
- Operation.removeFile('foo'),
- makeOneFileSnapshot()
- )
- .expectFiles({})
- .expectSymmetry()
- })
- it('transforms no-op with other operation', function () {
- runConcurrently(Operation.NO_OP, addFile('foo', 'test')).expectFiles({
- foo: 'test',
- })
- })
- describe('EditFile sub operations', function () {
- it('transforms AddCommentOperation-AddCommentOperation', function () {
- runConcurrently(
- Operation.editFile(
- 'foo',
- AddCommentOperation.fromJSON({
- commentId: '1',
- ranges: [
- {
- pos: 10,
- length: 2,
- },
- ],
- })
- ),
- Operation.editFile(
- 'foo',
- AddCommentOperation.fromJSON({
- commentId: '1',
- ranges: [
- {
- pos: 0,
- length: 1,
- },
- ],
- })
- ),
- makeOneFileSnapshot()
- )
- .expectTransform()
- .expectFiles({
- foo: {
- content: '',
- metadata: {},
- comments: [
- {
- id: '1',
- ranges: [
- {
- pos: 0,
- length: 1,
- },
- ],
- resolved: false,
- },
- ],
- },
- })
- })
- it('transforms TextOperation-AddCommentOperation', function () {
- runConcurrently(
- Operation.editFile(
- 'foo',
- TextOperation.fromJSON({ textOperation: ['xyz'] })
- ),
- Operation.editFile(
- 'foo',
- AddCommentOperation.fromJSON({
- commentId: '1',
- ranges: [
- {
- pos: 0,
- length: 1,
- },
- ],
- })
- ),
- makeOneFileSnapshot()
- )
- .expectTransform()
- .expectFiles({
- foo: {
- content: 'xyz',
- metadata: {},
- comments: [
- { id: '1', ranges: [{ pos: 3, length: 1 }], resolved: false },
- ],
- },
- })
- .expectSymmetry()
- })
- it('transforms TextOperation-AddCommentOperation (insert with commentId)', function () {
- runConcurrently(
- Operation.editFile(
- 'foo',
- TextOperation.fromJSON({
- textOperation: [{ i: 'xyz', commentIds: ['1'] }],
- })
- ),
- Operation.editFile(
- 'foo',
- AddCommentOperation.fromJSON({
- commentId: '1',
- ranges: [
- {
- pos: 0,
- length: 1,
- },
- ],
- })
- ),
- makeOneFileSnapshot()
- )
- .expectTransform()
- .expectFiles({
- foo: {
- content: 'xyz',
- metadata: {},
- comments: [
- { id: '1', ranges: [{ pos: 0, length: 4 }], resolved: false },
- ],
- },
- })
- .expectSymmetry()
- })
- it('transforms AddCommentOperation-SetCommentStateOperation', function () {
- runConcurrently(
- Operation.editFile(
- 'foo',
- AddCommentOperation.fromJSON({
- commentId: '1',
- ranges: [{ pos: 1, length: 2 }],
- })
- ),
- Operation.editFile(
- 'foo',
- SetCommentStateOperation.fromJSON({
- commentId: '1',
- resolved: true,
- })
- ),
- makeOneFileSnapshot()
- )
- .expectTransform()
- .expectFiles({
- foo: {
- content: '',
- metadata: {},
- comments: [
- { id: '1', ranges: [{ pos: 1, length: 2 }], resolved: true },
- ],
- },
- })
- .expectSymmetry()
- })
- it('transforms AddCommentOperation-DeleteCommentOperation ', function () {
- runConcurrently(
- Operation.editFile(
- 'foo',
- AddCommentOperation.fromJSON({
- commentId: '1',
- ranges: [{ pos: 1, length: 2 }],
- })
- ),
- Operation.editFile(
- 'foo',
- DeleteCommentOperation.fromJSON({ deleteComment: '1' })
- ),
- makeOneFileSnapshot()
- )
- .expectTransform()
- .expectFiles({
- foo: {
- content: '',
- metadata: {},
- comments: [],
- },
- })
- .expectSymmetry()
- })
- it('transforms DeleteCommentOperation-SetCommentStateOperation ', function () {
- runConcurrently(
- Operation.editFile(
- 'foo',
- DeleteCommentOperation.fromJSON({ deleteComment: '1' })
- ),
- Operation.editFile(
- 'foo',
- SetCommentStateOperation.fromJSON({
- commentId: '1',
- resolved: true,
- })
- ),
- makeOneFileSnapshot()
- )
- .expectTransform()
- .expectFiles({
- foo: {
- content: '',
- metadata: {},
- comments: [],
- },
- })
- .expectSymmetry()
- })
- it('transforms DeleteCommentOperation-DeleteCommentOperation ', function () {
- runConcurrently(
- Operation.editFile(
- 'foo',
- DeleteCommentOperation.fromJSON({ deleteComment: '1' })
- ),
- Operation.editFile(
- 'foo',
- DeleteCommentOperation.fromJSON({ deleteComment: '1' })
- ),
- makeOneFileSnapshot()
- )
- .expectTransform()
- .expectFiles({
- foo: {
- content: '',
- metadata: {},
- comments: [],
- },
- })
- .expectSymmetry()
- })
- it('transforms SetCommentStateOperation-SetCommentStateOperation to resolved comment', function () {
- const snapshot = makeEmptySnapshot()
- const file = new File(
- new StringFileData('xyz', [
- { id: '1', ranges: [{ pos: 0, length: 3 }] },
- ])
- )
- snapshot.addFile('foo', file)
- runConcurrently(
- Operation.editFile(
- 'foo',
- SetCommentStateOperation.fromJSON({
- commentId: '1',
- resolved: true,
- })
- ),
- Operation.editFile(
- 'foo',
- SetCommentStateOperation.fromJSON({
- commentId: '1',
- resolved: true,
- })
- ),
- snapshot
- )
- .expectTransform()
- .expectFiles({
- foo: {
- content: 'xyz',
- metadata: {},
- comments: [
- { id: '1', ranges: [{ pos: 0, length: 3 }], resolved: true },
- ],
- },
- })
- .expectSymmetry()
- })
- it('transforms SetCommentStateOperation-SetCommentStateOperation to unresolved comment', function () {
- const snapshot = makeEmptySnapshot()
- const file = new File(
- new StringFileData('xyz', [
- { id: '1', ranges: [{ pos: 0, length: 3 }] },
- ])
- )
- snapshot.addFile('foo', file)
- runConcurrently(
- Operation.editFile(
- 'foo',
- SetCommentStateOperation.fromJSON({
- commentId: '1',
- resolved: true,
- })
- ),
- Operation.editFile(
- 'foo',
- SetCommentStateOperation.fromJSON({
- commentId: '1',
- resolved: false,
- })
- ),
- snapshot
- )
- .expectTransform()
- .expectFiles({
- foo: {
- content: 'xyz',
- metadata: {},
- comments: [
- { id: '1', ranges: [{ pos: 0, length: 3 }], resolved: false },
- ],
- },
- })
- .expectSymmetry()
- })
- })
- })
|