Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

38.5 KB
Raw Blame
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:math' show min;
import 'dart:typed_data';

import 'package:cryptography/cryptography.dart';
import 'package:cryptography/dart.dart' show DartSha256;
import 'package:dio/dio.dart';
import 'package:archive/archive.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:harness/update/desktop_updater.dart';
import 'package:harness/update/update_signature.dart';
import 'package:path/path.dart' as p;

Future<String> _sha256Hex(List<int> bytes) async {
  final hash = await Sha256().hash(bytes);
  return hash.bytes.map((b) => b.toRadixString(16).padLeft(2, '0')).join();
}

String _sha256HexSync(List<int> bytes) => const DartSha256()
    .hashSync(bytes)
    .bytes
    .map((b) => b.toRadixString(16).padLeft(2, '0'))
    .join();

/// Serves [bytes] for any GET, in 64 KiB pieces as a socket would, without a
/// server on this isolate whose own copying would muddy [_EventLoopGaps].
class _MemoryAdapter implements HttpClientAdapter {
  _MemoryAdapter(this.bytes);
  final Uint8List bytes;

  @override
  Future<ResponseBody> fetch(
    RequestOptions options,
    Stream<Uint8List>? requestStream,
    Future<void>? cancelFuture,
  ) async {
    const piece = 64 << 10;
    return ResponseBody(
      Stream.fromIterable([
        for (var at = 0; at < bytes.length; at += piece)
          Uint8List.sublistView(bytes, at, min(at + piece, bytes.length)),
      ]),
      200,
      headers: {
        Headers.contentLengthHeader: ['${bytes.length}'],
      },
    );
  }

  @override
  void close({bool force = false}) {}
}

/// The longest stretch the event loop went without running a 1 ms timer.
class _EventLoopGaps {
  final _clock = Stopwatch();
  Timer? _timer;
  var _last = Duration.zero, _longest = Duration.zero;

  void start() {
    _clock.start();
    _timer = Timer.periodic(const Duration(milliseconds: 1), (_) => _tick());
  }

  void _tick() {
    final now = _clock.elapsed;
    if (now - _last > _longest) _longest = now - _last;
    _last = now;
  }

  Duration stop() {
    _tick();
    _timer?.cancel();
    return _longest;
  }
}

/// Why the macOS half of this file is host-gated.
///
/// `ditto` is macOS-only, and so is the branch it stands in for: unpacking a
/// `.app` and reading its Info.plist. A Linux host — the only host that can
/// build a Linux release, so the one a Linux release is cut on — cannot build
/// that archive at all. Rather than fail the whole file there (which is what it
/// did: 21 of these, every Linux-packaging test included, died in the shared
/// `setUp`), the macOS bundle is built only on macOS, the two tests that
/// actually unpack one are skipped elsewhere, and everything that just needs
/// *some* bytes with a known size and hash gets [_fakeArchiveBytes]. The rest
/// names the branch it means (`isLinux:`) instead of inheriting the host's.
final String? _macOnly = Platform.isMacOS
    ? null
    : 'needs a macOS host: `ditto` builds the .app archive this unpacks';

/// Stand-in bytes for a host with no `ditto`.
///
/// Enough for anything that only serves the archive over HTTP and checks its
/// length or sha256 — a manifest entry, a mismatched-hash rejection. Nothing
/// unpacks it.
Future<(List<int>, String)> _fakeArchiveBytes(String version) async {
  final bytes = utf8.encode('not-a-real-bundle:$version\n' * 8);
  return (bytes, await _sha256Hex(bytes));
}

/// Builds a real, tiny `.app`-shaped bundle at [dir]/Harness.app with the given version stamped into
/// its Info.plist, zips it with the same `ditto` invocation the upload script uses, and returns
/// (zipBytes, sha256Hex).
Future<(List<int>, String)> _buildFakeBundleZip(
  Directory dir,
  String version,
) async {
  final bundle = Directory('${dir.path}/Harness.app/Contents')
    ..createSync(recursive: true);
  File('${bundle.path}/Info.plist').writeAsStringSync('''
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
	<key>CFBundleShortVersionString</key>
	<string>$version</string>
</dict>
</plist>
''');
  final zipPath = '${dir.path}/Harness-macos.zip';
  final result = await Process.run('/usr/bin/ditto', [
    '-c',
    '-k',
    '--sequesterRsrc',
    '--keepParent',
    'Harness.app',
    'Harness-macos.zip',
  ], workingDirectory: dir.path);
  expect(result.exitCode, 0, reason: 'ditto failed: ${result.stderr}');
  final bytes = File(zipPath).readAsBytesSync();
  return (bytes, await _sha256Hex(bytes));
}

