Skip to content

Commit 4cf35bb

Browse files
committed
fixing test that would fail due to uncontrolled RNG
1 parent 023a4ff commit 4cf35bb

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/nps/userSurvey.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import { ReactWebviewPanelController } from "../controllers/reactWebviewPanelCon
1515
import { sendActionEvent } from "../telemetry/telemetry";
1616
import VscodeWrapper from "../controllers/vscodeWrapper";
1717

18-
const PROBABILITY = 0.15;
18+
/** Likelihood that a user is prompted to take the survey, after they've already passed all other checks */
19+
export const SELECTION_PROBABILITY = 0.15;
20+
1921
export const SESSION_COUNT_KEY = "nps/sessionCount";
2022
export const LAST_SESSION_DATE_KEY = "nps/lastSessionDate";
2123
export const SKIP_VERSION_KEY = "nps/skipVersion";
@@ -227,7 +229,8 @@ export class UserSurvey {
227229
}
228230

229231
// 5. Of the remaining users, randomly select a subset to prompt to ensure we get feedback from a variety of users over time
230-
const isCandidate = globalState.get(IS_CANDIDATE_KEY, false) || Math.random() < PROBABILITY;
232+
const isCandidate =
233+
globalState.get(IS_CANDIDATE_KEY, false) || Math.random() < SELECTION_PROBABILITY;
231234
await globalState.update(IS_CANDIDATE_KEY, isCandidate);
232235

233236
if (!isCandidate) {

test/unit/userSurvey.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { TelemetryActions, TelemetryViews } from "../../src/sharedInterfaces/tel
1515
import {
1616
FunnelSteps,
1717
NEVER_KEY,
18+
SELECTION_PROBABILITY,
1819
SKIP_VERSION_KEY,
1920
UserSurvey,
2021
UserSurveyWebviewController,
@@ -289,7 +290,9 @@ suite("UserSurvey Tests", () => {
289290
globalState.get.withArgs(SKIP_VERSION_KEY, "").returns("");
290291
globalState.get.withArgs("nps/lastSessionDate").returns("01/01/2023"); // not today
291292
globalState.get.withArgs("nps/sessionCount").returns(999); // high enough to be eligible
292-
globalState.get.withArgs("nps/isCandidate").returns(false); // not selected by die roll
293+
globalState.get.withArgs("nps/isCandidate").returns(false); // not already determined to be a candidate
294+
295+
sinon.stub(Math, "random").returns(SELECTION_PROBABILITY + 0.1); // ensure not selected by RNG check
293296

294297
const userSurvey = UserSurvey.getInstance();
295298
const result = await userSurvey["shouldPromptForFeedback"](testSurveySource);

0 commit comments

Comments
 (0)