|
| 1 | +/** |
| 2 | + * Copyright 2025, Optimizely |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +import { expect, describe, it } from 'vitest'; |
| 17 | +import { sprintf } from '../../utils/fns'; |
| 18 | +import { generateBucketValue } from './bucket_value_generator'; |
| 19 | +import { OptimizelyError } from '../../error/optimizly_error'; |
| 20 | +import { INVALID_BUCKETING_ID } from 'error_message'; |
| 21 | + |
| 22 | +describe('generateBucketValue', () => { |
| 23 | + it('should return a bucket value for different inputs', () => { |
| 24 | + const experimentId = 1886780721; |
| 25 | + const bucketingKey1 = sprintf('%s%s', 'ppid1', experimentId); |
| 26 | + const bucketingKey2 = sprintf('%s%s', 'ppid2', experimentId); |
| 27 | + const bucketingKey3 = sprintf('%s%s', 'ppid2', 1886780722); |
| 28 | + const bucketingKey4 = sprintf('%s%s', 'ppid3', experimentId); |
| 29 | + |
| 30 | + expect(generateBucketValue(bucketingKey1)).toBe(5254); |
| 31 | + expect(generateBucketValue(bucketingKey2)).toBe(4299); |
| 32 | + expect(generateBucketValue(bucketingKey3)).toBe(2434); |
| 33 | + expect(generateBucketValue(bucketingKey4)).toBe(5439); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should return an error if it cannot generate the hash value', () => { |
| 37 | + // eslint-disable-next-line @typescript-eslint/ban-ts-comment |
| 38 | + // @ts-ignore |
| 39 | + expect(() => generateBucketValue(null)).toThrowError( |
| 40 | + new OptimizelyError(INVALID_BUCKETING_ID) |
| 41 | + ); |
| 42 | + }); |
| 43 | +}); |
0 commit comments