Phuedx has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/230099

Change subject: Tweak the layout of the experiments code
......................................................................

Tweak the layout of the experiments code

In order to ease extracting the experiments code - either upstreaming
it to mediawiki/core or creating a micro-library - the core function,
bucket, needs to be made agnostic of MobileFrontend.

* Create the getBucketInternal function from the bucket function and
  those parts of the getBucket function that aren't MobileFrontend-
  specific
* Make getBucket wrap the getBucketInternal function, passing in the
  MobileFrontend experiments configuration, wgMFExperiments, and the
  user's session ID

Bug: T107592
Change-Id: Id23edeffb3cd025bf0db7f80e4133e5334e704f7
---
M resources/mobile.experiments/experiments.js
M tests/qunit/mobile.experiments/test_experiments.js
2 files changed, 42 insertions(+), 30 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend 
refs/changes/99/230099/1

diff --git a/resources/mobile.experiments/experiments.js 
b/resources/mobile.experiments/experiments.js
index a5d8cb8..f2f82aa 100644
--- a/resources/mobile.experiments/experiments.js
+++ b/resources/mobile.experiments/experiments.js
@@ -32,32 +32,57 @@
        }
 
        /**
-        * Assigns a user to a bucket.
+        * Gets the bucket for the experiment given the token.
         *
         * The name of the experiment and the user's token are hashed. The hash 
is converted to a number
         * which is then used to assign the user to a bucket.
         *
-        * Based on the `mw.user.bucket` function.
+        * Consider the following experiment configuration:
         *
+        * <code>
+        * {
+        *   enabled: true,
+        *   buckets: {
+        *     control: 0.5
+        *     A: 0.25,
+        *     B: 0.25
+        *   }
+        * }
+        * </code>
+        *
+        * The experiment has three buckets: control, A, and B. The user has a 
50% chance of being
+        * assigned to the control bucket, and a 25% chance of being assigned 
to either the A or B
+        * buckets. If the experiment were disabled, then the user would always 
be assigned to the
+        * control bucket.
+        *
+        * This function is based on the deprecated `mw.user.bucket` function.
+        *
+        * @param {Object} experiments A map of experiment name to experiment 
definition
         * @param {String} experiment
-        * @param {Object} buckets A map of bucket name to weight, e.g.
-        *  <code>
-        *  {
-        *      "control": 0.5,
-        *      "A": 0.25,
-        *      "B": 0.25
-        *  }
-        *  </code>
-        * @ignore
         * @param {String} token
-        * @return {String}
+        * @throws Error If the experiment hasn't been defined
+        * @returns {String}
         */
-       function bucket( experiment, buckets, token ) {
-               var key,
+       function getBucketInternal( experiments, experiment, token ) {
+               var options,
+                       buckets,
+                       key,
                        range = 0,
                        hash,
                        max,
                        acc = 0;
+
+               if ( !experiments.hasOwnProperty( experiment ) ) {
+                       throw new Error( 'The experiment "' + experiment + '" 
hasn\'t been defined.' );
+               }
+
+               options = experiments[experiment];
+
+               if ( !options.enabled ) {
+                       return CONTROL_BUCKET;
+               }
+
+               buckets = options.buckets;
 
                for ( key in buckets ) {
                        range += buckets[key];
@@ -97,26 +122,14 @@
                getBucket: function ( experiment ) {
                        var experiments = mw.config.get( 'wgMFExperiments' ) || 
{},
                                options,
-                               token;
-
-                       if ( !experiments.hasOwnProperty( experiment ) ) {
-                               throw new Error( 'The experiment "' + 
experiment + '" hasn\'t been defined.' );
-                       }
-
-                       options = experiments[experiment];
-
-                       if ( !options.enabled ) {
-                               return CONTROL_BUCKET;
-                       }
-
-                       token = user.getSessionId();
+                               token = user.getSessionId();
 
                        // The browser doesn't support local storage? See 
`browser.supportsLocalStorage`.
                        if ( token === '' ) {
                                return CONTROL_BUCKET;
                        }
 
-                       return bucket( experiment, options.buckets, token );
+                       return getBucketInternal( experiments, experiment, 
token )
                }
        } );
 
diff --git a/tests/qunit/mobile.experiments/test_experiments.js 
b/tests/qunit/mobile.experiments/test_experiments.js
index 72ba0ad..7025e3d 100644
--- a/tests/qunit/mobile.experiments/test_experiments.js
+++ b/tests/qunit/mobile.experiments/test_experiments.js
@@ -35,11 +35,10 @@
                } );
        } );
 
-       QUnit.test( 'it should always return "control" if the experiment has 
been defined as disabled', 2, function ( assert ) {
+       QUnit.test( 'it should always return "control" if the experiment has 
been defined as disabled', 1, function ( assert ) {
                var bucket = this.experiments.getBucket( 'bar' );
 
                assert.strictEqual( 'control', bucket );
-               assert.strictEqual( false, user.getSessionId.called );
        } );
 
        QUnit.test( 'it should always assign the user to the same bucket given 
the same token', 1, function ( assert ) {

-- 
To view, visit https://gerrit.wikimedia.org/r/230099
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id23edeffb3cd025bf0db7f80e4133e5334e704f7
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: Phuedx <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to