| 1 | using System.Collections.Generic; |
| 2 | using CascadeIDE.Cockpit.ComputingUnits.IdeHealth; |
| 3 | using CascadeIDE.Cockpit.DataBus; |
| 4 | using CascadeIDE.Services; |
| 5 | using Xunit; |
| 6 | |
| 7 | namespace CascadeIDE.Tests; |
| 8 | |
| 9 | public sealed class IdeHealthSnapshotUnitTests |
| 10 | { |
| 11 | [Fact] |
| 12 | public void Build_uses_solution_scope_when_startup_project_has_no_active_signal() |
| 13 | { |
| 14 | var bus = new InMemoryDataBus(); |
| 15 | var unit = new IdeHealthSnapshotUnit(bus); |
| 16 | bus.Publish(new StartupProjectPathChanged("src/App/App.csproj")); |
| 17 | bus.Publish(new GitStateChanged("Git: clean", "main")); |
| 18 | |
| 19 | var snapshot = unit.Build(default); |
| 20 | |
| 21 | Assert.Equal(IdeHealthScope.Solution, snapshot.Solution.Build.Scope); |
| 22 | Assert.Equal(IdeHealthScope.Solution, snapshot.Solution.Tests.Scope); |
| 23 | Assert.Equal(IdeHealthScope.Solution, snapshot.Solution.Debug.Scope); |
| 24 | Assert.Null(snapshot.Solution.Build.ProjectPath); |
| 25 | Assert.Null(snapshot.Solution.Tests.ProjectPath); |
| 26 | Assert.Null(snapshot.Solution.Debug.ProjectPath); |
| 27 | } |
| 28 | |
| 29 | [Fact] |
| 30 | public void Build_uses_project_scope_when_solution_signal_is_active() |
| 31 | { |
| 32 | var bus = new InMemoryDataBus(); |
| 33 | var unit = new IdeHealthSnapshotUnit(bus); |
| 34 | bus.Publish(new StartupProjectPathChanged("src/App/App.csproj")); |
| 35 | bus.Publish(new GitStateChanged("Git: clean", "main")); |
| 36 | bus.Publish(new BuildStateChanged(true)); |
| 37 | |
| 38 | var snapshot = unit.Build(default); |
| 39 | |
| 40 | Assert.Equal(IdeHealthScope.Project, snapshot.Solution.Build.Scope); |
| 41 | Assert.Equal(IdeHealthScope.Project, snapshot.Solution.Tests.Scope); |
| 42 | Assert.Equal(IdeHealthScope.Project, snapshot.Solution.Debug.Scope); |
| 43 | Assert.Equal("src/App/App.csproj", snapshot.Solution.Build.ProjectPath); |
| 44 | Assert.Equal("src/App/App.csproj", snapshot.Solution.Tests.ProjectPath); |
| 45 | Assert.Equal("src/App/App.csproj", snapshot.Solution.Debug.ProjectPath); |
| 46 | } |
| 47 | |
| 48 | [Fact] |
| 49 | public void Build_reflects_IdeHostStateChanged_for_Lsp_hint() |
| 50 | { |
| 51 | var bus = new InMemoryDataBus(asynchronousDispatch: false); |
| 52 | var unit = new IdeHealthSnapshotUnit(bus); |
| 53 | bus.Publish(new GitStateChanged("Git: clean", "main")); |
| 54 | bus.Publish(new IdeHostStateChanged( |
| 55 | CSharpLspProcessActive: true, |
| 56 | MarkdownLspProcessActive: false, |
| 57 | CSharpLspHostPresent: true, |
| 58 | MarkdownLspHostPresent: false)); |
| 59 | |
| 60 | var snapshot = unit.Build(default); |
| 61 | |
| 62 | Assert.Equal("LSP · C#", snapshot.IdeHost.LspStatusHint); |
| 63 | } |
| 64 | |
| 65 | [Fact] |
| 66 | public void Build_uses_git_segment_unit_output() |
| 67 | { |
| 68 | var bus = new InMemoryDataBus(); |
| 69 | var unit = new IdeHealthSnapshotUnit(bus); |
| 70 | bus.Publish(new GitStateChanged("Git: dirty +2/-1", "main*")); |
| 71 | |
| 72 | var snapshot = unit.Build(default); |
| 73 | |
| 74 | Assert.Equal("Git: dirty +2/-1", snapshot.Workspace.Git.LineText); |
| 75 | Assert.Equal("main*", snapshot.Workspace.Git.CockpitShort); |
| 76 | Assert.Equal(IdeHealthStratum.Workspace, snapshot.Workspace.Git.Stratum); |
| 77 | } |
| 78 | |
| 79 | [Fact] |
| 80 | public void Build_reflects_last_build_result_after_finish_event() |
| 81 | { |
| 82 | var bus = new InMemoryDataBus(); |
| 83 | var unit = new IdeHealthSnapshotUnit(bus); |
| 84 | bus.Publish(new StartupProjectPathChanged("src/App/App.csproj")); |
| 85 | bus.Publish(new GitStateChanged("Git: clean", "main")); |
| 86 | bus.Publish(new BuildStateChanged(true)); |
| 87 | bus.Publish(new BuildStateChanged(false, 0, true)); |
| 88 | |
| 89 | var snapshot = unit.Build(default); |
| 90 | |
| 91 | Assert.Contains("last OK", snapshot.Solution.Build.LineText, StringComparison.Ordinal); |
| 92 | } |
| 93 | |
| 94 | [Fact] |
| 95 | public void Build_reads_build_signal_from_databus() |
| 96 | { |
| 97 | var bus = new InMemoryDataBus(); |
| 98 | var unit = new IdeHealthSnapshotUnit(bus); |
| 99 | bus.Publish(new StartupProjectPathChanged("src/App/App.csproj")); |
| 100 | bus.Publish(new GitStateChanged("Git: clean", "main")); |
| 101 | bus.Publish(new BuildStateChanged(true)); |
| 102 | |
| 103 | var snapshot = unit.Build(default); |
| 104 | |
| 105 | Assert.Equal(IdeHealthScope.Project, snapshot.Solution.Build.Scope); |
| 106 | Assert.True(snapshot.Solution.Build.IsBuildRunning); |
| 107 | Assert.Equal("BUILD…", snapshot.Solution.Build.CockpitShort); |
| 108 | } |
| 109 | |
| 110 | [Fact] |
| 111 | public void Build_reads_tests_signal_from_databus() |
| 112 | { |
| 113 | var bus = new InMemoryDataBus(); |
| 114 | var unit = new IdeHealthSnapshotUnit(bus); |
| 115 | bus.Publish(new StartupProjectPathChanged("src/App/App.csproj")); |
| 116 | bus.Publish(new GitStateChanged("Git: clean", "main")); |
| 117 | bus.Publish(new TestsStateChanged("7/7 passed, 0 failed", 0)); |
| 118 | |
| 119 | var snapshot = unit.Build(default); |
| 120 | |
| 121 | Assert.Equal(IdeHealthScope.Project, snapshot.Solution.Tests.Scope); |
| 122 | Assert.Equal("src/App/App.csproj", snapshot.Solution.Tests.ProjectPath); |
| 123 | Assert.Contains("7/7 passed, 0 failed", snapshot.Solution.Tests.LineText, StringComparison.Ordinal); |
| 124 | } |
| 125 | |
| 126 | [Fact] |
| 127 | public void Build_reads_debug_signal_from_databus() |
| 128 | { |
| 129 | var bus = new InMemoryDataBus(); |
| 130 | var unit = new IdeHealthSnapshotUnit(bus); |
| 131 | bus.Publish(new StartupProjectPathChanged("src/App/App.csproj")); |
| 132 | bus.Publish(new GitStateChanged("Git: clean", "main")); |
| 133 | |
| 134 | var paused = DebugSessionSnapshot.Empty with |
| 135 | { |
| 136 | HasActiveSession = true, |
| 137 | IsExecutionStopped = true, |
| 138 | StackFrames = new (string Name, string? File, int Line)[] { ("A", "a.cs", 1) }, |
| 139 | VariableRootScopes = new[] |
| 140 | { |
| 141 | new DebugVariableRootScope("Locals", new List<DebugVariableRow> { new("x", "1", "int") }) |
| 142 | } |
| 143 | }; |
| 144 | bus.Publish(new BuildStateChanged(true)); |
| 145 | bus.Publish(new DebugStateChanged(paused)); |
| 146 | |
| 147 | var snapshot = unit.Build(default); |
| 148 | |
| 149 | Assert.Equal(IdeHealthScope.Project, snapshot.Solution.Debug.Scope); |
| 150 | Assert.Contains("paused", snapshot.Solution.Debug.LineText, StringComparison.Ordinal); |
| 151 | } |
| 152 | |
| 153 | [Fact] |
| 154 | public void Dispose_makes_subsequent_Build_throw() |
| 155 | { |
| 156 | var bus = new InMemoryDataBus(); |
| 157 | var unit = new IdeHealthSnapshotUnit(bus); |
| 158 | unit.Dispose(); |
| 159 | Assert.Throws<ObjectDisposedException>(() => unit.Build(default)); |
| 160 | } |
| 161 | } |
| 162 | |