// SPDX-License-Identifier: AGPL-3.0-or-later import 'dart:convert'; import 'package:docwell_mobile/api/docwell_api.dart'; import 'package:docwell_mobile/main.dart'; import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:http/http.dart' as http; import 'package:http/testing.dart'; const ann = { 'id': 'u1', 'email': 'ann@docwell.test', 'name': 'Ann', 'avatar': '', 'locale': 'en', 'theme': 'system', 'status': 'active', 'verified': true, 'created': '2026-10-01 09:00:00.000Z', 'role': 'admin', 'organization': 'o1', 'organizationName': 'Acme', }; http.Response json(int status, Object body) => http.Response( jsonEncode(body), status, headers: {'content-type': 'application/json'}, ); DocwellApi fakeApi() => DocwellApi( 'http://server.test/', client: MockClient((req) async { switch (req.url.path) { case '/api/v1/auth/login': final body = jsonDecode(req.body) as Map; if (body['password'] == 'password123') { return json(200, {'token': 'tok', 'user': ann}); } return json(401, {'error': 'invalid_credentials'}); case '/api/v1/version': return req.headers['Authorization'] == 'Bearer tok' ? json(200, {'name': 'docwell-server', 'version': '1.0.0'}) : json(401, {'error': 'unauthorized'}); } return json(404, {'error': 'not_found'}); }), ); Future signIn(WidgetTester tester, String password) async { await tester.enterText(find.byKey(const Key('email')), 'ann@docwell.test'); await tester.enterText(find.byKey(const Key('password')), password); await tester.tap(find.widgetWithText(FilledButton, 'Sign in')); await tester.pumpAndSettle(); } void main() { testWidgets('signs in, shows the user, and signs out', (tester) async { await tester.pumpWidget(DocwellApp(api: fakeApi())); await signIn(tester, 'password123'); expect(find.text('Hello, Ann'), findsOneWidget); expect(find.text('Admin · Acme'), findsOneWidget); expect(find.text('Connected · v1.0.0'), findsOneWidget); await tester.tap(find.text('Sign out')); await tester.pumpAndSettle(); expect(find.text('Sign in to Docwell'), findsOneWidget); }); testWidgets('shows an error for a wrong password', (tester) async { await tester.pumpWidget(DocwellApp(api: fakeApi())); await signIn(tester, 'wrong'); expect(find.text('Wrong email or password.'), findsOneWidget); }); }