|
| 1 | +using System.Collections.Generic; |
| 2 | +using System.Linq; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using GamerGuardian.Models; |
| 5 | +using GamerGuardian.Services; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace GamerGuardian.Tests; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Covers <see cref="MonitorService.SelectNotifiable"/> — the pure rule that |
| 12 | +/// decides which drifted settings raise an on-screen notification. The key |
| 13 | +/// guarantee under test: "silent means silent" — a setting the user set to |
| 14 | +/// auto-apply must never notify, on any tick, for any reason. |
| 15 | +/// </summary> |
| 16 | +public class MonitorServiceTests |
| 17 | +{ |
| 18 | + private static DriftItem Drift(string id, bool autoApply) => new( |
| 19 | + SettingId: id, |
| 20 | + DisplayKey: "global", |
| 21 | + DisplayLabel: "Global", |
| 22 | + Description: id, |
| 23 | + CurrentValue: "On", |
| 24 | + DesiredValue: "Off", |
| 25 | + AutoApply: autoApply, |
| 26 | + Apply: () => Task.CompletedTask); |
| 27 | + |
| 28 | + private static bool NeverTripped(string _) => false; |
| 29 | + |
| 30 | + [Fact] |
| 31 | + public void NotifyOnly_Setting_IsNotified() |
| 32 | + { |
| 33 | + var drifted = new[] { Drift("hags", autoApply: false) }; |
| 34 | + var result = MonitorService.SelectNotifiable(drifted, NeverTripped); |
| 35 | + Assert.Single(result); |
| 36 | + Assert.Equal("hags", result[0].SettingId); |
| 37 | + } |
| 38 | + |
| 39 | + [Fact] |
| 40 | + public void AutoApply_Setting_IsNeverNotified() |
| 41 | + { |
| 42 | + // The whole point: a "silently change" setting must not produce a toast. |
| 43 | + var drifted = new[] { Drift("vrr", autoApply: true) }; |
| 44 | + Assert.Empty(MonitorService.SelectNotifiable(drifted, NeverTripped)); |
| 45 | + } |
| 46 | + |
| 47 | + [Fact] |
| 48 | + public void AutoApply_Setting_IsNotNotified_EvenWhenNotBeingApplied() |
| 49 | + { |
| 50 | + // This models the verify-backoff / breaker-cooldown case: the setting is |
| 51 | + // auto-apply but couldn't be applied this tick. It must STILL stay silent — |
| 52 | + // this was the regression where backed-off auto-apply settings leaked into |
| 53 | + // the notification path. |
| 54 | + var drifted = new[] { Drift("svc:DoSvc", autoApply: true) }; |
| 55 | + Assert.Empty(MonitorService.SelectNotifiable(drifted, NeverTripped)); |
| 56 | + } |
| 57 | + |
| 58 | + [Fact] |
| 59 | + public void BreakerTripped_NotifyOnly_Setting_IsSuppressed() |
| 60 | + { |
| 61 | + var drifted = new[] { Drift("hags", autoApply: false) }; |
| 62 | + var result = MonitorService.SelectNotifiable(drifted, id => id == "hags"); |
| 63 | + Assert.Empty(result); |
| 64 | + } |
| 65 | + |
| 66 | + [Fact] |
| 67 | + public void MixedBatch_KeepsOnlyNotifyOnlyUntrippedSettings() |
| 68 | + { |
| 69 | + var drifted = new[] |
| 70 | + { |
| 71 | + Drift("auto1", autoApply: true), // silent -> excluded |
| 72 | + Drift("notify1", autoApply: false), // notify -> kept |
| 73 | + Drift("notify2", autoApply: false), // notify but tripped -> excluded |
| 74 | + Drift("auto2", autoApply: true), // silent -> excluded |
| 75 | + }; |
| 76 | + |
| 77 | + var result = MonitorService.SelectNotifiable(drifted, id => id == "notify2"); |
| 78 | + |
| 79 | + Assert.Equal(new[] { "notify1" }, result.Select(d => d.SettingId).ToArray()); |
| 80 | + } |
| 81 | +} |
0 commit comments