import 'package:cutdex/app/editor_theme.dart'; import 'package:cutdex/features/settings/agent_settings_panel.dart'; import 'package:cutdex/integrations/agent/agent_credentials.dart'; import 'package:cutdex/integrations/agent/agent_models.dart'; import 'package:cutdex/l10n/app_localizations.dart'; import 'package:cutdex/persistence/app_settings_repositories.dart'; import 'package:cutdex_ui/theme.dart'; import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_test/flutter_test.dart'; class _Repository implements AgentSettingsRepository { AgentSettingsState value = const AgentSettingsState(); @override Future load() async => value; @override Future save(AgentSettingsState settings) async => value = settings; } void main() { testWidgets('provider model fields stack in a narrow settings viewport', ( tester, ) async { await tester.binding.setSurfaceSize(const Size(390, 640)); addTearDown(() => tester.binding.setSurfaceSize(null)); await tester.pumpWidget( ProviderScope( overrides: [ agentSettingsRepositoryProvider.overrideWithValue(_Repository()), agentCredentialStoreProvider.overrideWithValue( MemoryAgentCredentialStore(), ), ], child: MaterialApp( localizationsDelegates: AppLocalizations.localizationsDelegates, supportedLocales: AppLocalizations.supportedLocales, theme: EditorTheme.penDark, home: const Scaffold( body: AgentSettingsPanel( contentPadding: AppSettingsMetrics.contentPadding, ), ), ), ), ); await tester.pumpAndSettle(); await tester.tap(find.text('Add provider')); await tester.pumpAndSettle(); final model = tester.getRect(find.byKey(const Key('agent-model-id-0'))); final budget = tester.getRect( find.byKey(const Key('agent-model-context-0')), ); expect(budget.top, greaterThan(model.bottom)); expect(budget.left, model.left); expect(budget.width, model.width); expect(tester.takeException(), isNull); }); testWidgets('inline Provider form has one stable scroll region', ( tester, ) async { await tester.binding.setSurfaceSize(const Size(1000, 420)); addTearDown(() => tester.binding.setSurfaceSize(null)); await tester.pumpWidget( ProviderScope( overrides: [ agentSettingsRepositoryProvider.overrideWithValue(_Repository()), agentCredentialStoreProvider.overrideWithValue( MemoryAgentCredentialStore(), ), ], child: MaterialApp( localizationsDelegates: AppLocalizations.localizationsDelegates, supportedLocales: AppLocalizations.supportedLocales, theme: EditorTheme.penDark, home: const Scaffold(body: AgentSettingsPanel()), ), ), ); await tester.pumpAndSettle(); await tester.tap(find.text('Add provider')); await tester.pumpAndSettle(); final scroll = find.byKey(const Key('agent-settings-scroll')); final controller = tester.widget(scroll).controller!; final extent = controller.position.maxScrollExtent; expect(extent, greaterThan(0)); expect(find.byType(Scrollbar), findsOneWidget); for (var i = 0; i < 3; i++) { await tester.drag(scroll, const Offset(0, -80)); await tester.pumpAndSettle(); final offset = controller.offset; await tester.pump(const Duration(milliseconds: 200)); expect(controller.offset, offset); expect(controller.position.maxScrollExtent, extent); } // Desktop edge scrolling must not stretch the viewport or thumb. controller.jumpTo(extent); await tester.pumpAndSettle(); await tester.drag(scroll, const Offset(0, -160)); await tester.pump(); expect(controller.offset, extent); expect(controller.position.maxScrollExtent, extent); await tester.pumpAndSettle(); controller.jumpTo(0); await tester.pumpAndSettle(); await tester.drag(scroll, const Offset(0, 160)); await tester.pump(); expect(controller.offset, 0); expect(controller.position.maxScrollExtent, extent); await tester.pumpAndSettle(); await tester.tap(find.byKey(const Key('agent-provider-back'))); await tester.pumpAndSettle(); expect(controller.offset, 0); expect(controller.position.maxScrollExtent, 0); expect(tester.takeException(), isNull); }); test('all supported non-English locales translate Agent settings', () { final english = lookupAppLocalizations(const Locale('en')); for (final languageCode in [ 'de', 'es', 'fr', 'it', 'ja', 'ko', 'pt', 'ru', ]) { final localized = lookupAppLocalizations(Locale(languageCode)); expect( localized.agentAddProvider, isNot(english.agentAddProvider), reason: languageCode, ); expect( localized.agentConversationLocation, isNot(english.agentConversationLocation), reason: languageCode, ); expect( localized.agentTrash, isNot(english.agentTrash), reason: languageCode, ); expect( localized.agentDeleteProviderDescription('Example'), contains('Example'), reason: languageCode, ); } }); testWidgets('renders Agent settings in Chinese', (tester) async { await tester.pumpWidget( ProviderScope( overrides: [ agentSettingsRepositoryProvider.overrideWithValue(_Repository()), agentCredentialStoreProvider.overrideWithValue( MemoryAgentCredentialStore(), ), ], child: MaterialApp( locale: const Locale('zh'), localizationsDelegates: AppLocalizations.localizationsDelegates, supportedLocales: AppLocalizations.supportedLocales, theme: EditorTheme.penDark, home: const Scaffold(body: AgentSettingsPanel()), ), ), ); await tester.pumpAndSettle(); expect(find.text('自定义 Provider'), findsNothing); expect(find.text('添加 Provider'), findsOneWidget); expect(find.text('请先添加 Provider,再启用助手。'), findsNothing); expect(find.byType(Switch), findsNothing); }); testWidgets('adds provider inline and returns to the provider list', ( tester, ) async { final repository = _Repository(); final credentials = MemoryAgentCredentialStore(); await tester.binding.setSurfaceSize(const Size(1000, 760)); addTearDown(() => tester.binding.setSurfaceSize(null)); await tester.pumpWidget( ProviderScope( overrides: [ agentSettingsRepositoryProvider.overrideWithValue(repository), agentCredentialStoreProvider.overrideWithValue(credentials), ], child: MaterialApp( localizationsDelegates: AppLocalizations.localizationsDelegates, supportedLocales: AppLocalizations.supportedLocales, theme: EditorTheme.penDark, home: const Scaffold(body: AgentSettingsPanel()), ), ), ); await tester.pumpAndSettle(); await tester.tap(find.text('Add provider')); await tester.pump(); await tester.pump(const Duration(milliseconds: 70)); expect( tester .widget( find.byKey(const Key('agent-provider-page-transition')), ) .opacity .value, lessThan(1), ); expect(find.byKey(const Key('agent-provider-list')), findsOneWidget); expect(find.byKey(const Key('agent-provider-editor')), findsNothing); await tester.pumpAndSettle(); expect(find.byType(Dialog), findsNothing); expect(find.byKey(const Key('agent-provider-editor')), findsOneWidget); await tester.enterText( find.byKey(const Key('agent-provider-name')), 'Unsaved', ); await tester.tap(find.byKey(const Key('agent-provider-back'))); await tester.pumpAndSettle(); expect(find.byKey(const Key('agent-provider-list')), findsOneWidget); expect(repository.value.providers, isEmpty); await tester.tap(find.text('Add provider')); await tester.pumpAndSettle(); await tester.enterText( find.byKey(const Key('agent-provider-name')), 'Local provider', ); await tester.enterText( find.byKey(const Key('agent-provider-base-url')), 'http://127.0.0.1:11434/v1', ); await tester.enterText( find.byKey(const Key('agent-provider-api-key')), 'secret', ); await tester.enterText( find.byKey(const Key('agent-model-id-0')), 'local-model', ); await tester.tap(find.text('Save provider')); await tester.pumpAndSettle(); expect(find.text('Local provider'), findsOneWidget); expect(find.byKey(const Key('agent-provider-list')), findsOneWidget); expect(find.byKey(const Key('agent-provider-editor')), findsNothing); expect(repository.value.providers.single.models.single.id, 'local-model'); expect( await credentials.read(repository.value.providers.single.credentialId!), 'secret', ); expect(find.byType(Switch), findsOneWidget); final providerId = repository.value.providers.single.id; await tester.tap(find.byKey(ValueKey('agent-provider-edit-$providerId'))); await tester.pumpAndSettle(); expect( tester .widget(find.byKey(const Key('agent-provider-name'))) .controller! .text, 'Local provider', ); expect( tester .widget( find.byKey(const Key('agent-provider-api-key')), ) .controller! .text, isEmpty, ); expect(find.text('Leave blank to keep the saved key.'), findsOneWidget); await tester.enterText( find.byKey(const Key('agent-provider-name')), 'Edited provider', ); await tester.ensureVisible(find.text('Save provider')); await tester.tap(find.text('Save provider')); await tester.pumpAndSettle(); expect(repository.value.providers.length, 1); expect(repository.value.providers.single.id, providerId); expect(repository.value.providers.single.name, 'Edited provider'); expect( await credentials.read(repository.value.providers.single.credentialId!), 'secret', ); await tester.tap(find.byType(Switch)); await tester.pumpAndSettle(); expect(repository.value.providers.single.enabled, false); expect(repository.value.selectedProviderId, isNull); expect(tester.takeException(), isNull); }); }