auto-select-file.test.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. import { expect } from 'chai'
  2. import type { HistoryContextValue } from '../../../../../frontend/js/features/history/context/types/history-context-value'
  3. import type { FileDiff } from '../../../../../frontend/js/features/history/services/types/file'
  4. import { autoSelectFile } from '../../../../../frontend/js/features/history/utils/auto-select-file'
  5. import type { User } from '../../../../../frontend/js/features/history/services/types/shared'
  6. import { UpdateSelection } from '../../../../../frontend/js/features/history/services/types/update'
  7. describe('autoSelectFile', function () {
  8. const historyUsers: User[] = [
  9. {
  10. first_name: 'first_name',
  11. last_name: 'last_name',
  12. email: 'email@overleaf.com',
  13. id: '6266xb6b7a366460a66186xx',
  14. },
  15. ]
  16. describe('for comparing version with previous', function () {
  17. const comparing = false
  18. it('return the file with `edited` as the last operation', function () {
  19. const files: FileDiff[] = [
  20. {
  21. pathname: 'main.tex',
  22. },
  23. {
  24. pathname: 'sample.bib',
  25. },
  26. {
  27. pathname: 'frog.jpg',
  28. },
  29. {
  30. pathname: 'newfile5.tex',
  31. },
  32. {
  33. pathname: 'newfolder1/newfolder2/newfile2.tex',
  34. },
  35. {
  36. pathname: 'newfolder1/newfile10.tex',
  37. operation: 'edited',
  38. },
  39. ]
  40. const updates: HistoryContextValue['updates'] = [
  41. {
  42. fromV: 25,
  43. toV: 26,
  44. meta: {
  45. users: historyUsers,
  46. start_ts: 1680888731881,
  47. end_ts: 1680888731881,
  48. },
  49. labels: [],
  50. pathnames: ['newfolder1/newfile10.tex'],
  51. project_ops: [],
  52. },
  53. {
  54. fromV: 23,
  55. toV: 25,
  56. meta: {
  57. users: historyUsers,
  58. start_ts: 1680888725098,
  59. end_ts: 1680888729123,
  60. },
  61. labels: [],
  62. pathnames: [],
  63. project_ops: [
  64. {
  65. rename: {
  66. pathname: 'newfolder1/newfile3.tex',
  67. newPathname: 'newfolder1/newfile10.tex',
  68. },
  69. atV: 24,
  70. },
  71. {
  72. rename: {
  73. pathname: 'newfile3.tex',
  74. newPathname: 'newfolder1/newfile3.tex',
  75. },
  76. atV: 23,
  77. },
  78. ],
  79. },
  80. {
  81. fromV: 22,
  82. toV: 23,
  83. meta: {
  84. users: historyUsers,
  85. start_ts: 1680888721015,
  86. end_ts: 1680888721015,
  87. },
  88. labels: [],
  89. pathnames: ['newfile3.tex'],
  90. project_ops: [],
  91. },
  92. {
  93. fromV: 19,
  94. toV: 22,
  95. meta: {
  96. users: historyUsers,
  97. start_ts: 1680888715364,
  98. end_ts: 1680888718726,
  99. },
  100. labels: [],
  101. pathnames: [],
  102. project_ops: [
  103. {
  104. rename: {
  105. pathname: 'newfolder1/newfolder2/newfile3.tex',
  106. newPathname: 'newfile3.tex',
  107. },
  108. atV: 21,
  109. },
  110. {
  111. rename: {
  112. pathname: 'newfolder1/newfile2.tex',
  113. newPathname: 'newfolder1/newfolder2/newfile2.tex',
  114. },
  115. atV: 20,
  116. },
  117. {
  118. rename: {
  119. pathname: 'newfolder1/newfile5.tex',
  120. newPathname: 'newfile5.tex',
  121. },
  122. atV: 19,
  123. },
  124. ],
  125. },
  126. {
  127. fromV: 16,
  128. toV: 19,
  129. meta: {
  130. users: historyUsers,
  131. start_ts: 1680888705042,
  132. end_ts: 1680888712662,
  133. },
  134. labels: [],
  135. pathnames: [
  136. 'main.tex',
  137. 'newfolder1/newfile2.tex',
  138. 'newfolder1/newfile5.tex',
  139. ],
  140. project_ops: [],
  141. },
  142. {
  143. fromV: 0,
  144. toV: 16,
  145. meta: {
  146. users: historyUsers,
  147. start_ts: 1680888456499,
  148. end_ts: 1680888640774,
  149. },
  150. labels: [],
  151. pathnames: [],
  152. project_ops: [
  153. {
  154. add: {
  155. pathname: 'newfolder1/newfile2.tex',
  156. },
  157. atV: 15,
  158. },
  159. {
  160. remove: {
  161. pathname: 'newfile2.tex',
  162. },
  163. atV: 14,
  164. },
  165. {
  166. rename: {
  167. pathname: 'newfolder1/frog.jpg',
  168. newPathname: 'frog.jpg',
  169. },
  170. atV: 13,
  171. },
  172. {
  173. rename: {
  174. pathname: 'newfolder1/newfile2.tex',
  175. newPathname: 'newfile2.tex',
  176. },
  177. atV: 12,
  178. },
  179. {
  180. rename: {
  181. pathname: 'newfile5.tex',
  182. newPathname: 'newfolder1/newfile5.tex',
  183. },
  184. atV: 11,
  185. },
  186. {
  187. rename: {
  188. pathname: 'newfile4.tex',
  189. newPathname: 'newfile5.tex',
  190. },
  191. atV: 10,
  192. },
  193. {
  194. add: {
  195. pathname: 'newfile4.tex',
  196. },
  197. atV: 9,
  198. },
  199. {
  200. remove: {
  201. pathname: 'newfolder1/newfolder2/newfile1.tex',
  202. },
  203. atV: 8,
  204. },
  205. {
  206. rename: {
  207. pathname: 'frog.jpg',
  208. newPathname: 'newfolder1/frog.jpg',
  209. },
  210. atV: 7,
  211. },
  212. {
  213. add: {
  214. pathname: 'newfolder1/newfolder2/newfile3.tex',
  215. },
  216. atV: 6,
  217. },
  218. {
  219. add: {
  220. pathname: 'newfolder1/newfile2.tex',
  221. },
  222. atV: 5,
  223. },
  224. {
  225. rename: {
  226. pathname: 'newfolder1/newfile1.tex',
  227. newPathname: 'newfolder1/newfolder2/newfile1.tex',
  228. },
  229. atV: 4,
  230. },
  231. {
  232. add: {
  233. pathname: 'newfolder1/newfile1.tex',
  234. },
  235. atV: 3,
  236. },
  237. {
  238. add: {
  239. pathname: 'frog.jpg',
  240. },
  241. atV: 2,
  242. },
  243. {
  244. add: {
  245. pathname: 'sample.bib',
  246. },
  247. atV: 1,
  248. },
  249. {
  250. add: {
  251. pathname: 'main.tex',
  252. },
  253. atV: 0,
  254. },
  255. ],
  256. },
  257. ]
  258. const updateSelection: UpdateSelection = {
  259. update: updates[0],
  260. comparing,
  261. }
  262. const pathname = autoSelectFile(files, updateSelection, updates)
  263. expect(pathname).to.equal('newfolder1/newfile10.tex')
  264. })
  265. it('return file with `added` operation on highest `atV` value if no other operation is available on the latest `updates` entry', function () {
  266. const files: FileDiff[] = [
  267. {
  268. pathname: 'main.tex',
  269. operation: 'added',
  270. },
  271. {
  272. pathname: 'sample.bib',
  273. operation: 'added',
  274. },
  275. {
  276. pathname: 'frog.jpg',
  277. operation: 'added',
  278. },
  279. {
  280. pathname: 'newfile1.tex',
  281. operation: 'added',
  282. },
  283. ]
  284. const updates: HistoryContextValue['updates'] = [
  285. {
  286. fromV: 0,
  287. toV: 4,
  288. meta: {
  289. users: historyUsers,
  290. start_ts: 1680861468999,
  291. end_ts: 1680861491861,
  292. },
  293. labels: [],
  294. pathnames: [],
  295. project_ops: [
  296. {
  297. add: {
  298. pathname: 'newfile1.tex',
  299. },
  300. atV: 3,
  301. },
  302. {
  303. add: {
  304. pathname: 'frog.jpg',
  305. },
  306. atV: 2,
  307. },
  308. {
  309. add: {
  310. pathname: 'sample.bib',
  311. },
  312. atV: 1,
  313. },
  314. {
  315. add: {
  316. pathname: 'main.tex',
  317. },
  318. atV: 0,
  319. },
  320. ],
  321. },
  322. ]
  323. const updateSelection: UpdateSelection = {
  324. update: updates[0],
  325. comparing,
  326. }
  327. const pathname = autoSelectFile(files, updateSelection, updates)
  328. expect(pathname).to.equal('newfile1.tex')
  329. })
  330. it('return the last non-`removed` operation with the highest `atV` value', function () {
  331. const files: FileDiff[] = [
  332. {
  333. pathname: 'main.tex',
  334. operation: 'removed',
  335. deletedAtV: 6,
  336. },
  337. {
  338. pathname: 'sample.bib',
  339. },
  340. {
  341. pathname: 'main2.tex',
  342. operation: 'added',
  343. },
  344. {
  345. pathname: 'main3.tex',
  346. operation: 'added',
  347. },
  348. ]
  349. const updates: HistoryContextValue['updates'] = [
  350. {
  351. fromV: 4,
  352. toV: 7,
  353. meta: {
  354. users: historyUsers,
  355. start_ts: 1680874742389,
  356. end_ts: 1680874755552,
  357. },
  358. labels: [],
  359. pathnames: [],
  360. project_ops: [
  361. {
  362. remove: {
  363. pathname: 'main.tex',
  364. },
  365. atV: 6,
  366. },
  367. {
  368. add: {
  369. pathname: 'main3.tex',
  370. },
  371. atV: 5,
  372. },
  373. {
  374. add: {
  375. pathname: 'main2.tex',
  376. },
  377. atV: 4,
  378. },
  379. ],
  380. },
  381. {
  382. fromV: 0,
  383. toV: 4,
  384. meta: {
  385. users: historyUsers,
  386. start_ts: 1680861975947,
  387. end_ts: 1680861988442,
  388. },
  389. labels: [],
  390. pathnames: [],
  391. project_ops: [
  392. {
  393. remove: {
  394. pathname: 'frog.jpg',
  395. },
  396. atV: 3,
  397. },
  398. {
  399. add: {
  400. pathname: 'frog.jpg',
  401. },
  402. atV: 2,
  403. },
  404. {
  405. add: {
  406. pathname: 'sample.bib',
  407. },
  408. atV: 1,
  409. },
  410. {
  411. add: {
  412. pathname: 'main.tex',
  413. },
  414. atV: 0,
  415. },
  416. ],
  417. },
  418. ]
  419. const updateSelection: UpdateSelection = {
  420. update: updates[0],
  421. comparing,
  422. }
  423. const pathname = autoSelectFile(files, updateSelection, updates)
  424. expect(pathname).to.equal('main3.tex')
  425. })
  426. it('if `removed` is the last operation, and no other operation is available on the latest `updates` entry, with `main.tex` available as a file name somewhere in the file tree, return `main.tex`', function () {
  427. const files: FileDiff[] = [
  428. {
  429. pathname: 'main.tex',
  430. },
  431. {
  432. pathname: 'sample.bib',
  433. },
  434. {
  435. pathname: 'frog.jpg',
  436. },
  437. {
  438. pathname: 'newfolder/maybewillbedeleted.tex',
  439. newPathname: 'newfolder2/maybewillbedeleted.tex',
  440. operation: 'removed',
  441. deletedAtV: 10,
  442. },
  443. ]
  444. const updates: HistoryContextValue['updates'] = [
  445. {
  446. fromV: 9,
  447. toV: 11,
  448. meta: {
  449. users: historyUsers,
  450. start_ts: 1680904414419,
  451. end_ts: 1680904417538,
  452. },
  453. labels: [],
  454. pathnames: [],
  455. project_ops: [
  456. {
  457. remove: {
  458. pathname: 'newfolder2/maybewillbedeleted.tex',
  459. },
  460. atV: 10,
  461. },
  462. {
  463. rename: {
  464. pathname: 'newfolder/maybewillbedeleted.tex',
  465. newPathname: 'newfolder2/maybewillbedeleted.tex',
  466. },
  467. atV: 9,
  468. },
  469. ],
  470. },
  471. {
  472. fromV: 8,
  473. toV: 9,
  474. meta: {
  475. users: historyUsers,
  476. start_ts: 1680904410333,
  477. end_ts: 1680904410333,
  478. },
  479. labels: [],
  480. pathnames: ['newfolder/maybewillbedeleted.tex'],
  481. project_ops: [],
  482. },
  483. {
  484. fromV: 7,
  485. toV: 8,
  486. meta: {
  487. users: historyUsers,
  488. start_ts: 1680904407448,
  489. end_ts: 1680904407448,
  490. },
  491. labels: [],
  492. pathnames: [],
  493. project_ops: [
  494. {
  495. rename: {
  496. pathname: 'newfolder/tobedeleted.tex',
  497. newPathname: 'newfolder/maybewillbedeleted.tex',
  498. },
  499. atV: 7,
  500. },
  501. ],
  502. },
  503. {
  504. fromV: 6,
  505. toV: 7,
  506. meta: {
  507. users: historyUsers,
  508. start_ts: 1680904400839,
  509. end_ts: 1680904400839,
  510. },
  511. labels: [],
  512. pathnames: ['newfolder/tobedeleted.tex'],
  513. project_ops: [],
  514. },
  515. {
  516. fromV: 5,
  517. toV: 6,
  518. meta: {
  519. users: historyUsers,
  520. start_ts: 1680904398544,
  521. end_ts: 1680904398544,
  522. },
  523. labels: [],
  524. pathnames: [],
  525. project_ops: [
  526. {
  527. rename: {
  528. pathname: 'tobedeleted.tex',
  529. newPathname: 'newfolder/tobedeleted.tex',
  530. },
  531. atV: 5,
  532. },
  533. ],
  534. },
  535. {
  536. fromV: 4,
  537. toV: 5,
  538. meta: {
  539. users: historyUsers,
  540. start_ts: 1680904389891,
  541. end_ts: 1680904389891,
  542. },
  543. labels: [],
  544. pathnames: ['tobedeleted.tex'],
  545. project_ops: [],
  546. },
  547. {
  548. fromV: 0,
  549. toV: 4,
  550. meta: {
  551. users: historyUsers,
  552. start_ts: 1680904363778,
  553. end_ts: 1680904385308,
  554. },
  555. labels: [],
  556. pathnames: [],
  557. project_ops: [
  558. {
  559. add: {
  560. pathname: 'tobedeleted.tex',
  561. },
  562. atV: 3,
  563. },
  564. {
  565. add: {
  566. pathname: 'frog.jpg',
  567. },
  568. atV: 2,
  569. },
  570. {
  571. add: {
  572. pathname: 'sample.bib',
  573. },
  574. atV: 1,
  575. },
  576. {
  577. add: {
  578. pathname: 'main.tex',
  579. },
  580. atV: 0,
  581. },
  582. ],
  583. },
  584. ]
  585. const updateSelection: UpdateSelection = {
  586. update: updates[0],
  587. comparing,
  588. }
  589. const pathname = autoSelectFile(files, updateSelection, updates)
  590. expect(pathname).to.equal('main.tex')
  591. })
  592. it('if `removed` is the last operation, and no other operation is available on the latest `updates` entry, with `main.tex` is not available as a file name somewhere in the file tree, return any tex file based on ascending alphabetical order', function () {
  593. const files: FileDiff[] = [
  594. {
  595. pathname: 'certainly_not_main.tex',
  596. },
  597. {
  598. pathname: 'newfile.tex',
  599. },
  600. {
  601. pathname: 'file2.tex',
  602. },
  603. ]
  604. const updates: HistoryContextValue['updates'] = [
  605. {
  606. fromV: 7,
  607. toV: 8,
  608. meta: {
  609. users: historyUsers,
  610. start_ts: 1680905536168,
  611. end_ts: 1680905536168,
  612. },
  613. labels: [],
  614. pathnames: [],
  615. project_ops: [
  616. {
  617. remove: {
  618. pathname: 'newfolder/tobedeleted.txt',
  619. },
  620. atV: 7,
  621. },
  622. ],
  623. },
  624. {
  625. fromV: 6,
  626. toV: 7,
  627. meta: {
  628. users: historyUsers,
  629. start_ts: 1680905531816,
  630. end_ts: 1680905531816,
  631. },
  632. labels: [],
  633. pathnames: ['newfolder/tobedeleted.txt'],
  634. project_ops: [],
  635. },
  636. {
  637. fromV: 0,
  638. toV: 6,
  639. meta: {
  640. users: historyUsers,
  641. start_ts: 1680905492130,
  642. end_ts: 1680905529186,
  643. },
  644. labels: [],
  645. pathnames: [],
  646. project_ops: [
  647. {
  648. rename: {
  649. pathname: 'tobedeleted.txt',
  650. newPathname: 'newfolder/tobedeleted.txt',
  651. },
  652. atV: 5,
  653. },
  654. {
  655. add: {
  656. pathname: 'file2.tex',
  657. },
  658. atV: 4,
  659. },
  660. {
  661. add: {
  662. pathname: 'newfile.tex',
  663. },
  664. atV: 3,
  665. },
  666. {
  667. add: {
  668. pathname: 'tobedeleted.txt',
  669. },
  670. atV: 2,
  671. },
  672. {
  673. rename: {
  674. pathname: 'main.tex',
  675. newPathname: 'certainly_not_main.tex',
  676. },
  677. atV: 1,
  678. },
  679. {
  680. add: {
  681. pathname: 'main.tex',
  682. },
  683. atV: 0,
  684. },
  685. ],
  686. },
  687. ]
  688. const updateSelection: UpdateSelection = {
  689. update: updates[0],
  690. comparing,
  691. }
  692. const pathname = autoSelectFile(files, updateSelection, updates)
  693. expect(pathname).to.equal('certainly_not_main.tex')
  694. })
  695. })
  696. })