fix(chat): fix ChatBloc event callback and test reliability

- Fix onEvent callback initialization in ChatBloc constructor
- Add MockAgUiService to isolate test from mock API behavior
- Remove unnecessary non-null assertions in tests
This commit is contained in:
qzl
2026-02-28 14:41:21 +08:00
parent 92781ddbbe
commit d37677c533
9 changed files with 254 additions and 217 deletions
+16 -8
View File
@@ -2,14 +2,22 @@ import 'package:bloc_test/bloc_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:social_app/features/chat/data/models/ag_ui_event.dart';
import 'package:social_app/features/chat/data/models/chat_list_item.dart';
import 'package:social_app/features/chat/data/services/ag_ui_service.dart';
import 'package:social_app/features/chat/presentation/bloc/chat_bloc.dart';
class MockAgUiService extends AgUiService {
MockAgUiService() : super(onEvent: (_) {});
@override
Future<void> sendMessage(String content) async {}
}
void main() {
late ChatBloc chatBloc;
late AgUiService service;
setUp(() {
service = AgUiService();
service = MockAgUiService();
chatBloc = ChatBloc(service: service);
});
@@ -49,7 +57,7 @@ void main() {
build: () => chatBloc,
act: (bloc) {
bloc.emit(chatBloc.state.copyWith(isLoading: true));
service.onEvent!(
service.onEvent(
TextMessageStartEvent(messageId: 'msg_1', role: 'assistant'),
);
},
@@ -86,7 +94,7 @@ void main() {
currentMessageId: 'msg_1',
),
act: (bloc) {
service.onEvent!(
service.onEvent(
TextMessageContentEvent(messageId: 'msg_1', delta: 'Hello'),
);
},
@@ -115,7 +123,7 @@ void main() {
currentMessageId: 'msg_1',
),
act: (bloc) {
service.onEvent!(TextMessageEndEvent(messageId: 'msg_1'));
service.onEvent(TextMessageEndEvent(messageId: 'msg_1'));
},
expect: () => [
isA<ChatState>()
@@ -132,7 +140,7 @@ void main() {
'runStarted sets isLoading to true',
build: () => chatBloc,
act: (bloc) {
service.onEvent!(RunStartedEvent(threadId: 't1', runId: 'r1'));
service.onEvent(RunStartedEvent(threadId: 't1', runId: 'r1'));
},
expect: () => [
isA<ChatState>()
@@ -146,7 +154,7 @@ void main() {
build: () => chatBloc,
seed: () => const ChatState(isLoading: true),
act: (bloc) {
service.onEvent!(RunFinishedEvent(threadId: 't1', runId: 'r1'));
service.onEvent(RunFinishedEvent(threadId: 't1', runId: 'r1'));
},
expect: () => [
isA<ChatState>()
@@ -160,7 +168,7 @@ void main() {
build: () => chatBloc,
seed: () => const ChatState(isLoading: true),
act: (bloc) {
service.onEvent!(
service.onEvent(
RunErrorEvent(message: 'Something went wrong', code: 'ERR'),
);
},
@@ -183,7 +191,7 @@ void main() {
'toolCallStart adds ToolCallItem',
build: () => chatBloc,
act: (bloc) {
service.onEvent!(
service.onEvent(
ToolCallStartEvent(
toolCallId: 'tc_1',
toolCallName: 'create_calendar_event',