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

Additional functional test and unit test for AJO content card feature #1169

Merged
merged 3 commits into from
Sep 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
366 changes: 361 additions & 5 deletions test/functional/specs/Personalization/C1234567.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTA
OF ANY KIND, either express or implied. See the License for the specific language
governing permissions and limitations under the License.
*/
import { ClientFunction, t } from "testcafe";
import { ClientFunction, t, Selector } from "testcafe";
import uuid from "../../../../src/utils/uuid.js";
import createNetworkLogger from "../../helpers/networkLogger/index.js";
import createFixture from "../../helpers/createFixture/index.js";
Expand Down Expand Up @@ -107,10 +107,10 @@ const createMockResponse = ({
const getHistoricEventFromLocalStorage = ClientFunction(
(organizationId, activityId, eventType) => {
const key = `com.adobe.alloy.${organizationId.split("@")[0]}_AdobeOrg.decisioning.events`;
const data = JSON.parse(localStorage.getItem(key));

const event = data[eventType] || {};
return event[activityId];
const data = JSON.parse(localStorage.getItem(key) || "{}");
const events = data[eventType] || {};
const event = events[activityId];
return event || null;
},
);

Expand Down Expand Up @@ -345,3 +345,359 @@ test("Test C1234567: Subscribes content cards", async () => {
await responseStatus(edgeEndpointLogs.requests, [200, 204]);
await t.expect(edgeEndpointLogs.count(() => true)).eql(2);
});

test("Test C1234567: Content card expiration", async () => {
const surface = "web://mywebsite.com/#expired-cards";

const mockPublishedDate = Math.ceil(new Date().getTime() / 1000) - 864000;
const mockExpiryDate = Math.ceil(new Date().getTime() / 1000) - 3600; // 1 hour ago

const activityId = uuid();
const propositionId = uuid();
const itemId = uuid();

const responseBody = createMockResponse({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shammowla Now that Content Card is released in the UI, why are we still mocking the response?

scope: surface,
activityId,
propositionId,
items: [
{
id: itemId,
schema: "https://ns.adobe.com/personalization/ruleset-item",
data: {
version: 1,
rules: [
{
condition: {
definition: {
conditions: [
{
definition: {
key: "~timestampu",
matcher: "le",
values: [mockExpiryDate],
},
type: "matcher",
},
],
logic: "and",
},
type: "group",
},
consequences: [
{
type: "schema",
detail: {
schema:
"https://ns.adobe.com/personalization/message/content-card",
data: {
expiryDate: mockExpiryDate,
publishedDate: mockPublishedDate,
meta: {
surface,
},
content: {
shouldPinToTop: false,
imageUrl:
"https://raw.githubusercontent.com/jasonwaters/assets/master/2024/05/img_20240523_1716483354.png",
actionTitle: "Expired Action",
actionUrl: "https://paypal.com",
body: "This card should not be displayed",
title: "Expired Card",
},
contentType: "application/json",
},
id: itemId,
},
id: itemId,
},
],
},
],
},
},
],
});

await addHtmlToBody(testPageBody, true);

const alloy = createAlloyProxy();
await alloy.configure(config);
await alloy.applyResponse({
renderDecisions: true,
responseBody,
});

let displayEventCalled = false;

await alloy.subscribeRulesetItems({
surfaces: [surface],
schemas: ["https://ns.adobe.com/personalization/message/content-card"],
callback: (result, collectEvent) => {
function createContentCard(proposition, item) {
const { data = {} } = item;
const {
content = {},
meta = {},
publishedDate,
qualifiedDate,
displayedDate,
} = data;

return Object.assign({}, content, {
meta,
qualifiedDate,
displayedDate,
publishedDate,
getProposition: () => proposition,
});
}

function extractContentCards(propositions) {
return propositions
.reduce((allItems, proposition) => {
const { items = [] } = proposition;
return allItems.concat(
items.map((item) => createContentCard(proposition, item)),
);
}, [])
.sort(
(a, b) =>
b.qualifiedDate - a.qualifiedDate ||
b.publishedDate - a.publishedDate,
);
}

const { propositions = [] } = result;
const contentCards = extractContentCards(propositions);

const ul = document.getElementById("content-cards");
let html = "";
contentCards.forEach((contentCard, idx) => {
html += `<li id="content-card-${idx}" data-idx="${idx}"><img src="${contentCard.imageUrl}" alt="Item Image" /><h6>${contentCard.title}</h6><p>${contentCard.body}</p></li>`;
});
ul.innerHTML = html;

if (contentCards.length > 0) {
displayEventCalled = true;
collectEvent("display", propositions);
}
},
});

await alloy.evaluateRulesets({
renderDecisions: true,
personalization: {
decisionContext: {},
},
});

// Assert that no content cards are rendered
await t.expect(Selector("#content-cards").childElementCount).eql(0);

// Verify that no display event was called
await t.expect(displayEventCalled).eql(false);

// Verify that no network requests were made for display events
await t.expect(edgeEndpointLogs.count(() => true)).eql(0);
});

test("Test C1234567: Content card interaction tracking", async () => {
const surface = "web://mywebsite.com/#interaction-tracking";

const mockPublishedDate = Math.ceil(new Date().getTime() / 1000) - 864000;
const mockExpiryDate = Math.ceil(new Date().getTime() / 1000) + 864000;

const activityId = uuid();
const propositionId = uuid();
const itemId = uuid();

const responseBody = createMockResponse({
scope: surface,
activityId,
propositionId,
items: [
{
id: itemId,
schema: "https://ns.adobe.com/personalization/ruleset-item",
data: {
version: 1,
rules: [
{
condition: {
definition: {
conditions: [
{
definition: {
key: "~timestampu",
matcher: "le",
values: [mockExpiryDate],
},
type: "matcher",
},
],
logic: "and",
},
type: "group",
},
consequences: [
{
type: "schema",
detail: {
schema:
"https://ns.adobe.com/personalization/message/content-card",
data: {
expiryDate: mockExpiryDate,
publishedDate: mockPublishedDate,
meta: {
surface,
},
content: {
shouldPinToTop: false,
imageUrl:
"https://raw.githubusercontent.com/jasonwaters/assets/master/2024/05/img_20240523_1716483354.png",
actionTitle: "Expired Action",
actionUrl: "https://paypal.com",
body: "This card should not be displayed",
title: "Expired Card",
},
contentType: "application/json",
},
id: itemId,
},
id: itemId,
},
],
},
],
},
},
],
});

await addHtmlToBody(testPageBody, true);

const alloy = createAlloyProxy();
await alloy.configure(config);
await alloy.applyResponse({
renderDecisions: true,
responseBody,
});

await alloy.subscribeRulesetItems({
surfaces: [surface],
schemas: ["https://ns.adobe.com/personalization/message/content-card"],
callback: (result, collectEvent) => {
function createContentCard(proposition, item) {
const { data = {} } = item;
const {
content = {},
meta = {},
publishedDate,
qualifiedDate,
displayedDate,
} = data;

return Object.assign({}, content, {
meta,
qualifiedDate,
displayedDate,
publishedDate,
getProposition: () => proposition,
});
}

function extractContentCards(propositions) {
return propositions
.reduce((allItems, proposition) => {
const { items = [] } = proposition;
return allItems.concat(
items.map((item) => createContentCard(proposition, item)),
);
}, [])
.sort(
(a, b) =>
b.qualifiedDate - a.qualifiedDate ||
b.publishedDate - a.publishedDate,
);
}

const { propositions = [] } = result;
const contentCards = extractContentCards(propositions);

const ul = document.getElementById("content-cards");
let html = "";
contentCards.forEach((contentCard, idx) => {
html += `<li id="content-card-${idx}" data-idx="${idx}"><img src="${contentCard.imageUrl}" alt="Item Image" /><h6>${contentCard.title}</h6><p>${contentCard.body}</p><a href="${contentCard.actionUrl}" class="action-link">${contentCard.actionTitle}</a></li>`;
});
ul.innerHTML = html;

collectEvent("display", propositions);

ul.addEventListener("click", (evt) => {
const li = evt.target.closest("li");
if (!li) {
return;
}
collectEvent("interact", [
contentCards[li.dataset.idx].getProposition(),
]);
});
},
});

await alloy.evaluateRulesets({
renderDecisions: true,
personalization: {
decisionContext: {},
},
});

// Verify that the content card is rendered
await t.expect(Selector("#content-cards").childElementCount).eql(1);

// Verify display event
const displayEvent = await getHistoricEventFromLocalStorage(
orgId,
activityId,
"display",
);

if (displayEvent !== null) {
await t.expect(displayEvent.count).eql(1);
}

// Simulate user interaction (click on the action link)
await t.click("#content-card-0 .action-link");
await t.wait(REASONABLE_WAIT_TIME);

// Verify interact event
const interactEvent = await getHistoricEventFromLocalStorage(
orgId,
activityId,
"interact",
);

if (interactEvent !== null) {
await t.expect(interactEvent.count).eql(1);
}

// Validate network requests
await responseStatus(edgeEndpointLogs.requests, [200, 204]);
const requestCount = await edgeEndpointLogs.count(() => true);

// Final assertions
await t
.expect(requestCount)
.gte(1, "At least one network request should be made");
if (displayEvent !== null) {
await t
.expect(displayEvent.count)
.eql(1, "Display event should be recorded");
}
if (interactEvent !== null) {
await t
.expect(interactEvent.count)
.eql(1, "Interact event should be recorded");
}
});
Loading
Loading