DiffGeneratorTests.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /* eslint-disable
  2. no-return-assign,
  3. no-undef,
  4. no-unused-vars,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS101: Remove unnecessary use of Array.from
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. import sinon from 'sinon'
  15. import { expect } from 'chai'
  16. import { strict as esmock } from 'esmock'
  17. const MODULE_PATH = '../../../../app/js/DiffGenerator.js'
  18. describe('DiffGenerator', function () {
  19. beforeEach(async function () {
  20. this.DiffGenerator = await esmock(MODULE_PATH, {})
  21. this.ts = Date.now()
  22. this.user_id = 'mock-user-id'
  23. this.user_id_2 = 'mock-user-id-2'
  24. return (this.meta = {
  25. start_ts: this.ts,
  26. end_ts: this.ts,
  27. user_id: this.user_id,
  28. })
  29. })
  30. describe('buildDiff', function () {
  31. beforeEach(function () {
  32. this.diff = [{ u: 'mock-diff' }]
  33. this.content = 'Hello world'
  34. this.updates = [
  35. { i: 'mock-update-1' },
  36. { i: 'mock-update-2' },
  37. { i: 'mock-update-3' },
  38. ]
  39. this.DiffGenerator._mocks.applyUpdateToDiff = sinon
  40. .stub()
  41. .returns(this.diff)
  42. this.DiffGenerator._mocks.compressDiff = sinon.stub().returns(this.diff)
  43. return (this.result = this.DiffGenerator.buildDiff(
  44. this.content,
  45. this.updates
  46. ))
  47. })
  48. it('should return the diff', function () {
  49. return this.result.should.deep.equal(this.diff)
  50. })
  51. it('should build the content into an initial diff', function () {
  52. return this.DiffGenerator._mocks.applyUpdateToDiff
  53. .calledWith(
  54. [
  55. {
  56. u: this.content,
  57. },
  58. ],
  59. this.updates[0]
  60. )
  61. .should.equal(true)
  62. })
  63. it('should apply each update', function () {
  64. return Array.from(this.updates).map(update =>
  65. this.DiffGenerator._mocks.applyUpdateToDiff
  66. .calledWith(sinon.match.any, update)
  67. .should.equal(true)
  68. )
  69. })
  70. return it('should compress the diff', function () {
  71. return this.DiffGenerator._mocks.compressDiff
  72. .calledWith(this.diff)
  73. .should.equal(true)
  74. })
  75. })
  76. describe('compressDiff', function () {
  77. describe('with adjacent inserts with the same user_id', function () {
  78. return it('should create one update with combined meta data and min/max timestamps', function () {
  79. const diff = this.DiffGenerator.compressDiff([
  80. {
  81. i: 'foo',
  82. meta: { start_ts: 10, end_ts: 20, user: { id: this.user_id } },
  83. },
  84. {
  85. i: 'bar',
  86. meta: { start_ts: 5, end_ts: 15, user: { id: this.user_id } },
  87. },
  88. ])
  89. return expect(diff).to.deep.equal([
  90. {
  91. i: 'foobar',
  92. meta: { start_ts: 5, end_ts: 20, user: { id: this.user_id } },
  93. },
  94. ])
  95. })
  96. })
  97. describe('with adjacent inserts with different user_ids', function () {
  98. return it('should leave the inserts unchanged', function () {
  99. const input = [
  100. {
  101. i: 'foo',
  102. meta: { start_ts: 10, end_ts: 20, user: { id: this.user_id } },
  103. },
  104. {
  105. i: 'bar',
  106. meta: { start_ts: 5, end_ts: 15, user: { id: this.user_id_2 } },
  107. },
  108. ]
  109. const output = this.DiffGenerator.compressDiff(input)
  110. return expect(output).to.deep.equal(input)
  111. })
  112. })
  113. describe('with adjacent deletes with the same user_id', function () {
  114. return it('should create one update with combined meta data and min/max timestamps', function () {
  115. const diff = this.DiffGenerator.compressDiff([
  116. {
  117. d: 'foo',
  118. meta: { start_ts: 10, end_ts: 20, user: { id: this.user_id } },
  119. },
  120. {
  121. d: 'bar',
  122. meta: { start_ts: 5, end_ts: 15, user: { id: this.user_id } },
  123. },
  124. ])
  125. return expect(diff).to.deep.equal([
  126. {
  127. d: 'foobar',
  128. meta: { start_ts: 5, end_ts: 20, user: { id: this.user_id } },
  129. },
  130. ])
  131. })
  132. })
  133. return describe('with adjacent deletes with different user_ids', function () {
  134. return it('should leave the deletes unchanged', function () {
  135. const input = [
  136. {
  137. d: 'foo',
  138. meta: { start_ts: 10, end_ts: 20, user: { id: this.user_id } },
  139. },
  140. {
  141. d: 'bar',
  142. meta: { start_ts: 5, end_ts: 15, user: { id: this.user_id_2 } },
  143. },
  144. ]
  145. const output = this.DiffGenerator.compressDiff(input)
  146. return expect(output).to.deep.equal(input)
  147. })
  148. })
  149. })
  150. return describe('applyUpdateToDiff', function () {
  151. describe('an insert', function () {
  152. it('should insert into the middle of (u)nchanged text', function () {
  153. const diff = this.DiffGenerator.applyUpdateToDiff([{ u: 'foobar' }], {
  154. op: [{ p: 3, i: 'baz' }],
  155. meta: this.meta,
  156. })
  157. return expect(diff).to.deep.equal([
  158. { u: 'foo' },
  159. { i: 'baz', meta: this.meta },
  160. { u: 'bar' },
  161. ])
  162. })
  163. it('should insert into the start of (u)changed text', function () {
  164. const diff = this.DiffGenerator.applyUpdateToDiff([{ u: 'foobar' }], {
  165. op: [{ p: 0, i: 'baz' }],
  166. meta: this.meta,
  167. })
  168. return expect(diff).to.deep.equal([
  169. { i: 'baz', meta: this.meta },
  170. { u: 'foobar' },
  171. ])
  172. })
  173. it('should insert into the end of (u)changed text', function () {
  174. const diff = this.DiffGenerator.applyUpdateToDiff([{ u: 'foobar' }], {
  175. op: [{ p: 6, i: 'baz' }],
  176. meta: this.meta,
  177. })
  178. return expect(diff).to.deep.equal([
  179. { u: 'foobar' },
  180. { i: 'baz', meta: this.meta },
  181. ])
  182. })
  183. it('should insert into the middle of (i)inserted text', function () {
  184. const diff = this.DiffGenerator.applyUpdateToDiff(
  185. [{ i: 'foobar', meta: this.meta }],
  186. { op: [{ p: 3, i: 'baz' }], meta: this.meta }
  187. )
  188. return expect(diff).to.deep.equal([
  189. { i: 'foo', meta: this.meta },
  190. { i: 'baz', meta: this.meta },
  191. { i: 'bar', meta: this.meta },
  192. ])
  193. })
  194. return it('should not count deletes in the running length total', function () {
  195. const diff = this.DiffGenerator.applyUpdateToDiff(
  196. [{ d: 'deleted', meta: this.meta }, { u: 'foobar' }],
  197. { op: [{ p: 3, i: 'baz' }], meta: this.meta }
  198. )
  199. return expect(diff).to.deep.equal([
  200. { d: 'deleted', meta: this.meta },
  201. { u: 'foo' },
  202. { i: 'baz', meta: this.meta },
  203. { u: 'bar' },
  204. ])
  205. })
  206. })
  207. return describe('a delete', function () {
  208. describe('deleting unchanged text', function () {
  209. it('should delete from the middle of (u)nchanged text', function () {
  210. const diff = this.DiffGenerator.applyUpdateToDiff(
  211. [{ u: 'foobazbar' }],
  212. { op: [{ p: 3, d: 'baz' }], meta: this.meta }
  213. )
  214. return expect(diff).to.deep.equal([
  215. { u: 'foo' },
  216. { d: 'baz', meta: this.meta },
  217. { u: 'bar' },
  218. ])
  219. })
  220. it('should delete from the start of (u)nchanged text', function () {
  221. const diff = this.DiffGenerator.applyUpdateToDiff(
  222. [{ u: 'foobazbar' }],
  223. { op: [{ p: 0, d: 'foo' }], meta: this.meta }
  224. )
  225. return expect(diff).to.deep.equal([
  226. { d: 'foo', meta: this.meta },
  227. { u: 'bazbar' },
  228. ])
  229. })
  230. it('should delete from the end of (u)nchanged text', function () {
  231. const diff = this.DiffGenerator.applyUpdateToDiff(
  232. [{ u: 'foobazbar' }],
  233. { op: [{ p: 6, d: 'bar' }], meta: this.meta }
  234. )
  235. return expect(diff).to.deep.equal([
  236. { u: 'foobaz' },
  237. { d: 'bar', meta: this.meta },
  238. ])
  239. })
  240. return it('should delete across multiple (u)changed text parts', function () {
  241. const diff = this.DiffGenerator.applyUpdateToDiff(
  242. [{ u: 'foo' }, { u: 'baz' }, { u: 'bar' }],
  243. { op: [{ p: 2, d: 'obazb' }], meta: this.meta }
  244. )
  245. return expect(diff).to.deep.equal([
  246. { u: 'fo' },
  247. { d: 'o', meta: this.meta },
  248. { d: 'baz', meta: this.meta },
  249. { d: 'b', meta: this.meta },
  250. { u: 'ar' },
  251. ])
  252. })
  253. })
  254. describe('deleting inserts', function () {
  255. it('should delete from the middle of (i)nserted text', function () {
  256. const diff = this.DiffGenerator.applyUpdateToDiff(
  257. [{ i: 'foobazbar', meta: this.meta }],
  258. { op: [{ p: 3, d: 'baz' }], meta: this.meta }
  259. )
  260. return expect(diff).to.deep.equal([
  261. { i: 'foo', meta: this.meta },
  262. { i: 'bar', meta: this.meta },
  263. ])
  264. })
  265. it('should delete from the start of (u)nchanged text', function () {
  266. const diff = this.DiffGenerator.applyUpdateToDiff(
  267. [{ i: 'foobazbar', meta: this.meta }],
  268. { op: [{ p: 0, d: 'foo' }], meta: this.meta }
  269. )
  270. return expect(diff).to.deep.equal([{ i: 'bazbar', meta: this.meta }])
  271. })
  272. it('should delete from the end of (u)nchanged text', function () {
  273. const diff = this.DiffGenerator.applyUpdateToDiff(
  274. [{ i: 'foobazbar', meta: this.meta }],
  275. { op: [{ p: 6, d: 'bar' }], meta: this.meta }
  276. )
  277. return expect(diff).to.deep.equal([{ i: 'foobaz', meta: this.meta }])
  278. })
  279. return it('should delete across multiple (u)changed and (i)nserted text parts', function () {
  280. const diff = this.DiffGenerator.applyUpdateToDiff(
  281. [{ u: 'foo' }, { i: 'baz', meta: this.meta }, { u: 'bar' }],
  282. { op: [{ p: 2, d: 'obazb' }], meta: this.meta }
  283. )
  284. return expect(diff).to.deep.equal([
  285. { u: 'fo' },
  286. { d: 'o', meta: this.meta },
  287. { d: 'b', meta: this.meta },
  288. { u: 'ar' },
  289. ])
  290. })
  291. })
  292. describe('deleting over existing deletes', function () {
  293. return it('should delete across multiple (u)changed and (d)deleted text parts', function () {
  294. const diff = this.DiffGenerator.applyUpdateToDiff(
  295. [{ u: 'foo' }, { d: 'baz', meta: this.meta }, { u: 'bar' }],
  296. { op: [{ p: 2, d: 'ob' }], meta: this.meta }
  297. )
  298. return expect(diff).to.deep.equal([
  299. { u: 'fo' },
  300. { d: 'o', meta: this.meta },
  301. { d: 'baz', meta: this.meta },
  302. { d: 'b', meta: this.meta },
  303. { u: 'ar' },
  304. ])
  305. })
  306. })
  307. describe("deleting when the text doesn't match", function () {
  308. it('should throw an error when deleting from the middle of (u)nchanged text', function () {
  309. return expect(() =>
  310. this.DiffGenerator.applyUpdateToDiff([{ u: 'foobazbar' }], {
  311. op: [{ p: 3, d: 'xxx' }],
  312. meta: this.meta,
  313. })
  314. ).to.throw(this.DiffGenerator.ConsistencyError)
  315. })
  316. it('should throw an error when deleting from the start of (u)nchanged text', function () {
  317. return expect(() =>
  318. this.DiffGenerator.applyUpdateToDiff([{ u: 'foobazbar' }], {
  319. op: [{ p: 0, d: 'xxx' }],
  320. meta: this.meta,
  321. })
  322. ).to.throw(this.DiffGenerator.ConsistencyError)
  323. })
  324. return it('should throw an error when deleting from the end of (u)nchanged text', function () {
  325. return expect(() =>
  326. this.DiffGenerator.applyUpdateToDiff([{ u: 'foobazbar' }], {
  327. op: [{ p: 6, d: 'xxx' }],
  328. meta: this.meta,
  329. })
  330. ).to.throw(this.DiffGenerator.ConsistencyError)
  331. })
  332. })
  333. describe('when the last update in the existing diff is a delete', function () {
  334. return it('should insert the new update before the delete', function () {
  335. const diff = this.DiffGenerator.applyUpdateToDiff(
  336. [{ u: 'foo' }, { d: 'bar', meta: this.meta }],
  337. { op: [{ p: 3, i: 'baz' }], meta: this.meta }
  338. )
  339. return expect(diff).to.deep.equal([
  340. { u: 'foo' },
  341. { i: 'baz', meta: this.meta },
  342. { d: 'bar', meta: this.meta },
  343. ])
  344. })
  345. })
  346. return describe('when the only update in the existing diff is a delete', function () {
  347. return it('should insert the new update after the delete', function () {
  348. const diff = this.DiffGenerator.applyUpdateToDiff(
  349. [{ d: 'bar', meta: this.meta }],
  350. { op: [{ p: 0, i: 'baz' }], meta: this.meta }
  351. )
  352. return expect(diff).to.deep.equal([
  353. { d: 'bar', meta: this.meta },
  354. { i: 'baz', meta: this.meta },
  355. ])
  356. })
  357. })
  358. })
  359. })
  360. })