enterpriseCodingPlanProducts.test.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. buildEnterpriseCodingPlanCampaignDiscountDetails,
  4. buildEnterpriseCodingPlanCreateOrderRequest,
  5. buildEnterpriseCodingPlanEstimateFromPendingOrder,
  6. buildEnterpriseCodingPlanPaymentContent,
  7. buildEnterpriseCodingPlanProductList,
  8. calculateEnterpriseCodingPlanPricingDiscountAmount,
  9. isEnterpriseCodingPlanThirdPartyPaymentRequired,
  10. isEnterpriseCodingPlanPaymentSuccessStatus,
  11. } from "@/settings/model-provider-section/enterpriseCodingPlanProducts.js";
  12. describe("Enterprise Coding Plan products", () => {
  13. it("按后端下发顺序展示企业套餐,并保留 subscribeMode 和采购方式", () => {
  14. const products = buildEnterpriseCodingPlanProductList([
  15. {
  16. productId: "enterprise-lite-monthly",
  17. tier: "LITE",
  18. subscribeMode: "ONE_TIME",
  19. subscribePeriod: "MONTHLY",
  20. purchaseMethodName: "按月采购",
  21. payAmount: 270,
  22. },
  23. {
  24. productId: "enterprise-pro-yearly",
  25. tier: "PRO",
  26. subscribeMode: "ONE_TIME",
  27. subscribePeriod: "YEARLY",
  28. purchaseMethodName: "按年采购",
  29. payAmount: 2700,
  30. },
  31. {
  32. productId: "enterprise-lite-continuous-monthly",
  33. tier: "LITE",
  34. subscribeMode: "CONTINUOUS",
  35. subscribePeriod: "MONTHLY",
  36. purchaseMethodName: "连续包月",
  37. payAmount: 243,
  38. },
  39. {
  40. productId: "enterprise-max-monthly",
  41. tier: "MAX",
  42. subscribeMode: "ONE_TIME",
  43. subscribePeriod: "MONTHLY",
  44. purchaseMethodName: "按月采购",
  45. payAmount: 900,
  46. },
  47. ]);
  48. expect(products.map((product) => product.productId)).toEqual([
  49. "enterprise-lite-monthly",
  50. "enterprise-pro-yearly",
  51. "enterprise-lite-continuous-monthly",
  52. "enterprise-max-monthly",
  53. ]);
  54. expect(products.map((product) => product.priceUnit)).toEqual([
  55. "month",
  56. "year",
  57. "month",
  58. "month",
  59. ]);
  60. expect(products.map((product) => product.purchaseMethodName)).toEqual([
  61. "按月采购",
  62. "按年采购",
  63. "连续包月",
  64. "按月采购",
  65. ]);
  66. });
  67. it("企业卡片价格字段对齐个人套餐:主价用 payAmount,原价用 originalAmount,不把优惠金额当价格", () => {
  68. const [monthlyProduct, continuousYearlyProduct, oneTimeYearlyProduct] =
  69. buildEnterpriseCodingPlanProductList([
  70. {
  71. productId: "enterprise-pro-monthly",
  72. tier: "PRO",
  73. subscribeMode: "ONE_TIME",
  74. subscribePeriod: "MONTHLY",
  75. purchaseMethodName: "按月采购",
  76. originalAmount: 720,
  77. discountAmount: 72,
  78. payAmount: 648,
  79. renewAmount: 648,
  80. },
  81. {
  82. productId: "enterprise-pro-continuous-yearly",
  83. tier: "PRO",
  84. subscribeMode: "CONTINUOUS",
  85. subscribePeriod: "YEARLY",
  86. purchaseMethodName: "连续包年",
  87. originalAmount: 7176,
  88. discountAmount: 0,
  89. payAmount: 7176,
  90. renewAmount: 6458.4,
  91. },
  92. {
  93. productId: "enterprise-pro-yearly",
  94. tier: "PRO",
  95. subscribeMode: "ONE_TIME",
  96. subscribePeriod: "YEARLY",
  97. purchaseMethodName: "按年采购",
  98. originalAmount: 7176,
  99. discountAmount: 0,
  100. payAmount: 7176,
  101. renewAmount: 6458.4,
  102. },
  103. ]);
  104. expect(monthlyProduct).toMatchObject({
  105. originalAmount: 720,
  106. payAmount: 648,
  107. renewAmount: 648,
  108. });
  109. expect(monthlyProduct.discountAmount).toBeUndefined();
  110. expect(monthlyProduct.enterpriseProduct.discountAmount).toBe(72);
  111. expect(continuousYearlyProduct).toMatchObject({
  112. originalAmount: 7176,
  113. payAmount: 6458.4,
  114. priceUnit: "year",
  115. });
  116. expect(continuousYearlyProduct.discountAmount).toBeUndefined();
  117. expect(oneTimeYearlyProduct).toMatchObject({
  118. originalAmount: 7176,
  119. payAmount: 6458.4,
  120. priceUnit: "year",
  121. });
  122. expect(oneTimeYearlyProduct.discountAmount).toBeUndefined();
  123. });
  124. it("把企业折扣明细转换为支付弹窗活动优惠明细", () => {
  125. expect(
  126. buildEnterpriseCodingPlanCampaignDiscountDetails({
  127. campaignDiscountAmount: 270,
  128. discountDetails: [
  129. {
  130. discountType: "REGULAR_DISCOUNT",
  131. discountName: "企业套餐九折",
  132. discountAmount: 270,
  133. },
  134. ],
  135. }),
  136. ).toEqual([
  137. {
  138. campaignName: "企业套餐九折",
  139. campaignDiscountAmount: 270,
  140. rewardDetail: "企业套餐九折",
  141. applyScene: "REGULAR_DISCOUNT",
  142. },
  143. ]);
  144. expect(
  145. buildEnterpriseCodingPlanCampaignDiscountDetails({
  146. campaignDiscountAmount: 270,
  147. discountDetails: [],
  148. }),
  149. ).toEqual([
  150. {
  151. campaignDiscountAmount: 270,
  152. },
  153. ]);
  154. });
  155. it("优先使用 pricing 下发的 campaignDiscountDetails 展示企业活动信息,并使用订单总优惠金额", () => {
  156. expect(
  157. buildEnterpriseCodingPlanCampaignDiscountDetails({
  158. campaignDiscountAmount: 216,
  159. discountDetails: [],
  160. pricingCampaignDiscountDetails: [
  161. {
  162. campaignName: "连续包季 9折",
  163. campaignDiscountAmount: 72,
  164. rewardMode: "PERCENT",
  165. rewardAmount: 10,
  166. rewardDetail: "连续包月享9折优惠",
  167. applyScene: "SUBSCRIPTION",
  168. },
  169. ],
  170. }),
  171. ).toEqual([
  172. {
  173. campaignName: "连续包季 9折",
  174. campaignDiscountAmount: 216,
  175. rewardMode: "PERCENT",
  176. rewardAmount: 10,
  177. rewardDetail: "连续包月享9折优惠",
  178. applyScene: "SUBSCRIPTION",
  179. },
  180. ]);
  181. });
  182. it("试算未返回汇总优惠时,用 pricing 顶层 discountAmount 按席位和时长折算", () => {
  183. const [monthlyOneTimeProduct, yearlyContinuousProduct] = buildEnterpriseCodingPlanProductList([
  184. {
  185. productId: "enterprise-pro-monthly",
  186. tier: "PRO",
  187. subscribeMode: "ONE_TIME",
  188. subscribePeriod: "MONTHLY",
  189. purchaseMethodName: "按月采购",
  190. originalAmount: 720,
  191. discountAmount: 72,
  192. payAmount: 648,
  193. renewAmount: 648,
  194. },
  195. {
  196. productId: "enterprise-pro-continuous-yearly",
  197. tier: "PRO",
  198. subscribeMode: "CONTINUOUS",
  199. subscribePeriod: "YEARLY",
  200. purchaseMethodName: "连续包年",
  201. originalAmount: 7176,
  202. discountAmount: 0,
  203. payAmount: 7176,
  204. renewAmount: 6458.4,
  205. },
  206. ]);
  207. expect(
  208. calculateEnterpriseCodingPlanPricingDiscountAmount({
  209. product: monthlyOneTimeProduct,
  210. maxSeats: 3,
  211. duration: 2,
  212. }),
  213. ).toBe(432);
  214. expect(
  215. calculateEnterpriseCodingPlanPricingDiscountAmount({
  216. product: yearlyContinuousProduct,
  217. maxSeats: 3,
  218. duration: 2,
  219. }),
  220. ).toBe(2152.8);
  221. });
  222. it("多条 pricing 活动按下发金额比例分摊订单总优惠", () => {
  223. expect(
  224. buildEnterpriseCodingPlanCampaignDiscountDetails({
  225. campaignDiscountAmount: 300,
  226. pricingCampaignDiscountDetails: [
  227. {
  228. campaignName: "活动 A",
  229. campaignDiscountAmount: 20,
  230. },
  231. {
  232. campaignName: "活动 B",
  233. campaignDiscountAmount: 10,
  234. },
  235. ],
  236. }),
  237. ).toEqual([
  238. {
  239. campaignName: "活动 A",
  240. campaignDiscountAmount: 200,
  241. },
  242. {
  243. campaignName: "活动 B",
  244. campaignDiscountAmount: 100,
  245. },
  246. ]);
  247. });
  248. it("ONE_TIME 下单请求包含 duration,并原样回传试算金额", () => {
  249. const product = buildEnterpriseCodingPlanProductList([
  250. {
  251. productId: "enterprise-lite-monthly",
  252. tier: "LITE",
  253. subscribeMode: "ONE_TIME",
  254. subscribePeriod: "MONTHLY",
  255. purchaseMethodName: "按月采购",
  256. payAmount: 270,
  257. },
  258. ])[0];
  259. expect(
  260. buildEnterpriseCodingPlanCreateOrderRequest({
  261. product,
  262. maxSeats: 3,
  263. duration: 2,
  264. estimate: {
  265. totalOriginalAmount: 1620,
  266. totalPayAmount: 1458,
  267. thirdPayAmount: 1458,
  268. },
  269. }),
  270. ).toMatchObject({
  271. productId: "enterprise-lite-monthly",
  272. maxSeats: 3,
  273. subscribeMode: "ONE_TIME",
  274. subscribePeriod: "MONTHLY",
  275. purchaseType: "PAY",
  276. duration: 2,
  277. giveAmount: 0,
  278. balanceDeductAmount: 0,
  279. totalOriginalAmount: 1620,
  280. totalPayAmount: 1458,
  281. thirdPayAmount: 1458,
  282. });
  283. });
  284. it("支付内容优先使用 payUrl,否则使用 alipayJumpSchema", () => {
  285. expect(
  286. buildEnterpriseCodingPlanPaymentContent({
  287. orderNo: "order-1",
  288. totalOriginalAmount: 100,
  289. totalPayAmount: 90,
  290. thirdPayAmount: 90,
  291. alipayJumpSchema: "alipays://pay",
  292. payUrl: "https://pay.example",
  293. }),
  294. ).toBe("https://pay.example");
  295. expect(
  296. buildEnterpriseCodingPlanPaymentContent({
  297. orderNo: "order-2",
  298. totalOriginalAmount: 100,
  299. totalPayAmount: 90,
  300. thirdPayAmount: 90,
  301. alipayJumpSchema: "alipays://pay",
  302. }),
  303. ).toBe("alipays://pay");
  304. });
  305. it("余额全额抵扣且无支付内容时不要求三方支付", () => {
  306. expect(
  307. isEnterpriseCodingPlanThirdPartyPaymentRequired({
  308. orderNo: "order-free",
  309. totalOriginalAmount: 100,
  310. totalPayAmount: 100,
  311. thirdPayAmount: 0,
  312. }),
  313. ).toBe(false);
  314. expect(
  315. isEnterpriseCodingPlanThirdPartyPaymentRequired({
  316. orderNo: "order-pay",
  317. totalOriginalAmount: 100,
  318. totalPayAmount: 100,
  319. thirdPayAmount: 0,
  320. payUrl: "https://pay.example",
  321. }),
  322. ).toBe(true);
  323. });
  324. it("把企业待支付订单映射为确认页试算金额", () => {
  325. expect(
  326. buildEnterpriseCodingPlanEstimateFromPendingOrder({
  327. orderNo: "order-pending",
  328. productId: "enterprise-lite-monthly",
  329. amount: 2430,
  330. totalAmount: 2700,
  331. deductionAmount: 270,
  332. paymentStatus: "WAIT_PAY",
  333. createTime: "2026-05-09 14:30:00",
  334. subscriptionNo: "SUB20260513a1b2c3d4",
  335. seatCount: 10,
  336. }),
  337. ).toEqual({
  338. totalOriginalAmount: 2700,
  339. campaignDiscountAmount: 270,
  340. totalPayAmount: 2430,
  341. thirdPayAmount: 2430,
  342. });
  343. });
  344. it("识别企业支付成功与待支付状态", () => {
  345. expect(isEnterpriseCodingPlanPaymentSuccessStatus("SUCCESS")).toBe(true);
  346. expect(isEnterpriseCodingPlanPaymentSuccessStatus("WAIT_PAY")).toBe(false);
  347. });
  348. });