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
15 changes: 11 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,19 @@ help: Makefile

## test-data
testDataDir := test/data/
tempDir := ${testDataDir}temp/
gitDataDir := ${tempDir}sdk-test-data/
branchName := main
githubRepoLink := https://github.com/Eppo-exp/sdk-test-data.git
repoName := sdk-test-data
.PHONY: test-data
test-data:
test-data:
rm -rf $(testDataDir)
mkdir -p $(testDataDir)
gsutil cp gs://sdk-test-data/rac-experiments-v3.json $(testDataDir)
gsutil cp -r gs://sdk-test-data/assignment-v2 $(testDataDir)
mkdir -p $(tempDir)
git clone -b ${branchName} --depth 1 --single-branch ${githubRepoLink} ${gitDataDir}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥

cp ${gitDataDir}rac-experiments-v3.json ${testDataDir}
cp -r ${gitDataDir}assignment-v2 ${testDataDir}
rm -rf ${tempDir}

## prepare
.PHONY: prepare
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@eppo/js-client-sdk-common",
"version": "1.3.1",
"version": "1.4.0",
"description": "Eppo SDK for client-side JavaScript applications (base for both web and react native)",
"main": "dist/index.js",
"files": [
Expand Down
6 changes: 4 additions & 2 deletions src/assignment-hooks.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { EppoValue } from './eppo_value';

/**
* Implement this interface to override an assignment or receive a callback post assignment
* @public
Expand All @@ -12,7 +14,7 @@ export interface IAssignmentHooks {
* then the subject will be assigned with the default assignment logic.
* @public
*/
onPreAssignment(experimentKey: string, subject: string): string | null;
onPreAssignment(experimentKey: string, subject: string): EppoValue | null;

/**
* Invoked after a subject is assigned. Useful for any post assignment logic needed which is specific
Expand All @@ -22,5 +24,5 @@ export interface IAssignmentHooks {
* @param variation the assigned variation
* @public
*/
onPostAssignment(experimentKey: string, subject: string, variation: string): void;
onPostAssignment(experimentKey: string, subject: string, variation: EppoValue | null): void;
}
116 changes: 99 additions & 17 deletions src/client/eppo-client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import mock from 'xhr-mock';

import {
IAssignmentTestCase,
ValueTestType,
readAssignmentTestData,
readMockRacResponse,
} from '../../test/testHelpers';
Expand All @@ -15,6 +16,7 @@ import { IAssignmentLogger } from '../assignment-logger';
import { IConfigurationStore } from '../configuration-store';
import { MAX_EVENT_QUEUE_SIZE } from '../constants';
import { OperatorType } from '../dto/rule-dto';
import { EppoValue } from '../eppo_value';
import ExperimentConfigurationRequestor from '../experiment-configuration-requestor';
import HttpClient from '../http-client';

Expand Down Expand Up @@ -85,6 +87,7 @@ describe('EppoClient E2E test', () => {
enabled: true,
subjectShards: 100,
overrides: {},
typedOverrides: {},
rules: [
{
allocationKey: 'allocation1',
Expand All @@ -98,6 +101,7 @@ describe('EppoClient E2E test', () => {
{
name: 'control',
value: 'control',
typedValue: 'control',
shardRange: {
start: 0,
end: 34,
Expand All @@ -106,6 +110,7 @@ describe('EppoClient E2E test', () => {
{
name: 'variant-1',
value: 'variant-1',
typedValue: 'variant-1',
shardRange: {
start: 34,
end: 67,
Expand All @@ -114,6 +119,7 @@ describe('EppoClient E2E test', () => {
{
name: 'variant-2',
value: 'variant-2',
typedValue: 'variant-2',
shardRange: {
start: 67,
end: 100,
Expand Down Expand Up @@ -172,17 +178,34 @@ describe('EppoClient E2E test', () => {
'test variation assignment splits',
async ({
experiment,
valueType = ValueTestType.StringType,
subjects,
subjectsWithAttributes,
expectedAssignments,
}: IAssignmentTestCase) => {
`---- Test Case for ${experiment} Experiment ----`;
const assignments = subjectsWithAttributes
? getAssignmentsWithSubjectAttributes(subjectsWithAttributes, experiment)
: getAssignments(subjects, experiment);

// temporarily cast to string under dynamic configuration support is added
expect(assignments).toEqual(expectedAssignments.map((e) => (e ? e.toString() : null)));
const assignments = subjectsWithAttributes
? getAssignmentsWithSubjectAttributes(subjectsWithAttributes, experiment, valueType)
: getAssignments(subjects, experiment, valueType);

switch (valueType) {
case ValueTestType.BoolType: {
const boolAssignments = assignments.map((a) => a?.boolValue ?? null);
expect(boolAssignments).toEqual(expectedAssignments);
break;
}
case ValueTestType.NumericType: {
const numericAssignments = assignments.map((a) => a?.numericValue ?? null);
expect(numericAssignments).toEqual(expectedAssignments);
break;
}
case ValueTestType.StringType: {
const stringAssignments = assignments.map((a) => a?.stringValue ?? null);
expect(stringAssignments).toEqual(expectedAssignments);
break;
}
}
},
);
});
Expand All @@ -198,7 +221,7 @@ describe('EppoClient E2E test', () => {
experimentName,
JSON.stringify({
...mockExperimentConfig,
overrides: {
typedOverrides: {
'1b50f33aef8f681a13f623963da967ed': 'control',
},
}),
Expand All @@ -212,7 +235,7 @@ describe('EppoClient E2E test', () => {
const entry = {
...mockExperimentConfig,
enabled: false,
overrides: {
typedOverrides: {
'1b50f33aef8f681a13f623963da967ed': 'control',
},
};
Expand Down Expand Up @@ -281,9 +304,29 @@ describe('EppoClient E2E test', () => {
expect(assignment).toEqual('control');
});

function getAssignments(subjects: string[], experiment: string): string[] {
function getAssignments(
subjects: string[],
experiment: string,
valueTestType: ValueTestType = ValueTestType.StringType,
): (EppoValue | null)[] {
return subjects.map((subjectKey) => {
return globalClient.getAssignment(subjectKey, experiment);
switch (valueTestType) {
case ValueTestType.BoolType: {
const ba = globalClient.getBoolAssignment(subjectKey, experiment);
if (ba === null) return null;
return EppoValue.Bool(ba);
Comment on lines +316 to +317
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor but with short-circuiting and I think we could simplify this to return ba && EppoValue.Bool(ba).

> v = null
null
> v && Bool(v)
null
> v = true
true
> v && Boolean(v)
true

}
case ValueTestType.NumericType: {
const na = globalClient.getNumericAssignment(subjectKey, experiment);
if (na === null) return null;
return EppoValue.Numeric(na);
}
case ValueTestType.StringType: {
const sa = globalClient.getStringAssignment(subjectKey, experiment);
if (sa === null) return null;
return EppoValue.String(sa);
}
}
});
}

Expand All @@ -294,9 +337,38 @@ describe('EppoClient E2E test', () => {
subjectAttributes: Record<string, any>;
}[],
experiment: string,
): string[] {
valueTestType: ValueTestType = ValueTestType.StringType,
): (EppoValue | null)[] {
return subjectsWithAttributes.map((subject) => {
return globalClient.getAssignment(subject.subjectKey, experiment, subject.subjectAttributes);
switch (valueTestType) {
case ValueTestType.BoolType: {
const ba = globalClient.getBoolAssignment(
subject.subjectKey,
experiment,
subject.subjectAttributes,
);
if (ba === null) return null;
return EppoValue.Bool(ba);
}
case ValueTestType.NumericType: {
const na = globalClient.getNumericAssignment(
subject.subjectKey,
experiment,
subject.subjectAttributes,
);
if (na === null) return null;
return EppoValue.Numeric(na);
}
case ValueTestType.StringType: {
const sa = globalClient.getStringAssignment(
subject.subjectKey,
experiment,
subject.subjectAttributes,
);
if (sa === null) return null;
return EppoValue.String(sa);
}
}
});
}

Expand Down Expand Up @@ -324,12 +396,16 @@ describe('EppoClient E2E test', () => {
{},
{
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onPreAssignment(experimentKey: string, subject: string): string {
return 'my-overridden-variation';
onPreAssignment(experimentKey: string, subject: string): EppoValue | null {
return EppoValue.String('my-overridden-variation');
},

// eslint-disable-next-line @typescript-eslint/no-unused-vars
onPostAssignment(experimentKey: string, subject: string, variation: string): void {
onPostAssignment(
experimentKey: string,
subject: string,
variation: EppoValue | null,
): void {
// no-op
},
},
Expand All @@ -345,12 +421,16 @@ describe('EppoClient E2E test', () => {
{},
{
// eslint-disable-next-line @typescript-eslint/no-unused-vars
onPreAssignment(experimentKey: string, subject: string): string | null {
onPreAssignment(experimentKey: string, subject: string): EppoValue | null {
return null;
},

// eslint-disable-next-line @typescript-eslint/no-unused-vars
onPostAssignment(experimentKey: string, subject: string, variation: string): void {
onPostAssignment(
experimentKey: string,
subject: string,
variation: EppoValue | null,
): void {
// no-op
},
},
Expand All @@ -369,7 +449,9 @@ describe('EppoClient E2E test', () => {
expect(td.explain(mockHooks.onPostAssignment).callCount).toEqual(1);
expect(td.explain(mockHooks.onPostAssignment).calls[0].args[0]).toEqual(experimentName);
expect(td.explain(mockHooks.onPostAssignment).calls[0].args[1]).toEqual(subject);
expect(td.explain(mockHooks.onPostAssignment).calls[0].args[2]).toEqual(variation);
expect(td.explain(mockHooks.onPostAssignment).calls[0].args[2]).toEqual(
EppoValue.String(variation ?? ''),
);
});
});
});
Expand Down
Loading