// 功能描述:充值金额精度、协议完整性和到账事实校验;版本:1.0.0。 import 'package:flutter_test/flutter_test.dart'; import 'package:user_app/domain/models/recharge.dart'; void main() { test('金额按十进制精确转分,拒绝超精度和指数输入', () { expect(parseRechargeAmount('0.29'), 29); expect(parseRechargeAmount(' 100.1 '), 10010); expect(parseRechargeAmount('999999999.99'), 99999999999); for (final value in ['-1', '1e2', '1.001', '.5', '1.', 'NaN', '']) { expect(parseRechargeAmount(value), isNull, reason: value); } }); test('只有钱包充值完成状态才表示到账', () { RechargeRecord record(int status, int? paymentStatus) => RechargeRecord.fromJson({ 'identity': 'recharge-1', 'amount': 10000, 'recharge_status': status, 'payment_status': paymentStatus, 'created_at': '2026-09-11T00:00:00Z', }); expect(record(10, 23).credited, isFalse); expect(record(10, 30).statusText, '支付已关闭,未入账'); expect(record(23, null).credited, isTrue); expect(record(23, 30).statusText, '已到账'); }); test('缺少协议允许读取配置,残缺协议必须拒绝', () { final json = {'min_amount': 1, 'max_amount': 500000, 'channels': []}; expect(RechargeOptions.fromJson(json).agreement, isNull); for (final identity in ['', ' ']) { expect( () => RechargeOptions.fromJson({ ...json, 'agreement': { 'identity': identity, 'version': 1, 'body': '协议正文', }, }), throwsFormatException, ); } expect(() => RechargeOptions.fromJson({...json, 'min_amount': 0}), throwsFormatException); expect(() => RechargeOptions.fromJson({...json, 'max_amount': 0}), throwsFormatException); expect(() => rechargeCents(1.5), throwsFormatException); }); }