feat(routing): 客户端 model + AccountApi + AsyncNotifier provider
- RoutingProfile/RoutingRule/Builtin 手写 fromJson/toJson/copyWith,对齐服务端 GET/POST /v1/me/routing 契约;enabled 缺省容错默认 true,toJson 恒输出该字段 - AccountApi.routingProfile()/saveRoutingProfile() 封装两端点 - RoutingProfileNotifier(AsyncNotifier):addRule/removeRule/updateRule/reorder/ setMode/setBuiltin 均本地乐观更新后落盘;save() 失败整体回退到变更前 state 并 rethrow(不静默吞,交调用方处理)——Riverpod asyncTransition 的 seamless copyWithPrevious 会用当前已存 state 覆盖手动挂的错误值,故不用 AsyncError 路径,改走显式回退
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
// routing_profile_model_test.dart — RoutingProfile/RoutingRule JSON 无损往返。
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pangolin_vpn/models/routing_profile.dart';
|
||||
|
||||
void main() {
|
||||
test('RoutingProfile round-trips json', () {
|
||||
const j = {
|
||||
'mode': 'rule',
|
||||
'builtin': {'china_direct': true, 'lan_direct': true, 'private_via_tunnel': true},
|
||||
'rules': [
|
||||
{'type': 'domain_suffix', 'value': 'x.com', 'action': 'direct', 'note': 'ci', 'enabled': true}
|
||||
],
|
||||
'final': 'proxy',
|
||||
};
|
||||
final p = RoutingProfile.fromJson(j);
|
||||
expect(p.mode, 'rule');
|
||||
expect(p.rules.single.value, 'x.com');
|
||||
expect(p.toJson(), j); // 无损往返
|
||||
});
|
||||
|
||||
test('RoutingRule enabled 缺省容错默认 true', () {
|
||||
final r = RoutingRule.fromJson({'type': 'domain', 'value': 'a.com', 'action': 'proxy'});
|
||||
expect(r.enabled, isTrue);
|
||||
});
|
||||
|
||||
test('RoutingRule 构造默认 enabled=true 且 toJson 恒输出 enabled 字段', () {
|
||||
const r = RoutingRule(type: 'domain', value: 'a.com', action: 'proxy');
|
||||
expect(r.enabled, isTrue);
|
||||
expect(r.toJson().containsKey('enabled'), isTrue);
|
||||
expect(r.toJson()['enabled'], isTrue);
|
||||
});
|
||||
|
||||
test('RoutingProfile.fromJson 无档案默认(builtin 缺省)容错', () {
|
||||
final p = RoutingProfile.fromJson({'mode': 'global', 'rules': [], 'final': 'direct'});
|
||||
expect(p.builtin.chinaDirect, isTrue);
|
||||
expect(p.builtin.lanDirect, isTrue);
|
||||
expect(p.builtin.privateViaTunnel, isTrue);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
// routing_provider_test.dart — 分流档案 provider:乐观更新 + 保存调用 + 失败回退。
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pangolin_vpn/models/routing_profile.dart';
|
||||
import 'package:pangolin_vpn/services/account_api.dart';
|
||||
import 'package:pangolin_vpn/services/api_client.dart';
|
||||
import 'package:pangolin_vpn/services/token_store.dart';
|
||||
import 'package:pangolin_vpn/state/account_providers.dart';
|
||||
import 'package:pangolin_vpn/state/auth_provider.dart';
|
||||
import 'package:pangolin_vpn/state/routing_provider.dart';
|
||||
|
||||
/// 已登录的桩 TokenStore(无平台依赖,仿 nodes_provider_test 的 _NullTokenStore)。
|
||||
class _LoggedInTokenStore implements TokenStore {
|
||||
const _LoggedInTokenStore();
|
||||
@override
|
||||
Future<void> saveTokens({required String access, required String refresh}) async {}
|
||||
@override
|
||||
Future<String?> loadAccessToken() async => 'test-token';
|
||||
@override
|
||||
Future<String?> loadRefreshToken() async => 'test-refresh';
|
||||
@override
|
||||
Future<void> clear() async {}
|
||||
@override
|
||||
Future<void> markOnboarded() async {}
|
||||
@override
|
||||
Future<bool> isOnboarded() async => true;
|
||||
@override
|
||||
Future<void> saveLastEmail(String email) async {}
|
||||
@override
|
||||
Future<String?> loadLastEmail() async => null;
|
||||
}
|
||||
|
||||
/// 轮询等 authProvider 的异步 _loadFromStore() 落地已登录态。
|
||||
Future<void> _waitLoggedIn(ProviderContainer c) async {
|
||||
for (var i = 0; i < 60; i++) {
|
||||
if (c.read(authProvider).isLoggedIn) return;
|
||||
await Future<void>.delayed(const Duration(milliseconds: 10));
|
||||
}
|
||||
throw StateError('authProvider 未在预期时间内进入已登录态');
|
||||
}
|
||||
|
||||
/// 假 AccountApi:只桩 routingProfile()/saveRoutingProfile(),记录调用便于断言。
|
||||
class _FakeAccountApi extends AccountApi {
|
||||
_FakeAccountApi()
|
||||
: super(ApiClient(
|
||||
baseUrl: 'http://test.local',
|
||||
getToken: () => null,
|
||||
refresh: () async => false,
|
||||
));
|
||||
|
||||
RoutingProfile initial = const RoutingProfile();
|
||||
final List<RoutingProfile> saved = [];
|
||||
|
||||
/// 非 null 时 saveRoutingProfile 抛此异常(用于测失败回退)。
|
||||
Exception? saveError;
|
||||
|
||||
@override
|
||||
Future<RoutingProfile> routingProfile() async => initial;
|
||||
|
||||
@override
|
||||
Future<void> saveRoutingProfile(RoutingProfile p) async {
|
||||
final err = saveError;
|
||||
if (err != null) throw err;
|
||||
saved.add(p);
|
||||
}
|
||||
}
|
||||
|
||||
ProviderContainer _makeContainer(_FakeAccountApi api) => ProviderContainer(overrides: [
|
||||
tokenStoreProvider.overrideWithValue(const _LoggedInTokenStore()),
|
||||
accountApiProvider.overrideWithValue(api),
|
||||
]);
|
||||
|
||||
void main() {
|
||||
test('addRule 乐观更新 state 并调用 saveRoutingProfile', () async {
|
||||
final api = _FakeAccountApi();
|
||||
final c = _makeContainer(api);
|
||||
addTearDown(c.dispose);
|
||||
|
||||
await _waitLoggedIn(c);
|
||||
await c.read(routingProfileProvider.future); // 等初次加载
|
||||
|
||||
const rule = RoutingRule(type: 'domain_suffix', value: 'x.com', action: 'direct');
|
||||
await c.read(routingProfileProvider.notifier).addRule(rule);
|
||||
|
||||
final state = c.read(routingProfileProvider).value!;
|
||||
expect(state.rules, hasLength(1));
|
||||
expect(state.rules.single.value, 'x.com');
|
||||
expect(state.rules.single.action, 'direct');
|
||||
|
||||
expect(api.saved, hasLength(1), reason: '应调用 saveRoutingProfile 落盘');
|
||||
expect(api.saved.single.rules.single.value, 'x.com');
|
||||
});
|
||||
|
||||
test('removeRule / setMode / reorder 均乐观更新 + 落盘', () async {
|
||||
final api = _FakeAccountApi()
|
||||
..initial = const RoutingProfile(rules: [
|
||||
RoutingRule(type: 'domain', value: 'a.com', action: 'direct'),
|
||||
RoutingRule(type: 'domain', value: 'b.com', action: 'proxy'),
|
||||
]);
|
||||
final c = _makeContainer(api);
|
||||
addTearDown(c.dispose);
|
||||
await _waitLoggedIn(c);
|
||||
await c.read(routingProfileProvider.future);
|
||||
|
||||
await c.read(routingProfileProvider.notifier).setMode('global');
|
||||
expect(c.read(routingProfileProvider).value!.mode, 'global');
|
||||
|
||||
await c.read(routingProfileProvider.notifier).reorder(0, 2);
|
||||
expect(c.read(routingProfileProvider).value!.rules.map((r) => r.value).toList(),
|
||||
['b.com', 'a.com']);
|
||||
|
||||
await c.read(routingProfileProvider.notifier).removeRule(0);
|
||||
expect(c.read(routingProfileProvider).value!.rules.single.value, 'a.com');
|
||||
|
||||
expect(api.saved, hasLength(3));
|
||||
});
|
||||
|
||||
test('保存失败:回退到变更前 state 且 rethrow,不静默吞', () async {
|
||||
final api = _FakeAccountApi();
|
||||
final c = _makeContainer(api);
|
||||
addTearDown(c.dispose);
|
||||
await _waitLoggedIn(c);
|
||||
await c.read(routingProfileProvider.future);
|
||||
|
||||
final before = c.read(routingProfileProvider).value!;
|
||||
api.saveError = Exception('network down');
|
||||
|
||||
const rule = RoutingRule(type: 'domain', value: 'fail.com', action: 'proxy');
|
||||
await expectLater(
|
||||
c.read(routingProfileProvider.notifier).addRule(rule),
|
||||
throwsA(isA<Exception>()),
|
||||
);
|
||||
|
||||
// 不静默吞:异常已通过 expectLater 的 throwsA 断言被抛出给调用方。
|
||||
final after = c.read(routingProfileProvider);
|
||||
expect(after.hasError, isFalse, reason: '失败走「整体回退」而非挂错误态');
|
||||
expect(after.value, before, reason: '应回退到变更前 state');
|
||||
expect(api.saved, isEmpty);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user