b4701419d0
Keep the permission-card regression aligned with the redesigned running composer. Verify that the composer stop control is absent and Stop remains available through the More menu while the turn runs.
400 lines
12 KiB
Dart
400 lines
12 KiB
Dart
import 'dart:async';
|
|
|
|
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));
|
|
|
|
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,
|
|
};
|
|
|
|
Future<void> expectStopInMoreMenu(WidgetTester tester) async {
|
|
await tester.tap(find.byTooltip('More actions').hitTestable().first);
|
|
await tester.pumpAndSettle();
|
|
expect(find.text('Stop'), findsOneWidget);
|
|
await tester.tapAt(const Offset(1, 1));
|
|
await tester.pumpAndSettle();
|
|
expect(find.text('Stop'), findsNothing);
|
|
}
|
|
|
|
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('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',
|
|
},
|
|
});
|
|
// The answered request folds into one chip, the way tool calls do.
|
|
expect(find.text('Approve bash'), findsNothing);
|
|
expect(find.text('1 permission'), findsOneWidget);
|
|
await tester.tap(find.byKey(const Key('chat-permission-chip')));
|
|
await tester.pumpAndSettle();
|
|
await tester.tap(find.text('bash · Allowed once'));
|
|
await tester.pumpAndSettle();
|
|
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('Allow once'), findsNothing);
|
|
await tester.tap(find.byKey(const Key('chat-permission-chip')));
|
|
await tester.pumpAndSettle();
|
|
await tester.tap(find.text('bash · Allowed once'));
|
|
await tester.pumpAndSettle();
|
|
expect(find.text('Another device allowed once.'), findsOneWidget);
|
|
});
|
|
|
|
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.byKey(const Key('chat-composer-stop')), findsNothing);
|
|
await expectStopInMoreMenu(tester);
|
|
|
|
gateway.emitRaw({
|
|
'type': 'chat_permission',
|
|
'chat_permission': permission(resolved: true, decision: 'once'),
|
|
});
|
|
await tester.pumpAndSettle();
|
|
|
|
// No filler line while the turn runs; the More menu keeps Stop reachable.
|
|
expect(find.byKey(const Key('chat-running')), findsNothing);
|
|
expect(find.byKey(const Key('chat-composer-stop')), findsNothing);
|
|
await expectStopInMoreMenu(tester);
|
|
});
|
|
|
|
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));
|
|
});
|
|
}
|