/// Stand-in bytes for a Linux AppImage.
///
/// `downloadAndStage` no longer unpacks or inspects the Linux artifact — sha256 already
/// authenticates the whole single-file download — so a fake AppImage needs nothing more than
/// deterministic bytes with a known size and hash, the same idea as [_fakeArchiveBytes].
Future<(List<int>, String)> _fakeAppImageBytes(String version) async {
  final bytes = utf8.encode('not-a-real-appimage:$version\n' * 8);
  return (bytes, await _sha256Hex(bytes));
}

Uint8List _windowsZip(String version) {
  final archive = Archive();
  for (final entry in <String, String>{
    'Harness.exe': 'launcher $version',
    'Harness.ico': 'icon $version',
    'current': '$version\r\n',
    'app/$version/harness-desktop.exe': 'app $version',
  }.entries) {
    archive.addFile(ArchiveFile.bytes(entry.key, utf8.encode(entry.value)));
  }
  return ZipEncoder().encodeBytes(archive);
}

/// The fixtures here are not signed; update_signature_*_test.dart checks real
/// signatures, and these tests check what the updater does with the verdict.
Future<void> _signedByUs(
  String staged, {
  String? launcher,
  required String version,
}) async {}

void main() {
  late Directory scratch;
  HttpServer? server;
  late List<int> zipBytes;
  late String zipSha;
  const newVersion = '9.9.9';

  setUp(() async {
    scratch = await Directory.systemTemp.createTemp('desktop-updater-');
    final (bytes, sha) = Platform.isMacOS
        ? await _buildFakeBundleZip(scratch, newVersion)
        : await _fakeArchiveBytes(newVersion);
    zipBytes = bytes;
    zipSha = sha;
  });

  tearDown(() async {
    await server?.close(force: true);
    server = null;
    if (await scratch.exists()) await scratch.delete(recursive: true);
  });

  test(
    'Windows uses its own channel, native preference, and manifest precedence',
    () async {
      expect(
        DesktopUpdater(isWindows: true, releaseMode: true).canCheck,
        isTrue,
      );
      expect(
        desktopUpdateMetadataUrl(staging: true, override: ''),
        contains('/harness/staging/'),
      );
      expect(
        desktopUpdateMetadataUrl(
          staging: true,
          override: 'https://test.invalid/',
        ),
        'https://test.invalid/',
      );
      server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);
      final url = 'http://127.0.0.1:${server!.port}/metadata.json';
      server!.listen((request) async {
        request.response.headers.contentType = ContentType.json;
        request.response.write(
          jsonEncode({
            'desktop-windows-x64': {
              'version': '1.3.0',
              'url': '$url/x64',
              'sha256': 'a' * 64,
              'size': 5,
            },
            'desktop-windows-arm64': {
              'version': '1.3.0',
              'url': '$url/arm',
              'sha256': 'b' * 64,
              'size': 5,
            },
          }),
        );
        await request.response.close();
      });
      final x64 = DesktopUpdater(
        isWindows: true,
        releaseMode: true,
        architecture: 'x64',
        metadataUrl: url,
      );
      final arm = DesktopUpdater(
        isWindows: true,
        releaseMode: true,
        architecture: 'arm64',
        metadataUrl: url,
      );
      expect((await x64.checkOnce(currentVersion: '1.2.0'))?.sha256, 'a' * 64);
      expect((await arm.checkOnce(currentVersion: '1.2.0'))?.sha256, 'b' * 64);
    },
  );

  test('Windows stage carries its owned swap through apply and starts the launcher', () async {
    final root = p.join(scratch.path, 'Harness');
    Directory(p.join(root, 'app', '1.2.0')).createSync(recursive: true);
    File(p.join(root, 'app', '1.2.0', 'harness-desktop.exe'))
        .writeAsStringSync('old app');
    File(p.join(root, 'Harness.exe')).writeAsStringSync('old launcher');
    File(p.join(root, 'Harness.ico')).writeAsStringSync('old icon');
    File(p.join(root, 'current')).writeAsStringSync('1.2.0\r\n');
    final bytes = _windowsZip('1.3.0');
    final dio = Dio()..httpClientAdapter = _MemoryAdapter(bytes);
    final starts = <(String, List<String>)>[];
    final updater = DesktopUpdater(
      isWindows: true,
      releaseMode: true,
      architecture: 'x64',
      windowsInstallRoot: root,
      startDetached: (exe, args) async => starts.add((exe, args)),
      dio: dio,
      verifySignature: _signedByUs,
    );
    final staged = await updater.downloadAndStage(
      UpdateInfo(
        version: '1.3.0',
        url: 'https://fixture.invalid/update.zip',
        sha256: await _sha256Hex(bytes),
        size: bytes.length,
      ),
    );
    expect(staged, isNotNull);
    expect(File(p.join(root, 'current')).readAsStringSync(), '1.2.0\r\n');
    expect(await updater.applyStaged(staged!, selfPid: 777), isTrue);
    expect(File(p.join(root, 'current')).readAsStringSync(), '1.3.0\r\n');
    expect(starts.single.$1, p.join(root, 'Harness.exe'));
    expect(starts.single.$2, ['--wait-pid', '777']);
    expect(
      currentBundlePath(
        isWindows: true,
        executablePath: p.join(root, 'app', '1.3.0', 'harness-desktop.exe'),
      ),
      root,
    );
  });

  test('Windows refuses a package its signature check rejects and keeps the current version', () async {
    final root = p.join(scratch.path, 'Harness');
    Directory(p.join(root, 'app', '1.2.0')).createSync(recursive: true);
    File(p.join(root, 'app', '1.2.0', 'harness-desktop.exe'))
        .writeAsStringSync('old app');
    File(p.join(root, 'Harness.exe')).writeAsStringSync('old launcher');
    File(p.join(root, 'Harness.ico')).writeAsStringSync('old icon');
    File(p.join(root, 'current')).writeAsStringSync('1.2.0\r\n');
    final bytes = _windowsZip('1.3.0');
    final checked = <(String, String?)>[];
    final updater = DesktopUpdater(
      isWindows: true,
      releaseMode: true,
      windowsInstallRoot: root,
      dio: Dio()..httpClientAdapter = _MemoryAdapter(bytes),
      verifySignature: (staged, {launcher, required version}) async {
        expect(
          File(p.join(staged, 'harness-desktop.exe')).existsSync(),
          isTrue,
        );
        expect(File(launcher!).readAsStringSync(), 'launcher 1.3.0');
        // The staged app's own version is checked against the manifest's.
        expect(version, '1.3.0');
        checked.add((staged, launcher));
        throw const UpdateRefused('it is not signed by AUTONOMOUS INC.');
      },
    );
    await expectLater(
      updater.downloadAndStage(
        UpdateInfo(
          version: '1.3.0',
          url: 'https://fixture.invalid/update.zip',
          sha256: await _sha256Hex(bytes),
          size: bytes.length,
        ),
      ),
      throwsA(isA<UpdateRefused>()),
    );
    expect(checked.single.$1, endsWith('1.3.0.staging'));
    expect(Directory(checked.single.$1).existsSync(), isFalse);
    expect(File(p.join(root, 'current')).readAsStringSync(), '1.2.0\r\n');
    expect(
      File(p.join(root, 'Harness.exe')).readAsStringSync(),
      'old launcher',
    );
    expect(
      Directory(p.join(root, 'app')).listSync().map((e) => p.basename(e.path)),
      ['1.2.0'],
    );
  });

  test(
    'Windows reports partial publication when launcher start fails',
    () async {
      final root = p.join(scratch.path, 'Harness');
      Directory(p.join(root, 'app', '1.2.0')).createSync(recursive: true);
      File(p.join(root, 'app', '1.2.0', 'harness-desktop.exe'))
          .writeAsStringSync('old app');
      File(p.join(root, 'Harness.exe')).writeAsStringSync('old launcher');
      File(p.join(root, 'Harness.ico')).writeAsStringSync('old icon');
      File(p.join(root, 'current')).writeAsStringSync('1.2.0\r\n');
      final bytes = _windowsZip('1.3.0');
      final updater = DesktopUpdater(
        isWindows: true,
        releaseMode: true,
        windowsInstallRoot: root,
        startDetached: (_, _) async =>
            throw const ProcessException('Harness.exe', [], 'blocked'),
        dio: Dio()..httpClientAdapter = _MemoryAdapter(bytes),
        verifySignature: _signedByUs,
      );
      final staged = await updater.downloadAndStage(
        UpdateInfo(
          version: '1.3.0',
          url: 'https://fixture.invalid/update.zip',
          sha256: await _sha256Hex(bytes),
          size: bytes.length,
        ),
      );
      expect(staged, isNotNull);
      await expectLater(
        updater.applyStaged(staged!, selfPid: 777),
        throwsA(isA<WindowsPartialUpdateException>()),
      );
      expect(File(p.join(root, 'current')).readAsStringSync(), '1.3.0\r\n');
    },
  );

  /// Serves `/metadata.json` (the `desktop-macos` entry) and `/Harness-macos.zip` from a loopback
  /// server, and returns the manifest URL a [DesktopUpdater] should be pointed at.
  Future<String> serveMetadataAndZip({
    required String manifestVersion,
    String? shaOverride,
    int? sizeOverride,

    /// Writes the body in pieces, as a CDN does, so a progress callback has
    /// more than one number to report.
    bool chunked = false,
  }) async {
    server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);
    final base = 'http://127.0.0.1:${server!.port}';
    server!.listen((request) async {
      if (request.uri.path == '/metadata.json') {
        request.response.headers.contentType = ContentType.json;
        request.response.write(
          jsonEncode({
            'desktop-macos': {
              'version': manifestVersion,
              'url': '$base/Harness-macos.zip',
              'sha256': shaOverride ?? zipSha,
              'size': sizeOverride ?? zipBytes.length,
            },
          }),
        );
      } else if (request.uri.path == '/Harness-macos.zip') {
        if (chunked) {
          const pieces = 4;
          final step = (zipBytes.length / pieces).ceil();
          for (var at = 0; at < zipBytes.length; at += step) {
            request.response.add(
              zipBytes.sublist(at, min(at + step, zipBytes.length)),
            );
            await request.response.flush();
          }
        } else {
          request.response.add(zipBytes);
        }
      } else {
        request.response.statusCode = HttpStatus.notFound;
      }
      await request.response.close();
    });
    return '$base/metadata.json';
  }

  test(
    'checkOnce returns the entry when the manifest is strictly newer',
    () async {
      final url = await serveMetadataAndZip(manifestVersion: newVersion);
      final updater = DesktopUpdater(
        isWindows: false,
        enabled: true,
        dio: Dio(),
        isLinux: false,
        metadataUrl: url,
        releaseMode: true,
      );
      final info = await updater.checkOnce(currentVersion: '1.0.0');
      expect(info, isNotNull);
      expect(info!.version, newVersion);
      expect(info.sha256, zipSha);
      expect(info.size, zipBytes.length);
    },
  );

  test('checkOnce never reports an update outside release mode (debug/profile builds)', () async {
    final url = await serveMetadataAndZip(manifestVersion: newVersion);
    // No releaseMode override — defaults to kReleaseMode, which is false under `flutter test`.
    final updater = DesktopUpdater(
      isWindows: false,
      enabled: true,
      dio: Dio(),
      isLinux: false,
      metadataUrl: url,
    );
    expect(await updater.checkOnce(currentVersion: '1.0.0'), isNull);

    final explicitlyOff = DesktopUpdater(
      isWindows: false,
      enabled: true,
      dio: Dio(),
      isLinux: false,
      metadataUrl: url,
      releaseMode: false,
    );
    expect(await explicitlyOff.checkOnce(currentVersion: '1.0.0'), isNull);
  });

  test('a disabled updater skips checks, downloads and applying even in release mode', () async {
    final updater = DesktopUpdater(
      isWindows: false,
      enabled: false,
      releaseMode: true,
    );
    const info = UpdateInfo(
      version: '9.9.9',
      url: 'https://fixture.invalid/update.zip',
      sha256: 'unused',
      size: 1,
    );
    expect(await updater.checkOnce(currentVersion: '1.0.0'), isNull);
    expect(await updater.downloadAndStage(info), isNull);
    expect(
      await updater.applyStaged(
        const StagedUpdate(
          version: '9.9.9',
          bundlePath: '/nonexistent/fixture.app',
          stagingDirPath: '/nonexistent',
        ),
        selfPid: 1,
      ),
      isFalse,
    );
  });

  test('a caller that names the host with isLinux describes macOS or Linux, not Windows', () {
    expect(
      DesktopUpdater(
        isWindows: false,
        enabled: true,
        releaseMode: true,
        isLinux: false,
      ).canCheck,
      isTrue,
    );
  });

  test('checkOnce returns null when the running version is already current or newer', () async {
    final url = await serveMetadataAndZip(manifestVersion: '1.0.0');
    final updater = DesktopUpdater(
      isWindows: false,
      enabled: true,
      dio: Dio(),
      isLinux: false,
      metadataUrl: url,
      releaseMode: true,
    );
    expect(await updater.checkOnce(currentVersion: '1.0.0'), isNull);
    expect(await updater.checkOnce(currentVersion: '2.0.0'), isNull);
  });

  test(
    'checkOnce returns null (not an error) when the manifest is unreachable',
    () async {
      final updater = DesktopUpdater(
        isWindows: false,
        enabled: true,
        dio: Dio(),
        isLinux: false,
        metadataUrl: 'http://127.0.0.1:1/metadata.json', // nothing listens here
        releaseMode: true,
      );
      expect(await updater.checkOnce(currentVersion: '1.0.0'), isNull);
    },
  );

  test('checkOnce also treats unavailable package metadata as no update', () async {
    final updater = DesktopUpdater(
      isWindows: false,
      enabled: true,
      dio: Dio(),
      isLinux: false,
      metadataUrl: 'http://127.0.0.1:1/metadata.json',
      releaseMode: true,
    );
    // Widget/unit tests have no package_info platform channel. The startup
    // checker must not leave an unhandled asynchronous exception in that case.
    expect(await updater.checkOnce(), isNull);
  });

  test('semverGt is strict and ignores prerelease/build metadata', () {
    expect(semverGt('1.2.4', '1.2.3'), isTrue);
    expect(semverGt('2.0.0', '1.9.9'), isTrue);
    expect(semverGt('1.2.3', '1.2.3'), isFalse);
    expect(semverGt('1.2.3', '1.2.4'), isFalse);
    expect(semverGt('1.2.3-beta', '1.2.3'), isFalse);
    expect(semverGt('not-a-version', '1.0.0'), isFalse);
  });

  test(
    'startChecking calls onUpdateAvailable only when a newer build exists',
    () async {
      final urlUpToDate = await serveMetadataAndZip(manifestVersion: '1.0.0');
      final noUpdates = <UpdateInfo>[];
      final t1 =
          DesktopUpdater(
            isWindows: false,
            enabled: true,
            dio: Dio(),
            isLinux: false,
            metadataUrl: urlUpToDate,
            releaseMode: true,
          ).startChecking(
            interval: const Duration(days: 1),
            currentVersion: '1.0.0',
            onUpdateAvailable: noUpdates.add,
          );
      addTearDown(t1.cancel);
      await Future<void>.delayed(const Duration(milliseconds: 100));
      expect(noUpdates, isEmpty);

      await server?.close(force: true);
      server = null;
      final urlNewer = await serveMetadataAndZip(manifestVersion: newVersion);
      final found = <UpdateInfo>[];
      final t2 =
          DesktopUpdater(
            isWindows: false,
            enabled: true,
            dio: Dio(),
            isLinux: false,
            metadataUrl: urlNewer,
            releaseMode: true,
          ).startChecking(
            interval: const Duration(days: 1),
            currentVersion: '1.0.0',
            onUpdateAvailable: found.add,
          );
      addTearDown(t2.cancel);
      await Future<void>.delayed(const Duration(milliseconds: 100));
      expect(found, hasLength(1));
      expect(found.single.version, newVersion);
    },
  );

  // What the banner counts. The manifest's size is the denominator, so the
  // fraction is right even from a server that sends no Content-Length.
  test('downloadAndStage reports the bytes as they arrive', () async {
    await serveMetadataAndZip(manifestVersion: newVersion, chunked: true);
    final updater = DesktopUpdater(
      isWindows: false,
      enabled: true,
      dio: Dio(),
      isLinux: false,
      verifySignature: _signedByUs,
    );
    final info = UpdateInfo(
      version: newVersion,
      url: 'http://127.0.0.1:${server!.port}/Harness-macos.zip',
      sha256: zipSha,
      size: zipBytes.length,
    );
    final seen = <(int, int)>[];
    final staged = await updater.downloadAndStage(
      info,
      onProgress: (received, total) => seen.add((received, total)),
    );
    expect(staged, isNotNull, reason: 'the download itself must still verify');
    expect(seen, isNotEmpty);
    expect(seen.every((row) => row.$2 == zipBytes.length), isTrue);
    expect(
      seen.map((row) => row.$1).toList(),
      orderedEquals(seen.map((row) => row.$1).toList()..sort()),
    );
    expect(seen.last.$1, zipBytes.length);
  }, skip: _macOnly);

  test(
    'downloadAndStage verifies sha256 before trusting the download',
    () async {
      await serveMetadataAndZip(manifestVersion: newVersion);
      final updater = DesktopUpdater(
        isWindows: false,
        enabled: true,
        dio: Dio(),
        isLinux: false,
      );
      final badInfo = UpdateInfo(
        version: newVersion,
        url: 'http://127.0.0.1:${server!.port}/Harness-macos.zip',
        sha256: '0' * 64,
        size: zipBytes.length,
      );
      final staged = await updater.downloadAndStage(badInfo);
      expect(staged, isNull);
    },
  );

  test('downloadAndStage unpacks and confirms the staged bundle really carries the advertised version', () async {
    await serveMetadataAndZip(manifestVersion: newVersion);
    final updater = DesktopUpdater(
      isWindows: false,
      enabled: true,
      dio: Dio(),
      isLinux: false,
      verifySignature: _signedByUs,
    );
    final info = UpdateInfo(
      version: newVersion,
      url: 'http://127.0.0.1:${server!.port}/Harness-macos.zip',
      sha256: zipSha,
      size: zipBytes.length,
    );
    final staged = await updater.downloadAndStage(info);
    expect(staged, isNotNull);
    expect(staged!.version, newVersion);
    expect(Directory(staged.bundlePath).existsSync(), isTrue);
    await Directory(staged.stagingDirPath).delete(recursive: true);
  }, skip: _macOnly);

  test('downloadAndStage refuses a bundle its signature check rejects, and removes it', () async {
    await serveMetadataAndZip(manifestVersion: newVersion);
    final checked = <String>[];
    final updater = DesktopUpdater(
      isWindows: false,
      enabled: true,
      dio: Dio(),
      isLinux: false,
      verifySignature: (staged, {launcher, required version}) async {
        expect(File('$staged/Contents/Info.plist').existsSync(), isTrue);
        checked.add(staged);
        throw const UpdateRefused('it is not signed by Autonomous Inc.');
      },
    );
    await expectLater(
      updater.downloadAndStage(
        UpdateInfo(
          version: newVersion,
          url: 'http://127.0.0.1:${server!.port}/Harness-macos.zip',
          sha256: zipSha,
          size: zipBytes.length,
        ),
      ),
      throwsA(
        isA<UpdateRefused>().having(
          (e) => e.message,
          'message',
          'it is not signed by Autonomous Inc.',
        ),
      ),
    );
    expect(checked.single, endsWith('Harness.app'));
    expect(Directory(p.dirname(checked.single)).existsSync(), isFalse);
  }, skip: _macOnly);

  test('downloadAndStage rejects a bundle whose Info.plist does not match the advertised version', () async {
    await serveMetadataAndZip(manifestVersion: newVersion);
    final updater = DesktopUpdater(
      isWindows: false,
      enabled: true,
      dio: Dio(),
      isLinux: false,
    );
    // Real zip on disk is stamped $newVersion — advertise a different one.
    final mismatched = UpdateInfo(
      version: '1.2.3',
      url: 'http://127.0.0.1:${server!.port}/Harness-macos.zip',
      sha256: zipSha,
      size: zipBytes.length,
    );
    final staged = await updater.downloadAndStage(mismatched);
    expect(staged, isNull);
  }, skip: _macOnly);

  test('applyStaged spawns a detached command and never launches a real process', () async {
    final calls = <String>[];
    final updater = DesktopUpdater(
      isWindows: false,
      enabled: true,
      // The macOS relaunch, asked for by name rather than inherited from the
      // host — the Linux one is the group at the bottom of this file, and both
      // deserve to run wherever the suite does.
      isLinux: false,
      launchDetached: (command) async => calls.add(command),
    );
    final staged = StagedUpdate(
      version: newVersion,
      bundlePath: '${scratch.path}/staged/Harness.app',
      stagingDirPath: '${scratch.path}/staged',
    );
    final ok = await updater.applyStaged(
      staged,
      selfPid: 12345,
      runningBundlePath: '/tmp/does-not-exist/Harness.app',
    );
    expect(ok, isTrue);
    expect(calls, hasLength(1));
    expect(calls.single, contains('kill -0 12345'));
    expect(calls.single, contains('/tmp/does-not-exist/Harness.app'));
    expect(calls.single, contains(staged.bundlePath));
    expect(calls.single, contains('open -n'));
    expect(calls.single, contains('pgrep -f'));
  });

  test(
    'applyStaged does nothing when the running bundle path cannot be resolved',
    () async {
      var called = false;
      final updater = DesktopUpdater(
        isWindows: false,
        enabled: true,
        // Same as above: the macOS "not inside a .app" branch. On Linux every
        // executable has a parent directory, so the host's own answer would
        // never be the null this is about.
        isLinux: false,
        launchDetached: (command) async => called = true,
      );
      final staged = StagedUpdate(
        version: newVersion,
        bundlePath: '${scratch.path}/staged/Harness.app',
        stagingDirPath: '${scratch.path}/staged',
      );
      // The test runner's own executable is not inside a `.app` bundle, and no override is given, so
      // currentBundlePath(isWindows: false, ) resolves to null — the same "cannot resolve" branch a real, non-.app-hosted
      // process (e.g. running via `flutter test`) would hit.
      final ok = await updater.applyStaged(staged, selfPid: 1);
      expect(ok, isFalse);
      expect(called, isFalse);
    },
  );

  test('currentBundlePath walks up to the enclosing .app', () {
    expect(
      currentBundlePath(
        isWindows: false,
        executablePath: '/Applications/Harness.app/Contents/MacOS/Harness',
        isLinux: false, // the .app walk, on whatever host runs the suite
      ),
      '/Applications/Harness.app',
    );
    expect(
      currentBundlePath(
        isWindows: false,
        executablePath: '/usr/local/bin/some-tool',
        isLinux: false,
      ),
      isNull,
    );
  });

  test(
    'currentBundlePath on Linux resolves to the given AppImage override',
    () {
      expect(
        currentBundlePath(
          isWindows: false,
          isLinux: true,
          appImagePath: '/home/user/.local/opt/Harness.AppImage',
        ),
        '/home/user/.local/opt/Harness.AppImage',
      );
    },
  );

  test('currentBundlePath on Linux is null with no override and no APPIMAGE env var', () {
    // Production falls through to Platform.environment['APPIMAGE'], which is unset for the test
    // runner's own process — the same "cannot resolve" state a non-packaged dev run would hit.
    expect(currentBundlePath(isWindows: false, isLinux: true), isNull);
  });

  group('macOS picks its build by CPU', () {
    /// Serves a manifest holding exactly [versions] (key → version). Every entry points at an archive
    /// named after its key, because which KEY was chosen is the whole question here.
    Future<String> serveMacManifest(Map<String, String> versions) async {
      server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);
      final base = 'http://127.0.0.1:${server!.port}';
      server!.listen((request) async {
        if (request.uri.path == '/metadata.json') {
          request.response.headers.contentType = ContentType.json;
          request.response.write(
            jsonEncode({
              for (final entry in versions.entries)
                entry.key: {
                  'version': entry.value,
                  'url': '$base/${entry.key}.zip',
                  'sha256': zipSha,
                  'size': zipBytes.length,
                },
            }),
          );
        } else {
          request.response.statusCode = HttpStatus.notFound;
        }
        await request.response.close();
      });
      return '$base/metadata.json';
    }

    Future<UpdateInfo?> checkAs(
      String architecture,
      Map<String, String> versions,
    ) async {
      final updater = DesktopUpdater(
        isWindows: false,
        enabled: true,
        dio: Dio(),
        metadataUrl: await serveMacManifest(versions),
        releaseMode: true,
        isLinux: false,
        architecture: architecture,
      );
      return updater.checkOnce(currentVersion: '1.0.0');
    }

    test(
      'Apple Silicon takes its Impeller build when both are published',
      () async {
        final info = await checkAs('arm64', {
          'desktop-macos': '1.2.0',
          'desktop-macos-arm64': '1.2.0',
        });
        expect(info!.url, endsWith('/desktop-macos-arm64.zip'));
      },
    );

    test(
      'an Intel Mac never takes the Apple Silicon build, however new',
      () async {
        final info = await checkAs('x64', {
          'desktop-macos': '1.2.0',
          'desktop-macos-arm64': '1.3.0',
        });
        expect(info!.version, '1.2.0');
        expect(info.url, endsWith('/desktop-macos.zip'));
      },
    );

    test('an Intel Mac has nothing to install when only the Apple Silicon build moved', () async {
      expect(await checkAs('x64', {'desktop-macos-arm64': '1.3.0'}), isNull);
    });

    test('Apple Silicon falls back to desktop-macos on a manifest from before the split', () async {
      final info = await checkAs('arm64', {'desktop-macos': '1.2.0'});
      expect(info!.url, endsWith('/desktop-macos.zip'));
    });

    test(
      'Apple Silicon follows a newer desktop-macos over an older arm64 entry',
      () async {
        final info = await checkAs('arm64', {
          'desktop-macos': '1.3.0',
          'desktop-macos-arm64': '1.2.0',
        });
        expect(info!.version, '1.3.0');
        expect(info.url, endsWith('/desktop-macos.zip'));
      },
    );
  });

  group('Linux architecture packaging', () {
    late List<int> appImageBytes;
    late String appImageSha;

    setUp(() async {
      final (bytes, sha) = await _fakeAppImageBytes(newVersion);
      appImageBytes = bytes;
      appImageSha = sha;
    });

    Future<String> serveLinuxMetadataAndAppImage({
      required String manifestVersion,
      String architecture = 'x64',
      String? shaOverride,
    }) async {
      server = await HttpServer.bind(InternetAddress.loopbackIPv4, 0);
      final base = 'http://127.0.0.1:${server!.port}';
      final archive = 'Harness-linux-$architecture.AppImage';
      server!.listen((request) async {
        if (request.uri.path == '/metadata.json') {
          request.response.headers.contentType = ContentType.json;
          request.response.write(
            jsonEncode({
              'desktop-linux-$architecture': {
                'version': manifestVersion,
                'url': '$base/$archive',
                'sha256': shaOverride ?? appImageSha,
                'size': appImageBytes.length,
              },
            }),
          );
        } else if (request.uri.path == '/$archive') {
          request.response.add(appImageBytes);
        } else {
          request.response.statusCode = HttpStatus.notFound;
        }
        await request.response.close();
      });
      return '$base/metadata.json';
    }

    test('checkOnce reads the desktop-linux-x64 manifest entry', () async {
      final url = await serveLinuxMetadataAndAppImage(
        manifestVersion: newVersion,
      );
      final updater = DesktopUpdater(
        isWindows: false,
        enabled: true,
        dio: Dio(),
        metadataUrl: url,
        releaseMode: true,
        isLinux: true,
        architecture: 'x64',
      );
      final info = await updater.checkOnce(currentVersion: '1.0.0');
      expect(info, isNotNull);
      expect(info!.version, newVersion);
      expect(info.sha256, appImageSha);
    });

    test('checkOnce reads the desktop-linux-arm64 manifest entry', () async {
      final url = await serveLinuxMetadataAndAppImage(
        manifestVersion: newVersion,
        architecture: 'arm64',
      );
      final updater = DesktopUpdater(
        isWindows: false,
        enabled: true,
        dio: Dio(),
        metadataUrl: url,
        releaseMode: true,
        isLinux: true,
        architecture: 'arm64',
      );
      final info = await updater.checkOnce(currentVersion: '1.0.0');
      expect(info, isNotNull);
      expect(info!.url, endsWith('/Harness-linux-arm64.AppImage'));
      expect(info.sha256, appImageSha);
    });

    test(
      'downloadAndStage stages the AppImage as-is and makes it executable',
      () async {
        await serveLinuxMetadataAndAppImage(manifestVersion: newVersion);
        final updater = DesktopUpdater(
          isWindows: false,
          enabled: true,
          dio: Dio(),
          isLinux: true,
          architecture: 'x64',
        );
        final info = UpdateInfo(
          version: newVersion,
          url: 'http://127.0.0.1:${server!.port}/Harness-linux-x64.AppImage',
          sha256: appImageSha,
          size: appImageBytes.length,
        );
        final staged = await updater.downloadAndStage(info);
        expect(staged, isNotNull);
        expect(staged!.version, newVersion);
        expect(staged.bundlePath, endsWith('/Harness-linux-x64.AppImage'));
        expect(File(staged.bundlePath).existsSync(), isTrue);
        final mode = File(staged.bundlePath).statSync().modeString();
        expect(mode, contains('x'), reason: 'staged AppImage should be +x');
        await Directory(staged.stagingDirPath).delete(recursive: true);
      },
    );

    test(
      'downloadAndStage rejects a Linux download whose sha256 does not match',
      () async {
        await serveLinuxMetadataAndAppImage(manifestVersion: newVersion);
        final updater = DesktopUpdater(
          isWindows: false,
          enabled: true,
          dio: Dio(),
          isLinux: true,
          architecture: 'x64',
        );
        final badInfo = UpdateInfo(
          version: newVersion,
          url: 'http://127.0.0.1:${server!.port}/Harness-linux-x64.AppImage',
          sha256: '0' * 64,
          size: appImageBytes.length,
        );
        final staged = await updater.downloadAndStage(badInfo);
        expect(staged, isNull);
      },
    );

    // A release is ~45 MB. Its hash ran synchronously on the UI isolate
    // (535 ms for 45 MiB in an AOT build), so nothing else — no frame, no
    // timer — ran until it finished. The same hash timed here, twice so the
    // second is warm, is the yardstick: the longest the event loop goes
    // unserved while the download is verified must be well under it (the old
    // code measured 0.96 of it; this one ~0.13; 0.75 leaves room for a busy CI
    // runner slowing both alike).
    test('downloadAndStage verifies a large download off the UI isolate', () async {
      final bytes = Uint8List(24 << 20);
      for (var i = 0; i < bytes.length; i += 4093) {
        bytes[i] = i & 0xff;
      }
      var onThisIsolate = const Duration(days: 1);
      late String expected;
      for (var round = 0; round < 2; round++) {
        final timing = Stopwatch()..start();
        expected = _sha256HexSync(bytes);
        if (timing.elapsed < onThisIsolate) onThisIsolate = timing.elapsed;
      }
      final dio = Dio()..httpClientAdapter = _MemoryAdapter(bytes);
      final updater = DesktopUpdater(
        isWindows: false,
        enabled: true,
        dio: dio,
        isLinux: true,
        architecture: 'x64',
      );
      final gaps = _EventLoopGaps()..start();
      final staged = await updater.downloadAndStage(
        UpdateInfo(
          version: newVersion,
          url: 'https://fixture.invalid/Harness-linux-x64.AppImage',
          sha256: expected,
          size: bytes.length,
        ),
      );
      final longest = gaps.stop();
      expect(staged, isNotNull, reason: 'the digest must still match');
      addTearDown(
        () => Directory(staged!.stagingDirPath).delete(recursive: true),
      );
      expect(File(staged!.bundlePath).lengthSync(), bytes.length);
      expect(
        longest,
        lessThan(onThisIsolate * 0.75),
        reason:
            'the UI isolate went ${longest.inMilliseconds} ms without an event; '
            'hashing these bytes here takes ${onThisIsolate.inMilliseconds} ms',
      );
    });

    test('the background digest is the sha256 the manifest carries', () async {
      expect(
        await sha256HexOffUiIsolate(utf8.encode('abc')),
        'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad',
      );
      expect(await sha256HexOffUiIsolate(appImageBytes), appImageSha);
    });

    test('applyStaged on Linux execs the swapped AppImage file directly instead of `open -n`', () async {
      final calls = <String>[];
      final updater = DesktopUpdater(
        isWindows: false,
        enabled: true,
        isLinux: true,
        architecture: 'x64',
        launchDetached: (command) async => calls.add(command),
      );
      final staged = StagedUpdate(
        version: newVersion,
        bundlePath: '${scratch.path}/staged/Harness-linux-x64.AppImage',
        stagingDirPath: '${scratch.path}/staged',
      );
      final ok = await updater.applyStaged(
        staged,
        selfPid: 12345,
        runningBundlePath: '/home/user/.local/opt/Harness.AppImage',
      );
      expect(ok, isTrue);
      expect(calls, hasLength(1));
      expect(calls.single, contains('kill -0 12345'));
      expect(calls.single, contains('/home/user/.local/opt/Harness.AppImage'));
      expect(calls.single, contains(staged.bundlePath));
      expect(calls.single, contains('nohup'));
      expect(calls.single, isNot(contains('open -n')));
      expect(calls.single, contains('pgrep -f'));
    });
  });
}