From bafa9f827a479c340f6f57bd1a1ca679bc77e5ed Mon Sep 17 00:00:00 2001 From: Jason Salaber Date: Mon, 2 Dec 2024 11:31:46 -0500 Subject: [PATCH] fix: added test for non 100 or 0 start percentages --- .../bucketing/__tests__/bucketing.test.ts | 75 +++++++++++++++++++ lib/shared/bucketing/src/bucketing.ts | 2 +- 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/lib/shared/bucketing/__tests__/bucketing.test.ts b/lib/shared/bucketing/__tests__/bucketing.test.ts index b155c1b89..1b757d1fb 100644 --- a/lib/shared/bucketing/__tests__/bucketing.test.ts +++ b/lib/shared/bucketing/__tests__/bucketing.test.ts @@ -1416,6 +1416,81 @@ describe('Rollout Logic', () => { ).toBeFalsy() }) + it('should handle stepped rollout with 0% start and 100% later stage', () => { + const rollout: PublicRollout = { + type: 'stepped', + startDate: new Date(), + startPercentage: 0, + stages: [ + { + type: 'discrete', + date: new Date( + new Date().getTime() + 1000 * 60 * 60 * 24 * 7, + ), + percentage: 1, + }, + ], + } + + // Before next stage - should be 0% + jest.useFakeTimers().setSystemTime(new Date()) + for (let i = 0; i < 100; i++) { + expect( + doesUserPassRollout({ rollout, boundedHash: i / 100 }), + ).toBeFalsy() + } + + // After 100% stage - should pass all users + jest.useFakeTimers().setSystemTime( + new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 8), + ) + for (let i = 0; i < 100; i++) { + expect( + doesUserPassRollout({ rollout, boundedHash: i / 100 }), + ).toBeTruthy() + } + + jest.useRealTimers() + }) + + it('should handle stepped rollout with 50% start and 100% later stage', () => { + const rollout: PublicRollout = { + type: 'stepped', + startDate: new Date(), + startPercentage: 0.5, + stages: [ + { + type: 'discrete', + date: new Date( + new Date().getTime() + 1000 * 60 * 60 * 24 * 7, + ), + percentage: 1, + }, + ], + } + + // Before next stage - should be 50% + jest.useFakeTimers().setSystemTime(new Date()) + expect( + doesUserPassRollout({ rollout, boundedHash: 0.49 }), + ).toBeTruthy() + expect( + doesUserPassRollout({ rollout, boundedHash: 0.51 }), + ).toBeFalsy() + + // After 100% stage - should pass all users + jest.useFakeTimers().setSystemTime( + new Date(new Date().getTime() + 1000 * 60 * 60 * 24 * 8), + ) + for (let i = 0; i < 100; i++) { + expect( + doesUserPassRollout({ rollout, boundedHash: i / 100 }), + ).toBeTruthy() + } + + jest.useRealTimers() + }) + it('should handle stepped rollout with 100% start and 0% later stage', () => { const rollout: PublicRollout = { type: 'stepped', diff --git a/lib/shared/bucketing/src/bucketing.ts b/lib/shared/bucketing/src/bucketing.ts index abf6dce06..03eda02ab 100644 --- a/lib/shared/bucketing/src/bucketing.ts +++ b/lib/shared/bucketing/src/bucketing.ts @@ -105,7 +105,7 @@ export const getCurrentRolloutPercentage = ( : null) if (!currentStage) { - return 0 + return start } if (!nextStage || nextStage.type === 'discrete') {