Merge pull request #18717 from overleaf/jel-isSplitTestActive

[web] Add helper for checking if test is active

GitOrigin-RevId: 1e8987517915e3947812086863da11fad252daf1
This commit is contained in:
Jessica Lawshe 2024-06-05 10:20:18 -05:00 committed by Copybot
parent 8fe46feffb
commit 3948b6ddb3
2 changed files with 43 additions and 0 deletions

View file

@ -522,6 +522,16 @@ async function _getSplitTest(name) {
}
}
async function isSplitTestActive(splitTestName) {
try {
const splitTest = await _getSplitTest(splitTestName)
const currentVersion = SplitTestUtils.getCurrentVersion(splitTest)
return currentVersion?.active
} catch (e) {
logger.log('unable to check if split test is active ', splitTestName)
}
}
module.exports = {
getPercentile,
getAssignment: callbackify(getAssignment),
@ -535,5 +545,6 @@ module.exports = {
getAssignmentForMongoUser,
getAssignmentForUser,
getActiveAssignmentsForUser,
isSplitTestActive,
},
}

View file

@ -15,6 +15,7 @@ describe('SplitTestHandler', function () {
beforeEach(function () {
this.splitTests = [
makeSplitTest('active-test'),
makeSplitTest('not-active-test', { active: false }),
makeSplitTest('legacy-test'),
makeSplitTest('no-analytics-test-1', { analyticsEnabled: false }),
makeSplitTest('no-analytics-test-2', {
@ -194,6 +195,11 @@ describe('SplitTestHandler', function () {
variantName: 'variant-1',
versionNumber: 2,
},
'not-active-test': {
phase: 'release',
variantName: 'variant-1',
versionNumber: 1,
},
})
})
})
@ -305,6 +311,32 @@ describe('SplitTestHandler', function () {
)
})
})
describe('isSplitTestActive', function () {
it('returns false when current version is not active', async function () {
const res =
await this.SplitTestHandler.promises.isSplitTestActive(
'not-active-test'
)
expect(res).to.be.false
})
it('returns undefined false when current version is active', async function () {
const res =
await this.SplitTestHandler.promises.isSplitTestActive('active-test')
expect(res).to.be.true
})
it('returns undefined when there is an error checking', async function () {
this.SplitTestCache.get.rejects(new Error('oops'))
const res =
await this.SplitTestHandler.promises.isSplitTestActive('active-test')
expect(res).to.be.undefined
})
it('returns undefined when there is no test', async function () {
const res =
await this.SplitTestHandler.promises.isSplitTestActive('not-a-test')
expect(res).to.be.undefined
})
})
})
function makeSplitTest(