-
Notifications
You must be signed in to change notification settings - Fork 274
π§ͺ Add tests for isInRollout algorithm #544
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
+90
β1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import { describe, expect, it, mock } from 'bun:test'; | ||
|
|
||
| // Use the preload setup file instead of inline mocks since bun resolves | ||
| // dynamic imports relative to the test runner's context and caching. | ||
| import './setup'; | ||
|
|
||
| let mockUuid = ''; | ||
| mock.module('../core', () => { | ||
| return { | ||
| cInfo: { | ||
| get uuid() { return mockUuid; } | ||
| } | ||
| }; | ||
| }); | ||
|
|
||
| import { murmurhash3_32_gc } from '../isInRollout'; | ||
|
|
||
| describe('murmurhash3_32_gc', () => { | ||
| it('should be deterministic (return the same output for the same input)', () => { | ||
| const input1 = '123e4567-e89b-12d3-a456-426614174000'; | ||
| const input2 = 'test-string'; | ||
|
|
||
| expect(murmurhash3_32_gc(input1)).toBe(murmurhash3_32_gc(input1)); | ||
| expect(murmurhash3_32_gc(input2)).toBe(murmurhash3_32_gc(input2)); | ||
| }); | ||
|
|
||
| it('should return different outputs for different inputs', () => { | ||
| const input1 = '123e4567-e89b-12d3-a456-426614174000'; | ||
| const input2 = '123e4567-e89b-12d3-a456-426614174001'; | ||
|
|
||
| expect(murmurhash3_32_gc(input1)).not.toBe(murmurhash3_32_gc(input2)); | ||
| }); | ||
|
|
||
| it('should handle empty string correctly', () => { | ||
| expect(typeof murmurhash3_32_gc('')).toBe('number'); | ||
| }); | ||
|
|
||
| it('should return known outputs for known inputs', () => { | ||
| expect(murmurhash3_32_gc('test1') % 100).toBe(24); | ||
| expect(murmurhash3_32_gc('test2') % 100).toBe(69); | ||
| expect(murmurhash3_32_gc('test3') % 100).toBe(0); | ||
| expect(murmurhash3_32_gc('123e4567-e89b-12d3-a456-426614174000') % 100).toBe(36); | ||
| expect(murmurhash3_32_gc('123e4567-e89b-12d3-a456-426614174001') % 100).toBe(94); | ||
| }); | ||
| }); | ||
|
|
||
| describe('isInRollout', () => { | ||
| it('should return true when the rollout is greater than the hash modulo', async () => { | ||
| mockUuid = 'test1'; | ||
| const { isInRollout } = await import(`../isInRollout?id=${Date.now()}`); | ||
| expect(isInRollout(25)).toBe(true); | ||
| }); | ||
|
|
||
| it('should return false when the rollout is equal to the hash modulo', async () => { | ||
| mockUuid = 'test1'; | ||
| const { isInRollout } = await import(`../isInRollout?id=${Date.now()}`); | ||
| expect(isInRollout(24)).toBe(false); | ||
| }); | ||
|
|
||
| it('should return false when the rollout is less than the hash modulo', async () => { | ||
| mockUuid = 'test1'; | ||
| const { isInRollout } = await import(`../isInRollout?id=${Date.now()}`); | ||
| expect(isInRollout(23)).toBe(false); | ||
| }); | ||
|
|
||
| it('should evaluate correctly for a different uuid', async () => { | ||
| mockUuid = 'test3'; | ||
| const { isInRollout } = await import(`../isInRollout?id=${Date.now()}`); | ||
| expect(isInRollout(1)).toBe(true); | ||
| expect(isInRollout(0)).toBe(false); | ||
| expect(isInRollout(-1)).toBe(false); | ||
| }); | ||
|
|
||
| it('should always return false for 0% rollout', async () => { | ||
| mockUuid = 'test1'; | ||
| const { isInRollout } = await import(`../isInRollout?id=${Date.now()}`); | ||
| expect(isInRollout(0)).toBe(false); | ||
| }); | ||
|
|
||
| it('should always return true for 100% rollout', async () => { | ||
| mockUuid = 'test1'; | ||
| const { isInRollout } = await import(`../isInRollout?id=${Date.now()}`); | ||
| expect(isInRollout(100)).toBe(true); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π§© Analysis chain
π Script executed:
Repository: reactnativecn/react-native-update
Length of output: 59
π Script executed:
Repository: reactnativecn/react-native-update
Length of output: 112
π Script executed:
Repository: reactnativecn/react-native-update
Length of output: 3699
π Script executed:
ls -la ./src/__tests__/ | grep -i setupRepository: reactnativecn/react-native-update
Length of output: 134
π Script executed:
Repository: reactnativecn/react-native-update
Length of output: 1443
π Script executed:
cat -n ./src/isInRollout.ts | head -50Repository: reactnativecn/react-native-update
Length of output: 1773
π Script executed:
cat -n ./src/isInRollout.ts | tail -30Repository: reactnativecn/react-native-update
Length of output: 928
Use a monotonic nonce instead of
Date.now()for dynamic-import cache busting.Date.now()can repeat within the same millisecond, which risks reusing the same module cache key across consecutive tests and causing test flakiness when mockUuid values differ.Suggested fix
π€ Prompt for AI Agents