-
Notifications
You must be signed in to change notification settings - Fork 33
Add full test coverage for core logic #67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
a2e9ee3
Add full unit test coverage for macOS and Windows core logic
google-labs-jules[bot] 46ae907
Add full unit test coverage for macOS and Windows core logic
google-labs-jules[bot] 1c85dcb
Add full unit test coverage for macOS and Windows core logic
google-labs-jules[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| using System; | ||
| using System.Text.Json; | ||
| using KeyStats.Models; | ||
| using Xunit; | ||
|
|
||
| namespace KeyStats.Tests; | ||
|
|
||
| public class DailyStatsTests | ||
| { | ||
| [Fact] | ||
| public void Initialization_ShouldSetDefaultValues() | ||
| { | ||
| var stats = new DailyStats(); | ||
| Assert.Equal(0, stats.KeyPresses); | ||
| Assert.Equal(0, stats.TotalClicks); | ||
| Assert.Equal(0, stats.MouseDistance); | ||
| Assert.Equal(DateTime.Today, stats.Date.Date); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TotalClicks_ShouldSumAllClicks() | ||
| { | ||
| var stats = new DailyStats | ||
| { | ||
| LeftClicks = 10, | ||
| RightClicks = 5, | ||
| SideBackClicks = 2, | ||
| SideForwardClicks = 1 | ||
| }; | ||
| Assert.Equal(18, stats.TotalClicks); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Serialization_ShouldPreserveValues() | ||
| { | ||
| var original = new DailyStats | ||
| { | ||
| KeyPresses = 42, | ||
| LeftClicks = 10, | ||
| MouseDistance = 123.45 | ||
| }; | ||
| original.KeyPressCounts["Enter"] = 5; | ||
|
|
||
| var json = JsonSerializer.Serialize(original); | ||
| var deserialized = JsonSerializer.Deserialize<DailyStats>(json); | ||
|
|
||
| Assert.NotNull(deserialized); | ||
| Assert.Equal(42, deserialized.KeyPresses); | ||
| Assert.Equal(10, deserialized.LeftClicks); | ||
| Assert.Equal(123.45, deserialized.MouseDistance); | ||
| Assert.True(deserialized.KeyPressCounts.ContainsKey("Enter")); | ||
| Assert.Equal(5, deserialized.KeyPressCounts["Enter"]); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net48</TargetFramework> | ||
| <Nullable>enable</Nullable> | ||
| <LangVersion>10.0</LangVersion> | ||
| <IsPackable>false</IsPackable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
| <PackageReference Include="xunit" Version="2.6.2" /> | ||
| <PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" /> | ||
| <PackageReference Include="coverlet.collector" Version="6.0.0" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\KeyStats\KeyStats.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| using System; | ||
| using System.IO; | ||
| using System.Threading; | ||
| using KeyStats.Services; | ||
| using KeyStats.Models; | ||
| using Xunit; | ||
|
|
||
| namespace KeyStats.Tests; | ||
|
|
||
| public class StatsManagerTests : IDisposable | ||
| { | ||
| private readonly string _testDataFolder; | ||
| private readonly StatsManager _statsManager; | ||
| private readonly InputMonitorService _inputMonitor; | ||
|
|
||
| public StatsManagerTests() | ||
| { | ||
| _testDataFolder = Path.Combine(Path.GetTempPath(), "KeyStatsTests_" + Guid.NewGuid()); | ||
| Directory.CreateDirectory(_testDataFolder); | ||
|
|
||
| StatsManager.ResetInstanceForTesting(_testDataFolder); | ||
| _statsManager = StatsManager.Instance; | ||
|
|
||
| // InputMonitorService is a singleton, so we get the same instance. | ||
| // We can't easily reset it, but we can use it to fire events. | ||
| _inputMonitor = InputMonitorService.Instance; | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| // Cleanup | ||
| StatsManager.DisposeInstance(); | ||
| try | ||
| { | ||
| if (Directory.Exists(_testDataFolder)) | ||
| { | ||
| Directory.Delete(_testDataFolder, true); | ||
| } | ||
| } | ||
| catch { /* Ignore cleanup errors */ } | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Initialization_ShouldStartWithZeroStats() | ||
| { | ||
| Assert.Equal(0, _statsManager.CurrentStats.KeyPresses); | ||
| Assert.Equal(0, _statsManager.CurrentStats.TotalClicks); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void IncrementKeyPresses_ShouldIncreaseCount() | ||
| { | ||
| _inputMonitor.SimulateKeyPress("A", "TestApp", "Test App"); | ||
|
|
||
| Assert.Equal(1, _statsManager.CurrentStats.KeyPresses); | ||
| Assert.True(_statsManager.CurrentStats.KeyPressCounts.ContainsKey("A")); | ||
| Assert.Equal(1, _statsManager.CurrentStats.KeyPressCounts["A"]); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void IncrementClicks_ShouldIncreaseCount() | ||
| { | ||
| _inputMonitor.SimulateLeftClick("TestApp", "Test App"); | ||
| Assert.Equal(1, _statsManager.CurrentStats.LeftClicks); | ||
| Assert.Equal(1, _statsManager.CurrentStats.TotalClicks); | ||
|
|
||
| _inputMonitor.SimulateRightClick("TestApp", "Test App"); | ||
| Assert.Equal(1, _statsManager.CurrentStats.RightClicks); | ||
| Assert.Equal(2, _statsManager.CurrentStats.TotalClicks); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Persistence_ShouldSaveAndLoadStats() | ||
| { | ||
| _inputMonitor.SimulateKeyPress("SaveTest", "TestApp", "Test App"); | ||
|
|
||
| // Force save | ||
| _statsManager.FlushPendingSave(); | ||
|
|
||
| // Reset instance to simulate restart | ||
| StatsManager.ResetInstanceForTesting(_testDataFolder); | ||
| var newManager = StatsManager.Instance; | ||
|
|
||
| Assert.Equal(1, newManager.CurrentStats.KeyPresses); | ||
| Assert.True(newManager.CurrentStats.KeyPressCounts.ContainsKey("SaveTest")); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,18 @@ public class StatsManager : IDisposable | |
| private static StatsManager? _instance; | ||
| public static StatsManager Instance => _instance ??= new StatsManager(); | ||
|
|
||
| internal static void ResetInstanceForTesting(string? dataFolder = null) | ||
| { | ||
| DisposeInstance(); | ||
| _instance = new StatsManager(dataFolder); | ||
| } | ||
|
|
||
| internal static void DisposeInstance() | ||
| { | ||
| _instance?.Dispose(); | ||
| _instance = null; | ||
| } | ||
|
|
||
| private const double DefaultMetersPerPixel = AppSettings.DefaultMouseMetersPerPixel; | ||
|
|
||
| private readonly string _dataFolder; | ||
|
|
@@ -42,12 +54,20 @@ public class StatsManager : IDisposable | |
|
|
||
| public event Action? StatsUpdateRequested; | ||
|
|
||
| private StatsManager() | ||
| private StatsManager(string? dataFolder = null) | ||
| { | ||
| _dataFolder = Path.Combine( | ||
| Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), | ||
| "KeyStats"); | ||
| Directory.CreateDirectory(_dataFolder); | ||
| if (dataFolder != null) | ||
| { | ||
| _dataFolder = dataFolder; | ||
| Directory.CreateDirectory(_dataFolder); | ||
| } | ||
| else | ||
| { | ||
| _dataFolder = Path.Combine( | ||
| Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), | ||
| "KeyStats"); | ||
| Directory.CreateDirectory(_dataFolder); | ||
| } | ||
|
Comment on lines
+59
to
+70
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The constructor logic for determining _dataFolder = dataFolder ?? Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"KeyStats");
Directory.CreateDirectory(_dataFolder); |
||
|
|
||
| _statsFilePath = Path.Combine(_dataFolder, "daily_stats.json"); | ||
| _historyFilePath = Path.Combine(_dataFolder, "history.json"); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import XCTest | ||
| @testable import KeyStats | ||
|
|
||
| class DailyStatsTests: XCTestCase { | ||
|
|
||
| func testInitialization() { | ||
| let stats = DailyStats() | ||
| XCTAssertEqual(stats.keyPresses, 0) | ||
| XCTAssertEqual(stats.totalClicks, 0) | ||
| XCTAssertEqual(stats.mouseDistance, 0) | ||
| } | ||
|
|
||
| func testTotalClicks() { | ||
| var stats = DailyStats() | ||
| stats.leftClicks = 10 | ||
| stats.rightClicks = 5 | ||
| stats.sideBackClicks = 2 | ||
| stats.sideForwardClicks = 1 | ||
|
|
||
| XCTAssertEqual(stats.totalClicks, 18) | ||
| } | ||
|
|
||
| func testCorrectionRate() { | ||
| var stats = DailyStats() | ||
| stats.keyPresses = 100 | ||
| stats.keyPressCounts["Delete"] = 5 | ||
| stats.keyPressCounts["ForwardDelete"] = 5 | ||
| stats.keyPressCounts["A"] = 90 | ||
|
|
||
| // 10 deletes out of 100 keys | ||
| XCTAssertEqual(stats.correctionRate, 0.1, accuracy: 0.0001) | ||
| } | ||
|
|
||
| func testInputRatio() { | ||
| var stats = DailyStats() | ||
| stats.keyPresses = 100 | ||
| stats.leftClicks = 50 | ||
| // total clicks = 50 | ||
|
|
||
| XCTAssertEqual(stats.inputRatio, 2.0, accuracy: 0.0001) | ||
| } | ||
|
|
||
| func testEncodingDecoding() { | ||
| var stats = DailyStats() | ||
| stats.keyPresses = 42 | ||
| stats.leftClicks = 10 | ||
| stats.keyPressCounts["Enter"] = 5 | ||
|
|
||
| do { | ||
| let data = try JSONEncoder().encode(stats) | ||
| let decoded = try JSONDecoder().decode(DailyStats.self, from: data) | ||
|
|
||
| XCTAssertEqual(decoded.keyPresses, 42) | ||
| XCTAssertEqual(decoded.leftClicks, 10) | ||
| XCTAssertEqual(decoded.keyPressCounts["Enter"], 5) | ||
| } catch { | ||
| XCTFail("Encoding/Decoding failed: \(error)") | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While ignoring cleanup errors in tests can be acceptable, swallowing all exceptions silently with
catch {}can hide underlying problems like file locks or permission issues that might cause subsequent tests to fail unexpectedly. It's better practice to catch specific exceptions or at least log the error to aid in debugging.