test(m3b): headless golden tests via m3b match source abstraction

This commit is contained in:
2026-10-24 23:02:13 +02:00
parent bf267a97f9
commit e8908ef4bb
8 changed files with 265 additions and 16 deletions
+24 -11
View File
@@ -4,6 +4,7 @@ import 'dart:ui' as ui;
import 'package:flame/game.dart'; import 'package:flame/game.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import '../native/match_source.dart';
import '../native/native_match.dart'; import '../native/native_match.dart';
/// World dimensions in simulation pixels (must match crates/sim). /// World dimensions in simulation pixels (must match crates/sim).
@@ -58,11 +59,14 @@ class HudState {
/// The playable match. Flame drives the loop; Rust owns the simulation. /// The playable match. Flame drives the loop; Rust owns the simulation.
class IronArcGame extends FlameGame { class IronArcGame extends FlameGame {
IronArcGame({this.seed = 42}); IronArcGame({this.seed = 42, this.matchFactory});
final int seed; final int seed;
late NativeMatch _match; /// Optional override, used by golden tests to run without the native library.
final MatchSource Function()? matchFactory;
late final MatchSource _match;
final ValueNotifier<HudState> hud = ValueNotifier<HudState>( final ValueNotifier<HudState> hud = ValueNotifier<HudState>(
const HudState( const HudState(
turn: 1, turn: 1,
@@ -97,7 +101,7 @@ class IronArcGame extends FlameGame {
@override @override
Future<void> onLoad() async { Future<void> onLoad() async {
_match = NativeMatch(seed); _match = matchFactory?.call() ?? NativeMatch(seed);
_pushHud(); _pushHud();
} }
@@ -245,16 +249,25 @@ class IronArcGame extends FlameGame {
); );
canvas.drawRect(rect, paint); canvas.drawRect(rect, paint);
// Distant hills for depth. // Distant hills for depth. They stay well above any playable ground so they
// can never bleed through the destructible terrain.
final hills = Path() final hills = Path()
..moveTo(0, 760) ..moveTo(0, 470)
..quadraticBezierTo(360, 620, 720, 750) ..quadraticBezierTo(300, 360, 620, 450)
..quadraticBezierTo(1120, 880, 1500, 720) ..quadraticBezierTo(1000, 540, 1340, 400)
..quadraticBezierTo(1800, 610, kWorldWidth, 740) ..quadraticBezierTo(1700, 300, kWorldWidth, 440)
..lineTo(kWorldWidth, kWorldHeight) ..lineTo(kWorldWidth, 560)
..lineTo(0, kWorldHeight) ..lineTo(0, 560)
..close(); ..close();
canvas.drawPath(hills, Paint()..color = const Color(0x33FFFFFF)); canvas.drawPath(
hills,
Paint()
..shader = ui.Gradient.linear(
const Offset(0, 330),
const Offset(0, 560),
const [Color(0x2EFFFFFF), Color(0x00FFFFFF)],
),
);
} }
void _drawTerrain(Canvas canvas) { void _drawTerrain(Canvas canvas) {
+48
View File
@@ -0,0 +1,48 @@
import 'dart:typed_data';
/// Everything the renderer needs from a match.
///
/// Implemented by [NativeMatch] (real Rust simulation) and by a fake in tests,
/// so the renderer can be verified headlessly (golden tests) without the
/// Android shared library.
abstract class MatchSource {
/// Allows subclasses to inherit the default getters below.
const MatchSource();
/// 0 = aiming, 1 = flying, 2 = finished.
int get phase;
/// Player whose turn it is.
int get current;
/// Wind in units -8..8.
int get wind;
/// Turn number starting at 1.
int get turn;
/// 0/1 winner, 255 draw, 254 running.
int get winner;
bool get isAiming => phase == 0;
bool get isFlying => phase == 1;
bool get isFinished => phase == 2;
int tankX(int player);
int tankY(int player);
int tankHp(int player);
/// Bumped whenever the terrain changed.
int get terrainVersion;
/// Heightmap samples in world pixels.
Uint16List terrain();
({int x, int y})? projectile();
({int x, int y}) lastImpact();
int fire({required int angleMdeg, required int powerQ16, int weapon});
int endTurn();
int tick(int ticks);
void dispose();
}
+19 -5
View File
@@ -3,6 +3,8 @@ import 'dart:typed_data';
import 'package:ffi/ffi.dart'; import 'package:ffi/ffi.dart';
import 'match_source.dart';
/// Raw C signatures of the Rust match API (see crates/ffi/src/match_api.rs). /// Raw C signatures of the Rust match API (see crates/ffi/src/match_api.rs).
typedef _NewMatchC = Pointer<Void> Function(Uint64); typedef _NewMatchC = Pointer<Void> Function(Uint64);
typedef _NewMatchDart = Pointer<Void> Function(int); typedef _NewMatchDart = Pointer<Void> Function(int);
@@ -55,7 +57,7 @@ typedef _ImpactDart = int Function(Pointer<Void>, Pointer<Int32>);
/// Thin typed wrapper around the Rust match handle. /// Thin typed wrapper around the Rust match handle.
/// ///
/// The handle owns the simulation; Dart only pulls snapshots for rendering. /// The handle owns the simulation; Dart only pulls snapshots for rendering.
class NativeMatch { class NativeMatch extends MatchSource {
NativeMatch(int seed) : _handle = _newMatch(seed); NativeMatch(int seed) : _handle = _newMatch(seed);
final Pointer<Void> _handle; final Pointer<Void> _handle;
@@ -102,45 +104,55 @@ class NativeMatch {
_lib.lookupFunction<_ImpactC, _ImpactDart>('iron_arc_last_impact'); _lib.lookupFunction<_ImpactC, _ImpactDart>('iron_arc_last_impact');
/// Phase: 0 aiming, 1 flying, 2 finished. /// Phase: 0 aiming, 1 flying, 2 finished.
@override
int get phase => _phase(_handle); int get phase => _phase(_handle);
bool get isAiming => phase == 0;
bool get isFlying => phase == 1;
bool get isFinished => phase == 2;
/// Active player index. /// Active player index.
@override
int get current => _current(_handle); int get current => _current(_handle);
/// Wind in units -8..8. /// Wind in units -8..8.
@override
int get wind => _wind(_handle); int get wind => _wind(_handle);
/// Turn number starting at 1. /// Turn number starting at 1.
@override
int get turn => _turn(_handle); int get turn => _turn(_handle);
/// 0/1 winner, 255 draw, 254 still running. /// 0/1 winner, 255 draw, 254 still running.
@override
int get winner => _winner(_handle); int get winner => _winner(_handle);
@override
int tankX(int player) => _tankX(_handle, player); int tankX(int player) => _tankX(_handle, player);
@override
int tankY(int player) => _tankY(_handle, player); int tankY(int player) => _tankY(_handle, player);
@override
int tankHp(int player) => _tankHp(_handle, player); int tankHp(int player) => _tankHp(_handle, player);
/// Fires a shot. Returns 0 on success. /// Fires a shot. Returns 0 on success.
@override
int fire({required int angleMdeg, required int powerQ16, int weapon = 1}) => int fire({required int angleMdeg, required int powerQ16, int weapon = 1}) =>
_fire(_handle, angleMdeg, powerQ16, weapon); _fire(_handle, angleMdeg, powerQ16, weapon);
@override
int endTurn() => _endTurn(_handle); int endTurn() => _endTurn(_handle);
/// Advances the simulation, returns the number of impacts. /// Advances the simulation, returns the number of impacts.
@override
int tick(int ticks) => _tick(_handle, ticks); int tick(int ticks) => _tick(_handle, ticks);
/// Bumped whenever the terrain changed. /// Bumped whenever the terrain changed.
@override
int get terrainVersion => _terrainVersion(_handle); int get terrainVersion => _terrainVersion(_handle);
/// Live view into the Rust heightmap (valid until [dispose]). /// Live view into the Rust heightmap (valid until [dispose]).
@override
Uint16List terrain() => Uint16List terrain() =>
_terrainPtr(_handle).asTypedList(_terrainLen()); _terrainPtr(_handle).asTypedList(_terrainLen());
/// Projectile position in world pixels, or null if none is in flight. /// Projectile position in world pixels, or null if none is in flight.
@override
({int x, int y})? projectile() { ({int x, int y})? projectile() {
final out = calloc<Int32>(2); final out = calloc<Int32>(2);
try { try {
@@ -153,6 +165,7 @@ class NativeMatch {
} }
/// World position of the most recent impact. /// World position of the most recent impact.
@override
({int x, int y}) lastImpact() { ({int x, int y}) lastImpact() {
final out = calloc<Int32>(2); final out = calloc<Int32>(2);
try { try {
@@ -163,6 +176,7 @@ class NativeMatch {
} }
} }
@override
void dispose() { void dispose() {
if (_disposed) return; if (_disposed) return;
_disposed = true; _disposed = true;
+125
View File
@@ -0,0 +1,125 @@
import 'dart:math' as math;
import 'dart:typed_data';
import 'package:iron_arc/src/native/match_source.dart';
/// Deterministic stand-in for [NativeMatch] used by golden tests.
///
/// Golden tests must run on a plain Dart VM without the Android shared library,
/// so the renderer is fed with a hand-built world instead.
class FakeMatch extends MatchSource {
FakeMatch({
this.phase = 0,
this.current = 0,
this.wind = 3,
this.turn = 1,
this.winner = 254,
this.projectilePos,
this.impactPos,
List<int>? heights,
}) : _heights = Uint16List.fromList(heights ?? _defaultHeights()),
_terrainVersion = heights == null ? 0 : 1;
@override
int phase;
@override
int current;
@override
int wind;
@override
int turn;
@override
int winner;
({int x, int y})? projectilePos;
({int x, int y})? impactPos;
final Uint16List _heights;
int _terrainVersion;
/// Aims scene: player 0 to move, no projectile.
factory FakeMatch.aiming() => FakeMatch();
/// Mid-flight scene: projectile above the middle of the map.
factory FakeMatch.flying() => FakeMatch(
phase: 1,
projectilePos: (x: 1180, y: 300),
);
/// After impact: crater carved, player 1 to move.
factory FakeMatch.afterImpact() {
final heights = _defaultHeights();
// Carve a crater around x = 1500 (sample index 750).
for (var i = 735; i <= 765; i++) {
final dx = (i - 750).abs();
heights[i] = math.min(1152, heights[i] + (30 - dx) * 4);
}
heights[820] = math.min(1152, heights[820] + 25);
return FakeMatch(
phase: 0,
current: 1,
turn: 2,
wind: -2,
heights: heights,
impactPos: (x: 1500, y: 700),
);
}
static List<int> _defaultHeights() {
final heights = <int>[];
for (var i = 0; i < 1024; i++) {
final y = 700 +
(60 * math.sin(i / 47.0)).round() +
(26 * math.sin(i / 13.0 + 1.3)).round();
heights.add(y.clamp(120, 1100));
}
// Level platform under both spawns so tanks stand straight.
for (var i = 100; i <= 120; i++) {
heights[i] = heights[110];
}
for (var i = 900; i <= 920; i++) {
heights[i] = heights[910];
}
return heights;
}
@override
int tankX(int player) => player == 0 ? 220 : 1828;
@override
int tankY(int player) => _heights[tankX(player) ~/ 2];
@override
int tankHp(int player) => player == 0 ? 100 : 62;
@override
int get terrainVersion => _terrainVersion;
@override
Uint16List terrain() => _heights;
@override
({int x, int y})? projectile() => projectilePos;
@override
({int x, int y}) lastImpact() => impactPos ?? (x: 0, y: 0);
@override
int fire({required int angleMdeg, required int powerQ16, int weapon = 1}) {
phase = 1;
return 0;
}
@override
int endTurn() {
current = 1 - current;
turn += 1;
return 0;
}
@override
int tick(int ticks) => 0;
@override
void dispose() {}
}
+49
View File
@@ -0,0 +1,49 @@
import 'package:flame/game.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:iron_arc/src/game/iron_arc_game.dart';
import 'fake_match.dart';
/// Golden tests render the game headlessly (no device, no native library).
/// Update with: flutter test --update-goldens
Future<void> pumpGame(WidgetTester tester, IronArcGame game) async {
await tester.binding.setSurfaceSize(const Size(420, 840));
addTearDown(() => tester.binding.setSurfaceSize(null));
await tester.pumpWidget(MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData.dark(useMaterial3: true),
home: GameWidget(game: game),
));
await tester.pump();
await tester.pump(const Duration(milliseconds: 40));
}
void main() {
testWidgets('aiming scene renders', (tester) async {
final game = IronArcGame(matchFactory: FakeMatch.aiming);
await pumpGame(tester, game);
await expectLater(
find.byType(MaterialApp),
matchesGoldenFile('goldens/aiming.png'),
);
});
testWidgets('projectile in flight renders', (tester) async {
final game = IronArcGame(matchFactory: FakeMatch.flying);
await pumpGame(tester, game);
await expectLater(
find.byType(MaterialApp),
matchesGoldenFile('goldens/flying.png'),
);
});
testWidgets('crater after impact renders', (tester) async {
final game = IronArcGame(matchFactory: FakeMatch.afterImpact);
await pumpGame(tester, game);
await expectLater(
find.byType(MaterialApp),
matchesGoldenFile('goldens/after_impact.png'),
);
});
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 102 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 88 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 87 KiB