Chat list: spinner and background-task badge from server activity on every row
The list applies chat_activity pushes and each row's activity by seq, so every listed chat shows a running turn, not only the chat open here. An idle chat with running background tasks wears a quiet badge with tooltip and semantics "Background tasks running (N)". Older servers keep the old behaviour. List rows that never changed carry the server's current seq so they order after snapshots kept from an earlier server process.
This commit is contained in:
@@ -0,0 +1,264 @@
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/api_gateway.dart';
|
||||
import 'package:pipelines_app/features/chat/data/chat_repository.dart';
|
||||
import 'package:pipelines_app/features/chat/domain/chat_list_activity.dart';
|
||||
import 'package:pipelines_app/features/chat/ui/sessions/chat_sessions_controller.dart';
|
||||
import 'package:pipelines_app/features/chat/ui/sessions/chat_sessions_screen.dart';
|
||||
|
||||
import 'chat_test_harness.dart';
|
||||
import 'fake_chat_gateway.dart';
|
||||
|
||||
/// The list shows every chat's live activity from the server: a spinner while
|
||||
/// a turn runs, a quieter badge while only background tasks run, nothing when
|
||||
/// idle. `chat_activity` pushes reach chats this device never opened, and an
|
||||
/// older server (no `activity`) leaves the list as before.
|
||||
JsonMap _row(String id, {JsonMap? activity}) => {
|
||||
'id': id,
|
||||
'title': 'Chat $id',
|
||||
'directory': '/w',
|
||||
'updated_at': 1000,
|
||||
'activity': ?activity,
|
||||
};
|
||||
|
||||
JsonMap _activity({bool turn = false, int tasks = 0, int seq = 1}) => {
|
||||
if (turn) 'turn_running': true,
|
||||
if (tasks > 0) 'background_tasks': tasks,
|
||||
'seq': seq,
|
||||
};
|
||||
|
||||
JsonMap _push(
|
||||
String id, {
|
||||
bool turn = false,
|
||||
int tasks = 0,
|
||||
required int seq,
|
||||
}) => {
|
||||
'type': 'chat_activity',
|
||||
'chat_activity': {
|
||||
'session_id': id,
|
||||
..._activity(turn: turn, tasks: tasks, seq: seq),
|
||||
},
|
||||
};
|
||||
|
||||
/// The row merges its semantics, so the badge's label is part of the row's.
|
||||
Finder _label(int tasks) => find.bySemanticsLabel(
|
||||
RegExp(RegExp.escape('Background tasks running ($tasks)')),
|
||||
);
|
||||
|
||||
Finder _badge(String id) => find.descendant(
|
||||
of: find.byKey(Key('chat-row-$id')),
|
||||
matching: find.byKey(const Key('chat-row-background')),
|
||||
);
|
||||
|
||||
Finder _spinner(String id) => find.descendant(
|
||||
of: find.byKey(Key('chat-row-$id')),
|
||||
matching: find.byKey(const Key('chat-row-working')),
|
||||
);
|
||||
|
||||
Future<void> _pump(
|
||||
WidgetTester tester,
|
||||
FakeChatGateway fake, {
|
||||
bool cupertino = false,
|
||||
}) async {
|
||||
final screen = ChatSessionsScreen(
|
||||
gateway: fake,
|
||||
onOpen: (_) {},
|
||||
localStore: MemoryChatSessionLocalStore(),
|
||||
);
|
||||
await tester.pumpWidget(
|
||||
cupertino
|
||||
? CupertinoApp(home: screen)
|
||||
: MaterialApp(
|
||||
theme: ThemeData(platform: TargetPlatform.android),
|
||||
home: screen,
|
||||
),
|
||||
);
|
||||
// A spinner never settles, so pump a few frames instead.
|
||||
for (var i = 0; i < 5; i++) {
|
||||
await tester.pump(const Duration(milliseconds: 50));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> _frames(WidgetTester tester) async {
|
||||
await tester.pump();
|
||||
await tester.pump();
|
||||
}
|
||||
|
||||
void main() {
|
||||
testWidgets(
|
||||
'listed activity draws a spinner, a background badge or nothing',
|
||||
(tester) async {
|
||||
final fake = FakeChatGateway()
|
||||
..sessionsReply = {
|
||||
'chat_sessions': [
|
||||
_row('run', activity: _activity(turn: true, tasks: 1)),
|
||||
_row('bg', activity: _activity(tasks: 2)),
|
||||
_row('idle', activity: _activity()),
|
||||
],
|
||||
};
|
||||
await _pump(tester, fake);
|
||||
|
||||
expect(_spinner('run'), findsOneWidget);
|
||||
expect(_badge('run'), findsNothing, reason: 'the turn spinner wins');
|
||||
expect(_spinner('bg'), findsNothing);
|
||||
expect(_badge('bg'), findsOneWidget);
|
||||
expect(_spinner('idle'), findsNothing);
|
||||
expect(_badge('idle'), findsNothing);
|
||||
expect(_label(2), findsOneWidget);
|
||||
final tooltip = tester.widget<Tooltip>(_badge('bg'));
|
||||
expect(tooltip.message, 'Background tasks running (2)');
|
||||
expect(tooltip.triggerMode, TooltipTriggerMode.manual);
|
||||
expect(
|
||||
find.descendant(of: _badge('bg'), matching: find.byIcon(Icons.pending)),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(
|
||||
find.descendant(
|
||||
of: _badge('bg'),
|
||||
matching: find.byType(CircularProgressIndicator),
|
||||
),
|
||||
findsNothing,
|
||||
reason: 'the background badge is not a second spinner',
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets('pushes move a chat this device never opened', (tester) async {
|
||||
final fake = FakeChatGateway()
|
||||
..sessionsReply = {
|
||||
'chat_sessions': [_row('s1', activity: _activity(seq: 10))],
|
||||
};
|
||||
await _pump(tester, fake);
|
||||
expect(_spinner('s1'), findsNothing);
|
||||
|
||||
fake.pushed.add(_push('s1', turn: true, seq: 11));
|
||||
await _frames(tester);
|
||||
expect(_spinner('s1'), findsOneWidget);
|
||||
|
||||
fake.pushed.add(_push('s1', tasks: 3, seq: 13));
|
||||
await _frames(tester);
|
||||
expect(_spinner('s1'), findsNothing);
|
||||
expect(_label(3), findsOneWidget);
|
||||
|
||||
// A push older than the snapshot already applied is ignored.
|
||||
fake.pushed.add(_push('s1', turn: true, seq: 12));
|
||||
await _frames(tester);
|
||||
expect(_spinner('s1'), findsNothing);
|
||||
expect(_badge('s1'), findsOneWidget);
|
||||
|
||||
fake.pushed.add(_push('s1', seq: 14));
|
||||
await _frames(tester);
|
||||
expect(_badge('s1'), findsNothing);
|
||||
expect(_spinner('s1'), findsNothing);
|
||||
expect(fake.count('chat_sessions'), 1);
|
||||
});
|
||||
|
||||
testWidgets('an older server without activity keeps the old spinner', (
|
||||
tester,
|
||||
) async {
|
||||
final fake = FakeChatGateway()
|
||||
..sessionsReply = {
|
||||
'chat_sessions': [_row('s1')],
|
||||
};
|
||||
await _pump(tester, fake);
|
||||
expect(_badge('s1'), findsNothing);
|
||||
expect(_spinner('s1'), findsNothing);
|
||||
fake.pushed.add({
|
||||
'type': 'chat_event',
|
||||
'chat_event': {
|
||||
'session_id': 's1',
|
||||
'kind': 'step',
|
||||
'step': {'status': 'started'},
|
||||
},
|
||||
});
|
||||
await _frames(tester);
|
||||
expect(_spinner('s1'), findsOneWidget);
|
||||
expect(_badge('s1'), findsNothing);
|
||||
});
|
||||
|
||||
testWidgets('the Cupertino host draws its own glyph', (tester) async {
|
||||
final fake = FakeChatGateway()
|
||||
..sessionsReply = {
|
||||
'chat_sessions': [_row('bg', activity: _activity(tasks: 1))],
|
||||
};
|
||||
await _pump(tester, fake, cupertino: true);
|
||||
expect(
|
||||
find.descendant(
|
||||
of: _badge('bg'),
|
||||
matching: find.byIcon(CupertinoIcons.ellipsis_circle_fill),
|
||||
),
|
||||
findsOneWidget,
|
||||
);
|
||||
expect(_label(1), findsOneWidget);
|
||||
});
|
||||
|
||||
group('controller', () {
|
||||
late FakeChatGateway fake;
|
||||
late ChatSessionsController controller;
|
||||
late List<JsonMap> rows;
|
||||
|
||||
setUp(() {
|
||||
fake = FakeChatGateway();
|
||||
fake.sessionsHandler = () async => {'chat_sessions': rows};
|
||||
controller = ChatSessionsController(
|
||||
ChatRepository(fake),
|
||||
localStore: MemoryChatSessionLocalStore(),
|
||||
);
|
||||
});
|
||||
tearDown(() => controller.dispose());
|
||||
|
||||
JsonMap row() => controller.sessions.single;
|
||||
|
||||
test('a list read older than an applied push keeps the push', () async {
|
||||
rows = [_row('s1', activity: _activity(seq: 5))];
|
||||
await controller.refresh();
|
||||
fake.pushed.add(_push('s1', tasks: 2, seq: 7));
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
expect(chatSessionBackgroundTasks(row()), 2);
|
||||
|
||||
rows = [_row('s1', activity: _activity(seq: 6))];
|
||||
await controller.refresh();
|
||||
expect(chatSessionBackgroundTasks(row()), 2);
|
||||
expect(row()['running'], false);
|
||||
});
|
||||
|
||||
test('a newer list read (a reconnect) replaces the pushed state', () async {
|
||||
rows = [_row('s1', activity: _activity(seq: 5))];
|
||||
await controller.refresh();
|
||||
fake.pushed.add(_push('s1', turn: true, tasks: 2, seq: 7));
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
expect(row()['running'], true);
|
||||
|
||||
rows = [_row('s1', activity: _activity(seq: 90))];
|
||||
await controller.refresh();
|
||||
expect(row()['running'], false);
|
||||
expect(chatSessionBackgroundTasks(row()), 0);
|
||||
});
|
||||
});
|
||||
|
||||
test('chatListActivityOf reads rows and pushes and refuses malformed', () {
|
||||
expect(
|
||||
chatListActivityOf({
|
||||
'turn_running': true,
|
||||
'background_tasks': 2,
|
||||
'seq': 9,
|
||||
}, sessionId: 's1'),
|
||||
(sessionId: 's1', turnRunning: true, backgroundTasks: 2, seq: 9),
|
||||
);
|
||||
expect(chatListActivityOf({'session_id': 's2'}), (
|
||||
sessionId: 's2',
|
||||
turnRunning: false,
|
||||
backgroundTasks: 0,
|
||||
seq: 0,
|
||||
));
|
||||
expect(chatListActivityOf({'background_tasks': 1}), isNull);
|
||||
expect(chatListActivityOf('nope', sessionId: 's1'), isNull);
|
||||
expect(
|
||||
chatListActivityOf({
|
||||
'background_tasks': -4,
|
||||
}, sessionId: 's1')?.backgroundTasks,
|
||||
0,
|
||||
);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user