import 'dart:async' as async; import 'package:flame_lua_runtime/runtime/lifecycle/runtime_task_registry.dart'; import 'package:flutter_test/flutter_test.dart'; void main() { group('RuntimeTaskRegistry', () { test('cancels tasks by scope', () async { final registry = RuntimeTaskRegistry(cancelledValue: 'cancelled'); final scoped = registry.create(scope: 'panel'); final other = registry.create(scope: 'other'); registry.cancelScope('panel'); expect(await scoped.future, 'cancelled'); expect(scoped.isCancelled, isTrue); expect(other.isCancelled, isFalse); expect(registry.scopedTaskCount('panel'), 0); expect(registry.scopedTaskCount('other'), 1); other.complete('done'); expect(await other.future, 'done'); expect(registry.activeTaskCount, 0); }); test('cancel runs callbacks and cancels timers', () async { final registry = RuntimeTaskRegistry(cancelledValue: 'cancelled'); final task = registry.create(scope: 'panel'); var callbackCalled = false; var timerFired = false; final timer = async.Timer(const Duration(milliseconds: 30), () { timerFired = true; }); task ..addTimer(timer) ..addCancelCallback(() { callbackCalled = true; }) ..cancel(); await Future.delayed(const Duration(milliseconds: 40)); expect(await task.future, 'cancelled'); expect(callbackCalled, isTrue); expect(timerFired, isFalse); }); test('dispose cancels all active tasks', () async { final registry = RuntimeTaskRegistry(cancelledValue: 'cancelled'); final first = registry.create(); final second = registry.create(scope: 'panel'); registry.dispose(); expect(await first.future, 'cancelled'); expect(await second.future, 'cancelled'); expect(registry.activeTaskCount, 0); }); }); }