Files
Europa/Nuvolari/app/test/data/radar/radar_manifest_test.dart
Alby96andClaude Opus 5 71557a02d7 Add the radar data layer with the offline mock adapter
Introduces the RadarSource seam and the manifest it speaks, plus the demo
frames that make the app runnable with no network and no credentials.

MockRadarSource is a real adapter rather than test scaffolding, and that is
what makes the rest of milestone 3 testable: the timeline, prefetching, cache
eviction and every degraded path can be exercised offline because the demo
frames parse the same manifest document the Python worker will publish. Its
assets come from tool/generate_mock_frames.py — committed so the app runs from
a clone, generated by a script so they can be regenerated instead of
hand-edited. Twenty-four 512x512 frames total 108 KB, and the generator is
pure standard library so nobody needs Pillow to build the app.

The manifest parser rejects three things that would otherwise fail silently and
look plausible:

- A CRS other than EPSG:3857. The map overlays each PNG on a lat/lng quad,
  which only lines up if the image is already in Web Mercator; anything else
  renders visibly skewed with no error to explain why.
- A missing attribution. The frames are a derived product of CC BY-SA data, so
  the credit has to travel with them rather than be remembered at render time.
- Legend stops that do not ascend, which would silently mislabel intensities.

The legend travels in the manifest rather than living as a constant here,
because the worker chose those colours when it rendered the PNGs and a local
copy could drift. RadarLegend.colorFor returns null below the lowest stop:
"no precipitation" has to be transparent, not the first colour of the ramp, or
a dry region renders as drizzle everywhere.

Every failure reaches the caller as a single RadarUnavailableException
regardless of cause, because the app's response is the same in all of them —
hold the last good frame and say how old it is — and branching on cause would
only invite divergence.

ArpaRadarSource is a stub whose every method throws. It is named by the region
config as unavailable and must fail loudly: quietly serving something else
would misreport where the data came from.

Verified: analyze clean, 95 tests passing, including a check that every frame
the manifest lists exists and is a real PNG, and that its bbox matches the
region config.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-10 12:04:22 +02:00

234 lines
6.9 KiB
Dart

