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
+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