SplitTestHandlerTests.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. const Path = require('path')
  2. const SandboxedModule = require('sandboxed-module')
  3. const sinon = require('sinon')
  4. const { ObjectId } = require('mongodb')
  5. const { assert, expect } = require('chai')
  6. const MockRequest = require('../helpers/MockRequest')
  7. const MockResponse = require('../helpers/MockResponse')
  8. const MODULE_PATH = Path.join(
  9. __dirname,
  10. '../../../../app/src/Features/SplitTests/SplitTestHandler'
  11. )
  12. describe('SplitTestHandler', function () {
  13. beforeEach(function () {
  14. this.splitTests = [
  15. makeSplitTest('active-test'),
  16. makeSplitTest('legacy-test'),
  17. makeSplitTest('no-analytics-test-1', { analyticsEnabled: false }),
  18. makeSplitTest('no-analytics-test-2', {
  19. analyticsEnabled: false,
  20. versionNumber: 2,
  21. }),
  22. ]
  23. this.cachedSplitTests = new Map()
  24. for (const splitTest of this.splitTests) {
  25. this.cachedSplitTests.set(splitTest.name, splitTest)
  26. }
  27. this.SplitTest = {
  28. find: sinon.stub().returns({
  29. exec: sinon.stub().resolves(this.splitTests),
  30. }),
  31. }
  32. this.SplitTestCache = {
  33. get: sinon.stub().resolves({}),
  34. }
  35. this.SplitTestCache.get.resolves(this.cachedSplitTests)
  36. this.Settings = {
  37. moduleImportSequence: [],
  38. overleaf: {},
  39. devToolbar: {
  40. enabled: false,
  41. },
  42. }
  43. this.AnalyticsManager = {
  44. getIdsFromSession: sinon.stub(),
  45. setUserPropertyForAnalyticsId: sinon.stub().resolves(),
  46. }
  47. this.LocalsHelper = {
  48. setSplitTestVariant: sinon.stub(),
  49. setSplitTestInfo: sinon.stub(),
  50. }
  51. this.SplitTestSessionHandler = {
  52. collectSessionStats: sinon.stub(),
  53. }
  54. this.SplitTestUserGetter = {
  55. promises: {
  56. getUser: sinon.stub().resolves(null),
  57. },
  58. }
  59. this.SplitTestHandler = SandboxedModule.require(MODULE_PATH, {
  60. requires: {
  61. '../User/UserGetter': this.UserGetter,
  62. './SplitTestCache': this.SplitTestCache,
  63. '../../models/SplitTest': { SplitTest: this.SplitTest },
  64. '../User/UserUpdater': {},
  65. '../Analytics/AnalyticsManager': this.AnalyticsManager,
  66. './LocalsHelper': this.LocalsHelper,
  67. './SplitTestSessionHandler': this.SplitTestSessionHandler,
  68. './SplitTestUserGetter': this.SplitTestUserGetter,
  69. '@overleaf/settings': this.Settings,
  70. },
  71. })
  72. this.req = new MockRequest()
  73. this.res = new MockResponse()
  74. })
  75. describe('with an existing user', function () {
  76. beforeEach(async function () {
  77. this.user = {
  78. _id: new ObjectId(),
  79. splitTests: {
  80. 'active-test': [
  81. {
  82. variantName: 'default',
  83. versionNumber: 1,
  84. assignedAt: 'active-test-assigned-at',
  85. },
  86. ],
  87. 'legacy-test': 'legacy-variant',
  88. 'inactive-test': [{ variantName: 'trythis' }],
  89. 'unknown-test': [{ variantName: 'trythis' }],
  90. 'no-analytics-test-2': [
  91. {
  92. variantName: 'some-variant',
  93. versionNumber: 1,
  94. assignedAt: 'no-analytics-assigned-at',
  95. },
  96. ],
  97. },
  98. }
  99. this.SplitTestUserGetter.promises.getUser.resolves(this.user)
  100. this.assignments =
  101. await this.SplitTestHandler.promises.getActiveAssignmentsForUser(
  102. this.user._id
  103. )
  104. })
  105. it('handles the legacy assignment format', function () {
  106. expect(this.assignments['legacy-test']).to.deep.equal({
  107. variantName: 'variant-1',
  108. phase: 'release',
  109. versionNumber: 1,
  110. })
  111. })
  112. it('returns the current assignment for each active test', function () {
  113. expect(this.assignments['active-test']).to.deep.equal({
  114. variantName: 'variant-1',
  115. phase: 'release',
  116. versionNumber: 1,
  117. assignedAt: 'active-test-assigned-at',
  118. })
  119. })
  120. it('returns the current assignment for tests with analytics disabled', function () {
  121. expect(this.assignments['no-analytics-test-1']).to.deep.equal({
  122. variantName: 'variant-1',
  123. phase: 'release',
  124. versionNumber: 1,
  125. })
  126. })
  127. it('returns the current assignment for tests with analytics disabled that had previous assignments', function () {
  128. expect(this.assignments['no-analytics-test-2']).to.deep.equal({
  129. variantName: 'variant-1',
  130. phase: 'release',
  131. versionNumber: 2,
  132. })
  133. })
  134. it('does not return assignments for unknown tests', function () {
  135. expect(this.assignments).not.to.have.property('unknown-test')
  136. })
  137. })
  138. describe('with an non-existent user', function () {
  139. beforeEach(async function () {
  140. const unknownUserId = new ObjectId()
  141. this.assignments =
  142. await this.SplitTestHandler.promises.getActiveAssignmentsForUser(
  143. unknownUserId
  144. )
  145. })
  146. it('returns empty assignments', function () {
  147. expect(this.assignments).to.deep.equal({})
  148. })
  149. })
  150. describe('with a user without assignments', function () {
  151. beforeEach(async function () {
  152. this.user = { _id: new ObjectId() }
  153. this.SplitTestUserGetter.promises.getUser.resolves(this.user)
  154. this.assignments =
  155. await this.SplitTestHandler.promises.getActiveAssignmentsForUser(
  156. this.user._id
  157. )
  158. })
  159. it('returns current assignments', function () {
  160. expect(this.assignments).to.deep.equal({
  161. 'active-test': {
  162. phase: 'release',
  163. variantName: 'variant-1',
  164. versionNumber: 1,
  165. },
  166. 'legacy-test': {
  167. phase: 'release',
  168. variantName: 'variant-1',
  169. versionNumber: 1,
  170. },
  171. 'no-analytics-test-1': {
  172. phase: 'release',
  173. variantName: 'variant-1',
  174. versionNumber: 1,
  175. },
  176. 'no-analytics-test-2': {
  177. phase: 'release',
  178. variantName: 'variant-1',
  179. versionNumber: 2,
  180. },
  181. })
  182. })
  183. })
  184. describe('with settings overrides', function () {
  185. beforeEach(function () {
  186. this.Settings.splitTestOverrides = {
  187. 'my-test-name': 'foo-1',
  188. }
  189. })
  190. it('should not use the override when in SaaS mode', async function () {
  191. this.AnalyticsManager.getIdsFromSession.returns({
  192. userId: 'abc123abc123',
  193. })
  194. this.SplitTestCache.get.resolves(
  195. new Map([
  196. [
  197. 'my-test-name',
  198. {
  199. name: 'my-test-name',
  200. versions: [
  201. {
  202. versionNumber: 0,
  203. active: true,
  204. variants: [
  205. {
  206. name: '100-percent-variant',
  207. rolloutPercent: 100,
  208. rolloutStripes: [{ start: 0, end: 100 }],
  209. },
  210. ],
  211. },
  212. ],
  213. },
  214. ],
  215. ])
  216. )
  217. const assignment = await this.SplitTestHandler.promises.getAssignment(
  218. this.req,
  219. this.res,
  220. 'my-test-name'
  221. )
  222. assert.equal('100-percent-variant', assignment.variant)
  223. })
  224. it('should use the override when not in SaaS mode', async function () {
  225. this.Settings.splitTestOverrides = {
  226. 'my-test-name': 'foo-1',
  227. }
  228. this.Settings.overleaf = undefined
  229. const assignment = await this.SplitTestHandler.promises.getAssignment(
  230. this.req,
  231. this.res,
  232. 'my-test-name'
  233. )
  234. assert.equal('foo-1', assignment.variant)
  235. })
  236. it('should use default when not in SaaS mode and no override is provided', async function () {
  237. this.Settings.splitTestOverrides = {}
  238. this.Settings.overleaf = undefined
  239. const assignment = await this.SplitTestHandler.promises.getAssignment(
  240. this.req,
  241. this.res,
  242. 'my-test-name'
  243. )
  244. assert.equal('default', assignment.variant)
  245. })
  246. })
  247. describe('save assignments to res.locals', function () {
  248. beforeEach(function () {
  249. this.AnalyticsManager.getIdsFromSession.returns({
  250. userId: 'abc123abc123',
  251. })
  252. })
  253. it('when in SaaS mode it should set the variant', async function () {
  254. await this.SplitTestHandler.promises.getAssignment(
  255. this.req,
  256. this.res,
  257. 'active-test'
  258. )
  259. expect(this.LocalsHelper.setSplitTestVariant).to.have.been.calledWith(
  260. this.res.locals,
  261. 'active-test',
  262. 'variant-1'
  263. )
  264. })
  265. it('when not in SaaS mode it should set the default variant', async function () {
  266. this.Settings.overleaf = undefined
  267. await this.SplitTestHandler.promises.getAssignment(
  268. this.req,
  269. this.res,
  270. 'active-test'
  271. )
  272. expect(this.LocalsHelper.setSplitTestVariant).to.have.been.calledWith(
  273. this.res.locals,
  274. 'active-test',
  275. 'default'
  276. )
  277. })
  278. })
  279. })
  280. function makeSplitTest(
  281. name,
  282. {
  283. active = true,
  284. analyticsEnabled = active,
  285. phase = 'release',
  286. versionNumber = 1,
  287. } = {}
  288. ) {
  289. return {
  290. name,
  291. versions: [
  292. {
  293. active,
  294. analyticsEnabled,
  295. phase,
  296. versionNumber,
  297. variants: [
  298. {
  299. name: 'variant-1',
  300. rolloutPercent: 100,
  301. rolloutStripes: [{ start: 0, end: 100 }],
  302. },
  303. ],
  304. },
  305. ],
  306. }
  307. }