import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:machweis/core/auth/auth_client.dart';
import 'package:machweis/core/auth/auth_store.dart';
import 'package:machweis/core/storage/local_storage.dart';
import 'package:machweis/features/profile/data/company_store.dart';
import 'package:machweis/features/profile/presentation/profile_screen.dart';
import 'package:shared_preferences/shared_preferences.dart';
AuthSession _session(String tenantId, {String role = 'owner'}) => AuthSession(
access[REDACTED],
refresh[REDACTED],
userId: 'user-$tenantId',
email: '$tenantId@example.test',
tenantId: tenantId,
role: role,
);
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
test(
'Firmendaten bleiben beim Wechsel zwischen Mandanten getrennt',
() async {
SharedPreferences.setMockInitialValues({});
await LocalStorage.instance.initialize(forcePreferences: true);
final auth = AuthStore.instance;
final previousSession = auth.session;
try {
auth.session = _session('tenant-a');
await CompanyStore.instance.initialize();
expect(CompanyStore.instance.profile.name, isEmpty);
await CompanyStore.instance.save(
const CompanyProfile(
name: 'Firma A',
owner: 'Inhaber A',
email: '',
phone: '',
address: '',
taxNumber: '',
reportFooter: '',
),
);
auth.session = _session('tenant-b');
await CompanyStore.instance.initialize();
expect(CompanyStore.instance.profile.name, isEmpty);
await CompanyStore.instance.save(
const CompanyProfile(
name: 'Firma B',
owner: 'Inhaber B',
email: '',
phone: '',
address: '',
taxNumber: '',
reportFooter: '',
),
);
auth.session = _session('tenant-a');
await CompanyStore.instance.initialize();
expect(CompanyStore.instance.profile.name, 'Firma A');
expect(CompanyStore.instance.profile.owner, 'Inhaber A');
} finally {
auth.session = previousSession;
await CompanyStore.instance.initialize();
}
},
);
testWidgets('Firmendaten lassen sich im Dialog speichern', (tester) async {
SharedPreferences.setMockInitialValues({});
await LocalStorage.instance.initialize(forcePreferences: true);
final auth = AuthStore.instance;
final previousSession = auth.session;
auth.session = _session('tenant-dialog');
await CompanyStore.instance.initialize();
try {
await tester.pumpWidget(const MaterialApp(home: ProfileScreen()));
await tester.tap(find.text('Bearbeiten'));
await tester.pumpAndSettle();
await tester.enterText(
find.widgetWithText(TextField, 'Firmenname'),
'Neue Firma',
);
await tester.pump();
await tester.tap(find.widgetWithText(FilledButton, 'Speichern'));
await tester.pumpAndSettle();
expect(CompanyStore.instance.profile.name, 'Neue Firma');
expect(find.text('Neue Firma'), findsOneWidget);
} finally {
auth.session = previousSession;
await CompanyStore.instance.initialize();
}
});
testWidgets('Nur Firmen-Admins können Firmendaten bearbeiten', (
tester,
) async {
SharedPreferences.setMockInitialValues({});
await LocalStorage.instance.initialize(forcePreferences: true);
final auth = AuthStore.instance;
final previousSession = auth.session;
auth.session = _session('tenant-readonly', role: 'technician');
await CompanyStore.instance.initialize();
try {
await tester.pumpWidget(const MaterialApp(home: ProfileScreen()));
await tester.tap(find.text('Bearbeiten'));
await tester.pumpAndSettle();
expect(find.text('Firmendaten bearbeiten'), findsNothing);
} finally {
auth.session = previousSession;
await CompanyStore.instance.initialize();
}
});
}
