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

[web] Fix unique project name generator

GitOrigin-RevId: 9b30041ab532b4ad87ee647fb7d5b280b069ce12
This commit is contained in:
ilkin-overleaf 2024-01-23 12:41:24 +02:00 committed by Copybot
parent 7e11e8a62d
commit 784ebcbd57
2 changed files with 79 additions and 11 deletions

View file

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

View file

@ -273,7 +273,7 @@ describe('ProjectDetailsHandler', function () {
{ _id: 140, name: 'numeric (40)' }, { _id: 140, name: 'numeric (40)' },
{ _id: 141, name: 'Yearbook (2021)' }, { _id: 141, name: 'Yearbook (2021)' },
{ _id: 142, name: 'Yearbook (2021) (1)' }, { _id: 142, name: 'Yearbook (2021) (1)' },
{ _id: 142, name: 'Resume (2020' }, { _id: 143, name: 'Resume (2020' },
], ],
readAndWrite: [ readAndWrite: [
{ _id: 4, name: 'name2' }, { _id: 4, name: 'name2' },
@ -362,15 +362,6 @@ describe('ProjectDetailsHandler', function () {
expect(name).to.equal('numeric (21)') 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 () { it('should not find a numeric index lower than the one already present', async function () {
const name = await this.handler.promises.generateUniqueName( const name = await this.handler.promises.generateUniqueName(
this.user._id, this.user._id,
@ -415,6 +406,83 @@ describe('ProjectDetailsHandler', function () {
expect(name).to.equal('Yearbook (2021') 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 () { describe('fixProjectName', function () {