Skip to content

Commit

Permalink
chore(test): add repartition test
Browse files Browse the repository at this point in the history
  • Loading branch information
mfrachet committed Oct 17, 2024
1 parent 5ab431e commit f835461
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions packages/core/src/__tests__/repartition.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { describe, expect, it } from "vitest";
import { createFlagEngine } from "..";

const engine = createFlagEngine([
{
key: "summer-sale",
status: "enabled",
strategies: [
{
name: "All audience",
rules: [],
variants: [
{
name: "A",
percent: 50,
},
{
name: "B",
percent: 50,
},
],
},
],
},
]);

describe("repartition", () => {
it("should be distributive", () => {
const COUNT = 1000_000;
let A = 0;
let B = 0;

for (let i = 0; i < COUNT; i++) {
const userCtx = engine.createUserContext({ __id: `user-${i}` });
const variant = userCtx.evaluate("summer-sale");
if (variant === "A") {
A++;
} else if (variant === "B") {
B++;
}
}

const halfCount = COUNT / 2;
const halfCountUpper = halfCount * 1.01;
const halfCountLower = halfCount * 0.99;

expect(A).toBeGreaterThan(halfCountLower);
expect(A).toBeLessThan(halfCountUpper);
expect(B).toBeGreaterThan(halfCountLower);
expect(B).toBeLessThan(halfCountUpper);
});
});

0 comments on commit f835461

Please sign in to comment.