Files
cutdex/test/features/media/background_removal_controller_test.dart
T
lingjhf 27e77fff39 feat: initial Cutdex application
Add the Flutter video editing application with project management, media
import, timeline editing, preview, AI assistance, diagnostics and settings.

Include shared UI and native host packages, platform runners, application
tests and release tooling.

Provide usage and contribution guides, the MIT license and third-party
notices. Exclude local credentials, build outputs and validation data.
2026-10-05 23:36:23 +08:00

88 lines
3.1 KiB
Dart

import 'dart:async';
import 'dart:io';
import 'package:crypto/crypto.dart';
import 'package:cutdex/features/media/background_removal_controller.dart';
import 'package:cutdex/features/media/processing_targets.dart';
import 'package:cutdex/integrations/runtime/processing_resource_repository.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
import '../../support/preview_fixture.dart';
void main() {
test('closing the download view retains the operation until explicit cancellation', () async {
final fixture = await createPreviewFixture(Uri.file('/unused.mov'));
final server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);
final started = Completer<void>();
final release = Completer<void>();
const payload = [1, 2, 3];
server.listen((request) async {
started.complete();
await release.future;
try {
request.response.add(payload);
await request.response.close();
} on SocketException {
// Cancellation closes the downloading client's socket.
} on HttpException {
// The cancelled request may already have closed.
}
});
final repository = ProcessingResourceRepository(
root: Directory('${fixture.directory.path}/Processing'),
catalog: [
ProcessingResourcePackage(
capability: ProcessingCapability.backgroundRemoval,
id: 'model',
version: '1',
name: 'Model',
description: '',
artifacts: [
ProcessingResourceArtifact(
path: 'model.onnx',
url: Uri.parse('http://localhost:${server.port}/model'),
sha256: sha256.convert(payload).toString(),
bytes: payload.length,
),
],
),
],
);
final container = ProviderContainer();
addTearDown(() async {
if (!release.isCompleted) release.complete();
container.dispose();
await server.close(force: true);
repository.dispose();
await fixture.workspace.close();
await fixture.directory.delete(recursive: true);
});
final provider = backgroundRemovalControllerProvider(
fixture.timeline,
BackgroundRemovalClipTarget(
sequence: fixture.timeline.activeSequenceId!,
clipId: 'clip',
mediaId: 'media',
name: 'Clip',
),
);
final subscription = container.listen(provider, (_, _) {});
final controller = container.read(provider.notifier);
final operation = controller.start(repository, download: true);
await started.future.timeout(const Duration(seconds: 5));
subscription.close();
await container.pump();
expect(container.exists(provider), isTrue);
expect(container.read(provider), isA<RemovalDownloading>());
controller.cancel(repository);
release.complete();
await operation.timeout(const Duration(seconds: 5));
expect(fixture.timeline.backgroundRemovals.tasks, isEmpty);
expect(
repository.installFor(ProcessingCapability.backgroundRemoval).state,
ProcessingResourceState.notInstalled,
);
});
}