UpdateTranslatorTests.js 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  1. import sinon from 'sinon'
  2. import { expect } from 'chai'
  3. import { strict as esmock } from 'esmock'
  4. import Core from 'overleaf-editor-core'
  5. const MODULE_PATH = '../../../../app/js/UpdateTranslator.js'
  6. describe('UpdateTranslator', function () {
  7. beforeEach(async function () {
  8. this.UpdateTranslator = await esmock(MODULE_PATH, {
  9. 'overleaf-editor-core': Core,
  10. })
  11. this.callback = sinon.stub()
  12. this.project_id = '59bfd450e3028c4d40a1e9aa'
  13. this.doc_id = '59bfd450e3028c4d40a1e9ab'
  14. this.file_id = '59bfd450e3028c4d40a1easd'
  15. this.user_id = '59bb9051abf6e8682a269b64'
  16. this.version = 0
  17. this.timestamp = new Date().toJSON()
  18. this.mockBlobHash = '12345abc12345abc12345abc12345abc12345abc'
  19. })
  20. describe('convertToChanges', function () {
  21. it('can translate doc additions', function () {
  22. const updates = [
  23. {
  24. update: {
  25. doc: this.doc_id,
  26. pathname: '/main.tex',
  27. docLines: 'a\nb',
  28. meta: {
  29. user_id: this.user_id,
  30. ts: this.timestamp,
  31. },
  32. },
  33. blobHashes: { file: this.mockBlobHash },
  34. },
  35. ]
  36. const changes = this.UpdateTranslator.convertToChanges(
  37. this.project_id,
  38. updates
  39. ).map(change => change.toRaw())
  40. expect(changes).to.deep.equal([
  41. {
  42. authors: [],
  43. operations: [
  44. {
  45. pathname: 'main.tex',
  46. file: {
  47. hash: this.mockBlobHash,
  48. },
  49. },
  50. ],
  51. v2Authors: [this.user_id],
  52. timestamp: this.timestamp,
  53. },
  54. ])
  55. })
  56. it('can translate file additions', function () {
  57. const updates = [
  58. {
  59. update: {
  60. file: this.file_id,
  61. pathname: '/test.png',
  62. url: 'filestore.example.com/test.png',
  63. meta: {
  64. user_id: this.user_id,
  65. ts: this.timestamp,
  66. },
  67. },
  68. blobHashes: { file: this.mockBlobHash },
  69. },
  70. ]
  71. const changes = this.UpdateTranslator.convertToChanges(
  72. this.project_id,
  73. updates
  74. ).map(change => change.toRaw())
  75. expect(changes).to.deep.equal([
  76. {
  77. authors: [],
  78. operations: [
  79. {
  80. pathname: 'test.png',
  81. file: {
  82. hash: this.mockBlobHash,
  83. },
  84. },
  85. ],
  86. v2Authors: [this.user_id],
  87. timestamp: this.timestamp,
  88. },
  89. ])
  90. })
  91. it('can translate doc renames', function () {
  92. const updates = [
  93. {
  94. update: {
  95. doc: this.doc_id,
  96. pathname: '/main.tex',
  97. new_pathname: '/new_main.tex',
  98. meta: {
  99. user_id: this.user_id,
  100. ts: this.timestamp,
  101. },
  102. },
  103. },
  104. ]
  105. const changes = this.UpdateTranslator.convertToChanges(
  106. this.project_id,
  107. updates
  108. ).map(change => change.toRaw())
  109. expect(changes).to.deep.equal([
  110. {
  111. authors: [],
  112. operations: [
  113. {
  114. pathname: 'main.tex',
  115. newPathname: 'new_main.tex',
  116. },
  117. ],
  118. v2Authors: [this.user_id],
  119. timestamp: this.timestamp,
  120. },
  121. ])
  122. })
  123. it('can translate file renames', function () {
  124. const updates = [
  125. {
  126. update: {
  127. file: this.file_id,
  128. pathname: '/test.png',
  129. new_pathname: '/new_test.png',
  130. meta: {
  131. user_id: this.user_id,
  132. ts: this.timestamp,
  133. },
  134. },
  135. },
  136. ]
  137. const changes = this.UpdateTranslator.convertToChanges(
  138. this.project_id,
  139. updates
  140. ).map(change => change.toRaw())
  141. expect(changes).to.deep.equal([
  142. {
  143. authors: [],
  144. operations: [
  145. {
  146. pathname: 'test.png',
  147. newPathname: 'new_test.png',
  148. },
  149. ],
  150. v2Authors: [this.user_id],
  151. timestamp: this.timestamp,
  152. },
  153. ])
  154. })
  155. it('can translate multiple updates with the correct versions', function () {
  156. const updates = [
  157. {
  158. update: {
  159. doc: this.doc_id,
  160. pathname: '/main.tex',
  161. docLines: 'a\nb',
  162. meta: {
  163. user_id: this.user_id,
  164. ts: this.timestamp,
  165. },
  166. },
  167. blobHashes: { file: this.mockBlobHash },
  168. },
  169. {
  170. update: {
  171. file: this.file_id,
  172. pathname: '/test.png',
  173. url: 'filestore.example.com/test.png',
  174. meta: {
  175. user_id: this.user_id,
  176. ts: this.timestamp,
  177. },
  178. },
  179. blobHashes: { file: this.mockBlobHash },
  180. },
  181. ]
  182. const changes = this.UpdateTranslator.convertToChanges(
  183. this.project_id,
  184. updates
  185. ).map(change => change.toRaw())
  186. expect(changes).to.deep.equal([
  187. {
  188. authors: [],
  189. operations: [
  190. {
  191. pathname: 'main.tex',
  192. file: {
  193. hash: this.mockBlobHash,
  194. },
  195. },
  196. ],
  197. v2Authors: [this.user_id],
  198. timestamp: this.timestamp,
  199. },
  200. {
  201. authors: [],
  202. operations: [
  203. {
  204. pathname: 'test.png',
  205. file: {
  206. hash: this.mockBlobHash,
  207. },
  208. },
  209. ],
  210. v2Authors: [this.user_id],
  211. timestamp: this.timestamp,
  212. },
  213. ])
  214. })
  215. it('returns an error if the update has an unknown format', function () {
  216. const updates = [
  217. {
  218. update: {
  219. foo: 'bar',
  220. },
  221. },
  222. ]
  223. expect(() =>
  224. this.UpdateTranslator.convertToChanges(this.project_id, updates)
  225. ).to.throw('update with unknown format')
  226. })
  227. it('replaces backslashes with underscores in pathnames', function () {
  228. const updates = [
  229. {
  230. update: {
  231. doc: this.doc_id,
  232. pathname: '/\\main\\foo.tex',
  233. new_pathname: '/\\new_main\\foo\\bar.tex',
  234. meta: {
  235. user_id: this.user_id,
  236. ts: this.timestamp,
  237. },
  238. },
  239. },
  240. ]
  241. const changes = this.UpdateTranslator.convertToChanges(
  242. this.project_id,
  243. updates
  244. ).map(change => change.toRaw())
  245. expect(changes).to.deep.equal([
  246. {
  247. authors: [],
  248. operations: [
  249. {
  250. pathname: '_main_foo.tex',
  251. newPathname: '_new_main_foo_bar.tex',
  252. },
  253. ],
  254. v2Authors: [this.user_id],
  255. timestamp: this.timestamp,
  256. },
  257. ])
  258. })
  259. it('replaces leading asterisks with __ASTERISK__ in pathnames', function () {
  260. const updates = [
  261. {
  262. update: {
  263. file: this.file_id,
  264. pathname: '/test*test.png',
  265. meta: {
  266. user_id: this.user_id,
  267. ts: this.timestamp,
  268. },
  269. url: 'filestore.example.com/test*test.png',
  270. },
  271. blobHashes: { file: this.mockBlobHash },
  272. },
  273. ]
  274. const changes = this.UpdateTranslator.convertToChanges(
  275. this.project_id,
  276. updates
  277. ).map(change => change.toRaw())
  278. expect(changes).to.deep.equal([
  279. {
  280. authors: [],
  281. operations: [
  282. {
  283. pathname: 'test__ASTERISK__test.png',
  284. file: {
  285. hash: this.mockBlobHash,
  286. },
  287. },
  288. ],
  289. v2Authors: [this.user_id],
  290. timestamp: this.timestamp,
  291. },
  292. ])
  293. })
  294. it('replaces a leading space for top-level files with __SPACE__', function () {
  295. const updates = [
  296. {
  297. update: {
  298. file: this.file_id,
  299. pathname: '/ test.png',
  300. meta: {
  301. user_id: this.user_id,
  302. ts: this.timestamp,
  303. },
  304. url: 'filestore.example.com/test.png',
  305. },
  306. blobHashes: { file: this.mockBlobHash },
  307. },
  308. ]
  309. const changes = this.UpdateTranslator.convertToChanges(
  310. this.project_id,
  311. updates
  312. ).map(change => change.toRaw())
  313. expect(changes).to.deep.equal([
  314. {
  315. authors: [],
  316. operations: [
  317. {
  318. pathname: '__SPACE__test.png',
  319. file: {
  320. hash: this.mockBlobHash,
  321. },
  322. },
  323. ],
  324. v2Authors: [this.user_id],
  325. timestamp: this.timestamp,
  326. },
  327. ])
  328. })
  329. it('replaces leading spaces of files in subfolders with __SPACE__', function () {
  330. const updates = [
  331. {
  332. update: {
  333. file: this.file_id,
  334. pathname: '/folder/ test.png',
  335. meta: {
  336. user_id: this.user_id,
  337. ts: this.timestamp,
  338. },
  339. url: 'filestore.example.com/folder/test.png',
  340. },
  341. blobHashes: { file: this.mockBlobHash },
  342. },
  343. ]
  344. const changes = this.UpdateTranslator.convertToChanges(
  345. this.project_id,
  346. updates
  347. ).map(change => change.toRaw())
  348. expect(changes).to.deep.equal([
  349. {
  350. authors: [],
  351. operations: [
  352. {
  353. pathname: 'folder/__SPACE__test.png',
  354. file: {
  355. hash: this.mockBlobHash,
  356. },
  357. },
  358. ],
  359. v2Authors: [this.user_id],
  360. timestamp: this.timestamp,
  361. },
  362. ])
  363. })
  364. it('sets a null author when user_id is "anonymous-user"', function () {
  365. const updates = [
  366. {
  367. update: {
  368. doc: this.doc_id,
  369. pathname: '/main.tex',
  370. docLines: 'a\nb',
  371. meta: {
  372. user_id: 'anonymous-user',
  373. ts: this.timestamp,
  374. },
  375. },
  376. blobHashes: { file: this.mockBlobHash },
  377. },
  378. ]
  379. const changes = this.UpdateTranslator.convertToChanges(
  380. this.project_id,
  381. updates
  382. ).map(change => change.toRaw())
  383. expect(changes).to.deep.equal([
  384. {
  385. authors: [],
  386. operations: [
  387. {
  388. pathname: 'main.tex',
  389. file: {
  390. hash: this.mockBlobHash,
  391. },
  392. },
  393. ],
  394. v2Authors: [null],
  395. timestamp: this.timestamp,
  396. },
  397. ])
  398. })
  399. it('sets an empty array as author when there is no meta.user_id', function () {
  400. const updates = [
  401. {
  402. update: {
  403. doc: this.doc_id,
  404. pathname: '/main.tex',
  405. docLines: 'a\nb',
  406. meta: {
  407. ts: this.timestamp,
  408. },
  409. },
  410. blobHashes: { file: this.mockBlobHash },
  411. },
  412. ]
  413. const changes = this.UpdateTranslator.convertToChanges(
  414. this.project_id,
  415. updates
  416. ).map(change => change.toRaw())
  417. expect(changes).to.deep.equal([
  418. {
  419. authors: [],
  420. operations: [
  421. {
  422. pathname: 'main.tex',
  423. file: {
  424. hash: this.mockBlobHash,
  425. },
  426. },
  427. ],
  428. v2Authors: [],
  429. timestamp: this.timestamp,
  430. },
  431. ])
  432. })
  433. describe('text updates', function () {
  434. it('can translate insertions', function () {
  435. const updates = [
  436. {
  437. update: {
  438. doc: this.doc_id,
  439. op: [
  440. { p: 3, i: 'foo' },
  441. { p: 15, i: 'bar', commentIds: ['comment1'] },
  442. ],
  443. v: this.version,
  444. meta: {
  445. user_id: this.user_id,
  446. ts: new Date(this.timestamp).getTime(),
  447. pathname: '/main.tex',
  448. doc_length: 20,
  449. source: 'some-editor-id',
  450. },
  451. },
  452. },
  453. ]
  454. const changes = this.UpdateTranslator.convertToChanges(
  455. this.project_id,
  456. updates
  457. ).map(change => change.toRaw())
  458. expect(changes).to.deep.equal([
  459. {
  460. authors: [],
  461. operations: [
  462. {
  463. pathname: 'main.tex',
  464. textOperation: [
  465. 3,
  466. 'foo',
  467. 9,
  468. { i: 'bar', commentIds: ['comment1'] },
  469. 8,
  470. ],
  471. },
  472. ],
  473. v2Authors: [this.user_id],
  474. timestamp: this.timestamp,
  475. v2DocVersions: {
  476. '59bfd450e3028c4d40a1e9ab': {
  477. pathname: 'main.tex',
  478. v: 0,
  479. },
  480. },
  481. },
  482. ])
  483. })
  484. it('can translate deletions', function () {
  485. const updates = [
  486. {
  487. update: {
  488. doc: this.doc_id,
  489. op: [
  490. { p: 3, d: 'lo' },
  491. { p: 10, d: 'bar' },
  492. ],
  493. v: this.version,
  494. meta: {
  495. user_id: this.user_id,
  496. ts: new Date(this.timestamp).getTime(),
  497. pathname: '/main.tex',
  498. doc_length: 20,
  499. },
  500. },
  501. },
  502. ]
  503. const changes = this.UpdateTranslator.convertToChanges(
  504. this.project_id,
  505. updates
  506. ).map(change => change.toRaw())
  507. expect(changes).to.deep.equal([
  508. {
  509. authors: [],
  510. operations: [
  511. {
  512. pathname: 'main.tex',
  513. textOperation: [3, -2, 7, -3, 5],
  514. },
  515. ],
  516. v2Authors: [this.user_id],
  517. timestamp: this.timestamp,
  518. v2DocVersions: {
  519. '59bfd450e3028c4d40a1e9ab': {
  520. pathname: 'main.tex',
  521. v: 0,
  522. },
  523. },
  524. },
  525. ])
  526. })
  527. it('should translate retains without tracking data', function () {
  528. const updates = [
  529. {
  530. update: {
  531. doc: this.doc_id,
  532. op: [
  533. {
  534. p: 3,
  535. r: 'lo',
  536. },
  537. ],
  538. v: this.version,
  539. meta: {
  540. user_id: this.user_id,
  541. ts: new Date(this.timestamp).getTime(),
  542. pathname: '/main.tex',
  543. doc_length: 20,
  544. },
  545. },
  546. },
  547. ]
  548. const changes = this.UpdateTranslator.convertToChanges(
  549. this.project_id,
  550. updates
  551. ).map(change => change.toRaw())
  552. expect(changes).to.deep.equal([
  553. {
  554. authors: [],
  555. operations: [
  556. {
  557. pathname: 'main.tex',
  558. textOperation: [20],
  559. },
  560. ],
  561. v2Authors: [this.user_id],
  562. timestamp: this.timestamp,
  563. v2DocVersions: {
  564. [this.doc_id]: {
  565. pathname: 'main.tex',
  566. v: 0,
  567. },
  568. },
  569. },
  570. ])
  571. })
  572. it('can translate retains with tracking data', function () {
  573. const updates = [
  574. {
  575. update: {
  576. doc: this.doc_id,
  577. op: [
  578. {
  579. p: 3,
  580. r: 'lo',
  581. tracking: { type: 'none' },
  582. },
  583. ],
  584. v: this.version,
  585. meta: {
  586. user_id: this.user_id,
  587. ts: new Date(this.timestamp).getTime(),
  588. pathname: '/main.tex',
  589. doc_length: 20,
  590. },
  591. },
  592. },
  593. ]
  594. const changes = this.UpdateTranslator.convertToChanges(
  595. this.project_id,
  596. updates
  597. ).map(change => change.toRaw())
  598. expect(changes).to.deep.equal([
  599. {
  600. authors: [],
  601. operations: [
  602. {
  603. pathname: 'main.tex',
  604. textOperation: [
  605. 3,
  606. {
  607. r: 2,
  608. tracking: { type: 'none' },
  609. },
  610. 15,
  611. ],
  612. },
  613. ],
  614. v2Authors: [this.user_id],
  615. timestamp: this.timestamp,
  616. v2DocVersions: {
  617. '59bfd450e3028c4d40a1e9ab': {
  618. pathname: 'main.tex',
  619. v: 0,
  620. },
  621. },
  622. },
  623. ])
  624. })
  625. it('drops zero-length retains carrying tracking data', function () {
  626. // A zero-length retain (e.g. an empty `r` string produced by the resync
  627. // mechanism) must not be emitted as a zero-length tracked delete.
  628. const updates = [
  629. {
  630. update: {
  631. doc: this.doc_id,
  632. op: [
  633. {
  634. p: 3,
  635. r: '',
  636. tracking: { type: 'delete', userId: this.user_id, ts: 0 },
  637. },
  638. ],
  639. v: this.version,
  640. meta: {
  641. user_id: this.user_id,
  642. ts: new Date(this.timestamp).getTime(),
  643. pathname: '/main.tex',
  644. doc_length: 20,
  645. },
  646. },
  647. },
  648. ]
  649. const changes = this.UpdateTranslator.convertToChanges(
  650. this.project_id,
  651. updates
  652. ).map(change => change.toRaw())
  653. // The empty tracked retain is dropped, leaving a plain retain of the
  654. // whole document.
  655. expect(changes).to.deep.equal([
  656. {
  657. authors: [],
  658. operations: [
  659. {
  660. pathname: 'main.tex',
  661. textOperation: [20],
  662. },
  663. ],
  664. v2Authors: [this.user_id],
  665. timestamp: this.timestamp,
  666. v2DocVersions: {
  667. '59bfd450e3028c4d40a1e9ab': {
  668. pathname: 'main.tex',
  669. v: 0,
  670. },
  671. },
  672. },
  673. ])
  674. })
  675. it('can translate insertions at the start and end (with zero retained)', function () {
  676. const updates = [
  677. {
  678. update: {
  679. doc: this.doc_id,
  680. op: [
  681. { p: 0, i: 'foo' },
  682. { p: 23, i: 'bar' },
  683. { p: 0, d: 'foo' },
  684. { p: 20, d: 'bar' },
  685. ],
  686. v: this.version,
  687. meta: {
  688. user_id: this.user_id,
  689. ts: new Date(this.timestamp).getTime(),
  690. pathname: '/main.tex',
  691. doc_length: 20,
  692. },
  693. },
  694. },
  695. ]
  696. const changes = this.UpdateTranslator.convertToChanges(
  697. this.project_id,
  698. updates
  699. ).map(change => change.toRaw())
  700. expect(changes).to.deep.equal([
  701. {
  702. authors: [],
  703. operations: [
  704. {
  705. pathname: 'main.tex',
  706. textOperation: [20],
  707. },
  708. ],
  709. v2Authors: [this.user_id],
  710. timestamp: this.timestamp,
  711. v2DocVersions: {
  712. '59bfd450e3028c4d40a1e9ab': {
  713. pathname: 'main.tex',
  714. v: 0,
  715. },
  716. },
  717. },
  718. ])
  719. })
  720. it('can handle operations in non-linear offset order', function () {
  721. const updates = [
  722. {
  723. update: {
  724. doc: this.doc_id,
  725. op: [
  726. { p: 15, i: 'foo' },
  727. { p: 3, i: 'bar' },
  728. ],
  729. v: this.version,
  730. meta: {
  731. user_id: this.user_id,
  732. ts: this.timestamp,
  733. pathname: '/main.tex',
  734. doc_length: 20,
  735. },
  736. },
  737. },
  738. ]
  739. const changes = this.UpdateTranslator.convertToChanges(
  740. this.project_id,
  741. updates
  742. ).map(change => change.toRaw())
  743. expect(changes).to.deep.equal([
  744. {
  745. authors: [],
  746. operations: [
  747. {
  748. pathname: 'main.tex',
  749. textOperation: [3, 'bar', 12, 'foo', 5],
  750. },
  751. ],
  752. v2Authors: [this.user_id],
  753. timestamp: this.timestamp,
  754. v2DocVersions: {
  755. '59bfd450e3028c4d40a1e9ab': {
  756. pathname: 'main.tex',
  757. v: 0,
  758. },
  759. },
  760. },
  761. ])
  762. })
  763. it('handles comment ops', function () {
  764. const updates = [
  765. {
  766. update: {
  767. doc: this.doc_id,
  768. op: [
  769. { p: 0, i: 'foo' },
  770. { p: 3, d: 'bar' },
  771. { p: 5, c: 'comment this', t: 'comment-id-1' },
  772. { p: 7, c: 'another comment', t: 'comment-id-2' },
  773. { p: 9, c: '', t: 'comment-id-3' },
  774. { p: 10, i: 'baz' },
  775. ],
  776. v: this.version,
  777. meta: {
  778. user_id: this.user_id,
  779. ts: new Date(this.timestamp).getTime(),
  780. pathname: '/main.tex',
  781. doc_length: 20,
  782. },
  783. },
  784. },
  785. ]
  786. const changes = this.UpdateTranslator.convertToChanges(
  787. this.project_id,
  788. updates
  789. ).map(change => change.toRaw())
  790. expect(changes).to.deep.equal([
  791. {
  792. authors: [],
  793. operations: [
  794. {
  795. pathname: 'main.tex',
  796. textOperation: ['foo', -3, 17],
  797. },
  798. {
  799. pathname: 'main.tex',
  800. commentId: 'comment-id-1',
  801. ranges: [{ pos: 5, length: 12 }],
  802. },
  803. {
  804. pathname: 'main.tex',
  805. commentId: 'comment-id-2',
  806. ranges: [{ pos: 7, length: 15 }],
  807. },
  808. {
  809. pathname: 'main.tex',
  810. commentId: 'comment-id-3',
  811. ranges: [],
  812. },
  813. {
  814. pathname: 'main.tex',
  815. textOperation: [10, 'baz', 10],
  816. },
  817. ],
  818. v2Authors: [this.user_id],
  819. timestamp: this.timestamp,
  820. v2DocVersions: {
  821. '59bfd450e3028c4d40a1e9ab': {
  822. pathname: 'main.tex',
  823. v: 0,
  824. },
  825. },
  826. },
  827. ])
  828. })
  829. it('handles insertions after the end of the document', function () {
  830. const updates = [
  831. {
  832. update: {
  833. doc: this.doc_id,
  834. op: [{ p: 3, i: '\\' }],
  835. v: this.version,
  836. meta: {
  837. user_id: this.user_id,
  838. ts: new Date(this.timestamp).getTime(),
  839. pathname: '/main.tex',
  840. doc_length: 2,
  841. },
  842. },
  843. },
  844. ]
  845. const changes = this.UpdateTranslator.convertToChanges(
  846. this.project_id,
  847. updates
  848. ).map(change => change.toRaw())
  849. expect(changes).to.deep.equal([
  850. {
  851. authors: [],
  852. operations: [
  853. {
  854. pathname: 'main.tex',
  855. textOperation: [2, '\\'],
  856. },
  857. ],
  858. v2Authors: [this.user_id],
  859. timestamp: this.timestamp,
  860. v2DocVersions: {
  861. '59bfd450e3028c4d40a1e9ab': {
  862. pathname: 'main.tex',
  863. v: 0,
  864. },
  865. },
  866. },
  867. ])
  868. })
  869. it('translates external source metadata into an origin', function () {
  870. const updates = [
  871. {
  872. update: {
  873. doc: this.doc_id,
  874. op: [{ p: 3, i: 'foo' }],
  875. v: this.version,
  876. meta: {
  877. user_id: this.user_id,
  878. ts: new Date(this.timestamp).getTime(),
  879. pathname: '/main.tex',
  880. doc_length: 20,
  881. type: 'external',
  882. source: 'dropbox',
  883. },
  884. },
  885. },
  886. ]
  887. const changes = this.UpdateTranslator.convertToChanges(
  888. this.project_id,
  889. updates
  890. ).map(change => change.toRaw())
  891. expect(changes).to.deep.equal([
  892. {
  893. authors: [],
  894. operations: [
  895. {
  896. pathname: 'main.tex',
  897. textOperation: [3, 'foo', 17],
  898. },
  899. ],
  900. v2Authors: [this.user_id],
  901. timestamp: this.timestamp,
  902. v2DocVersions: {
  903. '59bfd450e3028c4d40a1e9ab': {
  904. pathname: 'main.tex',
  905. v: 0,
  906. },
  907. },
  908. origin: { kind: 'dropbox' },
  909. },
  910. ])
  911. })
  912. it('errors on unexpected ops', function () {
  913. const updates = [
  914. {
  915. update: {
  916. doc: this.doc_id,
  917. op: [{ p: 5, z: 'bar' }],
  918. v: this.version,
  919. meta: {
  920. user_id: this.user_id,
  921. ts: new Date(this.timestamp).getTime(),
  922. pathname: '/main.tex',
  923. doc_length: 20,
  924. },
  925. },
  926. },
  927. ]
  928. expect(() => {
  929. this.UpdateTranslator.convertToChanges(this.project_id, updates)
  930. }).to.throw('unexpected op type')
  931. })
  932. })
  933. describe('text updates with history metadata', function () {
  934. it('handles deletes over tracked deletes', function () {
  935. const updates = [
  936. {
  937. update: {
  938. doc: this.doc_id,
  939. op: [
  940. { i: 'foo', p: 3, hpos: 5 },
  941. {
  942. d: 'quux',
  943. p: 10,
  944. hpos: 15,
  945. trackedChanges: [
  946. { type: 'delete', offset: 2, length: 3 },
  947. { type: 'delete', offset: 3, length: 1 },
  948. ],
  949. },
  950. { c: 'noteworthy', p: 8, t: 'comment-id', hpos: 11, hlen: 14 },
  951. ],
  952. v: this.version,
  953. meta: {
  954. user_id: this.user_id,
  955. ts: new Date(this.timestamp).getTime(),
  956. pathname: '/main.tex',
  957. doc_length: 20,
  958. history_doc_length: 30,
  959. source: 'some-editor-id',
  960. },
  961. },
  962. },
  963. ]
  964. const changes = this.UpdateTranslator.convertToChanges(
  965. this.project_id,
  966. updates
  967. ).map(change => change.toRaw())
  968. expect(changes).to.deep.equal([
  969. {
  970. authors: [],
  971. operations: [
  972. {
  973. pathname: 'main.tex',
  974. textOperation: [5, 'foo', 7, -2, 3, -1, 1, -1, 10],
  975. },
  976. {
  977. pathname: 'main.tex',
  978. commentId: 'comment-id',
  979. ranges: [{ pos: 11, length: 14 }],
  980. },
  981. ],
  982. v2Authors: [this.user_id],
  983. timestamp: this.timestamp,
  984. v2DocVersions: {
  985. '59bfd450e3028c4d40a1e9ab': {
  986. pathname: 'main.tex',
  987. v: 0,
  988. },
  989. },
  990. },
  991. ])
  992. })
  993. it('handles tracked delete rejections specially', function () {
  994. const updates = [
  995. {
  996. update: {
  997. doc: this.doc_id,
  998. op: [{ i: 'foo', p: 3, trackedDeleteRejection: true }],
  999. v: this.version,
  1000. meta: {
  1001. user_id: this.user_id,
  1002. ts: new Date(this.timestamp).getTime(),
  1003. pathname: '/main.tex',
  1004. doc_length: 20,
  1005. source: 'some-editor-id',
  1006. },
  1007. },
  1008. },
  1009. ]
  1010. const changes = this.UpdateTranslator.convertToChanges(
  1011. this.project_id,
  1012. updates
  1013. ).map(change => change.toRaw())
  1014. expect(changes).to.deep.equal([
  1015. {
  1016. authors: [],
  1017. operations: [
  1018. {
  1019. pathname: 'main.tex',
  1020. textOperation: [
  1021. 3,
  1022. {
  1023. r: 3,
  1024. tracking: { type: 'none' },
  1025. },
  1026. 14,
  1027. ],
  1028. },
  1029. ],
  1030. v2Authors: [this.user_id],
  1031. timestamp: this.timestamp,
  1032. v2DocVersions: {
  1033. '59bfd450e3028c4d40a1e9ab': {
  1034. pathname: 'main.tex',
  1035. v: 0,
  1036. },
  1037. },
  1038. },
  1039. ])
  1040. })
  1041. it('handles tracked changes', function () {
  1042. const updates = [
  1043. {
  1044. update: {
  1045. doc: this.doc_id,
  1046. op: [
  1047. { i: 'inserted', p: 5 },
  1048. { d: 'deleted', p: 20 },
  1049. { i: 'rejected deletion', p: 30, trackedDeleteRejection: true },
  1050. {
  1051. d: 'rejected insertion',
  1052. p: 50,
  1053. trackedChanges: [{ type: 'insert', offset: 0, length: 18 }],
  1054. },
  1055. ],
  1056. v: this.version,
  1057. meta: {
  1058. tc: 'tracked-change-id',
  1059. user_id: this.user_id,
  1060. ts: new Date(this.timestamp).getTime(),
  1061. pathname: '/main.tex',
  1062. doc_length: 70,
  1063. source: 'some-editor-id',
  1064. },
  1065. },
  1066. },
  1067. ]
  1068. const changes = this.UpdateTranslator.convertToChanges(
  1069. this.project_id,
  1070. updates
  1071. ).map(change => change.toRaw())
  1072. expect(changes).to.deep.equal([
  1073. {
  1074. authors: [],
  1075. operations: [
  1076. {
  1077. pathname: 'main.tex',
  1078. textOperation: [
  1079. 5,
  1080. {
  1081. i: 'inserted',
  1082. tracking: {
  1083. type: 'insert',
  1084. userId: this.user_id,
  1085. ts: new Date(this.timestamp).toISOString(),
  1086. },
  1087. },
  1088. 7,
  1089. {
  1090. r: 7,
  1091. tracking: {
  1092. type: 'delete',
  1093. userId: this.user_id,
  1094. ts: new Date(this.timestamp).toISOString(),
  1095. },
  1096. },
  1097. 3,
  1098. {
  1099. r: 17,
  1100. tracking: { type: 'none' },
  1101. },
  1102. 3,
  1103. -18,
  1104. 10,
  1105. ],
  1106. },
  1107. ],
  1108. v2Authors: [this.user_id],
  1109. timestamp: this.timestamp,
  1110. v2DocVersions: {
  1111. '59bfd450e3028c4d40a1e9ab': {
  1112. pathname: 'main.tex',
  1113. v: 0,
  1114. },
  1115. },
  1116. },
  1117. ])
  1118. })
  1119. it('handles a delete over a mix of tracked inserts and tracked deletes', function () {
  1120. const updates = [
  1121. {
  1122. update: {
  1123. doc: this.doc_id,
  1124. op: [
  1125. {
  1126. d: 'abcdef',
  1127. p: 10,
  1128. trackedChanges: [
  1129. { type: 'insert', offset: 0, length: 3 },
  1130. { type: 'delete', offset: 2, length: 10 },
  1131. { type: 'insert', offset: 2, length: 2 },
  1132. ],
  1133. },
  1134. ],
  1135. v: this.version,
  1136. meta: {
  1137. tc: 'tracking-id',
  1138. user_id: this.user_id,
  1139. ts: new Date(this.timestamp).getTime(),
  1140. pathname: '/main.tex',
  1141. doc_length: 20,
  1142. history_doc_length: 30,
  1143. source: 'some-editor-id',
  1144. },
  1145. },
  1146. },
  1147. ]
  1148. const changes = this.UpdateTranslator.convertToChanges(
  1149. this.project_id,
  1150. updates
  1151. ).map(change => change.toRaw())
  1152. expect(changes).to.deep.equal([
  1153. {
  1154. authors: [],
  1155. operations: [
  1156. {
  1157. pathname: 'main.tex',
  1158. textOperation: [
  1159. 10,
  1160. -3,
  1161. 10,
  1162. -2,
  1163. {
  1164. r: 1,
  1165. tracking: {
  1166. type: 'delete',
  1167. userId: this.user_id,
  1168. ts: this.timestamp,
  1169. },
  1170. },
  1171. 4,
  1172. ],
  1173. },
  1174. ],
  1175. v2Authors: [this.user_id],
  1176. timestamp: this.timestamp,
  1177. v2DocVersions: {
  1178. '59bfd450e3028c4d40a1e9ab': {
  1179. pathname: 'main.tex',
  1180. v: 0,
  1181. },
  1182. },
  1183. },
  1184. ])
  1185. })
  1186. })
  1187. })
  1188. })