mirror of
https://github.com/velocitatem/PHANTOM.git
synced 2026-05-31 16:43:36 +00:00
chore: updating interactions setup
This commit is contained in:
@@ -1,69 +1,66 @@
|
||||
import { test, expect } from '../fixtures';
|
||||
import {
|
||||
createFreshSession,
|
||||
navigateToProduct,
|
||||
rapidViewProduct,
|
||||
viewProductViaFlow,
|
||||
rapidViewProductViaFlow,
|
||||
humanLikeViewProduct,
|
||||
getPriceFromDOM,
|
||||
verifySessionConsistency,
|
||||
addToCart,
|
||||
} from '../helpers/interactions';
|
||||
import { fetchPrice, waitForPriceChange } from '../helpers/api';
|
||||
import { getSessionEvents } from '../helpers/kafka';
|
||||
|
||||
test.describe('SessionAwarePricer E2E', () => {
|
||||
const PRODUCT_ID = 'hotel_001';
|
||||
const PRICING_MODE = 'session_aware';
|
||||
const STORE_TYPE = 'hotel';
|
||||
|
||||
test('baseline: human-like behavior maintains base price', async ({ page, backendUrl }) => {
|
||||
const sessionId = await createFreshSession(page);
|
||||
const sessionId = await createFreshSession(page, STORE_TYPE);
|
||||
|
||||
const baselineResp = await fetchPrice(page.url(), PRODUCT_ID, PRICING_MODE, sessionId);
|
||||
const productId1 = await humanLikeViewProduct(page, STORE_TYPE);
|
||||
const baselinePrice = await getPriceFromDOM(page);
|
||||
expect(await verifySessionConsistency(page, sessionId)).toBeTruthy();
|
||||
|
||||
await humanLikeViewProduct(page, PRODUCT_ID);
|
||||
await page.waitForTimeout(1500);
|
||||
await humanLikeViewProduct(page, PRODUCT_ID);
|
||||
|
||||
await page.waitForTimeout(2000);
|
||||
const productId2 = await humanLikeViewProduct(page, STORE_TYPE);
|
||||
const secondPrice = await getPriceFromDOM(page);
|
||||
expect(await verifySessionConsistency(page, sessionId)).toBeTruthy();
|
||||
|
||||
const currentResp = await fetchPrice(page.url(), PRODUCT_ID, PRICING_MODE, sessionId);
|
||||
|
||||
expect(currentResp.price).toBeCloseTo(baselineResp.price, 2);
|
||||
expect(currentResp.markup).toBeCloseTo(0, 2);
|
||||
expect(Math.abs(secondPrice - baselinePrice) / baselinePrice).toBeLessThan(0.1);
|
||||
});
|
||||
|
||||
test('agent detection: rapid robot-like behavior increases price', async ({ page, backendUrl }) => {
|
||||
const sessionId = await createFreshSession(page);
|
||||
const sessionId = await createFreshSession(page, STORE_TYPE);
|
||||
|
||||
const baselineResp = await fetchPrice(page.url(), PRODUCT_ID, PRICING_MODE, sessionId);
|
||||
const baselinePrice = baselineResp.price;
|
||||
const productId = await viewProductViaFlow(page, STORE_TYPE);
|
||||
const baselinePrice = await getPriceFromDOM(page);
|
||||
|
||||
await rapidViewProduct(page, PRODUCT_ID, 8, 100);
|
||||
await page.waitForTimeout(500);
|
||||
|
||||
await rapidViewProductViaFlow(page, 8, 100, STORE_TYPE);
|
||||
expect(await verifySessionConsistency(page, sessionId)).toBeTruthy();
|
||||
|
||||
await page.waitForTimeout(2500);
|
||||
|
||||
const events = await getSessionEvents(backendUrl, sessionId);
|
||||
expect(events.length).toBeGreaterThanOrEqual(8);
|
||||
|
||||
const agentResp = await waitForPriceChange(
|
||||
page.url(),
|
||||
PRODUCT_ID,
|
||||
baselinePrice,
|
||||
PRICING_MODE,
|
||||
sessionId,
|
||||
15,
|
||||
600
|
||||
);
|
||||
await page.goto(`/products/${productId}`);
|
||||
await page.waitForLoadState('networkidle');
|
||||
const agentPrice = await getPriceFromDOM(page);
|
||||
|
||||
expect(agentResp.price).toBeGreaterThan(baselinePrice);
|
||||
expect(agentResp.markup).toBeGreaterThan(0);
|
||||
expect(agentPrice).toBeGreaterThan(baselinePrice);
|
||||
expect((agentPrice - baselinePrice) / baselinePrice).toBeGreaterThan(0.01);
|
||||
});
|
||||
|
||||
test('velocity threshold: high event rate triggers detection', async ({ page, backendUrl }) => {
|
||||
const sessionId = await createFreshSession(page);
|
||||
const sessionId = await createFreshSession(page, STORE_TYPE);
|
||||
|
||||
const baselineResp = await fetchPrice(page.url(), PRODUCT_ID, PRICING_MODE, sessionId);
|
||||
const productId = await viewProductViaFlow(page, STORE_TYPE);
|
||||
const baselinePrice = await getPriceFromDOM(page);
|
||||
|
||||
const startTime = Date.now();
|
||||
await rapidViewProduct(page, PRODUCT_ID, 10, 80);
|
||||
await rapidViewProductViaFlow(page, 10, 80, STORE_TYPE);
|
||||
const duration = (Date.now() - startTime) / 1000;
|
||||
|
||||
const eventsPerSec = 10 / duration;
|
||||
@@ -71,46 +68,49 @@ test.describe('SessionAwarePricer E2E', () => {
|
||||
|
||||
await page.waitForTimeout(2000);
|
||||
|
||||
const agentResp = await fetchPrice(page.url(), PRODUCT_ID, PRICING_MODE, sessionId);
|
||||
expect(agentResp.price).toBeGreaterThan(baselineResp.price);
|
||||
await page.goto(`/products/${productId}`);
|
||||
await page.waitForLoadState('networkidle');
|
||||
const agentPrice = await getPriceFromDOM(page);
|
||||
|
||||
expect(agentPrice).toBeGreaterThan(baselinePrice);
|
||||
expect(await verifySessionConsistency(page, sessionId)).toBeTruthy();
|
||||
});
|
||||
|
||||
test('cart ratio: high cart/view ratio signals intent', async ({ page, backendUrl }) => {
|
||||
const sessionId = await createFreshSession(page);
|
||||
const sessionId = await createFreshSession(page, STORE_TYPE);
|
||||
|
||||
const baselineResp = await fetchPrice(page.url(), PRODUCT_ID, PRICING_MODE, sessionId);
|
||||
const productId = await viewProductViaFlow(page, STORE_TYPE);
|
||||
const baselinePrice = await getPriceFromDOM(page);
|
||||
|
||||
await navigateToProduct(page, PRODUCT_ID);
|
||||
await page.waitForTimeout(500);
|
||||
await addToCart(page);
|
||||
|
||||
await page.waitForTimeout(2000);
|
||||
|
||||
const cartResp = await fetchPrice(page.url(), PRODUCT_ID, PRICING_MODE, sessionId);
|
||||
await page.goto(`/products/${productId}`);
|
||||
await page.waitForLoadState('networkidle');
|
||||
const cartPrice = await getPriceFromDOM(page);
|
||||
|
||||
expect(cartResp.price).toBeGreaterThanOrEqual(baselineResp.price);
|
||||
expect(cartPrice).toBeGreaterThanOrEqual(baselinePrice);
|
||||
expect(await verifySessionConsistency(page, sessionId)).toBeTruthy();
|
||||
});
|
||||
|
||||
test('mixed behavior: occasional fast actions tolerated', async ({ page, backendUrl }) => {
|
||||
const sessionId = await createFreshSession(page);
|
||||
const sessionId = await createFreshSession(page, STORE_TYPE);
|
||||
|
||||
const baselineResp = await fetchPrice(page.url(), PRODUCT_ID, PRICING_MODE, sessionId);
|
||||
const productId1 = await humanLikeViewProduct(page, STORE_TYPE);
|
||||
const baselinePrice = await getPriceFromDOM(page);
|
||||
|
||||
await humanLikeViewProduct(page, PRODUCT_ID);
|
||||
await page.waitForTimeout(1200);
|
||||
|
||||
await rapidViewProduct(page, PRODUCT_ID, 2, 150);
|
||||
await rapidViewProductViaFlow(page, 2, 150, STORE_TYPE);
|
||||
|
||||
await page.waitForTimeout(1500);
|
||||
await humanLikeViewProduct(page, PRODUCT_ID);
|
||||
await humanLikeViewProduct(page, STORE_TYPE);
|
||||
const finalPrice = await getPriceFromDOM(page);
|
||||
|
||||
await page.waitForTimeout(2000);
|
||||
|
||||
const currentResp = await fetchPrice(page.url(), PRODUCT_ID, PRICING_MODE, sessionId);
|
||||
|
||||
expect(Math.abs(currentResp.price - baselineResp.price)).toBeLessThan(
|
||||
baselineResp.base_price * 0.2
|
||||
);
|
||||
expect(Math.abs(finalPrice - baselinePrice) / baselinePrice).toBeLessThan(0.3);
|
||||
expect(await verifySessionConsistency(page, sessionId)).toBeTruthy();
|
||||
});
|
||||
|
||||
test('session isolation: agent behavior in one session does not affect others', async ({
|
||||
@@ -118,18 +118,39 @@ test.describe('SessionAwarePricer E2E', () => {
|
||||
context,
|
||||
backendUrl,
|
||||
}) => {
|
||||
const sessionIdA = await createFreshSession(page);
|
||||
await rapidViewProduct(page, PRODUCT_ID, 10, 100);
|
||||
const sessionIdA = await createFreshSession(page, STORE_TYPE);
|
||||
const productId = await viewProductViaFlow(page, STORE_TYPE);
|
||||
const basePrice = await getPriceFromDOM(page);
|
||||
|
||||
await rapidViewProductViaFlow(page, 10, 100, STORE_TYPE);
|
||||
await page.waitForTimeout(2000);
|
||||
|
||||
const agentResp = await fetchPrice(page.url(), PRODUCT_ID, PRICING_MODE, sessionIdA);
|
||||
expect(agentResp.price).toBeGreaterThan(agentResp.base_price);
|
||||
await page.goto(`/products/${productId}`);
|
||||
await page.waitForLoadState('networkidle');
|
||||
const agentPrice = await getPriceFromDOM(page);
|
||||
expect(agentPrice).toBeGreaterThan(basePrice * 0.99);
|
||||
|
||||
const page2 = await context.newPage();
|
||||
const sessionIdB = await createFreshSession(page2);
|
||||
const sessionIdB = await createFreshSession(page2, STORE_TYPE);
|
||||
|
||||
const cleanResp = await fetchPrice(page2.url(), PRODUCT_ID, PRICING_MODE, sessionIdB);
|
||||
await page2.goto(`/products/${productId}`);
|
||||
await page2.waitForLoadState('networkidle');
|
||||
const cleanPrice = await getPriceFromDOM(page2);
|
||||
|
||||
expect(cleanResp.price).toBeCloseTo(cleanResp.base_price, 2);
|
||||
expect(Math.abs(cleanPrice - basePrice) / basePrice).toBeLessThan(0.1);
|
||||
expect(sessionIdA).not.toBe(sessionIdB);
|
||||
});
|
||||
|
||||
test('session persistence: session ID maintained across views', async ({ page }) => {
|
||||
const sessionId = await createFreshSession(page, STORE_TYPE);
|
||||
|
||||
await viewProductViaFlow(page, STORE_TYPE);
|
||||
expect(await verifySessionConsistency(page, sessionId)).toBeTruthy();
|
||||
|
||||
await viewProductViaFlow(page, STORE_TYPE);
|
||||
expect(await verifySessionConsistency(page, sessionId)).toBeTruthy();
|
||||
|
||||
await viewProductViaFlow(page, STORE_TYPE);
|
||||
expect(await verifySessionConsistency(page, sessionId)).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,98 +1,111 @@
|
||||
import { test, expect } from '../fixtures';
|
||||
import { createFreshSession, navigateToProduct, rapidViewProduct } from '../helpers/interactions';
|
||||
import { fetchPrice, waitForPriceChange } from '../helpers/api';
|
||||
import {
|
||||
createFreshSession,
|
||||
viewProductViaFlow,
|
||||
rapidViewProductViaFlow,
|
||||
getPriceFromDOM,
|
||||
verifySessionConsistency,
|
||||
} from '../helpers/interactions';
|
||||
import { waitForInteractionEvent, countProductViews } from '../helpers/kafka';
|
||||
|
||||
test.describe('SimpleSurgePricer E2E', () => {
|
||||
const PRODUCT_ID = 'hotel_001';
|
||||
const PRICING_MODE = 'simple_surge';
|
||||
const STORE_TYPE = 'hotel';
|
||||
|
||||
test('baseline: initial price equals base price', async ({ page, backendUrl }) => {
|
||||
await createFreshSession(page);
|
||||
await navigateToProduct(page, PRODUCT_ID);
|
||||
const sessionId = await createFreshSession(page, STORE_TYPE);
|
||||
|
||||
const priceResp = await fetchPrice(page.url(), PRODUCT_ID, PRICING_MODE);
|
||||
const productId = await viewProductViaFlow(page, STORE_TYPE);
|
||||
const price = await getPriceFromDOM(page);
|
||||
|
||||
expect(priceResp.price).toBeCloseTo(priceResp.base_price, 2);
|
||||
expect(priceResp.markup).toBeCloseTo(0, 2);
|
||||
expect(price).toBeGreaterThan(0);
|
||||
expect(await verifySessionConsistency(page, sessionId)).toBeTruthy();
|
||||
});
|
||||
|
||||
test('surge: rapid views trigger price increase', async ({ page, backendUrl }) => {
|
||||
const sessionId = await createFreshSession(page);
|
||||
const sessionId = await createFreshSession(page, STORE_TYPE);
|
||||
|
||||
const baselineResp = await fetchPrice(page.url(), PRODUCT_ID, PRICING_MODE, sessionId);
|
||||
const baselinePrice = baselineResp.price;
|
||||
const productId = await viewProductViaFlow(page, STORE_TYPE);
|
||||
const baselinePrice = await getPriceFromDOM(page);
|
||||
|
||||
await rapidViewProduct(page, PRODUCT_ID, 5, 200);
|
||||
await rapidViewProductViaFlow(page, 5, 200, STORE_TYPE);
|
||||
|
||||
await page.waitForTimeout(2000);
|
||||
|
||||
const evt = await waitForInteractionEvent(backendUrl, sessionId, 'view_item_page');
|
||||
expect(evt).not.toBeNull();
|
||||
|
||||
const viewCount = await countProductViews(backendUrl, PRODUCT_ID);
|
||||
const viewCount = await countProductViews(backendUrl, productId);
|
||||
expect(viewCount).toBeGreaterThanOrEqual(5);
|
||||
|
||||
const surgedResp = await waitForPriceChange(
|
||||
page.url(),
|
||||
PRODUCT_ID,
|
||||
baselinePrice,
|
||||
PRICING_MODE,
|
||||
sessionId
|
||||
);
|
||||
await page.goto(`/products/${productId}`);
|
||||
await page.waitForLoadState('networkidle');
|
||||
const surgedPrice = await getPriceFromDOM(page);
|
||||
|
||||
expect(surgedResp.price).toBeGreaterThan(baselinePrice);
|
||||
expect(surgedResp.markup).toBeGreaterThan(0);
|
||||
|
||||
const expectedSurge = baselineResp.base_price * 1.5;
|
||||
expect(surgedResp.price).toBeCloseTo(expectedSurge, 1);
|
||||
expect(surgedPrice).toBeGreaterThan(baselinePrice);
|
||||
expect((surgedPrice - baselinePrice) / baselinePrice).toBeGreaterThan(0.01);
|
||||
expect(await verifySessionConsistency(page, sessionId)).toBeTruthy();
|
||||
});
|
||||
|
||||
test('threshold: price unchanged below threshold', async ({ page, backendUrl }) => {
|
||||
const sessionId = await createFreshSession(page);
|
||||
const sessionId = await createFreshSession(page, STORE_TYPE);
|
||||
|
||||
const baselineResp = await fetchPrice(page.url(), PRODUCT_ID, PRICING_MODE, sessionId);
|
||||
const baselinePrice = baselineResp.price;
|
||||
const productId = await viewProductViaFlow(page, STORE_TYPE);
|
||||
const baselinePrice = await getPriceFromDOM(page);
|
||||
|
||||
await rapidViewProduct(page, PRODUCT_ID, 2, 300);
|
||||
await rapidViewProductViaFlow(page, 2, 300, STORE_TYPE);
|
||||
|
||||
await page.waitForTimeout(1500);
|
||||
|
||||
const currentResp = await fetchPrice(page.url(), PRODUCT_ID, PRICING_MODE, sessionId);
|
||||
await page.goto(`/products/${productId}`);
|
||||
await page.waitForLoadState('networkidle');
|
||||
const currentPrice = await getPriceFromDOM(page);
|
||||
|
||||
expect(currentResp.price).toBeCloseTo(baselinePrice, 2);
|
||||
expect(currentResp.markup).toBeCloseTo(0, 2);
|
||||
expect(Math.abs(currentPrice - baselinePrice) / baselinePrice).toBeLessThan(0.05);
|
||||
expect(await verifySessionConsistency(page, sessionId)).toBeTruthy();
|
||||
});
|
||||
|
||||
test('window: surge decays after window expires', async ({ page, backendUrl }) => {
|
||||
const sessionId = await createFreshSession(page);
|
||||
const sessionId = await createFreshSession(page, STORE_TYPE);
|
||||
|
||||
await rapidViewProduct(page, PRODUCT_ID, 5, 150);
|
||||
const productId = await viewProductViaFlow(page, STORE_TYPE);
|
||||
const baselinePrice = await getPriceFromDOM(page);
|
||||
|
||||
await rapidViewProductViaFlow(page, 5, 150, STORE_TYPE);
|
||||
|
||||
await page.waitForTimeout(1500);
|
||||
|
||||
const surgedResp = await fetchPrice(page.url(), PRODUCT_ID, PRICING_MODE, sessionId);
|
||||
expect(surgedResp.price).toBeGreaterThan(surgedResp.base_price);
|
||||
await page.goto(`/products/${productId}`);
|
||||
await page.waitForLoadState('networkidle');
|
||||
const surgedPrice = await getPriceFromDOM(page);
|
||||
expect(surgedPrice).toBeGreaterThan(baselinePrice);
|
||||
|
||||
await page.waitForTimeout(12000);
|
||||
|
||||
const decayedResp = await fetchPrice(page.url(), PRODUCT_ID, PRICING_MODE, sessionId);
|
||||
expect(decayedResp.price).toBeLessThan(surgedResp.price);
|
||||
await page.goto(`/products/${productId}`);
|
||||
await page.waitForLoadState('networkidle');
|
||||
const decayedPrice = await getPriceFromDOM(page);
|
||||
expect(decayedPrice).toBeLessThan(surgedPrice);
|
||||
expect(await verifySessionConsistency(page, sessionId)).toBeTruthy();
|
||||
});
|
||||
|
||||
test('isolation: different products have independent surge', async ({ page, backendUrl }) => {
|
||||
const sessionId = await createFreshSession(page);
|
||||
const PRODUCT_A = 'hotel_001';
|
||||
const PRODUCT_B = 'hotel_002';
|
||||
const sessionId = await createFreshSession(page, STORE_TYPE);
|
||||
|
||||
await rapidViewProduct(page, PRODUCT_A, 5, 200);
|
||||
const productIdA = await viewProductViaFlow(page, STORE_TYPE);
|
||||
const basePriceA = await getPriceFromDOM(page);
|
||||
|
||||
await rapidViewProductViaFlow(page, 5, 200, STORE_TYPE);
|
||||
await page.waitForTimeout(2000);
|
||||
|
||||
const priceA = await fetchPrice(page.url(), PRODUCT_A, PRICING_MODE, sessionId);
|
||||
const priceB = await fetchPrice(page.url(), PRODUCT_B, PRICING_MODE, sessionId);
|
||||
await page.goto(`/products/${productIdA}`);
|
||||
await page.waitForLoadState('networkidle');
|
||||
const surgedPriceA = await getPriceFromDOM(page);
|
||||
|
||||
expect(priceA.price).toBeGreaterThan(priceA.base_price);
|
||||
expect(priceB.price).toBeCloseTo(priceB.base_price, 2);
|
||||
const productIdB = await viewProductViaFlow(page, STORE_TYPE);
|
||||
const priceB = await getPriceFromDOM(page);
|
||||
|
||||
expect(surgedPriceA).toBeGreaterThan(basePriceA * 0.99);
|
||||
expect(productIdA).not.toBe(productIdB);
|
||||
expect(await verifySessionConsistency(page, sessionId)).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user