persist_changes.test.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. 'use strict'
  2. const { createHash } = require('node:crypto')
  3. const { expect } = require('chai')
  4. const cleanup = require('./support/cleanup')
  5. const fixtures = require('./support/fixtures')
  6. const storage = require('../../../../storage')
  7. const chunkStore = storage.chunkStore
  8. const persistChanges = storage.persistChanges
  9. const core = require('overleaf-editor-core')
  10. const AddFileOperation = core.AddFileOperation
  11. const EditFileOperation = core.EditFileOperation
  12. const TextOperation = core.TextOperation
  13. const Change = core.Change
  14. const Chunk = core.Chunk
  15. const File = core.File
  16. const History = core.History
  17. const Snapshot = core.Snapshot
  18. describe('persistChanges', function () {
  19. beforeEach(cleanup.everything)
  20. beforeEach(fixtures.create)
  21. let farFuture
  22. before(function () {
  23. // used to provide a limit which forces us to persist all of the changes.
  24. farFuture = new Date()
  25. farFuture.setTime(farFuture.getTime() + 7 * 24 * 3600 * 1000)
  26. })
  27. it('persists changes', async function () {
  28. const limitsToPersistImmediately = {
  29. minChangeTimestamp: farFuture,
  30. maxChangeTimestamp: farFuture,
  31. }
  32. const projectId = fixtures.docs.uninitializedProject.id
  33. const change = new Change(
  34. [new AddFileOperation('test.tex', File.fromString(''))],
  35. new Date(),
  36. []
  37. )
  38. const changes = [change]
  39. await chunkStore.initializeProject(projectId)
  40. const result = await persistChanges(
  41. projectId,
  42. changes,
  43. limitsToPersistImmediately,
  44. 0
  45. )
  46. const history = new History(new Snapshot(), changes)
  47. const currentChunk = new Chunk(history, 0)
  48. expect(result).to.deep.equal({
  49. numberOfChangesPersisted: 1,
  50. originalEndVersion: 0,
  51. currentChunk,
  52. resyncNeeded: false,
  53. })
  54. const chunk = await chunkStore.loadLatest(projectId)
  55. expect(chunk.getStartVersion()).to.equal(0)
  56. expect(chunk.getEndVersion()).to.equal(1)
  57. expect(chunk.getChanges().length).to.equal(1)
  58. })
  59. it('persists changes in three chunks', async function () {
  60. const limitsToPersistImmediately = {
  61. maxChunkChanges: 1,
  62. minChangeTimestamp: farFuture,
  63. maxChangeTimestamp: farFuture,
  64. }
  65. const projectId = fixtures.docs.uninitializedProject.id
  66. const firstChange = new Change(
  67. [new AddFileOperation('a.tex', File.fromString(''))],
  68. new Date(),
  69. []
  70. )
  71. const secondChange = new Change(
  72. [new AddFileOperation('b.tex', File.fromString(''))],
  73. new Date(),
  74. []
  75. )
  76. const thirdChange = new Change(
  77. [new AddFileOperation('c.tex', File.fromString(''))],
  78. new Date(),
  79. []
  80. )
  81. const changes = [firstChange, secondChange, thirdChange]
  82. await chunkStore.initializeProject(projectId)
  83. const result = await persistChanges(
  84. projectId,
  85. changes,
  86. limitsToPersistImmediately,
  87. 0
  88. )
  89. const snapshot = Snapshot.fromRaw({
  90. files: {
  91. 'a.tex': {
  92. content: '',
  93. },
  94. 'b.tex': {
  95. content: '',
  96. },
  97. },
  98. })
  99. const history = new History(snapshot, [thirdChange])
  100. const currentChunk = new Chunk(history, 2)
  101. expect(result).to.deep.equal({
  102. numberOfChangesPersisted: 3,
  103. originalEndVersion: 0,
  104. currentChunk,
  105. resyncNeeded: false,
  106. })
  107. const chunk = await chunkStore.loadLatest(projectId)
  108. expect(chunk.getStartVersion()).to.equal(2)
  109. expect(chunk.getEndVersion()).to.equal(3)
  110. expect(chunk.getChanges().length).to.equal(1)
  111. })
  112. it('persists the snapshot at the start of the chunk', async function () {
  113. const limitsToPersistImmediately = {
  114. maxChunkChanges: 2,
  115. minChangeTimestamp: farFuture,
  116. maxChangeTimestamp: farFuture,
  117. }
  118. const projectId = fixtures.docs.uninitializedProject.id
  119. const firstChange = new Change(
  120. [new AddFileOperation('a.tex', File.fromString(''))],
  121. new Date(),
  122. []
  123. )
  124. const secondChange = new Change(
  125. [new AddFileOperation('b.tex', File.fromString(''))],
  126. new Date(),
  127. []
  128. )
  129. const changes = [firstChange, secondChange]
  130. await chunkStore.initializeProject(projectId)
  131. const result = await persistChanges(
  132. projectId,
  133. changes,
  134. limitsToPersistImmediately,
  135. 0
  136. )
  137. const history = new History(new Snapshot(), changes)
  138. const currentChunk = new Chunk(history, 0)
  139. expect(result).to.deep.equal({
  140. numberOfChangesPersisted: 2,
  141. originalEndVersion: 0,
  142. currentChunk,
  143. resyncNeeded: false,
  144. })
  145. const chunk = await chunkStore.loadLatest(projectId)
  146. expect(chunk.getStartVersion()).to.equal(0)
  147. expect(chunk.getEndVersion()).to.equal(2)
  148. expect(chunk.getChanges().length).to.equal(2)
  149. })
  150. it("errors if the version doesn't match the latest chunk", async function () {
  151. const limitsToPersistImmediately = {
  152. minChangeTimestamp: farFuture,
  153. maxChangeTimestamp: farFuture,
  154. }
  155. const projectId = fixtures.docs.uninitializedProject.id
  156. const firstChange = new Change(
  157. [new AddFileOperation('a.tex', File.fromString(''))],
  158. new Date(),
  159. []
  160. )
  161. const secondChange = new Change(
  162. [new AddFileOperation('b.tex', File.fromString(''))],
  163. new Date(),
  164. []
  165. )
  166. const changes = [firstChange, secondChange]
  167. await chunkStore.initializeProject(projectId)
  168. await expect(
  169. persistChanges(projectId, changes, limitsToPersistImmediately, 1)
  170. ).to.be.rejectedWith(
  171. 'client sent updates with end_version 1 but latest chunk has end_version 0'
  172. )
  173. })
  174. describe('content hash validation', function () {
  175. it('acccepts a change with a valid hash', async function () {
  176. const limitsToPersistImmediately = {
  177. minChangeTimestamp: farFuture,
  178. maxChangeTimestamp: farFuture,
  179. }
  180. const projectId = fixtures.docs.uninitializedProject.id
  181. await chunkStore.initializeProject(projectId)
  182. const textOperation = new TextOperation()
  183. textOperation.insert('hello ')
  184. textOperation.retain(5)
  185. textOperation.contentHash = hashString('hello world')
  186. const change = new Change(
  187. [
  188. new AddFileOperation('a.tex', File.fromString('world')),
  189. new EditFileOperation('a.tex', textOperation),
  190. ],
  191. new Date(),
  192. []
  193. )
  194. const changes = [change]
  195. const result = await persistChanges(
  196. projectId,
  197. changes,
  198. limitsToPersistImmediately,
  199. 0
  200. )
  201. expect(result.numberOfChangesPersisted).to.equal(1)
  202. })
  203. it('turns on the resyncNeeded flag if content hash validation fails', async function () {
  204. const limitsToPersistImmediately = {
  205. minChangeTimestamp: farFuture,
  206. maxChangeTimestamp: farFuture,
  207. }
  208. const projectId = fixtures.docs.uninitializedProject.id
  209. await chunkStore.initializeProject(projectId)
  210. const textOperation = new TextOperation()
  211. textOperation.insert('hello ')
  212. textOperation.retain(5)
  213. textOperation.contentHash = hashString('bad hash')
  214. const change = new Change(
  215. [
  216. new AddFileOperation('a.tex', File.fromString('world')),
  217. new EditFileOperation('a.tex', textOperation),
  218. ],
  219. new Date(),
  220. []
  221. )
  222. const changes = [change]
  223. const result = await persistChanges(
  224. projectId,
  225. changes,
  226. limitsToPersistImmediately,
  227. 0
  228. )
  229. expect(result.resyncNeeded).to.be.true
  230. })
  231. })
  232. })
  233. function hashString(s) {
  234. const hash = createHash('sha-1')
  235. hash.update(s)
  236. return hash.digest('hex')
  237. }