305 lines
8.2 KiB
Dart
305 lines
8.2 KiB
Dart
import 'dart:async';
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:flutter/widgets.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:social_app/core/chat/agent_stage.dart';
|
|
import 'package:social_app/core/chat/ag_ui_event.dart';
|
|
import 'package:social_app/core/chat/ag_ui_service.dart';
|
|
import 'package:social_app/core/chat/chat_api.dart';
|
|
import 'package:social_app/core/chat/chat_list_item.dart';
|
|
import 'package:social_app/core/l10n/l10n.dart';
|
|
import 'package:social_app/features/chat/presentation/bloc/chat_bloc.dart';
|
|
|
|
class _NoopChatApi implements ChatApi {
|
|
@override
|
|
Future<void> cancelRun({required String threadId, required String runId}) {
|
|
throw UnimplementedError();
|
|
}
|
|
|
|
@override
|
|
Future<Map<String, dynamic>> createRun(Map<String, dynamic> runInput) {
|
|
throw UnimplementedError();
|
|
}
|
|
|
|
@override
|
|
Future<Map<String, dynamic>> fetchHistory({
|
|
String? threadId,
|
|
DateTime? beforeDate,
|
|
}) {
|
|
throw UnimplementedError();
|
|
}
|
|
|
|
@override
|
|
Future<Uint8List> fetchAttachmentPreview(String previewPath) {
|
|
throw UnimplementedError();
|
|
}
|
|
|
|
@override
|
|
Future<Stream<String>> streamRunEvents(
|
|
String threadId, {
|
|
required String runId,
|
|
String? lastEventId,
|
|
}) {
|
|
throw UnimplementedError();
|
|
}
|
|
|
|
@override
|
|
Future<String> transcribeAudio(String filePath) {
|
|
throw UnimplementedError();
|
|
}
|
|
|
|
@override
|
|
Future<Map<String, dynamic>> uploadAttachment({
|
|
required String threadId,
|
|
required String filename,
|
|
required String mimeType,
|
|
required Uint8List bytes,
|
|
}) {
|
|
throw UnimplementedError();
|
|
}
|
|
}
|
|
|
|
class _FakeAgUiService extends AgUiService {
|
|
_FakeAgUiService() : super(chatApi: _NoopChatApi(), onEvent: (_) {});
|
|
|
|
Future<SendMessageResult> Function(
|
|
String content,
|
|
List<AttachmentUploadInput>? attachments,
|
|
)?
|
|
sendMessageHandler;
|
|
|
|
Future<HistorySnapshot> Function({DateTime? beforeDate, bool forceRefresh})?
|
|
loadHistoryHandler;
|
|
|
|
int loadHistoryCalls = 0;
|
|
|
|
void emitEventForTest(AgUiEvent event) {
|
|
onEvent(event);
|
|
}
|
|
|
|
@override
|
|
Future<SendMessageResult> sendMessage(
|
|
String content, {
|
|
List<AttachmentUploadInput>? attachments,
|
|
}) async {
|
|
final handler = sendMessageHandler;
|
|
if (handler == null) {
|
|
throw UnimplementedError();
|
|
}
|
|
return handler(content, attachments);
|
|
}
|
|
|
|
@override
|
|
Future<HistorySnapshot> loadHistory({
|
|
DateTime? beforeDate,
|
|
bool forceRefresh = false,
|
|
}) async {
|
|
loadHistoryCalls += 1;
|
|
final handler = loadHistoryHandler;
|
|
if (handler == null) {
|
|
throw UnimplementedError();
|
|
}
|
|
return handler(beforeDate: beforeDate, forceRefresh: forceRefresh);
|
|
}
|
|
|
|
@override
|
|
Future<void> setUserContext(String? userId) async {}
|
|
}
|
|
|
|
HistorySnapshot _snapshot(
|
|
List<HistoryMessage> messages, {
|
|
bool hasMore = false,
|
|
}) {
|
|
return HistorySnapshot(
|
|
scope: 'history_day',
|
|
threadId: 'thread-1',
|
|
day: '2026-03-30',
|
|
hasMore: hasMore,
|
|
messages: messages,
|
|
);
|
|
}
|
|
|
|
HistoryMessage _historyMessage({
|
|
required String id,
|
|
required int seq,
|
|
required String role,
|
|
required String content,
|
|
required DateTime timestamp,
|
|
}) {
|
|
return HistoryMessage(
|
|
id: id,
|
|
seq: seq,
|
|
role: role,
|
|
content: content,
|
|
timestamp: timestamp,
|
|
);
|
|
}
|
|
|
|
void main() {
|
|
setUp(() {
|
|
L10n.setLocale(const Locale('zh'));
|
|
});
|
|
|
|
test(
|
|
'loadHistory ignores stale result after switchUser epoch change',
|
|
() async {
|
|
final service = _FakeAgUiService();
|
|
final completer = Completer<HistorySnapshot>();
|
|
service.loadHistoryHandler =
|
|
({DateTime? beforeDate, bool forceRefresh = false}) {
|
|
return completer.future;
|
|
};
|
|
|
|
final bloc = ChatBloc(
|
|
service: service,
|
|
chatApi: _NoopChatApi(),
|
|
recoveryPollInterval: const Duration(milliseconds: 1),
|
|
recoveryTimeout: const Duration(milliseconds: 80),
|
|
);
|
|
|
|
final pendingLoad = bloc.loadHistory();
|
|
await bloc.switchUser('user-b');
|
|
completer.complete(
|
|
_snapshot([
|
|
_historyMessage(
|
|
id: 'old-1',
|
|
seq: 1,
|
|
role: 'assistant',
|
|
content: 'old session data',
|
|
timestamp: DateTime.now(),
|
|
),
|
|
]),
|
|
);
|
|
await pendingLoad;
|
|
|
|
expect(bloc.state.items, isEmpty);
|
|
expect(bloc.state.isLoadingHistory, isFalse);
|
|
},
|
|
);
|
|
|
|
test(
|
|
'sendMessage recovers from premature SSE close with polled history',
|
|
() async {
|
|
final service = _FakeAgUiService();
|
|
service.sendMessageHandler = (content, attachments) async {
|
|
throw StateError('SSE closed before terminal event for run');
|
|
};
|
|
|
|
var loadAttempt = 0;
|
|
service.loadHistoryHandler =
|
|
({DateTime? beforeDate, bool forceRefresh = false}) async {
|
|
loadAttempt += 1;
|
|
final now = DateTime.now();
|
|
if (loadAttempt == 1) {
|
|
return _snapshot([
|
|
_historyMessage(
|
|
id: 'db-user-1',
|
|
seq: 1,
|
|
role: 'user',
|
|
content: 'hello',
|
|
timestamp: now,
|
|
),
|
|
]);
|
|
}
|
|
return _snapshot([
|
|
_historyMessage(
|
|
id: 'db-user-1',
|
|
seq: 1,
|
|
role: 'user',
|
|
content: 'hello',
|
|
timestamp: now,
|
|
),
|
|
_historyMessage(
|
|
id: 'db-assistant-1',
|
|
seq: 2,
|
|
role: 'assistant',
|
|
content: 'world',
|
|
timestamp: now.add(const Duration(seconds: 1)),
|
|
),
|
|
]);
|
|
};
|
|
|
|
final bloc = ChatBloc(
|
|
service: service,
|
|
chatApi: _NoopChatApi(),
|
|
recoveryPollInterval: const Duration(milliseconds: 1),
|
|
recoveryTimeout: const Duration(milliseconds: 50),
|
|
);
|
|
await bloc.sendMessage('hello');
|
|
|
|
final userMessages = bloc.state.items
|
|
.whereType<TextMessageItem>()
|
|
.where((item) => item.sender == MessageSender.user)
|
|
.toList();
|
|
expect(userMessages.length, 1);
|
|
expect(userMessages.first.id, 'db-user-1');
|
|
expect(
|
|
bloc.state.items.any(
|
|
(item) =>
|
|
item is TextMessageItem &&
|
|
item.sender == MessageSender.ai &&
|
|
item.content == 'world',
|
|
),
|
|
isTrue,
|
|
);
|
|
expect(bloc.state.error, isNull);
|
|
expect(service.loadHistoryCalls, 2);
|
|
},
|
|
);
|
|
|
|
test('sendMessage reports error after recovery attempts exhausted', () async {
|
|
final service = _FakeAgUiService();
|
|
service.sendMessageHandler = (content, attachments) async {
|
|
throw StateError('SSE closed before terminal event for run');
|
|
};
|
|
service.loadHistoryHandler =
|
|
({DateTime? beforeDate, bool forceRefresh = false}) async {
|
|
final now = DateTime.now();
|
|
return _snapshot([
|
|
_historyMessage(
|
|
id: 'db-user-1',
|
|
seq: 1,
|
|
role: 'user',
|
|
content: 'hello',
|
|
timestamp: now,
|
|
),
|
|
]);
|
|
};
|
|
|
|
final bloc = ChatBloc(
|
|
service: service,
|
|
chatApi: _NoopChatApi(),
|
|
recoveryPollInterval: const Duration(milliseconds: 1),
|
|
recoveryTimeout: const Duration(milliseconds: 15),
|
|
);
|
|
await bloc.sendMessage('hello');
|
|
|
|
expect(bloc.state.error, L10n.current.chatSseInterruptedRetry);
|
|
expect(service.loadHistoryCalls, greaterThanOrEqualTo(1));
|
|
});
|
|
|
|
test(
|
|
'tracks hasSeenStep to distinguish requesting vs processing stage',
|
|
() async {
|
|
final service = _FakeAgUiService();
|
|
final bloc = ChatBloc(service: service, chatApi: _NoopChatApi());
|
|
|
|
service.emitEventForTest(
|
|
RunStartedEvent(threadId: 'thread-1', runId: 'run-1'),
|
|
);
|
|
expect(bloc.state.isWaitingFirstToken, isTrue);
|
|
expect(bloc.state.hasSeenStep, isFalse);
|
|
expect(bloc.state.currentStage, isNull);
|
|
|
|
service.emitEventForTest(StepStartedEvent(stepName: 'router'));
|
|
expect(bloc.state.hasSeenStep, isTrue);
|
|
expect(bloc.state.currentStage, AgentStage.routing);
|
|
|
|
service.emitEventForTest(StepFinishedEvent(stepName: 'router'));
|
|
expect(bloc.state.hasSeenStep, isTrue);
|
|
expect(bloc.state.currentStage, isNull);
|
|
},
|
|
);
|
|
}
|