import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:myapp/main.dart'; import 'package:myapp/models/bank.dart'; import 'package:myapp/models/transaction_meta.dart'; import 'package:myapp/services/storage_service.dart'; import 'package:myapp/utils/constants.dart'; import 'package:myapp/utils/helpers.dart'; import 'package:myapp/utils/look_ahead.dart'; import 'package:shared_preferences/shared_preferences.dart'; void main() { TestWidgetsFlutterBinding.ensureInitialized(); Future pumpApp(WidgetTester tester) async { SharedPreferences.setMockInitialValues({}); final storage = StorageService(); await storage.init(); await tester.pumpWidget(MyApp(storage: storage)); } testWidgets('splash shows the app name then opens Home', (tester) async { await pumpApp(tester); expect(find.text(kAppName), findsWidgets); await tester.pump(kSplashDuration); await tester.pump(const Duration(milliseconds: 450)); await tester.pump(const Duration(milliseconds: 350)); expect(find.text('Home'), findsWidgets); expect(find.text('Overview'), findsOneWidget); expect(find.byTooltip('Settings'), findsOneWidget); }); test('ConnectStart.fromJson maps authUrl and state', () { final start = ConnectStart.fromJson({ 'authUrl': 'https://example.test/auth', 'state': 'abc', }); expect(start.authUrl, 'https://example.test/auth'); expect(start.state, 'abc'); }); test('BankTransaction match keys support auto-label and notes', () { final tx = BankTransaction.fromJson({ 'date': '2026-09-01', 'description': 'BCK*AH CAMPUS DIEMEN ZUID NLD Pasvolgnr: 001 Transactie: C00338123', 'amount': -3.5, 'currency': 'EUR', 'pending': false, }); expect(tx.descriptionKey, contains('pasvolgnr')); expect(tx.merchantKey, 'ah campus'); expect(tx.merchantKey.contains('pasvolgnr'), isFalse); expect(tx.exactKey, contains('-3.5')); expect(formatMoney(tx.amount, tx.currency), contains('EUR')); expect(kBuiltinLabels.first.name, 'Groceries'); }); test('merchant stems group Albert Heijn / AH across stores', () { final a = merchantKeyFromDescription('Albert Heijn 1562 Utrecht NLD'); final b = merchantKeyFromDescription('ALBERT HEIJN 1847 AMSTERDAM'); final c = merchantKeyFromDescription( 'BCK*AH CAMPUS DIEMEN ZUID NLD Pasvolgnr: 001 Transactie: X', ); final d = merchantKeyFromDescription('AH CAMPUS AMSTERDAM NLD'); expect(a, 'albert heijn'); expect(b, 'albert heijn'); expect(c, 'ah campus'); expect(d, 'ah campus'); expect(descriptionMatchesPattern('Albert Heijn 1562 Utrecht', a), isTrue); expect( descriptionMatchesPattern('Jumbo City Centrum', a), isFalse, ); expect( descriptionMatchesPattern('AH CAMPUS DIEMEN', '/ah campus/'), isTrue, ); }); test('flattenLabelTree indents children under parents', () { const parent = TxLabel( id: 'groceries', name: 'Groceries', color: Color(0xFF16A34A), ); const child = TxLabel( id: 'custom_ah', name: 'Albert Heijn', color: Color(0xFF0F766E), custom: true, parentId: 'groceries', ); const other = TxLabel( id: 'dining', name: 'Dining', color: Color(0xFFEA580C), ); final tree = flattenLabelTree([child, other, parent]); expect(tree.map((e) => e.label.id).toList(), [ 'dining', 'groceries', 'custom_ah', ]); expect(tree.map((e) => e.depth).toList(), [0, 0, 1]); }); test('look-ahead applies monthly recurring amounts', () { final items = [ const RecurringItem( description: 'Salary', amount: 2000, currency: 'EUR', dayOfMonth: 25, labelIds: ['salary'], ), const RecurringItem( description: 'Rent', amount: -900, currency: 'EUR', dayOfMonth: 1, labelIds: ['mortgage'], ), ]; final from = DateTime(2026, 9, 6); final through = DateTime(2026, 10, 31); final predicted = predictBalance( currentBalance: 1000, from: from, throughDate: through, items: items, ); // Sep 25 salary + Oct 1 rent + Oct 25 salary = 1000+2000-900+2000 expect(predicted, 4100); }); }