Skip to content

Commit

Permalink
Fix patching for correct access to
Browse files Browse the repository at this point in the history
  • Loading branch information
aliu39 committed Jan 9, 2025
1 parent f1a5574 commit 34af880
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,14 @@ sentryTest('Basic isEnabled test with eviction, update, and no async tasks', asy
const req = await reqPromise;
const event = envelopeRequestParser(req);

const expectedFlags = [{ flag: 'feat2', result: false }];
for (let i = 4; i <= FLAG_BUFFER_SIZE; i++) {
const expectedFlags = [{ flag: 'feat2', result: true }];
expectedFlags.push({ flag: 'feat4', result: true });
expectedFlags.push({ flag: 'feat5', result: true });
for (let i = 6; i <= FLAG_BUFFER_SIZE; i++) {
expectedFlags.push({ flag: `feat${i}`, result: false });
}
expectedFlags.push({ flag: `feat${FLAG_BUFFER_SIZE + 1}`, result: false });
expectedFlags.push({ flag: 'feat3', result: false });
expectedFlags.push({ flag: 'feat3', result: true });

expect(event.contexts?.flags?.values).toEqual(expectedFlags);
});
Expand Down Expand Up @@ -76,12 +78,14 @@ sentryTest('Basic getVariant test with eviction, update, and no async tasks', as
const req = await reqPromise;
const event = envelopeRequestParser(req);

const expectedFlags = [{ flag: 'feat2', result: false }];
for (let i = 4; i <= FLAG_BUFFER_SIZE; i++) {
const expectedFlags = [{ flag: 'feat2', result: true }];
expectedFlags.push({ flag: 'feat4', result: true });
expectedFlags.push({ flag: 'feat5', result: true });
for (let i = 6; i <= FLAG_BUFFER_SIZE; i++) {
expectedFlags.push({ flag: `feat${i}`, result: false });
}
expectedFlags.push({ flag: `feat${FLAG_BUFFER_SIZE + 1}`, result: false });
expectedFlags.push({ flag: 'feat3', result: false });
expectedFlags.push({ flag: 'feat3', result: true });

expect(event.contexts?.flags?.values).toEqual(expectedFlags);
});
Original file line number Diff line number Diff line change
@@ -1,16 +1,43 @@
import * as Sentry from '@sentry/browser';

window.UnleashClient = class {
isEnabled(_toggleName) {
return false;
}
constructor() {
this._featureToVariant = {
'feat2': {name: 'variant1', enabled: true, feature_enabled: true, payload: {type: 'string', value: 'test'}},
'feat3': {name: 'eu-west', enabled: true, feature_enabled: true},
'feat4': {
name: 'paid-orgs',
enabled: true,
feature_enabled: true,
payload: {
type: 'json',
value: '{"foo": {"bar": "baz"}, "hello": [1, 2, 3]}',
},
},

// Enabled feature with no configured variants.
'feat5': {name: 'disabled', enabled: false, feature_enabled: true},

// Disabled feature.
'feat6': {name: 'disabled', enabled: false, feature_enabled: false},
};

getVariant(_toggleName) {
return {
// Variant returned for features that don't exist.
// `feature_enabled` may be defined in prod, but we want to test the undefined case.
this._fallbackVariant = {
name: 'disabled',
enabled: false,
};
}

isEnabled(toggleName) {
const variant = this._featureToVariant[toggleName] || this._fallbackVariant;
return variant.feature_enabled || false;
}

getVariant(toggleName) {
return this._featureToVariant[toggleName] || this._fallbackVariant;
}
};

window.Sentry = Sentry;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export const unleashIntegration = defineIntegration((unleashClientClass: Unleash
return result;
},
};
const originalIsEnabled = unleashClientPrototype.isEnabled.bind(unleashClientPrototype);
const originalIsEnabled = unleashClientPrototype.isEnabled;
unleashClientPrototype.isEnabled = new Proxy(originalIsEnabled, sentryIsEnabled);

const sentryGetVariant = {
Expand All @@ -61,12 +61,12 @@ export const unleashIntegration = defineIntegration((unleashClientClass: Unleash
args: [toggleName: string],
) => {
const variant = Reflect.apply(target, thisArg, args);
const result = variant.enabled;
const result = variant.feature_enabled || false; // undefined means the feature does not exist.
insertFlagToScope(args[0], result);
return variant;
},
};
const originalGetVariant = unleashClientPrototype.getVariant.bind(unleashClientPrototype);
const originalGetVariant = unleashClientPrototype.getVariant;
unleashClientPrototype.getVariant = new Proxy(originalGetVariant, sentryGetVariant);
},
};
Expand Down

0 comments on commit 34af880

Please sign in to comment.