-
-
Notifications
You must be signed in to change notification settings - Fork 83
gate the workout health write on a delete that actually cleared #301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
abdulsaheel
merged 1 commit into
OpenStrap:main
from
DropTabl:fix/health-workout-delete-gate
Aug 28, 2026
+221
−28
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| import 'package:flutter/services.dart'; | ||
| import 'package:flutter_test/flutter_test.dart'; | ||
| import 'package:openstrap_edge/health/health_export.dart'; | ||
|
|
||
| /// A stand-in for the platform health store, driven over the `health` plugin's | ||
| /// own method channel — the only seam `HealthExporter` reaches the store | ||
| /// through. Records every call so a test can assert what the exporter did | ||
| /// AFTER a delete came back false, which is the whole point: the bug was that | ||
| /// it wrote anyway. | ||
| class _FakeHealthStore { | ||
| _FakeHealthStore({required this.deleteResult, this.deleteThrows = false}); | ||
|
|
||
| /// What `delete` answers. On Health Connect a `false` here means the delete | ||
| /// genuinely failed and whatever we wrote before is STILL in the store. | ||
| final bool deleteResult; | ||
| final bool deleteThrows; | ||
|
|
||
| final calls = <String>[]; | ||
|
|
||
| static const _channel = MethodChannel('flutter_health'); | ||
|
|
||
| void install() { | ||
| TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger | ||
| .setMockMethodCallHandler(_channel, (call) async { | ||
| calls.add(call.method); | ||
| switch (call.method) { | ||
| case 'delete': | ||
| if (deleteThrows) { | ||
| throw PlatformException(code: 'delete-failed'); | ||
| } | ||
| return deleteResult; | ||
| case 'writeWorkoutData': | ||
| return true; | ||
| default: | ||
| return null; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| void remove() { | ||
| TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger | ||
| .setMockMethodCallHandler(_channel, null); | ||
| } | ||
| } | ||
|
|
||
| Map<String, Object?> _session() { | ||
| final start = DateTime(2026, 8, 26, 18, 30); | ||
| final end = DateTime(2026, 8, 26, 19, 15); | ||
| return { | ||
| 'status': 'done', | ||
| 'type': 'run', | ||
| 'start_ts': start.millisecondsSinceEpoch ~/ 1000, | ||
| 'end_ts': end.millisecondsSinceEpoch ~/ 1000, | ||
| 'calories': 412, | ||
| }; | ||
| } | ||
|
|
||
| void main() { | ||
| TestWidgetsFlutterBinding.ensureInitialized(); | ||
|
|
||
| group('healthDeleteClearedRange', () { | ||
| // The fact the whole gate hinges on, pinned so a future reader does not | ||
| // "simplify" it back to a bare `deleted`. Verified against the plugin | ||
| // sources in health 12.2.1 and Apple's own docs: | ||
| // | ||
| // * HealthPlugin.kt `deleteData` -> deleteRecords over a time range, | ||
| // `result.success(true)` unless it threw. Zero matches is a success. | ||
| // * SwiftHealthPlugin.swift `delete` -> HKSampleQuery scoped to | ||
| // HKSource.default(), then HKHealthStore.delete(samples) with whatever | ||
| // came back — including an EMPTY array, which Apple documents as | ||
| // "Deleting an empty array fails with an errorInvalidArgument error". | ||
| // | ||
| // So on Apple a false delete is what every first export sees, and gating | ||
| // the write on it would mean no workout ever reaches HealthKit at all. | ||
| test('Health Connect false is a genuine failure', () { | ||
| expect(healthDeleteClearedRange(deleted: false, ios: false), isFalse); | ||
| expect(healthDeleteClearedRange(deleted: true, ios: false), isTrue); | ||
| }); | ||
|
|
||
| test('HealthKit false is the documented empty-range answer', () { | ||
| expect(healthDeleteClearedRange(deleted: false, ios: true), isTrue); | ||
| expect(healthDeleteClearedRange(deleted: true, ios: true), isTrue); | ||
| }); | ||
| }); | ||
|
|
||
| group('exportWorkout delete-then-write', () { | ||
| // The host VM is neither iOS nor macOS, so `HealthExporter.isApple` is | ||
| // false and these exercise the Health-Connect reading of `delete`. | ||
| late _FakeHealthStore store; | ||
|
|
||
| tearDown(() => store.remove()); | ||
|
|
||
| test('a failed delete does not write a duplicate on top of it', () async { | ||
| store = _FakeHealthStore(deleteResult: false)..install(); | ||
|
|
||
| final ok = await HealthExporter().exportWorkout(_session()); | ||
|
|
||
| expect(ok, isFalse, reason: 'the caller must retry this workout'); | ||
| expect(store.calls, contains('delete')); | ||
| expect( | ||
| store.calls, | ||
| isNot(contains('writeWorkoutData')), | ||
| reason: | ||
| 'the previously exported copy survived the delete, so writing ' | ||
| 'would leave two of this workout in the store — and the false ' | ||
| 'return drives a retry that would write a third', | ||
| ); | ||
| }); | ||
|
|
||
| test('a thrown delete does not write either', () async { | ||
| store = _FakeHealthStore(deleteResult: true, deleteThrows: true) | ||
| ..install(); | ||
|
|
||
| final ok = await HealthExporter().exportWorkout(_session()); | ||
|
|
||
| expect(ok, isFalse); | ||
| expect(store.calls, isNot(contains('writeWorkoutData'))); | ||
| }); | ||
|
|
||
| test('a successful delete still writes', () async { | ||
| store = _FakeHealthStore(deleteResult: true)..install(); | ||
|
|
||
| final ok = await HealthExporter().exportWorkout(_session()); | ||
|
|
||
| expect(ok, isTrue); | ||
| expect(store.calls, containsAllInOrder(['delete', 'writeWorkoutData'])); | ||
| }); | ||
| }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (broader_impact): On Apple,
deleted == falseis treated as a cleared range even when HealthKit returned no readable samples because the app requests only write authorization. An existing app-owned workout can therefore survive the delete while_writeOneWorkoutwrites another copy, recreating the duplicate the gate is intended to prevent.Triggers: When the user has write authorization but lacks HealthKit read authorization, or read access otherwise returns an empty result for an existing sample.
Suggested fix: Request and verify the necessary read authorization before relying on the HealthKit query, or make the delete API distinguish an empty range from a failed/unauthorized query instead of treating every Apple
falseas safe.