Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions packages/remote-config/src/abt/experiment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/**
* @license
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { Storage } from '../storage/storage';
import { FirebaseExperimentDescription } from '../public_types';

export class Experiment {
constructor(private readonly storage: Storage) {}

async updateActiveExperiments(
latestExperiments: FirebaseExperimentDescription[]
): Promise<void> {
const currentActiveExperiments =
(await this.storage.getActiveExperiments()) || new Set<string>();
const experimentInfoMap = this.createExperimentInfoMap(latestExperiments);
this.addActiveExperiments(currentActiveExperiments, experimentInfoMap);
this.removeInactiveExperiments(currentActiveExperiments, experimentInfoMap);
return this.storage.setActiveExperiments(new Set(experimentInfoMap.keys()));
}

private createExperimentInfoMap(
latestExperiments: FirebaseExperimentDescription[]
): Map<string, FirebaseExperimentDescription> {
const experimentInfoMap = new Map<string, FirebaseExperimentDescription>();
for (const experiment of latestExperiments) {
experimentInfoMap.set(experiment.experimentId, experiment);
}
return experimentInfoMap;
}

private addActiveExperiments(
currentActiveExperiments: Set<string>,
experimentInfoMap: Map<string, FirebaseExperimentDescription>
): void {
for (const [experimentId, experimentInfo] of experimentInfoMap.entries()) {
if (!currentActiveExperiments.has(experimentId)) {
this.addExperimentToAnalytics(experimentId, experimentInfo.variantId);
}
}
}

private removeInactiveExperiments(
currentActiveExperiments: Set<string>,
experimentInfoMap: Map<string, FirebaseExperimentDescription>
): void {
for (const experimentId of currentActiveExperiments) {
if (!experimentInfoMap.has(experimentId)) {
this.removeExperimentFromAnalytics(experimentId);
}
}
}

private addExperimentToAnalytics(
_experimentId: string,
_variantId: string
): void {
// TODO
}

private removeExperimentFromAnalytics(_experimentId: string): void {
// TODO
}
}
10 changes: 9 additions & 1 deletion packages/remote-config/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { ERROR_FACTORY, ErrorCode, hasErrorCode } from './errors';
import { RemoteConfig as RemoteConfigImpl } from './remote_config';
import { Value as ValueImpl } from './value';
import { LogLevel as FirebaseLogLevel } from '@firebase/logger';
import { Experiment } from './abt/experiment';

/**
*
Expand Down Expand Up @@ -110,12 +111,19 @@ export async function activate(remoteConfig: RemoteConfig): Promise<boolean> {
// config.
return false;
}
const experiment = new Experiment(rc._storage);
const updateActiveExperiments = lastSuccessfulFetchResponse.experiments
? experiment.updateActiveExperiments(
lastSuccessfulFetchResponse.experiments
)
: Promise.resolve();
await Promise.all([
rc._storageCache.setActiveConfig(lastSuccessfulFetchResponse.config),
rc._storage.setActiveConfigEtag(lastSuccessfulFetchResponse.eTag),
rc._storage.setActiveConfigTemplateVersion(
lastSuccessfulFetchResponse.templateVersion
)
),
updateActiveExperiments
]);
return true;
}
Expand Down
9 changes: 9 additions & 0 deletions packages/remote-config/src/storage/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export interface RealtimeBackoffMetadata {
type ProjectNamespaceKeyFieldValue =
| 'active_config'
| 'active_config_etag'
| 'active_experiments'
| 'last_fetch_status'
| 'last_successful_fetch_timestamp_millis'
| 'last_successful_fetch_response'
Expand Down Expand Up @@ -165,6 +166,14 @@ export abstract class Storage {
return this.set<string>('active_config_etag', etag);
}

getActiveExperiments(): Promise<Set<string> | undefined> {
return this.get<Set<string>>('active_experiments');
}

setActiveExperiments(experiments: Set<string>): Promise<void> {
return this.set<Set<string>>('active_experiments', experiments);
}

getThrottleMetadata(): Promise<ThrottleMetadata | undefined> {
return this.get<ThrottleMetadata>('throttle_metadata');
}
Expand Down
92 changes: 92 additions & 0 deletions packages/remote-config/test/abt/experiment.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/**
* @license
* Copyright 2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import '../setup';
import { expect } from 'chai';
import * as sinon from 'sinon';
import { Experiment } from '../../src/abt/experiment';
import { FirebaseExperimentDescription } from '../../src/public_types';
import { Storage } from '../../src/storage/storage';

describe('Experiment', () => {
const storage = {} as Storage;
const experiment = new Experiment(storage);

describe('updateActiveExperiments', () => {
beforeEach(() => {
storage.getActiveExperiments = sinon.stub();
storage.setActiveExperiments = sinon.stub();
});

it('adds mew experiments to storage', async () => {
const latestExperiments: FirebaseExperimentDescription[] = [
{
experimentId: '_exp_3',
variantId: '1',
experimentStartTime: '0',
triggerTimeoutMillis: '0',
timeToLiveMillis: '0'
},
{
experimentId: '_exp_1',
variantId: '2',
experimentStartTime: '0',
triggerTimeoutMillis: '0',
timeToLiveMillis: '0'
},
{
experimentId: '_exp_2',
variantId: '1',
experimentStartTime: '0',
triggerTimeoutMillis: '0',
timeToLiveMillis: '0'
}
];
const expectedStoredExperiments = new Set(['_exp_3', '_exp_1', '_exp_2']);
storage.getActiveExperiments = sinon
.stub()
.returns(new Set(['_exp_1', '_exp_2']));

await experiment.updateActiveExperiments(latestExperiments);

expect(storage.setActiveExperiments).to.have.been.calledWith(
expectedStoredExperiments
);
});

it('removes missing experiment in fetch response from storage', async () => {
const latestExperiments: FirebaseExperimentDescription[] = [
{
experimentId: '_exp_1',
variantId: '2',
experimentStartTime: '0',
triggerTimeoutMillis: '0',
timeToLiveMillis: '0'
}
];
const expectedStoredExperiments = new Set(['_exp_1']);
storage.getActiveExperiments = sinon
.stub()
.returns(new Set(['_exp_1', '_exp_2']));

await experiment.updateActiveExperiments(latestExperiments);

expect(storage.setActiveExperiments).to.have.been.calledWith(
expectedStoredExperiments
);
});
});
});
15 changes: 15 additions & 0 deletions packages/remote-config/test/remote_config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import * as api from '../src/api';
import { fetchAndActivate } from '../src';
import { restore } from 'sinon';
import { RealtimeHandler } from '../src/client/realtime_handler';
import { Experiment } from '../src/abt/experiment';

describe('RemoteConfig', () => {
const ACTIVE_CONFIG = {
Expand Down Expand Up @@ -401,6 +402,8 @@ describe('RemoteConfig', () => {
}
];

let sandbox: sinon.SinonSandbox;
let updateActiveExperimentsStub: sinon.SinonStub;
let getLastSuccessfulFetchResponseStub: sinon.SinonStub;
let getActiveConfigEtagStub: sinon.SinonStub;
let getActiveConfigTemplateVersionStub: sinon.SinonStub;
Expand All @@ -409,6 +412,11 @@ describe('RemoteConfig', () => {
let setActiveConfigTemplateVersionStub: sinon.SinonStub;

beforeEach(() => {
sandbox = sinon.createSandbox();
updateActiveExperimentsStub = sandbox.stub(
Experiment.prototype,
'updateActiveExperiments'
);
getLastSuccessfulFetchResponseStub = sinon.stub();
getActiveConfigEtagStub = sinon.stub();
getActiveConfigTemplateVersionStub = sinon.stub();
Expand All @@ -427,6 +435,10 @@ describe('RemoteConfig', () => {
setActiveConfigTemplateVersionStub;
});

afterEach(() => {
sandbox.restore();
});

it('does not activate if last successful fetch response is undefined', async () => {
getLastSuccessfulFetchResponseStub.returns(Promise.resolve());
getActiveConfigEtagStub.returns(Promise.resolve(ETAG));
Expand All @@ -440,6 +452,7 @@ describe('RemoteConfig', () => {
expect(storage.setActiveConfigEtag).to.not.have.been.called;
expect(storageCache.setActiveConfig).to.not.have.been.called;
expect(storage.setActiveConfigTemplateVersion).to.not.have.been.called;
expect(updateActiveExperimentsStub).to.not.have.been.called;
});

it('does not activate if fetched and active etags are the same', async () => {
Expand All @@ -458,6 +471,7 @@ describe('RemoteConfig', () => {
expect(storage.setActiveConfigEtag).to.not.have.been.called;
expect(storageCache.setActiveConfig).to.not.have.been.called;
expect(storage.setActiveConfigTemplateVersion).to.not.have.been.called;
expect(updateActiveExperimentsStub).to.not.have.been.called;
});

it('activates if fetched and active etags are different', async () => {
Expand Down Expand Up @@ -500,6 +514,7 @@ describe('RemoteConfig', () => {
expect(storage.setActiveConfigTemplateVersion).to.have.been.calledWith(
TEMPLATE_VERSION
);
expect(updateActiveExperimentsStub).to.have.been.calledWith(EXPERIMENTS);
});
});

Expand Down
Loading