Files
pipelines/app/test/features/chat/conversation/chat_mode_control_test.dart
T

185 lines
5.8 KiB
Dart

import 'dart:async';
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/core/transport/pipelines_client.dart';
import 'package:pipelines_app/features/chat/conversation/chat_mode_control.dart';
class _ModeGateway implements ApiGateway {
_ModeGateway({this.current = '', this.refuse, this.supported = true});
String current;
final String? refuse;
final bool supported;
final requests = <JsonMap>[];
@override
Stream<JsonMap> get frames => const Stream.empty();
@override
Future<JsonMap> call(String type, [JsonMap fields = const {}]) async {
requests.add({'type': type, ...fields});
if (!supported) throw ProtocolError('unknown request type');
if (type == 'chat_modes') {
return {
'chat_modes': [
{
'id': 'default',
'name': 'Ask for permission',
'description': 'Ask first.',
},
{'id': 'plan', 'name': 'Plan', 'description': 'Plan only.'},
{'id': 'auto', 'name': 'Auto', 'description': 'Act without asking.'},
],
'chat_mode': {'session_id': 's', 'mode': current},
};
}
if (type == 'chat_mode') {
final refused = refuse;
if (refused != null) throw ProtocolError(refused);
current = '${(fields['chat_mode'] as Map)['mode']}';
return {
'chat_mode': {'session_id': 's', 'mode': current},
};
}
return {};
}
}
Widget _material(
ApiGateway gateway, {
String mode = 'default',
String name = 'Ask for permission',
void Function(String, String)? onChanged,
}) => MaterialApp(
home: Scaffold(
appBar: AppBar(
actions: [
ChatModeControl(
gateway: gateway,
sessionId: 's',
mode: mode,
modeName: name,
onChanged: onChanged,
),
],
),
),
);
void main() {
testWidgets("shows the row's mode without a request and switches to Auto", (
tester,
) async {
final gateway = _ModeGateway();
final changes = <String>[];
await tester.pumpWidget(
_material(gateway, onChanged: (mode, name) => changes.add('$mode/$name')),
);
await tester.pumpAndSettle();
// The row names the mode; nothing is requested until the picker opens,
// so the serialized transport never delays the transcript.
expect(find.text('Ask for permission'), findsOneWidget);
expect(gateway.requests, isEmpty);
await tester.tap(find.byKey(const Key('chat-mode')));
await tester.pumpAndSettle();
expect(find.text('Act without asking.'), findsOneWidget);
await tester.tap(find.byKey(const ValueKey('chat-mode-option-auto')));
await tester.pumpAndSettle();
expect(find.byKey(const Key('chat-mode-sheet')), findsNothing);
expect(gateway.requests.last, {
'type': 'chat_mode',
'chat_mode': {'session_id': 's', 'mode': 'auto'},
});
expect(
find.descendant(
of: find.byKey(const Key('chat-mode')),
matching: find.text('Auto'),
),
findsOneWidget,
);
expect(changes, ['auto/Auto']);
});
testWidgets('the stored mode is shown and checked', (tester) async {
await tester.pumpWidget(
_material(
_ModeGateway(current: 'plan'),
mode: 'plan',
name: 'Plan',
),
);
await tester.pumpAndSettle();
expect(find.text('Plan'), findsOneWidget);
await tester.tap(find.byKey(const Key('chat-mode')));
await tester.pumpAndSettle();
expect(
find.descendant(
of: find.byKey(const ValueKey('chat-mode-option-plan')),
matching: find.byIcon(Icons.check),
),
findsOneWidget,
);
});
testWidgets('a refusal keeps the picker open with the reason', (
tester,
) async {
final gateway = _ModeGateway(refuse: 'mode switch refused');
await tester.pumpWidget(_material(gateway));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('chat-mode')));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const ValueKey('chat-mode-option-auto')));
await tester.pumpAndSettle();
expect(find.byKey(const Key('chat-mode-sheet')), findsOneWidget);
expect(find.textContaining('mode switch refused'), findsOneWidget);
expect(gateway.current, '');
});
testWidgets('a row without a mode shows no control', (tester) async {
await tester.pumpWidget(_material(_ModeGateway(), mode: '', name: ''));
await tester.pumpAndSettle();
expect(find.byKey(const Key('chat-mode')), findsNothing);
});
testWidgets('a failed mode list is reported in the picker', (tester) async {
await tester.pumpWidget(_material(_ModeGateway(supported: false)));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('chat-mode')));
await tester.pumpAndSettle();
expect(find.textContaining('unknown request type'), findsOneWidget);
});
testWidgets('Cupertino uses a native action sheet', (tester) async {
final gateway = _ModeGateway();
await tester.pumpWidget(
CupertinoApp(
home: CupertinoPageScaffold(
navigationBar: CupertinoNavigationBar(
trailing: ChatModeControl(
gateway: gateway,
sessionId: 's',
mode: 'default',
modeName: 'Ask for permission',
),
),
child: const SizedBox.shrink(),
),
),
);
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('chat-mode')));
await tester.pumpAndSettle();
expect(find.byType(CupertinoActionSheet), findsOneWidget);
await tester.tap(find.byKey(const ValueKey('chat-mode-option-auto')));
await tester.pumpAndSettle();
expect(find.byType(CupertinoActionSheet), findsNothing);
expect(gateway.current, 'auto');
});
}