feat(app): add prompt presets to chat creation
This commit is contained in:
@@ -1,8 +1,12 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/features/chat/data/chat_create_prompt_store.dart';
|
||||
import 'package:pipelines_app/features/chat/data/chat_repository.dart';
|
||||
import 'package:pipelines_app/features/chat/ui/sessions/create_chat_dialog.dart';
|
||||
import 'package:pipelines_app/features/chat/domain/chat_commands.dart';
|
||||
|
||||
import 'fake_chat_gateway.dart';
|
||||
|
||||
@@ -21,6 +25,14 @@ class _MemoryPrompts implements ChatCreatePromptStore {
|
||||
) async => saved['$backend|$directory'] = value;
|
||||
}
|
||||
|
||||
class _DelayedPrompts extends _MemoryPrompts {
|
||||
final Completer<ChatCreatePrompt?> result = Completer<ChatCreatePrompt?>();
|
||||
|
||||
@override
|
||||
Future<ChatCreatePrompt?> read(String backend, String directory) =>
|
||||
result.future;
|
||||
}
|
||||
|
||||
Future<ChatDraft?> _create(
|
||||
WidgetTester tester,
|
||||
_MemoryPrompts prompts, {
|
||||
@@ -72,19 +84,18 @@ void main() {
|
||||
expect(defaultChatName('~', 'OpenCode'), 'Home OpenCode');
|
||||
});
|
||||
|
||||
testWidgets(
|
||||
'a custom prompt is sent, but the next chat defaults to off',
|
||||
(tester) async {
|
||||
final prompts = _MemoryPrompts();
|
||||
final first = await _create(tester, prompts, prompt: 'Review only.');
|
||||
expect(first!.instructions, 'Review only.');
|
||||
expect(prompts.saved.values.single, (send: true, text: 'Review only.'));
|
||||
testWidgets('a custom prompt is sent, but the next chat defaults to off', (
|
||||
tester,
|
||||
) async {
|
||||
final prompts = _MemoryPrompts();
|
||||
final first = await _create(tester, prompts, prompt: 'Review only.');
|
||||
expect(first!.instructions, 'Review only.');
|
||||
expect(prompts.saved.values.single, (send: true, text: 'Review only.'));
|
||||
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
final second = await _create(tester, prompts);
|
||||
expect(second!.instructions, isEmpty);
|
||||
},
|
||||
);
|
||||
await tester.pumpWidget(const SizedBox());
|
||||
final second = await _create(tester, prompts);
|
||||
expect(second!.instructions, isEmpty);
|
||||
});
|
||||
|
||||
testWidgets('a chat starts without a prompt even after a prior choice', (
|
||||
tester,
|
||||
@@ -98,4 +109,130 @@ void main() {
|
||||
await _create(tester, prompts);
|
||||
expect(prompts.saved.values.single.send, isFalse);
|
||||
});
|
||||
|
||||
testWidgets('the prompt menu selects a preset and permits custom edits', (
|
||||
tester,
|
||||
) async {
|
||||
final prompts = _MemoryPrompts();
|
||||
await _open(tester, prompts);
|
||||
await tester.tap(find.byKey(const Key('chat-create-send-prompt')));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byKey(const Key('chat-create-prompt-choice')));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text('Careful coding').last);
|
||||
await tester.pumpAndSettle();
|
||||
expect(
|
||||
tester
|
||||
.widget<TextField>(find.byKey(const Key('chat-create-instructions')))
|
||||
.controller!
|
||||
.text,
|
||||
chatDefaultSessionInstructions,
|
||||
);
|
||||
await tester.enterText(
|
||||
find.byKey(const Key('chat-create-instructions')),
|
||||
'My own instructions',
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('Custom'), findsOneWidget);
|
||||
expect(tester.takeException(), isNull);
|
||||
});
|
||||
|
||||
testWidgets('a recalled prompt is offered as an editable preset', (
|
||||
tester,
|
||||
) async {
|
||||
final prompts = _MemoryPrompts()
|
||||
..saved['opencode|~'] = (send: true, text: 'Previously used');
|
||||
await _open(tester, prompts);
|
||||
expect(find.byKey(const Key('chat-create-instructions')), findsNothing);
|
||||
await tester.tap(find.byKey(const Key('chat-create-send-prompt')));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byKey(const Key('chat-create-prompt-choice')));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.text('Last used prompt').last);
|
||||
await tester.pumpAndSettle();
|
||||
expect(
|
||||
tester
|
||||
.widget<TextField>(find.byKey(const Key('chat-create-instructions')))
|
||||
.controller!
|
||||
.text,
|
||||
'Previously used',
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('a delayed saved prompt cannot override a user edit', (
|
||||
tester,
|
||||
) async {
|
||||
final prompts = _DelayedPrompts();
|
||||
await _open(tester, prompts, settle: false);
|
||||
await tester.pump(const Duration(milliseconds: 1));
|
||||
await tester.tap(find.byKey(const Key('chat-create-send-prompt')));
|
||||
await tester.pump();
|
||||
await tester.enterText(
|
||||
find.byKey(const Key('chat-create-instructions')),
|
||||
'User choice wins',
|
||||
);
|
||||
prompts.result.complete((send: true, text: 'Stale saved prompt'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(
|
||||
tester
|
||||
.widget<TextField>(find.byKey(const Key('chat-create-instructions')))
|
||||
.controller!
|
||||
.text,
|
||||
'User choice wins',
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('Cupertino prompt chooser has native preset and custom choices', (
|
||||
tester,
|
||||
) async {
|
||||
final prompts = _MemoryPrompts();
|
||||
await tester.pumpWidget(
|
||||
CupertinoApp(
|
||||
home: CupertinoPageScaffold(
|
||||
child: CreateChatDialog(
|
||||
repository: ChatRepository(FakeChatGateway()),
|
||||
operationId: 'op',
|
||||
prompts: prompts,
|
||||
onSubmit: (_, _) async => {'id': 'new'},
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byKey(const Key('chat-create-send-prompt')), findsOneWidget);
|
||||
expect(find.byKey(const Key('chat-create-instructions')), findsNothing);
|
||||
await tester.tap(find.byKey(const Key('chat-create-send-prompt')));
|
||||
await tester.pumpAndSettle();
|
||||
await tester.tap(find.byKey(const Key('chat-create-prompt-choice')));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.byType(CupertinoActionSheet), findsOneWidget);
|
||||
expect(find.text('Careful coding'), findsOneWidget);
|
||||
await tester.tap(find.text('Cancel').last);
|
||||
await tester.pumpAndSettle();
|
||||
expect(tester.takeException(), isNull);
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _open(
|
||||
WidgetTester tester,
|
||||
ChatCreatePromptStore prompts, {
|
||||
bool settle = true,
|
||||
}) async {
|
||||
final gateway = FakeChatGateway()
|
||||
..directoriesHandler = () async => {
|
||||
'chat_directories': ['/work/pipelines'],
|
||||
};
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: CreateChatDialog(
|
||||
repository: ChatRepository(gateway),
|
||||
operationId: 'op',
|
||||
prompts: prompts,
|
||||
onSubmit: (_, _) async => {'id': 'new'},
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
if (settle) await tester.pumpAndSettle();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user