string_file_data.test.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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().toRaw()).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().toRaw()).to.eql([
  58. { id: 'comm1', ranges: [{ pos: 5, length: 10 }] },
  59. { id: 'comm2', ranges: [{ pos: 20, length: 5 }] },
  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. },
  75. {
  76. id: 'comm2',
  77. ranges: [
  78. {
  79. pos: 20,
  80. length: 5,
  81. },
  82. ],
  83. resolved: true,
  84. },
  85. ],
  86. })
  87. expect(fileData.getComments().toRaw()).to.eql([
  88. { id: 'comm1', ranges: [{ pos: 5, length: 10 }] },
  89. { id: 'comm2', ranges: [{ pos: 20, length: 5 }], resolved: true },
  90. ])
  91. })
  92. it('getContent should filter out tracked deletions when passed option', function () {
  93. const fileData = new StringFileData(
  94. 'the quick brown fox jumps over the lazy dog',
  95. undefined,
  96. [
  97. {
  98. range: { pos: 4, length: 6 },
  99. tracking: {
  100. type: 'delete',
  101. ts: '2024-01-01T00:00:00.000Z',
  102. userId: 'user1',
  103. },
  104. },
  105. {
  106. range: { pos: 35, length: 5 },
  107. tracking: {
  108. type: 'delete',
  109. ts: '2023-01-01T00:00:00.000Z',
  110. userId: 'user2',
  111. },
  112. },
  113. ]
  114. )
  115. expect(fileData.getContent()).to.equal(
  116. 'the quick brown fox jumps over the lazy dog'
  117. )
  118. expect(fileData.getContent({ filterTrackedDeletes: true })).to.equal(
  119. 'the brown fox jumps over the dog'
  120. )
  121. })
  122. it('getContent should keep tracked insertions when passed option to remove tracked changes', function () {
  123. const fileData = new StringFileData(
  124. 'the quick brown fox jumps over the lazy dog',
  125. undefined,
  126. [
  127. {
  128. range: { pos: 4, length: 6 },
  129. tracking: {
  130. type: 'insert',
  131. ts: '2024-01-01T00:00:00.000Z',
  132. userId: 'user1',
  133. },
  134. },
  135. {
  136. range: { pos: 35, length: 5 },
  137. tracking: {
  138. type: 'delete',
  139. ts: '2023-01-01T00:00:00.000Z',
  140. userId: 'user2',
  141. },
  142. },
  143. ]
  144. )
  145. expect(fileData.getContent()).to.equal(
  146. 'the quick brown fox jumps over the lazy dog'
  147. )
  148. expect(fileData.getContent({ filterTrackedDeletes: true })).to.equal(
  149. 'the quick brown fox jumps over the dog'
  150. )
  151. })
  152. })