Merge pull request #13244 from overleaf/msm-fix-split-test-client

[web] Fix split test assignments not sent via LocalsHelper in Server Pro

GitOrigin-RevId: 3a69fda3a4a0e66ecde3772d918d9c6d46e89ecf
This commit is contained in:
Miguel Serrano 2023-05-31 15:24:38 +02:00 committed by Copybot
parent a74eecd110
commit db70128d03
2 changed files with 60 additions and 25 deletions

View file

@ -49,37 +49,37 @@ const DEFAULT_ASSIGNMENT = {
* @returns {Promise<{variant: string, analytics: {segmentation: {splitTest: string, variant: string, phase: string, versionNumber: number}|{}}}>} * @returns {Promise<{variant: string, analytics: {segmentation: {splitTest: string, variant: string, phase: string, versionNumber: number}|{}}}>}
*/ */
async function getAssignment(req, res, splitTestName, { sync = false } = {}) { async function getAssignment(req, res, splitTestName, { sync = false } = {}) {
if (!Features.hasFeature('saas')) {
return _getNonSaasAssignment(splitTestName)
}
const query = req.query || {} const query = req.query || {}
let assignment let assignment
// Check the query string for an override, ignoring an invalid value if (!Features.hasFeature('saas')) {
const queryVariant = query[splitTestName] assignment = _getNonSaasAssignment(splitTestName)
if (queryVariant) { } else {
const variants = await _getVariantNames(splitTestName) // Check the query string for an override, ignoring an invalid value
if (variants.includes(queryVariant)) { const queryVariant = query[splitTestName]
assignment = { if (queryVariant) {
variant: queryVariant, const variants = await _getVariantNames(splitTestName)
analytics: { if (variants.includes(queryVariant)) {
segmentation: {}, assignment = {
}, variant: queryVariant,
analytics: {
segmentation: {},
},
}
} }
} }
}
if (!assignment) { if (!assignment) {
const { userId, analyticsId } = AnalyticsManager.getIdsFromSession( const { userId, analyticsId } = AnalyticsManager.getIdsFromSession(
req.session req.session
) )
assignment = await _getAssignment(splitTestName, { assignment = await _getAssignment(splitTestName, {
analyticsId, analyticsId,
userId, userId,
session: req.session, session: req.session,
sync, sync,
}) })
}
} }
LocalsHelper.setSplitTestVariant( LocalsHelper.setSplitTestVariant(

View file

@ -257,6 +257,41 @@ describe('SplitTestHandler', function () {
assert.equal('default', assignment.variant) assert.equal('default', assignment.variant)
}) })
}) })
describe('save assignments to res.locals', function () {
beforeEach(function () {
this.AnalyticsManager.getIdsFromSession.returns({
userId: 'abc123abc123',
})
})
it('when in SaaS mode it should set the variant', async function () {
await this.SplitTestHandler.promises.getAssignment(
this.req,
this.res,
'active-test'
)
expect(this.LocalsHelper.setSplitTestVariant).to.have.been.calledWith(
this.res.locals,
'active-test',
'variant-1'
)
})
it('when not in SaaS mode it should set the default variant', async function () {
this.Settings.overleaf = undefined
await this.SplitTestHandler.promises.getAssignment(
this.req,
this.res,
'active-test'
)
expect(this.LocalsHelper.setSplitTestVariant).to.have.been.calledWith(
this.res.locals,
'active-test',
'default'
)
})
})
}) })
function makeSplitTest( function makeSplitTest(