import 'dart:convert';
import 'dart:ui' show Color;
import 'package:flutter_test/flutter_test.dart';
import 'package:nuvolari/data/radar/radar_manifest.dart';
Map<String, Object?> validManifest() => <String, Object?>{
'region': 'piemonte',
'product': 'VMI',
'generatedAt': 1768478400000,
'bbox': <double>[6.55, 43.95, 9.30, 46.55],
'crs': 'EPSG:3857',
'frames': <Object?>[
<String, Object?>{'ts': 1768478400000, 'url': 'frames/a.png'},
<String, Object?>{'ts': 1768478700000, 'url': 'frames/b.png'},
],
'legend': <String, Object?>{
'unit': 'dBZ',
'stops': <Object?>[
<String, Object?>{'value': 5, 'color': '#4FA3D1'},
<String, Object?>{'value': 30, 'color': '#E9D22B'},
],
},
'attribution': 'Radar-DPC — CC BY-SA',
};
RadarManifest parseWith(void Function(Map<String, Object?> doc) mutate) {
final doc = validManifest();
mutate(doc);
return RadarManifest.fromJson(doc);
}
void main() {
group('RadarManifest.fromJson', () {
test('parses a complete manifest', () {
final manifest = RadarManifest.fromJson(validManifest());
expect(manifest.regionId, 'piemonte');
expect(manifest.product, 'VMI');
expect(manifest.bounds.west, 6.55);
expect(manifest.frames, hasLength(2));
expect(manifest.legend.unit, 'dBZ');
expect(manifest.attribution, contains('Radar-DPC'));
expect(manifest.generatedAt.isUtc, isTrue);
});
test('frame timestamps are UTC', () {
final manifest = RadarManifest.fromJson(validManifest());
expect(manifest.frames.first.timestamp.isUtc, isTrue);
expect(
manifest.frames.first.timestamp.millisecondsSinceEpoch,
1768478400000,
);
});
test('sorts frames by timestamp regardless of publication order', () {
final manifest = parseWith((doc) {
doc['frames'] = <Object?>[
<String, Object?>{'ts': 1768478700000, 'url': 'frames/b.png'},
<String, Object?>{'ts': 1768478400000, 'url': 'frames/a.png'},
];
});
expect(
manifest.frames.map((frame) => frame.path),
orderedEquals(<String>['frames/a.png', 'frames/b.png']),
);
});
// The map overlays each PNG on a lat/lng quad, which only lines up if the
// image is already in Web Mercator. A different CRS renders visibly skewed
// with no error, so it has to be caught here.
test('rejects a manifest that is not in EPSG:3857', () {
expect(
() => parseWith((doc) => doc['crs'] = 'EPSG:4326'),
throwsA(
isA<FormatException>().having(
(e) => e.message,
'message',
contains('EPSG:3857'),
),
),
);
});
test('rejects a manifest with no CRS at all', () {
expect(
() => parseWith((doc) => doc.remove('crs')),
throwsFormatException,
);
});
// The frames are a derived product of CC BY-SA data. Publishing them
// without the credit travelling alongside is a licence breach, so a
// manifest that omits it must not load.
test('rejects a manifest with no attribution', () {
expect(
() => parseWith((doc) => doc.remove('attribution')),
throwsFormatException,
);
expect(
() => parseWith((doc) => doc['attribution'] = ''),
throwsFormatException,
);
});
test('rejects a non-integer timestamp', () {
expect(
() => parseWith((doc) {
doc['frames'] = <Object?>[
<String, Object?>{'ts': '1768478400000', 'url': 'frames/a.png'},
];
}),
throwsFormatException,
);
});
test('accepts an empty frame list', () {
final manifest = parseWith((doc) => doc['frames'] = <Object?>[]);
expect(manifest.isEmpty, isTrue);
expect(manifest.newestFrame, isNull);
expect(manifest.ageAt(DateTime.now()), Duration.zero);
});
});
group('RadarManifest.ageAt', () {
test('measures from the newest frame', () {
final manifest = RadarManifest.fromJson(validManifest());
final now = DateTime.fromMillisecondsSinceEpoch(
1768478700000 + Duration.millisecondsPerMinute * 12,
isUtc: true,
);
expect(manifest.ageAt(now), const Duration(minutes: 12));
});
test('handles a local-time argument', () {
final manifest = RadarManifest.fromJson(validManifest());
final now = DateTime.fromMillisecondsSinceEpoch(
1768478700000 + Duration.millisecondsPerMinute * 5,
);
expect(manifest.ageAt(now), const Duration(minutes: 5));
});
});
group('RadarLegend', () {
late RadarLegend legend;
setUp(() {
legend = RadarManifest.fromJson(validManifest()).legend;
});
test('parses #RRGGBB as opaque', () {
expect(legend.stops.first.color, const Color(0xFF4FA3D1));
});
test('picks the highest stop at or below the value', () {
expect(legend.colorFor(5), const Color(0xFF4FA3D1));
expect(legend.colorFor(29.9), const Color(0xFF4FA3D1));
expect(legend.colorFor(30), const Color(0xFFE9D22B));
expect(legend.colorFor(120), const Color(0xFFE9D22B));
});
// Below the lowest stop means "no precipitation", which must be transparent
// rather than the first colour of the ramp — otherwise a dry region renders
// as light drizzle everywhere.
test('returns null below the lowest stop', () {
expect(legend.colorFor(4.9), isNull);
expect(legend.colorFor(-10), isNull);
expect(legend.minimumValue, 5);
});
test('rejects stops that do not ascend', () {
expect(
() => parseWith((doc) {
doc['legend'] = <String, Object?>{
'unit': 'dBZ',
'stops': <Object?>[
<String, Object?>{'value': 30, 'color': '#E9D22B'},
<String, Object?>{'value': 5, 'color': '#4FA3D1'},
],
};
}),
throwsA(
isA<FormatException>().having(
(e) => e.message,
'message',
contains('ascend'),
),
),
);
});
test('rejects an empty stop list', () {
expect(
() => parseWith((doc) {
doc['legend'] = <String, Object?>{
'unit': 'dBZ',
'stops': <Object?>[],
};
}),
throwsFormatException,
);
});
test('rejects a malformed colour', () {
expect(
() => parseWith((doc) {
doc['legend'] = <String, Object?>{
'unit': 'dBZ',
'stops': <Object?>[
<String, Object?>{'value': 5, 'color': 'blue'},
],
};
}),
throwsFormatException,
);
});
});
group('RadarManifest.parse', () {
test('parses JSON text', () {
expect(RadarManifest.parse(jsonEncode(validManifest())).product, 'VMI');
});
test('rejects JSON that is not an object', () {
expect(() => RadarManifest.parse('[]'), throwsFormatException);
});
});
}