| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509 |
- const Path = require('path')
- const SandboxedModule = require('sandboxed-module')
- const sinon = require('sinon')
- const { ObjectId } = require('mongodb-legacy')
- const { assert, expect } = require('chai')
- const MockRequest = require('../helpers/MockRequest')
- const MockResponse = require('../helpers/MockResponse')
- const MODULE_PATH = Path.join(
- __dirname,
- '../../../../app/src/Features/SplitTests/SplitTestHandler'
- )
- describe('SplitTestHandler', function () {
- beforeEach(function () {
- this.splitTests = [
- makeSplitTest('active-test', { versionNumber: 2 }),
- makeSplitTest('not-active-test', { active: false }),
- makeSplitTest('legacy-test'),
- makeSplitTest('no-analytics-test-1', { analyticsEnabled: false }),
- makeSplitTest('no-analytics-test-2', {
- analyticsEnabled: false,
- versionNumber: 2,
- }),
- ]
- this.cachedSplitTests = new Map()
- for (const splitTest of this.splitTests) {
- this.cachedSplitTests.set(splitTest.name, splitTest)
- }
- this.SplitTest = {
- find: sinon.stub().returns({
- exec: sinon.stub().resolves(this.splitTests),
- }),
- }
- this.SplitTestCache = {
- get: sinon.stub().resolves({}),
- }
- this.SplitTestCache.get.resolves(this.cachedSplitTests)
- this.Settings = {
- moduleImportSequence: [],
- overleaf: {},
- devToolbar: {
- enabled: false,
- },
- }
- this.AnalyticsManager = {
- getIdsFromSession: sinon.stub(),
- setUserPropertyForAnalyticsId: sinon.stub().resolves(),
- }
- this.LocalsHelper = {
- setSplitTestVariant: sinon.stub(),
- setSplitTestInfo: sinon.stub(),
- }
- this.SplitTestSessionHandler = {
- collectSessionStats: sinon.stub(),
- getCachedVariant: sinon.stub(),
- setVariantInCache: sinon.stub(),
- }
- this.SplitTestUserGetter = {
- promises: {
- getUser: sinon.stub().resolves(null),
- },
- }
- this.SessionManager = {
- isUserLoggedIn: sinon.stub().returns(false),
- }
- this.SplitTestHandler = SandboxedModule.require(MODULE_PATH, {
- requires: {
- '../User/UserGetter': this.UserGetter,
- './SplitTestCache': this.SplitTestCache,
- '../../models/SplitTest': { SplitTest: this.SplitTest },
- '../User/UserUpdater': {},
- '../Analytics/AnalyticsManager': this.AnalyticsManager,
- './LocalsHelper': this.LocalsHelper,
- './SplitTestSessionHandler': this.SplitTestSessionHandler,
- './SplitTestUserGetter': this.SplitTestUserGetter,
- '../Authentication/SessionManager': this.SessionManager,
- '@overleaf/settings': this.Settings,
- },
- })
- this.req = new MockRequest()
- this.res = new MockResponse()
- })
- describe('with an existing user', function () {
- beforeEach(async function () {
- this.user = {
- _id: new ObjectId(),
- splitTests: {
- 'active-test': [
- {
- variantName: 'default',
- versionNumber: 1,
- assignedAt: 'active-test-assigned-at',
- },
- ],
- 'legacy-test': 'legacy-variant',
- 'inactive-test': [{ variantName: 'trythis' }],
- 'unknown-test': [{ variantName: 'trythis' }],
- 'no-analytics-test-2': [
- {
- variantName: 'some-variant',
- versionNumber: 1,
- assignedAt: 'no-analytics-assigned-at',
- },
- ],
- },
- }
- this.SplitTestUserGetter.promises.getUser.resolves(this.user)
- this.SessionManager.isUserLoggedIn.returns(true)
- this.assignments =
- await this.SplitTestHandler.promises.getActiveAssignmentsForUser(
- this.user._id
- )
- this.explicitAssignments =
- await this.SplitTestHandler.promises.getActiveAssignmentsForUser(
- this.user._id,
- false,
- true
- )
- this.assignedToActiveTest =
- await this.SplitTestHandler.promises.hasUserBeenAssignedToVariant(
- this.req,
- this.user._id,
- 'active-test',
- 'variant-1'
- )
- this.assignedToActiveTestAnyVersion =
- await this.SplitTestHandler.promises.hasUserBeenAssignedToVariant(
- this.req,
- this.user._id,
- 'active-test',
- 'variant-1',
- true
- )
- })
- it('handles the legacy assignment format', function () {
- expect(this.assignments['legacy-test']).to.deep.equal({
- variantName: 'variant-1',
- phase: 'release',
- versionNumber: 1,
- })
- })
- it('returns the current assignment for each active test', function () {
- expect(this.assignments['active-test']).to.deep.equal({
- variantName: 'variant-1',
- phase: 'release',
- versionNumber: 2,
- })
- })
- it('returns the explicit assignment for each active test', function () {
- expect(this.explicitAssignments['active-test']).to.deep.equal({
- variantName: 'variant-1',
- phase: 'release',
- versionNumber: 2,
- assignedAt: 'active-test-assigned-at',
- })
- })
- it('returns the current assignment for tests with analytics disabled', function () {
- expect(this.assignments['no-analytics-test-1']).to.deep.equal({
- variantName: 'variant-1',
- phase: 'release',
- versionNumber: 1,
- })
- })
- it('returns the current assignment for tests with analytics disabled that had previous assignments', function () {
- expect(this.assignments['no-analytics-test-2']).to.deep.equal({
- variantName: 'variant-1',
- phase: 'release',
- versionNumber: 2,
- })
- })
- it('shows user has been assigned to previous version of variant', function () {
- expect(this.assignedToActiveTestAnyVersion).to.be.true
- })
- it('shows user has not been explicitly assigned to current version of variant', function () {
- expect(this.assignedToActiveTest).to.be.false
- })
- it('does not return assignments for unknown tests', function () {
- expect(this.assignments).not.to.have.property('unknown-test')
- })
- })
- describe('with an non-existent user', function () {
- beforeEach(async function () {
- const unknownUserId = new ObjectId()
- this.assignments =
- await this.SplitTestHandler.promises.getActiveAssignmentsForUser(
- unknownUserId
- )
- })
- it('returns empty assignments', function () {
- expect(this.assignments).to.deep.equal({})
- })
- })
- describe('with a user without assignments', function () {
- beforeEach(async function () {
- this.user = { _id: new ObjectId() }
- this.SplitTestUserGetter.promises.getUser.resolves(this.user)
- this.assignments =
- await this.SplitTestHandler.promises.getActiveAssignmentsForUser(
- this.user._id
- )
- this.explicitAssignments =
- await this.SplitTestHandler.promises.getActiveAssignmentsForUser(
- this.user._id,
- false,
- true
- )
- this.assignedToActiveTest =
- await this.SplitTestHandler.promises.hasUserBeenAssignedToVariant(
- this.req,
- this.user._id,
- 'active-test',
- 'variant-1'
- )
- })
- it('returns current assignments', function () {
- expect(this.assignments).to.deep.equal({
- 'active-test': {
- phase: 'release',
- variantName: 'variant-1',
- versionNumber: 2,
- },
- 'legacy-test': {
- phase: 'release',
- variantName: 'variant-1',
- versionNumber: 1,
- },
- 'no-analytics-test-1': {
- phase: 'release',
- variantName: 'variant-1',
- versionNumber: 1,
- },
- 'no-analytics-test-2': {
- phase: 'release',
- variantName: 'variant-1',
- versionNumber: 2,
- },
- 'not-active-test': {
- phase: 'release',
- variantName: 'variant-1',
- versionNumber: 1,
- },
- })
- })
- it('shows user not assigned to variant', function () {
- expect(this.assignedToActiveTest).to.be.false
- })
- })
- describe('with settings overrides', function () {
- beforeEach(function () {
- this.Settings.splitTestOverrides = {
- 'my-test-name': 'foo-1',
- }
- })
- it('should not use the override when in SaaS mode', async function () {
- this.AnalyticsManager.getIdsFromSession.returns({
- userId: 'abc123abc123',
- })
- this.SplitTestCache.get.resolves(
- new Map([
- [
- 'my-test-name',
- {
- name: 'my-test-name',
- versions: [
- {
- versionNumber: 0,
- active: true,
- variants: [
- {
- name: '100-percent-variant',
- rolloutPercent: 100,
- rolloutStripes: [{ start: 0, end: 100 }],
- },
- ],
- },
- ],
- },
- ],
- ])
- )
- const assignment = await this.SplitTestHandler.promises.getAssignment(
- this.req,
- this.res,
- 'my-test-name'
- )
- assert.equal('100-percent-variant', assignment.variant)
- })
- it('should use the override when not in SaaS mode', async function () {
- this.Settings.splitTestOverrides = {
- 'my-test-name': 'foo-1',
- }
- this.Settings.overleaf = undefined
- const assignment = await this.SplitTestHandler.promises.getAssignment(
- this.req,
- this.res,
- 'my-test-name'
- )
- assert.equal('foo-1', assignment.variant)
- })
- it('should use default when not in SaaS mode and no override is provided', async function () {
- this.Settings.splitTestOverrides = {}
- this.Settings.overleaf = undefined
- const assignment = await this.SplitTestHandler.promises.getAssignment(
- this.req,
- this.res,
- 'my-test-name'
- )
- 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'
- )
- })
- })
- describe('variant user limits', function () {
- beforeEach(function () {
- this.AnalyticsManager.getIdsFromSession.returns({
- userId: 'abc123abc123',
- })
- this.SplitTestUserGetter.promises.getUser.resolves({
- _id: new ObjectId('abc123abc123abc123abc123'),
- splitTests: {},
- })
- })
- it('should assign to variant when under limit', async function () {
- this.cachedSplitTests.set(
- 'active-test',
- makeSplitTest('active-test', { userLimit: 100, userCount: 50 })
- )
- const assignment = await this.SplitTestHandler.promises.getAssignment(
- this.req,
- this.res,
- 'active-test'
- )
- expect(assignment.variant).to.equal('variant-1')
- })
- it('should assign to default when limit reached', async function () {
- this.cachedSplitTests.set(
- 'active-test',
- makeSplitTest('active-test', { userLimit: 100, userCount: 100 })
- )
- const assignment = await this.SplitTestHandler.promises.getAssignment(
- this.req,
- this.res,
- 'active-test'
- )
- expect(assignment.variant).to.equal('default')
- })
- it('should not apply limits when no limit configured', async function () {
- const assignment = await this.SplitTestHandler.promises.getAssignment(
- this.req,
- this.res,
- 'active-test'
- )
- expect(assignment.variant).to.equal('variant-1')
- })
- it('should allow already assigned users even when limit reached', async function () {
- this.cachedSplitTests.set(
- 'active-test',
- makeSplitTest('active-test', { userLimit: 100, userCount: 100 })
- )
- this.SplitTestUserGetter.promises.getUser.resolves({
- _id: new ObjectId('abc123abc123abc123abc123'),
- splitTests: {
- 'active-test': [
- {
- variantName: 'variant-1',
- versionNumber: 1,
- assignedAt: new Date(),
- phase: 'release',
- },
- ],
- },
- })
- const assignment = await this.SplitTestHandler.promises.getAssignment(
- this.req,
- this.res,
- 'active-test'
- )
- expect(assignment.variant).to.equal('variant-1')
- })
- it('should assign to default if userCount is undefined', async function () {
- this.cachedSplitTests.set(
- 'active-test',
- makeSplitTest('active-test', { userLimit: 100, userCount: undefined })
- )
- const assignment = await this.SplitTestHandler.promises.getAssignment(
- this.req,
- this.res,
- 'active-test'
- )
- expect(assignment.variant).to.equal('default')
- })
- })
- })
- function makeSplitTest(
- name,
- {
- active = true,
- analyticsEnabled = active,
- phase = 'release',
- versionNumber = 1,
- userLimit = undefined,
- userCount = undefined,
- } = {}
- ) {
- const variant = {
- name: 'variant-1',
- rolloutPercent: 100,
- rolloutStripes: [{ start: 0, end: 100 }],
- }
- if (userLimit !== undefined) {
- variant.userLimit = userLimit
- }
- if (userCount !== undefined) {
- variant.userCount = userCount
- }
- return {
- name,
- versions: [
- {
- active,
- analyticsEnabled,
- phase,
- versionNumber,
- variants: [variant],
- },
- ],
- }
- }
|