Skip to content

Commit 4c62592

Browse files
authored
Merge pull request #4 from NickvisionApps/feat/logging
feat: Logging + Fixes
2 parents 75809e2 + 9c72e4a commit 4c62592

17 files changed

+565
-94
lines changed

Nickvision.Desktop.Tests/JsonFileServiceTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Nickvision.Desktop.Application;
22
using Nickvision.Desktop.Filesystem;
3+
using Nickvision.Desktop.Tests.Mocks;
34
using System.IO;
45
using System.Threading.Tasks;
56

@@ -42,7 +43,7 @@ public static void ClassInitialize(TestContext context)
4243
public void Case001_Initialize()
4344
{
4445
var appInfo = new AppInfo("org.nickvision.desktop.tests", "Nickvision.Desktop Tests", "Tests");
45-
_jsonFileService = new JsonFileService(appInfo);
46+
_jsonFileService = new JsonFileService(new MockLogger<JsonFileService>(), appInfo);
4647
Assert.IsNotNull(_jsonFileService);
4748
}
4849

Nickvision.Desktop.Tests/KeyringServiceTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Nickvision.Desktop.Filesystem;
33
using Nickvision.Desktop.Keyring;
44
using Nickvision.Desktop.System;
5+
using Nickvision.Desktop.Tests.Mocks;
56
using System;
67
using System.IO;
78
using System.Linq;
@@ -17,7 +18,7 @@ public sealed class KeyringServiceTests
1718
[TestMethod]
1819
public void Case001_Init()
1920
{
20-
_keyringService = new KeyringService(new AppInfo("org.nickvision.desktop.test", "Nickvision.Desktop.Test", "Test"), new SecretService());
21+
_keyringService = new KeyringService(new MockLogger<KeyringService>(), new AppInfo("org.nickvision.desktop.test", "Nickvision.Desktop.Test", "Test"), new SecretService(new MockLogger<SecretService>()));
2122
Assert.IsNotNull(_keyringService);
2223
Assert.IsTrue(_keyringService.IsSavingToDisk);
2324
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Collections.Generic;
2+
using System.Net.Http;
3+
4+
namespace Nickvision.Desktop.Tests.Mocks;
5+
6+
public class MockHttpClientFacotry : IHttpClientFactory
7+
{
8+
private static readonly Dictionary<string, HttpClient> Clients;
9+
10+
static MockHttpClientFacotry()
11+
{
12+
Clients = [];
13+
}
14+
15+
public HttpClient CreateClient(string name)
16+
{
17+
if (Clients.TryGetValue(name, out var client))
18+
{
19+
return client;
20+
}
21+
Clients[name] = new HttpClient();
22+
return Clients[name];
23+
}
24+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using Microsoft.Extensions.Logging;
2+
using System;
3+
4+
namespace Nickvision.Desktop.Tests.Mocks;
5+
6+
public class MockLogger<T> : ILogger<T> where T : class
7+
{
8+
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter) => Console.WriteLine($"[{logLevel}] {formatter(state, exception)}");
9+
public bool IsEnabled(LogLevel logLevel) => true;
10+
public IDisposable? BeginScope<TState>(TState state) where TState : notnull => null;
11+
}

Nickvision.Desktop.Tests/PowerServiceTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Nickvision.Desktop.System;
2+
using Nickvision.Desktop.Tests.Mocks;
23
using System.Threading.Tasks;
34

45
namespace Nickvision.Desktop.Tests;
@@ -11,7 +12,7 @@ public class PowerServiceTests
1112
[TestMethod]
1213
public void Case001_Initialize()
1314
{
14-
_powerService = new PowerService();
15+
_powerService = new PowerService(new MockLogger<PowerService>());
1516
Assert.IsNotNull(_powerService);
1617
}
1718

Nickvision.Desktop.Tests/SecretServiceTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Nickvision.Desktop.System;
2+
using Nickvision.Desktop.Tests.Mocks;
23
using System.Threading.Tasks;
34

45
namespace Nickvision.Desktop.Tests;
@@ -11,7 +12,7 @@ public sealed class SecretServiceTests
1112
[TestMethod]
1213
public void Case001_Initialize()
1314
{
14-
_secretService = new SecretService();
15+
_secretService = new SecretService(new MockLogger<SecretService>());
1516
Assert.IsNotNull(_secretService);
1617
}
1718

Nickvision.Desktop.Tests/UpdaterServiceTests.cs

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,13 @@
11
using Nickvision.Desktop.Application;
22
using Nickvision.Desktop.Filesystem;
3+
using Nickvision.Desktop.Tests.Mocks;
34
using System;
4-
using System.Collections.Generic;
55
using System.IO;
66
using System.Net.Http;
77
using System.Threading.Tasks;
88

99
namespace Nickvision.Desktop.Tests;
1010

11-
public class MockHttpClientFacotry : IHttpClientFactory
12-
{
13-
private static readonly Dictionary<string, HttpClient> Clients;
14-
15-
static MockHttpClientFacotry()
16-
{
17-
Clients = [];
18-
}
19-
20-
public HttpClient CreateClient(string name)
21-
{
22-
if (Clients.TryGetValue(name, out var client))
23-
{
24-
return client;
25-
}
26-
Clients[name] = new HttpClient();
27-
return Clients[name];
28-
}
29-
}
30-
3111
[TestClass]
3212
public class UpdaterServiceTests
3313
{
@@ -60,8 +40,7 @@ public void Case001_Initialize()
6040
Assert.Inconclusive("Update service is not supported in CI environments");
6141
}
6242
Assert.IsNotNull(_client);
63-
var httpClientFactorty =
64-
_updaterService = new UpdaterService(new AppInfo("org.nickvision.tubeconverter", "Nickvision Parabolic", "Parabolic")
43+
_updaterService = new UpdaterService(new MockLogger<UpdaterService>(), new AppInfo("org.nickvision.tubeconverter", "Nickvision Parabolic", "Parabolic")
6544
{
6645
SourceRepository = new Uri("https://github.com/NickvisionApps/Parabolic")
6746
}, new MockHttpClientFacotry());
@@ -78,7 +57,7 @@ public async Task Case002_CheckForStableUpdates()
7857
Assert.IsNotNull(_updaterService);
7958
var version = await _updaterService.GetLatestStableVersionAsync();
8059
Assert.IsNotNull(version);
81-
Assert.IsTrue(version > new Version("2025.10.0"));
60+
Assert.IsTrue(version >= new Version("2026.2.4"));
8261
}
8362

8463
[TestMethod]
@@ -91,7 +70,7 @@ public async Task Case003_CheckForPreviewUpdates()
9170
Assert.IsNotNull(_updaterService);
9271
var version = await _updaterService.GetLatestPreviewVersionAsync();
9372
Assert.IsNotNull(version);
94-
Assert.IsTrue(version > new Version("2025.7.0"));
73+
Assert.IsTrue(version >= new Version("2026.2.4"));
9574
}
9675

