Skip to content

Commit

Permalink
MWPW-153987 [MILO][MEP] looks in wrong location for MEP test block co…
Browse files Browse the repository at this point in the history
…de on stage (#2693)

* calculate even on stage, find miloLibs and origin outside of loop

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

port over fixes

---------

Co-authored-by: Jingle Huang <32369333+JingleH@users.noreply.github.com>
  • Loading branch information
vgoodric and JingleH authored Aug 12, 2024
1 parent c566823 commit df40a7b
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 20 deletions.
43 changes: 24 additions & 19 deletions libs/features/personalization/personalization.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,21 +169,19 @@ const fetchData = async (url, type = DATA_TYPE.JSON) => {
return null;
};

const getBlockProps = (fVal) => {
const getBlockProps = (fVal, miloLibs, origin) => {
let val = fVal;
if (val?.includes('\\')) val = val?.split('\\').join('/');
if (!val?.startsWith('/')) val = `/${val}`;
const blockSelector = val?.split('/').pop();
const { origin } = PAGE_URL;
if (origin.includes('.hlx.') || origin.includes('localhost')) {
if (val.startsWith('/libs/')) {
/* c8 ignore next 2 */
const { miloLibs, codeRoot } = getConfig();
val = `${miloLibs || codeRoot}${val.replace('/libs', '')}`;
} else {
val = `${origin}${val}`;
}

if (val.startsWith('/libs/')) {
/* c8 ignore next 1 */
val = `${miloLibs}${val.replace('/libs', '')}`;
} else {
val = `${origin}${val}`;
}

return { blockSelector, blockTarget: val };
};

Expand Down Expand Up @@ -457,6 +455,7 @@ const getVariantInfo = (line, variantNames, variants, manifestPath, manifestOver
if (pageFilter && !matchGlob(pageFilter, new URL(window.location).pathname)) return;

if (!config.mep?.preview) manifestId = false;
const { origin } = PAGE_URL;
variantNames.forEach((vn) => {
const targetManifestId = vn.startsWith(TARGET_EXP_PREFIX) ? targetId : false;
if (!line[vn] || line[vn].toLowerCase() === 'false') return;
Expand All @@ -483,7 +482,7 @@ const getVariantInfo = (line, variantNames, variants, manifestPath, manifestOver
variants[vn][action] = variants[vn][action] || [];

if (action === 'useblockcode') {
const { blockSelector, blockTarget } = getBlockProps(line[vn]);
const { blockSelector, blockTarget } = getBlockProps(line[vn], config.miloLibs, origin);
variants[vn][action].push({
selector: blockSelector,
val: blockTarget,
Expand Down Expand Up @@ -578,20 +577,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 @@ -852,6 +852,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

0 comments on commit df40a7b

Please sign in to comment.