|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +import '../setup'; |
| 18 | +import { expect } from 'chai'; |
| 19 | +import * as sinon from 'sinon'; |
| 20 | +import { Experiment } from '../../src/abt/experiment'; |
| 21 | +import { FirebaseExperimentDescription } from '../../src/public_types'; |
| 22 | +import { Storage } from '../../src/storage/storage'; |
| 23 | + |
| 24 | +describe('Experiment', () => { |
| 25 | + const storage = {} as Storage; |
| 26 | + const experiment = new Experiment(storage); |
| 27 | + |
| 28 | + describe('updateActiveExperiments', () => { |
| 29 | + beforeEach(() => { |
| 30 | + storage.getActiveExperiments = sinon.stub(); |
| 31 | + storage.setActiveExperiments = sinon.stub(); |
| 32 | + }); |
| 33 | + |
| 34 | + it('adds mew experiments to storage', async () => { |
| 35 | + const latestExperiments: FirebaseExperimentDescription[] = [ |
| 36 | + { |
| 37 | + experimentId: '_exp_3', |
| 38 | + variantId: '1', |
| 39 | + experimentStartTime: '0', |
| 40 | + triggerTimeoutMillis: '0', |
| 41 | + timeToLiveMillis: '0' |
| 42 | + }, |
| 43 | + { |
| 44 | + experimentId: '_exp_1', |
| 45 | + variantId: '2', |
| 46 | + experimentStartTime: '0', |
| 47 | + triggerTimeoutMillis: '0', |
| 48 | + timeToLiveMillis: '0' |
| 49 | + }, |
| 50 | + { |
| 51 | + experimentId: '_exp_2', |
| 52 | + variantId: '1', |
| 53 | + experimentStartTime: '0', |
| 54 | + triggerTimeoutMillis: '0', |
| 55 | + timeToLiveMillis: '0' |
| 56 | + }, |
| 57 | + ]; |
| 58 | + const expectedStoredExperiments = new Set(['_exp_3', '_exp_1', '_exp_2']); |
| 59 | + storage.getActiveExperiments = sinon.stub().returns(new Set(['_exp_1', '_exp_2'])); |
| 60 | + |
| 61 | + |
| 62 | + await experiment.updateActiveExperiments(latestExperiments); |
| 63 | + |
| 64 | + expect(storage.setActiveExperiments).to.have.been.calledWith(expectedStoredExperiments); |
| 65 | + }); |
| 66 | + |
| 67 | + it('removes missing experiment in fetch response from storage', async () => { |
| 68 | + const latestExperiments: FirebaseExperimentDescription[] = [ |
| 69 | + { |
| 70 | + experimentId: '_exp_1', |
| 71 | + variantId: '2', |
| 72 | + experimentStartTime: '0', |
| 73 | + triggerTimeoutMillis: '0', |
| 74 | + timeToLiveMillis: '0' |
| 75 | + } |
| 76 | + ]; |
| 77 | + const expectedStoredExperiments = new Set(['_exp_1']); |
| 78 | + storage.getActiveExperiments = sinon.stub().returns(new Set(['_exp_1', '_exp_2'])); |
| 79 | + |
| 80 | + |
| 81 | + await experiment.updateActiveExperiments(latestExperiments); |
| 82 | + |
| 83 | + expect(storage.setActiveExperiments).to.have.been.calledWith(expectedStoredExperiments); |
| 84 | + }); |
| 85 | + }); |
| 86 | +}); |
0 commit comments