CtrlK
BlogDocsLog inGet started
Tessl Logo
PluginPublic

adobe/aem-eds-personalization

Section-based personalization for AEM Edge Delivery Services (DA) pages: Section Metadata rows let rules (geo, device, visitor, URL/UTM param, page state, custom audiences), a decision engine (v1 API, OF1), an A/B split or Adobe Target (inline or popup) pick the EDS fragment a section shows, with the authored content as default. Consent first, measurement events per decision, a client runtime plus an optional Cloudflare edge worker, a simulator and Playwright checks.

0.83x
Quality

88%

Does it follow best practices?

Impact

83%

0.83x

1 of 3 eval scenarios. Add 2 more for a full score.

SecuritybySnyk

Low

Low-risk findings worth noting

Overview
Quality
Evals
Security
Files

helpers.mjsskills/personalize/scripts/test/

/*
 * Shared test helpers: asset imports, fixtures, temp repos and the --help
 * contract every CLI answers.
 */

import { spawnSync } from 'node:child_process';
import {
  cpSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, writeFileSync,
} from 'node:fs';
import { tmpdir } from 'node:os';
import { dirname, join } from 'node:path';
import { fileURLToPath, pathToFileURL } from 'node:url';
import { EDGE_DIR, RUNTIME_DIR, SHARED_WITH_EDGE } from '../lib.mjs';

export const FIXTURES = join(dirname(fileURLToPath(import.meta.url)), 'fixtures');
export const SITE = join(FIXTURES, 'site');

export const rules = await import(pathToFileURL(join(RUNTIME_DIR, 'scripts', 'personalization', 'rules.js')).href);
export const contract = await import(pathToFileURL(join(RUNTIME_DIR, 'scripts', 'personalization', 'contract.js')).href);
export const context = await import(pathToFileURL(join(RUNTIME_DIR, 'scripts', 'personalization', 'context.js')).href);
export const html = await import(pathToFileURL(join(EDGE_DIR, 'src', 'personalization', 'html.js')).href);

/**
 * @param {string} name file under fixtures/site
 * @returns {string}
 */
export function fixture(name) {
  return readFileSync(join(SITE, name), 'utf8');
}

/**
 * Creates a temp dir and returns it with a cleanup function.
 * @param {string} prefix
 * @returns {{dir: string, cleanup: () => void}}
 */
export function tempDir(prefix = 'pzn-test-') {
  const dir = mkdtempSync(join(tmpdir(), prefix));
  return { dir, cleanup: () => rmSync(dir, { recursive: true, force: true }) };
}

/**
 * Assembles the edge worker sources (as install-edge does) in a temp dir so
 * the worker's relative imports resolve.
 * @returns {{dir: string, cleanup: () => void, load: (file: string) => Promise<object>}}
 */
export function edgeBundle() {
  const { dir, cleanup } = tempDir('pzn-edge-test-');
  cpSync(join(EDGE_DIR, 'src'), dir, { recursive: true });
  SHARED_WITH_EDGE.forEach((name) => cpSync(
    join(RUNTIME_DIR, 'scripts', 'personalization', name),
    join(dir, 'personalization', name),
  ));
  return { dir, cleanup, load: (file) => import(pathToFileURL(join(dir, file)).href) };
}

const { parseSection } = await import(pathToFileURL(join(RUNTIME_DIR, 'scripts', 'personalization', 'section-spec.js')).href);

/**
 * A section spec from Section Metadata rows, as the pipeline renders them.
 * @param {object} rows row name after "Personalization " ('' for the id row,
 *   e.g. 'rule-india') -> value
 * @returns {object} parseSection spec
 */
export function sectionSpec(rows) {
  return parseSection(Object.entries(rows)
    .map(([key, value]) => [key ? `data-personalization-${key}` : 'data-personalization', value]));
}

/**
 * The engine request placeholder of a section (contract.js).
 * @param {object} rows as sectionSpec
 * @returns {object}
 */
export function contractSpec(rows) {
  return contract.placeholder(sectionSpec(rows));
}

// loadEager as shipped in adobe/aem-boilerplate.
export const BOILERPLATE_SCRIPTS = `import { decorateMain as d } from './aem.js';

/**
 * Loads everything needed to get to LCP.
 */
async function loadEager(doc) {
  document.documentElement.lang = 'en';
  decorateTemplateAndTheme();
  const main = doc.querySelector('main');
  if (main) {
    decorateMain(main);
    document.body.classList.add('appear');
    await loadSection(main.querySelector('.section'), waitForFirstImage);
  }
}

async function loadLazy(doc) {
  decorateMain(main);
}
`;

export const FRAGMENT_JS = 'export async function loadFragment(path) { return path; }\n';

/**
 * A boilerplate-shaped EDS repo (scripts.js + fragment block) in a temp dir.
 * @returns {{dir: string, cleanup: () => void}}
 */
export function siteRepo() {
  const temp = tempDir('pzn-repo-');
  mkdirSync(join(temp.dir, 'scripts'), { recursive: true });
  mkdirSync(join(temp.dir, 'blocks', 'fragment'), { recursive: true });
  writeFileSync(join(temp.dir, 'scripts', 'scripts.js'), BOILERPLATE_SCRIPTS);
  writeFileSync(join(temp.dir, 'blocks', 'fragment', 'fragment.js'), FRAGMENT_JS);
  return temp;
}

/**
 * Runs a CLI with --help in an empty temp cwd: exit 0, usage on stdout and
 * nothing written (the --help contract every script keeps).
 * @param {string} script file name under scripts/
 * @returns {{status: number|null, stdout: string, wrote: string[]}}
 */
export function runHelp(script) {
  const { dir, cleanup } = tempDir('pzn-help-');
  try {
    const result = spawnSync(process.execPath, [join(dirname(fileURLToPath(import.meta.url)), '..', script), '--help'], { cwd: dir, encoding: 'utf8' });
    return { status: result.status, stdout: result.stdout, wrote: readdirSync(dir) };
  } finally {
    cleanup();
  }
}

skills

personalize

scripts

test

conformance.test.mjs

context.test.mjs

contract.test.mjs

detect-project.test.mjs

edge.test.mjs

helpers.mjs

html.test.mjs

install-edge.test.mjs

install-runtime.test.mjs

lib.test.mjs

mock-decision-api.test.mjs

outcomes.test.mjs

pilot.test.mjs

provider-api.test.mjs

rules.test.mjs

runtime-style.test.mjs

section-spec.test.mjs

sections.test.mjs

simulate.test.mjs

sources.test.mjs

validate-placeholders.test.mjs

verify-preview.test.mjs

detect-project.mjs

install-edge.mjs

install-runtime.mjs

lib.mjs

mock-decision-api.mjs

schema.mjs

simulate.mjs

validate-placeholders.mjs

verify-preview.mjs

SKILL.md

README.md

tile.json