|
| 1 | +import * as assert from "assert"; |
| 2 | + |
| 3 | +import * as vscode from "vscode"; |
| 4 | +import sinon from "sinon"; |
| 5 | + |
| 6 | +import { featureEnabled, FEATURE_FLAGS } from "../../common"; |
| 7 | + |
| 8 | +suite("Common", () => { |
| 9 | + let sandbox: sinon.SinonSandbox; |
| 10 | + |
| 11 | + setup(() => { |
| 12 | + sandbox = sinon.createSandbox(); |
| 13 | + const number = 42; |
| 14 | + sandbox.stub(vscode.env, "machineId").value(number.toString(16)); |
| 15 | + }); |
| 16 | + |
| 17 | + teardown(() => { |
| 18 | + sandbox.restore(); |
| 19 | + }); |
| 20 | + |
| 21 | + test("returns consistent results for the same rollout percentage", () => { |
| 22 | + const firstCall = featureEnabled("tapiocaAddon"); |
| 23 | + |
| 24 | + for (let i = 0; i < 50; i++) { |
| 25 | + const result = featureEnabled("tapiocaAddon"); |
| 26 | + |
| 27 | + assert.strictEqual( |
| 28 | + firstCall, |
| 29 | + result, |
| 30 | + "Feature flag should be deterministic", |
| 31 | + ); |
| 32 | + } |
| 33 | + }); |
| 34 | + |
| 35 | + test("maintains enabled state when increasing rollout percentage", () => { |
| 36 | + // For the fake machine of 42 in base 16 and the name `fakeFeature`, the feature flag activation percetange is |
| 37 | + // 0.357. For every percetange below that, the feature should appear as disabled |
| 38 | + [0.25, 0.3, 0.35].forEach((percentage) => { |
| 39 | + (FEATURE_FLAGS as any).fakeFeature = percentage; |
| 40 | + assert.strictEqual(featureEnabled("fakeFeature" as any), false); |
| 41 | + }); |
| 42 | + |
| 43 | + // And for every percentage above that, the feature should appear as enabled |
| 44 | + [0.36, 0.45, 0.55, 0.65, 0.75, 0.85, 0.9, 1].forEach((percentage) => { |
| 45 | + (FEATURE_FLAGS as any).fakeFeature = percentage; |
| 46 | + assert.strictEqual(featureEnabled("fakeFeature" as any), true); |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + test("returns false if user opted out of specific feature", () => { |
| 51 | + (FEATURE_FLAGS as any).fakeFeature = 1; |
| 52 | + |
| 53 | + const stub = sandbox.stub(vscode.workspace, "getConfiguration").returns({ |
| 54 | + get: () => { |
| 55 | + return { fakeFeature: false }; |
| 56 | + }, |
| 57 | + } as any); |
| 58 | + |
| 59 | + const result = featureEnabled("fakeFeature" as any); |
| 60 | + stub.restore(); |
| 61 | + assert.strictEqual(result, false); |
| 62 | + }); |
| 63 | + |
| 64 | + test("returns false if user opted out of all features", () => { |
| 65 | + (FEATURE_FLAGS as any).fakeFeature = 1; |
| 66 | + |
| 67 | + const stub = sandbox.stub(vscode.workspace, "getConfiguration").returns({ |
| 68 | + get: () => { |
| 69 | + return { all: false }; |
| 70 | + }, |
| 71 | + } as any); |
| 72 | + |
| 73 | + const result = featureEnabled("fakeFeature" as any); |
| 74 | + stub.restore(); |
| 75 | + assert.strictEqual(result, false); |
| 76 | + }); |
| 77 | + |
| 78 | + test("returns true if user opted in to all features", () => { |
| 79 | + (FEATURE_FLAGS as any).fakeFeature = 0.02; |
| 80 | + |
| 81 | + const stub = sandbox.stub(vscode.workspace, "getConfiguration").returns({ |
| 82 | + get: () => { |
| 83 | + return { all: true }; |
| 84 | + }, |
| 85 | + } as any); |
| 86 | + |
| 87 | + const result = featureEnabled("fakeFeature" as any); |
| 88 | + stub.restore(); |
| 89 | + assert.strictEqual(result, true); |
| 90 | + }); |
| 91 | +}); |
0 commit comments