HistoryConversionsTests.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. const _ = require('lodash')
  2. const { expect } = require('chai')
  3. const HistoryConversions = require('../../../app/js/HistoryConversions')
  4. describe('HistoryConversions', function () {
  5. describe('toHistoryRanges', function () {
  6. it('handles empty ranges', function () {
  7. expect(HistoryConversions.toHistoryRanges({})).to.deep.equal({})
  8. })
  9. it("doesn't modify comments when there are no tracked changes", function () {
  10. const ranges = {
  11. comments: [makeComment('comment1', 5, 12)],
  12. }
  13. const historyRanges = HistoryConversions.toHistoryRanges(ranges)
  14. expect(historyRanges).to.deep.equal(ranges)
  15. })
  16. it('adjusts comments and tracked changes to account for tracked deletes', function () {
  17. const comments = [
  18. makeComment('comment0', 0, 1),
  19. makeComment('comment1', 10, 12),
  20. makeComment('comment2', 20, 10),
  21. makeComment('comment3', 15, 3),
  22. ]
  23. const changes = [
  24. makeTrackedDelete('change0', 2, 5),
  25. makeTrackedInsert('change1', 4, 5),
  26. makeTrackedDelete('change2', 10, 10),
  27. makeTrackedDelete('change3', 21, 6),
  28. makeTrackedDelete('change4', 50, 7),
  29. ]
  30. const ranges = { comments, changes }
  31. const historyRanges = HistoryConversions.toHistoryRanges(ranges)
  32. expect(historyRanges.comments).to.have.deep.members([
  33. comments[0],
  34. // shifted by change0 and change2, extended by change3
  35. enrichOp(comments[1], {
  36. hpos: 25, // 10 + 5 + 10
  37. hlen: 18, // 12 + 6
  38. }),
  39. // shifted by change0 and change2, extended by change3
  40. enrichOp(comments[2], {
  41. hpos: 35, // 20 + 5 + 10
  42. hlen: 16, // 10 + 6
  43. }),
  44. // shifted by change0 and change2
  45. enrichOp(comments[3], {
  46. hpos: 30, // 15 + 5 + 10
  47. }),
  48. ])
  49. expect(historyRanges.changes).to.deep.equal([
  50. changes[0],
  51. enrichOp(changes[1], {
  52. hpos: 9, // 4 + 5
  53. }),
  54. enrichOp(changes[2], {
  55. hpos: 15, // 10 + 5
  56. }),
  57. enrichOp(changes[3], {
  58. hpos: 36, // 21 + 5 + 10
  59. }),
  60. enrichOp(changes[4], {
  61. hpos: 71, // 50 + 5 + 10 + 6
  62. }),
  63. ])
  64. })
  65. })
  66. })
  67. function makeComment(id, pos, length) {
  68. return {
  69. id,
  70. op: {
  71. c: 'c'.repeat(length),
  72. p: pos,
  73. t: id,
  74. },
  75. metadata: makeMetadata(),
  76. }
  77. }
  78. function makeTrackedInsert(id, pos, length) {
  79. return {
  80. id,
  81. op: {
  82. i: 'i'.repeat(length),
  83. p: pos,
  84. },
  85. metadata: makeMetadata(),
  86. }
  87. }
  88. function makeTrackedDelete(id, pos, length) {
  89. return {
  90. id,
  91. op: {
  92. d: 'd'.repeat(length),
  93. p: pos,
  94. },
  95. metadata: makeMetadata(),
  96. }
  97. }
  98. function makeMetadata() {
  99. return {
  100. user_id: 'user-id',
  101. ts: new Date().toISOString(),
  102. }
  103. }
  104. function enrichOp(commentOrChange, extraFields) {
  105. const result = _.cloneDeep(commentOrChange)
  106. Object.assign(result.op, extraFields)
  107. return result
  108. }