Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MWPW-155314 [MEP] Enable Entitlements + Operator #2638

Merged
merged 1 commit into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions libs/features/personalization/personalization.js
Original file line number Diff line number Diff line change
Expand Up @@ -576,20 +576,26 @@ const checkForParamMatch = (paramStr) => {
return false;
};

function trimNames(arr) {
return arr.map((v) => v.trim()).filter(Boolean);
}
export function buildVariantInfo(variantNames) {
return variantNames.reduce((acc, name) => {
let nameArr = [name];
if (!name.startsWith(TARGET_EXP_PREFIX)) nameArr = name.split(',');
acc[name] = trimNames(nameArr);
acc.allNames = [...acc.allNames, ...trimNames(name.split(/[,&]|\bnot\b/))];
return acc;
}, { allNames: [] });
}

async function getPersonalizationVariant(manifestPath, variantNames = [], variantLabel = null) {
const config = getConfig();
if (config.mep?.variantOverride?.[manifestPath]) {
return config.mep.variantOverride[manifestPath];
}

const variantInfo = variantNames.reduce((acc, name) => {
let nameArr = [name];
if (!name.startsWith(TARGET_EXP_PREFIX)) nameArr = name.split(',');
const vNames = nameArr.map((v) => v.trim()).filter(Boolean);
acc[name] = vNames;
acc.allNames = [...acc.allNames, ...vNames];
return acc;
}, { allNames: [] });
const variantInfo = buildVariantInfo(variantNames);

const entitlementKeys = Object.values(await getEntitlementMap());
const hasEntitlementTag = entitlementKeys.some((tag) => variantInfo.allNames.includes(tag));
Expand Down
3 changes: 3 additions & 0 deletions libs/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,9 @@ export async function loadIms() {
if (!window.adobeIMS?.isSignedInUser()) {
getConfig().entitlements([]);
}
}).catch((e) => {
getConfig().entitlements([]);
throw e;
});

return imsLoaded;
Expand Down
105 changes: 104 additions & 1 deletion test/features/personalization/personalization.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { assert, stub } from 'sinon';
import { getConfig, setConfig } from '../../../libs/utils/utils.js';
import {
handleFragmentCommand, applyPers,
init, matchGlob, createFrag, combineMepSources,
init, matchGlob, createFrag, combineMepSources, buildVariantInfo,
} from '../../../libs/features/personalization/personalization.js';
import spoofParams from './spoofParams.js';
import mepSettings from './mepSettings.js';
Expand Down Expand Up @@ -175,6 +175,109 @@ describe('Functional Test', () => {
expect(getConfig().mep?.martech).to.equal('|fireflies|manifest');
});

it('should resolve variants correctly with entitlements and tags exist', async () => {
expect(buildVariantInfo(['cc-all-apps-any & desktop'])).to.deep.equal({
allNames: [
'cc-all-apps-any',
'desktop',
],
'cc-all-apps-any & desktop': [
'cc-all-apps-any & desktop',
],
});
expect(buildVariantInfo(['desktop & cc-all-apps-any'])).to.deep.equal({
allNames: [
'desktop',
'cc-all-apps-any',
],
'desktop & cc-all-apps-any': [
'desktop & cc-all-apps-any',
],
});
expect(buildVariantInfo(['cc-all-apps-any'])).to.deep.equal({
allNames: [
'cc-all-apps-any',
],
'cc-all-apps-any': [
'cc-all-apps-any',
],
});
expect(buildVariantInfo(['phone, cc-all-apps-any'])).to.deep.equal({
allNames: [
'phone',
'cc-all-apps-any',
],
'phone, cc-all-apps-any': [
'phone',
'cc-all-apps-any',
],
});
expect(buildVariantInfo(['cc-all-apps-any, not desktop'])).to.deep.equal({
allNames: [
'cc-all-apps-any',
'desktop',
],
'cc-all-apps-any, not desktop': [
'cc-all-apps-any',
'not desktop',
],
});
expect(buildVariantInfo(['phone & not cc-all-apps-any'])).to.deep.equal({
allNames: [
'phone',
'cc-all-apps-any',
],
'phone & not cc-all-apps-any': [
'phone & not cc-all-apps-any',
],
});
expect(buildVariantInfo(['not phone & not cc-all-apps-any'])).to.deep.equal({
allNames: [
'phone',
'cc-all-apps-any',
],
'not phone & not cc-all-apps-any': [
'not phone & not cc-all-apps-any',
],
});
expect(buildVariantInfo(['not cc-free & not cc-all-apps-any'])).to.deep.equal({
allNames: [
'cc-free',
'cc-all-apps-any',
],
'not cc-free & not cc-all-apps-any': [
'not cc-free & not cc-all-apps-any',
],
});
expect(buildVariantInfo(['not cc-free, not cc-all-apps-any'])).to.deep.equal({
allNames: [
'cc-free',
'cc-all-apps-any',
],
'not cc-free, not cc-all-apps-any': [
'not cc-free',
'not cc-all-apps-any',
],
});
expect(buildVariantInfo(['not cc-free, not cc-all-apps-any', 'desktop & cc-paid, ios'])).to.deep.equal({
allNames: [
'cc-free',
'cc-all-apps-any',
'desktop',
'cc-paid',
'ios',
],
'not cc-free, not cc-all-apps-any': [
'not cc-free',
'not cc-all-apps-any',
],
'desktop & cc-paid, ios': [
'desktop & cc-paid',
'ios',
],
});
});

it('invalid selector should output error to console', async () => {
window.console.log = stub();

Expand Down