Skip to content

Commit

Permalink
MWPW-166002 | [MEP] Use Target served version of manifest in prod whe…
Browse files Browse the repository at this point in the history
…n Target experience is chosen (#3527)

* Added server vs target manifest handling

* refactored a bit

* refactored test

---------

Co-authored-by: Denys Fedotov <dfedotov@Denyss-MacBook-Pro.local>
  • Loading branch information
denlight and Denys Fedotov authored Jan 24, 2025
1 parent 2f2a6dd commit db30bfc
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 9 deletions.
14 changes: 10 additions & 4 deletions libs/features/personalization/personalization.js
Original file line number Diff line number Diff line change
Expand Up @@ -967,10 +967,11 @@ function compareExecutionOrder(a, b) {
return a.executionOrder > b.executionOrder ? 1 : -1;
}

export function cleanAndSortManifestList(manifests) {
const config = getConfig();
export function cleanAndSortManifestList(manifests, conf) {
const config = conf ?? getConfig();
const manifestObj = {};
let allManifests = manifests;
let targetManifestWinsOverServerManifest = false;
if (config.mep?.experiments) allManifests = [...manifests, ...config.mep.experiments];
allManifests.forEach((manifest) => {
try {
Expand All @@ -986,6 +987,12 @@ export function cleanAndSortManifestList(manifests) {
freshManifest.source = freshManifest.source.concat(fullManifest.source);
freshManifest.name = fullManifest.name;
freshManifest.selectedVariantName = fullManifest.selectedVariantName;
targetManifestWinsOverServerManifest = config?.env?.name === 'prod' && fullManifest.selectedVariantName.startsWith('target-');

freshManifest.variants = targetManifestWinsOverServerManifest
? fullManifest.variants
: freshManifest.variants;

freshManifest.selectedVariant = freshManifest.variants[freshManifest.selectedVariantName];
manifestObj[manifest.manifestPath] = freshManifest;
} else {
Expand All @@ -994,9 +1001,8 @@ export function cleanAndSortManifestList(manifests) {

const manifestConfig = manifestObj[manifest.manifestPath];
const { selectedVariantName, variantNames, placeholderData } = manifestConfig;

if (selectedVariantName && variantNames.includes(selectedVariantName)) {
manifestConfig.run = true;
manifestConfig.selectedVariantName = selectedVariantName;
manifestConfig.selectedVariant = manifestConfig.variants[selectedVariantName];
} else {
/* c8 ignore next 2 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,33 @@
}
],
"fragments": []
},
"target-test": {
"commands": [
{
"action": "append",
"selector": "section1",
"pageFilter": "",
"target": "https://main--milo--adobecom.hlx.page/drafts/vgoodrich/fragments/2024/q1/mepmanifestorder/fragments/test1",
"selectorType": "css"
}
],
"fragments": []
}
},
"variantNames": [
"all"
"all",
"target-test"
],
"manifestOverrideName": "",
"manifestType": "test",
"executionOrder": "1-2",
"run": true,
"selectedVariantName": "all",
"selectedVariantName": "target-test",
"selectedVariant": {
"commands": [
{
"action": "appendtosection",
"action": "append",
"selector": "section1",
"pageFilter": "",
"target": "https://main--milo--adobecom.hlx.page/drafts/vgoodrich/fragments/2024/q1/mepmanifestorder/fragments/test",
Expand All @@ -34,6 +47,7 @@
],
"fragments": []
},
"source": ["target"],
"name": "MILO0013 - manifest order",
"manifest": "https://main--milo--adobecom.hlx.page/drafts/vgoodrich/fragments/2024/q1/mepmanifestorder/manifests/test-normal.json",
"manifestUrl": "https://main--milo--adobecom.hlx.page/drafts/vgoodrich/fragments/2024/q1/mepmanifestorder/manifests/test-normal.json",
Expand All @@ -52,10 +66,23 @@
}
],
"fragments": []
},
"target-test": {
"commands": [
{
"action": "appendtosection",
"selector": "section1",
"pageFilter": "",
"target": "https://main--milo--adobecom.hlx.page/drafts/vgoodrich/fragments/2024/q1/mepmanifestorder/fragments/test1",
"selectorType": "css"
}
],
"fragments": []
}
},
"variantNames": [
"all"
"all",
"target-test"
],
"manifestOverrideName": "",
"manifestType": "personalization",
Expand All @@ -74,6 +101,7 @@
],
"fragments": []
},
"source": ["pzn"],
"manifest": "/drafts/vgoodrich/fragments/2024/q1/mepmanifestorder/manifests/pzn-normal.json",
"manifestUrl": "https://main--milo--adobecom.hlx.page/drafts/vgoodrich/fragments/2024/q1/mepmanifestorder/manifests/pzn-normal.json",
"manifestPath": "/drafts/vgoodrich/fragments/2024/q1/mepmanifestorder/manifests/pzn-normal.json"
Expand Down
26 changes: 25 additions & 1 deletion test/features/personalization/personalization.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { readFile } from '@web/test-runner-commands';
import { assert, stub } from 'sinon';
import { getConfig, setConfig } from '../../../libs/utils/utils.js';
import {
handleFragmentCommand, applyPers,
handleFragmentCommand, applyPers, cleanAndSortManifestList,
init, matchGlob, createContent, combineMepSources, buildVariantInfo,
} from '../../../libs/features/personalization/personalization.js';
import mepSettings from './mepSettings.js';
Expand Down Expand Up @@ -396,4 +396,28 @@ describe('MEP Utils', () => {
expect(manifests[4].manifestPath).to.equal('/mep-param/manifest2.json');
});
});
describe('cleanAndSortManifestList', async () => {
it('chooses server manifest over target manifest if same manifest path', async () => {
const config = { env: { name: 'stage' } };
let manifests = await readFile({ path: './mocks/manifestLists/two-manifests-one-from-target.json' });
manifests = JSON.parse(manifests);
manifests[0].manifestPath = 'same path';
manifests[1].manifestPath = 'same path';
const response = cleanAndSortManifestList(manifests, config);
const result = response.find((manifest) => manifest.source.length > 1);
expect(result).to.be.not.null;
expect(result.selectedVariant.commands[0].action).to.equal('appendtosection');
});
it('chooses target manifest over server manifest if same manifest path and in production and selected audience is "target-*"', async () => {
const config = { env: { name: 'prod' } };
let manifests = await readFile({ path: './mocks/manifestLists/two-manifests-one-from-target.json' });
manifests = JSON.parse(manifests);
manifests[0].manifestPath = 'same path';
manifests[1].manifestPath = 'same path';
const response = cleanAndSortManifestList(manifests, config);
const result = response.find((manifest) => manifest.source.length > 1);
expect(result).to.be.not.null;
expect(result.selectedVariant.commands[0].action).to.equal('append');
});
});
});

0 comments on commit db30bfc

Please sign in to comment.