Преглед изворни кода

Merge pull request #16610 from overleaf/ii-generate-unique-name-for-project

[web] Fix unique project name generator

GitOrigin-RevId: 9b30041ab532b4ad87ee647fb7d5b280b069ce12
ilkin-overleaf пре 2 година
родитељ
комит
784ebcbd57

+ 1 - 1
services/web/app/src/Features/Project/ProjectHelper.js

@@ -146,7 +146,6 @@ function _addNumericSuffixToProjectName(name, allProjectNames, maxLength) {
   const match = name.match(NUMERIC_SUFFIX_MATCH)
   let basename = name
   let n = 1
-  const last = allProjectNames.size + n
 
   if (match != null) {
     basename = name.replace(NUMERIC_SUFFIX_MATCH, '')
@@ -158,6 +157,7 @@ function _addNumericSuffixToProjectName(name, allProjectNames, maxLength) {
   const projectNamesWithSamePrefix = Array.from(allProjectNames).filter(name =>
     prefixMatcher.test(name)
   )
+  const last = allProjectNames.size + n
   const nIsLikelyAYear = n > 1000 && projectNamesWithSamePrefix.length < n / 2
   if (nIsLikelyAYear) {
     basename = name

+ 78 - 10
services/web/test/unit/src/Project/ProjectDetailsHandlerTests.js

@@ -273,7 +273,7 @@ describe('ProjectDetailsHandler', function () {
           { _id: 140, name: 'numeric (40)' },
           { _id: 141, name: 'Yearbook (2021)' },
           { _id: 142, name: 'Yearbook (2021) (1)' },
-          { _id: 142, name: 'Resume (2020' },
+          { _id: 143, name: 'Resume (2020' },
         ],
         readAndWrite: [
           { _id: 4, name: 'name2' },
@@ -362,15 +362,6 @@ describe('ProjectDetailsHandler', function () {
       expect(name).to.equal('numeric (21)')
     })
 
-    it('should find the next available numeric index when a numeric index is already present', async function () {
-      const name = await this.handler.promises.generateUniqueName(
-        this.user._id,
-        'numeric (5)',
-        []
-      )
-      expect(name).to.equal('numeric (21)')
-    })
-
     it('should not find a numeric index lower than the one already present', async function () {
       const name = await this.handler.promises.generateUniqueName(
         this.user._id,
@@ -415,6 +406,83 @@ describe('ProjectDetailsHandler', function () {
         expect(name).to.equal('Yearbook (2021')
       })
     })
+
+    describe('numeric index is already present', function () {
+      describe('when there is 1 project "x (2)"', function () {
+        beforeEach(function () {
+          const usersProjects = {
+            owned: [{ _id: 1, name: 'x (2)' }],
+          }
+          this.ProjectGetter.promises.findAllUsersProjects.resolves(
+            usersProjects
+          )
+        })
+
+        it('should produce "x (3)" uploading a zip with name "x (2)"', async function () {
+          const name = await this.handler.promises.generateUniqueName(
+            this.user._id,
+            'x (2)',
+            []
+          )
+          expect(name).to.equal('x (3)')
+        })
+      })
+
+      describe('when there are 2 projects "x (2)" and "x (3)"', function () {
+        beforeEach(function () {
+          const usersProjects = {
+            owned: [
+              { _id: 1, name: 'x (2)' },
+              { _id: 2, name: 'x (3)' },
+            ],
+          }
+          this.ProjectGetter.promises.findAllUsersProjects.resolves(
+            usersProjects
+          )
+        })
+
+        it('should produce "x (4)" when uploading a zip with name "x (2)"', async function () {
+          const name = await this.handler.promises.generateUniqueName(
+            this.user._id,
+            'x (2)',
+            []
+          )
+          expect(name).to.equal('x (4)')
+        })
+      })
+
+      describe('when there are 2 projects "x (2)" and "x (4)"', function () {
+        beforeEach(function () {
+          const usersProjects = {
+            owned: [
+              { _id: 1, name: 'x (2)' },
+              { _id: 2, name: 'x (4)' },
+            ],
+          }
+          this.ProjectGetter.promises.findAllUsersProjects.resolves(
+            usersProjects
+          )
+        })
+
+        it('should produce "x (3)" when uploading a zip with name "x (2)"', async function () {
+          const name = await this.handler.promises.generateUniqueName(
+            this.user._id,
+            'x (2)',
+            []
+          )
+          expect(name).to.equal('x (3)')
+        })
+
+        it('should produce "x (5)" when uploading a zip with name "x (4)"', async function () {
+          const name = await this.handler.promises.generateUniqueName(
+            this.user._id,
+            'x (4)',
+            []
+          )
+          expect(name).to.equal('x (5)')
+        })
+      })
+    })
   })
 
   describe('fixProjectName', function () {