272eca04ce
购买/支付(Phase B): - 新 SegSwitch 段控 widget(镜像 .segswitch 原子) - 购买页重构:顶部支付渠道 switch(默认 USDT→加密货币 / 人民币→支付宝),套餐价随渠道分币种显示,删底部弹窗选方式;点购买直达支付页 - 支付页加订单信息卡(套餐/渠道/金额按币种) - models:PayChannel + PayCatalogItem.priceUsdtMicro/priceLabel(ch)/perMonthLabel(ch) 订单(P0,全新): - payment_api.listOrders() + orders_provider(列表 + 详情 family) - OrdersScreen 列表→详情(状态 pill / 可复制订单号 / 继续支付·重新购买) - 导航:NavView.orders + desktop_shell 分支 + account 入口 配套: - l10n +19 getter(支付渠道 4 + 订单 15)×6 语言,l10n 闸绿 - 图标单源迁移 lucide_icons_flutter + pangolin_icons.dart(codegen) - 联系渠道订正(Telegram/邮件 support@yanmeiai.com/官网) + 前端去广告&时长 - 测试:购买 switch 分币种 + 订单列表/空态/详情 + 假 API listOrders;golden 重录 - flutter analyze 0 error,flutter test 226 全绿 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013nMthbVEmQquxBRKb9Fj8u
104 lines
4.0 KiB
Dart
104 lines
4.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<List<PayOrderSummary>> listOrders() 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('stopPolling() 只停 Timer,不远程 cancel、不重置 state', () 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);
|
|
|
|
ctl.stopPolling();
|
|
|
|
expect(api.cancelCalls, 0);
|
|
expect(c.read(paymentFlowProvider).phase, PaymentPhase.awaitingPayment);
|
|
expect(c.read(paymentFlowProvider).order?.orderNo, 'pay1');
|
|
|
|
// Timer 已停:再等一拍轮询周期,statusCalls 不应继续增长(pollOnce 只由
|
|
// Timer 或测试显式调用驱动,这里断言的是"没有 Timer 在背后继续触发")。
|
|
final callsAfterStop = api.statusCalls;
|
|
await Future<void>.delayed(const Duration(milliseconds: 10));
|
|
expect(api.statusCalls, callsAfterStop);
|
|
});
|
|
|
|
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');
|
|
});
|
|
}
|