Skip to content
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
85 changes: 85 additions & 0 deletions src/__tests__/isInRollout.test.ts
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()}`);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟑 Minor

🧩 Analysis chain

🏁 Script executed:

cd /tmp && find . -name "isInRollout.test.ts" -type f 2>/dev/null | head -5

Repository: reactnativecn/react-native-update

Length of output: 59


🏁 Script executed:

find . -name "isInRollout.test.ts" -type f

Repository: reactnativecn/react-native-update

Length of output: 112


🏁 Script executed:

cat -n ./src/__tests__/isInRollout.test.ts

Repository: reactnativecn/react-native-update

Length of output: 3699


🏁 Script executed:

ls -la ./src/__tests__/ | grep -i setup

Repository: reactnativecn/react-native-update

Length of output: 134


🏁 Script executed:

cat -n ./src/__tests__/setup.ts

Repository: reactnativecn/react-native-update

Length of output: 1443


🏁 Script executed:

cat -n ./src/isInRollout.ts | head -50

Repository: reactnativecn/react-native-update

Length of output: 1773


🏁 Script executed:

cat -n ./src/isInRollout.ts | tail -30

Repository: 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
+let importNonce = 0;
+const importFreshIsInRollout = () => import(`../isInRollout?id=${++importNonce}`);
+
 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()}`);
+    const { isInRollout } = await importFreshIsInRollout();
     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()}`);
+    const { isInRollout } = await importFreshIsInRollout();
     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()}`);
+    const { isInRollout } = await importFreshIsInRollout();
     expect(isInRollout(23)).toBe(false);
   });

   it('should evaluate correctly for a different uuid', async () => {
     mockUuid = 'test3';
-    const { isInRollout } = await import(`../isInRollout?id=${Date.now()}`);
+    const { isInRollout } = await importFreshIsInRollout();
     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()}`);
+    const { isInRollout } = await importFreshIsInRollout();
     expect(isInRollout(0)).toBe(false);
   });

   it('should always return true for 100% rollout', async () => {
     mockUuid = 'test1';
-    const { isInRollout } = await import(`../isInRollout?id=${Date.now()}`);
+    const { isInRollout } = await importFreshIsInRollout();
     expect(isInRollout(100)).toBe(true);
   });
 });
πŸ€– Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/__tests__/isInRollout.test.ts` at line 50, The dynamic import
cache-busting currently uses Date.now() which can collide; replace it with a
monotonic nonce so each import path is unique across rapid consecutive tests. In
the test where you do const { isInRollout } = await
import(`../isInRollout?id=${Date.now()}`); introduce a monotonic counter (e.g.,
a module-scoped let monotonicNonce = 0 and increment it for each import, or a
helper getMonotonicNonce that returns ++monotonicNonce or
process.hrtime.bigint().toString()) and use that value in the import template
string instead of Date.now(); ensure the helper is declared in the test module
so the import line for isInRollout uses the monotonic value.

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);
});
});
4 changes: 4 additions & 0 deletions src/__tests__/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,7 @@ mock.module('../i18n', () => {
},
};
});

mock.module('react-native/Libraries/Core/ReactNativeVersion', () => ({
version: { major: 0, minor: 73, patch: 0 },
}));
2 changes: 1 addition & 1 deletion src/isInRollout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { cInfo } from './core';

/* eslint-disable no-bitwise */
function murmurhash3_32_gc(key: string, seed = 0) {
export function murmurhash3_32_gc(key: string, seed = 0) {
let remainder, bytes, h1, h1b, c1, c2, k1, i;

remainder = key.length & 3; // key.length % 4
Expand Down
Loading