| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401 |
- import { createHash } from "node:crypto";
- import type { IncomingMessage, ServerResponse } from "node:http";
- import { createMarketingTouchAssets } from "./marketing-touch-assets.js";
- import { createMarketingAssetGate } from "./marketing-touch-asset-gate.js";
- import { createTelemetryReportFixture } from "./telemetry-report-fixture.js";
- export async function createMarketingTouchFixture() {
- const { png, zip, assets } = await createMarketingTouchAssets();
- const assetGate = createMarketingAssetGate();
- const hash = (bytes: Buffer) => createHash("sha256").update(bytes).digest("hex");
- const events: unknown[] = [];
- const handleTelemetry = createTelemetryReportFixture();
- let held = true;
- let claimed = false;
- let claimFailure: unknown = null;
- let popupVisible = true;
- let actionDelivery: { campaign_id: string; [key: string]: unknown } | null = null;
- let actionSequence = 0;
- const waiters: Array<() => void> = [];
- const queryWaiters: Array<() => void> = [];
- let queryHeld = false;
- const requests: string[] = [];
- const requestDetails: Array<{
- method: string;
- path: string;
- seq: string | null;
- timestamp: number;
- headers: {
- deviceMid: string | string[] | undefined;
- language: string | string[] | undefined;
- appVersion: string | string[] | undefined;
- authenticated: boolean;
- };
- }> = [];
- let generation = 0;
- let overrideDeliveries: unknown[] | null = null;
- let queryStatus = 200;
- let reportStatus = 200;
- let video = Buffer.alloc(0);
- const text = (content: string) => ({ format: "plaintext", content, style: null });
- const close = { text: text("Close campaign"), action: { type: "close" } };
- return {
- stop() {
- assetGate.reset();
- waiters.splice(0).forEach((done) => done());
- queryWaiters.splice(0).forEach((done) => done());
- },
- async handle(req: IncomingMessage, res: ServerResponse) {
- const url = new URL(req.url ?? "/", "http://localhost");
- const path = url.pathname;
- const json = (value: unknown, status = 200) => {
- res.writeHead(status, { "Content-Type": "application/json" });
- res.end(JSON.stringify(value));
- };
- const assetControl = assetGate.control(path);
- if (await handleTelemetry(req, res)) return true;
- if (assetControl) {
- json(assetControl);
- return true;
- }
- if (path === "/__e2e/marketing/state") {
- json({ events, requests, requestDetails, pendingQueries: queryWaiters.length });
- return true;
- }
- if (path.startsWith("/__e2e/marketing/query-")) {
- if (path.endsWith("/query-hold")) queryHeld = true;
- else if (path.endsWith("/query-release-one")) queryWaiters.shift()?.();
- else if (path.endsWith("/query-release-all")) {
- queryHeld = false;
- queryWaiters.splice(0).forEach((done) => done());
- } else return false;
- json({ ok: true });
- return true;
- }
- if (path === "/__e2e/marketing/assets") {
- json(
- Object.fromEntries(
- Object.entries(assets).map(([key, asset]) => [
- key,
- { src: `http://${req.headers.host}${asset.path}`, sha256: hash(asset.bytes) },
- ]),
- ),
- );
- return true;
- }
- if (path === "/__e2e/marketing/reset") {
- const chunks: Buffer[] = [];
- for await (const chunk of req) chunks.push(Buffer.from(chunk));
- const options = JSON.parse(Buffer.concat(chunks).toString() || "{}");
- // 旧用例挂起的 claim 不能在 reset 后改写下一用例的服务端投放。
- generation++;
- assetGate.reset();
- queryHeld = false;
- queryWaiters.splice(0).forEach((done) => done());
- waiters.splice(0).forEach((done) => done());
- held = options.held === true;
- claimed = options.banner !== true;
- popupVisible = options.popup === true;
- claimFailure = null;
- actionDelivery = null;
- overrideDeliveries = Array.isArray(options.deliveries) ? options.deliveries : null;
- queryStatus = 200;
- reportStatus = 200;
- events.length = 0;
- requests.length = 0;
- requestDetails.length = 0;
- json({ ok: true });
- return true;
- }
- if (path === "/__e2e/marketing/faults") {
- const body: Buffer[] = [];
- for await (const chunk of req) body.push(Buffer.from(chunk));
- const options = JSON.parse(Buffer.concat(body).toString() || "{}");
- queryStatus = options.queryStatus ?? 200;
- reportStatus = options.reportStatus ?? 200;
- json({ ok: true });
- return true;
- }
- if (path === "/__e2e/marketing/claim-failure") {
- const body: Buffer[] = [];
- for await (const chunk of req) body.push(Buffer.from(chunk));
- claimFailure = JSON.parse(Buffer.concat(body).toString());
- claimed = false;
- actionDelivery = null;
- held = false;
- json({ ok: true });
- return true;
- }
- if (path === "/__e2e/marketing/rich-delivery" || path === "/__e2e/marketing/video-banner") {
- const body: Buffer[] = [];
- for await (const chunk of req) body.push(Buffer.from(chunk));
- const data = JSON.parse(Buffer.concat(body).toString());
- const campaignId = `rich-${++actionSequence}`;
- const origin = `http://${req.headers.host}`;
- if (path.endsWith("video-banner")) {
- video = Buffer.from(data.video, "base64");
- actionDelivery = {
- campaign_id: campaignId,
- resource_position: "banner",
- priority: 100,
- banner: {
- background: {
- type: "video",
- video: {
- src: { src: `${origin}/marketing-assets/banner.mp4`, sha256: hash(video) },
- fallback: { src: `${origin}/marketing-assets/banner.png`, sha256: hash(png) },
- },
- },
- buttons: [
- close,
- {
- text: text("Copy video"),
- action: { type: "copy_text", args: { text: "Video copied" } },
- },
- ],
- },
- };
- } else {
- actionDelivery = {
- campaign_id: campaignId,
- resource_position: "popup",
- priority: 100,
- popup: data,
- };
- }
- json({ campaignId });
- return true;
- }
- if (path === "/marketing-assets/banner.mp4") {
- res.writeHead(200, { "Content-Type": "video/mp4" });
- res.end(video);
- return true;
- }
- if (path === "/__e2e/marketing/bundle-banner") {
- const origin = `http://${req.headers.host}`;
- const campaignId = `bundle-banner-${++actionSequence}`;
- actionDelivery = {
- campaign_id: campaignId,
- resource_position: "banner",
- priority: 100,
- banner: {
- background: {
- type: "bundle",
- bundle: {
- bundle: { src: `${origin}/marketing-assets/hero.zip`, sha256: hash(zip) },
- entry: "index.html",
- },
- args: {
- planName: "E2E Bundle Banner",
- amountValue: "321",
- amountUnit: "tokens",
- benefits: ["Banner resource"],
- },
- },
- buttons: [
- { ...close, text: { ...text(""), format: "" } },
- {
- text: text(""),
- action: { type: "copy_text", args: { text: "Bundle banner copied" } },
- },
- ],
- },
- };
- json({ campaignId });
- return true;
- }
- if (
- path === "/__e2e/marketing/action-delivery" ||
- path === "/__e2e/marketing/html-delivery"
- ) {
- const body: Buffer[] = [];
- for await (const chunk of req) body.push(Buffer.from(chunk));
- const action = JSON.parse(Buffer.concat(body).toString());
- const campaignId = `action-test-${++actionSequence}`;
- actionDelivery = {
- campaign_id: campaignId,
- resource_position: "popup",
- priority: 100,
- popup: {
- title: text(`E2E actions ${campaignId}`),
- description: path.endsWith("html-delivery")
- ? { format: "html", content: action }
- : text("Action fixture"),
- buttons: path.endsWith("html-delivery")
- ? [
- {
- ...close,
- theme: {
- variant: "outline",
- class: "rounded-lg font-semibold",
- style: "font-weight:400;color:var(--color-foreground);position:fixed",
- },
- },
- ]
- : [{ text: text("Run action"), action }, close],
- },
- };
- json({ ok: true, campaignId });
- return true;
- }
- if (path === "/__e2e/marketing/release") {
- held = false;
- waiters.splice(0).forEach((done) => done());
- json({ ok: true });
- return true;
- }
- if (path === "/__e2e/marketing/redeliver-banner") {
- claimed = false;
- json({ ok: true });
- return true;
- }
- if (path === "/__e2e/marketing/redeliver-popup") {
- popupVisible = true;
- json({ ok: true });
- return true;
- }
- if (
- path.startsWith("/api/v1/marketing/") ||
- path.startsWith("/marketing-assets/") ||
- path.startsWith("/api/v1/zcode-plan/billing/")
- ) {
- requests.push(path);
- requestDetails.push({
- method: req.method ?? "GET",
- path,
- seq: url.searchParams.get("seq"),
- timestamp: Date.now(),
- headers: {
- deviceMid: req.headers["x-device-mid"],
- language: req.headers["x-client-language"],
- appVersion: req.headers["x-zcode-app-version"],
- authenticated: Boolean(req.headers.authorization),
- },
- });
- }
- const staticAsset = Object.values(assets).find((asset) => asset.path === path);
- if (staticAsset) {
- await assetGate.wait();
- res.writeHead(200, { "Content-Type": staticAsset.type });
- res.end(staticAsset.bytes);
- return true;
- }
- if (path === "/api/v1/marketing/touch/action") {
- const body: Buffer[] = [];
- for await (const chunk of req) body.push(Buffer.from(chunk));
- const event = JSON.parse(Buffer.concat(body).toString());
- events.push(event);
- if (reportStatus !== 200) {
- json({ code: reportStatus, data: { message: "E2E report fault" } }, reportStatus);
- return true;
- }
- if (event.campaign_id === actionDelivery?.campaign_id) actionDelivery = null;
- // 是否继续投放由服务端 fixture 决定;重新下发保留原 campaign_id。
- if (event.campaign_id === "independent") popupVisible = false;
- if (event.campaign_id === "banner-1") claimed = true;
- json({ code: 0 });
- return true;
- }
- if (path === "/api/v1/marketing/touch") {
- if (queryStatus !== 200) {
- json({ code: queryStatus, data: { message: "E2E query fault" } }, queryStatus);
- return true;
- }
- const origin = `http://${req.headers.host}`;
- const hero = {
- type: "bundle",
- bundle: {
- bundle: { src: `${origin}/marketing-assets/hero.zip`, sha256: hash(zip) },
- entry: "index.html",
- },
- args: {
- planName: "E2E Weekend",
- amountValue: "100",
- amountUnit: "tokens",
- benefits: ["Fixture benefit"],
- },
- };
- const result = {
- layout: "v1",
- title: text("E2E claim succeeded"),
- description: { format: "html", content: "<p><b>E2E result</b></p>" },
- hero,
- buttons: [close],
- };
- const snapshot = {
- code: 0,
- data: {
- server_time: Math.floor(Date.now() / 1000),
- language: req.headers["x-client-language"] === "zh-CN" ? "zh-CN" : "en-US",
- deliveries:
- overrideDeliveries ??
- (actionDelivery
- ? [actionDelivery]
- : [
- ...(!claimed
- ? [
- {
- campaign_id: "banner-1",
- resource_position: "banner",
- priority: 90,
- banner: {
- background: {
- type: "image",
- image: {
- default: {
- src: `${origin}/marketing-assets/banner.png`,
- sha256: hash(png),
- },
- },
- },
- buttons: [
- { ...close, text: { ...text(""), format: "" } },
- {
- text: { ...text(""), format: "" },
- action: {
- type: "claim_zcode_plan",
- args: { plan_id: "weekend-plan-e2e" },
- },
- },
- ],
- success_popup: result,
- },
- },
- ]
- : []),
- ...(popupVisible
- ? [
- {
- campaign_id: "independent",
- resource_position: "popup",
- priority: 100,
- popup: { ...result, title: text("E2E independent popup"), hero: null },
- },
- ]
- : []),
- ]),
- },
- };
- // 在操作前冻结响应;释放时不重新计算投放,才能复现真实迟到快照。
- if (queryHeld) await new Promise<void>((done) => queryWaiters.push(done));
- json(snapshot);
- return true;
- }
- if (path === "/api/v1/zcode-plan/billing/claim") {
- const requestGeneration = generation;
- if (held) await new Promise<void>((done) => waiters.push(done));
- if (requestGeneration !== generation) {
- json({ code: 409, data: { message: "Fixture case reset" } });
- return true;
- }
- claimed = true;
- if (claimFailure) {
- json(claimFailure);
- claimFailure = null;
- return true;
- }
- }
- return false;
- },
- };
- }
|