Skip to content

Commit

Permalink
Update paths to test files
Browse files Browse the repository at this point in the history
  • Loading branch information
sameerank committed Dec 16, 2024
1 parent 409019a commit c625655
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 33 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"webpack-cli": "^4.10.0"
},
"dependencies": {
"@eppo/js-client-sdk-common": "^4.6.3"
"@eppo/js-client-sdk-common": "^4.6.4"
},
"packageManager": "yarn@1.22.22+sha512.a6b2f7906b721bba3d67d4aff083df04dad64c399707841b7acf00f6b133b7ac24255f2652fa22ae3534329dc6180534e98d17432037ff6fd140556e2bb3137e"
}
13 changes: 7 additions & 6 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ import {
Flag,
HybridConfigurationStore,
IAsyncStore,
IPrecomputedConfigurationResponse,
VariationType,
} from '@eppo/js-client-sdk-common';
import * as td from 'testdouble';

import {
getTestAssignments,
IAssignmentTestCase,
MOCK_PRECOMPUTED_RESPONSE_FILE,
MOCK_PRECOMPUTED_WIRE_FILE,
MOCK_UFC_RESPONSE_FILE,
OBFUSCATED_MOCK_UFC_RESPONSE_FILE,
readAssignmentTestData,
Expand Down Expand Up @@ -1069,10 +1070,10 @@ describe('EppoPrecomputedJSClient E2E test', () => {

beforeAll(async () => {
global.fetch = jest.fn(() => {
const precomputedConfigurationWire = readMockPrecomputedResponse(
MOCK_PRECOMPUTED_RESPONSE_FILE,
);
const precomputedResponse = JSON.parse(precomputedConfigurationWire).precomputed.response;
const precomputedConfigurationWire = readMockPrecomputedResponse(MOCK_PRECOMPUTED_WIRE_FILE);
const precomputedResponse: IPrecomputedConfigurationResponse = JSON.parse(
precomputedConfigurationWire,
).precomputed.response;
return Promise.resolve({
ok: true,
status: 200,
Expand Down Expand Up @@ -1144,7 +1145,7 @@ describe('offlinePrecomputedInit', () => {
let precomputedConfigurationWire: string;

beforeAll(() => {
precomputedConfigurationWire = readMockPrecomputedResponse(MOCK_PRECOMPUTED_RESPONSE_FILE);
precomputedConfigurationWire = readMockPrecomputedResponse(MOCK_PRECOMPUTED_WIRE_FILE);
});

beforeEach(() => {
Expand Down
44 changes: 22 additions & 22 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ import {
ObfuscatedFlag,
BoundedEventQueue,
validation,
PrecomputedFlag,
Event,
IConfigurationWire,
IPrecomputedConfigurationResponse,
convertContextAttributesToSubjectAttributes,
Attributes,
} from '@eppo/js-client-sdk-common';
import { Environment, FormatEnum } from '@eppo/js-client-sdk-common/dist/interfaces';

import { assignmentCacheFactory } from './cache/assignment-cache-factory';
import HybridAssignmentCache from './cache/hybrid-assignment-cache';
Expand Down Expand Up @@ -467,6 +469,8 @@ export async function init(config: IClientConfig): Promise<EppoClient> {
// both failed, make the "fatal" error the fetch one
initializationError = initFromFetchError;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
} catch (error: any) {
initializationError = error;
}
Expand Down Expand Up @@ -621,23 +625,6 @@ export interface IPrecomputedClientConfigSync {
throwOnFailedInitialization?: boolean;
}

// TODO: remove when this interface is exported from the common library
export interface IConfigurationWire {
version: number;
precomputed: {
subjectKey: string;
subjectAttributes: Record<string, AttributeType>;
fetchedAt: string;
response: {
createdAt: string;
format: FormatEnum;
obfuscated: boolean;
environment: Environment;
flags: Record<string, PrecomputedFlag>;
};
};
}

/**
* Initializes the Eppo precomputed client with configuration parameters.
*
Expand All @@ -656,8 +643,16 @@ export function offlinePrecomputedInit(
const throwOnFailedInitialization = config.throwOnFailedInitialization ?? true;

const configurationWire: IConfigurationWire = JSON.parse(config.precomputedConfigurationWire);
const { subjectKey, subjectAttributes, response } = configurationWire.precomputed;
const parsedResponse = response; // TODO: use a JSON.parse when the obfuscated version is usable
if (!configurationWire.precomputed) {
applicationLogger.error('Invalid precomputed configuration wire');
return EppoPrecomputedJSClient.instance;
}
const {
subjectKey,
subjectAttributes: contextAttributes,
response,
} = configurationWire.precomputed;
const parsedResponse: IPrecomputedConfigurationResponse = JSON.parse(response); // TODO: use a JSON.parse when the obfuscated version is usable

try {
const memoryOnlyPrecomputedStore = precomputedFlagsStorageFactory();
Expand All @@ -667,9 +662,14 @@ export function offlinePrecomputedInit(
applicationLogger.warn('Error setting precomputed assignments for memory-only store', err),
);

EppoPrecomputedJSClient.instance.setSubjectAndPrecomputedFlagStore(
const subjectAttributes: Attributes = convertContextAttributesToSubjectAttributes(
contextAttributes ?? { numericAttributes: {}, categoricalAttributes: {} },
);

EppoPrecomputedJSClient.instance.setSubjectSaltAndPrecomputedFlagStore(
subjectKey,
subjectAttributes,
parsedResponse.salt,
memoryOnlyPrecomputedStore,
);

Expand Down
13 changes: 9 additions & 4 deletions test/testHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ const MOCK_UFC_FILENAME = 'flags-v1';
export const MOCK_UFC_RESPONSE_FILE = `${MOCK_UFC_FILENAME}.json`;
export const OBFUSCATED_MOCK_UFC_RESPONSE_FILE = `${MOCK_UFC_FILENAME}-obfuscated.json`;

export const TEST_PRECOMPUTED_DATA_DIR = './test/data/configuration-wire/';
// export const TEST_PRECOMPUTED_DATA_DIR = './test/data/configuration-wire/';
// const MOCK_PRECOMPUTED_FILENAME = 'precomputed-v1';
// export const MOCK_PRECOMPUTED_RESPONSE_FILE = `${MOCK_PRECOMPUTED_FILENAME}.json`;
// export const OBFUSCATED_MOCK_PRECOMPUTED_RESPONSE_FILE = `${MOCK_PRECOMPUTED_FILENAME}-obfuscated.json`;

const TEST_CONFIGURATION_WIRE_DATA_DIR = './test/data/configuration-wire/';
const MOCK_PRECOMPUTED_FILENAME = 'precomputed-v1';
export const MOCK_PRECOMPUTED_RESPONSE_FILE = `${MOCK_PRECOMPUTED_FILENAME}.json`;
export const OBFUSCATED_MOCK_PRECOMPUTED_RESPONSE_FILE = `${MOCK_PRECOMPUTED_FILENAME}-obfuscated.json`;
export const MOCK_PRECOMPUTED_WIRE_FILE = `${MOCK_PRECOMPUTED_FILENAME}.json`;
export const MOCK_DEOBFUSCATED_PRECOMPUTED_RESPONSE_FILE = `${MOCK_PRECOMPUTED_WIRE_FILE}-deobfuscated.json`;

export enum ValueTestType {
BoolType = 'boolean',
Expand Down Expand Up @@ -67,7 +72,7 @@ export function readMockUfcResponse(filename: string): {
}

export function readMockPrecomputedResponse(filename: string): string {
return fs.readFileSync(TEST_PRECOMPUTED_DATA_DIR + filename, 'utf-8');
return fs.readFileSync(TEST_CONFIGURATION_WIRE_DATA_DIR + filename, 'utf-8');
}

export function readAssignmentTestData(): IAssignmentTestCase[] {
Expand Down

0 comments on commit c625655

Please sign in to comment.