119 lines
3.8 KiB
Dart
119 lines
3.8 KiB
Dart
// 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/download_api.dart';
|
|
|
|
import '../support/fake_http_adapter.dart';
|
|
|
|
void main() {
|
|
String envelope(Object? data) =>
|
|
jsonEncode({'code': 0, 'message': 'ok', 'data': data});
|
|
|
|
Map<String, dynamic> licenseJson({int progress = 35}) => {
|
|
'licenseId': '2111012753262120962',
|
|
'dramaId': '2111012753262120963',
|
|
'dramaTitle': 'The Signal',
|
|
'episodeId': '2111012753262120964',
|
|
'episodeNo': 2,
|
|
'deviceId': 'device-9',
|
|
'quality': '720P',
|
|
'playUrl': '/files/episode.mp4?e=1&s=sig',
|
|
'licenseKey': 'license-key',
|
|
'status': 'ACTIVE',
|
|
'grantedAt': '2026-10-17T12:00:00',
|
|
'expireAt': '2026-11-16T12:00:00',
|
|
'taskStatus': 'DOWNLOADING',
|
|
'progressPercent': progress,
|
|
'downloadedBytes': '1024',
|
|
'totalBytes': '4096',
|
|
'errorMsg': '',
|
|
};
|
|
|
|
test(
|
|
'should_call_all_download_contracts_and_parse_string_ids_when_api_is_used',
|
|
() async {
|
|
final adapter = FakeHttpAdapter((options) async {
|
|
final path = options.uri.path;
|
|
if (path == '/app/downloads/policy') {
|
|
return FakeHttpResponse(
|
|
envelope({
|
|
'maxPerDevice': 20,
|
|
'licenseDays': 30,
|
|
'wifiOnly': true,
|
|
'maxQuality': 'AUTO',
|
|
'maxStorageBytes': '0',
|
|
'used': 1,
|
|
'remaining': 19,
|
|
'expirePolicyText': '30 days',
|
|
}),
|
|
);
|
|
}
|
|
if (path == '/app/downloads/settings') {
|
|
return FakeHttpResponse(envelope(null));
|
|
}
|
|
if (path == '/app/downloads' && options.method == 'DELETE') {
|
|
return FakeHttpResponse(envelope(2));
|
|
}
|
|
if (path == '/app/downloads') {
|
|
return FakeHttpResponse(
|
|
envelope(options.method == 'GET' ? [licenseJson()] : licenseJson()),
|
|
);
|
|
}
|
|
return FakeHttpResponse(envelope(licenseJson(progress: 80)));
|
|
});
|
|
final client = ApiClient(
|
|
baseUrl: 'http://127.0.0.1:8081',
|
|
adapter: adapter,
|
|
);
|
|
final api = DownloadApi(client);
|
|
|
|
final policy = await api.policy();
|
|
await api.updateSettings(wifiOnly: false, maxQuality: '1080P');
|
|
final granted = await api.grant('2111012753262120964');
|
|
final listed = await api.list();
|
|
final refreshed = await api.refreshUrl('2111012753262120964');
|
|
final progress = await api.reportProgress(
|
|
'2111012753262120964',
|
|
status: 'DOWNLOADING',
|
|
progressPercent: 80,
|
|
);
|
|
await api.remove('2111012753262120964');
|
|
final cleared = await api.clear();
|
|
|
|
expect(policy.remaining, 19);
|
|
expect(policy.expirePolicyText, '30 days');
|
|
expect(granted.licenseId, '2111012753262120962');
|
|
expect(granted.dramaId, '2111012753262120963');
|
|
expect(granted.episodeId, '2111012753262120964');
|
|
expect(listed.single.taskStatus, 'DOWNLOADING');
|
|
expect(refreshed.taskStatus, 'DOWNLOADING');
|
|
expect(progress.progressPercent, 80);
|
|
expect(cleared, 2);
|
|
|
|
expect(
|
|
adapter.requests.map(
|
|
(request) => '${request.method} ${request.uri.path}',
|
|
),
|
|
[
|
|
'GET /app/downloads/policy',
|
|
'PUT /app/downloads/settings',
|
|
'POST /app/downloads',
|
|
'GET /app/downloads',
|
|
'GET /app/downloads/2111012753262120964/url',
|
|
'POST /app/downloads/2111012753262120964/progress',
|
|
'DELETE /app/downloads/2111012753262120964',
|
|
'DELETE /app/downloads',
|
|
],
|
|
);
|
|
expect(adapter.requests[2].data, {'episodeId': '2111012753262120964'});
|
|
expect(adapter.requests[1].data, {
|
|
'wifiOnly': false,
|
|
'maxQuality': '1080P',
|
|
});
|
|
},
|
|
);
|
|
}
|