enterpriseCodingPlanProducts.test.ts 11 KB

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