feat(app): 完成 P0 钱包解锁、下载落盘与手机号登录
This commit is contained in:
1 parent
de941c841c
commit
136ee74618
47 files changed
+3264
-70
No files matched your search
@@ -0,0 +1,81 @@
|
||||
// Author: fruit.com.cn
|
||||
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:fruitshorts/core/data/remote/api_client.dart';
|
||||
import 'package:fruitshorts/core/data/remote/commerce_api.dart';
|
||||
|
||||
import '../support/fake_http_adapter.dart';
|
||||
|
||||
void main() {
|
||||
test('should_parse_wallet_and_ledger_when_server_returns_string_ids', () async {
|
||||
final adapter = FakeHttpAdapter(
|
||||
(options) async => FakeHttpResponse(
|
||||
jsonEncode({
|
||||
'code': 0,
|
||||
'message': 'ok',
|
||||
'data': {
|
||||
'wallet': {
|
||||
'id': '2111012753262120962',
|
||||
'coinBalance': '320',
|
||||
'totalRechargedCoin': '500',
|
||||
'totalConsumedCoin': '180',
|
||||
'totalRefundedCoin': '0',
|
||||
},
|
||||
'ledger': [
|
||||
{
|
||||
'id': '2111012753262120963',
|
||||
'changeAmount': '-50',
|
||||
'balanceAfter': '320',
|
||||
'bizType': 'UNLOCK',
|
||||
'remark': '解锁第 3 集',
|
||||
'createdAt': '2026-10-19 10:00:00',
|
||||
},
|
||||
],
|
||||
},
|
||||
}),
|
||||
),
|
||||
);
|
||||
final api = CommerceApi(
|
||||
ApiClient(baseUrl: 'http://127.0.0.1:8081', adapter: adapter),
|
||||
);
|
||||
|
||||
final snapshot = await api.wallet();
|
||||
|
||||
expect(adapter.requests.single.uri.path, '/app/wallet');
|
||||
expect(snapshot.wallet.coinBalance, 320);
|
||||
expect(snapshot.ledger.single.id, '2111012753262120963');
|
||||
expect(snapshot.ledger.single.changeAmount, -50);
|
||||
});
|
||||
|
||||
test('should_post_unlock_for_episode_when_requested', () async {
|
||||
final adapter = FakeHttpAdapter(
|
||||
(options) async => FakeHttpResponse(
|
||||
jsonEncode({
|
||||
'code': 0,
|
||||
'message': 'ok',
|
||||
'data': {
|
||||
'episodeId': '12',
|
||||
'dramaId': '5',
|
||||
'unlocked': true,
|
||||
'accessType': 'UNLOCKED',
|
||||
'coinCost': '50',
|
||||
'coinBalance': '270',
|
||||
},
|
||||
}),
|
||||
),
|
||||
);
|
||||
final api = CommerceApi(
|
||||
ApiClient(baseUrl: 'http://127.0.0.1:8081', adapter: adapter),
|
||||
);
|
||||
|
||||
final result = await api.unlock('12');
|
||||
|
||||
expect(adapter.requests.single.method, 'POST');
|
||||
expect(adapter.requests.single.uri.path, '/app/episodes/12/unlock');
|
||||
expect(result.unlocked, isTrue);
|
||||
expect(result.coinCost, 50);
|
||||
expect(result.coinBalance, 270);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
// Author: fruit.com.cn
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:fruitshorts/core/data/models/wallet.dart';
|
||||
import 'package:fruitshorts/core/data/remote/api_exception.dart';
|
||||
import 'package:fruitshorts/core/data/repositories/commerce_repository.dart';
|
||||
import 'package:fruitshorts/core/data/services/wallet_controller.dart';
|
||||
|
||||
void main() {
|
||||
test('should_expose_server_balance_when_wallet_loads', () async {
|
||||
final controller = WalletController(
|
||||
FakeCommerceRepository(snapshot: _snapshot(balance: 320)),
|
||||
);
|
||||
|
||||
await controller.load();
|
||||
|
||||
expect(controller.balance, 320);
|
||||
expect(controller.ledger.single.changeAmount, -50);
|
||||
expect(controller.error, isNull);
|
||||
});
|
||||
|
||||
test('should_update_balance_and_clear_error_when_unlock_succeeds', () async {
|
||||
final repository = FakeCommerceRepository(
|
||||
snapshot: _snapshot(balance: 320),
|
||||
unlockResult: const UnlockResult(
|
||||
unlocked: true,
|
||||
accessType: 'UNLOCKED',
|
||||
coinCost: 50,
|
||||
coinBalance: 270,
|
||||
),
|
||||
);
|
||||
final controller = WalletController(repository);
|
||||
await controller.load();
|
||||
|
||||
final result = await controller.unlock('episode-9');
|
||||
|
||||
expect(result?.unlocked, isTrue);
|
||||
expect(controller.balance, 270);
|
||||
expect(controller.unlocking, isFalse);
|
||||
expect(controller.unlockError, isNull);
|
||||
});
|
||||
|
||||
test('should_keep_balance_and_expose_message_when_unlock_is_rejected', () async {
|
||||
final controller = WalletController(
|
||||
FakeCommerceRepository(
|
||||
snapshot: _snapshot(balance: 20),
|
||||
unlockError: const ApiException(100002, '金币余额不足'),
|
||||
),
|
||||
);
|
||||
await controller.load();
|
||||
|
||||
final result = await controller.unlock('episode-9');
|
||||
|
||||
expect(result, isNull);
|
||||
expect(controller.balance, 20);
|
||||
expect(controller.unlockError, '金币余额不足');
|
||||
});
|
||||
}
|
||||
|
||||
WalletSnapshot _snapshot({required int balance}) => WalletSnapshot(
|
||||
wallet: CoinWallet(
|
||||
coinBalance: balance,
|
||||
totalRechargedCoin: 500,
|
||||
totalConsumedCoin: 180,
|
||||
totalRefundedCoin: 0,
|
||||
),
|
||||
ledger: [
|
||||
CoinLedgerEntry(
|
||||
id: '1',
|
||||
changeAmount: -50,
|
||||
balanceAfter: balance,
|
||||
bizType: 'UNLOCK',
|
||||
remark: '解锁第 3 集',
|
||||
createdAt: DateTime(2026, 10, 19),
|
||||
),
|
||||
],
|
||||
);
|
||||
|
||||
class FakeCommerceRepository implements CommerceRepository {
|
||||
FakeCommerceRepository({
|
||||
required this.snapshot,
|
||||
this.unlockResult,
|
||||
this.unlockError,
|
||||
});
|
||||
|
||||
final WalletSnapshot snapshot;
|
||||
final UnlockResult? unlockResult;
|
||||
final ApiException? unlockError;
|
||||
final List<String> unlockedEpisodes = [];
|
||||
|
||||
@override
|
||||
Future<WalletSnapshot> fetchWallet() async => snapshot;
|
||||
|
||||
@override
|
||||
Future<UnlockResult> unlock(String episodeId) async {
|
||||
unlockedEpisodes.add(episodeId);
|
||||
if (unlockError != null) throw unlockError!;
|
||||
return unlockResult!;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user