Add bidirectional host bridge
This commit is contained in:
93
test/runtime/host/runtime_host_bridge_test.dart
Normal file
93
test/runtime/host/runtime_host_bridge_test.dart
Normal file
@@ -0,0 +1,93 @@
|
||||
import 'package:flame_lua_runtime/runtime/diagnostics/runtime_diagnostics.dart';
|
||||
import 'package:flame_lua_runtime/runtime/host/runtime_host_bridge.dart';
|
||||
import 'package:flame_lua_runtime/runtime/models/runtime_event.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
|
||||
void main() {
|
||||
group('RuntimeHostBridgeManager', () {
|
||||
test('calls registered host handler and emits result event', () async {
|
||||
final events = <RuntimeEvent>[];
|
||||
final manager = RuntimeHostBridgeManager(
|
||||
bridge: RuntimeHostBridge(
|
||||
handlers: {
|
||||
'user.profile': (call) => {'name': 'Lua', 'id': call.data},
|
||||
},
|
||||
),
|
||||
eventSink: events.add,
|
||||
);
|
||||
|
||||
await manager.callHost(
|
||||
const RuntimeHostCall(id: 'call_1', method: 'user.profile', data: 7),
|
||||
);
|
||||
|
||||
expect(events.single.type, RuntimeHostEventType.callResult);
|
||||
expect(events.single.data['ok'], isTrue);
|
||||
expect(events.single.data['result'], {'name': 'Lua', 'id': 7});
|
||||
});
|
||||
|
||||
test(
|
||||
'emits failed result and diagnostics when host handler throws',
|
||||
() async {
|
||||
final diagnostics = RuntimeDiagnostics();
|
||||
final events = <RuntimeEvent>[];
|
||||
final manager = RuntimeHostBridgeManager(
|
||||
bridge: RuntimeHostBridge(
|
||||
handlers: {'boom': (_) => throw StateError('boom')},
|
||||
),
|
||||
eventSink: events.add,
|
||||
diagnostics: diagnostics,
|
||||
);
|
||||
|
||||
await manager.callHost(
|
||||
const RuntimeHostCall(id: 'call_1', method: 'boom'),
|
||||
);
|
||||
|
||||
expect(events.single.data['ok'], isFalse);
|
||||
expect(events.single.data['error'], contains('boom'));
|
||||
expect(
|
||||
diagnostics.entries.single.type,
|
||||
RuntimeDiagnosticType.hostBridgeError,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
test('notifies host and emits Lua calls', () async {
|
||||
RuntimeHostNotification? notification;
|
||||
final events = <RuntimeEvent>[];
|
||||
final manager = RuntimeHostBridgeManager(
|
||||
bridge: RuntimeHostBridge(onNotify: (value) => notification = value),
|
||||
eventSink: events.add,
|
||||
);
|
||||
|
||||
expect(
|
||||
manager.notifyHost(
|
||||
const RuntimeHostNotification(
|
||||
method: 'analytics',
|
||||
data: {'level': 2},
|
||||
),
|
||||
),
|
||||
isTrue,
|
||||
);
|
||||
expect(notification?.method, 'analytics');
|
||||
expect(manager.notifyLua('pause', data: {'reason': 'host'}), isTrue);
|
||||
|
||||
expect(events.single.type, RuntimeHostEventType.notify);
|
||||
expect(events.single.data['method'], 'pause');
|
||||
});
|
||||
|
||||
test('completes Flutter-to-Lua call through host response', () async {
|
||||
final events = <RuntimeEvent>[];
|
||||
final manager = RuntimeHostBridgeManager(
|
||||
bridge: const RuntimeHostBridge(),
|
||||
eventSink: events.add,
|
||||
);
|
||||
|
||||
final future = manager.callLua('select_avatar', data: {'current': 1});
|
||||
final id = events.single.data['id']! as String;
|
||||
expect(events.single.type, RuntimeHostEventType.call);
|
||||
expect(manager.completeLuaCall(id, result: {'selected': 3}), isTrue);
|
||||
|
||||
expect(await future, {'selected': 3});
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user