string_file_data.test.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // @ts-check
  2. 'use strict'
  3. const { expect } = require('chai')
  4. const _ = require('lodash')
  5. const ot = require('..')
  6. const StringFileData = require('../lib/file_data/string_file_data')
  7. const TextOperation = ot.TextOperation
  8. describe('StringFileData', function () {
  9. it('throws when it contains non BMP chars', function () {
  10. const content = '𝌆𝌆𝌆'
  11. const fileData = new StringFileData(content)
  12. const operation = new TextOperation()
  13. operation.insert('aa')
  14. expect(() => {
  15. fileData.edit(operation)
  16. }).to.throw(TextOperation.ApplyError, /string contains non BMP characters/)
  17. })
  18. it('validates string length when edited', function () {
  19. const longString = _.repeat('a', TextOperation.MAX_STRING_LENGTH)
  20. const fileData = new StringFileData(longString)
  21. expect(fileData.getByteLength()).to.equal(longString.length)
  22. expect(fileData.getStringLength()).to.equal(longString.length)
  23. expect(() => {
  24. fileData.edit(new TextOperation().retain(longString.length).insert('x'))
  25. }).to.throw(TextOperation.TooLongError)
  26. expect(fileData.getByteLength()).to.equal(longString.length)
  27. expect(fileData.getStringLength()).to.equal(longString.length)
  28. fileData.edit(new TextOperation().retain(longString.length - 1).remove(1))
  29. expect(fileData.getByteLength()).to.equal(longString.length - 1)
  30. expect(fileData.getStringLength()).to.equal(longString.length - 1)
  31. })
  32. it('getComments() should return an empty array', function () {
  33. const fileData = new StringFileData('test')
  34. expect(fileData.getComments()).to.eql([])
  35. })
  36. it('creates StringFileData with comments', function () {
  37. const fileData = new StringFileData('test', [
  38. {
  39. id: 'comm1',
  40. ranges: [
  41. {
  42. pos: 5,
  43. length: 10,
  44. },
  45. ],
  46. },
  47. {
  48. id: 'comm2',
  49. ranges: [
  50. {
  51. pos: 20,
  52. length: 5,
  53. },
  54. ],
  55. },
  56. ])
  57. expect(fileData.getComments()).to.eql([
  58. { id: 'comm1', ranges: [{ pos: 5, length: 10 }], resolved: false },
  59. { id: 'comm2', ranges: [{ pos: 20, length: 5 }], resolved: false },
  60. ])
  61. })
  62. it('fromRaw() should create StringFileData with comments', function () {
  63. const fileData = StringFileData.fromRaw({
  64. content: 'test',
  65. comments: [
  66. {
  67. id: 'comm1',
  68. ranges: [
  69. {
  70. pos: 5,
  71. length: 10,
  72. },
  73. ],
  74. resolved: false,
  75. },
  76. {
  77. id: 'comm2',
  78. ranges: [
  79. {
  80. pos: 20,
  81. length: 5,
  82. },
  83. ],
  84. resolved: true,
  85. },
  86. ],
  87. })
  88. expect(fileData.getComments()).to.eql([
  89. { id: 'comm1', ranges: [{ pos: 5, length: 10 }], resolved: false },
  90. { id: 'comm2', ranges: [{ pos: 20, length: 5 }], resolved: true },
  91. ])
  92. })
  93. it('getContent should filter out tracked deletions when passed option', function () {
  94. const fileData = new StringFileData(
  95. 'the quick brown fox jumps over the lazy dog',
  96. undefined,
  97. [
  98. {
  99. range: { pos: 4, length: 6 },
  100. tracking: {
  101. type: 'delete',
  102. ts: '2024-01-01T00:00:00.000Z',
  103. userId: 'user1',
  104. },
  105. },
  106. {
  107. range: { pos: 35, length: 5 },
  108. tracking: {
  109. type: 'delete',
  110. ts: '2023-01-01T00:00:00.000Z',
  111. userId: 'user2',
  112. },
  113. },
  114. ]
  115. )
  116. expect(fileData.getContent()).to.equal(
  117. 'the quick brown fox jumps over the lazy dog'
  118. )
  119. expect(fileData.getContent({ filterTrackedDeletes: true })).to.equal(
  120. 'the brown fox jumps over the dog'
  121. )
  122. })
  123. it('getContent should keep tracked insertions when passed option to remove tracked changes', function () {
  124. const fileData = new StringFileData(
  125. 'the quick brown fox jumps over the lazy dog',
  126. undefined,
  127. [
  128. {
  129. range: { pos: 4, length: 6 },
  130. tracking: {
  131. type: 'insert',
  132. ts: '2024-01-01T00:00:00.000Z',
  133. userId: 'user1',
  134. },
  135. },
  136. {
  137. range: { pos: 35, length: 5 },
  138. tracking: {
  139. type: 'delete',
  140. ts: '2023-01-01T00:00:00.000Z',
  141. userId: 'user2',
  142. },
  143. },
  144. ]
  145. )
  146. expect(fileData.getContent()).to.equal(
  147. 'the quick brown fox jumps over the lazy dog'
  148. )
  149. expect(fileData.getContent({ filterTrackedDeletes: true })).to.equal(
  150. 'the quick brown fox jumps over the dog'
  151. )
  152. })
  153. })