Skip to content

Junaed/fssdk 1119 test js to ts #1002

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

Merged
merged 10 commits into from
Feb 19, 2025
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
43 changes: 43 additions & 0 deletions lib/core/bucketer/bucket_value_generator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright 2025, Optimizely
*
* 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
*
* https://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 { expect, describe, it } from 'vitest';
import { sprintf } from '../../utils/fns';
import { generateBucketValue } from './bucket_value_generator';
import { OptimizelyError } from '../../error/optimizly_error';
import { INVALID_BUCKETING_ID } from 'error_message';

describe('generateBucketValue', () => {
it('should return a bucket value for different inputs', () => {
const experimentId = 1886780721;
const bucketingKey1 = sprintf('%s%s', 'ppid1', experimentId);
const bucketingKey2 = sprintf('%s%s', 'ppid2', experimentId);
const bucketingKey3 = sprintf('%s%s', 'ppid2', 1886780722);
const bucketingKey4 = sprintf('%s%s', 'ppid3', experimentId);

expect(generateBucketValue(bucketingKey1)).toBe(5254);
expect(generateBucketValue(bucketingKey2)).toBe(4299);
expect(generateBucketValue(bucketingKey3)).toBe(2434);
expect(generateBucketValue(bucketingKey4)).toBe(5439);
});

it('should return an error if it cannot generate the hash value', () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
expect(() => generateBucketValue(null)).toThrowError(
new OptimizelyError(INVALID_BUCKETING_ID)
);
});
});
40 changes: 40 additions & 0 deletions lib/core/bucketer/bucket_value_generator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright 2025, Optimizely
*
* 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
*
* https://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 murmurhash from 'murmurhash';
import { INVALID_BUCKETING_ID } from 'error_message';
import { OptimizelyError } from '../../error/optimizly_error';

const HASH_SEED = 1;
const MAX_HASH_VALUE = Math.pow(2, 32);
const MAX_TRAFFIC_VALUE = 10000;

/**
* Helper function to generate bucket value in half-closed interval [0, MAX_TRAFFIC_VALUE)
* @param {string} bucketingKey String value for bucketing
* @return {number} The generated bucket value
* @throws If bucketing value is not a valid string
*/
export const generateBucketValue = function(bucketingKey: string): number {
try {
// NOTE: the mmh library already does cast the hash value as an unsigned 32bit int
// https://github.com/perezd/node-murmurhash/blob/master/murmurhash.js#L115
const hashValue = murmurhash.v3(bucketingKey, HASH_SEED);
const ratio = hashValue / MAX_HASH_VALUE;
return Math.floor(ratio * MAX_TRAFFIC_VALUE);
} catch (ex) {
throw new OptimizelyError(INVALID_BUCKETING_ID, bucketingKey, ex.message);
}
};
Loading
Loading