e66f79fdf6
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013nMthbVEmQquxBRKb9Fj8u
81 lines
3.0 KiB
Dart
81 lines
3.0 KiB
Dart
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:pangolin_vpn/models/payment.dart';
|
|
import 'package:pangolin_vpn/services/auth_api.dart';
|
|
import 'package:pangolin_vpn/services/payment_api.dart';
|
|
import 'package:pangolin_vpn/state/payment_provider.dart';
|
|
|
|
class _FakePaymentApi implements PaymentApi {
|
|
_FakePaymentApi();
|
|
int createCalls = 0, cancelCalls = 0, statusCalls = 0;
|
|
bool activated = false;
|
|
Exception? retryError;
|
|
String lastMethod = '';
|
|
|
|
@override
|
|
Future<List<PayCatalogItem>> catalog() async => const [];
|
|
|
|
@override
|
|
Future<PayOrder> createOrder({required String sku, required String method, Map<String, String>? metadata}) async {
|
|
createCalls++;
|
|
lastMethod = method;
|
|
return PayOrder(
|
|
orderNo: 'pay$createCalls',
|
|
session: PaySession(renderType: method == 'crypto' ? 'crypto_address' : 'redirect', payload: const {'url': 'https://x'}, expiresAt: null));
|
|
}
|
|
|
|
@override
|
|
Future<PayOrderStatus> orderStatus(String orderNo) async {
|
|
statusCalls++;
|
|
return PayOrderStatus(orderNo: orderNo, payStatus: activated ? 'succeeded' : 'pending', activated: activated, expiresAt: null);
|
|
}
|
|
|
|
@override
|
|
Future<PayOrder> retry(String orderNo, {required String method, Map<String, String>? metadata}) async {
|
|
if (retryError != null) throw retryError!;
|
|
return PayOrder(orderNo: orderNo, session: PaySession(renderType: 'redirect', payload: const {'url': 'https://y'}, expiresAt: null));
|
|
}
|
|
|
|
@override
|
|
Future<void> cancel(String orderNo) async {
|
|
cancelCalls++;
|
|
}
|
|
}
|
|
|
|
ProviderContainer _container(_FakePaymentApi api) {
|
|
final c = ProviderContainer(overrides: [paymentApiProvider.overrideWithValue(api)]);
|
|
addTearDown(c.dispose);
|
|
return c;
|
|
}
|
|
|
|
void main() {
|
|
const item = PayCatalogItem(sku: 'pro_month', plan: 'pro', days: 31, priceMinor: 2999, currency: 'CNY');
|
|
|
|
test('start → awaitingPayment,pollOnce 到 activated 变 succeeded', () async {
|
|
final api = _FakePaymentApi();
|
|
final c = _container(api);
|
|
final ctl = c.read(paymentFlowProvider.notifier);
|
|
await ctl.start(item, 'crypto');
|
|
expect(c.read(paymentFlowProvider).phase, PaymentPhase.awaitingPayment);
|
|
await ctl.pollOnce();
|
|
expect(c.read(paymentFlowProvider).phase, PaymentPhase.awaitingPayment);
|
|
api.activated = true;
|
|
await ctl.pollOnce();
|
|
expect(c.read(paymentFlowProvider).phase, PaymentPhase.succeeded);
|
|
});
|
|
|
|
test('switchMethod 遇 CURRENCY_MISMATCH → cancel 旧单 + 新 method 重新下单', () async {
|
|
final api = _FakePaymentApi();
|
|
final c = _container(api);
|
|
final ctl = c.read(paymentFlowProvider.notifier);
|
|
await ctl.start(item, 'crypto');
|
|
api.retryError = const AuthApiException(
|
|
statusCode: 409, messageZh: 'x', messageEn: 'x', code: 'CURRENCY_MISMATCH');
|
|
await ctl.switchMethod('alipay');
|
|
expect(api.cancelCalls, 1);
|
|
expect(api.createCalls, 2, reason: '换币种必须新单(pay 契约)');
|
|
expect(api.lastMethod, 'alipay');
|
|
expect(c.read(paymentFlowProvider).order?.orderNo, 'pay2');
|
|
});
|
|
}
|