marketingPopupAdapter.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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("preserves popup themes and uses default when omitted", () => {
  37. const text = { format: "plaintext" as const, content: "Close" };
  38. const theme = { variant: "ghost" as const, class: "rounded-lg", style: "color: red" };
  39. const result = adaptMarketingPopup(
  40. "test",
  41. "en-US",
  42. {
  43. title: text,
  44. description: text,
  45. buttons: [
  46. { text, theme, action: { type: "close" } },
  47. { text, action: { type: "close" } },
  48. ],
  49. },
  50. null,
  51. );
  52. expect(result.dialog.buttons[0]?.theme).toEqual(theme);
  53. expect(result.dialog.buttons[1]?.theme).toEqual({ variant: "default" });
  54. });
  55. it("keeps copy text literal and correlates navigation through stable button ids", () => {
  56. const text = { format: "plaintext" as const, content: "Open" };
  57. const payload = adaptMarketingPopup(
  58. "action",
  59. "en-US",
  60. {
  61. title: text,
  62. description: text,
  63. buttons: [
  64. {
  65. text,
  66. action: {
  67. type: "navigate",
  68. args: { page: "settings", section: "models", provider_id: "bigmodel" },
  69. },
  70. },
  71. {
  72. text,
  73. action: {
  74. type: "navigate",
  75. args: { page: "plugin_marketplace", plugin_id: "plugin@market" },
  76. },
  77. },
  78. { text, action: { type: "copy_text", args: { text: "<b>literal</b>\n next " } } },
  79. ],
  80. },
  81. null,
  82. );
  83. expect(payload.actions).toEqual({
  84. "button-0": { type: "navigate", destination: "settings" },
  85. "button-1": { type: "navigate", destination: "plugin_store" },
  86. "button-2": { type: "copy_text", text: "<b>literal</b>\n next " },
  87. });
  88. expect(payload.dialog.buttons.map((button) => button.actionId)).toEqual([
  89. "button-0",
  90. "button-1",
  91. "button-2",
  92. ]);
  93. });
  94. it("maps existing inline actions and strips markup from button labels", () => {
  95. const payload = adaptMarketingPopup(
  96. "193001",
  97. "en-US",
  98. {
  99. title: { format: "plaintext", content: "News" },
  100. description: { format: "html", content: "<p>Hello</p>" },
  101. buttons: [
  102. {
  103. text: { format: "html", content: "<b>Claim</b><script>bad()</script>" },
  104. action: { type: "claim_zcode_plan", args: { plan_id: "plan" } },
  105. },
  106. ],
  107. },
  108. null,
  109. );
  110. expect(payload.dialog.hero).toBeNull();
  111. expect(payload.dialog.buttons[0]?.label).toBe("Claim");
  112. expect(payload.dialog.buttons[0]?.formattedLabel).toEqual({
  113. format: "html",
  114. text: "<b>Claim</b><script>bad()</script>",
  115. });
  116. expect(payload.dialog.formattedTitle).toEqual({ format: "plain_text", text: "News" });
  117. expect(payload.actions["button-0"]).toEqual({ type: "claim_plan", planId: "plan" });
  118. expect(payload.dialog.description).toEqual({ format: "html", text: "<p>Hello</p>" });
  119. });
  120. it("keeps plaintext literal and does not attach remote styles", () => {
  121. expect(marketingLabel({ format: "plaintext", content: "<b>literal</b>" })).toBe(
  122. "<b>literal</b>",
  123. );
  124. });
  125. });