marketingTouch.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. parseMarketingTouchResponse,
  4. marketingActionSchema,
  5. marketingPopupSchema,
  6. } from "../src/marketingTouch.js";
  7. const text = { format: "plaintext", content: "Hello", style: {} };
  8. // 服务端未约定 Lottie;避免将通用播放器能力误扩展为营销接口字段。
  9. it("rejects uncontracted Lottie deliveries without discarding valid siblings", () => {
  10. const hero = { type: "lottie", lottie: { src: asset } };
  11. expect(marketingPopupSchema.safeParse({ ...popup, hero }).success).toBe(false);
  12. const result = parseMarketingTouchResponse(
  13. envelope([
  14. {
  15. campaign_id: "unsupported",
  16. resource_position: "banner",
  17. priority: 1,
  18. banner: { background: hero, buttons: [] },
  19. },
  20. { campaign_id: "valid", resource_position: "popup", priority: 1, popup },
  21. ]),
  22. );
  23. expect(result.rejectedCount).toBe(1);
  24. expect(result.deliveries.map((delivery) => delivery.campaign_id)).toEqual(["valid"]);
  25. });
  26. const asset = { src: "https://cdn.example.com/banner.png", sha256: "a".repeat(64) };
  27. const button = (type = "close") => ({ text, action: { type, args: {} } });
  28. const popup = { title: text, description: text, buttons: [button()] };
  29. const envelope = (deliveries: unknown[]) => ({
  30. code: 0,
  31. data: { server_time: 1, language: "en-US", deliveries },
  32. });
  33. describe("marketing touch wire contract", () => {
  34. it("accepts video backgrounds through the same VisualBody as popup heroes", () => {
  35. const background = { type: "video", video: { src: asset, fallback: asset } };
  36. const result = parseMarketingTouchResponse(
  37. envelope([
  38. {
  39. campaign_id: "video",
  40. resource_position: "banner",
  41. priority: 1,
  42. banner: { background, buttons: [button()] },
  43. },
  44. ]),
  45. );
  46. expect(result.rejectedCount).toBe(0);
  47. expect(result.deliveries).toHaveLength(1);
  48. });
  49. const bannerDelivery = (format: unknown, content = "") => ({
  50. campaign_id: "banner",
  51. resource_position: "banner",
  52. priority: 1,
  53. banner: {
  54. background: { type: "image", image: { default: asset } },
  55. buttons: [
  56. { text: { format, content }, action: { type: "close" } },
  57. {
  58. text: { format, content },
  59. action: { type: "claim_zcode_plan", args: { plan_id: "plan" } },
  60. },
  61. ],
  62. success_popup: popup,
  63. },
  64. });
  65. it.each(["", "领取"])("accepts empty banner format with content %j", (content) => {
  66. const result = parseMarketingTouchResponse(envelope([bannerDelivery("", content)]));
  67. expect(result.rejectedCount).toBe(0);
  68. const delivery = result.deliveries[0];
  69. expect(delivery?.resource_position).toBe("banner");
  70. if (delivery?.resource_position !== "banner") throw new Error("Missing banner");
  71. expect(delivery.banner.buttons.map((b) => b.text)).toEqual([
  72. { format: "plaintext", content },
  73. { format: "plaintext", content },
  74. ]);
  75. });
  76. it.each([undefined, null, "unknown"])("rejects invalid banner format %j", (format) => {
  77. expect(parseMarketingTouchResponse(envelope([bannerDelivery(format)])).rejectedCount).toBe(1);
  78. });
  79. it("accepts banner bundles with args and fallback, but rejects unsafe entry paths", () => {
  80. const delivery = bannerDelivery("");
  81. const background = {
  82. type: "bundle",
  83. bundle: {
  84. bundle: { ...asset, src: "https://cdn.example.com/banner.zip" },
  85. entry: "index.html",
  86. fallback: asset,
  87. },
  88. args: { heading: "Weekend" },
  89. };
  90. const raw = { ...delivery, banner: { ...delivery.banner, background } };
  91. expect(parseMarketingTouchResponse(envelope([raw])).rejectedCount).toBe(0);
  92. background.bundle.entry = "../index.html";
  93. expect(parseMarketingTouchResponse(envelope([raw])).rejectedCount).toBe(1);
  94. });
  95. it.each(["title", "description", "buttons"])(
  96. "still rejects empty popup %s format in both positions",
  97. (field) => {
  98. const emptyText = { ...text, format: "" };
  99. const invalidPopup = {
  100. ...popup,
  101. [field]: field === "buttons" ? [{ ...button(), text: emptyText }] : emptyText,
  102. };
  103. const delivery = bannerDelivery("");
  104. delivery.banner.success_popup = invalidPopup;
  105. const result = parseMarketingTouchResponse(
  106. envelope([
  107. delivery,
  108. { campaign_id: "popup", resource_position: "popup", priority: 1, popup: invalidPopup },
  109. ]),
  110. );
  111. expect(result.deliveries).toEqual([]);
  112. expect(result.rejectedCount).toBe(2);
  113. },
  114. );
  115. it("rejects the retired card position without dropping a valid popup", () => {
  116. const result = parseMarketingTouchResponse(
  117. envelope([
  118. {
  119. campaign_id: "legacy",
  120. resource_position: "card",
  121. priority: 1,
  122. card: { background: { type: "image", image: { default: asset } }, buttons: [] },
  123. },
  124. { campaign_id: "popup", resource_position: "popup", priority: 1, popup },
  125. ]),
  126. );
  127. expect(result.deliveries.map((delivery) => delivery.campaign_id)).toEqual(["popup"]);
  128. expect(result.rejectedCount).toBe(1);
  129. });
  130. it.each([undefined, null, {}, { color: "red" }])(
  131. "accepts optional text style %j across both delivery positions",
  132. (style) => {
  133. const optionalText = {
  134. format: "plaintext",
  135. content: "Hello",
  136. ...(style === undefined ? {} : { style }),
  137. };
  138. const close = { text: optionalText, action: { type: "close", args: null } };
  139. const content = { title: optionalText, description: optionalText, buttons: [close] };
  140. const result = parseMarketingTouchResponse(
  141. envelope([
  142. {
  143. campaign_id: "banner",
  144. resource_position: "banner",
  145. priority: 1,
  146. banner: {
  147. background: { type: "image", image: { default: asset } },
  148. buttons: [
  149. close,
  150. {
  151. text: optionalText,
  152. action: { type: "claim_zcode_plan", args: { plan_id: "plan" } },
  153. },
  154. ],
  155. success_popup: content,
  156. },
  157. },
  158. { campaign_id: "popup", resource_position: "popup", priority: 1, popup: content },
  159. ]),
  160. );
  161. expect(result.deliveries).toHaveLength(2);
  162. expect(result.rejectedCount).toBe(0);
  163. },
  164. );
  165. it.each(["red", 1, []])("discards obsolete text style %j", (style) => {
  166. const result = parseMarketingTouchResponse(
  167. envelope([
  168. {
  169. campaign_id: "popup",
  170. resource_position: "popup",
  171. priority: 1,
  172. popup: { ...popup, title: { ...text, style } },
  173. },
  174. ]),
  175. );
  176. expect(result.rejectedCount).toBe(0);
  177. expect(JSON.stringify(result.deliveries)).not.toContain('"style"');
  178. });
  179. it.each([
  180. "default",
  181. "outline",
  182. "secondary",
  183. "ghost",
  184. "destructive",
  185. "warning",
  186. "link",
  187. "",
  188. undefined,
  189. ])("accepts popup variant %s", (variant) => {
  190. const result = marketingPopupSchema.parse({
  191. ...popup,
  192. buttons: [{ ...button(), theme: { variant, class: "rounded-lg", style: "color:red" } }],
  193. });
  194. expect(result.buttons[0]?.theme?.variant).toBe(variant || "default");
  195. });
  196. it("validates popup theme while ignoring banner theme", () => {
  197. for (const theme of [
  198. { variant: "invalid" },
  199. { class: "x".repeat(513) },
  200. { style: "x".repeat(513) },
  201. ]) {
  202. expect(
  203. marketingPopupSchema.safeParse({ ...popup, buttons: [{ ...button(), theme }] }).success,
  204. ).toBe(false);
  205. const delivery = bannerDelivery("", "");
  206. Object.assign(delivery.banner.buttons[0]!, { theme });
  207. const parsed = parseMarketingTouchResponse(envelope([delivery]));
  208. expect(parsed.rejectedCount).toBe(0);
  209. expect(JSON.stringify(parsed.deliveries)).not.toContain('"theme"');
  210. }
  211. });
  212. it("accepts existing banner buttons and a popup without a hero", () => {
  213. const result = parseMarketingTouchResponse(
  214. envelope([
  215. {
  216. campaign_id: "1",
  217. resource_position: "banner",
  218. priority: 90,
  219. banner: {
  220. background: { type: "image", image: { default: asset } },
  221. buttons: [
  222. button(),
  223. { text, action: { type: "claim_zcode_plan", args: { plan_id: "plan" } } },
  224. ],
  225. success_popup: popup,
  226. },
  227. },
  228. { campaign_id: "2", resource_position: "popup", priority: 80, popup },
  229. ]),
  230. );
  231. expect(result.deliveries).toHaveLength(2);
  232. expect(result.rejectedCount).toBe(0);
  233. });
  234. it("rejects ambiguous banner actions independently of a valid popup", () => {
  235. const action = { text, action: { type: "open_url", args: { url: "https://example.com" } } };
  236. const result = parseMarketingTouchResponse(
  237. envelope([
  238. {
  239. campaign_id: "1",
  240. resource_position: "banner",
  241. priority: 0,
  242. banner: {
  243. background: { type: "image", image: { default: asset } },
  244. buttons: [action, action],
  245. },
  246. },
  247. { campaign_id: "2", resource_position: "popup", priority: 0, popup },
  248. ]),
  249. );
  250. expect(result.deliveries.map((d) => d.campaign_id)).toEqual(["2"]);
  251. expect(result.rejectedCount).toBe(1);
  252. });
  253. it("rejects unsafe URLs, unknown actions and missing plan identifiers", () => {
  254. for (const action of [
  255. { type: "open_url", args: { url: "javascript:alert(1)" } },
  256. { type: "open_url", args: { url: "https://user:pass@example.com" } },
  257. { type: "open_popup" },
  258. { type: "claim_zcode_plan", args: {} },
  259. ])
  260. expect(marketingActionSchema.safeParse(action).success).toBe(false);
  261. });
  262. it("distinguishes empty success from an invalid envelope", () => {
  263. expect(parseMarketingTouchResponse(envelope([])).deliveries).toEqual([]);
  264. expect(() => parseMarketingTouchResponse({ code: 500, data: {} })).toThrow();
  265. expect(() => parseMarketingTouchResponse({ code: 0 })).toThrow();
  266. });
  267. it("rejects unknown layouts and unsupported visual types", () => {
  268. const result = parseMarketingTouchResponse(
  269. envelope([
  270. {
  271. campaign_id: "1",
  272. resource_position: "popup",
  273. priority: 0,
  274. popup: { ...popup, layout: "v2" },
  275. },
  276. {
  277. campaign_id: "2",
  278. resource_position: "banner",
  279. priority: 0,
  280. banner: {
  281. buttons: [],
  282. background: { type: "lottie", src: asset },
  283. },
  284. },
  285. ]),
  286. );
  287. expect(result.rejectedCount).toBe(2);
  288. });
  289. });