|
| 1 | +using OpenClaw.Connection.LocalAi; |
| 2 | +using OpenClawTray.Presentation; |
| 3 | + |
| 4 | +namespace OpenClaw.Tray.Tests.Presentation; |
| 5 | + |
| 6 | +public sealed class LocalAiPageViewModelTests |
| 7 | +{ |
| 8 | + [Theory] |
| 9 | + [InlineData(LocalAiModelAvailabilityState.Verified)] |
| 10 | + [InlineData(LocalAiModelAvailabilityState.Loaded)] |
| 11 | + public void InstalledModel_OffersChangeModelThroughExistingSetupRoute( |
| 12 | + LocalAiModelAvailabilityState modelState) |
| 13 | + { |
| 14 | + using var harness = new LocalAiHarness(modelState); |
| 15 | + |
| 16 | + Assert.True(harness.ViewModel.CanChangeModel); |
| 17 | + Assert.True(harness.ViewModel.HasInstalledModel); |
| 18 | + Assert.False(harness.ViewModel.CanRetrySetup); |
| 19 | + |
| 20 | + Assert.True(harness.ViewModel.ChangeModel()); |
| 21 | + |
| 22 | + Assert.Equal(0, harness.Commands.ShowGatewayWizardCount); |
| 23 | + Assert.Equal(1, harness.Commands.ShowOnboardingCount); |
| 24 | + } |
| 25 | + |
| 26 | + [Theory] |
| 27 | + [InlineData(LocalAiModelAvailabilityState.Unknown)] |
| 28 | + [InlineData(LocalAiModelAvailabilityState.NotInstalled)] |
| 29 | + public void MissingModel_KeepsRetrySetupAndDoesNotOfferChangeModel( |
| 30 | + LocalAiModelAvailabilityState modelState) |
| 31 | + { |
| 32 | + using var harness = new LocalAiHarness(modelState); |
| 33 | + |
| 34 | + Assert.False(harness.ViewModel.CanChangeModel); |
| 35 | + Assert.False(harness.ViewModel.HasInstalledModel); |
| 36 | + Assert.True(harness.ViewModel.CanRetrySetup); |
| 37 | + |
| 38 | + Assert.False(harness.ViewModel.ChangeModel()); |
| 39 | + Assert.True(harness.ViewModel.RetrySetup()); |
| 40 | + |
| 41 | + Assert.Equal(0, harness.Commands.ShowGatewayWizardCount); |
| 42 | + Assert.Equal(1, harness.Commands.ShowOnboardingCount); |
| 43 | + } |
| 44 | + |
| 45 | + [Fact] |
| 46 | + public async Task InstalledModel_RemainsVisibleButCannotOpenSetupDuringRuntimeAction() |
| 47 | + { |
| 48 | + using var harness = new LocalAiHarness( |
| 49 | + LocalAiModelAvailabilityState.Verified, |
| 50 | + LocalAiRuntimeState.Stopped); |
| 51 | + harness.Runtime.BlockStart(); |
| 52 | + |
| 53 | + Task<bool> startTask = harness.ViewModel.StartAsync(); |
| 54 | + |
| 55 | + Assert.True(harness.ViewModel.HasInstalledModel); |
| 56 | + Assert.False(harness.ViewModel.CanChangeModel); |
| 57 | + Assert.False(harness.ViewModel.ChangeModel()); |
| 58 | + Assert.Equal(0, harness.Commands.ShowOnboardingCount); |
| 59 | + |
| 60 | + harness.Runtime.CompleteStart(); |
| 61 | + Assert.True(await startTask); |
| 62 | + } |
| 63 | + |
| 64 | + [Fact] |
| 65 | + public void LocalAiPage_ChangeModelActionIsLocalizedAccessibleAndWired() |
| 66 | + { |
| 67 | + string root = TestRepositoryPaths.GetRepositoryRoot(); |
| 68 | + string pageDirectory = Path.Combine(root, "src", "OpenClaw.Tray.WinUI", "Pages"); |
| 69 | + string xaml = File.ReadAllText(Path.Combine(pageDirectory, "LocalAiPage.xaml")); |
| 70 | + string codeBehind = File.ReadAllText(Path.Combine(pageDirectory, "LocalAiPage.xaml.cs")); |
| 71 | + |
| 72 | + Assert.Contains("x:Uid=\"LocalAiPage_ChangeModelButton\"", xaml); |
| 73 | + Assert.Contains("x:Uid=\"LocalAiPage_ChangeModelDescription\"", xaml); |
| 74 | + Assert.Contains("AutomationProperties.AutomationId=\"LocalAiChangeModel\"", xaml); |
| 75 | + Assert.Contains("Click=\"OnChangeModel\"", xaml); |
| 76 | + Assert.Contains("_viewModel?.ChangeModel()", codeBehind); |
| 77 | + } |
| 78 | + |
| 79 | + private sealed class LocalAiHarness : IDisposable |
| 80 | + { |
| 81 | + private readonly FakeLocalAiRuntime _runtime; |
| 82 | + private readonly PermissionsPageRuntimeSource _gatewaySource; |
| 83 | + private readonly RecordingUiDispatcher _dispatcher; |
| 84 | + |
| 85 | + public LocalAiHarness( |
| 86 | + LocalAiModelAvailabilityState modelState, |
| 87 | + LocalAiRuntimeState runtimeState = LocalAiRuntimeState.Healthy) |
| 88 | + { |
| 89 | + _runtime = new FakeLocalAiRuntime(CreateSnapshot(modelState, runtimeState)); |
| 90 | + var gatewayHost = new FakePermissionsPageRuntimeHost(); |
| 91 | + _gatewaySource = new PermissionsPageRuntimeSource(gatewayHost); |
| 92 | + Commands = new FakeAppCommands(); |
| 93 | + _dispatcher = new RecordingUiDispatcher(); |
| 94 | + ViewModel = new LocalAiPageViewModel(_runtime, _gatewaySource, Commands, _dispatcher); |
| 95 | + } |
| 96 | + |
| 97 | + public FakeAppCommands Commands { get; } |
| 98 | + public FakeLocalAiRuntime Runtime => _runtime; |
| 99 | + public LocalAiPageViewModel ViewModel { get; } |
| 100 | + |
| 101 | + public void Dispose() |
| 102 | + { |
| 103 | + ViewModel.Dispose(); |
| 104 | + Commands.Dispose(); |
| 105 | + _gatewaySource.Dispose(); |
| 106 | + _dispatcher.Dispose(); |
| 107 | + _runtime.DisposeAsync().AsTask().GetAwaiter().GetResult(); |
| 108 | + } |
| 109 | + |
| 110 | + private static LocalAiRuntimeSnapshot CreateSnapshot( |
| 111 | + LocalAiModelAvailabilityState modelState, |
| 112 | + LocalAiRuntimeState runtimeState) |
| 113 | + { |
| 114 | + DateTimeOffset now = DateTimeOffset.UtcNow; |
| 115 | + LocalAiModelEvidence evidence = modelState switch |
| 116 | + { |
| 117 | + LocalAiModelAvailabilityState.Verified => new( |
| 118 | + modelState, |
| 119 | + now, |
| 120 | + new string('a', 64), |
| 121 | + 1024), |
| 122 | + LocalAiModelAvailabilityState.Loaded => new( |
| 123 | + modelState, |
| 124 | + now, |
| 125 | + new string('a', 64), |
| 126 | + 1024, |
| 127 | + "test-model"), |
| 128 | + LocalAiModelAvailabilityState.NotInstalled => LocalAiModelEvidence.NotInstalled(now), |
| 129 | + _ => LocalAiModelEvidence.Unknown(now), |
| 130 | + }; |
| 131 | + |
| 132 | + return new LocalAiRuntimeSnapshot( |
| 133 | + modelState is LocalAiModelAvailabilityState.Verified or LocalAiModelAvailabilityState.Loaded |
| 134 | + ? runtimeState |
| 135 | + : LocalAiRuntimeState.NotInstalled, |
| 136 | + modelState is LocalAiModelAvailabilityState.Verified or LocalAiModelAvailabilityState.Loaded |
| 137 | + ? LocalAiOwnership.CompanionManaged |
| 138 | + : LocalAiOwnership.None, |
| 139 | + new Uri("http://127.0.0.1:11983"), |
| 140 | + "test", |
| 141 | + "test-model", |
| 142 | + evidence, |
| 143 | + null, |
| 144 | + null, |
| 145 | + null, |
| 146 | + now); |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + internal sealed class FakeLocalAiRuntime(LocalAiRuntimeSnapshot snapshot) : ILocalAiRuntime |
| 151 | + { |
| 152 | + private TaskCompletionSource<LocalAiRuntimeSnapshot>? _startCompletion; |
| 153 | + |
| 154 | + public LocalAiRuntimeSnapshot Snapshot { get; } = snapshot; |
| 155 | + |
| 156 | + public event EventHandler<LocalAiRuntimeSnapshotChangedEventArgs>? StateChanged |
| 157 | + { |
| 158 | + add { } |
| 159 | + remove { } |
| 160 | + } |
| 161 | + |
| 162 | + public void BlockStart() => |
| 163 | + _startCompletion = new(TaskCreationOptions.RunContinuationsAsynchronously); |
| 164 | + |
| 165 | + public void CompleteStart() => _startCompletion?.TrySetResult(Snapshot); |
| 166 | + |
| 167 | + public Task<LocalAiRuntimeSnapshot> EnsureStartedAsync(CancellationToken cancellationToken = default) => |
| 168 | + _startCompletion?.Task ?? Task.FromResult(Snapshot); |
| 169 | + |
| 170 | + public Task<LocalAiRuntimeSnapshot> StopAsync(CancellationToken cancellationToken = default) => |
| 171 | + Task.FromResult(Snapshot); |
| 172 | + |
| 173 | + public Task<LocalAiRuntimeSnapshot> RestartAsync(CancellationToken cancellationToken = default) => |
| 174 | + Task.FromResult(Snapshot); |
| 175 | + |
| 176 | + public Task<LocalAiRuntimeSnapshot> RefreshAsync(CancellationToken cancellationToken = default) => |
| 177 | + Task.FromResult(Snapshot); |
| 178 | + |
| 179 | + public ValueTask DisposeAsync() => ValueTask.CompletedTask; |
| 180 | + } |
| 181 | +} |
0 commit comments