Browse Source

✅ test(payment): 完善支付回调集成测试

- 添加PaymentService验签方法的mock实现
- 引入afterEach清理测试间谍
- 导入afterEach测试工具函数

🔧 chore(test): 优化测试配置

- 关闭并行测试以避免数据库连接冲突
yourname 3 weeks ago
parent
commit
c8368ae6c5

+ 23 - 1
packages/mini-payment/tests/integration/payment-callback.integration.test.ts

@@ -1,4 +1,4 @@
-import { describe, it, expect, beforeEach, vi } from 'vitest';
+import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest';
 import { testClient } from 'hono/testing';
 import {
   IntegrationTestDatabase,
@@ -10,6 +10,7 @@ import { PaymentStatus } from '../../src/entities/payment.types.js';
 import { UserEntity } from '@d8d/user-module';
 import { Role } from '@d8d/user-module';
 import { File } from '@d8d/file-module';
+import { PaymentService } from '../../src/services/payment.service.js';
 import { config } from 'dotenv';
 import { resolve } from 'path';
 
@@ -23,6 +24,7 @@ describe('支付回调API集成测试', () => {
   let client: ReturnType<typeof testClient<typeof PaymentRoutes>>;
   let testUser: UserEntity;
   let testPayment: PaymentEntity;
+  let verifySignSpy: any;
 
   // 使用真实的微信支付回调数据 - 直接使用原始请求体字符串
   const rawBody = '{"id":"495e231b-9fd8-54a1-8a30-2a38a807744c","create_time":"2025-10-25T12:48:11+08:00","resource_type":"encrypt-resource","event_type":"TRANSACTION.SUCCESS","summary":"支付成功","resource":{"original_type":"transaction","algorithm":"AEAD_AES_256_GCM","ciphertext":"tl1/8FRRn6g0gRq8IoVR8+95VuIADYBDOt6N9PKiHVhiD6l++W5g/wg6VlsCRIZJ+KWMYTaf5FzQHMjCs8o9otIkLLuJA2aZC+kCQtGxNfyVBwxool/tLT9mHd0dFGThqbj8vb/lm+jjNcmmiWHz+J1ZRvGl7mH4I714vudok7JRt5Q0u0tYaLWr76TTXuQErlA7T4KbeVeGAj8iMpu2ErCpR9QRif36Anc5ARjNYrIWfraXlmUXVbXermDyJ8r4o/4QCFfGk8L1u1WqNYASrRTQvQ8OPqj/J21OkDxbPPrOiEmAX1jOvONvIVEe9Lbkm6rdhW4aLRoZYtiusAk/Vm7MI/UYPwRZbyuc4wwdA1T1D4RdJd/m2I4KSvZHQgs0DM0tLqlb0z3880XYNr8iPFnyu2r8Z8LGcXD+COm06vc7bvNWh3ODwmMrmZQkym/Y/T3X/h/4MZj7+1h2vYHqnnrsgtNPHc/2IwWC/fQlPwtSrLh6iUxSd0betFpKLSq08CaJZvnenpDf1ORRMvd8EhTtIJJ4mV4v+VzCOYNhIcBhKp9XwsuhxIdkpGGmNPpow2c2BXY=","associated_data":"transaction","nonce":"sTnWce32BTQP"}}';
@@ -64,6 +66,26 @@ describe('支付回调API集成测试', () => {
 
     // 手动更新支付记录ID为13,与真实回调数据一致
     await dataSource.query('UPDATE payments SET external_order_id = 13 WHERE id = $1', [testPayment.id]);
+
+    // Mock PaymentService 的验签方法
+    verifySignSpy = vi.spyOn(PaymentService.prototype as any, 'wxPay').mockImplementation(() => ({
+      verifySign: vi.fn().mockResolvedValue(true),
+      decipher_gcm: vi.fn().mockReturnValue(JSON.stringify({
+        out_trade_no: `ORDER_13_${Date.now()}`,
+        trade_state: 'SUCCESS',
+        transaction_id: 'test_transaction_id',
+        amount: {
+          total: 1
+        }
+      }))
+    }));
+  });
+
+  afterEach(() => {
+    // 清理 spy
+    if (verifySignSpy) {
+      verifySignSpy.mockRestore();
+    }
   });
 
   describe('POST /payment/callback - 支付回调', () => {

+ 3 - 1
packages/mini-payment/vitest.config.ts

@@ -14,6 +14,8 @@ export default defineConfig({
         '**/*.config.*',
         '**/dist/**'
       ]
-    }
+    },
+    // 关闭并行测试以避免数据库连接冲突
+    fileParallelism: false
   }
 });