Initial flame_lua_runtime package

This commit is contained in:
gem
2026-06-07 22:53:58 +08:00
commit 733b2fb798
262 changed files with 28439 additions and 0 deletions

View File

@@ -0,0 +1,121 @@
import 'package:flame_lua_runtime/runtime/models/game_diff.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
group('GameDiff', () {
test('parses render, ui and commands', () {
final diff = GameDiff.fromMap({
'render': {
'creates': [
{'id': 'board', 'type': 'image', 'asset': 'board'},
],
'updates': [
{
'id': 'piece_red_1',
'props': {'x': 100, 'y': 120},
},
],
'removes': ['old_piece'],
},
'ui': {
'creates': [
{'id': 'dice_button', 'type': 'button', 'text': 'Roll'},
],
},
'commands': [
{'type': 'move_path', 'target': 'piece_red_1', 'duration': 0.5},
],
});
expect(diff.render.creates.single.id, 'board');
expect(diff.render.updates.single.id, 'piece_red_1');
expect(diff.render.updates.single.props['x'], 100);
expect(diff.render.removes.single.id, 'old_piece');
expect(diff.ui.creates.single.id, 'dice_button');
expect(diff.commands.single.type, 'move_path');
expect(diff.commands.single.target, 'piece_red_1');
expect(diff.commands.single.payload['duration'], 0.5);
});
test('treats missing sections as empty diffs', () {
final diff = GameDiff.fromMap({});
expect(diff.render.creates, isEmpty);
expect(diff.render.updates, isEmpty);
expect(diff.render.removes, isEmpty);
expect(diff.ui.creates, isEmpty);
expect(diff.commands, isEmpty);
});
test('accepts Lua numeric-key tables as lists', () {
final diff = GameDiff.fromMap({
'render': {
'creates': {
2: {'id': 'b', 'type': 'text'},
1: {'id': 'a', 'type': 'text'},
},
'updates': {},
'removes': {
1: {'id': 'old_a'},
2: 'old_b',
},
},
'commands': {
1: {'type': 'toast', 'message': 'hi'},
},
});
expect(diff.render.creates.map((node) => node.id), ['a', 'b']);
expect(diff.render.removes.map((node) => node.id), ['old_a', 'old_b']);
expect(diff.commands.single.type, 'toast');
expect(diff.commands.single.payload['message'], 'hi');
});
test('rejects malformed diff fields', () {
expect(
() => GameDiff.fromMap({
'render': {'creates': 'bad'},
}),
throwsFormatException,
);
expect(
() => GameDiff.fromMap({
'render': {
'updates': [
{'id': 'node', 'props': 'bad'},
],
},
}),
throwsFormatException,
);
expect(
() => GameDiff.fromMap({
'commands': [
{'type': ''},
],
}),
throwsFormatException,
);
expect(() => GameDiff.fromMap({'unknown': {}}), throwsFormatException);
expect(
() => GameDiff.fromMap({
'render': {'createz': []},
}),
throwsFormatException,
);
expect(
() => GameDiff.fromMap({
'render': {
'updates': [
{
'id': 'node',
'props': {'interative': true},
},
],
},
}),
throwsFormatException,
);
});
});
}