open_ai_smart_menu_test.dart 3.8 KB

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