Skip to content

test(fxa-shared): Make experiments/base hash tests deterministic - #20971

Open
nshirley wants to merge 1 commit into
mainfrom
FXA-14281
Open

test(fxa-shared): Make experiments/base hash tests deterministic#20971
nshirley wants to merge 1 commit into
mainfrom
FXA-14281

Conversation

@nshirley

@nshirley nshirley commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

Because

The hash and luckyNumber tests in packages/fxa-shared/test/experiments/base.js hashed 1000 Math.random() values and asserted every result was unique. The hash is 32 bits, so by the birthday paradox a collision is expected roughly 1% of the time (~1 - e^(-1000²/2^33)) — this flaked the Unit Test job on the v1.342.0 stage deploy pipeline.

This pull request

  • Uses fixed keys (key-0key-999) instead of Math.random(), so the tests are deterministic.
  • Replaces the uniqueness assertions with the behaviour actually under test: output range, stability for a given key, and spread across the output range (every decile non-empty).

Issue that this pull request solves

Fixes: FXA-14281

Checklist

Put an x in the boxes that apply

  • My commit is GPG signed.
  • If applicable, I have modified or added tests which pass locally.
  • I have added necessary documentation (if appropriate).
  • I have verified that my changes render correctly in RTL (if appropriate).

Screenshots (Optional)

N/A

Other information (Optional)

Verified locally: all 20 tests in the file pass.

Because:

* The hash and luckyNumber tests hashed 1000 Math.random() values and
  asserted every result was unique. The hash is 32 bits, so by the
  birthday paradox a collision occurred roughly 1% of the time.

This commit:

* Uses fixed keys instead of Math.random().
* Replaces the uniqueness assertions with the behaviour actually under
  test: output range, stability for a given key, and spread across the
  output range.

Fixes FXA-14281
@nshirley
nshirley marked this pull request as ready for review August 6, 2026 22:50
@nshirley
nshirley requested a review from a team as a code owner August 6, 2026 22:50
Copilot AI review requested due to automatic review settings August 6, 2026 22:50

Copilot AI left a comment

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.

Pull request overview

This PR fixes unit-test flakiness in fxa-shared experiments tests by removing randomness from hash/luckyNumber inputs and replacing collision-based assertions with deterministic distribution and stability checks.

Changes:

  • Replace Math.random() inputs with deterministic keys (key-0key-999) for repeatable test runs.
  • Update hash and luckyNumber tests to validate range, determinism for a given key, and basic spread across the output space.
Suppressed comments (1)

packages/fxa-shared/test/experiments/base.js:72

  • This range check relies on JS coercion; e.g. a string value could still satisfy the comparison. Adding a type assertion keeps the test aligned with the method contract (luckyNumber returns a number in experiments/base.ts).
        const luckyNumber = experiment.luckyNumber(key);
        assert.ok(0 <= luckyNumber && luckyNumber <= 1);
      });

Comment on lines +47 to +49
hashes.forEach((hash) => {
assert.ok(0 <= hash && hash < MAX_HASH_VALUE);
hashes[i] = hash;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants