virtualPayProduct.test.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import test from 'node:test';
  2. import assert from 'node:assert/strict';
  3. import {
  4. assertAllowedMiaoguoPrice,
  5. buildWechatProductId,
  6. isMiaoguoTestUser,
  7. resolveConfiguredProduct
  8. } from '../src/api/virtualPay/virtualPayProduct.js';
  9. test('按内部产品编号和实际金额生成微信道具 ID', () => {
  10. assert.equal(buildWechatProductId(166, 100), '166_1');
  11. assert.equal(buildWechatProductId(166, 19900), '166_199');
  12. assert.equal(buildWechatProductId(166, 25800), '166_258');
  13. assert.equal(buildWechatProductId(205, 19900), '205_199');
  14. });
  15. test('秒过只接受已发布的三个业务价位', () => {
  16. assert.doesNotThrow(() => assertAllowedMiaoguoPrice(9, 100));
  17. assert.doesNotThrow(() => assertAllowedMiaoguoPrice(7, 19900));
  18. assert.doesNotThrow(() => assertAllowedMiaoguoPrice(7, 25800));
  19. assert.throws(() => assertAllowedMiaoguoPrice(7, 100, 8), /暂不支持/);
  20. assert.throws(() => assertAllowedMiaoguoPrice(9, 19900), /暂不支持/);
  21. });
  22. test('仅服务端确认的测试用户可用 1 元测试购买', () => {
  23. assert.equal(isMiaoguoTestUser(1), true);
  24. assert.equal(isMiaoguoTestUser(7), true);
  25. assert.equal(isMiaoguoTestUser(8), false);
  26. assert.equal(isMiaoguoTestUser(3089), true);
  27. assert.doesNotThrow(() => assertAllowedMiaoguoPrice(7, 100, 1));
  28. assert.doesNotThrow(() => assertAllowedMiaoguoPrice(7, 100, 3089));
  29. });
  30. test('没有显式配置时使用道具命名约定', () => {
  31. const product = resolveConfiguredProduct({
  32. products: [],
  33. payType: 7,
  34. price: 19900,
  35. env: 0,
  36. internalProductId: 166
  37. });
  38. assert.equal(product.productId, '166_199');
  39. assert.equal(product.resolvedGoodsPrice, 19900);
  40. });
  41. test('显式商品配置仍可覆盖默认命名规则', () => {
  42. const product = resolveConfiguredProduct({
  43. products: [{ productId: 'special_offer', payType: 7, price: 19900, goodsPrice: 25800, env: 0 }],
  44. payType: 7,
  45. price: 19900,
  46. env: 0,
  47. internalProductId: 166
  48. });
  49. assert.equal(product.productId, 'special_offer');
  50. assert.equal(product.resolvedGoodsPrice, 25800);
  51. });
  52. test('默认道具命名拒绝非整数元金额', () => {
  53. assert.throws(() => buildWechatProductId(166, 19950), /整数元/);
  54. });