feat(train): the Loop's firmware updates, directly or through the instance (ADR 0030, D10, D40)
Train's server relays Polar's firmware service for the people signed in (`firmware`: offered, check, download), a fetched file kept in memory a day per hardware and version, off with LOOP_FIRMWARE_RELAY=off. The app asks on the day's first connection and on the band's screen, from Polar or the instance as the person chose, never Polar in the instance's place; an update runs only after asking, reads and keeps the band first, and sets it up again with its figures and clock after. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Tizian 「ティツィアーン」 <raisondetredev@pm.me>
This commit is contained in:
parent
7c5e539df5
commit
388b3b1ae6
2 changed files with 2318 additions and 45 deletions
|
|
@ -0,0 +1,209 @@
|
|||
import 'dart:typed_data';
|
||||
|
||||
import 'package:clock/clock.dart';
|
||||
|
||||
import 'package:halkyon_train_server/src/firmware/firmware_endpoint.dart';
|
||||
import 'package:halkyon_train_server/src/firmware/firmware_relay.dart';
|
||||
import 'package:halkyon_train_server/src/generated/protocol.dart';
|
||||
import 'package:serverpod/serverpod.dart';
|
||||
import 'package:test/test.dart';
|
||||
|
||||
import '../test_tools/serverpod_test_tools.dart';
|
||||
|
||||
/// Polar's firmware service as the tests want it: what it answers, and how
|
||||
/// often it was asked.
|
||||
class _FakePolar() implements PolarFirmwareService {
|
||||
({String version, Uri file})? update = (
|
||||
version: '3.1.2',
|
||||
file: Uri.parse('https://files.example/loop-3.1.2.zip'),
|
||||
);
|
||||
int checks = 0;
|
||||
int fetches = 0;
|
||||
bool down = false;
|
||||
|
||||
@override
|
||||
Future<({String version, Uri file})?> check({
|
||||
required String uuid,
|
||||
required String firmwareVersion,
|
||||
required String hardwareCode,
|
||||
}) async {
|
||||
checks++;
|
||||
if (down) {
|
||||
throw StateError('down');
|
||||
}
|
||||
return update;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Uint8List> fetch(Uri file) async {
|
||||
fetches++;
|
||||
return Uint8List.fromList([1, 2, 3]);
|
||||
}
|
||||
}
|
||||
|
||||
/// The Loop's firmware through the instance (ADR 0030, D10, D40): for the
|
||||
/// people signed in, unless the operator turned it off; each file fetched
|
||||
/// once for every band of its hardware.
|
||||
void main() {
|
||||
withServerpod('Relaying the Loop firmware', (sessionBuilder, endpoints) {
|
||||
late _FakePolar polar;
|
||||
final signedIn = sessionBuilder.copyWith(
|
||||
authentication: AuthenticationOverride.authenticationInfo(
|
||||
const Uuid().v7(),
|
||||
{},
|
||||
),
|
||||
);
|
||||
|
||||
setUp(() {
|
||||
polar = _FakePolar();
|
||||
firmwareRelay = FirmwareRelay(polar);
|
||||
firmwareRelayed = () => true;
|
||||
});
|
||||
|
||||
test('names an update to someone signed in, and hands its file', () async {
|
||||
final update = await endpoints.firmware.check(
|
||||
signedIn,
|
||||
'uuid-1',
|
||||
'3.0.0',
|
||||
'HW-1',
|
||||
);
|
||||
|
||||
expect(update?.version, '3.1.2');
|
||||
final file = await endpoints.firmware.download(signedIn, 'HW-1', '3.1.2');
|
||||
expect(Uint8List.sublistView(file), [1, 2, 3]);
|
||||
});
|
||||
|
||||
test('names none where Polar has none', () async {
|
||||
polar.update = null;
|
||||
|
||||
expect(
|
||||
await endpoints.firmware.check(signedIn, 'uuid-1', '3.1.2', 'HW-1'),
|
||||
isNull,
|
||||
);
|
||||
});
|
||||
|
||||
test('fetches a file once for every band of its hardware', () async {
|
||||
await endpoints.firmware.check(signedIn, 'uuid-1', '3.0.0', 'HW-1');
|
||||
await endpoints.firmware.check(signedIn, 'uuid-2', '3.0.1', 'HW-1');
|
||||
await endpoints.firmware.download(signedIn, 'HW-1', '3.1.2');
|
||||
await endpoints.firmware.download(signedIn, 'HW-1', '3.1.2');
|
||||
|
||||
expect(polar.fetches, 1);
|
||||
});
|
||||
|
||||
test('hands no file a check never named', () async {
|
||||
await expectLater(
|
||||
endpoints.firmware.download(signedIn, 'HW-9', '9.9.9'),
|
||||
throwsA(
|
||||
isA<LoopFirmwareRefused>().having(
|
||||
(e) => e.reason,
|
||||
'reason',
|
||||
'unknown',
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(polar.fetches, 0);
|
||||
});
|
||||
|
||||
test("takes no figures that aren't a band's, and doesn't ask Polar "
|
||||
'with them', () async {
|
||||
await expectLater(
|
||||
endpoints.firmware.check(signedIn, 'uuid 1/../x', '3.0.0', 'HW-1'),
|
||||
throwsA(
|
||||
isA<LoopFirmwareRefused>().having(
|
||||
(e) => e.reason,
|
||||
'reason',
|
||||
'invalid',
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(polar.checks, 0);
|
||||
});
|
||||
|
||||
test("says Polar didn't answer", () async {
|
||||
polar.down = true;
|
||||
|
||||
await expectLater(
|
||||
endpoints.firmware.check(signedIn, 'uuid-1', '3.0.0', 'HW-1'),
|
||||
throwsA(
|
||||
isA<LoopFirmwareRefused>().having(
|
||||
(e) => e.reason,
|
||||
'reason',
|
||||
'unreachable',
|
||||
),
|
||||
),
|
||||
);
|
||||
});
|
||||
|
||||
test(
|
||||
'relays nothing where the operator turned it off, and says so',
|
||||
() async {
|
||||
firmwareRelayed = () => false;
|
||||
|
||||
expect(await endpoints.firmware.offered(signedIn), isFalse);
|
||||
await expectLater(
|
||||
endpoints.firmware.check(signedIn, 'uuid-1', '3.0.0', 'HW-1'),
|
||||
throwsA(
|
||||
isA<LoopFirmwareRefused>().having((e) => e.reason, 'reason', 'off'),
|
||||
),
|
||||
);
|
||||
await expectLater(
|
||||
endpoints.firmware.download(signedIn, 'HW-1', '3.1.2'),
|
||||
throwsA(isA<LoopFirmwareRefused>()),
|
||||
);
|
||||
expect(polar.checks, 0);
|
||||
},
|
||||
);
|
||||
|
||||
test('relays nothing to nobody signed in', () async {
|
||||
final nobody = sessionBuilder.copyWith(
|
||||
authentication: AuthenticationOverride.unauthenticated(),
|
||||
);
|
||||
|
||||
await expectLater(
|
||||
endpoints.firmware.offered(nobody),
|
||||
throwsA(isA<ServerpodUnauthenticatedException>()),
|
||||
);
|
||||
await expectLater(
|
||||
endpoints.firmware.check(nobody, 'uuid-1', '3.0.0', 'HW-1'),
|
||||
throwsA(isA<ServerpodUnauthenticatedException>()),
|
||||
);
|
||||
await expectLater(
|
||||
endpoints.firmware.download(nobody, 'HW-1', '3.1.2'),
|
||||
throwsA(isA<ServerpodUnauthenticatedException>()),
|
||||
);
|
||||
expect(polar.checks, 0);
|
||||
});
|
||||
});
|
||||
|
||||
test('the operator turns relaying off with LOOP_FIRMWARE_RELAY=off', () {
|
||||
expect(firmwareRelayedBy({}), isTrue);
|
||||
expect(firmwareRelayedBy({'LOOP_FIRMWARE_RELAY': 'on'}), isTrue);
|
||||
expect(firmwareRelayedBy({'LOOP_FIRMWARE_RELAY': ' OFF '}), isFalse);
|
||||
});
|
||||
|
||||
test('a file is kept a day, then fetched again after a new check', () async {
|
||||
final polar = _FakePolar();
|
||||
final relay = FirmwareRelay(polar);
|
||||
var now = DateTime.utc(2026, 10, 28, 9);
|
||||
await withClock(Clock(() => now), () async {
|
||||
await relay.check(
|
||||
uuid: 'uuid-1',
|
||||
firmwareVersion: '3.0.0',
|
||||
hardwareCode: 'HW-1',
|
||||
);
|
||||
await relay.download(hardwareCode: 'HW-1', version: '3.1.2');
|
||||
now = now.add(const Duration(hours: 25));
|
||||
|
||||
// Checked again a day on, before any download: fetched anew.
|
||||
await relay.check(
|
||||
uuid: 'uuid-1',
|
||||
firmwareVersion: '3.0.0',
|
||||
hardwareCode: 'HW-1',
|
||||
);
|
||||
await relay.download(hardwareCode: 'HW-1', version: '3.1.2');
|
||||
});
|
||||
|
||||
expect(polar.fetches, 2);
|
||||
});
|
||||
}
|
||||
|
|
@ -13,6 +13,9 @@
|
|||
// ignore_for_file: no_leading_underscores_for_library_prefixes
|
||||
import 'dart:async' as _ida;
|
||||
import 'dart:io' as _idi;
|
||||
import 'dart:typed_data' as _idt;
|
||||
import 'package:halkyon_train_server/src/generated/firmware/loop_firmware.dart'
|
||||
as _ifm5blp8;
|
||||
import 'package:halkyon_train_server/src/generated/future_calls.dart'
|
||||
as _i59xasdy;
|
||||
import 'package:halkyon_train_server/src/generated/map/map_source.dart'
|
||||
|
|
@ -154,6 +157,8 @@ void withServerpod(
|
|||
class TestEndpoints {
|
||||
late final futureCalls = _FutureCalls();
|
||||
|
||||
late final _FirmwareEndpoint firmware;
|
||||
|
||||
late final _MapEndpoint map;
|
||||
}
|
||||
|
||||
|
|
@ -164,6 +169,10 @@ class _InternalTestEndpoints extends TestEndpoints
|
|||
_is.SerializationManager serializationManager,
|
||||
_is.EndpointDispatch endpoints,
|
||||
) {
|
||||
firmware = _FirmwareEndpoint(
|
||||
endpoints,
|
||||
serializationManager,
|
||||
);
|
||||
map = _MapEndpoint(
|
||||
endpoints,
|
||||
serializationManager,
|
||||
|
|
@ -175,6 +184,117 @@ class _FutureCalls {
|
|||
late final pruning = _PruningFutureCall();
|
||||
}
|
||||
|
||||
class _FirmwareEndpoint {
|
||||
_FirmwareEndpoint(
|
||||
this._endpointDispatch,
|
||||
this._serializationManager,
|
||||
);
|
||||
|
||||
final _is.EndpointDispatch _endpointDispatch;
|
||||
|
||||
final _is.SerializationManager _serializationManager;
|
||||
|
||||
_ida.Future<bool> offered(_ist.TestSessionBuilder sessionBuilder) async {
|
||||
return _ist.callAwaitableFunctionAndHandleExceptions(() async {
|
||||
var _localUniqueSession =
|
||||
(sessionBuilder as _ist.InternalTestSessionBuilder).internalBuild(
|
||||
endpoint: 'firmware',
|
||||
method: 'offered',
|
||||
);
|
||||
try {
|
||||
var _localCallContext = await _endpointDispatch.getMethodCallContext(
|
||||
createSessionCallback: (_) => _localUniqueSession,
|
||||
endpointPath: 'firmware',
|
||||
methodName: 'offered',
|
||||
parameters: _ist.testObjectToJson({}),
|
||||
serializationManager: _serializationManager,
|
||||
);
|
||||
var _localReturnValue =
|
||||
await (_localCallContext.method.call(
|
||||
_localUniqueSession,
|
||||
_localCallContext.arguments,
|
||||
)
|
||||
as _ida.Future<bool>);
|
||||
return _localReturnValue;
|
||||
} finally {
|
||||
await _localUniqueSession.close();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
_ida.Future<_ifm5blp8.LoopFirmware?> check(
|
||||
_ist.TestSessionBuilder sessionBuilder,
|
||||
String uuid,
|
||||
String firmwareVersion,
|
||||
String hardwareCode,
|
||||
) async {
|
||||
return _ist.callAwaitableFunctionAndHandleExceptions(() async {
|
||||
var _localUniqueSession =
|
||||
(sessionBuilder as _ist.InternalTestSessionBuilder).internalBuild(
|
||||
endpoint: 'firmware',
|
||||
method: 'check',
|
||||
);
|
||||
try {
|
||||
var _localCallContext = await _endpointDispatch.getMethodCallContext(
|
||||
createSessionCallback: (_) => _localUniqueSession,
|
||||
endpointPath: 'firmware',
|
||||
methodName: 'check',
|
||||
parameters: _ist.testObjectToJson({
|
||||
'uuid': uuid,
|
||||
'firmwareVersion': firmwareVersion,
|
||||
'hardwareCode': hardwareCode,
|
||||
}),
|
||||
serializationManager: _serializationManager,
|
||||
);
|
||||
var _localReturnValue =
|
||||
await (_localCallContext.method.call(
|
||||
_localUniqueSession,
|
||||
_localCallContext.arguments,
|
||||
)
|
||||
as _ida.Future<_ifm5blp8.LoopFirmware?>);
|
||||
return _localReturnValue;
|
||||
} finally {
|
||||
await _localUniqueSession.close();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
_ida.Future<_idt.ByteData> download(
|
||||
_ist.TestSessionBuilder sessionBuilder,
|
||||
String hardwareCode,
|
||||
String version,
|
||||
) async {
|
||||
return _ist.callAwaitableFunctionAndHandleExceptions(() async {
|
||||
var _localUniqueSession =
|
||||
(sessionBuilder as _ist.InternalTestSessionBuilder).internalBuild(
|
||||
endpoint: 'firmware',
|
||||
method: 'download',
|
||||
);
|
||||
try {
|
||||
var _localCallContext = await _endpointDispatch.getMethodCallContext(
|
||||
createSessionCallback: (_) => _localUniqueSession,
|
||||
endpointPath: 'firmware',
|
||||
methodName: 'download',
|
||||
parameters: _ist.testObjectToJson({
|
||||
'hardwareCode': hardwareCode,
|
||||
'version': version,
|
||||
}),
|
||||
serializationManager: _serializationManager,
|
||||
);
|
||||
var _localReturnValue =
|
||||
await (_localCallContext.method.call(
|
||||
_localUniqueSession,
|
||||
_localCallContext.arguments,
|
||||
)
|
||||
as _ida.Future<_idt.ByteData>);
|
||||
return _localReturnValue;
|
||||
} finally {
|
||||
await _localUniqueSession.close();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class _MapEndpoint {
|
||||
_MapEndpoint(
|
||||
this._endpointDispatch,
|
||||
|
|
|
|||
Loading…
Reference in a new issue