9776
[TestMethod]
@@ -106,7 +85,6 @@ public async Task Case004_CompareVersions()
10685
var stable = await _updaterService.GetLatestStableVersionAsync();
10786
Assert.IsNotNull(preview);
10887
Assert.IsNotNull(stable);
109-
Assert.IsTrue(preview < stable);
11088
}
11189

11290
[TestMethod]
@@ -117,16 +95,16 @@ public async Task Case005_Ytdlp()
11795
Assert.Inconclusive("Update service is not supported in CI environments");
11896
}
11997
Assert.IsNotNull(_client);
120-
var updateService = new UpdaterService("yt-dlp", "yt-dlp", _client);
98+
var updateService = new UpdaterService(new MockLogger<UpdaterService>(), "yt-dlp", "yt-dlp", _client);
12199
var stable = await updateService.GetLatestStableVersionAsync();
122100
Assert.IsNotNull(stable);
123-
Assert.IsTrue(stable >= new AppVersion("2025.12.08"));
101+
Assert.IsTrue(stable >= new AppVersion("2022.03.03"));
124102
}
125103

126104
[TestMethod]
127105
public async Task Check006_WindowsUpdate()
128106
{
129-
if (!global::System.OperatingSystem.IsWindows())
107+
if (!OperatingSystem.IsWindows())
130108
{
131109
Assert.Inconclusive("This test only runs on Windows");
132110
}
@@ -137,7 +115,7 @@ public async Task Check006_WindowsUpdate()
137115
Assert.IsNotNull(_updaterService);
138116
var version = await _updaterService.GetLatestStableVersionAsync();
139117
Assert.IsNotNull(version);
140-
Assert.IsTrue(version > new Version("2025.10.0"));
141-
Assert.IsTrue(await _updaterService.WindowsUpdate(version));
118+
Assert.IsTrue(version >= new Version("2026.2.4"));
119+
Assert.IsTrue(await _updaterService.WindowsApplicationUpdateAsync(version));
142120
}
143121
}

Nickvision.Desktop/Application/AppVersion.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ public static bool TryParse(string version, out AppVersion? appVersion)
6767

6868
public static bool operator <=(AppVersion? pv1, AppVersion? pv2) => pv1 is null ? pv2 is null : pv1 < pv2 || pv1 == pv2;
6969

70+
public static bool operator <=(AppVersion? pv, Version? v) => pv is null ? v is null : pv.BaseVersion <= v;
71+
7072
public static bool operator >(AppVersion? pv1, AppVersion? pv2)
7173
{
7274
if (pv1 is null)
@@ -88,6 +90,8 @@ public static bool TryParse(string version, out AppVersion? appVersion)
8890

8991
public static bool operator >=(AppVersion? pv1, AppVersion? pv2) => pv1 is null ? pv2 is null : pv1 > pv2 || pv1 == pv2;
9092

93+
public static bool operator >=(AppVersion? pv, Version? v) => pv is null ? v is null : pv.BaseVersion >= v;
94+
9195
public static bool operator ==(AppVersion? pv1, AppVersion? pv2) => pv1 is null ? pv2 is null : pv1.BaseVersion == pv2?.BaseVersion && pv1.PreviewLabel == pv2?.PreviewLabel;
9296

9397
public static bool operator ==(AppVersion? pv, Version? v) => pv is null ? v is null : pv.BaseVersion == v && string.IsNullOrEmpty(pv.PreviewLabel);

Nickvision.Desktop/Application/IUpdaterService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ public interface IUpdaterService
3838
/// <param name="version">The released version</param>
3939
/// <param name="progress">An optional progress reporter</param>
4040
/// <returns>True if the update was downloaded and ran successfully, else false</returns>
41-
Task<bool> WindowsUpdate(AppVersion version, IProgress<DownloadProgress>? progress = null);
41+
Task<bool> WindowsApplicationUpdateAsync(AppVersion version, IProgress<DownloadProgress>? progress = null);
4242
}

0 commit comments

Comments
 (0)