101 lines
2.7 KiB
Dart
101 lines
2.7 KiB
Dart
|
|
/// Widget/单元测试共用替身(P3 计划 Task 3)。
|
|||
|
|
///
|
|||
|
|
/// 与 test/core/api_client_test.dart 中的手写打桩同款:FakeAdapter 为
|
|||
|
|
/// dio HttpClientAdapter,按路由分发预设响应并记录全部请求;未引入
|
|||
|
|
/// http_mock_adapter(零额外 dev 依赖,打桩面完全可控)。
|
|||
|
|
library;
|
|||
|
|
|
|||
|
|
import 'dart:async';
|
|||
|
|
import 'dart:convert';
|
|||
|
|
|
|||
|
|
import 'package:dio/dio.dart';
|
|||
|
|
import 'package:photo_live/core/session_store.dart';
|
|||
|
|
|
|||
|
|
/// 内存会话存储(测试替身)。
|
|||
|
|
class FakeSessionStore implements SessionStore {
|
|||
|
|
Session? session;
|
|||
|
|
|
|||
|
|
@override
|
|||
|
|
Future<Session?> read() async => session;
|
|||
|
|
|
|||
|
|
@override
|
|||
|
|
Future<void> save(Session value) async => session = value;
|
|||
|
|
|
|||
|
|
@override
|
|||
|
|
Future<void> clear() async => session = null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// 记录到适配器的请求快照。
|
|||
|
|
class RecordedRequest {
|
|||
|
|
const RecordedRequest({
|
|||
|
|
required this.method,
|
|||
|
|
required this.path,
|
|||
|
|
required this.headers,
|
|||
|
|
required this.body,
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
final String method;
|
|||
|
|
final String path;
|
|||
|
|
final Map<String, dynamic> headers;
|
|||
|
|
final Object? body;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// 手写 dio 打桩适配器:按路由分发预设响应并记录全部请求。
|
|||
|
|
///
|
|||
|
|
/// [router] 返回 `(status, body)`(可为 async 以模拟慢响应/加载态);
|
|||
|
|
/// body 为 null 表示无响应体(204)。
|
|||
|
|
class FakeAdapter implements HttpClientAdapter {
|
|||
|
|
FakeAdapter(this.router);
|
|||
|
|
|
|||
|
|
final FutureOr<(int, Object?)> Function(RequestOptions options) router;
|
|||
|
|
|
|||
|
|
final List<RecordedRequest> requests = <RecordedRequest>[];
|
|||
|
|
|
|||
|
|
@override
|
|||
|
|
Future<ResponseBody> fetch(
|
|||
|
|
RequestOptions options,
|
|||
|
|
Stream? requestStream,
|
|||
|
|
Future? cancelFuture,
|
|||
|
|
) async {
|
|||
|
|
requests.add(
|
|||
|
|
RecordedRequest(
|
|||
|
|
method: options.method,
|
|||
|
|
path: options.path,
|
|||
|
|
headers: Map<String, dynamic>.from(options.headers),
|
|||
|
|
body: options.data,
|
|||
|
|
),
|
|||
|
|
);
|
|||
|
|
final (status, body) = await router(options);
|
|||
|
|
if (body == null) {
|
|||
|
|
// 204 等:空体且不带 JSON content-type(transformer 不解码)。
|
|||
|
|
return ResponseBody.fromString('', status);
|
|||
|
|
}
|
|||
|
|
return ResponseBody.fromString(
|
|||
|
|
jsonEncode(body),
|
|||
|
|
status,
|
|||
|
|
headers: {
|
|||
|
|
Headers.contentTypeHeader: [Headers.jsonContentType],
|
|||
|
|
},
|
|||
|
|
);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@override
|
|||
|
|
void close({bool force = false}) {}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// 常用响应体:登录成功(openapi LoginResponse 形状)。
|
|||
|
|
Map<String, dynamic> loginOkBody({
|
|||
|
|
String token = 'tok-new',
|
|||
|
|
String expiresAt = '2026-09-20T12:00:00Z',
|
|||
|
|
}) => <String, dynamic>{
|
|||
|
|
'sessionToken': token,
|
|||
|
|
'expiresAt': expiresAt,
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
/// 常用响应体:契约 Error 形状。
|
|||
|
|
Map<String, dynamic> errorBody(String code) => <String, dynamic>{
|
|||
|
|
'code': code,
|
|||
|
|
'message': 'stub message',
|
|||
|
|
'requestId': 'req-stub',
|
|||
|
|
};
|