marketingPopupAdapter.test.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // @vitest-environment jsdom
  2. import { describe, expect, it } from "vitest";
  3. import {
  4. adaptMarketingPopup,
  5. marketingLabel,
  6. } from "@/components/marketing-touch/marketingPopupAdapter.js";
  7. describe("marketing popup adapter", () => {
  8. it.each(["zh-CN", "en-US"] as const)("%s Start 设置入口明确叫查看套餐", (locale) => {
  9. const text = { format: "plaintext" as const, content: "立即使用" };
  10. const popup = adaptMarketingPopup(
  11. "start",
  12. locale,
  13. {
  14. title: text,
  15. description: text,
  16. buttons: [
  17. {
  18. text,
  19. action: {
  20. type: "navigate",
  21. args: {
  22. page: "settings",
  23. section: "models",
  24. provider_id: "account:bigmodel-start-plan",
  25. },
  26. },
  27. },
  28. ],
  29. },
  30. null,
  31. locale === "zh-CN" ? "查看套餐" : "View plan",
  32. );
  33. expect(popup.dialog.buttons[0]?.label).toBe(locale === "zh-CN" ? "查看套餐" : "View plan");
  34. expect(popup.dialog.buttons[0]?.formattedLabel?.text).toBe(popup.dialog.buttons[0]?.label);
  35. });
  36. it("keeps copy text literal and correlates navigation through stable button ids", () => {
  37. const text = { format: "plaintext" as const, content: "Open" };
  38. const payload = adaptMarketingPopup(
  39. "action",
  40. "en-US",
  41. {
  42. title: text,
  43. description: text,
  44. buttons: [
  45. {
  46. text,
  47. action: {
  48. type: "navigate",
  49. args: { page: "settings", section: "models", provider_id: "bigmodel" },
  50. },
  51. },
  52. {
  53. text,
  54. action: {
  55. type: "navigate",
  56. args: { page: "plugin_marketplace", plugin_id: "plugin@market" },
  57. },
  58. },
  59. { text, action: { type: "copy_text", args: { text: "<b>literal</b>\n next " } } },
  60. ],
  61. },
  62. null,
  63. );
  64. expect(payload.actions).toEqual({
  65. "button-0": { type: "navigate", destination: "settings" },
  66. "button-1": { type: "navigate", destination: "plugin_store" },
  67. "button-2": { type: "copy_text", text: "<b>literal</b>\n next " },
  68. });
  69. expect(payload.dialog.buttons.map((button) => button.actionId)).toEqual([
  70. "button-0",
  71. "button-1",
  72. "button-2",
  73. ]);
  74. });
  75. it("maps existing inline actions and strips markup from button labels", () => {
  76. const payload = adaptMarketingPopup(
  77. "193001",
  78. "en-US",
  79. {
  80. title: { format: "plaintext", content: "News" },
  81. description: { format: "html", content: "<p>Hello</p>" },
  82. buttons: [
  83. {
  84. text: { format: "html", content: "<b>Claim</b><script>bad()</script>" },
  85. action: { type: "claim_zcode_plan", args: { plan_id: "plan" } },
  86. },
  87. ],
  88. },
  89. null,
  90. );
  91. expect(payload.dialog.hero).toBeNull();
  92. expect(payload.dialog.buttons[0]?.label).toBe("Claim");
  93. expect(payload.dialog.buttons[0]?.formattedLabel).toEqual({
  94. format: "html",
  95. text: "<b>Claim</b><script>bad()</script>",
  96. });
  97. expect(payload.dialog.formattedTitle).toEqual({ format: "plain_text", text: "News" });
  98. expect(payload.actions["button-0"]).toEqual({ type: "claim_plan", planId: "plan" });
  99. expect(payload.dialog.description).toEqual({ format: "html", text: "<p>Hello</p>" });
  100. });
  101. it("keeps plaintext literal and does not attach remote styles", () => {
  102. expect(marketingLabel({ format: "plaintext", content: "<b>literal</b>" })).toBe(
  103. "<b>literal</b>",
  104. );
  105. });
  106. });