416 lines
13 KiB
Dart
416 lines
13 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/features/chat/conversation/chat_permission_card.dart';
|
|
import 'package:pipelines_app/features/chat/conversation/chat_permission_card_model.dart';
|
|
import 'package:pipelines_app/features/chat/conversation/chat_thread_view.dart';
|
|
|
|
import 'chat_thread_fixtures.dart';
|
|
|
|
Widget materialHost(Widget child) => MaterialApp(
|
|
theme: ThemeData(platform: TargetPlatform.android),
|
|
home: Scaffold(body: child),
|
|
);
|
|
|
|
Widget scrollHost(Widget child) =>
|
|
materialHost(SingleChildScrollView(child: child));
|
|
|
|
Widget cupertinoHost(Widget child) => CupertinoApp(
|
|
theme: const CupertinoThemeData(brightness: Brightness.dark),
|
|
home: CupertinoPageScaffold(
|
|
child: SingleChildScrollView(
|
|
padding: const EdgeInsets.all(16),
|
|
child: child,
|
|
),
|
|
),
|
|
);
|
|
|
|
JsonMap permission({
|
|
String id = 'req-1',
|
|
String action = 'bash',
|
|
List<String> resources = const [
|
|
'command: rm -rf build',
|
|
'cwd: /workspace/demo',
|
|
],
|
|
String message = 'This deletes the build output.',
|
|
bool resolved = false,
|
|
String decision = '',
|
|
}) => {
|
|
'session_id': 's',
|
|
'request_id': id,
|
|
'action': action,
|
|
'resources': resources,
|
|
'message': message,
|
|
'source': {'type': 'tool'},
|
|
'resolved': resolved,
|
|
if (decision.isNotEmpty) 'decision': decision,
|
|
};
|
|
|
|
void main() {
|
|
test('recorded action, risk and options survive the card model', () {
|
|
final card = chatPermissionCard(permission(), '')!;
|
|
expect(card.headline, 'Approve bash');
|
|
expect(card.statusLine, chatApprovalStatusPending);
|
|
expect(card.risk, 'This deletes the build output.');
|
|
expect(card.resources, ['command: rm -rf build', 'cwd: /workspace/demo']);
|
|
expect(card.alwaysLabel, 'Always allow');
|
|
expect(card.actionable, isTrue);
|
|
expect(card.showRisk, isTrue);
|
|
});
|
|
|
|
test('resources keep their recorded indentation and order', () {
|
|
final card = chatPermissionCard(
|
|
permission(
|
|
resources: const [
|
|
' cd /workspace/demo',
|
|
'command: |\n echo hello',
|
|
' ',
|
|
],
|
|
),
|
|
'',
|
|
)!;
|
|
expect(card.resources, [
|
|
' cd /workspace/demo',
|
|
'command: |\n echo hello',
|
|
]);
|
|
expect(
|
|
card.resourceText,
|
|
' cd /workspace/demo\ncommand: |\n echo hello',
|
|
);
|
|
});
|
|
|
|
test('risk falls back to the action and resources cap at six lines', () {
|
|
final card = chatPermissionCard(
|
|
permission(
|
|
message: '',
|
|
resources: const [
|
|
'one',
|
|
'two\nthree',
|
|
'four',
|
|
'five',
|
|
'six',
|
|
'seven',
|
|
'eight',
|
|
],
|
|
),
|
|
'',
|
|
)!;
|
|
expect(
|
|
card.risk,
|
|
'The agent is asking to run bash; review the request before allowing.',
|
|
);
|
|
expect(card.resourceText.split('\n'), hasLength(6));
|
|
expect(card.resourceText.endsWith('…'), isTrue);
|
|
expect(card.resources, hasLength(7));
|
|
});
|
|
|
|
test('a server resolution outranks a local answer and a blank decision is '
|
|
'stale', () {
|
|
final resolved = chatPermissionCard(
|
|
permission(
|
|
resolved: true,
|
|
decision: 'always',
|
|
message: 'Another client already allowed this.',
|
|
),
|
|
'once',
|
|
)!;
|
|
expect(resolved.state, ChatPermissionState.resolved);
|
|
expect(resolved.headline, 'Always allowed');
|
|
expect(resolved.statusLine, 'Another client already allowed this.');
|
|
expect(resolved.actionable, isFalse);
|
|
expect(resolved.showRisk, isFalse);
|
|
|
|
final stale = chatPermissionCard(
|
|
permission(resolved: true, message: ''),
|
|
'',
|
|
)!;
|
|
expect(stale.state, ChatPermissionState.stale);
|
|
expect(stale.headline, 'Already resolved');
|
|
expect(stale.statusLine, chatApprovalStatusResolved);
|
|
expect(stale.actionable, isFalse);
|
|
});
|
|
|
|
test('a rejection keeps the risk panel while answered', () {
|
|
final rejected = chatPermissionCard(permission(), 'reject')!;
|
|
expect(rejected.state, ChatPermissionState.answered);
|
|
expect(rejected.headline, 'Rejected');
|
|
expect(rejected.statusLine, chatApprovalStatusAnswered);
|
|
expect(rejected.showRisk, isTrue);
|
|
expect(rejected.actionable, isFalse);
|
|
});
|
|
|
|
test('one stable reply id per confirmed action', () {
|
|
expect(
|
|
chatPermissionReplyId('req-1', 'once'),
|
|
chatPermissionReplyId('req-1', 'once'),
|
|
);
|
|
expect(
|
|
chatPermissionReplyId('req-1', 'once'),
|
|
isNot(chatPermissionReplyId('req-1', 'always')),
|
|
);
|
|
final gate = ChatActionGate();
|
|
final id = chatPermissionReplyId('req-1', 'once');
|
|
expect(gate.start(id), isTrue);
|
|
expect(gate.start(id), isFalse);
|
|
gate.release(id);
|
|
expect(gate.start(id), isTrue);
|
|
});
|
|
|
|
testWidgets('a pending approval renders native Material controls', (
|
|
tester,
|
|
) async {
|
|
final decisions = <String>[];
|
|
final card = chatPermissionCard(permission(), '')!;
|
|
await tester.pumpWidget(
|
|
scrollHost(
|
|
ChatPermissionCard(
|
|
card: card,
|
|
onDecision: (value) async {
|
|
decisions.add(value);
|
|
},
|
|
),
|
|
),
|
|
);
|
|
expect(find.text('Approve bash'), findsOneWidget);
|
|
expect(find.text('Waiting for your response'), findsOneWidget);
|
|
expect(find.text('RISK REVIEW'), findsOneWidget);
|
|
expect(find.text('This deletes the build output.'), findsOneWidget);
|
|
expect(find.text('Allow once'), findsOneWidget);
|
|
expect(find.text('Always allow'), findsOneWidget);
|
|
expect(find.text('Reject'), findsOneWidget);
|
|
expect(find.text('Permission request · tool'), findsNothing);
|
|
|
|
await tester.tap(find.text('Allow once'));
|
|
await tester.pump();
|
|
expect(decisions, ['once']);
|
|
});
|
|
|
|
testWidgets('Show full request expands to every recorded resource line', (
|
|
tester,
|
|
) async {
|
|
final card = chatPermissionCard(
|
|
permission(
|
|
resources: const [
|
|
' one',
|
|
'two',
|
|
'three',
|
|
'four',
|
|
'five',
|
|
'six',
|
|
'seven',
|
|
],
|
|
),
|
|
'',
|
|
)!;
|
|
await tester.pumpWidget(
|
|
scrollHost(ChatPermissionCard(card: card, onDecision: (_) async {})),
|
|
);
|
|
expect(find.text(' one'), findsNothing);
|
|
await tester.tap(find.text('Show full request'));
|
|
await tester.pumpAndSettle();
|
|
expect(find.text(card.resources.join('\n')), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('320px at 2x in dark Cupertino keeps the card native and '
|
|
'readable', (tester) async {
|
|
tester.view.physicalSize = const Size(640, 1400);
|
|
tester.view.devicePixelRatio = 2;
|
|
tester.platformDispatcher.textScaleFactorTestValue = 2;
|
|
tester.platformDispatcher.platformBrightnessTestValue = Brightness.dark;
|
|
addTearDown(tester.view.resetPhysicalSize);
|
|
addTearDown(tester.view.resetDevicePixelRatio);
|
|
addTearDown(tester.platformDispatcher.clearTextScaleFactorTestValue);
|
|
addTearDown(tester.platformDispatcher.clearPlatformBrightnessTestValue);
|
|
|
|
final card = chatPermissionCard(
|
|
permission(
|
|
action: 'a very long tool name that has to wrap at twice the text size',
|
|
resources: const [
|
|
'a long resource line that wraps instead of clipping any words at all',
|
|
],
|
|
),
|
|
'',
|
|
)!;
|
|
await tester.pumpWidget(
|
|
cupertinoHost(ChatPermissionCard(card: card, onDecision: (_) async {})),
|
|
);
|
|
expect(tester.takeException(), isNull);
|
|
expect(find.text('Allow once'), findsOneWidget);
|
|
expect(find.byType(Material), findsNothing);
|
|
});
|
|
|
|
testWidgets('a pushed chat_permission renders, sends the recorded reply, '
|
|
'and a second tap sends once', (tester) async {
|
|
final gateway = FakeChatGateway();
|
|
addTearDown(gateway.close);
|
|
gateway.history = (_) async => chatPage(const []);
|
|
final held = Completer<JsonMap>();
|
|
gateway.reply = (_) => held.future;
|
|
|
|
await tester.pumpWidget(
|
|
materialHost(
|
|
ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {}),
|
|
),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
gateway.emitRaw({
|
|
'type': 'chat_permission',
|
|
'chat_permission': permission(),
|
|
});
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(find.text('Approve bash'), findsOneWidget);
|
|
await tester.tap(find.text('Allow once'));
|
|
await tester.tap(find.text('Allow once'));
|
|
await tester.pump();
|
|
|
|
final replies = gateway.calls.where((call) => call.type == 'chat_reply');
|
|
expect(replies, hasLength(1));
|
|
expect(replies.single.fields, {
|
|
'chat_reply': {
|
|
'session_id': 's',
|
|
'request_id': 'req-1',
|
|
'decision': 'once',
|
|
'client': 'Web',
|
|
},
|
|
});
|
|
expect(find.text('Allowed once'), findsOneWidget);
|
|
expect(find.text('Waiting for the agent to continue…'), findsOneWidget);
|
|
held.complete(const {});
|
|
await tester.pumpAndSettle();
|
|
});
|
|
|
|
testWidgets('a refused reply keeps the card pending and shows the server '
|
|
'reason', (tester) async {
|
|
final gateway = FakeChatGateway();
|
|
addTearDown(gateway.close);
|
|
gateway.history = (_) async => chatPage(const []);
|
|
gateway.reply = (_) async => throw StateError('request expired');
|
|
|
|
await tester.pumpWidget(
|
|
materialHost(
|
|
ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {}),
|
|
),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
gateway.emitRaw({
|
|
'type': 'chat_permission',
|
|
'chat_permission': permission(),
|
|
});
|
|
await tester.pumpAndSettle();
|
|
|
|
await tester.tap(find.text('Reject'));
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(find.textContaining('request expired'), findsOneWidget);
|
|
expect(find.text('Approve bash'), findsOneWidget);
|
|
expect(find.text('Allow once'), findsOneWidget);
|
|
expect(find.text('Reject'), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('a server resolution clears the controls and shows the recorded '
|
|
'decision', (tester) async {
|
|
final gateway = FakeChatGateway();
|
|
addTearDown(gateway.close);
|
|
gateway.history = (_) async => chatPage(const []);
|
|
|
|
await tester.pumpWidget(
|
|
materialHost(
|
|
ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {}),
|
|
),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
gateway.emitRaw({
|
|
'type': 'chat_permission',
|
|
'chat_permission': permission(),
|
|
});
|
|
await tester.pumpAndSettle();
|
|
expect(find.text('Allow once'), findsOneWidget);
|
|
|
|
gateway.emitRaw({
|
|
'type': 'chat_permission',
|
|
'chat_permission': permission(
|
|
resolved: true,
|
|
decision: 'once',
|
|
message: 'Another device allowed once.',
|
|
),
|
|
});
|
|
await tester.pumpAndSettle();
|
|
expect(find.text('Allowed once'), findsOneWidget);
|
|
expect(find.text('Another device allowed once.'), findsOneWidget);
|
|
expect(find.text('Allow once'), findsNothing);
|
|
});
|
|
|
|
testWidgets('a turn waiting on a pending card hides the running indicator '
|
|
'but keeps Stop', (tester) async {
|
|
final gateway = FakeChatGateway();
|
|
addTearDown(gateway.close);
|
|
gateway.history = (_) async => chatPage(const []);
|
|
|
|
await tester.pumpWidget(
|
|
materialHost(
|
|
ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {}),
|
|
),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
gateway.emitRaw({
|
|
'type': 'chat_permission',
|
|
'chat_permission': permission(),
|
|
});
|
|
gateway.emitEvent(chatStepFrame('running'));
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(find.text('Approve bash'), findsOneWidget);
|
|
expect(find.text('Assistant is responding…'), findsNothing);
|
|
expect(find.text('Stop'), findsOneWidget);
|
|
|
|
gateway.emitRaw({
|
|
'type': 'chat_permission',
|
|
'chat_permission': permission(resolved: true, decision: 'once'),
|
|
});
|
|
await tester.pumpAndSettle();
|
|
|
|
expect(find.text('Assistant is responding…'), findsOneWidget);
|
|
expect(find.text('Stop'), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('consecutive Material cards stay separate surfaces', (
|
|
tester,
|
|
) async {
|
|
final gateway = FakeChatGateway();
|
|
addTearDown(gateway.close);
|
|
gateway.history = (_) async => chatPage(const []);
|
|
|
|
await tester.pumpWidget(
|
|
materialHost(
|
|
ChatThreadView(gateway: gateway, sessionId: 's', onBack: () {}),
|
|
),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
gateway.emitRaw({
|
|
'type': 'chat_permission',
|
|
'chat_permission': permission(id: 'req-1'),
|
|
});
|
|
gateway.emitRaw({
|
|
'type': 'chat_permission',
|
|
'chat_permission': permission(id: 'req-2', action: 'python'),
|
|
});
|
|
await tester.pumpAndSettle();
|
|
|
|
final first = tester.getRect(
|
|
find
|
|
.descendant(of: find.byType(Card).at(0), matching: find.byType(Material))
|
|
.first,
|
|
);
|
|
final second = tester.getRect(
|
|
find
|
|
.descendant(of: find.byType(Card).at(1), matching: find.byType(Material))
|
|
.first,
|
|
);
|
|
expect(second.top - first.bottom, greaterThanOrEqualTo(12));
|
|
});
|
|
}
|