// Dedicated test app: never distribute its release packages. import 'dart:async'; import 'dart:convert'; import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; const channel = MethodChannel('deskup'); const smokeRoot = String.fromEnvironment('CUTDEX_UPDATE_SMOKE_ROOT'); void main() { WidgetsFlutterBinding.ensureInitialized(); // The isolated smoke fixture depends only on Flutter, without Riverpod. // ignore: riverpod_lint/missing_provider_scope runApp( const MaterialApp( home: Scaffold(body: Center(child: Text('Cutdex update test'))), ), ); unawaited(runSmoke()); } Future> status() async => await channel.invokeMapMethod('status') ?? {}; Future> waitFor( bool Function(Map) predicate, ) async { final deadline = DateTime.now().add(const Duration(minutes: 3)); while (DateTime.now().isBefore(deadline)) { final value = await status(); if (predicate(value)) return value; await Future.delayed(const Duration(milliseconds: 100)); } throw StateError( 'Timed out waiting for native update state: ${await status()}', ); } Future record(String name, Object value) async { await File('$smokeRoot/$name.json') .writeAsString(jsonEncode(value), flush: true); } Future runSmoke() async { if (smokeRoot.isEmpty) { throw StateError('Set CUTDEX_UPDATE_SMOKE_ROOT for this test app'); } try { await channel.invokeMethod('ready'); final initial = await waitFor( (value) => value['configured'] == true && value['canCheck'] == true, ); await channel.invokeMethod('check'); var checked = await waitFor((value) => value['canCheck'] == true); if (initial['build'] == '900002') { if (checked['phase'] != 'current') { throw StateError('Updated version was offered again: $checked'); } await record('after', checked); exit(0); } if (checked['phase'] != 'available' || checked['availableVersion'] != '1.0.0-build.900002') { throw StateError('Expected version 1.0.0-build.900002: $checked'); } await channel.invokeMethod('download'); final rejected = await waitFor((value) => value['canCheck'] == true); if (rejected['phase'] == 'ready' || rejected['error'] == '') { throw StateError('Corrupted package was not rejected: $rejected'); } await record('corrupt-rejected', rejected); await channel.invokeMethod('download'); checked = await waitFor((value) => value['canCheck'] == true); if (checked['phase'] != 'ready' || checked['error'] != '') { throw StateError('Could not download valid update: $checked'); } await record('before', checked); await channel.invokeMethod('validateInstall'); await channel.invokeMethod('install'); } catch (error, stack) { await record('failure', {'error': '$error', 'stack': '$stack'}); exit(1); } }