open_ai_smart_menu_test.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import 'package:appflowy/plugins/document/presentation/plugins/openai/service/openai_client.dart';
  2. import 'package:appflowy_editor/appflowy_editor.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:flutter_test/flutter_test.dart';
  5. import 'package:integration_test/integration_test.dart';
  6. import 'util/mock/mock_openai_repository.dart';
  7. import 'util/util.dart';
  8. import 'package:flowy_infra_ui/flowy_infra_ui.dart';
  9. import 'package:appflowy_editor/src/render/toolbar/toolbar_widget.dart';
  10. import 'package:appflowy/startup/startup.dart';
  11. void main() {
  12. IntegrationTestWidgetsFlutterBinding.ensureInitialized();
  13. const service = TestWorkspaceService(TestWorkspace.aiWorkSpace);
  14. group('integration tests for open-ai smart menu', () {
  15. setUpAll(() async => await service.setUpAll());
  16. setUp(() async => await service.setUp());
  17. testWidgets('testing selection on open-ai smart menu replace', (tester) async {
  18. final appFlowyEditor = await setUpOpenAITesting(tester);
  19. final editorState = appFlowyEditor.editorState;
  20. editorState.service.selectionService.updateSelection(
  21. Selection(
  22. start: Position(path: [1], offset: 4),
  23. end: Position(path: [1], offset: 10),
  24. ),
  25. );
  26. await tester.pumpAndSettle(const Duration(milliseconds: 500));
  27. await tester.pumpAndSettle();
  28. expect(find.byType(ToolbarWidget), findsAtLeastNWidgets(1));
  29. await tester.tap(find.byTooltip('AI Assistants'));
  30. await tester.pumpAndSettle(const Duration(milliseconds: 500));
  31. await tester.tap(find.text('Summarize'));
  32. await tester.pumpAndSettle();
  33. await tester.tap(find.byType(FlowyRichTextButton, skipOffstage: false).first);
  34. await tester.pumpAndSettle();
  35. expect(
  36. editorState.service.selectionService.currentSelection.value,
  37. Selection(
  38. start: Position(path: [1], offset: 4),
  39. end: Position(path: [1], offset: 84),
  40. ),
  41. );
  42. });
  43. testWidgets('testing selection on open-ai smart menu insert', (tester) async {
  44. final appFlowyEditor = await setUpOpenAITesting(tester);
  45. final editorState = appFlowyEditor.editorState;
  46. editorState.service.selectionService.updateSelection(
  47. Selection(
  48. start: Position(path: [1], offset: 0),
  49. end: Position(path: [1], offset: 5),
  50. ),
  51. );
  52. await tester.pumpAndSettle(const Duration(milliseconds: 500));
  53. await tester.pumpAndSettle();
  54. expect(find.byType(ToolbarWidget), findsAtLeastNWidgets(1));
  55. await tester.tap(find.byTooltip('AI Assistants'));
  56. await tester.pumpAndSettle(const Duration(milliseconds: 500));
  57. await tester.tap(find.text('Summarize'));
  58. await tester.pumpAndSettle();
  59. await tester.tap(find.byType(FlowyRichTextButton, skipOffstage: false).at(1));
  60. await tester.pumpAndSettle();
  61. expect(
  62. editorState.service.selectionService.currentSelection.value,
  63. Selection(
  64. start: Position(path: [2], offset: 0),
  65. end: Position(path: [3], offset: 0),
  66. ),
  67. );
  68. });
  69. });
  70. }
  71. Future<AppFlowyEditor> setUpOpenAITesting(WidgetTester tester) async {
  72. await tester.initializeAppFlowy();
  73. await mockOpenAIRepository();
  74. await simulateKeyDownEvent(LogicalKeyboardKey.controlLeft);
  75. await simulateKeyDownEvent(LogicalKeyboardKey.backslash);
  76. await tester.pumpAndSettle();
  77. final Finder editor = find.byType(AppFlowyEditor);
  78. await tester.tap(editor);
  79. await tester.pumpAndSettle();
  80. return (tester.state(editor).widget as AppFlowyEditor);
  81. }
  82. Future<void> mockOpenAIRepository() async {
  83. await getIt.unregister<OpenAIRepository>();
  84. getIt.registerFactoryAsync<OpenAIRepository>(
  85. () => Future.value(
  86. MockOpenAIRepository(),
  87. ),
  88. );
  89. return;
  90. }