import 'dart:async'; import 'package:flutter/services.dart'; import 'package:flutter_riverpod/misc.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:http/http.dart' as http; import 'package:http/testing.dart'; import 'package:lotti/classes/ai/ai_config.dart'; import 'package:lotti/classes/entity_definitions.dart'; import 'package:lotti/classes/onboarding_event.dart'; import 'package:lotti/database/onboarding_metrics_db.dart'; import 'package:lotti/database/state/config_flag_provider.dart'; import 'package:lotti/features/agents/service/agent_template_service.dart'; import 'package:lotti/features/ai/model/resolved_profile.dart'; import 'package:lotti/features/ai/repository/ai_config_repository.dart'; import 'package:lotti/features/ai/state/profile_automation_providers.dart'; import 'package:lotti/features/ai/ui/settings/services/connection_verifier_service.dart'; import 'package:lotti/features/ai/util/profile_seeding_service.dart'; import 'package:lotti/features/categories/repository/categories_repository.dart'; import 'package:lotti/features/companion/animation/mouth_track.dart'; import 'package:lotti/features/companion/rig/companion_painter.dart'; import 'package:lotti/features/companion/state/spoken_track.dart'; import 'package:lotti/features/daily_os_next/state/capture_controller.dart'; import 'package:lotti/features/demo/state/demo_mode_gateway.dart'; import 'package:lotti/features/design_system/components/toasts/design_system_toast.dart'; import 'package:lotti/features/design_system/theme/design_system_theme.dart'; import 'package:lotti/features/design_system/theme/design_tokens.dart'; import 'package:lotti/features/onboarding/services/onboarding_capture_to_task_service.dart'; import 'package:lotti/features/onboarding/ui/onboarding_welcome_modal.dart'; import 'package:lotti/features/onboarding/ui/widgets/onboarding_companion.dart'; import 'package:lotti/features/onboarding/ui/widgets/onboarding_hero.dart'; import 'package:lotti/features/onboarding/ui/widgets/onboarding_speech_bubble.dart'; import 'package:lotti/features/tts/cloud/cloud_speech_synthesizer.dart'; import 'package:lotti/features/tts/state/tts_playback_controller.dart'; import 'package:lotti/get_it.dart'; import 'package:lotti/logic/repositories/onboarding_metrics_repository.dart'; import 'package:lotti/services/logging_service.dart'; import 'package:lotti/services/nav_service.dart'; import 'package:material_ui/material_ui.dart'; import 'package:mocktail/mocktail.dart'; import '../../../helpers/fallbacks.dart'; import '../../../mocks/mocks.dart'; import '../../../widget_test_utils.dart'; import '../../agents/test_data/ai_config_factories.dart'; import '../../categories/test_utils.dart'; import '../../tts/test_utils.dart'; /// Canned probe so the API-key step's live verification resolves without a /// network call. class _FakeProbe extends ConnectionProbe { _FakeProbe(this.result); final ConnectionCheckState result; @override Future probe({ required Uri baseUri, required String apiKey, required Duration timeout, required http.Client client, }) async => result; } void main() { late OnboardingMetricsDb db; late OnboardingMetricsRepository repo; var idSeq = 0; // Reduced motion so the looping constellation controller stops and // pumpAndSettle can complete. // Phone-width, and tall: the panel fills the height and grows past it // where a step's text wraps long in the test font, so the surface is tall // enough that the main button at its foot is on-screen on every step. const mq = MediaQueryData(size: Size(390, 1400), disableAnimations: true); setUpAll(registerAllFallbackValues); setUp(() { idSeq = 0; db = OnboardingMetricsDb(inMemoryDatabase: true); repo = OnboardingMetricsRepository( db: db, clock: () => DateTime.utc(2026, 7, 1, 9), idGenerator: () => 'id-${idSeq++}', currentPlatform: () => 'testos', ); // The category step logs a write failure via getIt before // toasting; production registers it at startup, so mirror that here. if (!getIt.isRegistered()) { getIt.registerSingleton(LoggingService()); } }); tearDown(() async { await db.close(); if (getIt.isRegistered()) { getIt.unregister(); } }); Widget host({ VoidCallback? onDismiss, VoidCallback? onCompleted, DemoModeGateway? demoGateway, }) { return Builder( builder: (context) => ElevatedButton( onPressed: () => OnboardingWelcomeModal.show( context, metrics: repo, onDismiss: onDismiss ?? () {}, onCompleted: onCompleted, demoGateway: demoGateway, ), child: const Text('open'), ), ); } test('onboardingSeededProfileId maps surfaced providers to profiles', () { expect( onboardingSeededProfileId(InferenceProviderType.melious), profileMeliousId, ); expect( onboardingSeededProfileId(InferenceProviderType.mistral), profileMistralEuId, ); expect( onboardingSeededProfileId(InferenceProviderType.gemini), profileGeminiFlashId, ); expect( onboardingSeededProfileId(InferenceProviderType.alibaba), profileAlibabaId, ); expect(onboardingSeededProfileId(InferenceProviderType.whisper), isNull); }); Future openWelcome( WidgetTester tester, { Widget? child, ThemeData? theme, List overrides = const [], MediaQueryData mediaQuery = mq, }) async { final launchHost = child ?? host(); await tester.pumpWidget( theme == null ? makeTestableWidget( launchHost, mediaQueryData: mediaQuery, overrides: overrides, ) : makeTestableWidgetNoScroll( launchHost, mediaQueryData: mediaQuery, theme: theme, ), ); await tester.tap(find.text('open')); await tester.pumpAndSettle(); } Future tapVisible(WidgetTester tester, Finder finder) async { await tester.ensureVisible(finder); await tester.pumpAndSettle(); await tester.tap(finder); await tester.pumpAndSettle(); } /// From the welcome to the one-key step: one tap. Future toConnect(WidgetTester tester) async { await tapVisible(tester, find.text('Get started with one key')); } testWidgets('shows the welcome promise and records welcomeShown', ( tester, ) async { await openWelcome(tester); expect(find.text("Hi, I'm Lotti."), findsOneWidget); expect(find.text('Get started with one key'), findsOneWidget); // She leads onboarding whatever her flag says elsewhere. expect( tester.widget(find.byType(OnboardingCompanion)).beat, OnboardingCompanionBeat.welcome, ); final state = await repo.funnelState(); expect(state.reached(OnboardingEventName.welcomeShown), isTrue); }); for (final theme in [ ( name: 'light', data: DesignSystemTheme.light(), tokens: dsTokensLight, ), ( name: 'dark', data: DesignSystemTheme.dark(), tokens: dsTokensDark, ), ]) { testWidgets('welcome and provider steps follow the ${theme.name} theme', ( tester, ) async { await openWelcome(tester, theme: theme.data); final welcomeTitle = tester.widget( find.text("Hi, I'm Lotti."), ); final welcomeSurface = tester.widget( find.ancestor( of: find.text("Hi, I'm Lotti."), matching: find.byType(ColoredBox), ), ); expect( welcomeSurface.color, theme.tokens.colors.background.level01, ); expect( welcomeTitle.style?.color, theme.tokens.colors.text.highEmphasis, ); // She stands in the hero frame in place of the artwork, in either theme. expect(find.byType(OnboardingCompanion), findsOneWidget); expect(find.byType(OnboardingThinkingBarsHero), findsNothing); await toConnect(tester); final providerTitle = tester.widget( find.text('One key is all it takes'), ); final providerSurface = tester.widget( find .descendant( of: find.byKey(const ValueKey('onboarding-connect')), matching: find.byType(ColoredBox), ) .first, ); expect( providerTitle.style?.color, theme.tokens.colors.text.highEmphasis, ); expect( providerSurface.color, theme.tokens.colors.background.level01, ); }); } testWidgets('advancing to connect reveals the primary providers and records ' 'providerModalShown', (tester) async { await openWelcome(tester); await toConnect(tester); // One recommended card; the rest wait behind the disclosure. expect(find.text('Mistral'), findsOneWidget); expect(find.text('Recommended'), findsOneWidget); expect(find.text('Gemini'), findsNothing); expect(find.text('Other providers'), findsOneWidget); final state = await repo.funnelState(); expect(state.reached(OnboardingEventName.providerModalShown), isTrue); }); testWidgets( 'swapping steps crossfades rather than snapping instantly', (tester) async { await openWelcome(tester); await tester.tap(find.text('Get started with one key')); await tester.pump(); // start the crossfade (t=0) await tester.pump(const Duration(milliseconds: 200)); // mid-fade // Mid-transition at least one child sits at a partial opacity — proof // the AnimatedSwitcher is actually easing between steps rather than // hard-cutting from one to the next. final opacities = tester .widgetList(find.byType(FadeTransition)) .map((f) => f.opacity.value) .toList(); expect( opacities.any((o) => o > 0.0 && o < 1.0), isTrue, reason: 'expected an in-progress crossfade, got $opacities', ); await tester.pumpAndSettle(); }, ); testWidgets( 'the outgoing step freezes its animations during the crossfade (so two ' 'animated backdrops never paint at once) and unfreezes once settled', (tester) async { await openWelcome(tester); await tester.tap(find.text('Get started with one key')); await tester.pump(); // start the crossfade await tester.pump(const Duration(milliseconds: 200)); // mid-fade // The outgoing welcome step is still mounted (fading out) and wrapped in // a disabled TickerMode, halting its looping constellation controller so // it stops repainting while the incoming connect backdrop animates in. final outgoingWelcome = find.text("Hi, I'm Lotti."); expect(outgoingWelcome, findsOneWidget); expect( find.ancestor( of: outgoingWelcome, matching: find.byWidgetPredicate( (w) => w is TickerMode && !w.enabled, ), ), findsOneWidget, ); // The incoming step is NOT frozen — its animations run. final incomingConnect = find.text('One key is all it takes'); expect(incomingConnect, findsOneWidget); expect( find.ancestor( of: incomingConnect, matching: find.byWidgetPredicate( (w) => w is TickerMode && !w.enabled, ), ), findsNothing, ); // Once the transition settles the outgoing step (and its disabled // TickerMode) are gone, so nothing is left frozen. await tester.pumpAndSettle(); expect(outgoingWelcome, findsNothing); expect( find.byWidgetPredicate((w) => w is TickerMode && !w.enabled), findsNothing, ); }, ); testWidgets('back from connect returns to the welcome', (tester) async { await openWelcome(tester); await toConnect(tester); expect(find.text('Mistral'), findsOneWidget); await tester.tap(find.byIcon(LottiIcons.back)); await tester.pumpAndSettle(); expect(find.text("Hi, I'm Lotti."), findsOneWidget); expect(find.text('Mistral'), findsNothing); }); testWidgets('one tap takes the welcome to the one key, with nothing ' 'optional in between', (tester) async { await openWelcome(tester); var state = await repo.funnelState(); expect(state.reached(OnboardingEventName.providerModalShown), isFalse); await tester.tap(find.text('Get started with one key')); await tester.pumpAndSettle(); expect(find.byKey(const ValueKey('onboarding-connect')), findsOneWidget); expect(find.text('One key is all it takes'), findsOneWidget); // Her look and her voices are settings, not steps of the first run. expect(find.text('Make me yours'), findsNothing); expect(find.text('Hear how I can sound'), findsNothing); state = await repo.funnelState(); expect(state.reached(OnboardingEventName.providerModalShown), isTrue); }); testWidgets('Other providers reveals OpenAI and Ollama', (tester) async { await openWelcome(tester); await toConnect(tester); expect(find.text('OpenAI'), findsNothing); await tester.tap(find.text('Other providers')); await tester.pumpAndSettle(); expect(find.text('OpenAI'), findsOneWidget); expect(find.text('Ollama'), findsOneWidget); }); testWidgets('selecting a provider opens the API-key step', (tester) async { await openWelcome(tester); await toConnect(tester); await tester.tap(find.text('Mistral')); await tester.pumpAndSettle(); await tapVisible(tester, find.text('Continue with Mistral')); expect(find.text('Paste your API key'), findsOneWidget); }); testWidgets('back from the API-key step returns to the providers', ( tester, ) async { await openWelcome(tester); await toConnect(tester); await tester.tap(find.text('Mistral')); await tester.pumpAndSettle(); await tapVisible(tester, find.text('Continue with Mistral')); expect(find.text('Paste your API key'), findsOneWidget); // The key step's back arrow returns to the provider list. await tester.tap(find.byIcon(LottiIcons.back)); await tester.pumpAndSettle(); expect(find.text('Paste your API key'), findsNothing); expect(find.text('Recommended'), findsOneWidget); }); testWidgets('skipping invokes onDismiss and records welcomeSkipped', ( tester, ) async { var dismissed = false; await openWelcome(tester, child: host(onDismiss: () => dismissed = true)); // The welcome's way out is the close button in its bar. await tester.tap(find.byIcon(LottiIcons.close)); await tester.pumpAndSettle(); expect(dismissed, isTrue); final state = await repo.funnelState(); expect(state.reached(OnboardingEventName.welcomeSkipped), isTrue); }); testWidgets('Escape skips like a tap outside the panel, even with a ' 'control inside it focused', (tester) async { var dismissed = false; await openWelcome(tester, child: host(onDismiss: () => dismissed = true)); // Tab into the panel, below its own Scaffold. await tester.sendKeyEvent(LogicalKeyboardKey.tab); await tester.pump(); expect( FocusManager.instance.primaryFocus?.context ?.findAncestorWidgetOfExactType(), isNotNull, ); await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pumpAndSettle(); expect(dismissed, isTrue); final state = await repo.funnelState(); expect(state.reached(OnboardingEventName.welcomeSkipped), isTrue); }); testWidgets('past the welcome, Escape and system back go back one step ' 'like the arrow, and do not close the flow', (tester) async { var dismissed = false; await openWelcome(tester, child: host(onDismiss: () => dismissed = true)); await toConnect(tester); await tapVisible(tester, find.text('Continue with Mistral')); expect(find.text('Paste your API key'), findsOneWidget); // Escape from the key step: back to the providers, nothing lost. await tester.sendKeyEvent(LogicalKeyboardKey.escape); await tester.pumpAndSettle(); expect(find.text('Paste your API key'), findsNothing); expect(find.byKey(const ValueKey('onboarding-connect')), findsOneWidget); // System back from the providers: back to the welcome. await tester.binding.handlePopRoute(); await tester.pumpAndSettle(); expect(find.byKey(const ValueKey('onboarding-welcome')), findsOneWidget); expect(dismissed, isFalse); // On the welcome, which has no way back, it closes the flow. await tester.binding.handlePopRoute(); await tester.pumpAndSettle(); expect(find.byKey(const ValueKey('onboarding-welcome')), findsNothing); expect(dismissed, isTrue); }); testWidgets('Explore with sample data pops the modal, enters the demo, ' 'and records NEITHER completed nor skipped', (tester) async { var dismissed = false; var completed = false; final gateway = MockDemoModeGateway(); when(gateway.demoMediaBytesAwaitingChoice).thenAnswer((_) async => 0); when( () => gateway.enterDemo( locale: any(named: 'locale'), downloadMedia: any(named: 'downloadMedia'), ), ).thenAnswer((_) async {}); await openWelcome( tester, child: host( onDismiss: () => dismissed = true, onCompleted: () => completed = true, demoGateway: gateway, ), ); await tester.ensureVisible(find.text('Explore with sample data')); await tester.pumpAndSettle(); await tester.tap(find.text('Explore with sample data')); // Bounded pumps: the demo-entry progress route's spinner never settles. await tester.pump(); await tester.pump(const Duration(milliseconds: 400)); await tester.pump(const Duration(milliseconds: 400)); verify( () => gateway.enterDemo( locale: any(named: 'locale'), downloadMedia: any(named: 'downloadMedia'), ), ).called(1); expect(dismissed, isFalse, reason: 'demo entry is not a skip'); expect(completed, isFalse, reason: 'demo entry is not a completion'); final state = await repo.funnelState(); expect( state.reached(OnboardingEventName.welcomeSkipped), isFalse, reason: 'the FTUE auto-show budget must survive a demo detour', ); }); testWidgets('first-launch demo entry asks before downloading its photos, ' 'showing the size (App Store guideline 4.2.3(ii))', (tester) async { final gateway = MockDemoModeGateway(); when( gateway.demoMediaBytesAwaitingChoice, ).thenAnswer((_) async => 26544434); when( () => gateway.enterDemo( locale: any(named: 'locale'), downloadMedia: any(named: 'downloadMedia'), ), ).thenAnswer((_) async {}); await openWelcome( tester, child: host(onDismiss: () {}, demoGateway: gateway), ); await tester.ensureVisible(find.text('Explore with sample data')); await tester.pumpAndSettle(); await tester.tap(find.text('Explore with sample data')); await tester.pump(); await tester.pump(const Duration(milliseconds: 400)); await tester.pump(const Duration(milliseconds: 400)); expect(find.text('Download the demo photos?'), findsOneWidget); expect(find.textContaining('a 26.6 MB download'), findsOneWidget); verifyNever( () => gateway.enterDemo( locale: any(named: 'locale'), downloadMedia: any(named: 'downloadMedia'), ), ); await tester.tap(find.text('Download (26.6 MB)')); await tester.pump(); await tester.pump(const Duration(milliseconds: 400)); verify( () => gateway.enterDemo( locale: any(named: 'locale'), downloadMedia: true, ), ).called(1); }); testWidgets('on a phone the panel takes the screen below the status bar, ' 'on every step it is opened on', (tester) async { const phone = MediaQueryData( size: Size(390, 1400), padding: EdgeInsets.only(top: 50), disableAnimations: true, ); await openWelcome(tester, mediaQuery: phone); Rect panel(String key) => tester.getRect(find.byKey(ValueKey(key))); expect(panel('onboarding-welcome'), const Rect.fromLTWH(0, 50, 390, 1350)); await toConnect(tester); expect(panel('onboarding-connect'), const Rect.fromLTWH(0, 50, 390, 1350)); }); testWidgets('with the keyboard up on a phone, the room it takes is added ' 'below the panel so the key field can be scrolled clear', ( tester, ) async { await openWelcome( tester, mediaQuery: const MediaQueryData( size: Size(390, 1400), viewInsets: EdgeInsets.only(bottom: 300), disableAnimations: true, ), ); final scroll = tester.widget( find.ancestor( of: find.byKey(const ValueKey('onboarding-welcome')), matching: find.byType(SingleChildScrollView), ), ); expect(scroll.padding, const EdgeInsets.only(bottom: 300)); }); testWidgets( 'in a window, tapping outside the panel closes the flow ' '(tap-outside-to-dismiss)', (tester) async { var dismissed = false; await openWelcome( tester, child: host(onDismiss: () => dismissed = true), mediaQuery: const MediaQueryData( size: Size(1000, 1400), disableAnimations: true, ), ); expect(find.text('Get started with one key'), findsOneWidget); // The panel is a dialog in the middle of the window, with the dimmed // app around it. A tap there must close the flow — the route's modal // barrier alone never received the tap (the full-screen scroll view sat // on top of it), so the scaffold dismisses explicitly. final panelLeft = tester .getTopLeft(find.byKey(const ValueKey('onboarding-welcome'))) .dx; expect(panelLeft, greaterThan(30)); await tester.tapAt(Offset(panelLeft - 20, 700)); await tester.pumpAndSettle(); expect(find.text('Get started with one key'), findsNothing); expect(dismissed, isTrue); final state = await repo.funnelState(); expect(state.reached(OnboardingEventName.welcomeSkipped), isTrue); }, ); /// Drives the full flow welcome → connect → Ollama → verified → Connect → /// category → Continue, landing on the in-panel first-task step. Ollama needs no key and its FTUE setup only touches the /// category repository, so it's the cheapest provider to drive. Returns the /// mocks the caller asserts against. Future< ({ MockAiConfigRepository aiRepo, MockCategoryRepository catRepo, MockOnboardingCaptureToTaskService captureService, }) > driveToFirstTaskStep( WidgetTester tester, { List existingCategories = const [], bool failCategoryWrites = false, VoidCallback? onCompleted, Completer>? categoriesGate, }) async { final aiRepo = MockAiConfigRepository(); when(() => aiRepo.saveConfig(any())).thenAnswer((_) async {}); // The key step un-deletes this provider's bundled profile before the // FTUE setup seeds. when(() => aiRepo.restoreConfig(any())).thenAnswer((_) async {}); final catRepo = MockCategoryRepository(); // The duplicate check consults the unfiltered set (deleted/hidden rows // still trip the UNIQUE(name) constraint). when( catRepo.getAllCategoriesIncludingHidden, ).thenAnswer( (_) => categoriesGate?.future ?? Future.value(existingCategories), ); // Reused categories are resurrected/rebound through updateCategory; // echo back the updated definition like the real repository does. when(() => catRepo.updateCategory(any())).thenAnswer( (invocation) async => invocation.positionalArguments.first as CategoryDefinition, ); if (failCategoryWrites) { when( () => catRepo.createCategory( name: any(named: 'name'), color: any(named: 'color'), defaultProfileId: any(named: 'defaultProfileId'), defaultTemplateId: any(named: 'defaultTemplateId'), automaticInferenceEnabled: any(named: 'automaticInferenceEnabled'), ), ).thenThrow(Exception('category db down')); } else { when( () => catRepo.createCategory( name: any(named: 'name'), color: any(named: 'color'), defaultProfileId: any(named: 'defaultProfileId'), defaultTemplateId: any(named: 'defaultTemplateId'), automaticInferenceEnabled: any(named: 'automaticInferenceEnabled'), ), ).thenAnswer( (_) async => CategoryTestUtils.createTestCategory(id: 'c1', name: 'AI'), ); } final captureService = MockOnboardingCaptureToTaskService(); await tester.pumpWidget( makeTestableWidget( host(onCompleted: onCompleted), mediaQueryData: mq, overrides: [ aiConfigRepositoryProvider.overrideWithValue(aiRepo), categoryRepositoryProvider.overrideWithValue(catRepo), // The category step now advances to the in-panel first-task step; // override its providers so the step renders without the real mic // pipeline or a live structuring round-trip. captureControllerProvider.overrideWith(FakeCaptureController.new), onboardingCaptureToTaskServiceProvider.overrideWithValue( captureService, ), connectionVerifierClientProvider.overrideWith( (ref) => () => MockClient((_) async => http.Response('', 200)), ), connectionProbeRegistryProvider.overrideWith( (ref) => { InferenceProviderType.ollama: _FakeProbe( const ConnectionCheckVerified( modelCount: 2, latency: Duration(milliseconds: 5), ), ), }, ), ], ), ); await tester.tap(find.text('open')); await tester.pumpAndSettle(); await toConnect(tester); await tester.tap(find.text('Other providers')); await tester.pumpAndSettle(); await tester.ensureVisible(find.text('Ollama')); await tester.pumpAndSettle(); await tester.tap(find.text('Ollama')); await tester.pumpAndSettle(); await tapVisible(tester, find.text('Continue with Ollama')); // Ollama probes reachability on open; let the ≥1s checking dwell elapse. await tester.pump(const Duration(milliseconds: 1100)); await tester.pumpAndSettle(); expect(find.text('Connection verified'), findsOneWidget); await tester.tap(find.textContaining('Continue with')); await tester.pumpAndSettle(); // Connect leads straight into the category step: no success screen // waits to be tapped away, and her bubble there says the key worked. expect(find.text("You're all set"), findsNothing); expect(find.text('Get started'), findsNothing); expect(find.text('Where should your AI work?'), findsOneWidget); expect(find.textContaining("We're connected"), findsOneWidget); // The "why areas?" disclosure opens the per-category-AI explanation, // then dismisses. await tester.tap(find.text('Why areas?')); await tester.pumpAndSettle(); expect(find.textContaining('own AI'), findsOneWidget); await tester.tap(find.text('OK')); await tester.pumpAndSettle(); // "Add your own" opens a dialog. Cancelling adds nothing… await tester.tap(find.text('Add your own')); await tester.pumpAndSettle(); await tester.tap(find.text('Cancel')); await tester.pumpAndSettle(); expect(find.text('Hobbies'), findsNothing); // …while a typed name becomes a selected custom area. await tester.tap(find.text('Add your own')); await tester.pumpAndSettle(); await tester.enterText(find.byType(TextField), 'Hobbies'); await tester.tap(find.text('OK')); await tester.pumpAndSettle(); expect(find.text('Hobbies'), findsOneWidget); // Pick a preset area too, then continue with both selected. The flow // advances in place — the first-task finale stays inside the panel. Its // recording visual's tickers never settle, so step the crossfade with // bounded pumps rather than pumpAndSettle. await tester.tap(find.text('Work')); await tester.pump(); await tester.tap(find.text('Continue')); await tester.pump(); await tester.pump(const Duration(milliseconds: 400)); await tester.pump(const Duration(milliseconds: 400)); return (aiRepo: aiRepo, catRepo: catRepo, captureService: captureService); } /// Opens the flow and connects Ollama, landing on the category step. The /// profile resolver decides whether the connected provider has a voice. Future connectOllama( WidgetTester tester, { required MockProfileResolver resolver, }) async { final aiRepo = MockAiConfigRepository(); when(() => aiRepo.saveConfig(any())).thenAnswer((_) async {}); when(() => aiRepo.restoreConfig(any())).thenAnswer((_) async {}); final catRepo = MockCategoryRepository(); final cloudFile = MockIoFile(); when(() => cloudFile.path).thenReturn('/tmp/first_words.wav'); when(cloudFile.existsSync).thenReturn(false); await tester.pumpWidget( makeTestableWidget( host(), mediaQueryData: mq, overrides: [ aiConfigRepositoryProvider.overrideWithValue(aiRepo), categoryRepositoryProvider.overrideWithValue(catRepo), profileResolverProvider.overrideWithValue(resolver), cloudSpeechSynthesizerProvider.overrideWithValue( FakeCloudSpeechSynthesizer()..output = cloudFile, ), spokenTrackLoaderProvider.overrideWithValue( (clip, text) => Completer().future, ), connectionVerifierClientProvider.overrideWith( (ref) => () => MockClient((_) async => http.Response('', 200)), ), connectionProbeRegistryProvider.overrideWith( (ref) => { InferenceProviderType.ollama: _FakeProbe( const ConnectionCheckVerified( modelCount: 2, latency: Duration(milliseconds: 5), ), ), }, ), ], ), ); await tester.tap(find.text('open')); await tester.pumpAndSettle(); await toConnect(tester); await tapVisible(tester, find.text('Other providers')); await tapVisible(tester, find.text('Ollama')); await tapVisible(tester, find.text('Continue with Ollama')); await tester.pump(const Duration(milliseconds: 1100)); await tester.pumpAndSettle(); expect( tester.widget(find.byType(OnboardingCompanion)).beat, OnboardingCompanionBeat.keyVerified, ); await tapVisible(tester, find.textContaining('Continue with')); } const areasExplanation = 'Each area of your life gets its own space.'; testWidgets('she reacts to the key check and, connected to a provider ' 'without a voice, opens the areas saying she is ready', (tester) async { final resolver = MockProfileResolver(); when( () => resolver.resolveByProfileId(profileLocalId), ).thenAnswer((_) async => null); await connectOllama(tester, resolver: resolver); final her = tester.widget( find.byType(OnboardingCompanion), ); expect(her.beat, OnboardingCompanionBeat.areas); expect(her.framing, CompanionFraming.full); expect(her.cloudVoice, isNull); expect( her.line, "We're connected, and I'm ready to help. $areasExplanation", ); // Her line is the caption on screen. expect( tester .widget(find.byType(OnboardingSpeechBubble)) .text, her.line, ); verify(() => resolver.resolveByProfileId(profileLocalId)).called(1); }); testWidgets('while the connected profile is still being resolved her ' 'bubble shows the line but she does not say it; she says it, in the ' 'cloud voice, once it has', (tester) async { final provider = testInferenceProvider( id: 'voiced', inferenceProviderType: InferenceProviderType.mistral, ); final resolving = Completer(); final resolver = MockProfileResolver(); when( () => resolver.resolveByProfileId(profileLocalId), ).thenAnswer((_) => resolving.future); await connectOllama(tester, resolver: resolver); OnboardingCompanion her() => tester.widget(find.byType(OnboardingCompanion)); String? caption() => tester .widget(find.byType(OnboardingSpeechBubble)) .text; expect(her().line, isNull); expect(her().cloudVoice, isNull); expect( caption(), "We're connected, and I'm ready to help. $areasExplanation", ); resolving.complete( ResolvedProfile( thinkingModelId: 'thinking', thinkingProvider: provider, speechModelId: 'voxtral-mini-tts-3-0', speechProvider: provider, speechModel: testAiModel( id: 'voice-row', providerModelId: 'voxtral-mini-tts-3-0', ), ), ); await tester.pumpAndSettle(); expect(her().line, 'There, now you can hear me too! $areasExplanation'); expect(her().cloudVoice?.provider, provider); expect(caption(), her().line); }); testWidgets('connected to a provider whose profile names a read-aloud ' 'voice, she opens the areas with her first words in that voice', ( tester, ) async { final provider = testInferenceProvider( id: 'voiced', inferenceProviderType: InferenceProviderType.mistral, ); final resolver = MockProfileResolver(); when(() => resolver.resolveByProfileId(profileLocalId)).thenAnswer( (_) async => ResolvedProfile( thinkingModelId: 'thinking', thinkingProvider: provider, speechModelId: 'voxtral-mini-tts-3-0', speechProvider: provider, speechModel: testAiModel( id: 'voice-row', providerModelId: 'voxtral-mini-tts-3-0', ), ), ); await connectOllama(tester, resolver: resolver); final her = tester.widget( find.byType(OnboardingCompanion), ); expect(her.beat, OnboardingCompanionBeat.areas); expect(her.cloudVoice?.provider, provider); expect(her.cloudVoice?.model.providerModelId, 'voxtral-mini-tts-3-0'); expect(her.cloudVoice?.languageCode, 'en'); expect(her.line, 'There, now you can hear me too! $areasExplanation'); expect( tester .widget(find.byType(OnboardingSpeechBubble)) .text, her.line, ); }); testWidgets('the category step advances to the in-panel first-task step and ' 'a landed task pops the modal onto the task page', (tester) async { final beamed = []; beamToNamedOverride = beamed.add; addTearDown(() => beamToNamedOverride = null); var completedCount = 0; final mocks = await driveToFirstTaskStep( tester, onCompleted: () => completedCount++, ); // The finale renders inside the panel: guided suggestions over the same // modal surface, no full-screen takeover. Provider creation + FTUE setup // ran; both chosen areas became categories bound to the provider's seeded // inference profile. expect(find.text('Create your first task'), findsOneWidget); expect(find.text('Plan my week'), findsOneWidget); verify(() => mocks.aiRepo.saveConfig(any())).called(1); verify( () => mocks.catRepo.createCategory( name: 'Work', color: any(named: 'color'), defaultProfileId: profileLocalId, defaultTemplateId: lauraTemplateId, // Onboarding is the one caller that opts new areas into automation. automaticInferenceEnabled: true, ), ).called(1); verify( () => mocks.catRepo.createCategory( name: 'Hobbies', color: any(named: 'color'), defaultProfileId: profileLocalId, defaultTemplateId: lauraTemplateId, automaticInferenceEnabled: true, ), ).called(1); // A tapped starter suggestion rides the typed path through structuring; // the landed task is revealed inside the panel as a tappable card. when( () => mocks.captureService.createTaskFromTranscript( transcript: any(named: 'transcript'), categoryId: any(named: 'categoryId'), providerName: any(named: 'providerName'), audioId: any(named: 'audioId'), ), ).thenAnswer( (_) async => OnboardingCaptureResult( task: MockTask(id: 'task-1'), title: 'Plan the week', ), ); await tester.tap(find.text('Plan my week'), warnIfMissed: false); await tester.pump(); await tester.pump(); await tester.pump(const Duration(milliseconds: 400)); // The created beat stays inside the dialogue: the card shows the task and // the modal is still up until the user taps it. expect(find.text('Your first task is ready'), findsOneWidget); expect(find.text('Plan the week'), findsOneWidget); // The checklist is deliberately NOT previewed on the created card — it // lands on the task page as confirmable proposals instead. expect(find.text('Monday'), findsNothing); expect(beamed, isEmpty); // The main button pops the modal and deep-links to the real task page. await tester.tap(find.text('Open task'), warnIfMissed: false); await tester.pump(); await tester.pump(const Duration(milliseconds: 400)); expect(find.text('Create your first task'), findsNothing); expect(beamed, ['/tasks/task-1']); verify( () => mocks.captureService.createTaskFromTranscript( transcript: 'Plan my week', categoryId: 'c1', providerName: 'Ollama', // ignore: avoid_redundant_argument_values audioId: null, ), ).called(1); // Connecting a provider drove the flow to completion: onCompleted fires // exactly once when the modal pops, so the caller can retire the welcome. expect(completedCount, 1); final state = await repo.funnelState(); expect(state.reached(OnboardingEventName.providerConnected), isTrue); }); testWidgets('a total structuring failure finishes onboarding without ' 'navigating anywhere', (tester) async { final beamed = []; beamToNamedOverride = beamed.add; addTearDown(() => beamToNamedOverride = null); final mocks = await driveToFirstTaskStep(tester); expect(find.text('Create your first task'), findsOneWidget); // The orchestrator couldn't land even a floor task — the step finishes // onboarding (the modal pops); connected is still recorded, but there is // no task page to open. when( () => mocks.captureService.createTaskFromTranscript( transcript: any(named: 'transcript'), categoryId: any(named: 'categoryId'), providerName: any(named: 'providerName'), audioId: any(named: 'audioId'), ), ).thenAnswer( (_) async => const OnboardingCaptureResult( task: null, title: '', ), ); await tester.tap(find.text('Plan my week'), warnIfMissed: false); await tester.pump(); await tester.pump(); await tester.pump(const Duration(milliseconds: 400)); expect(find.text('Create your first task'), findsNothing); expect(beamed, isEmpty); final state = await repo.funnelState(); expect(state.reached(OnboardingEventName.providerConnected), isTrue); }); testWidgets('the category step reuses an existing category with the same ' 'name (case-insensitively) instead of tripping the unique constraint', ( tester, ) async { // The user once had a "work" category, since archived and soft-deleted, // still bound to a stale profile. Its row keeps the UNIQUE name, so a // blind create would throw and Continue used to die silently. The reuse // must resurrect it (undelete + reactivate) and rebind the just-seeded // profile so first-task structuring can actually run. final mocks = await driveToFirstTaskStep( tester, existingCategories: [ CategoryTestUtils.createTestCategory( id: 'existing-work', name: 'work', active: false, deletedAt: DateTime(2025), defaultProfileId: 'stale-profile', ), ], ); // The flow still advanced into the first-task step… expect(find.text('Create your first task'), findsOneWidget); // …"Work" was reused, only the custom "Hobbies" area was created… verifyNever( () => mocks.catRepo.createCategory( name: 'Work', color: any(named: 'color'), defaultProfileId: any(named: 'defaultProfileId'), defaultTemplateId: any(named: 'defaultTemplateId'), automaticInferenceEnabled: any(named: 'automaticInferenceEnabled'), ), ); verify( () => mocks.catRepo.createCategory( name: 'Hobbies', color: any(named: 'color'), defaultProfileId: profileLocalId, defaultTemplateId: lauraTemplateId, automaticInferenceEnabled: true, ), ).called(1); // …resurrected, reactivated, rebound to the seeded profile, and given // Laura as the default task-agent template (it had none of its own). final updated = verify( () => mocks.catRepo.updateCategory(captureAny()), ).captured.single as CategoryDefinition; expect(updated.id, 'existing-work'); expect(updated.deletedAt, isNull); expect(updated.active, isTrue); expect(updated.defaultProfileId, profileLocalId); expect(updated.defaultTemplateId, lauraTemplateId); // A category that never decided gets automation on, like a fresh one. expect(updated.automaticInferenceEnabled, isTrue); // …and the reused category is the pre-selected destination, so the // structured task lands in the user's real existing area. when( () => mocks.captureService.createTaskFromTranscript( transcript: any(named: 'transcript'), categoryId: any(named: 'categoryId'), providerName: any(named: 'providerName'), audioId: any(named: 'audioId'), ), ).thenAnswer( (_) async => OnboardingCaptureResult( task: MockTask(id: 'task-2'), title: 'Planned', ), ); await tester.tap(find.text('Plan my week'), warnIfMissed: false); await tester.pump(); await tester.pump(); await tester.pump(const Duration(milliseconds: 400)); verify( () => mocks.captureService.createTaskFromTranscript( transcript: 'Plan my week', categoryId: 'existing-work', providerName: 'Ollama', // ignore: avoid_redundant_argument_values audioId: null, ), ).called(1); }); // Automation is opt-in per category, and onboarding turns it on for the // areas it creates. Reuse is the one case where the user may already have // answered — and an explicit "off" must survive, or onboarding becomes a // back door around the switch. testWidgets('reusing a category respects an explicit automation opt-out', ( tester, ) async { final mocks = await driveToFirstTaskStep( tester, existingCategories: [ CategoryTestUtils.createTestCategory( id: 'existing-work', name: 'work', automaticInferenceEnabled: false, ), ], ); final updated = verify( () => mocks.catRepo.updateCategory(captureAny()), ).captured.single as CategoryDefinition; expect(updated.id, 'existing-work'); // Rebound to the seeded profile like any reused area… expect(updated.defaultProfileId, profileLocalId); // …but the user's "no" is left standing. expect(updated.automaticInferenceEnabled, isFalse); }); testWidgets('reusing a category preserves a user-chosen default agent ' 'template instead of overwriting it with Laura', (tester) async { final mocks = await driveToFirstTaskStep( tester, existingCategories: [ CategoryTestUtils.createTestCategory( id: 'existing-work', name: 'work', defaultTemplateId: 'template-tom-001', ), ], ); final updated = verify( () => mocks.catRepo.updateCategory(captureAny()), ).captured.single as CategoryDefinition; expect(updated.id, 'existing-work'); // The profile is rebound (structuring must run), but the user's own // template choice survives the reuse. expect(updated.defaultProfileId, profileLocalId); expect(updated.defaultTemplateId, 'template-tom-001'); }); testWidgets('while the areas are being written the close button is off, ' 'and the flow goes on to the first task once they are', (tester) async { final gate = Completer>(); await driveToFirstTaskStep(tester, categoriesGate: gate); // Continue is in flight: still on the areas, and no way to close the // flow from under the write. expect(find.text('Where should your AI work?'), findsOneWidget); // The button keeps its place in the bar, disabled. expect( tester .widget(find.widgetWithIcon(IconButton, LottiIcons.close)) .onPressed, isNull, ); gate.complete(const []); await tester.pump(); await tester.pump(const Duration(milliseconds: 400)); await tester.pump(const Duration(milliseconds: 400)); expect(find.text('Create your first task'), findsOneWidget); }); testWidgets('a tap outside the panel closes the flow on the welcome only: ' 'past it, one stray click throws nothing away', (tester) async { var dismissed = false; await openWelcome( tester, child: host(onDismiss: () => dismissed = true), mediaQuery: const MediaQueryData( size: Size(1000, 1400), disableAnimations: true, ), ); await toConnect(tester); final left = tester .getTopLeft(find.byKey(const ValueKey('onboarding-connect'))) .dx; await tester.tapAt(Offset(left - 20, 700)); await tester.pumpAndSettle(); expect(find.byKey(const ValueKey('onboarding-connect')), findsOneWidget); expect(dismissed, isFalse); // Back on the welcome, it closes again. await tester.tap(find.byIcon(LottiIcons.back)); await tester.pumpAndSettle(); await tester.tapAt(Offset(left - 20, 700)); await tester.pumpAndSettle(); expect(dismissed, isTrue); }); testWidgets('back from the first-task prompt returns to the areas', ( tester, ) async { await driveToFirstTaskStep(tester); expect(find.text('Create your first task'), findsOneWidget); await tester.tap(find.byIcon(LottiIcons.back)); await tester.pump(); await tester.pump(const Duration(milliseconds: 400)); await tester.pump(const Duration(milliseconds: 400)); expect(find.text('Where should your AI work?'), findsOneWidget); }); testWidgets('the close button on the first task ends the flow as ' 'completed, the key kept', (tester) async { var completed = 0; final mocks = await driveToFirstTaskStep( tester, onCompleted: () => completed++, ); expect(find.text('Create your first task'), findsOneWidget); await tester.tap(find.byIcon(LottiIcons.close)); await tester.pump(); await tester.pump(const Duration(milliseconds: 400)); await tester.pump(const Duration(milliseconds: 400)); expect(find.text('Create your first task'), findsNothing); expect(completed, 1); verify(() => mocks.aiRepo.saveConfig(any())).called(1); }); testWidgets('on the areas, where the arrow is disabled, system back does ' 'nothing rather than end onboarding', (tester) async { var completed = 0; final gate = Completer>(); await driveToFirstTaskStep( tester, categoriesGate: gate, onCompleted: () => completed++, ); // Hold the step on the areas by not letting Continue finish yet. expect(find.text('Where should your AI work?'), findsOneWidget); await tester.binding.handlePopRoute(); await tester.pump(); await tester.pump(const Duration(milliseconds: 400)); expect(find.text('Where should your AI work?'), findsOneWidget); expect(completed, 0); gate.complete(const []); await tester.pump(); await tester.pump(const Duration(milliseconds: 400)); await tester.pump(const Duration(milliseconds: 400)); }); testWidgets('a category write failure surfaces an error toast and keeps ' 'the category step usable', (tester) async { await driveToFirstTaskStep(tester, failCategoryWrites: true); // Continue failed: the flow did NOT advance to the first-task step, the // category step is still up for a retry, and the failure surfaced as an // error toast instead of dying silently under the button. expect(find.text('Create your first task'), findsNothing); expect(find.text('Where should your AI work?'), findsOneWidget); expect(find.byType(DesignSystemToast), findsOneWidget); expect( tester.widget(find.byType(DesignSystemToast)).tone, DesignSystemToastTone.error, ); // Let the toast's display timer expire so the test tears down cleanly. await tester.pump(const Duration(seconds: 10)); await tester.pump(const Duration(seconds: 1)); }); testWidgets('falls back to the getIt-registered metrics repo', ( tester, ) async { // No `metrics:` argument → show() resolves the repo from getIt. getIt.registerSingleton(repo); addTearDown(() => getIt.unregister()); await tester.pumpWidget( makeTestableWidget( Builder( builder: (context) => ElevatedButton( onPressed: () => OnboardingWelcomeModal.show(context, onDismiss: () {}), child: const Text('open'), ), ), mediaQueryData: mq, ), ); await tester.tap(find.text('open')); await tester.pumpAndSettle(); expect(find.text("Hi, I'm Lotti."), findsOneWidget); final state = await repo.funnelState(); expect(state.reached(OnboardingEventName.welcomeShown), isTrue); }); testWidgets('renders the desktop (centred) layout on a wide viewport', ( tester, ) async { tester.view ..physicalSize = const Size(1000, 800) ..devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); await tester.pumpWidget( makeTestableWidget( host(), mediaQueryData: const MediaQueryData( size: Size(1000, 800), disableAnimations: true, ), ), ); await tester.tap(find.text('open')); await tester.pumpAndSettle(); // The wide branch of the scaffold renders the centred panel. expect(find.text("Hi, I'm Lotti."), findsOneWidget); // Tapping a non-interactive area of the panel is swallowed (the panel's // opaque no-op tap), so the flow stays open — not dismissed. await tester.tap(find.text("Hi, I'm Lotti.")); await tester.pumpAndSettle(); expect(find.text("Hi, I'm Lotti."), findsOneWidget); }); test( 'openOnboardingCreatedTask deep-links through the canonical task route', () { // The deep link (rather than a bare detail-stack push) also switches to // the Tasks destination, so the task is visible even when onboarding was // launched from another tab (e.g. Settings → Maintenance). final beamed = []; beamToNamedOverride = beamed.add; addTearDown(() => beamToNamedOverride = null); openOnboardingCreatedTask('task-9'); expect(beamed, ['/tasks/task-9']); }, ); testWidgets('she is the welcome hero with her greeting as her line, ' 'recommends the one key on connect, and greets only once', ( tester, ) async { tester.view ..physicalSize = const Size(390, 1000) ..devicePixelRatio = 1; addTearDown(tester.view.resetPhysicalSize); addTearDown(tester.view.resetDevicePixelRatio); await tester.pumpWidget( makeTestableWidget( host(), mediaQueryData: mq, overrides: [ // Her own flag off: onboarding has her regardless. configFlagProvider.overrideWith( (ref, flagName) => Stream.value(false), ), ttsPlaybackControllerProvider.overrideWith( () => TtsPlaybackController(logger: MockDomainLogger()), ), spokenTrackLoaderProvider.overrideWithValue( (clip, text) => Completer().future, ), ], ), ); await tester.tap(find.text('open')); // Her ticker never settles: pump frames rather than settle. for (var i = 0; i < 30; i++) { await tester.pump(const Duration(milliseconds: 16)); } OnboardingCompanion her() => tester.widget(find.byType(OnboardingCompanion)); expect(her().beat, OnboardingCompanionBeat.welcome); expect( her().line, "Hi, I'm Lotti. Tell me what's on your mind and I'll turn it into a " 'plan. All I need is one key.', ); Future frames() async { for (var i = 0; i < 40; i++) { await tester.pump(const Duration(milliseconds: 16)); } } await tester.tap(find.text('Get started with one key')); await frames(); // On the one-key step she recommends Mistral, standing as on every step. expect(her().beat, OnboardingCompanionBeat.recommend); expect( her().line, "I'd go with Mistral: with its one key I can think, transcribe what you " 'say and read aloud to you.', ); expect(her().framing, CompanionFraming.full); // Back on the welcome she waves again, but says the promise only once. await tester.tap(find.byIcon(LottiIcons.back)); await frames(); expect(her().beat, OnboardingCompanionBeat.welcome); expect(her().line, isNull); }); }