forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat_scroll_layout_test.dart
More file actions
207 lines (187 loc) · 6.51 KB
/
Copy pathchat_scroll_layout_test.dart
File metadata and controls
207 lines (187 loc) · 6.51 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
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:provider/provider.dart';
import 'package:omi/backend/schema/message.dart';
import 'package:omi/l10n/app_localizations.dart';
import 'package:omi/pages/chat/chat_scroll_policy.dart';
import 'package:omi/pages/chat/widgets/ai_message.dart';
import 'package:omi/providers/connectivity_provider.dart';
import 'package:omi/providers/conversation_provider.dart';
void main() {
ServerMessage messageWithMemories({int memoryCount = 5}) {
final createdAt = DateTime.parse('2026-08-23T12:00:00Z');
final memories = List<MessageConversation>.generate(
memoryCount,
(index) => MessageConversation(
'convo-$index',
createdAt,
MessageConversationStructured('Conversation $index', '🧠'),
),
);
return ServerMessage(
'ai-1',
createdAt,
'You talked about shipping the citation fix. What is up.',
MessageSender.ai,
MessageType.text,
null,
false,
const [],
const [],
memories,
);
}
test(
'continued drag while already free-scrolling does not request a rebuild',
() {
expect(
ChatScrollPolicy.nextMode(
current: ChatScrollMode.freeScrolling,
isUserOrDragScroll: true,
atLiveEdge: false,
),
isNull,
);
},
);
test('first user drag while following requests free-scrolling once', () {
expect(
ChatScrollPolicy.nextMode(
current: ChatScrollMode.followingBottom,
isUserOrDragScroll: true,
atLiveEdge: false,
),
ChatScrollMode.freeScrolling,
);
});
test('transcript bottom padding does not depend on scroll mode', () {
expect(ChatScrollPolicy.transcriptBottomPadding, 72);
});
testWidgets('a drag while following rebuilds the transcript at most once', (
tester,
) async {
final rebuilds = <int>[];
await tester.pumpWidget(
MaterialApp(
home: _ScrollHost(
onRebuild: () => rebuilds.add(1),
child: ListView(
children: List.generate(
30,
(i) => SizedBox(height: 80, child: Text('row $i')),
),
),
),
),
);
await tester.drag(find.byType(ListView), const Offset(0, -120));
await tester.pump();
expect(rebuilds.length, lessThanOrEqualTo(1));
});
testWidgets(
'citation titles and heights survive a ListView drag under keyboard inset',
(tester) async {
tester.view.physicalSize = const Size(390, 844);
tester.view.devicePixelRatio = 1.0;
tester.view.viewInsets = const FakeViewPadding(bottom: 336);
addTearDown(tester.view.resetPhysicalSize);
addTearDown(tester.view.resetDevicePixelRatio);
addTearDown(tester.view.resetViewInsets);
final conversationProvider = ConversationProvider(
isSignedIn: () => false,
);
addTearDown(conversationProvider.dispose);
final message = messageWithMemories();
await tester.pumpWidget(
MultiProvider(
providers: [
ChangeNotifierProvider.value(value: conversationProvider),
ChangeNotifierProvider(create: (_) => ConnectivityProvider()),
],
child: MaterialApp(
theme: ThemeData.dark(),
localizationsDelegates: AppLocalizations.localizationsDelegates,
supportedLocales: AppLocalizations.supportedLocales,
home: Scaffold(
body: _ScrollHost(
child: ListView.builder(
padding: const EdgeInsets.fromLTRB(
18,
16,
18,
ChatScrollPolicy.transcriptBottomPadding,
),
itemCount: 4,
itemBuilder: (context, index) {
return Padding(
key: ValueKey('msg-$index'),
padding: const EdgeInsets.only(top: 16),
child: AIMessage(
message: message,
sendMessage: (_) {},
displayOptions: false,
updateConversation: (_) {},
setMessageNps: (int value, {String? reason}) {},
),
);
},
),
),
),
),
),
);
await tester.pump();
final citation = find.byKey(const ValueKey('chat-citation-convo-0')).first;
expect(citation, findsOneWidget);
final before = tester.getSize(citation);
expect(before.height, greaterThanOrEqualTo(40));
expect(find.textContaining('Conversation 0'), findsWidgets);
expect(find.textContaining('What is up'), findsWidgets);
await tester.drag(find.byType(ListView), const Offset(0, -24));
await tester.pump();
expect(find.byKey(const ValueKey('chat-citation-convo-0')), findsWidgets);
final after = tester.getSize(
find.byKey(const ValueKey('chat-citation-convo-0')).first,
);
expect(after.height, greaterThanOrEqualTo(40));
expect(after.height, closeTo(before.height, 1));
expect(find.textContaining('Conversation 0'), findsWidgets);
expect(find.textContaining('What is up'), findsWidgets);
},
);
}
class _ScrollHost extends StatefulWidget {
const _ScrollHost({required this.child, this.onRebuild});
final Widget child;
final VoidCallback? onRebuild;
@override
State<_ScrollHost> createState() => _ScrollHostState();
}
class _ScrollHostState extends State<_ScrollHost> {
ChatScrollMode _mode = ChatScrollMode.followingBottom;
bool _onNotification(ScrollNotification notification) {
if (notification.depth != 0) return false;
final isUserScroll = notification is UserScrollNotification && notification.direction != ScrollDirection.idle;
final isDragScroll = notification is ScrollUpdateNotification && notification.dragDetails != null;
final next = ChatScrollPolicy.nextMode(
current: _mode,
isUserOrDragScroll: isUserScroll || isDragScroll,
atLiveEdge: ChatScrollPolicy.atLiveEdge(notification.metrics),
);
if (next == null) return false;
setState(() {
_mode = next;
widget.onRebuild?.call();
});
return false;
}
@override
Widget build(BuildContext context) {
return NotificationListener<ScrollNotification>(
onNotification: _onNotification,
child: widget.child,
);
}
}