blob: 929060dd8e9c0c1358d10dba9b4f636ae29fc416 [file] [log] [blame]
Adrià Vilanova Martínezf19ea432024-01-23 20:20:52 +01001// Copyright 2020 The Chromium Authors
Copybara854996b2021-09-07 19:36:02 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5import {assert} from 'chai';
6import {isExperimentEnabled, SLO_EXPERIMENT} from './experiments.js';
7
8
9describe('isExperimentEnabled', () => {
10 it('throws error for unknown experiment', () => {
11 assert.throws(() =>
12 isExperimentEnabled('unknown-exp', {displayName: 'jessan@google.com'}));
13 });
14
15 it('returns false if user not in experiment', () => {
16 const ineligibleUser = {displayName: 'example@example.com'};
17 assert.isFalse(isExperimentEnabled(SLO_EXPERIMENT, ineligibleUser, {}));
18 });
19
20 it('returns false if no user provided', () => {
21 assert.isFalse(isExperimentEnabled(SLO_EXPERIMENT, undefined, {}));
22 });
23
24 it('returns true if user in experiment', () => {
25 const eligibleUser = {displayName: 'jessan@google.com'};
26 assert.isTrue(isExperimentEnabled(SLO_EXPERIMENT, eligibleUser, {}));
27 });
28
29 it('is false if user in experiment has disabled it with URL', () => {
30 const eligibleUser = {displayName: 'jessan@google.com'};
31 assert.isFalse(isExperimentEnabled(
32 SLO_EXPERIMENT, eligibleUser, {'e': '-slo'}));
33 });
34
35 it('ignores enabling experiments with URL', () => {
36 const ineligibleUser = {displayName: 'example@example.com'};
37 assert.isFalse(isExperimentEnabled(
38 SLO_EXPERIMENT, ineligibleUser, {'e': 'slo'}));
39 });
40
41 it('ignores ineligible users disabling experiment with URL', () => {
42 const ineligibleUser = {displayName: 'example@example.com'};
43 assert.isFalse(isExperimentEnabled(
44 SLO_EXPERIMENT, ineligibleUser, {'e': '-slo'}));
45 });
46
47 it('ignores invalid experiments in URL', () => {
48 const eligibleUser = {displayName: 'jessan@google.com'};
49 // Leading comma, unknown experiment str, empty experiment str in
50 // middle, disable_str with no experiment, trailing comma
51 assert.isFalse(isExperimentEnabled(
52 SLO_EXPERIMENT, eligibleUser, {'e': ',unknown,-slo,,-,'}));
53 });
54
55 it('respects last instance when experiment repeated in URL', () => {
56 const eligibleUser = {displayName: 'jessan@google.com'};
57 assert.isFalse(isExperimentEnabled(
58 SLO_EXPERIMENT, eligibleUser, {'e': 'slo,-slo'}));
59 assert.isTrue(isExperimentEnabled(
60 SLO_EXPERIMENT, eligibleUser, {'e': '-slo,slo'}));
61 });
62});