Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: convert initial variants so experiment key is tracked correctly #108

Merged
merged 2 commits into from
May 23, 2024
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
9 changes: 9 additions & 0 deletions packages/experiment-browser/src/experimentClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
getFlagStorage,
getVariantStorage,
LoadStoreCache,
transformVariantFromStorage,
} from './storage/cache';
import { LocalStorage } from './storage/local-storage';
import { FetchHttpClient, WrapperClient } from './transport/http';
Expand Down Expand Up @@ -114,6 +115,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;
}
Expand Down
34 changes: 32 additions & 2 deletions packages/experiment-browser/test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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');
});
Loading