import 'dart:io'; import 'dart:convert'; import 'package:crypto/crypto.dart'; import 'package:cutdex/integrations/runtime/export_publication.dart'; import 'package:cutdex_core/editor.dart'; import 'package:flutter_test/flutter_test.dart'; void main() { late Directory root; setUp( () async => root = await Directory.systemTemp.createTemp('export-recovery-'), ); tearDown(() => root.delete(recursive: true)); test( 'published output is recovered repeatedly without encoding again', () async { final destination = File('${root.path}/result.mp4').uri; var calls = 0; Future encode(Uri uri) async { calls++; await File.fromUri(uri).writeAsString('encoded movie'); return uri; } await ExportPublication(destination, 'op').run('snapshot', encode); for (var i = 0; i < 3; i++) { expect( await ExportPublication(destination, 'op').run('snapshot', encode), destination, ); } expect(calls, 1); await expectLater( ExportPublication(destination, 'op').run('other', encode), throwsA(isA()), ); }, ); test('ready staging receipt recovers the publication crash window', () async { final destination = File('${root.path}/result.mp4').uri; final job = ExportPublication(destination, 'op'); await job.directory.create(); final bytes = utf8.encode('complete movie'); await job.staged.writeAsBytes(bytes); await job.receipt.writeAsString( jsonEncode({ 'operationId': 'op', 'destination': destination.toString(), 'fingerprint': 'snapshot', 'bytes': bytes.length, 'sha256': sha256.convert(bytes).toString(), }), ); expect(await ExportPublication(destination, 'op').recover(), destination); expect(await job.staged.exists(), false); expect(await File.fromUri(destination).readAsBytes(), bytes); }); test( 'incomplete and externally changed output never silently overwrites', () async { final destination = File('${root.path}/result.mp4').uri; final job = ExportPublication(destination, 'op'); await expectLater( job.run('snapshot', (_) async => throw StateError('killed encoder')), throwsStateError, ); expect(await job.recover(), null); await expectLater( job.run('snapshot', (_) async => destination), throwsA(isA()), ); expect(await File.fromUri(destination).exists(), false); final next = ExportPublication(destination, 'explicit-retry'); await next.run('snapshot', (uri) async { await File.fromUri(uri).writeAsString('complete'); return uri; }); await File.fromUri(destination).writeAsString('changed'); await expectLater(next.recover(), throwsA(isA())); expect(await File.fromUri(destination).readAsString(), 'changed'); }, ); }