FolderStructureBuilder.test.mjs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { vi, expect } from 'vitest'
  2. import mongodb from 'mongodb-legacy'
  3. import sinon from 'sinon'
  4. const { ObjectId } = mongodb
  5. const MODULE_PATH =
  6. '../../../../app/src/Features/Project/FolderStructureBuilder'
  7. describe('FolderStructureBuilder', function () {
  8. beforeEach(async function (ctx) {
  9. vi.doMock('mongodb-legacy', () => ({
  10. default: { ObjectId },
  11. }))
  12. ctx.FolderStructureBuilder = (await import(MODULE_PATH)).default
  13. })
  14. describe('buildFolderStructure', function () {
  15. describe('when given no documents at all', function () {
  16. beforeEach(function (ctx) {
  17. ctx.result = ctx.FolderStructureBuilder.buildFolderStructure([], [])
  18. })
  19. it('returns an empty root folder', function (ctx) {
  20. sinon.assert.match(ctx.result, {
  21. _id: sinon.match.instanceOf(ObjectId),
  22. name: 'rootFolder',
  23. folders: [],
  24. docs: [],
  25. fileRefs: [],
  26. })
  27. })
  28. })
  29. describe('when given documents and files', function () {
  30. beforeEach(function (ctx) {
  31. const docUploads = [
  32. { path: '/main.tex', doc: { _id: 'doc-1', name: 'main.tex' } },
  33. { path: '/foo/other.tex', doc: { _id: 'doc-2', name: 'other.tex' } },
  34. { path: '/foo/other.bib', doc: { _id: 'doc-3', name: 'other.bib' } },
  35. {
  36. path: '/foo/foo1/foo2/another.tex',
  37. doc: { _id: 'doc-4', name: 'another.tex' },
  38. },
  39. ]
  40. const fileUploads = [
  41. { path: '/aaa.jpg', file: { _id: 'file-1', name: 'aaa.jpg' } },
  42. { path: '/foo/bbb.jpg', file: { _id: 'file-2', name: 'bbb.jpg' } },
  43. { path: '/bar/ccc.jpg', file: { _id: 'file-3', name: 'ccc.jpg' } },
  44. ]
  45. ctx.result = ctx.FolderStructureBuilder.buildFolderStructure(
  46. docUploads,
  47. fileUploads
  48. )
  49. })
  50. it('returns a full folder structure', function (ctx) {
  51. sinon.assert.match(ctx.result, {
  52. _id: sinon.match.instanceOf(ObjectId),
  53. name: 'rootFolder',
  54. docs: [{ _id: 'doc-1', name: 'main.tex' }],
  55. fileRefs: [{ _id: 'file-1', name: 'aaa.jpg' }],
  56. folders: [
  57. {
  58. _id: sinon.match.instanceOf(ObjectId),
  59. name: 'foo',
  60. docs: [
  61. { _id: 'doc-2', name: 'other.tex' },
  62. { _id: 'doc-3', name: 'other.bib' },
  63. ],
  64. fileRefs: [{ _id: 'file-2', name: 'bbb.jpg' }],
  65. folders: [
  66. {
  67. _id: sinon.match.instanceOf(ObjectId),
  68. name: 'foo1',
  69. docs: [],
  70. fileRefs: [],
  71. folders: [
  72. {
  73. _id: sinon.match.instanceOf(ObjectId),
  74. name: 'foo2',
  75. docs: [{ _id: 'doc-4', name: 'another.tex' }],
  76. fileRefs: [],
  77. folders: [],
  78. },
  79. ],
  80. },
  81. ],
  82. },
  83. {
  84. _id: sinon.match.instanceOf(ObjectId),
  85. name: 'bar',
  86. docs: [],
  87. fileRefs: [{ _id: 'file-3', name: 'ccc.jpg' }],
  88. folders: [],
  89. },
  90. ],
  91. })
  92. })
  93. })
  94. describe('when given duplicate files', function () {
  95. it('throws an error', function (ctx) {
  96. const docUploads = [
  97. { path: '/foo/doc.tex', doc: { _id: 'doc-1', name: 'doc.tex' } },
  98. { path: '/foo/doc.tex', doc: { _id: 'doc-2', name: 'doc.tex' } },
  99. ]
  100. expect(() =>
  101. ctx.FolderStructureBuilder.buildFolderStructure(docUploads, [])
  102. ).to.throw()
  103. })
  104. })
  105. })
  106. })