using System; using System.Diagnostics; using System.IO; using ClaudeDo.Ui.Localization; using ClaudeDo.Ui.Services; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; namespace ClaudeDo.Ui.ViewModels.Modals; public sealed partial class WorkerConnectionModalViewModel : ViewModelBase { private readonly WorkerLocator _workerLocator; private readonly InstallerLocator _installerLocator; // Test seam: real Process.Start by default, swappable to simulate a blocked/failed launch. internal Action ProcessStarter { get; set; } = psi => Process.Start(psi); public WorkerConnectionModalViewModel(WorkerLocator workerLocator, InstallerLocator installerLocator) { _workerLocator = workerLocator; _installerLocator = installerLocator; } public Action? CloseAction { get; set; } [ObservableProperty] private string? _errorText; [RelayCommand] private void Close() => CloseAction?.Invoke(); [RelayCommand] private void StartWorker() { var exe = _workerLocator.Find(); if (exe is null) { ErrorText = Loc.T("modals.workerConnection.workerNotFound"); return; } try { ProcessStarter(new ProcessStartInfo(exe) { UseShellExecute = true }); } catch (Exception ex) { ErrorText = Loc.T("modals.workerConnection.startFailed", ex.Message); return; } ErrorText = null; CloseAction?.Invoke(); } [RelayCommand] private void RerunInstaller() { var path = _installerLocator.Find(); if (path is null) { ErrorText = Loc.T("modals.workerConnection.installerNotFound"); return; } try { // See IslandsShellViewModel.UpdateNow: an inherited CWD inside the install dir // makes the installer block its own app\ rename. ProcessStarter(new ProcessStartInfo(path) { UseShellExecute = true, WorkingDirectory = Path.GetTempPath(), }); } catch (Exception ex) { ErrorText = Loc.T("modals.workerConnection.startFailed", ex.Message); return; } Environment.Exit(0); } }