From be3ca44409b8ac8491a1fe276a44eda3450efc09 Mon Sep 17 00:00:00 2001 From: Brian Giori Date: Wed, 22 May 2024 18:57:26 -0700 Subject: [PATCH 1/2] fix: convert initial variants so experiment key is tracked correctly --- .../src/experimentClient.ts | 10 +++++- .../experiment-browser/test/client.test.ts | 34 +++++++++++++++++-- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/packages/experiment-browser/src/experimentClient.ts b/packages/experiment-browser/src/experimentClient.ts index 3ce53abb..9fa130ac 100644 --- a/packages/experiment-browser/src/experimentClient.ts +++ b/packages/experiment-browser/src/experimentClient.ts @@ -23,7 +23,7 @@ import { DefaultUserProvider } from './integration/default'; import { getFlagStorage, getVariantStorage, - LoadStoreCache, + LoadStoreCache, transformVariantFromStorage } from './storage/cache'; import { LocalStorage } from './storage/local-storage'; import { FetchHttpClient, WrapperClient } from './transport/http'; @@ -114,6 +114,14 @@ export class ExperimentClient implements Client { ? euFlagsServerUrl : Defaults.flagsServerUrl), }; + // Transform initialVariants + if (this.config.initialVariants) { + for (const flagKey in this.config.initialVariants) { + this.config.initialVariants[flagKey] = transformVariantFromStorage( + this.config.initialVariants[flagKey], + ); + } + } if (this.config.userProvider) { this.userProvider = this.config.userProvider; } diff --git a/packages/experiment-browser/test/client.test.ts b/packages/experiment-browser/test/client.test.ts index 02104edf..eed94563 100644 --- a/packages/experiment-browser/test/client.test.ts +++ b/packages/experiment-browser/test/client.test.ts @@ -34,10 +34,10 @@ const testUser: ExperimentUser = { user_id: 'test_user' }; const serverKey = 'sdk-ci-test'; const serverVariant: Variant = { key: 'on', value: 'on', payload: 'payload' }; -const serverOffVariant: Variant = { value: 'off' }; +const serverOffVariant: Variant = { key: 'off', value: 'off' }; const initialKey = 'initial-key'; -const initialVariant: Variant = { value: 'initial' }; +const initialVariant: Variant = { key: 'initial', value: 'initial' }; const initialVariants: Variants = { 'sdk-ci-test': serverOffVariant, @@ -1131,3 +1131,33 @@ describe('fetch retry with different response codes', () => { }, ); }); + +test('test bootstrapping with v2 variants', async () => { + let exposureObject: Exposure; + const client = new ExperimentClient(API_KEY, { + initialVariants: { + 'test-v1': { + key: 'control', + value: 'control', + expKey: 'exp-1', + }, + 'test-v2': { + key: 'control', + value: 'control', + metadata: { + experimentKey: 'exp-2', + }, + }, + }, + source: Source.InitialVariants, + exposureTrackingProvider: { + track(exposure: Exposure) { + exposureObject = exposure; + }, + }, + }); + client.exposure('test-v1'); + expect(exposureObject.experiment_key).toEqual('exp-1'); + client.exposure('test-v2'); + expect(exposureObject.experiment_key).toEqual('exp-2'); +}); From 78de585e940aed32f1f6483843d769558fbe3366 Mon Sep 17 00:00:00 2001 From: Brian Giori Date: Wed, 22 May 2024 18:59:15 -0700 Subject: [PATCH 2/2] fix: lint --- packages/experiment-browser/src/experimentClient.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/experiment-browser/src/experimentClient.ts b/packages/experiment-browser/src/experimentClient.ts index 9fa130ac..1441b915 100644 --- a/packages/experiment-browser/src/experimentClient.ts +++ b/packages/experiment-browser/src/experimentClient.ts @@ -23,7 +23,8 @@ import { DefaultUserProvider } from './integration/default'; import { getFlagStorage, getVariantStorage, - LoadStoreCache, transformVariantFromStorage + LoadStoreCache, + transformVariantFromStorage, } from './storage/cache'; import { LocalStorage } from './storage/local-storage'; import { FetchHttpClient, WrapperClient } from './transport/http';