mirror of
https://github.com/jonasbark/swiftcontrol.git
synced 2026-12-02 10:09:09 +01:00
Connect an account in Settings > Connected accounts (in the browser, through bikecontrol.app/connect/<service>); rides upload by hand from Export or automatically once saved. Tokens stay in the activity-sync function. Garmin has no upload API for us and its app files a shared .fit as a course, so the Garmin row saves the file and opens Connect's import page. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
100 lines
4.0 KiB
Dart
100 lines
4.0 KiB
Dart
import 'dart:convert';
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:bike_control/gen/l10n.dart';
|
|
import 'package:bike_control/main.dart' show OtherLocalizationsDelegate;
|
|
import 'package:bike_control/services/activity_sync/activity_sync_api.dart';
|
|
import 'package:bike_control/services/activity_sync/activity_sync_service.dart';
|
|
import 'package:bike_control/widgets/activity_sync/activity_sync_section.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:http/testing.dart';
|
|
import 'package:shadcn_flutter/shadcn_flutter.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
Future<ActivitySyncService> _service(Map<String, Object?> connections, {required bool signedIn}) async {
|
|
SharedPreferences.setMockInitialValues({});
|
|
final service = ActivitySyncService(
|
|
api: ActivitySyncApi(
|
|
baseUrl: 'https://x/activity-sync',
|
|
anonKey: 'anon',
|
|
accessToken: () async => 't',
|
|
client: MockClient((_) async => http.Response(jsonEncode(connections), 200)),
|
|
),
|
|
prefs: await SharedPreferences.getInstance(),
|
|
isSignedIn: () => signedIn,
|
|
openUrl: (_) async => true,
|
|
platform: 'macos',
|
|
readBytes: (_) async => Uint8List(0),
|
|
recordUpload: (ride, _, _) async => ride,
|
|
onError: (_, _, _) {},
|
|
);
|
|
await service.refresh();
|
|
return service;
|
|
}
|
|
|
|
Future<void> _pump(WidgetTester tester, ActivitySyncService service) async {
|
|
tester.view.physicalSize = const Size(900, 1200);
|
|
tester.view.devicePixelRatio = 1;
|
|
addTearDown(tester.view.reset);
|
|
await tester.pumpWidget(
|
|
ShadcnApp(
|
|
localizationsDelegates: const [
|
|
...ShadcnLocalizations.localizationsDelegates,
|
|
OtherLocalizationsDelegate(),
|
|
AppLocalizations.delegate,
|
|
],
|
|
supportedLocales: const [Locale('en')],
|
|
theme: ThemeData(colorScheme: ColorSchemes.lightSlate, radius: 0.7),
|
|
home: Scaffold(child: SingleChildScrollView(child: ActivitySyncSection(sync: service))),
|
|
),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
}
|
|
|
|
void main() {
|
|
testWidgets('signed out: Strava asks to sign in, intervals.icu hidden until the backend offers it', (tester) async {
|
|
final service = await _service({'available': <String>[], 'connections': <Object>[]}, signedIn: false);
|
|
await _pump(tester, service);
|
|
expect(find.text('Strava'), findsOneWidget);
|
|
expect(find.text('Sign in to connect'), findsOneWidget);
|
|
expect(find.text('intervals.icu'), findsNothing);
|
|
});
|
|
|
|
testWidgets("not connected: Strava's own Connect button; intervals.icu a plain one", (tester) async {
|
|
final service = await _service({
|
|
'available': ['strava', 'intervals'],
|
|
'connections': <Object>[],
|
|
}, signedIn: true);
|
|
await _pump(tester, service);
|
|
expect(find.byKey(const ValueKey('activity-sync-connect-strava')), findsOneWidget);
|
|
expect(find.byType(Image), findsOneWidget, reason: 'the official artwork, not our own text');
|
|
expect(find.byKey(const ValueKey('activity-sync-connect-intervals')), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('connected: who, disconnect, and the automatic upload switch', (tester) async {
|
|
final service = await _service({
|
|
'available': ['strava'],
|
|
'connections': [
|
|
{'provider': 'strava', 'athlete_name': 'Jo Rider', 'reauth_required': false},
|
|
],
|
|
}, signedIn: true);
|
|
await _pump(tester, service);
|
|
expect(find.text('Connected as Jo Rider'), findsOneWidget);
|
|
expect(find.byKey(const ValueKey('activity-sync-disconnect-strava')), findsOneWidget);
|
|
expect(find.text('Upload every ride automatically'), findsOneWidget);
|
|
});
|
|
|
|
testWidgets('a revoked connection asks to connect again', (tester) async {
|
|
final service = await _service({
|
|
'available': ['strava'],
|
|
'connections': [
|
|
{'provider': 'strava', 'athlete_name': 'Jo', 'reauth_required': true},
|
|
],
|
|
}, signedIn: true);
|
|
await _pump(tester, service);
|
|
expect(find.text('Strava needs you to connect again'), findsOneWidget);
|
|
expect(find.byKey(const ValueKey('activity-sync-connect-strava')), findsOneWidget);
|
|
});
|
|
}
|