From 7722260eebb38b4f92d550ba42342ad3edfefc3a Mon Sep 17 00:00:00 2001 From: Krzysztof kuhy Rudnicki Date: Mon, 13 Jul 2026 12:47:55 +0200 Subject: [PATCH] Fix workout_app service tests on non-Android hosts Pre-existing failures unrelated to the manual-workout feature, exposed once the pre-push flutter gate started running the Dart suite: - http_server_service / sync_service: getExternalStorageDirectory() throws UnsupportedError (an Error, not an Exception) on non-Android platforms, so the `on Exception` fallbacks didn't catch it and it escaped, failing start() and writeWorkoutResult(). External storage is Android-only, so guard the call with Platform.isAndroid instead of trying to catch a platform Error. - sync_service_test: the "updates HttpServerService" assertion expected compact JSON ("workout_type":"A") but the payload is pretty-printed ("workout_type": "A"); fixed the expectation to match. Full workout_app suite (191 tests) now passes on this host. Co-Authored-By: Claude Opus 4.8 (1M context) Claude-Session: https://claude.ai/code/session_01PM3auNTifaVgcXWDizZrjH --- .../lib/services/http_server_service.dart | 15 +++++++++----- .../lib/services/sync_service.dart | 20 +++++++++++-------- .../test/services/sync_service_test.dart | 2 +- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/stronglift_replacement/workout_app/lib/services/http_server_service.dart b/stronglift_replacement/workout_app/lib/services/http_server_service.dart index b0e1ce5..f8abaad 100644 --- a/stronglift_replacement/workout_app/lib/services/http_server_service.dart +++ b/stronglift_replacement/workout_app/lib/services/http_server_service.dart @@ -67,11 +67,16 @@ class HttpServerService { /// HTTP endpoint is populated even before the next workout is completed. Future _loadFromDisk() async { final candidates = [kSyncFilePath]; - try { - final dir = await getExternalStorageDirectory(); - if (dir != null) candidates.add('${dir.path}/workout_result.json'); - } on Exception { - // Ignore; the /sdcard path is tried first. + // External storage is Android-only; getExternalStorageDirectory() throws an + // UnsupportedError (not an Exception) on other platforms (desktop / test + // host), so guard the call rather than trying to catch it. + if (Platform.isAndroid) { + try { + final dir = await getExternalStorageDirectory(); + if (dir != null) candidates.add('${dir.path}/workout_result.json'); + } on Exception { + // Ignore; the /sdcard path is tried first. + } } for (final path in candidates) { final file = File(path); diff --git a/stronglift_replacement/workout_app/lib/services/sync_service.dart b/stronglift_replacement/workout_app/lib/services/sync_service.dart index 6bfd08b..78661cb 100644 --- a/stronglift_replacement/workout_app/lib/services/sync_service.dart +++ b/stronglift_replacement/workout_app/lib/services/sync_service.dart @@ -29,15 +29,19 @@ class SyncService { // Fallback: app-specific external directory (still ADB accessible). } - try { - final dir = await getExternalStorageDirectory(); - if (dir != null) { - final file = File('${dir.path}/workout_result.json'); - await file.writeAsString(json); - return SyncResult(success: true, path: file.path); + // External storage is Android-only; getExternalStorageDirectory() throws an + // UnsupportedError (not an Exception) on other platforms, so guard the call. + if (Platform.isAndroid) { + try { + final dir = await getExternalStorageDirectory(); + if (dir != null) { + final file = File('${dir.path}/workout_result.json'); + await file.writeAsString(json); + return SyncResult(success: true, path: file.path); + } + } on Exception { + // Fallback failed. } - } on Exception { - // Fallback failed. } return const SyncResult( diff --git a/stronglift_replacement/workout_app/test/services/sync_service_test.dart b/stronglift_replacement/workout_app/test/services/sync_service_test.dart index 1e30b49..74678d1 100644 --- a/stronglift_replacement/workout_app/test/services/sync_service_test.dart +++ b/stronglift_replacement/workout_app/test/services/sync_service_test.dart @@ -63,7 +63,7 @@ void main() { // The HTTP server must be updated regardless of file write success. expect( HttpServerService.instance.latestWorkout, - contains('"workout_type":"A"'), + contains('"workout_type": "A"'), ); });