155 lines
4.3 KiB
Dart
155 lines
4.3 KiB
Dart
// 会员与钱包相关模型解析单测
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
import 'package:video_app/models/membership_models.dart';
|
|
import 'package:video_app/services/wallet_service.dart';
|
|
|
|
void main() {
|
|
group('MembershipPlan / PlansResult', () {
|
|
test('fromJson 解析', () {
|
|
final r = PlansResult.fromJson(const {
|
|
'plans': [
|
|
{
|
|
'plan_code': 'monthly',
|
|
'name': '月度会员',
|
|
'days': 30,
|
|
'points': 300,
|
|
'enabled': true,
|
|
'desc': '畅享全站',
|
|
},
|
|
{
|
|
'plan_code': 'lifetime',
|
|
'name': '终身会员',
|
|
'days': 0,
|
|
'points': 3000,
|
|
},
|
|
],
|
|
'benefits': ['免广告', '无限观看'],
|
|
});
|
|
expect(r.plans.length, 2);
|
|
expect(r.plans[0].planCode, 'monthly');
|
|
expect(r.plans[0].days, 30);
|
|
expect(r.plans[1].days, 0); // 0 = 终身
|
|
expect(r.plans[1].enabled, isTrue); // 缺省 true
|
|
expect(r.benefits, ['免广告', '无限观看']);
|
|
});
|
|
|
|
test('PlansResult 空兜底', () {
|
|
final r = PlansResult.fromJson(const {});
|
|
expect(r.plans, isEmpty);
|
|
expect(r.benefits, isEmpty);
|
|
});
|
|
});
|
|
|
|
group('MyMemberState', () {
|
|
test('fromJson 解析含历史', () {
|
|
final m = MyMemberState.fromJson(const {
|
|
'is_member': true,
|
|
'is_lifetime': false,
|
|
'current_plan': 'monthly',
|
|
'plan_name': '月度会员',
|
|
'expire_at': '2026-11-14 12:00:00',
|
|
'remaining_days': 30,
|
|
'history': [
|
|
{
|
|
'order_no': 'M202610150001',
|
|
'plan_name': '月度会员',
|
|
'points_cost': 300,
|
|
'start_at': '2026-10-15 12:00:00',
|
|
'end_at': '2026-11-14 12:00:00',
|
|
'source': 1,
|
|
}
|
|
],
|
|
});
|
|
expect(m.isMember, isTrue);
|
|
expect(m.remainingDays, 30);
|
|
expect(m.history.single.sourceLabel, '积分开通');
|
|
});
|
|
|
|
test('缺省兜底与赠送来源标签', () {
|
|
final m = MyMemberState.fromJson(const {});
|
|
expect(m.isMember, isFalse);
|
|
expect(m.history, isEmpty);
|
|
|
|
final gift = MemberHistoryItem.fromJson(const {
|
|
'order_no': 'G1',
|
|
'plan_name': '体验会员',
|
|
'points_cost': 0,
|
|
'source': 2,
|
|
});
|
|
expect(gift.sourceLabel, '活动赠送');
|
|
});
|
|
});
|
|
|
|
group('WalletInfo / CommentRewardProgress', () {
|
|
test('fromJson 解析', () {
|
|
final w = WalletInfo.fromJson(const {
|
|
'points': 860,
|
|
'total_recharge': 1000,
|
|
'total_earned': 120,
|
|
'total_consumed': 260,
|
|
'comment_reward_progress': {
|
|
'current': 4,
|
|
'target': 10,
|
|
'today_rewarded': 2,
|
|
'daily_limit': 5,
|
|
},
|
|
});
|
|
expect(w.points, 860);
|
|
expect(w.rewardProgress.current, 4);
|
|
expect(w.rewardProgress.dailyLimit, 5);
|
|
});
|
|
|
|
test('缺省兜底', () {
|
|
final w = WalletInfo.fromJson(const {});
|
|
expect(w.points, 0);
|
|
expect(w.rewardProgress.target, 10);
|
|
});
|
|
});
|
|
|
|
group('PointsRecord / PointsRecordsPage', () {
|
|
test('fromJson 解析与 biz_type 标签', () {
|
|
final r = PointsRecord.fromJson(const {
|
|
'id': 1,
|
|
'change_amount': -30,
|
|
'balance_after': 830,
|
|
'biz_type': 4,
|
|
'remark': '开通月度会员',
|
|
'created_at': '2026-10-15 12:00:00',
|
|
});
|
|
expect(r.isExpense, isTrue);
|
|
expect(r.bizTypeLabel, '会员');
|
|
|
|
final income = PointsRecord.fromJson(const {
|
|
'id': 2,
|
|
'change_amount': 50,
|
|
'balance_after': 880,
|
|
'biz_type': 3,
|
|
});
|
|
expect(income.isExpense, isFalse);
|
|
expect(income.bizTypeLabel, '评论奖励');
|
|
|
|
final unknown = PointsRecord.fromJson(const {
|
|
'id': 3,
|
|
'change_amount': 1,
|
|
'balance_after': 881,
|
|
'biz_type': 99,
|
|
});
|
|
expect(unknown.bizTypeLabel, '其他');
|
|
});
|
|
|
|
test('PointsRecordsPage 解析', () {
|
|
final p = PointsRecordsPage.fromJson(const {
|
|
'total': 2,
|
|
'list': [
|
|
{'id': 1, 'change_amount': 10, 'balance_after': 10, 'biz_type': 1},
|
|
{'id': 2, 'change_amount': -5, 'balance_after': 5, 'biz_type': 2},
|
|
],
|
|
});
|
|
expect(p.total, 2);
|
|
expect(p.items.length, 2);
|
|
expect(p.items[0].bizTypeLabel, '充值');
|
|
});
|
|
});
|
|
}
|