import 'dart:async'; import 'package:cutdex/workspace/preview_controller_registry.dart'; import 'package:cutdex_sdk/runtime.dart'; import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart'; import '../support/preview_fixture.dart'; void main() { TestWidgetsFlutterBinding.ensureInitialized(); const channel = MethodChannel('registry-startup-test'); final messenger = TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger; test( 'pending startup exposes its controller and callers share initialization', () async { final gate = Completer(); final started = Completer(); messenger.setMockMethodCallHandler(channel, (call) async { expect(call.method, 'capabilities'); started.complete(); await gate.future; throw PlatformException(code: 'failed', message: 'startup failed'); }); final fixture = await createPreviewFixture( Uri.parse('file:///fixture.mp4'), ); final registry = PreviewControllerRegistry( CutdexRuntime(channel: channel), ); try { final pending = registry.prepare(('p', 't'), fixture.timeline); final failure = expectLater(pending, throwsA(isA())); await started.future; expect(registry.cached(('p', 't')), isNotNull); expect(registry.cached(('p', 't'))!.ready, isFalse); expect(registry.prepare(('p', 't'), fixture.timeline), same(pending)); gate.complete(); await failure; expect(registry.cached(('p', 't')), isNull); } finally { registry.dispose(); messenger.setMockMethodCallHandler(channel, null); await fixture.workspace.close(); await fixture.directory.delete(recursive: true); } }, ); test( 'invalidating pending startup cannot publish a closed controller', () async { final gate = Completer(); final started = Completer(); messenger.setMockMethodCallHandler(channel, (call) async { expect(call.method, 'capabilities'); started.complete(); await gate.future; return { 'contractVersion': 1, 'features': {'playback.preview': 'conditional'}, }; }); final fixture = await createPreviewFixture( Uri.parse('file:///fixture.mp4'), ); final registry = PreviewControllerRegistry( CutdexRuntime(channel: channel), ); try { final pending = registry.prepare(('p', 't'), fixture.timeline); final failure = expectLater(pending, throwsStateError); await started.future; final invalidation = registry.invalidate(('p', 't')); expect(registry.cached(('p', 't')), isNull); gate.complete(); await failure; await invalidation; expect(registry.cached(('p', 't')), isNull); } finally { registry.dispose(); messenger.setMockMethodCallHandler(channel, null); await fixture.workspace.close(); await fixture.directory.delete(recursive: true); } }, ); test( 'LRU trims idle output but preserves leased controllers until release', () async { var sessionId = 0; final closed = []; for (final id in ['1', '2', '3']) { final events = MethodChannel('cutdex_runtime/sessions/$id'); messenger.setMockMethodCallHandler(events, (call) async { if (call.method == 'listen') { scheduleMicrotask( () => messenger.handlePlatformMessage( events.name, const StandardMethodCodec().encodeSuccessEnvelope({ 'type': 'attached', }), (_) {}, ), ); } return null; }); } messenger.setMockMethodCallHandler(channel, (call) async { switch (call.method) { case 'capabilities': return { 'contractVersion': 1, 'features': {'playback.preview': 'available'}, }; case 'createSession': return {'sessionId': '${++sessionId}'}; case 'probe': return { 'duration': MediaTime(2, 1).toMap(), 'videoTrackCount': 1, 'videoTrackIds': [1], 'audioTrackCount': 0, 'audioTrackIds': [], 'isProtected': false, }; case 'load': return (call.arguments as Map)['plan']['revision']; case 'close': closed.add((call.arguments as Map)['sessionId'] as String); } return null; }); final first = await createPreviewFixture(Uri.file('/fixture-1.mov')); final second = await createPreviewFixture(Uri.file('/fixture-2.mov')); final third = await createPreviewFixture(Uri.file('/fixture-3.mov')); final registry = PreviewControllerRegistry( CutdexRuntime(channel: channel), maximumCachedControllers: 2, ); try { final releaseFirst = registry.retain(('p', 'first')); final releaseSecond = registry.retain(('p', 'second')); final releaseThird = registry.retain(('p', 'third')); final one = await registry.prepare(('p', 'first'), first.timeline); final two = await registry.prepare(('p', 'second'), second.timeline); final three = await registry.prepare(('p', 'third'), third.timeline); expect(one!.ready, isTrue); expect(two!.ready, isTrue); expect(three!.ready, isTrue); expect(registry.cached(('p', 'first')), same(one)); releaseFirst(); expect(registry.cached(('p', 'first')), isNull); expect(registry.cached(('p', 'second')), same(two)); expect(registry.cached(('p', 'third')), same(three)); releaseFirst(); // Releases are idempotent. releaseSecond(); releaseThird(); await registry.invalidate(('p', 'second')); await registry.invalidate(('p', 'third')); expect(closed.toSet(), {'1', '2', '3'}); } finally { registry.dispose(); for (final id in ['1', '2', '3']) { messenger.setMockMethodCallHandler( MethodChannel('cutdex_runtime/sessions/$id'), null, ); } messenger.setMockMethodCallHandler(channel, null); for (final fixture in [first, second, third]) { await fixture.workspace.close(); await fixture.directory.delete(recursive: true); } } }, ); }