ChunkTranslatorTests.js 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743
  1. import sinon from 'sinon'
  2. import { expect } from 'chai'
  3. import { strict as esmock } from 'esmock'
  4. const MODULE_PATH = '../../../../app/js/ChunkTranslator.js'
  5. describe('ChunkTranslator', function () {
  6. beforeEach(async function () {
  7. this.projectId = '0123456789abc0123456789abc'
  8. this.historyId = 12345
  9. this.author1 = {
  10. id: 1,
  11. email: 'james.allen@overleaf.com',
  12. name: 'James Allen',
  13. }
  14. this.date = new Date()
  15. this.fileHash = 'some_hash'
  16. this.fileContents = 'Hello world, this is a test'
  17. this.HistoryStoreManager = {
  18. getProjectBlob: sinon.stub(),
  19. }
  20. this.HistoryStoreManager.getProjectBlob
  21. .withArgs(this.historyId, this.fileHash)
  22. .yields(null, this.fileContents)
  23. this.WebApiManager = {
  24. getHistoryId: sinon.stub().callsFake((projectId, cb) => {
  25. console.log({ projectId })
  26. }),
  27. }
  28. this.WebApiManager.getHistoryId
  29. .withArgs(this.projectId)
  30. .yields(null, this.historyId)
  31. this.ChunkTranslator = await esmock(MODULE_PATH, {
  32. '../../../../app/js/HistoryStoreManager.js': this.HistoryStoreManager,
  33. '../../../../app/js/WebApiManager.js': this.WebApiManager,
  34. })
  35. this.callback = sinon.stub()
  36. })
  37. describe('with changes to the text', function () {
  38. beforeEach(function () {
  39. this.chunk = {
  40. project_id: this.projectId,
  41. chunk: {
  42. startVersion: 0,
  43. history: {
  44. snapshot: {
  45. files: {
  46. 'main.tex': {
  47. hash: this.fileHash,
  48. stringLength: 42,
  49. },
  50. },
  51. },
  52. changes: [
  53. {
  54. operations: [
  55. { pathname: 'main.tex', textOperation: ['Hello test, ', -6] },
  56. ],
  57. timestamp: this.date.toISOString(),
  58. authors: [this.author1.id],
  59. },
  60. {
  61. operations: [
  62. { pathname: 'main.tex', textOperation: [6, 'foo '] },
  63. ],
  64. timestamp: this.date.toISOString(),
  65. authors: [this.author1.id],
  66. },
  67. {
  68. operations: [{ pathname: 'main.tex', textOperation: [6, -4] }],
  69. timestamp: this.date.toISOString(),
  70. authors: [this.author1.id],
  71. },
  72. ],
  73. },
  74. },
  75. authors: [this.author1.id],
  76. }
  77. })
  78. describe('convertToDiffUpdates', function () {
  79. it('should convert them to insert and delete ops', function (done) {
  80. this.ChunkTranslator.convertToDiffUpdates(
  81. this.projectId,
  82. this.chunk,
  83. 'main.tex',
  84. 0,
  85. 3,
  86. (error, param) => {
  87. if (param == null) {
  88. param = {}
  89. }
  90. const { initialContent, updates } = param
  91. expect(error).to.be.null
  92. expect(initialContent).to.equal(this.fileContents)
  93. expect(updates).to.deep.equal([
  94. {
  95. op: [
  96. { i: 'Hello test, ', p: 0 },
  97. { d: 'Hello ', p: 12 },
  98. ],
  99. meta: {
  100. users: [this.author1.id],
  101. start_ts: this.date.getTime(),
  102. end_ts: this.date.getTime(),
  103. },
  104. v: 0,
  105. },
  106. {
  107. op: [{ i: 'foo ', p: 6 }],
  108. meta: {
  109. users: [this.author1.id],
  110. start_ts: this.date.getTime(),
  111. end_ts: this.date.getTime(),
  112. },
  113. v: 1,
  114. },
  115. {
  116. op: [{ d: 'foo ', p: 6 }],
  117. meta: {
  118. users: [this.author1.id],
  119. start_ts: this.date.getTime(),
  120. end_ts: this.date.getTime(),
  121. },
  122. v: 2,
  123. },
  124. ])
  125. done()
  126. }
  127. )
  128. })
  129. it('should return the correct initial text if there are previous changes', function (done) {
  130. this.ChunkTranslator.convertToDiffUpdates(
  131. this.projectId,
  132. this.chunk,
  133. 'main.tex',
  134. 2,
  135. 3,
  136. (error, param) => {
  137. if (param == null) {
  138. param = {}
  139. }
  140. const { initialContent, updates } = param
  141. expect(error).to.be.null
  142. expect(initialContent).to.equal(
  143. 'Hello foo test, world, this is a test'
  144. )
  145. expect(updates).to.deep.equal([
  146. {
  147. op: [{ d: 'foo ', p: 6 }],
  148. meta: {
  149. users: [this.author1.id],
  150. start_ts: this.date.getTime(),
  151. end_ts: this.date.getTime(),
  152. },
  153. v: 2,
  154. },
  155. ])
  156. done()
  157. }
  158. )
  159. })
  160. })
  161. describe('convertToSummarizedUpdates', function () {
  162. it('should return a summary of which docs changes when', function (done) {
  163. const assertion = (error, updates) => {
  164. expect(error).to.be.null
  165. expect(updates).to.deep.equal([
  166. {
  167. pathnames: ['main.tex'],
  168. project_ops: [],
  169. meta: {
  170. users: [this.author1.id],
  171. start_ts: this.date.getTime(),
  172. end_ts: this.date.getTime(),
  173. },
  174. v: 0,
  175. },
  176. {
  177. pathnames: ['main.tex'],
  178. project_ops: [],
  179. meta: {
  180. users: [this.author1.id],
  181. start_ts: this.date.getTime(),
  182. end_ts: this.date.getTime(),
  183. },
  184. v: 1,
  185. },
  186. {
  187. pathnames: ['main.tex'],
  188. project_ops: [],
  189. meta: {
  190. users: [this.author1.id],
  191. start_ts: this.date.getTime(),
  192. end_ts: this.date.getTime(),
  193. },
  194. v: 2,
  195. },
  196. ])
  197. done()
  198. }
  199. this.ChunkTranslator.convertToSummarizedUpdates(this.chunk, assertion)
  200. })
  201. })
  202. })
  203. describe('with a sequence of inserts and deletes', function () {
  204. beforeEach(function () {
  205. this.fileHash = 'some_other_hash'
  206. this.initialFileContents = 'aa bbbbb ccc '
  207. this.HistoryStoreManager.getProjectBlob
  208. .withArgs(this.historyId, this.fileHash)
  209. .yields(null, this.initialFileContents)
  210. this.chunk = {
  211. chunk: {
  212. startVersion: 0,
  213. history: {
  214. snapshot: {
  215. files: {
  216. 'main.tex': {
  217. hash: this.fileHash,
  218. stringLength: 42,
  219. },
  220. },
  221. },
  222. changes: [
  223. {
  224. operations: [
  225. {
  226. pathname: 'main.tex',
  227. textOperation: [
  228. '111 ', // -> "111 aa bbbbb ccc "
  229. -3, // -> "111 bbbbb ccc "
  230. 6, // -> "111 bbbbb ccc "
  231. '2222 ', // -> "111 bbbbb 2222 ccc "
  232. -1, // -> "111 bbbbb 2222 cc "
  233. 'd', // -> "111 bbbbb 2222 dcc "
  234. 3,
  235. ],
  236. },
  237. ],
  238. timestamp: this.date.toISOString(),
  239. authors: [this.author1.id],
  240. },
  241. ],
  242. },
  243. },
  244. authors: [this.author1.id],
  245. }
  246. })
  247. describe('convertToDiffUpdates', function () {
  248. it('should convert them to insert and delete ops', function (done) {
  249. this.ChunkTranslator.convertToDiffUpdates(
  250. this.projectId,
  251. this.chunk,
  252. 'main.tex',
  253. 0,
  254. 1,
  255. (error, param) => {
  256. if (param == null) {
  257. param = {}
  258. }
  259. const { initialContent, updates } = param
  260. expect(error).to.be.null
  261. expect(initialContent).to.equal(this.initialFileContents)
  262. expect(updates).to.deep.equal([
  263. {
  264. op: [
  265. { i: '111 ', p: 0 },
  266. { d: 'aa ', p: 4 },
  267. // NOTE: The construction of TextOperation can merge an
  268. // insertion across a deletion operation, which is why this is
  269. // ever so slightly different from the textOperation defined
  270. // in the chunk. Both diffs represent the same change in
  271. // content.
  272. { i: '2222 d', p: 10 },
  273. { d: 'c', p: 16 },
  274. ],
  275. meta: {
  276. users: [this.author1.id],
  277. start_ts: this.date.getTime(),
  278. end_ts: this.date.getTime(),
  279. },
  280. v: 0,
  281. },
  282. ])
  283. done()
  284. }
  285. )
  286. })
  287. it('should apply them to the text correctly', function (done) {
  288. this.ChunkTranslator.convertToDiffUpdates(
  289. this.projectId,
  290. this.chunk,
  291. 'main.tex',
  292. 1,
  293. 1,
  294. (error, param) => {
  295. if (param == null) {
  296. param = {}
  297. }
  298. const { initialContent, updates } = param
  299. expect(error).to.be.null
  300. expect(initialContent).to.equal('111 bbbbb 2222 dcc ')
  301. expect(updates).to.deep.equal([])
  302. done()
  303. }
  304. )
  305. })
  306. })
  307. })
  308. describe('with unknown operations', function (done) {
  309. beforeEach(function () {
  310. this.chunk = {
  311. chunk: {
  312. startVersion: 0,
  313. history: {
  314. snapshot: {
  315. files: {
  316. 'main.tex': {
  317. hash: this.fileHash,
  318. stringLength: 42,
  319. },
  320. },
  321. },
  322. changes: [
  323. {
  324. operations: [{ unknown: true }],
  325. timestamp: this.date.toISOString(),
  326. authors: [this.author1.id, undefined],
  327. },
  328. {
  329. operations: [
  330. { pathname: 'main.tex', textOperation: [3, 'Hello world'] },
  331. ],
  332. timestamp: this.date.toISOString(),
  333. authors: [this.author1.id, undefined],
  334. },
  335. ],
  336. },
  337. },
  338. authors: [this.author1.id],
  339. }
  340. })
  341. describe('convertToDiffUpdates', function () {
  342. it('should ignore the unknown update', function (done) {
  343. this.ChunkTranslator.convertToDiffUpdates(
  344. this.projectId,
  345. this.chunk,
  346. 'main.tex',
  347. 0,
  348. 2,
  349. (error, param) => {
  350. if (param == null) {
  351. param = {}
  352. }
  353. const { initialContent, updates } = param
  354. expect(error).to.be.null
  355. expect(initialContent).to.equal(this.fileContents)
  356. expect(updates).to.deep.equal([
  357. {
  358. op: [{ i: 'Hello world', p: 3 }],
  359. meta: {
  360. users: [this.author1.id, null],
  361. start_ts: this.date.getTime(),
  362. end_ts: this.date.getTime(),
  363. },
  364. v: 1,
  365. },
  366. ])
  367. done()
  368. }
  369. )
  370. })
  371. })
  372. describe('convertToSummarizedUpdates', function () {
  373. it('should ignore the unknown update', function (done) {
  374. const assertion = (error, updates) => {
  375. expect(error).to.be.null
  376. expect(updates).to.deep.equal([
  377. {
  378. pathnames: [],
  379. project_ops: [],
  380. meta: {
  381. users: [this.author1.id, null],
  382. start_ts: this.date.getTime(),
  383. end_ts: this.date.getTime(),
  384. },
  385. v: 0,
  386. },
  387. {
  388. pathnames: ['main.tex'],
  389. project_ops: [],
  390. meta: {
  391. users: [this.author1.id, null],
  392. start_ts: this.date.getTime(),
  393. end_ts: this.date.getTime(),
  394. },
  395. v: 1,
  396. },
  397. ])
  398. done()
  399. }
  400. this.ChunkTranslator.convertToSummarizedUpdates(this.chunk, assertion)
  401. })
  402. })
  403. })
  404. describe('with changes to multiple files', function (done) {
  405. beforeEach(function () {
  406. this.chunk = {
  407. chunk: {
  408. startVersion: 0,
  409. history: {
  410. snapshot: {
  411. files: {
  412. 'main.tex': {
  413. hash: this.fileHash,
  414. stringLength: 42,
  415. },
  416. 'other.tex': {
  417. hash: this.fileHash,
  418. stringLength: 42,
  419. },
  420. },
  421. },
  422. changes: [
  423. {
  424. operations: [
  425. { pathname: 'other.tex', textOperation: [0, 'foo'] },
  426. ],
  427. timestamp: this.date.toISOString(),
  428. authors: [this.author1.id],
  429. },
  430. {
  431. operations: [
  432. { pathname: 'main.tex', textOperation: [6, 'bar'] },
  433. ],
  434. timestamp: this.date.toISOString(),
  435. authors: [this.author1.id],
  436. },
  437. {
  438. operations: [
  439. { pathname: 'other.tex', textOperation: [9, 'baz'] },
  440. ],
  441. timestamp: this.date.toISOString(),
  442. authors: [this.author1.id],
  443. },
  444. {
  445. operations: [
  446. { pathname: 'main.tex', textOperation: [12, 'qux'] },
  447. ],
  448. timestamp: this.date.toISOString(),
  449. authors: [this.author1.id],
  450. },
  451. ],
  452. },
  453. },
  454. authors: [this.author1.id],
  455. }
  456. })
  457. describe('convertToDiffUpdates', function () {
  458. it('should only return the changes to the requested file', function (done) {
  459. this.ChunkTranslator.convertToDiffUpdates(
  460. this.projectId,
  461. this.chunk,
  462. 'main.tex',
  463. 0,
  464. 4,
  465. (error, param) => {
  466. if (param == null) {
  467. param = {}
  468. }
  469. const { initialContent, updates } = param
  470. expect(error).to.be.null
  471. expect(initialContent).to.equal('Hello world, this is a test')
  472. expect(updates).to.deep.equal([
  473. {
  474. op: [{ i: 'bar', p: 6 }],
  475. meta: {
  476. users: [this.author1.id],
  477. start_ts: this.date.getTime(),
  478. end_ts: this.date.getTime(),
  479. },
  480. v: 1,
  481. },
  482. {
  483. op: [{ i: 'qux', p: 12 }],
  484. meta: {
  485. users: [this.author1.id],
  486. start_ts: this.date.getTime(),
  487. end_ts: this.date.getTime(),
  488. },
  489. v: 3,
  490. },
  491. ])
  492. done()
  493. }
  494. )
  495. })
  496. })
  497. describe('convertToSummarizedUpdates', function () {
  498. it('should return a summary of which docs changes when', function (done) {
  499. const assertion = (error, updates) => {
  500. expect(error).to.be.null
  501. expect(updates).to.deep.equal([
  502. {
  503. pathnames: ['other.tex'],
  504. project_ops: [],
  505. meta: {
  506. users: [this.author1.id],
  507. start_ts: this.date.getTime(),
  508. end_ts: this.date.getTime(),
  509. },
  510. v: 0,
  511. },
  512. {
  513. pathnames: ['main.tex'],
  514. project_ops: [],
  515. meta: {
  516. users: [this.author1.id],
  517. start_ts: this.date.getTime(),
  518. end_ts: this.date.getTime(),
  519. },
  520. v: 1,
  521. },
  522. {
  523. pathnames: ['other.tex'],
  524. project_ops: [],
  525. meta: {
  526. users: [this.author1.id],
  527. start_ts: this.date.getTime(),
  528. end_ts: this.date.getTime(),
  529. },
  530. v: 2,
  531. },
  532. {
  533. pathnames: ['main.tex'],
  534. project_ops: [],
  535. meta: {
  536. users: [this.author1.id],
  537. start_ts: this.date.getTime(),
  538. end_ts: this.date.getTime(),
  539. },
  540. v: 3,
  541. },
  542. ])
  543. done()
  544. }
  545. this.ChunkTranslator.convertToSummarizedUpdates(this.chunk, assertion)
  546. })
  547. })
  548. })
  549. describe('when the file is created during the chunk', function () {
  550. beforeEach(function () {
  551. this.chunk = {
  552. chunk: {
  553. startVersion: 0,
  554. history: {
  555. snapshot: {
  556. files: {
  557. 'main.tex': {
  558. hash: this.fileHash,
  559. stringLength: 42,
  560. },
  561. },
  562. },
  563. changes: [
  564. {
  565. operations: [
  566. { pathname: 'main.tex', textOperation: [6, 'bar'] },
  567. ],
  568. timestamp: this.date.toISOString(),
  569. authors: [this.author1.id],
  570. },
  571. {
  572. operations: [
  573. {
  574. pathname: 'new.tex',
  575. file: { hash: this.fileHash, stringLength: 10 },
  576. },
  577. ],
  578. timestamp: this.date.toISOString(),
  579. authors: [this.author1.id],
  580. },
  581. {
  582. operations: [
  583. { pathname: 'new.tex', textOperation: [6, 'bar'] },
  584. ],
  585. timestamp: this.date.toISOString(),
  586. authors: [this.author1.id],
  587. },
  588. {
  589. operations: [
  590. { pathname: 'new.tex', textOperation: [9, 'baz'] },
  591. ],
  592. timestamp: this.date.toISOString(),
  593. authors: [this.author1.id],
  594. },
  595. ],
  596. },
  597. },
  598. authors: [this.author1],
  599. }
  600. })
  601. describe('convertToDiffUpdates', function () {
  602. it('returns changes after the file was created before the fromVersion', function (done) {
  603. this.ChunkTranslator.convertToDiffUpdates(
  604. this.projectId,
  605. this.chunk,
  606. 'new.tex',
  607. 2,
  608. 4,
  609. (error, param) => {
  610. if (param == null) {
  611. param = {}
  612. }
  613. const { initialContent, updates } = param
  614. expect(error).to.be.null
  615. expect(initialContent).to.equal(this.fileContents)
  616. expect(updates).to.deep.equal([
  617. {
  618. op: [{ i: 'bar', p: 6 }],
  619. meta: {
  620. users: [this.author1.id],
  621. start_ts: this.date.getTime(),
  622. end_ts: this.date.getTime(),
  623. },
  624. v: 2,
  625. },
  626. {
  627. op: [{ i: 'baz', p: 9 }],
  628. meta: {
  629. users: [this.author1.id],
  630. start_ts: this.date.getTime(),
  631. end_ts: this.date.getTime(),
  632. },
  633. v: 3,
  634. },
  635. ])
  636. done()
  637. }
  638. )
  639. })
  640. it('returns changes when the file was created at the fromVersion', function (done) {
  641. this.ChunkTranslator.convertToDiffUpdates(
  642. this.projectId,
  643. this.chunk,
  644. 'new.tex',
  645. 1,
  646. 4,
  647. (error, param) => {
  648. if (param == null) {
  649. param = {}
  650. }
  651. const { initialContent, updates } = param
  652. expect(error).to.be.null
  653. expect(initialContent).to.equal(this.fileContents)
  654. expect(updates).to.deep.equal([
  655. {
  656. op: [{ i: 'bar', p: 6 }],
  657. meta: {
  658. users: [this.author1.id],
  659. start_ts: this.date.getTime(),
  660. end_ts: this.date.getTime(),
  661. },
  662. v: 2,
  663. },
  664. {
  665. op: [{ i: 'baz', p: 9 }],
  666. meta: {
  667. users: [this.author1.id],
  668. start_ts: this.date.getTime(),
  669. end_ts: this.date.getTime(),
  670. },
  671. v: 3,
  672. },
  673. ])
  674. done()
  675. }
  676. )
  677. })
  678. it('returns changes when the file was created after the fromVersion', function (done) {
  679. this.ChunkTranslator.convertToDiffUpdates(
  680. this.projectId,
  681. this.chunk,
  682. 'new.tex',
  683. 0,
  684. 4,
  685. (error, param) => {
  686. if (param == null) {
  687. param = {}
  688. }
  689. const { initialContent, updates } = param
  690. expect(error).to.be.null
  691. expect(initialContent).to.equal(this.fileContents)
  692. expect(updates).to.deep.equal([
  693. {
  694. op: [{ i: 'bar', p: 6 }],
  695. meta: {
  696. users: [this.author1.id],
  697. start_ts: this.date.getTime(),
  698. end_ts: this.date.getTime(),
  699. },
  700. v: 2,
  701. },
  702. {
  703. op: [{ i: 'baz', p: 9 }],
  704. meta: {
  705. users: [this.author1.id],
  706. start_ts: this.date.getTime(),
  707. end_ts: this.date.getTime(),
  708. },
  709. v: 3,
  710. },
  711. ])
  712. done()
  713. }
  714. )
  715. })
  716. })
  717. describe('convertToSummarizedUpdates', function () {
  718. it('should return a summary which includes the addition', function (done) {
  719. const assertion = (error, updates) => {
  720. expect(error).to.be.null
  721. expect(updates).to.deep.equal([
  722. {
  723. pathnames: ['main.tex'],
  724. project_ops: [],
  725. meta: {
  726. users: [this.author1.id],
  727. start_ts: this.date.getTime(),
  728. end_ts: this.date.getTime(),
  729. },
  730. v: 0,
  731. },
  732. {
  733. pathnames: [],
  734. project_ops: [
  735. {
  736. add: {
  737. pathname: 'new.tex',
  738. },
  739. },
  740. ],
  741. meta: {
  742. users: [this.author1.id],
  743. start_ts: this.date.getTime(),
  744. end_ts: this.date.getTime(),
  745. },
  746. v: 1,
  747. },
  748. {
  749. pathnames: ['new.tex'],
  750. project_ops: [],
  751. meta: {
  752. users: [this.author1.id],
  753. start_ts: this.date.getTime(),
  754. end_ts: this.date.getTime(),
  755. },
  756. v: 2,
  757. },
  758. {
  759. pathnames: ['new.tex'],
  760. project_ops: [],
  761. meta: {
  762. users: [this.author1.id],
  763. start_ts: this.date.getTime(),
  764. end_ts: this.date.getTime(),
  765. },
  766. v: 3,
  767. },
  768. ])
  769. done()
  770. }
  771. this.ChunkTranslator.convertToSummarizedUpdates(this.chunk, assertion)
  772. })
  773. })
  774. })
  775. describe('when the file is renamed during the chunk', function () {
  776. beforeEach(function () {
  777. this.chunk = {
  778. chunk: {
  779. startVersion: 0,
  780. history: {
  781. snapshot: {
  782. files: {
  783. 'main.tex': {
  784. hash: this.fileHash,
  785. stringLength: 42,
  786. },
  787. },
  788. },
  789. changes: [
  790. {
  791. operations: [{ pathname: 'main.tex', textOperation: ['foo'] }],
  792. timestamp: this.date.toISOString(),
  793. authors: [this.author1.id],
  794. },
  795. {
  796. operations: [
  797. { pathname: 'main.tex', newPathname: 'moved.tex' },
  798. ],
  799. timestamp: this.date.toISOString(),
  800. authors: [this.author1.id],
  801. },
  802. {
  803. operations: [
  804. { pathname: 'moved.tex', textOperation: [3, 'bar'] },
  805. ],
  806. timestamp: this.date.toISOString(),
  807. authors: [this.author1.id],
  808. },
  809. {
  810. operations: [
  811. { pathname: 'moved.tex', newPathname: 'moved_again.tex' },
  812. ],
  813. timestamp: this.date.toISOString(),
  814. authors: [this.author1.id],
  815. },
  816. {
  817. operations: [
  818. { pathname: 'moved_again.tex', textOperation: [6, 'baz'] },
  819. ],
  820. timestamp: this.date.toISOString(),
  821. authors: [this.author1.id],
  822. },
  823. ],
  824. },
  825. },
  826. authors: [this.author1],
  827. }
  828. })
  829. describe('convertToDiffUpdates', function () {
  830. it('uses the original pathname before it is moved', function (done) {
  831. this.ChunkTranslator.convertToDiffUpdates(
  832. this.projectId,
  833. this.chunk,
  834. 'main.tex',
  835. 0,
  836. 5,
  837. (error, param) => {
  838. if (param == null) {
  839. param = {}
  840. }
  841. const { initialContent, updates } = param
  842. expect(error).to.be.null
  843. expect(initialContent).to.equal(this.fileContents)
  844. expect(updates).to.deep.equal([
  845. {
  846. op: [{ i: 'foo', p: 0 }],
  847. meta: {
  848. users: [this.author1.id],
  849. start_ts: this.date.getTime(),
  850. end_ts: this.date.getTime(),
  851. },
  852. v: 0,
  853. },
  854. {
  855. op: [{ i: 'bar', p: 3 }],
  856. meta: {
  857. users: [this.author1.id],
  858. start_ts: this.date.getTime(),
  859. end_ts: this.date.getTime(),
  860. },
  861. v: 2,
  862. },
  863. {
  864. op: [{ i: 'baz', p: 6 }],
  865. meta: {
  866. users: [this.author1.id],
  867. start_ts: this.date.getTime(),
  868. end_ts: this.date.getTime(),
  869. },
  870. v: 4,
  871. },
  872. ])
  873. done()
  874. }
  875. )
  876. })
  877. it('uses the original pathname for before the move change', function (done) {
  878. this.ChunkTranslator.convertToDiffUpdates(
  879. this.projectId,
  880. this.chunk,
  881. 'main.tex',
  882. 1,
  883. 5,
  884. (error, param) => {
  885. if (param == null) {
  886. param = {}
  887. }
  888. const { initialContent, updates } = param
  889. expect(error).to.be.null
  890. expect(initialContent).to.equal('foo' + this.fileContents)
  891. expect(updates).to.deep.equal([
  892. {
  893. op: [{ i: 'bar', p: 3 }],
  894. meta: {
  895. users: [this.author1.id],
  896. start_ts: this.date.getTime(),
  897. end_ts: this.date.getTime(),
  898. },
  899. v: 2,
  900. },
  901. {
  902. op: [{ i: 'baz', p: 6 }],
  903. meta: {
  904. users: [this.author1.id],
  905. start_ts: this.date.getTime(),
  906. end_ts: this.date.getTime(),
  907. },
  908. v: 4,
  909. },
  910. ])
  911. done()
  912. }
  913. )
  914. })
  915. it('uses the new pathname for after the move change', function (done) {
  916. this.ChunkTranslator.convertToDiffUpdates(
  917. this.projectId,
  918. this.chunk,
  919. 'moved.tex',
  920. 2,
  921. 5,
  922. (error, param) => {
  923. if (param == null) {
  924. param = {}
  925. }
  926. const { initialContent, updates } = param
  927. expect(error).to.be.null
  928. expect(initialContent).to.equal('foo' + this.fileContents)
  929. expect(updates).to.deep.equal([
  930. {
  931. op: [{ i: 'bar', p: 3 }],
  932. meta: {
  933. users: [this.author1.id],
  934. start_ts: this.date.getTime(),
  935. end_ts: this.date.getTime(),
  936. },
  937. v: 2,
  938. },
  939. {
  940. op: [{ i: 'baz', p: 6 }],
  941. meta: {
  942. users: [this.author1.id],
  943. start_ts: this.date.getTime(),
  944. end_ts: this.date.getTime(),
  945. },
  946. v: 4,
  947. },
  948. ])
  949. done()
  950. }
  951. )
  952. })
  953. it('tracks multiple renames', function (done) {
  954. this.ChunkTranslator.convertToDiffUpdates(
  955. this.projectId,
  956. this.chunk,
  957. 'moved_again.tex',
  958. 4,
  959. 5,
  960. (error, param) => {
  961. if (param == null) {
  962. param = {}
  963. }
  964. const { initialContent, updates } = param
  965. expect(error).to.be.null
  966. expect(initialContent).to.equal('foobar' + this.fileContents)
  967. expect(updates).to.deep.equal([
  968. {
  969. op: [{ i: 'baz', p: 6 }],
  970. meta: {
  971. users: [this.author1.id],
  972. start_ts: this.date.getTime(),
  973. end_ts: this.date.getTime(),
  974. },
  975. v: 4,
  976. },
  977. ])
  978. done()
  979. }
  980. )
  981. })
  982. it('returns an error when referring to a file that is now moved', function (done) {
  983. this.ChunkTranslator.convertToDiffUpdates(
  984. this.projectId,
  985. this.chunk,
  986. 'main.tex',
  987. 4,
  988. 5,
  989. error => {
  990. expect(error.message).to.equal(
  991. "pathname 'main.tex' not found in range"
  992. )
  993. done()
  994. }
  995. )
  996. })
  997. })
  998. describe('convertToSummarizedUpdates', function () {
  999. it('should return a summary which includes the rename', function (done) {
  1000. const assertion = (error, updates) => {
  1001. expect(error).to.be.null
  1002. expect(updates).to.deep.equal([
  1003. {
  1004. pathnames: ['main.tex'],
  1005. project_ops: [],
  1006. meta: {
  1007. users: [this.author1.id],
  1008. start_ts: this.date.getTime(),
  1009. end_ts: this.date.getTime(),
  1010. },
  1011. v: 0,
  1012. },
  1013. {
  1014. pathnames: [],
  1015. project_ops: [
  1016. {
  1017. rename: {
  1018. pathname: 'main.tex',
  1019. newPathname: 'moved.tex',
  1020. },
  1021. },
  1022. ],
  1023. meta: {
  1024. users: [this.author1.id],
  1025. start_ts: this.date.getTime(),
  1026. end_ts: this.date.getTime(),
  1027. },
  1028. v: 1,
  1029. },
  1030. {
  1031. pathnames: ['moved.tex'],
  1032. project_ops: [],
  1033. meta: {
  1034. users: [this.author1.id],
  1035. start_ts: this.date.getTime(),
  1036. end_ts: this.date.getTime(),
  1037. },
  1038. v: 2,
  1039. },
  1040. {
  1041. pathnames: [],
  1042. project_ops: [
  1043. {
  1044. rename: {
  1045. pathname: 'moved.tex',
  1046. newPathname: 'moved_again.tex',
  1047. },
  1048. },
  1049. ],
  1050. meta: {
  1051. users: [this.author1.id],
  1052. start_ts: this.date.getTime(),
  1053. end_ts: this.date.getTime(),
  1054. },
  1055. v: 3,
  1056. },
  1057. {
  1058. pathnames: ['moved_again.tex'],
  1059. project_ops: [],
  1060. meta: {
  1061. users: [this.author1.id],
  1062. start_ts: this.date.getTime(),
  1063. end_ts: this.date.getTime(),
  1064. },
  1065. v: 4,
  1066. },
  1067. ])
  1068. done()
  1069. }
  1070. this.ChunkTranslator.convertToSummarizedUpdates(this.chunk, assertion)
  1071. })
  1072. })
  1073. })
  1074. describe('when the file is deleted during the chunk', function () {
  1075. beforeEach(function () {
  1076. this.chunk = {
  1077. chunk: {
  1078. startVersion: 0,
  1079. history: {
  1080. snapshot: {
  1081. files: {
  1082. 'main.tex': {
  1083. hash: this.fileHash,
  1084. stringLength: 42,
  1085. },
  1086. 'other.tex': {
  1087. hash: this.fileHash,
  1088. stringLength: 42,
  1089. },
  1090. },
  1091. },
  1092. changes: [
  1093. {
  1094. operations: [{ pathname: 'main.tex', textOperation: ['foo'] }],
  1095. timestamp: this.date.toISOString(),
  1096. authors: [this.author1.id],
  1097. },
  1098. {
  1099. operations: [{ pathname: 'main.tex', newPathname: '' }],
  1100. timestamp: this.date.toISOString(),
  1101. authors: [this.author1.id],
  1102. },
  1103. {
  1104. operations: [{ pathname: 'other.tex', textOperation: ['foo'] }],
  1105. timestamp: this.date.toISOString(),
  1106. authors: [this.author1.id],
  1107. },
  1108. ],
  1109. },
  1110. },
  1111. authors: [this.author1],
  1112. }
  1113. })
  1114. describe('convertToDiffUpdates', function () {
  1115. it('returns updates up to when it is deleted', function (done) {
  1116. this.ChunkTranslator.convertToDiffUpdates(
  1117. this.projectId,
  1118. this.chunk,
  1119. 'main.tex',
  1120. 0,
  1121. 3,
  1122. (error, param) => {
  1123. if (param == null) {
  1124. param = {}
  1125. }
  1126. const { initialContent, updates } = param
  1127. expect(error).to.be.null
  1128. expect(initialContent).to.equal(this.fileContents)
  1129. expect(updates).to.deep.equal([
  1130. {
  1131. op: [{ i: 'foo', p: 0 }],
  1132. meta: {
  1133. users: [this.author1.id],
  1134. start_ts: this.date.getTime(),
  1135. end_ts: this.date.getTime(),
  1136. },
  1137. v: 0,
  1138. },
  1139. ])
  1140. done()
  1141. }
  1142. )
  1143. })
  1144. it('returns nothing if fromVersion is when is it was deleted', function (done) {
  1145. this.ChunkTranslator.convertToDiffUpdates(
  1146. this.projectId,
  1147. this.chunk,
  1148. 'main.tex',
  1149. 1,
  1150. 3,
  1151. (error, param) => {
  1152. if (param == null) {
  1153. param = {}
  1154. }
  1155. const { initialContent, updates } = param
  1156. expect(error).to.be.null
  1157. expect(initialContent).to.equal('foo' + this.fileContents)
  1158. expect(updates).to.deep.equal([])
  1159. done()
  1160. }
  1161. )
  1162. })
  1163. it('returns an error requesting changes after deleted', function (done) {
  1164. this.ChunkTranslator.convertToDiffUpdates(
  1165. this.projectId,
  1166. this.chunk,
  1167. 'main.tex',
  1168. 2,
  1169. 3,
  1170. error => {
  1171. expect(error.message).to.equal(
  1172. "pathname 'main.tex' not found in range"
  1173. )
  1174. done()
  1175. }
  1176. )
  1177. })
  1178. })
  1179. describe('convertToSummarizedUpdates', function () {
  1180. it('should return a summary which includes the delete', function (done) {
  1181. const assertion = (error, updates) => {
  1182. expect(error).to.be.null
  1183. expect(updates).to.deep.equal([
  1184. {
  1185. pathnames: ['main.tex'],
  1186. project_ops: [],
  1187. meta: {
  1188. users: [this.author1.id],
  1189. start_ts: this.date.getTime(),
  1190. end_ts: this.date.getTime(),
  1191. },
  1192. v: 0,
  1193. },
  1194. {
  1195. pathnames: [],
  1196. project_ops: [
  1197. {
  1198. remove: {
  1199. pathname: 'main.tex',
  1200. },
  1201. },
  1202. ],
  1203. meta: {
  1204. users: [this.author1.id],
  1205. start_ts: this.date.getTime(),
  1206. end_ts: this.date.getTime(),
  1207. },
  1208. v: 1,
  1209. },
  1210. {
  1211. pathnames: ['other.tex'],
  1212. project_ops: [],
  1213. meta: {
  1214. users: [this.author1.id],
  1215. start_ts: this.date.getTime(),
  1216. end_ts: this.date.getTime(),
  1217. },
  1218. v: 2,
  1219. },
  1220. ])
  1221. done()
  1222. }
  1223. this.ChunkTranslator.convertToSummarizedUpdates(this.chunk, assertion)
  1224. })
  1225. })
  1226. })
  1227. describe("with text operations applied to files that don't exist", function (done) {
  1228. beforeEach(function () {
  1229. this.chunk = {
  1230. chunk: {
  1231. startVersion: 0,
  1232. history: {
  1233. snapshot: {
  1234. files: {
  1235. 'main.tex': {
  1236. hash: this.fileHash,
  1237. stringLength: 42,
  1238. },
  1239. },
  1240. },
  1241. changes: [
  1242. {
  1243. operations: [
  1244. {
  1245. pathname: 'not_here.tex',
  1246. textOperation: [3, 'Hello world'],
  1247. },
  1248. ],
  1249. timestamp: this.date.toISOString(),
  1250. authors: [this.author1.id, undefined],
  1251. },
  1252. ],
  1253. },
  1254. },
  1255. authors: [this.author1],
  1256. }
  1257. })
  1258. describe('convertToSummarizedUpdates', function () {
  1259. it('should return an empty diff instead of an error', function (done) {
  1260. this.ChunkTranslator.convertToDiffUpdates(
  1261. this.projectId,
  1262. this.chunk,
  1263. 'not_here.tex',
  1264. 0,
  1265. 1,
  1266. (error, result) => {
  1267. expect(error).to.equal(null)
  1268. expect(result.updates.length).to.equal(0)
  1269. done()
  1270. }
  1271. )
  1272. })
  1273. })
  1274. })
  1275. describe("with rename operations applied to files that don't exist", function (done) {
  1276. beforeEach(function () {
  1277. this.chunk = {
  1278. chunk: {
  1279. startVersion: 0,
  1280. history: {
  1281. snapshot: {
  1282. files: {
  1283. 'main.tex': {
  1284. hash: this.fileHash,
  1285. stringLength: 42,
  1286. },
  1287. },
  1288. },
  1289. changes: [
  1290. {
  1291. operations: [
  1292. { pathname: 'not_here.tex', newPathname: 'blah.tex' },
  1293. ],
  1294. timestamp: this.date.toISOString(),
  1295. authors: [this.author1.id, undefined],
  1296. },
  1297. ],
  1298. },
  1299. },
  1300. authors: [this.author1],
  1301. }
  1302. })
  1303. describe('convertToSummarizedUpdates', function () {
  1304. it('should return an empty diff instead of an error', function (done) {
  1305. this.ChunkTranslator.convertToDiffUpdates(
  1306. this.projectId,
  1307. this.chunk,
  1308. 'not_here.tex',
  1309. 0,
  1310. 1,
  1311. (error, result) => {
  1312. expect(error).to.equal(null)
  1313. expect(result.updates.length).to.equal(0)
  1314. done()
  1315. }
  1316. )
  1317. })
  1318. })
  1319. })
  1320. describe("with remove operations applied to files that don't exist", function (done) {
  1321. beforeEach(function () {
  1322. this.chunk = {
  1323. chunk: {
  1324. startVersion: 0,
  1325. history: {
  1326. snapshot: {
  1327. files: {
  1328. 'main.tex': {
  1329. hash: this.fileHash,
  1330. stringLength: 42,
  1331. },
  1332. },
  1333. },
  1334. changes: [
  1335. {
  1336. operations: [{ pathname: 'not_here.tex', newPathname: '' }],
  1337. timestamp: this.date.toISOString(),
  1338. authors: [this.author1.id, undefined],
  1339. },
  1340. ],
  1341. },
  1342. },
  1343. authors: [this.author1],
  1344. }
  1345. })
  1346. describe('convertToSummarizedUpdates', function () {
  1347. it('should return an empty diff instead of an error', function (done) {
  1348. this.ChunkTranslator.convertToDiffUpdates(
  1349. this.projectId,
  1350. this.chunk,
  1351. 'not_here.tex',
  1352. 0,
  1353. 1,
  1354. (error, result) => {
  1355. expect(error).to.equal(null)
  1356. expect(result.updates.length).to.equal(0)
  1357. done()
  1358. }
  1359. )
  1360. })
  1361. })
  1362. })
  1363. describe('with multiple operations in one change', function () {
  1364. beforeEach(function () {
  1365. this.chunk = {
  1366. chunk: {
  1367. startVersion: 0,
  1368. history: {
  1369. snapshot: {
  1370. files: {
  1371. 'main.tex': {
  1372. hash: this.fileHash,
  1373. stringLength: 42,
  1374. },
  1375. 'other.tex': {
  1376. hash: this.fileHash,
  1377. stringLength: 42,
  1378. },
  1379. 'old.tex': {
  1380. hash: this.fileHash,
  1381. stringLength: 42,
  1382. },
  1383. 'deleted.tex': {
  1384. hash: this.fileHash,
  1385. stringLength: 42,
  1386. },
  1387. },
  1388. },
  1389. changes: [
  1390. {
  1391. operations: [
  1392. { pathname: 'main.tex', textOperation: ['Hello test, ', -6] },
  1393. { pathname: 'main.tex', textOperation: [6, 'foo '] },
  1394. { pathname: 'other.tex', textOperation: [6, 'foo '] },
  1395. { pathname: 'old.tex', newPathname: 'new.tex' },
  1396. { pathname: 'deleted.tex', newPathname: '' },
  1397. {
  1398. pathname: 'created.tex',
  1399. file: { hash: this.fileHash, stringLength: 10 },
  1400. },
  1401. ],
  1402. timestamp: this.date.toISOString(),
  1403. authors: [this.author1.id],
  1404. },
  1405. {
  1406. operations: [{ pathname: 'main.tex', textOperation: [6, -4] }],
  1407. timestamp: this.date.toISOString(),
  1408. authors: [this.author1.id],
  1409. },
  1410. ],
  1411. },
  1412. },
  1413. authors: [this.author1],
  1414. }
  1415. })
  1416. describe('convertToDiffUpdates', function () {
  1417. it('should can return multiple ops from the same version', function (done) {
  1418. this.ChunkTranslator.convertToDiffUpdates(
  1419. this.projectId,
  1420. this.chunk,
  1421. 'main.tex',
  1422. 0,
  1423. 2,
  1424. (error, param) => {
  1425. if (param == null) {
  1426. param = {}
  1427. }
  1428. const { initialContent, updates } = param
  1429. expect(error).to.be.null
  1430. expect(initialContent).to.equal(this.fileContents)
  1431. expect(updates).to.deep.equal([
  1432. {
  1433. op: [
  1434. { i: 'Hello test, ', p: 0 },
  1435. { d: 'Hello ', p: 12 },
  1436. ],
  1437. meta: {
  1438. users: [this.author1.id],
  1439. start_ts: this.date.getTime(),
  1440. end_ts: this.date.getTime(),
  1441. },
  1442. v: 0,
  1443. },
  1444. {
  1445. op: [{ i: 'foo ', p: 6 }],
  1446. meta: {
  1447. users: [this.author1.id],
  1448. start_ts: this.date.getTime(),
  1449. end_ts: this.date.getTime(),
  1450. },
  1451. v: 0,
  1452. },
  1453. {
  1454. op: [{ d: 'foo ', p: 6 }],
  1455. meta: {
  1456. users: [this.author1.id],
  1457. start_ts: this.date.getTime(),
  1458. end_ts: this.date.getTime(),
  1459. },
  1460. v: 1,
  1461. },
  1462. ])
  1463. done()
  1464. }
  1465. )
  1466. })
  1467. it('should return the correct initial text if there are previous changes', function (done) {
  1468. this.ChunkTranslator.convertToDiffUpdates(
  1469. this.projectId,
  1470. this.chunk,
  1471. 'main.tex',
  1472. 1,
  1473. 2,
  1474. (error, param) => {
  1475. if (param == null) {
  1476. param = {}
  1477. }
  1478. const { initialContent, updates } = param
  1479. expect(error).to.be.null
  1480. expect(initialContent).to.equal(
  1481. 'Hello foo test, world, this is a test'
  1482. )
  1483. expect(updates).to.deep.equal([
  1484. {
  1485. op: [{ d: 'foo ', p: 6 }],
  1486. meta: {
  1487. users: [this.author1.id],
  1488. start_ts: this.date.getTime(),
  1489. end_ts: this.date.getTime(),
  1490. },
  1491. v: 1,
  1492. },
  1493. ])
  1494. done()
  1495. }
  1496. )
  1497. })
  1498. })
  1499. describe('convertToSummarizedUpdates', function () {
  1500. it('should return a summary of containing multiple changes', function (done) {
  1501. const assertion = (error, updates) => {
  1502. expect(error).to.be.null
  1503. expect(updates).to.deep.equal([
  1504. {
  1505. pathnames: ['main.tex', 'other.tex'],
  1506. project_ops: [
  1507. {
  1508. rename: {
  1509. pathname: 'old.tex',
  1510. newPathname: 'new.tex',
  1511. },
  1512. },
  1513. {
  1514. remove: {
  1515. pathname: 'deleted.tex',
  1516. },
  1517. },
  1518. {
  1519. add: {
  1520. pathname: 'created.tex',
  1521. },
  1522. },
  1523. ],
  1524. meta: {
  1525. users: [this.author1.id],
  1526. start_ts: this.date.getTime(),
  1527. end_ts: this.date.getTime(),
  1528. },
  1529. v: 0,
  1530. },
  1531. {
  1532. pathnames: ['main.tex'],
  1533. project_ops: [],
  1534. meta: {
  1535. users: [this.author1.id],
  1536. start_ts: this.date.getTime(),
  1537. end_ts: this.date.getTime(),
  1538. },
  1539. v: 1,
  1540. },
  1541. ])
  1542. done()
  1543. }
  1544. this.ChunkTranslator.convertToSummarizedUpdates(this.chunk, assertion)
  1545. })
  1546. })
  1547. })
  1548. describe('with a binary file', function () {
  1549. beforeEach(function () {
  1550. this.chunk = {
  1551. chunk: {
  1552. startVersion: 0,
  1553. history: {
  1554. snapshot: {
  1555. files: {
  1556. 'main.tex': {
  1557. hash: this.fileHash,
  1558. stringLength: 42,
  1559. },
  1560. 'binary.tex': {
  1561. hash: this.fileHash,
  1562. byteLength: 42,
  1563. },
  1564. },
  1565. },
  1566. changes: [
  1567. {
  1568. operations: [
  1569. { pathname: 'main.tex', textOperation: ['Hello test, ', -6] },
  1570. ],
  1571. timestamp: this.date.toISOString(),
  1572. authors: [this.author1.id],
  1573. },
  1574. ],
  1575. },
  1576. },
  1577. authors: [this.author1.id],
  1578. }
  1579. })
  1580. describe('convertToDiffUpdates', function () {
  1581. it('should convert them to a binary diff', function (done) {
  1582. this.ChunkTranslator.convertToDiffUpdates(
  1583. this.projectId,
  1584. this.chunk,
  1585. 'binary.tex',
  1586. 0,
  1587. 1,
  1588. (error, diff) => {
  1589. expect(error).to.be.null
  1590. expect(diff).to.deep.equal({ binary: true })
  1591. done()
  1592. }
  1593. )
  1594. })
  1595. })
  1596. })
  1597. describe('with v2 author ids', function () {
  1598. beforeEach(function () {
  1599. this.chunk = {
  1600. chunk: {
  1601. startVersion: 0,
  1602. history: {
  1603. snapshot: {
  1604. files: {
  1605. 'main.tex': {
  1606. hash: this.fileHash,
  1607. stringLength: 42,
  1608. },
  1609. },
  1610. },
  1611. changes: [
  1612. {
  1613. operations: [
  1614. { pathname: 'main.tex', textOperation: ['Hello test, ', -6] },
  1615. ],
  1616. timestamp: this.date.toISOString(),
  1617. v2Authors: [(this.v2AuthorId = '123456789')],
  1618. },
  1619. ],
  1620. },
  1621. },
  1622. }
  1623. })
  1624. describe('convertToDiffUpdates', function () {
  1625. it('should return the v2 author id in the users array', function (done) {
  1626. this.ChunkTranslator.convertToDiffUpdates(
  1627. this.projectId,
  1628. this.chunk,
  1629. 'main.tex',
  1630. 0,
  1631. 1,
  1632. (error, diff) => {
  1633. expect(error).to.be.null
  1634. expect(diff.updates).to.deep.equal([
  1635. {
  1636. op: [
  1637. { i: 'Hello test, ', p: 0 },
  1638. { d: 'Hello ', p: 12 },
  1639. ],
  1640. meta: {
  1641. users: [this.v2AuthorId],
  1642. start_ts: this.date.getTime(),
  1643. end_ts: this.date.getTime(),
  1644. },
  1645. v: 0,
  1646. },
  1647. ])
  1648. done()
  1649. }
  1650. )
  1651. })
  1652. })
  1653. describe('convertToSummarizedUpdates', function () {
  1654. it('should return the v2 author id in the users array', function (done) {
  1655. const assertion = (error, updateSet) => {
  1656. expect(error).to.be.null
  1657. expect(updateSet).to.deep.equal([
  1658. {
  1659. pathnames: ['main.tex'],
  1660. project_ops: [],
  1661. meta: {
  1662. users: [this.v2AuthorId],
  1663. start_ts: this.date.getTime(),
  1664. end_ts: this.date.getTime(),
  1665. },
  1666. v: 0,
  1667. },
  1668. ])
  1669. done()
  1670. }
  1671. this.ChunkTranslator.convertToSummarizedUpdates(this.chunk, assertion)
  1672. })
  1673. })
  1674. })
  1675. describe('with tracked changes in a file', function () {
  1676. describe('convertToDiffUpdates', function () {
  1677. beforeEach(function () {
  1678. this.rangesHash = 'some_ranges_hash'
  1679. this.fileContents = 'Hello planet world, this is a test'
  1680. this.ranges = JSON.stringify({
  1681. trackedChanges: [
  1682. {
  1683. range: { pos: 6, length: 7 },
  1684. tracking: {
  1685. type: 'delete',
  1686. userId: this.author1.id,
  1687. ts: '2024-01-01T00:00:00.000Z',
  1688. },
  1689. },
  1690. ],
  1691. })
  1692. this.HistoryStoreManager.getProjectBlob
  1693. .withArgs(this.historyId, this.rangesHash)
  1694. .yields(null, this.ranges)
  1695. this.HistoryStoreManager.getProjectBlob
  1696. .withArgs(this.historyId, this.fileHash)
  1697. .yields(null, this.fileContents)
  1698. })
  1699. it('should filter out the tracked deletes that were present in the chunk', function (done) {
  1700. this.chunk = {
  1701. chunk: {
  1702. startVersion: 0,
  1703. history: {
  1704. snapshot: {
  1705. files: {
  1706. 'main.tex': {
  1707. hash: this.fileHash,
  1708. rangesHash: this.rangesHash,
  1709. stringLength: 42,
  1710. },
  1711. },
  1712. },
  1713. changes: [
  1714. {
  1715. operations: [
  1716. {
  1717. pathname: 'main.tex',
  1718. textOperation: [
  1719. // [...] is a tracked delete
  1720. 28, // Hello [planet ]world, this is |a test
  1721. -1, // Hello [planet ]world, this is | test
  1722. 'the', // Hello [planet ]world, this is the| test
  1723. 5, // Hello [planet ]world, this is the test|
  1724. ],
  1725. },
  1726. ],
  1727. timestamp: this.date.toISOString(),
  1728. authors: [this.author1.id],
  1729. },
  1730. ],
  1731. },
  1732. },
  1733. authors: [this.author1.id],
  1734. }
  1735. this.ChunkTranslator.convertToDiffUpdates(
  1736. this.projectId,
  1737. this.chunk,
  1738. 'main.tex',
  1739. 0,
  1740. 1,
  1741. (error, diff) => {
  1742. expect(error).to.be.null
  1743. expect(diff.initialContent).to.equal('Hello world, this is a test')
  1744. expect(diff.updates).to.deep.equal([
  1745. {
  1746. op: [
  1747. { i: 'the', p: 21 },
  1748. { d: 'a', p: 24 },
  1749. ],
  1750. meta: {
  1751. users: [this.author1.id],
  1752. start_ts: this.date.getTime(),
  1753. end_ts: this.date.getTime(),
  1754. },
  1755. v: 0,
  1756. },
  1757. ])
  1758. done()
  1759. }
  1760. )
  1761. })
  1762. it('should filter out tracked deletes across multiple changes', function (done) {
  1763. this.chunk = {
  1764. chunk: {
  1765. startVersion: 0,
  1766. history: {
  1767. snapshot: {
  1768. files: {
  1769. 'main.tex': {
  1770. hash: this.fileHash,
  1771. rangesHash: this.rangesHash,
  1772. stringLength: 42,
  1773. },
  1774. },
  1775. },
  1776. changes: [
  1777. {
  1778. operations: [
  1779. {
  1780. pathname: 'main.tex',
  1781. textOperation: [
  1782. // [...] is a tracked delete
  1783. 28, // Hello [planet ]world, this is |a test
  1784. -1, // Hello [planet ]world, this is | test
  1785. 'the', // Hello [planet ]world, this is the| test
  1786. 5, // Hello [planet ]world, this is the test|
  1787. ],
  1788. },
  1789. {
  1790. pathname: 'main.tex',
  1791. textOperation: [
  1792. // [...] is a tracked delete
  1793. 22, // Hello [planet ]world, th|is is the test
  1794. -2, // Hello [planet ]world, th| is the test
  1795. 'at', // Hello [planet ]world, that| is the test
  1796. 12, // Hello [planet ]world, that is the test|
  1797. ],
  1798. },
  1799. ],
  1800. timestamp: this.date.toISOString(),
  1801. authors: [this.author1.id],
  1802. },
  1803. ],
  1804. },
  1805. },
  1806. authors: [this.author1.id],
  1807. }
  1808. this.ChunkTranslator.convertToDiffUpdates(
  1809. this.projectId,
  1810. this.chunk,
  1811. 'main.tex',
  1812. 0,
  1813. 1,
  1814. (error, diff) => {
  1815. expect(error).to.be.null
  1816. expect(diff.initialContent).to.equal('Hello world, this is a test')
  1817. expect(diff.updates).to.deep.equal([
  1818. {
  1819. op: [
  1820. { i: 'the', p: 21 },
  1821. { d: 'a', p: 24 },
  1822. ],
  1823. meta: {
  1824. users: [this.author1.id],
  1825. start_ts: this.date.getTime(),
  1826. end_ts: this.date.getTime(),
  1827. },
  1828. v: 0,
  1829. },
  1830. {
  1831. op: [
  1832. { i: 'at', p: 15 },
  1833. { d: 'is', p: 17 },
  1834. ],
  1835. meta: {
  1836. users: [this.author1.id],
  1837. start_ts: this.date.getTime(),
  1838. end_ts: this.date.getTime(),
  1839. },
  1840. v: 0,
  1841. },
  1842. ])
  1843. done()
  1844. }
  1845. )
  1846. })
  1847. it('should handle tracked delete in the operation', function (done) {
  1848. this.chunk = {
  1849. chunk: {
  1850. startVersion: 0,
  1851. history: {
  1852. snapshot: {
  1853. files: {
  1854. 'main.tex': {
  1855. hash: this.fileHash,
  1856. rangesHash: this.rangesHash,
  1857. stringLength: 42,
  1858. },
  1859. },
  1860. },
  1861. changes: [
  1862. {
  1863. operations: [
  1864. {
  1865. pathname: 'main.tex',
  1866. textOperation: [
  1867. // [...] is a tracked delete
  1868. 5, // Hello| [planet ]world, this is a test
  1869. {
  1870. r: 1,
  1871. tracking: {
  1872. type: 'delete',
  1873. userId: this.author1.id,
  1874. ts: '2024-01-01T00:00:00.000Z',
  1875. },
  1876. }, // Hello[ ]|[planet ]world, this is test
  1877. 7, // Hello[ ][planet ]|world, this is the test
  1878. {
  1879. r: 5,
  1880. tracking: {
  1881. type: 'delete',
  1882. userId: this.author1.id,
  1883. ts: '2024-01-01T00:00:00.000Z',
  1884. },
  1885. }, // Hello[ ][planet ][world]|, this is the test
  1886. 18, // Hello[ ][planet ][world], this is the test|
  1887. ],
  1888. },
  1889. ],
  1890. timestamp: this.date.toISOString(),
  1891. authors: [this.author1.id],
  1892. },
  1893. ],
  1894. },
  1895. },
  1896. authors: [this.author1.id],
  1897. }
  1898. this.ChunkTranslator.convertToDiffUpdates(
  1899. this.projectId,
  1900. this.chunk,
  1901. 'main.tex',
  1902. 0,
  1903. 1,
  1904. (error, diff) => {
  1905. expect(error).to.be.null
  1906. expect(diff.initialContent).to.equal('Hello world, this is a test')
  1907. expect(diff.updates).to.deep.equal([
  1908. {
  1909. op: [
  1910. { d: ' ', p: 5 },
  1911. { d: 'world', p: 5 },
  1912. ],
  1913. meta: {
  1914. users: [this.author1.id],
  1915. start_ts: this.date.getTime(),
  1916. end_ts: this.date.getTime(),
  1917. },
  1918. v: 0,
  1919. },
  1920. ])
  1921. done()
  1922. }
  1923. )
  1924. })
  1925. it('should filter out tracked deletes in insert operations', function (done) {
  1926. this.chunk = {
  1927. chunk: {
  1928. startVersion: 0,
  1929. history: {
  1930. snapshot: {
  1931. files: {
  1932. 'main.tex': {
  1933. hash: this.fileHash,
  1934. rangesHash: this.rangesHash,
  1935. stringLength: 42,
  1936. },
  1937. },
  1938. },
  1939. changes: [
  1940. {
  1941. operations: [
  1942. {
  1943. pathname: 'main.tex',
  1944. textOperation: [
  1945. // [...] is a tracked delete
  1946. 13, // Hello [planet ]|world, this is a test
  1947. {
  1948. i: 'pluto',
  1949. tracking: {
  1950. type: 'delete',
  1951. userId: this.author1.id,
  1952. ts: '2024-01-01T00:00:00.000Z',
  1953. },
  1954. }, // Hello [planet pluto]|world, this is a test
  1955. 21, // Hello [planet pluto]world, this is a test|
  1956. ],
  1957. },
  1958. ],
  1959. timestamp: this.date.toISOString(),
  1960. authors: [this.author1.id],
  1961. },
  1962. ],
  1963. },
  1964. },
  1965. authors: [this.author1.id],
  1966. }
  1967. this.ChunkTranslator.convertToDiffUpdates(
  1968. this.projectId,
  1969. this.chunk,
  1970. 'main.tex',
  1971. 0,
  1972. 1,
  1973. (error, diff) => {
  1974. expect(error).to.be.null
  1975. expect(diff.initialContent).to.equal('Hello world, this is a test')
  1976. expect(diff.updates).to.deep.equal([
  1977. {
  1978. op: [],
  1979. meta: {
  1980. users: [this.author1.id],
  1981. start_ts: this.date.getTime(),
  1982. end_ts: this.date.getTime(),
  1983. },
  1984. v: 0,
  1985. },
  1986. ])
  1987. done()
  1988. }
  1989. )
  1990. })
  1991. it('should filter out tracked deletes in delete operations', function (done) {
  1992. this.chunk = {
  1993. chunk: {
  1994. startVersion: 0,
  1995. history: {
  1996. snapshot: {
  1997. files: {
  1998. 'main.tex': {
  1999. hash: this.fileHash,
  2000. rangesHash: this.rangesHash,
  2001. stringLength: 42,
  2002. },
  2003. },
  2004. },
  2005. changes: [
  2006. {
  2007. operations: [
  2008. {
  2009. pathname: 'main.tex',
  2010. textOperation: [
  2011. // [...] is a tracked delete
  2012. 6, // Hello |[planet ]world, this is a test
  2013. -3, // Hello [|net ]world, this is a test
  2014. 6, // Hello [net ]wo|rld, this is a test
  2015. -3, // Hello [net ]wo|, this is a test
  2016. 16, // Hello [net ]wo, this is a test|
  2017. ],
  2018. },
  2019. ],
  2020. timestamp: this.date.toISOString(),
  2021. authors: [this.author1.id],
  2022. },
  2023. ],
  2024. },
  2025. },
  2026. authors: [this.author1.id],
  2027. }
  2028. this.ChunkTranslator.convertToDiffUpdates(
  2029. this.projectId,
  2030. this.chunk,
  2031. 'main.tex',
  2032. 0,
  2033. 1,
  2034. (error, diff) => {
  2035. expect(error).to.be.null
  2036. expect(diff.initialContent).to.equal('Hello world, this is a test')
  2037. expect(diff.updates).to.deep.equal([
  2038. {
  2039. op: [{ d: 'rld', p: 8 }],
  2040. meta: {
  2041. users: [this.author1.id],
  2042. start_ts: this.date.getTime(),
  2043. end_ts: this.date.getTime(),
  2044. },
  2045. v: 0,
  2046. },
  2047. ])
  2048. done()
  2049. }
  2050. )
  2051. })
  2052. it('should filter out tracked deletes in retain operations', function (done) {
  2053. this.chunk = {
  2054. chunk: {
  2055. startVersion: 0,
  2056. history: {
  2057. snapshot: {
  2058. files: {
  2059. 'main.tex': {
  2060. hash: this.fileHash,
  2061. rangesHash: this.rangesHash,
  2062. stringLength: 42,
  2063. },
  2064. },
  2065. },
  2066. changes: [
  2067. {
  2068. operations: [
  2069. {
  2070. pathname: 'main.tex',
  2071. textOperation: [
  2072. // [...] is a tracked delete
  2073. 4, // Hell|o [planet ]world, this is a test
  2074. {
  2075. r: 4,
  2076. tracking: { type: 'none' },
  2077. }, // Hello pl|[anet ]world, this is a test
  2078. {
  2079. r: 3,
  2080. tracking: {
  2081. type: 'insert',
  2082. userId: this.author1.id,
  2083. ts: '2024-01-01T00:00:00.000Z',
  2084. },
  2085. }, // Hello plane|[t ]world, this is a test
  2086. 23, // Hello plane[t ]world, this is a test|
  2087. ],
  2088. },
  2089. ],
  2090. timestamp: this.date.toISOString(),
  2091. authors: [this.author1.id],
  2092. },
  2093. ],
  2094. },
  2095. },
  2096. authors: [this.author1.id],
  2097. }
  2098. this.ChunkTranslator.convertToDiffUpdates(
  2099. this.projectId,
  2100. this.chunk,
  2101. 'main.tex',
  2102. 0,
  2103. 1,
  2104. (error, diff) => {
  2105. expect(error).to.be.null
  2106. expect(diff.initialContent).to.equal('Hello world, this is a test')
  2107. expect(diff.updates).to.deep.equal([
  2108. {
  2109. op: [
  2110. {
  2111. i: 'pl',
  2112. p: 6,
  2113. },
  2114. {
  2115. i: 'ane',
  2116. p: 8,
  2117. },
  2118. ],
  2119. meta: {
  2120. users: [this.author1.id],
  2121. start_ts: this.date.getTime(),
  2122. end_ts: this.date.getTime(),
  2123. },
  2124. v: 0,
  2125. },
  2126. ])
  2127. done()
  2128. }
  2129. )
  2130. })
  2131. it('should report tracked deletion (retains) as deletions', function (done) {
  2132. this.chunk = {
  2133. chunk: {
  2134. startVersion: 0,
  2135. history: {
  2136. snapshot: {
  2137. files: {
  2138. 'main.tex': {
  2139. hash: this.fileHash,
  2140. rangesHash: this.rangesHash,
  2141. stringLength: 42,
  2142. },
  2143. },
  2144. },
  2145. changes: [
  2146. {
  2147. operations: [
  2148. {
  2149. pathname: 'main.tex',
  2150. textOperation: [
  2151. // [...] is a tracked delete
  2152. {
  2153. r: 34,
  2154. tracking: {
  2155. type: 'delete',
  2156. userId: this.author1.id,
  2157. ts: '2024-01-01T00:00:00.000Z',
  2158. },
  2159. }, // [Hello planet world, this is a test]|
  2160. ],
  2161. },
  2162. ],
  2163. timestamp: this.date.toISOString(),
  2164. authors: [this.author1.id],
  2165. },
  2166. ],
  2167. },
  2168. },
  2169. authors: [this.author1.id],
  2170. }
  2171. this.ChunkTranslator.convertToDiffUpdates(
  2172. this.projectId,
  2173. this.chunk,
  2174. 'main.tex',
  2175. 0,
  2176. 1,
  2177. (error, diff) => {
  2178. expect(error).to.be.null
  2179. expect(diff.initialContent).to.equal('Hello world, this is a test')
  2180. expect(diff.updates).to.deep.equal([
  2181. {
  2182. op: [
  2183. {
  2184. d: 'Hello ',
  2185. p: 0,
  2186. },
  2187. {
  2188. d: 'world, this is a test',
  2189. p: 0,
  2190. },
  2191. ],
  2192. meta: {
  2193. users: [this.author1.id],
  2194. start_ts: this.date.getTime(),
  2195. end_ts: this.date.getTime(),
  2196. },
  2197. v: 0,
  2198. },
  2199. ])
  2200. done()
  2201. }
  2202. )
  2203. })
  2204. it('should properly create changes when deleting after moved track deletes', function (done) {
  2205. this.chunk = {
  2206. chunk: {
  2207. startVersion: 0,
  2208. history: {
  2209. snapshot: {
  2210. files: {
  2211. 'main.tex': {
  2212. hash: this.fileHash,
  2213. stringLength: 34,
  2214. },
  2215. },
  2216. },
  2217. changes: [
  2218. {
  2219. operations: [
  2220. {
  2221. pathname: 'main.tex',
  2222. textOperation: [
  2223. // [...] is a tracked delete
  2224. // // He[ll]o planet world, this is a test
  2225. 2,
  2226. {
  2227. r: 2,
  2228. tracking: {
  2229. type: 'delete',
  2230. userId: this.author1.id,
  2231. ts: this.date.toISOString(),
  2232. },
  2233. },
  2234. 30,
  2235. ],
  2236. },
  2237. ],
  2238. timestamp: this.date.toISOString(),
  2239. authors: [this.author1.id],
  2240. },
  2241. {
  2242. operations: [
  2243. {
  2244. pathname: 'main.tex',
  2245. textOperation: [
  2246. // [...] is a tracked delete
  2247. // {...} is a tracked insert
  2248. // He[ll]o {TEST }planet world, this is a test
  2249. 6,
  2250. {
  2251. i: 'TEST ',
  2252. tracking: {
  2253. type: 'insert',
  2254. userId: this.author1.id,
  2255. ts: this.date.toISOString(),
  2256. },
  2257. },
  2258. 28,
  2259. ],
  2260. },
  2261. ],
  2262. timestamp: this.date.toISOString(),
  2263. authors: [this.author1.id],
  2264. },
  2265. {
  2266. operations: [
  2267. {
  2268. pathname: 'main.tex',
  2269. textOperation: [
  2270. // [...] is a tracked delete
  2271. // {...} is a tracked insert
  2272. // He[ll]o {TEST }planet world, [this] is a test
  2273. 25,
  2274. {
  2275. r: 4,
  2276. tracking: {
  2277. type: 'delete',
  2278. userId: this.author1.id,
  2279. ts: this.date.toISOString(),
  2280. },
  2281. },
  2282. 10,
  2283. ],
  2284. },
  2285. ],
  2286. timestamp: this.date.toISOString(),
  2287. authors: [this.author1.id],
  2288. },
  2289. {
  2290. operations: [
  2291. {
  2292. pathname: 'main.tex',
  2293. textOperation: [
  2294. // [...] is a tracked delete
  2295. // {...} is a tracked insert
  2296. 2, // He|[ll]o {TEST }planet world, [this] is a test
  2297. -2, // He|o {TEST }planet world, [this] is a test
  2298. 2, // Heo |{TEST }planet world, [this] is a test
  2299. {
  2300. r: 5,
  2301. tracking: { type: 'none' },
  2302. }, // Heo TEST| planet world, [this] is a test
  2303. 14, // Heo TEST planet world, |[this] is a test
  2304. -4, // Heo TEST planet world, | is a test
  2305. 10, // Heo TEST planet world, is a test|
  2306. ],
  2307. },
  2308. ],
  2309. timestamp: this.date.toISOString(),
  2310. authors: [this.author1.id],
  2311. },
  2312. ],
  2313. },
  2314. },
  2315. authors: [this.author1.id],
  2316. }
  2317. this.ChunkTranslator.convertToDiffUpdates(
  2318. this.projectId,
  2319. this.chunk,
  2320. 'main.tex',
  2321. 0,
  2322. 4,
  2323. (error, diff) => {
  2324. expect(error).to.be.null
  2325. expect(diff.updates).to.deep.equal([
  2326. {
  2327. op: [
  2328. {
  2329. d: 'll',
  2330. p: 2,
  2331. },
  2332. ],
  2333. meta: {
  2334. users: [this.author1.id],
  2335. start_ts: this.date.getTime(),
  2336. end_ts: this.date.getTime(),
  2337. },
  2338. v: 0,
  2339. },
  2340. {
  2341. op: [
  2342. {
  2343. i: 'TEST ',
  2344. p: 4,
  2345. },
  2346. ],
  2347. meta: {
  2348. users: [this.author1.id],
  2349. start_ts: this.date.getTime(),
  2350. end_ts: this.date.getTime(),
  2351. },
  2352. v: 1,
  2353. },
  2354. {
  2355. op: [
  2356. {
  2357. d: 'this',
  2358. p: 23,
  2359. },
  2360. ],
  2361. meta: {
  2362. users: [this.author1.id],
  2363. start_ts: this.date.getTime(),
  2364. end_ts: this.date.getTime(),
  2365. },
  2366. v: 2,
  2367. },
  2368. {
  2369. op: [],
  2370. meta: {
  2371. users: [this.author1.id],
  2372. start_ts: this.date.getTime(),
  2373. end_ts: this.date.getTime(),
  2374. },
  2375. v: 3,
  2376. },
  2377. ])
  2378. done()
  2379. }
  2380. )
  2381. })
  2382. it('should properly create changes when retaining after moved track deletes', function (done) {
  2383. this.chunk = {
  2384. chunk: {
  2385. startVersion: 0,
  2386. history: {
  2387. snapshot: {
  2388. files: {
  2389. 'main.tex': {
  2390. hash: this.fileHash,
  2391. stringLength: 34,
  2392. },
  2393. },
  2394. },
  2395. changes: [
  2396. {
  2397. operations: [
  2398. {
  2399. pathname: 'main.tex',
  2400. textOperation: [
  2401. // [...] is a tracked delete
  2402. // // He[ll]o planet world, this is a test
  2403. 2,
  2404. {
  2405. r: 2,
  2406. tracking: {
  2407. type: 'delete',
  2408. userId: this.author1.id,
  2409. ts: this.date.toISOString(),
  2410. },
  2411. },
  2412. 30,
  2413. ],
  2414. },
  2415. ],
  2416. timestamp: this.date.toISOString(),
  2417. authors: [this.author1.id],
  2418. },
  2419. {
  2420. operations: [
  2421. {
  2422. pathname: 'main.tex',
  2423. textOperation: [
  2424. // [...] is a tracked delete
  2425. // He[ll]o planet world, [this] is a test
  2426. 20,
  2427. {
  2428. r: 4,
  2429. tracking: {
  2430. type: 'delete',
  2431. userId: this.author1.id,
  2432. ts: this.date.toISOString(),
  2433. },
  2434. },
  2435. 10,
  2436. ],
  2437. },
  2438. ],
  2439. timestamp: this.date.toISOString(),
  2440. authors: [this.author1.id],
  2441. },
  2442. {
  2443. operations: [
  2444. {
  2445. pathname: 'main.tex',
  2446. textOperation: [
  2447. // [...] is a tracked delete
  2448. // {...} is a tracked insert
  2449. // He[ll]o planet world, [this] {TEST }is a test
  2450. 25,
  2451. {
  2452. i: 'TEST ',
  2453. tracking: {
  2454. type: 'insert',
  2455. userId: this.author1.id,
  2456. ts: this.date.toISOString(),
  2457. },
  2458. },
  2459. 9,
  2460. ],
  2461. },
  2462. ],
  2463. timestamp: this.date.toISOString(),
  2464. authors: [this.author1.id],
  2465. },
  2466. {
  2467. operations: [
  2468. {
  2469. pathname: 'main.tex',
  2470. textOperation: [
  2471. // [...] is a tracked delete
  2472. // {...} is a tracked insert
  2473. 2, // He|[ll]o planet world, [this] {TEST }is a test
  2474. -2, // He|o planet world, [this] {TEST }is a test
  2475. {
  2476. r: 39,
  2477. tracking: { type: 'none' },
  2478. },
  2479. ], // He|o planet world, this TEST is a test
  2480. },
  2481. ],
  2482. timestamp: this.date.toISOString(),
  2483. authors: [this.author1.id],
  2484. },
  2485. ],
  2486. },
  2487. },
  2488. authors: [this.author1.id],
  2489. }
  2490. this.ChunkTranslator.convertToDiffUpdates(
  2491. this.projectId,
  2492. this.chunk,
  2493. 'main.tex',
  2494. 0,
  2495. 4,
  2496. (error, diff) => {
  2497. expect(error).to.be.null
  2498. expect(diff.updates).to.deep.equal([
  2499. {
  2500. op: [
  2501. {
  2502. d: 'll',
  2503. p: 2,
  2504. },
  2505. ],
  2506. meta: {
  2507. users: [this.author1.id],
  2508. start_ts: this.date.getTime(),
  2509. end_ts: this.date.getTime(),
  2510. },
  2511. v: 0,
  2512. },
  2513. {
  2514. op: [
  2515. {
  2516. d: 'this',
  2517. p: 18,
  2518. },
  2519. ],
  2520. meta: {
  2521. users: [this.author1.id],
  2522. start_ts: this.date.getTime(),
  2523. end_ts: this.date.getTime(),
  2524. },
  2525. v: 1,
  2526. },
  2527. {
  2528. op: [
  2529. {
  2530. i: 'TEST ',
  2531. p: 19,
  2532. },
  2533. ],
  2534. meta: {
  2535. users: [this.author1.id],
  2536. start_ts: this.date.getTime(),
  2537. end_ts: this.date.getTime(),
  2538. },
  2539. v: 2,
  2540. },
  2541. {
  2542. op: [
  2543. {
  2544. i: 'this',
  2545. p: 18,
  2546. },
  2547. ],
  2548. meta: {
  2549. users: [this.author1.id],
  2550. start_ts: this.date.getTime(),
  2551. end_ts: this.date.getTime(),
  2552. },
  2553. v: 3,
  2554. },
  2555. ])
  2556. done()
  2557. }
  2558. )
  2559. })
  2560. it('should handle deletion that starts before tracked delete', function (done) {
  2561. this.chunk = {
  2562. chunk: {
  2563. startVersion: 0,
  2564. history: {
  2565. snapshot: {
  2566. files: {
  2567. 'main.tex': {
  2568. hash: this.fileHash,
  2569. stringLength: 34,
  2570. },
  2571. },
  2572. },
  2573. changes: [
  2574. {
  2575. operations: [
  2576. {
  2577. pathname: 'main.tex',
  2578. textOperation: [
  2579. // [...] is a tracked delete
  2580. // Hello planet world, [this] is a test
  2581. 20,
  2582. {
  2583. r: 4,
  2584. tracking: {
  2585. type: 'delete',
  2586. userId: this.author1.id,
  2587. ts: this.date.toISOString(),
  2588. },
  2589. },
  2590. 10,
  2591. ],
  2592. },
  2593. ],
  2594. timestamp: this.date.toISOString(),
  2595. authors: [this.author1.id],
  2596. },
  2597. {
  2598. operations: [
  2599. {
  2600. pathname: 'main.tex',
  2601. textOperation: [
  2602. // [...] is a tracked delete
  2603. 5, // Hello| planet world, [this] is a test
  2604. -25, // Hellotest
  2605. 4,
  2606. ],
  2607. },
  2608. ],
  2609. timestamp: this.date.toISOString(),
  2610. authors: [this.author1.id],
  2611. },
  2612. ],
  2613. },
  2614. },
  2615. authors: [this.author1.id],
  2616. }
  2617. this.ChunkTranslator.convertToDiffUpdates(
  2618. this.projectId,
  2619. this.chunk,
  2620. 'main.tex',
  2621. 0,
  2622. 2,
  2623. (error, diff) => {
  2624. expect(error).to.be.null
  2625. expect(diff.updates).to.deep.equal([
  2626. {
  2627. op: [
  2628. {
  2629. d: 'this',
  2630. p: 20,
  2631. },
  2632. ],
  2633. meta: {
  2634. users: [this.author1.id],
  2635. start_ts: this.date.getTime(),
  2636. end_ts: this.date.getTime(),
  2637. },
  2638. v: 0,
  2639. },
  2640. {
  2641. op: [
  2642. {
  2643. d: ' planet world, ',
  2644. p: 5,
  2645. },
  2646. {
  2647. d: ' is a ',
  2648. p: 5,
  2649. },
  2650. ],
  2651. meta: {
  2652. users: [this.author1.id],
  2653. start_ts: this.date.getTime(),
  2654. end_ts: this.date.getTime(),
  2655. },
  2656. v: 1,
  2657. },
  2658. ])
  2659. done()
  2660. }
  2661. )
  2662. })
  2663. })
  2664. })
  2665. })