open_ai_smart_menu_test.dart 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. tearDown(() async => await service.tearDown());
  18. tearDownAll(() async => await service.tearDownAll());
  19. testWidgets('testing selection on open-ai smart menu replace', (tester) async {
  20. final appFlowyEditor = await setUpOpenAITesting(tester);
  21. final editorState = appFlowyEditor.editorState;
  22. editorState.service.selectionService.updateSelection(
  23. Selection(
  24. start: Position(path: [1], offset: 4),
  25. end: Position(path: [1], offset: 10),
  26. ),
  27. );
  28. await tester.pumpAndSettle(const Duration(milliseconds: 500));
  29. await tester.pumpAndSettle();
  30. expect(find.byType(ToolbarWidget), findsAtLeastNWidgets(1));
  31. await tester.tap(find.byTooltip('AI Assistants'));
  32. await tester.pumpAndSettle(const Duration(milliseconds: 500));
  33. await tester.tap(find.text('Summarize'));
  34. await tester.pumpAndSettle();
  35. await tester.tap(find.byType(FlowyRichTextButton, skipOffstage: false).first);
  36. await tester.pumpAndSettle();
  37. expect(
  38. editorState.service.selectionService.currentSelection.value,
  39. Selection(
  40. start: Position(path: [1], offset: 4),
  41. end: Position(path: [1], offset: 84),
  42. ),
  43. );
  44. });
  45. testWidgets('testing selection on open-ai smart menu insert', (tester) async {
  46. final appFlowyEditor = await setUpOpenAITesting(tester);
  47. final editorState = appFlowyEditor.editorState;
  48. editorState.service.selectionService.updateSelection(
  49. Selection(
  50. start: Position(path: [1], offset: 0),
  51. end: Position(path: [1], offset: 5),
  52. ),
  53. );
  54. await tester.pumpAndSettle(const Duration(milliseconds: 500));
  55. await tester.pumpAndSettle();
  56. expect(find.byType(ToolbarWidget), findsAtLeastNWidgets(1));
  57. await tester.tap(find.byTooltip('AI Assistants'));
  58. await tester.pumpAndSettle(const Duration(milliseconds: 500));
  59. await tester.tap(find.text('Summarize'));
  60. await tester.pumpAndSettle();
  61. await tester.tap(find.byType(FlowyRichTextButton, skipOffstage: false).at(1));
  62. await tester.pumpAndSettle();
  63. expect(
  64. editorState.service.selectionService.currentSelection.value,
  65. Selection(
  66. start: Position(path: [2], offset: 0),
  67. end: Position(path: [3], offset: 0),
  68. ),
  69. );
  70. });
  71. });
  72. }
  73. Future<AppFlowyEditor> setUpOpenAITesting(WidgetTester tester) async {
  74. await tester.initializeAppFlowy();
  75. await mockOpenAIRepository();
  76. await simulateKeyDownEvent(LogicalKeyboardKey.controlLeft);
  77. await simulateKeyDownEvent(LogicalKeyboardKey.backslash);
  78. await tester.pumpAndSettle();
  79. final Finder editor = find.byType(AppFlowyEditor);
  80. await tester.tap(editor);
  81. await tester.pumpAndSettle();
  82. return (tester.state(editor).widget as AppFlowyEditor);
  83. }
  84. Future<void> mockOpenAIRepository() async {
  85. await getIt.unregister<OpenAIRepository>();
  86. getIt.registerFactoryAsync<OpenAIRepository>(
  87. () => Future.value(
  88. MockOpenAIRepository(),
  89. ),
  90. );
  91. return;
  92. }