screen-locker/stronglift_replacement/workout_app/test/fake_break_service.dart
Krzysztof kuhy Rudnicki b6e89b93dc
Some checks failed
dependency freshness / dependencies on newest stable (push) Has been cancelled
md-naming / markdown naming convention (push) Has been cancelled
pre-commit / pre-commit (push) Has been cancelled
Tests / test (push) Has been cancelled
Tests / lock-popup-safety (push) Has been cancelled
release-apk / release (APK) (push) Has been cancelled
workout_app: run the break countdown in a foreground service with notifications
The between-set break never finished once the phone's screen was off. Three
independent causes (worked through on-device on 2026-09-12; left
uncommitted awaiting the go-ahead, committed here as-is plus one pin bump):

1. The countdown was not wall-clock: `_breakRemaining--` on a
   Timer.periodic, which Android throttles and Doze suspends in a
   backgrounded isolate, so the counter stalled and _onBreakFinished()
   never fired. Now a BreakClock deadline on the wall clock.
2. An expired break was silently dropped on restore (`if (remaining > 0)`
   only) — now cued and logged.
3. No notification infrastructure at all: no package, no
   POST_NOTIFICATIONS / FOREGROUND_SERVICE / WAKE_LOCK.

Now: flutter_foreground_task runs the countdown in a service isolate that
survives BACK and the screen lock; flutter_local_notifications shows an
ongoing "Workout A · break m:ss / Next: …" notification with −1 rep / Done
actions and a "Break over" alert on its own channel (raw break_end sound,
vibration). Button presses go through a persisted BreakIntent queue the
screen drains on resume, so a press with the app killed is not lost.
Finish and Reset stop the service; a startup reaper handles the crash case.

Verified on the phone with the screen off and locked: service alive and
ticking while Dozing, both notifications on the lock screen, channel
importance 5 with the break_end sound. Not verifiable from adb (receiver is
RECEIVER_NOT_EXPORTED): an actual action-button tap, and a true recents
swipe — see stronglift_replacement/TODO-workout-app-v2.md.

Also fixed en route: the break-end sound had never played in any widget
test (AssetSource needs path_provider, which nothing faked, and the failure
was swallowed by catchError); the queue's prune wrote back a stale list.

This commit additionally:
- takes the freshness gate's bump flutter_local_notifications 22.3.1;
- restores web/package.json to HEAD (the tree held a stale pre-bump copy);
- gives scripts/check_flutter_coverage.sh a FLUTTER_TEST_CONCURRENCY knob:
  the default one-isolate-per-core run peaks over 4 GiB and is OOM-killed
  under the resource cap. Default behaviour unchanged.

Gate: flutter analyze clean, 511 tests, 3187/3187 lines (100%), pre-commit
green. Not re-installed on the phone for the 22.3.1 pin: no device attached
at commit time.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01C7vgacLhb4WJjxuVj2QLEP
2026-09-13 20:28:36 +02:00

133 lines
3.3 KiB
Dart

/// Recording fakes for the two break-service seams.
///
/// Both stand in for a platform channel, so they record calls rather than
/// performing them — the assertions are about what the handler and the
/// controller DECIDED, which is the part that has to be right.
library;
import 'package:workout_app/models/break_snapshot.dart';
import 'package:workout_app/services/break_notification_text.dart';
import 'package:workout_app/services/break_service_port.dart';
import 'package:workout_app/services/foreground_break_client.dart';
/// Stands in for the real service-isolate side effects.
class FakeBreakServicePort implements BreakServicePort {
/// Notification redraws, in order.
final drawn = <BreakNotificationCopy>[];
/// How many times the break-end alert fired.
var alerts = 0;
/// How many times the UI was nudged to drain.
var nudges = 0;
/// True once the service stopped itself.
var stopped = false;
/// True once plugins were registered in this isolate.
var ready = false;
@override
Future<void> ensureIsolateReady() async => ready = true;
@override
Future<void> updateNotification(BreakNotificationCopy copy) async =>
drawn.add(copy);
@override
Future<void> fireBreakEndAlert() async => alerts++;
@override
void nudgeMainToDrain() => nudges++;
@override
Future<void> finishAndStop() async => stopped = true;
}
/// Stands in for `flutter_foreground_task` on the UI side.
class FakeForegroundBreakClient implements ForegroundBreakClient {
/// Creates a fake. Defaults describe a healthy Android device.
FakeForegroundBreakClient({
this.isSupported = true,
this.permissionGranted = true,
this.grantOnRequest = true,
this.startSucceeds = true,
});
@override
bool isSupported;
/// Whether the permission is already held.
bool permissionGranted;
/// Whether asking for it succeeds.
bool grantOnRequest;
/// Whether `startService` reports success.
bool startSucceeds;
/// Snapshots pushed to the service, in order.
final pushed = <BreakSnapshot>[];
/// Calls recorded in order, for asserting sequencing.
final calls = <String>[];
/// The currently registered drain callback, if any.
void Function()? onNudge;
/// Whether a service from a previous launch is still alive.
var running = false;
@override
Future<void> init() async => calls.add('init');
@override
Future<bool> isRunning() async {
calls.add('isRunning');
return running;
}
@override
Future<bool> hasNotificationPermission() async {
calls.add('hasPermission');
return permissionGranted;
}
@override
Future<bool> requestNotificationPermission() async {
calls.add('requestPermission');
return permissionGranted = grantOnRequest;
}
@override
Future<bool> start(BreakSnapshot snapshot) async {
calls.add('start');
if (startSucceeds) pushed.add(snapshot);
return startSucceeds;
}
@override
Future<void> push(BreakSnapshot snapshot) async {
calls.add('push');
pushed.add(snapshot);
}
@override
Future<void> stop() async {
calls.add('stop');
running = false;
}
@override
void listenForDrainNudges(void Function() onNudge) {
calls.add('listen');
this.onNudge = onNudge;
}
@override
void stopListeningForDrainNudges() {
calls.add('unlisten');
onNudge = null;
}
}