|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// Part of PyBLE (https://pyble.dev) — see /LICENSE. |
| 3 | + |
| 4 | +/// Two launches of an isolated qualification application exercise actual |
| 5 | +/// platform storage. Run seed, terminate the process, then run verify with the |
| 6 | +/// same application ID/data container. This uses a recording board seam and |
| 7 | +/// does not qualify physical BLE or a store-distributed binary. |
| 8 | +library; |
| 9 | + |
| 10 | +import 'dart:io'; |
| 11 | +import 'package:flutter_riverpod/flutter_riverpod.dart'; |
| 12 | +import 'package:flutter_test/flutter_test.dart'; |
| 13 | +import 'package:integration_test/integration_test.dart'; |
| 14 | +import 'package:path_provider/path_provider.dart'; |
| 15 | +import 'package:pyble/app/providers.dart'; |
| 16 | +import 'package:pyble/data/draft_repository.dart'; |
| 17 | +import 'package:pyble/data/drift_draft_repository.dart'; |
| 18 | +import 'package:pyble/editor/board_target_guard.dart'; |
| 19 | +import 'package:pyble/editor/draft_recovery.dart'; |
| 20 | +import 'package:pyble/editor/editor_document.dart'; |
| 21 | +import 'package:pyble/editor/save_controller.dart'; |
| 22 | +import 'package:pyble/pble/pble.dart'; |
| 23 | +import '../test/support/recording_connection.dart'; |
| 24 | + |
| 25 | +const _source = '# recovery qualification — ทดสอบ\nprint(“exact text”)\n'; |
| 26 | +const _phase = String.fromEnvironment( |
| 27 | + 'PYBLE_RECOVERY_TEST_PHASE', |
| 28 | + defaultValue: 'seed', |
| 29 | +); |
| 30 | + |
| 31 | +void main() { |
| 32 | + IntegrationTestWidgetsFlutterBinding.ensureInitialized(); |
| 33 | + testWidgets('platform durable recovery ($_phase)', (tester) async { |
| 34 | + final directory = await getApplicationSupportDirectory(); |
| 35 | + final file = File('${directory.path}/qualification-current-draft.sqlite'); |
| 36 | + final repository = await DriftDraftRepository.open(file); |
| 37 | + try { |
| 38 | + final existing = await repository.load(); |
| 39 | + if (_phase == 'seed') { |
| 40 | + await repository.write( |
| 41 | + DraftSnapshot( |
| 42 | + name: 'qualification.py', |
| 43 | + content: _source, |
| 44 | + originPath: '/previous-board/qualification.py', |
| 45 | + generation: existing.generation + 1, |
| 46 | + checkpointTime: DateTime.now().toUtc(), |
| 47 | + ), |
| 48 | + expectedGeneration: existing.generation, |
| 49 | + ); |
| 50 | + expect((await repository.load()).snapshot!.content, _source); |
| 51 | + } else { |
| 52 | + expect(_phase, 'verify'); |
| 53 | + expect( |
| 54 | + existing.snapshot, |
| 55 | + isNotNull, |
| 56 | + reason: 'The seed must survive a separate app process launch.', |
| 57 | + ); |
| 58 | + final connection = RecordingConnection(initial: ConnState.ready); |
| 59 | + final container = ProviderContainer( |
| 60 | + overrides: [ |
| 61 | + draftRepositoryProvider.overrideWithValue(repository), |
| 62 | + connectionProvider.overrideWithValue(connection), |
| 63 | + boardTargetConfirmationProvider.overrideWithValue( |
| 64 | + (_) async => false, |
| 65 | + ), |
| 66 | + ], |
| 67 | + ); |
| 68 | + try { |
| 69 | + final recovery = container.read(draftRecoveryProvider.notifier); |
| 70 | + await recovery.start(); |
| 71 | + expect( |
| 72 | + container.read(draftRecoveryProvider).status, |
| 73 | + DraftRecoveryStatus.decision, |
| 74 | + ); |
| 75 | + expect(recovery.restore(), isTrue); |
| 76 | + expect(container.read(editorDocumentProvider).content, _source); |
| 77 | + expect(container.read(editorDocumentProvider).boardPath, isNull); |
| 78 | + expect(container.read(editorDocumentProvider).dirty, isTrue); |
| 79 | + expect(connection.putFileCalls, isEmpty); |
| 80 | + expect(connection.runFileCalls, isEmpty); |
| 81 | + await expectLater( |
| 82 | + container.read(saveControllerProvider).save(), |
| 83 | + throwsA(isA<BoardActionCancelled>()), |
| 84 | + ); |
| 85 | + expect(connection.putFileCalls, isEmpty); |
| 86 | + } finally { |
| 87 | + container.dispose(); |
| 88 | + } |
| 89 | + } |
| 90 | + } finally { |
| 91 | + await repository.close(); |
| 92 | + } |
| 93 | + }); |
| 94 | +} |
0 commit comments