project-snapshot.test.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. import { expect } from 'chai'
  2. import fetchMock from 'fetch-mock'
  3. import { ProjectSnapshot } from '@/infrastructure/project-snapshot'
  4. describe('ProjectSnapshot', function () {
  5. let snapshot: ProjectSnapshot
  6. const projectId = 'project-id'
  7. beforeEach(function () {
  8. snapshot = new ProjectSnapshot(projectId)
  9. })
  10. describe('before initialization', function () {
  11. describe('getDocPaths()', function () {
  12. it('returns an empty string', function () {
  13. expect(snapshot.getDocPaths()).to.deep.equal([])
  14. })
  15. })
  16. describe('getDocContents()', function () {
  17. it('returns null', function () {
  18. expect(snapshot.getDocContents('main.tex')).to.be.null
  19. })
  20. })
  21. })
  22. const files = {
  23. 'main.tex': {
  24. contents: '\\documentclass{article}\netc.',
  25. hash: 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb',
  26. },
  27. 'hello.txt': {
  28. contents: 'Hello history!',
  29. hash: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
  30. },
  31. 'goodbye.txt': {
  32. contents: "We're done here",
  33. hash: 'dddddddddddddddddddddddddddddddddddddddd',
  34. },
  35. }
  36. const chunk = {
  37. history: {
  38. snapshot: {
  39. files: {},
  40. },
  41. changes: [
  42. {
  43. operations: [
  44. {
  45. pathname: 'hello.txt',
  46. file: {
  47. hash: files['hello.txt'].hash,
  48. stringLength: files['hello.txt'].contents.length,
  49. },
  50. },
  51. {
  52. pathname: 'main.tex',
  53. file: {
  54. hash: files['main.tex'].hash,
  55. stringLength: files['main.tex'].contents.length,
  56. },
  57. },
  58. {
  59. pathname: 'frog.jpg',
  60. file: {
  61. hash: 'cccccccccccccccccccccccccccccccccccccccc',
  62. byteLength: 97080,
  63. },
  64. },
  65. ],
  66. timestamp: '2025-01-01T12:00:00.000Z',
  67. },
  68. ],
  69. },
  70. startVersion: 0,
  71. }
  72. const changes = [
  73. {
  74. operations: [
  75. {
  76. pathname: 'hello.txt',
  77. textOperation: ['Quote: ', files['hello.txt'].contents.length],
  78. },
  79. {
  80. pathname: 'goodbye.txt',
  81. file: {
  82. hash: files['goodbye.txt'].hash,
  83. stringLength: files['goodbye.txt'].contents.length,
  84. },
  85. },
  86. ],
  87. timestamp: '2025-01-01T13:00:00.000Z',
  88. },
  89. ]
  90. function mockFlush(
  91. opts: { repeat?: number; failOnCall?: (call: number) => boolean } = {}
  92. ) {
  93. let currentCall = 0
  94. const getResponse = () => {
  95. currentCall += 1
  96. return opts.failOnCall?.(currentCall) ? 500 : 200
  97. }
  98. fetchMock.post(`/project/${projectId}/flush`, getResponse, {
  99. name: 'flush',
  100. repeat: opts.repeat ?? 1,
  101. })
  102. }
  103. function mockLatestChunk() {
  104. fetchMock.getOnce(
  105. `/project/${projectId}/latest/history`,
  106. { chunk },
  107. { name: 'latest-chunk' }
  108. )
  109. }
  110. function mockChanges() {
  111. fetchMock.getOnce(`/project/${projectId}/changes?since=1`, changes, {
  112. name: 'changes-1',
  113. })
  114. fetchMock.get(`/project/${projectId}/changes?since=2`, [], {
  115. name: 'changes-2',
  116. })
  117. }
  118. function mockBlobs(paths = Object.keys(files) as (keyof typeof files)[]) {
  119. for (const path of paths) {
  120. const file = files[path]
  121. fetchMock.get(`/project/${projectId}/blob/${file.hash}`, file.contents)
  122. }
  123. }
  124. async function initializeSnapshot() {
  125. mockFlush()
  126. mockLatestChunk()
  127. mockBlobs(['main.tex', 'hello.txt'])
  128. await snapshot.refresh()
  129. fetchMock.removeRoutes().clearHistory()
  130. }
  131. describe('after initialization', function () {
  132. beforeEach(initializeSnapshot)
  133. describe('getDocPaths()', function () {
  134. it('returns the editable docs', function () {
  135. expect(snapshot.getDocPaths()).to.have.members([
  136. 'main.tex',
  137. 'hello.txt',
  138. ])
  139. })
  140. })
  141. describe('getDocContents()', function () {
  142. it('returns the doc contents', function () {
  143. expect(snapshot.getDocContents('main.tex')).to.equal(
  144. files['main.tex'].contents
  145. )
  146. })
  147. it('returns null for binary files', function () {
  148. expect(snapshot.getDocContents('frog.jpg')).to.be.null
  149. })
  150. it('returns null for inexistent files', function () {
  151. expect(snapshot.getDocContents('does-not-exist.txt')).to.be.null
  152. })
  153. })
  154. })
  155. async function refreshSnapshot() {
  156. mockFlush()
  157. mockChanges()
  158. mockBlobs(['goodbye.txt'])
  159. await snapshot.refresh()
  160. fetchMock.removeRoutes().clearHistory()
  161. }
  162. describe('after refresh', function () {
  163. beforeEach(initializeSnapshot)
  164. beforeEach(refreshSnapshot)
  165. afterEach(function () {
  166. fetchMock.removeRoutes().clearHistory()
  167. })
  168. describe('getDocPaths()', function () {
  169. it('returns the editable docs', function () {
  170. expect(snapshot.getDocPaths()).to.have.members([
  171. 'main.tex',
  172. 'hello.txt',
  173. 'goodbye.txt',
  174. ])
  175. })
  176. })
  177. describe('getDocContents()', function () {
  178. it('returns the up to date content', function () {
  179. expect(snapshot.getDocContents('hello.txt')).to.equal(
  180. `Quote: ${files['hello.txt'].contents}`
  181. )
  182. })
  183. it('returns contents of new files', function () {
  184. expect(snapshot.getDocContents('goodbye.txt')).to.equal(
  185. files['goodbye.txt'].contents
  186. )
  187. })
  188. })
  189. })
  190. describe('concurrency', function () {
  191. afterEach(function () {
  192. fetchMock.removeRoutes().clearHistory()
  193. })
  194. specify('two concurrent inits', async function () {
  195. mockFlush({ repeat: 2 })
  196. mockLatestChunk()
  197. mockChanges()
  198. mockBlobs()
  199. await Promise.all([snapshot.refresh(), snapshot.refresh()])
  200. // The first request initializes, the second request loads changes
  201. expect(fetchMock.callHistory.calls('flush')).to.have.length(2)
  202. expect(fetchMock.callHistory.calls('latest-chunk')).to.have.length(1)
  203. expect(fetchMock.callHistory.calls('changes-1')).to.have.length(1)
  204. })
  205. specify('three concurrent inits', async function () {
  206. mockFlush({ repeat: 2 })
  207. mockLatestChunk()
  208. mockChanges()
  209. mockBlobs()
  210. await Promise.all([
  211. snapshot.refresh(),
  212. snapshot.refresh(),
  213. snapshot.refresh(),
  214. ])
  215. // The first request initializes, the second and third are combined and
  216. // load changes
  217. expect(fetchMock.callHistory.calls('flush')).to.have.length(2)
  218. expect(fetchMock.callHistory.calls('latest-chunk')).to.have.length(1)
  219. expect(fetchMock.callHistory.calls('changes-1')).to.have.length(1)
  220. })
  221. specify('two concurrent inits - first fails', async function () {
  222. mockFlush({ repeat: 2, failOnCall: call => call === 1 })
  223. mockLatestChunk()
  224. mockBlobs()
  225. const results = await Promise.allSettled([
  226. snapshot.refresh(),
  227. snapshot.refresh(),
  228. ])
  229. // The first init fails, but the second succeeds
  230. expect(results.filter(r => r.status === 'fulfilled')).to.have.length(1)
  231. expect(fetchMock.callHistory.calls('flush')).to.have.length(2)
  232. expect(fetchMock.callHistory.calls('latest-chunk')).to.have.length(1)
  233. expect(fetchMock.callHistory.calls('changes-1')).to.have.length(0)
  234. })
  235. specify('three concurrent inits - second fails', async function () {
  236. mockFlush({ repeat: 4, failOnCall: call => call === 2 })
  237. mockLatestChunk()
  238. mockChanges()
  239. mockBlobs()
  240. const results = await Promise.allSettled([
  241. snapshot.refresh(),
  242. snapshot.refresh(),
  243. snapshot.refresh(),
  244. ])
  245. // Another request afterwards
  246. await snapshot.refresh()
  247. // The first init succeeds, the two queued requests fail, the last request
  248. // succeeds
  249. expect(results.filter(r => r.status === 'fulfilled')).to.have.length(1)
  250. expect(fetchMock.callHistory.calls('flush')).to.have.length(3)
  251. expect(fetchMock.callHistory.calls('latest-chunk')).to.have.length(1)
  252. expect(fetchMock.callHistory.calls('changes-1')).to.have.length(1)
  253. expect(fetchMock.callHistory.calls('changes-2')).to.have.length(0)
  254. })
  255. specify('two concurrent load changes', async function () {
  256. mockFlush({ repeat: 3 })
  257. mockLatestChunk()
  258. mockChanges()
  259. mockBlobs()
  260. // Initialize
  261. await snapshot.refresh()
  262. // Two concurrent load changes
  263. await Promise.all([snapshot.refresh(), snapshot.refresh()])
  264. // One init, two load changes
  265. expect(fetchMock.callHistory.calls('flush')).to.have.length(3)
  266. expect(fetchMock.callHistory.calls('latest-chunk')).to.have.length(1)
  267. expect(fetchMock.callHistory.calls('changes-1')).to.have.length(1)
  268. expect(fetchMock.callHistory.calls('changes-2')).to.have.length(1)
  269. })
  270. specify('three concurrent load changes', async function () {
  271. mockFlush({ repeat: 3 })
  272. mockLatestChunk()
  273. mockChanges()
  274. mockBlobs()
  275. // Initialize
  276. await snapshot.refresh()
  277. // Three concurrent load changes
  278. await Promise.all([
  279. snapshot.refresh(),
  280. snapshot.refresh(),
  281. snapshot.refresh(),
  282. ])
  283. // One init, two load changes (the two last are queued and combined)
  284. expect(fetchMock.callHistory.calls('flush')).to.have.length(3)
  285. expect(fetchMock.callHistory.calls('latest-chunk')).to.have.length(1)
  286. expect(fetchMock.callHistory.calls('changes-1')).to.have.length(1)
  287. expect(fetchMock.callHistory.calls('changes-2')).to.have.length(1)
  288. })
  289. specify('two concurrent load changes - first fails', async function () {
  290. mockFlush({ repeat: 3, failOnCall: call => call === 2 })
  291. mockLatestChunk()
  292. mockChanges()
  293. mockBlobs()
  294. // Initialize
  295. await snapshot.refresh()
  296. // Two concurrent load changes
  297. const results = await Promise.allSettled([
  298. snapshot.refresh(),
  299. snapshot.refresh(),
  300. ])
  301. // One init, one load changes fails, the second succeeds
  302. expect(results.filter(r => r.status === 'fulfilled')).to.have.length(1)
  303. expect(fetchMock.callHistory.calls('flush')).to.have.length(3)
  304. expect(fetchMock.callHistory.calls('latest-chunk')).to.have.length(1)
  305. expect(fetchMock.callHistory.calls('changes-1')).to.have.length(1)
  306. expect(fetchMock.callHistory.calls('changes-2')).to.have.length(0)
  307. })
  308. specify('three concurrent load changes - second fails', async function () {
  309. mockFlush({ repeat: 4, failOnCall: call => call === 3 })
  310. mockLatestChunk()
  311. mockChanges()
  312. mockBlobs()
  313. // Initialize
  314. await snapshot.refresh()
  315. // Two concurrent load changes
  316. const results = await Promise.allSettled([
  317. snapshot.refresh(),
  318. snapshot.refresh(),
  319. snapshot.refresh(),
  320. ])
  321. // Another request afterwards
  322. await snapshot.refresh()
  323. // One init, one load changes succeeds, the second and third are combined
  324. // and fail, the last request succeeds
  325. expect(results.filter(r => r.status === 'fulfilled')).to.have.length(1)
  326. expect(fetchMock.callHistory.calls('flush')).to.have.length(4)
  327. expect(fetchMock.callHistory.calls('latest-chunk')).to.have.length(1)
  328. expect(fetchMock.callHistory.calls('changes-1')).to.have.length(1)
  329. expect(fetchMock.callHistory.calls('changes-2')).to.have.length(1)
  330. })
  331. })
  332. })