forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat_content_blocks_test.dart
More file actions
289 lines (251 loc) · 10.5 KB
/
Copy pathchat_content_blocks_test.dart
File metadata and controls
289 lines (251 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:provider/provider.dart';
import 'package:omi/backend/http/api/messages.dart';
import 'package:omi/backend/schema/action_item.dart';
import 'package:omi/backend/schema/gen/action_items_folders_wire.g.dart' as wire;
import 'package:omi/backend/schema/memory.dart';
import 'package:omi/backend/schema/message.dart';
import 'package:omi/l10n/app_localizations.dart';
import 'package:omi/pages/chat/widgets/ai_message.dart';
import 'package:omi/providers/action_items_provider.dart';
import 'package:omi/providers/connectivity_provider.dart';
import 'package:omi/providers/conversation_provider.dart';
import 'package:omi/providers/goals_provider.dart';
import 'package:omi/providers/memories_provider.dart';
import 'package:omi/providers/message_provider.dart';
/// Records the single task-mutation path instead of hitting the network.
class _RecordingActionItemsProvider extends ActionItemsProvider {
_RecordingActionItemsProvider(this._items)
: super(
getActionItems: ({
int limit = 100,
int offset = 0,
bool? completed,
String? conversationId,
DateTime? startDate,
DateTime? endDate,
}) async =>
const wire.GeneratedActionItemsResponse(actionItems: []),
);
final List<ActionItemWithMetadata> _items;
final List<(String, bool)> updates = [];
@override
List<ActionItemWithMetadata> get actionItems => _items;
@override
bool get isLoading => false;
@override
Future<void> ensureLoaded({bool showShimmer = false}) async {}
@override
Future<void> updateActionItemState(ActionItemWithMetadata item, bool newState) async {
updates.add((item.id, newState));
notifyListeners();
}
}
/// Records what the question card asked the chat to send.
class _RecordingMessageProvider extends MessageProvider {
final List<String> sent = [];
@override
Future sendMessageStreamToServer(String text, {ChatPageContext? context}) async {
sent.add(text);
}
}
class _StubMemoriesProvider extends MemoriesProvider {
_StubMemoriesProvider(this._memories);
final List<Memory> _memories;
@override
List<Memory> get memories => _memories;
@override
bool get loading => false;
@override
Future<void> loadMemories({int limit = 100}) async {}
}
class _StubGoalsProvider extends GoalsProvider {
@override
bool get isLoading => false;
}
void main() {
ActionItemWithMetadata task({required String id, bool completed = false}) {
return wire.GeneratedActionItemResponse(
id: id,
description: 'Send the launch email',
completed: completed,
);
}
ServerMessage messageWithBlocks({String? selectedOptionId}) {
return ServerMessage(
'ai-1',
DateTime.parse('2026-09-01T12:00:00Z'),
'Here is what I found.',
MessageSender.ai,
MessageType.text,
null,
false,
const [],
const [],
const [],
contentBlocks: [
{'type': 'text', 'id': 'block-text', 'text': 'Here is what I found.'},
{'type': 'taskCard', 'id': 'block-task', 'taskId': 'task-1'},
{'type': 'goalLink', 'id': 'block-goal', 'goalId': 'goal-1', 'summary': 'Ship the release'},
{
'type': 'captureLink',
'id': 'block-capture',
'conversationId': 'conversation-1',
'summary': 'Monday standup',
},
{
'type': 'conversationLink',
'id': 'block-conversation',
'conversationId': 'conversation-2',
'summary': 'Weekly planning',
'recommendedActionItems': [
{'description': 'Draft the launch plan'},
],
},
{'type': 'memoryLink', 'id': 'block-memory', 'memoryId': 'memory-1', 'summary': 'Prefers dark mode'},
{
'type': 'questionCard',
'id': 'block-question',
'questionId': 'question-1',
'text': 'What should we do next?',
'subject': {'kind': 'goal', 'id': 'goal-1'},
'options': [
{'optionId': 'ship', 'label': 'Ship it', 'preparedAnswer': 'Ship it today'},
{'optionId': 'later', 'label': 'Ask me later', 'preparedAnswer': 'Remind me tomorrow', 'defer': true},
],
if (selectedOptionId != null) 'selectedOptionId': selectedOptionId,
},
],
);
}
Future<
(
_RecordingActionItemsProvider,
_RecordingMessageProvider,
)> pumpBlocks(
WidgetTester tester, {
required ServerMessage message,
List<ActionItemWithMetadata> tasks = const [],
}) async {
final actionItems = _RecordingActionItemsProvider(tasks);
final messages = _RecordingMessageProvider();
final conversations = ConversationProvider(isSignedIn: () => false);
addTearDown(conversations.dispose);
await tester.pumpWidget(
MultiProvider(
providers: [
ChangeNotifierProvider<ActionItemsProvider>.value(value: actionItems),
ChangeNotifierProvider<MessageProvider>.value(value: messages),
ChangeNotifierProvider<GoalsProvider>(create: (_) => _StubGoalsProvider()),
ChangeNotifierProvider<MemoriesProvider>(create: (_) => _StubMemoriesProvider(const [])),
ChangeNotifierProvider.value(value: conversations),
ChangeNotifierProvider(create: (_) => ConnectivityProvider()),
],
child: MaterialApp(
theme: ThemeData.dark(),
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
home: Scaffold(
body: SingleChildScrollView(
child: AIMessage(
message: message,
sendMessage: (text) => messages.sendMessageStreamToServer(text),
displayOptions: false,
updateConversation: (_) {},
setMessageNps: (int value, {String? reason}) {},
),
),
),
),
),
);
await tester.pump();
await tester.pump();
return (actionItems, messages);
}
testWidgets('every interactable block renders its keyed component', (tester) async {
await pumpBlocks(
tester,
message: messageWithBlocks(),
tasks: [task(id: 'task-1')],
);
expect(find.byKey(const Key('chat-block-taskCard-block-task')), findsOneWidget);
expect(find.byKey(const Key('chat-block-taskCard-block-task-toggle')), findsOneWidget);
// No goal is loaded, so the goal link shows its unavailable state rather
// than a button that cannot resolve.
expect(find.byKey(const Key('chat-block-goalLink-block-goal-unavailable')), findsOneWidget);
expect(find.byKey(const Key('chat-block-captureLink-block-capture')), findsOneWidget);
expect(find.byKey(const Key('chat-block-captureLink-block-capture-open')), findsOneWidget);
expect(find.byKey(const Key('chat-block-conversationLink-block-conversation')), findsOneWidget);
expect(find.byKey(const Key('chat-block-conversationLink-block-conversation-open')), findsOneWidget);
expect(find.byKey(const Key('chat-block-memoryLink-block-memory-unavailable')), findsOneWidget);
expect(find.byKey(const Key('chat-block-questionCard-block-question')), findsOneWidget);
// Message text is preserved beside the components.
expect(find.textContaining('Here is what I found.'), findsWidgets);
// Conversation link's recommended items are listed.
expect(find.text('Draft the launch plan'), findsOneWidget);
// Task description comes from the resolved task, not the block.
expect(find.text('Send the launch email'), findsOneWidget);
});
testWidgets('an unresolved task shows the unavailable state', (tester) async {
await pumpBlocks(tester, message: messageWithBlocks());
expect(find.byKey(const Key('chat-block-taskCard-block-task-unavailable')), findsOneWidget);
expect(find.byKey(const Key('chat-block-taskCard-block-task-toggle')), findsNothing);
});
testWidgets('tapping the task checkbox toggles it through the tasks provider', (tester) async {
final (actionItems, _) = await pumpBlocks(
tester,
message: messageWithBlocks(),
tasks: [task(id: 'task-1')],
);
final toggle = find.byKey(const Key('chat-block-taskCard-block-task-toggle'));
await tester.ensureVisible(toggle);
await tester.tap(toggle);
await tester.pump();
expect(actionItems.updates, [('task-1', true)]);
});
testWidgets('tapping a question option sends its prepared answer', (tester) async {
final (_, messages) = await pumpBlocks(tester, message: messageWithBlocks());
final option = find.byKey(const Key('chat-block-questionCard-block-question-option-ship'));
await tester.ensureVisible(option);
await tester.tap(option);
await tester.pump();
expect(messages.sent, ['Ship it today']);
});
testWidgets('a deferral option sends its prepared answer too', (tester) async {
final (_, messages) = await pumpBlocks(tester, message: messageWithBlocks());
final option = find.byKey(const Key('chat-block-questionCard-block-question-option-later'));
await tester.ensureVisible(option);
await tester.tap(option);
await tester.pump();
expect(messages.sent, ['Remind me tomorrow']);
});
testWidgets('an answered question keeps only the chosen option, disabled', (tester) async {
final (_, messages) = await pumpBlocks(
tester,
message: messageWithBlocks(selectedOptionId: 'ship'),
);
expect(find.byKey(const Key('chat-block-questionCard-block-question-option-later')), findsNothing);
final chosen = find.byKey(const Key('chat-block-questionCard-block-question-option-ship'));
expect(chosen, findsOneWidget);
expect(tester.widget<OutlinedButton>(chosen).onPressed, isNull);
expect(messages.sent, isEmpty);
});
testWidgets('a chrome-only message renders components instead of its fallback dump', (tester) async {
final message = ServerMessage.fromJson({
'id': 'ai-2',
'created_at': '2026-09-01T12:00:00Z',
'text': '',
'sender': 'ai',
'type': 'text',
'content_blocks': [
{'type': 'goalLink', 'id': 'block-goal', 'goalId': 'goal-1', 'summary': 'Ship the release'},
{'type': 'taskCard', 'id': 'block-task', 'taskId': 'task-1'},
],
});
await pumpBlocks(tester, message: message, tasks: [task(id: 'task-1')]);
expect(find.byKey(const Key('chat-block-taskCard-block-task')), findsOneWidget);
expect(find.textContaining('Goal - Ship the release'), findsNothing);
});
}