Skip to content

Commit c72af24

Browse files
committed
test: add tests for chat viewmodel(cv: 50)
1 parent b2a346c commit c72af24

File tree

2 files changed

+730
-18
lines changed

2 files changed

+730
-18
lines changed

lib/dashbot/features/chat/viewmodel/chat_viewmodel.dart

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,8 @@ class ChatViewmodel extends StateNotifier<ChatState> {
5050
}) async {
5151
final ai = _selectedAIModel;
5252
if (text.trim().isEmpty && countAsUser) return;
53-
if (ai == null &&
54-
type != ChatMessageType.importCurl &&
55-
type != ChatMessageType.importOpenApi) {
56-
debugPrint('[Chat] No AI model configured');
57-
_appendSystem(
58-
'AI model is not configured. Please set one.',
59-
type,
60-
);
61-
return;
62-
}
6353

6454
final requestId = _currentRequest?.id ?? 'global';
65-
final existingMessages = state.chatSessions[requestId] ?? const [];
6655

6756
if (countAsUser) {
6857
_addMessage(
@@ -77,6 +66,19 @@ class ChatViewmodel extends StateNotifier<ChatState> {
7766
);
7867
}
7968

69+
if (ai == null &&
70+
type != ChatMessageType.importCurl &&
71+
type != ChatMessageType.importOpenApi) {
72+
debugPrint('[Chat] No AI model configured');
73+
_appendSystem(
74+
'AI model is not configured. Please set one.',
75+
type,
76+
);
77+
return;
78+
}
79+
80+
final existingMessages = state.chatSessions[requestId] ?? const [];
81+
8082
final lastSystemImport = existingMessages.lastWhere(
8183
(m) =>
8284
m.role == MessageRole.system &&
@@ -409,7 +411,6 @@ class ChatViewmodel extends StateNotifier<ChatState> {
409411
}
410412
}
411413

412-
413414
String sourceTitle = (payload['sourceName'] as String?) ?? '';
414415
if (sourceTitle.trim().isEmpty) {
415416
final specObj = payload['spec'];
@@ -931,12 +932,10 @@ class ChatViewmodel extends StateNotifier<ChatState> {
931932
// Helpers
932933
void _addMessage(String requestId, ChatMessage m) {
933934
final msgs = state.chatSessions[requestId] ?? const [];
934-
state = state.copyWith(
935-
chatSessions: {
936-
...state.chatSessions,
937-
requestId: [...msgs, m],
938-
},
939-
);
935+
final updatedSessions =
936+
Map<String, List<ChatMessage>>.from(state.chatSessions);
937+
updatedSessions[requestId] = [...msgs, m];
938+
state = state.copyWith(chatSessions: updatedSessions);
940939
}
941940

942941
void _appendSystem(String text, ChatMessageType type) {

0 commit comments

Comments
 (0)