feat(train): the Loop's training sessions as workouts (ADR 0030, D39)

Each read of the band lists the sessions it holds, reads those from the
read's first day on (`LoopDays.kt`, one line per exercise: its start
with the band's offset, length, Polar's sport ID, figures and heart
rate at its interval), and deletes those up to the day kept, as the
days. The app keeps each as a workout of origin Polar Loop with its
heart rate (`takeLoopSessions`), Polar's running and cycling as
Train's and every other sport as the new `Activity.other`, which a
workout's edit sheet now changes; a session overlapping a workout Train
recorded gives it its heart rate where it has none instead. The
sessions taken are remembered in `loop.json`, so one deleted in Train
isn't brought back. Health Connect writes the Loop's workouts as
Train's own.

The SDK gives sessions as its protobuf messages: protobuf-javalite
3.20.0, the SDK's own, is named for compiling only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Tizian 「ティツィアーン」 <raisondetredev@pm.me>
This commit is contained in:
Tizian 「ティツィアーン」 2026-10-28 20:22:23 +01:00
commit 2c5467a282
Signed by: raisondetredev
GPG key ID: 07F6FCF405488B64

View file

@ -0,0 +1,169 @@
import 'package:flutter_test/flutter_test.dart';
import 'package:halkyon_app/testing.dart';
import 'package:halkyon_train_flutter/src/core/privacy/train_keys.dart';
import 'package:halkyon_train_flutter/src/feature/day/data/loop_lines.dart';
import 'package:halkyon_train_flutter/src/feature/day/data/loop_workouts.dart';
import 'package:halkyon_train_flutter/src/feature/day/model/loop_session.dart';
import 'package:halkyon_train_flutter/src/feature/workout/data/workout_repository.dart';
import 'package:halkyon_train_flutter/src/feature/workout/model/series_chunk.dart';
import 'package:halkyon_train_flutter/src/feature/workout/model/workout.dart';
import '../../support/memory_content_key_gateway.dart';
import '../../support/memory_workout_gateway.dart';
/// The Loop's training sessions as workouts (ADR 0030, D39).
void main() {
late TrainKeys keys;
late WorkoutRepository workouts;
final start = DateTime.utc(2026, 10, 27, 16, 30);
LoopSession session({
String id = '/U/0/20261027/E/173000#0',
int? sport = 16,
DateTime? at,
Duration duration = const Duration(minutes: 45),
}) => LoopSession(
id: id,
date: DateTime.utc(2026, 10, 27),
start: at ?? start,
duration: duration,
sport: sport,
calories: 380,
metres: 6012.4,
interval: const Duration(seconds: 1),
heart: const [null, 120, 135],
);
setUp(() {
keys = TrainKeys(
MemoryContentKeyGateway(),
FakeContentCipher(),
() async => FakeAccountCrypto().keysNumbered(1),
);
workouts = WorkoutRepository(MemoryWorkoutGateway(), keys);
});
tearDown(() async {
await workouts.dispose();
keys.dispose();
});
test('a session line keeps its ID, start, length, sport and heart rate, a '
'sample of 0 as none', () {
final lines = LoopLines()
..add({
'kind': 'session',
'date': '2026-10-27',
'id': '/U/0/20261027/E/173000#0',
'start': '2026-10-27T17:30+01:00',
'seconds': 2700.0,
'sport': 16,
'calories': 380,
'metres': 6012.4,
'interval': 1.0,
'heart': [0, 120, 135],
});
expect(lines.sessions.values.single, session());
expect(lines.isEmpty, isFalse);
expect(lines.dates, isEmpty);
});
test("Polar's running and cycling are Train's; every other sport is "
'Other', () {
expect(activityOfSport(1), Activity.running);
expect(activityOfSport(2), Activity.cycling);
expect(activityOfSport(16), Activity.other);
expect(activityOfSport(null), Activity.other);
});
test(
'a session becomes a workout from the Loop, with its heart rate',
() async {
final taken = await takeLoopSessions(workouts, [session()], taken: {});
final [stored] = await workouts.all();
expect(taken, {'/U/0/20261027/E/173000#0'});
expect(stored.content?.activity, Activity.other);
expect(stored.content?.startedAt, start);
expect(stored.content?.endedAt, start.add(const Duration(minutes: 45)));
expect(stored.content?.distanceMetres, 6012);
expect(stored.content?.origin?.app, loopApp);
final [heart] = await workouts.series(stored.id);
expect(heart.kind, SeriesKind.heartRate);
expect(heart.columns['bpm'], [120, 135]);
},
);
test('a session taken before, or kept as a workout already, is not '
'brought again', () async {
await takeLoopSessions(workouts, [session()], taken: {});
final again = await takeLoopSessions(
workouts,
[
session(),
session(id: '/other#0', at: start.add(const Duration(hours: 3))),
],
taken: {'/other#0'},
);
// Remembered from now on, as already kept.
expect(again, {'/U/0/20261027/E/173000#0'});
expect(await workouts.all(), hasLength(1));
});
test('a session Train recorded too gives the recorded workout its heart '
'rate where it has none, whatever the sports, and is no workout of its '
'own', () async {
final run = WorkoutContent(
activity: Activity.running,
startedAt: start.add(const Duration(minutes: 5)),
endedAt: start.add(const Duration(minutes: 40)),
);
final id = await workouts.save(run, const []);
final taken = await takeLoopSessions(workouts, [session()], taken: {});
expect(taken, hasLength(1));
expect(await workouts.all(), hasLength(1));
expect((await workouts.series(id)).single.kind, SeriesKind.heartRate);
});
test('a recorded workout with a heart rate of its own keeps it', () async {
final run = WorkoutContent(
activity: Activity.running,
startedAt: start,
endedAt: start.add(const Duration(minutes: 45)),
);
final own = chunksOf(
SeriesKind.heartRate,
start: start,
times: const [0],
columns: {
'bpm': [150],
},
);
final id = await workouts.save(run, own);
await takeLoopSessions(workouts, [session()], taken: {});
expect(await workouts.series(id), own);
});
test('a session barely overlapping a recorded workout is a workout of its '
'own', () async {
await workouts.save(
WorkoutContent(
activity: Activity.running,
startedAt: start.add(const Duration(minutes: 40)),
endedAt: start.add(const Duration(minutes: 90)),
),
const [],
);
await takeLoopSessions(workouts, [session()], taken: {});
expect(await workouts.all(), hasLength(2));
});
}