|
| 1 | +using System.Collections.ObjectModel; |
| 2 | +using System.Text.Json; |
| 3 | +using Microsoft.Extensions.DependencyInjection; |
| 4 | +using Microsoft.Extensions.Hosting; |
| 5 | +using Microsoft.Extensions.Logging; |
| 6 | +using SecRandom.Core.Abstraction; |
| 7 | +using SecRandom.Core.Abstraction.Services; |
| 8 | +using SecRandom.Core.Enums; |
| 9 | +using SecRandom.Core.Enums.Configs; |
| 10 | +using SecRandom.Core.Models; |
| 11 | +using SecRandom.Core.Models.SubConfigs.Picking; |
| 12 | +using SecRandom.Core.Services.Config; |
| 13 | +using SecRandom.Core.Services.Draw; |
| 14 | +using SecRandom.Shared.Abstraction; |
| 15 | +using SecRandom.Shared.Models.Profile; |
| 16 | + |
| 17 | +namespace SecRandom.Core.Tests; |
| 18 | + |
| 19 | +public sealed class FairDrawAlgorithmTests |
| 20 | +{ |
| 21 | + [Fact] |
| 22 | + public void FairDraw_ExcludesAboveAverageCandidatesWhenLowerCandidatesCanSatisfyDraw() |
| 23 | + { |
| 24 | + var low = new Student { Name = "Low", RecordId = Guid.NewGuid() }; |
| 25 | + var average = new Student { Name = "Average", RecordId = Guid.NewGuid() }; |
| 26 | + var high = new Student { Name = "High", RecordId = Guid.NewGuid() }; |
| 27 | + var history = new StudentHistory |
| 28 | + { |
| 29 | + Students = |
| 30 | + { |
| 31 | + [low.RecordId.ToString("D")] = new History { TotalCount = 0 }, |
| 32 | + [average.RecordId.ToString("D")] = new History { TotalCount = 2 }, |
| 33 | + [high.RecordId.ToString("D")] = new History { TotalCount = 4 } |
| 34 | + } |
| 35 | + }; |
| 36 | + var config = CreateConfig(new FairDrawSettingsConfig |
| 37 | + { |
| 38 | + FairDraw = true, |
| 39 | + FairDrawGroup = false, |
| 40 | + FairDrawGender = false, |
| 41 | + FairDrawTime = false, |
| 42 | + ColdStartEnabled = false, |
| 43 | + EnableAvgGapProtection = true, |
| 44 | + GapThreshold = 10, |
| 45 | + MinWeight = 0, |
| 46 | + MaxWeight = 10 |
| 47 | + }); |
| 48 | + var students = new StudentList { Students = [low, average, high] }; |
| 49 | + |
| 50 | + using var host = CreateHost(config, new TestProfileService(history, students)); |
| 51 | + IAppHost.Host = host; |
| 52 | + var engine = new DrawEngine(); |
| 53 | + |
| 54 | + var localResult = engine.DrawPreparedStudents(1, [low, average, high], DrawSettingsType.RollCall); |
| 55 | + var verificationInput = engine.CreateStudentVerificationInput(1, [low, average, high], DrawSettingsType.RollCall); |
| 56 | + |
| 57 | + Assert.True(localResult.IsSuccess); |
| 58 | + Assert.DoesNotContain(high, localResult.Result); |
| 59 | + Assert.DoesNotContain(verificationInput.Candidates, candidate => candidate.RecordId == high.RecordId); |
| 60 | + using var audit = JsonDocument.Parse(verificationInput.AuditPayload); |
| 61 | + Assert.True(audit.RootElement.GetProperty("fairness").GetProperty("averageGapProtectionApplied").GetBoolean()); |
| 62 | + Assert.Equal(3, audit.RootElement.GetProperty("fairness").GetProperty("candidateCountBeforeAverageGapProtection").GetInt32()); |
| 63 | + Assert.Equal(2, audit.RootElement.GetProperty("fairness").GetProperty("candidateCountAfterAverageGapProtection").GetInt32()); |
| 64 | + } |
| 65 | + |
| 66 | + [Fact] |
| 67 | + public void CreateStudentVerificationInput_UsesCourseScopedBalanceWeights() |
| 68 | + { |
| 69 | + var groupA = new Student { Name = "A", Group = "A", RecordId = Guid.NewGuid() }; |
| 70 | + var groupB = new Student { Name = "B", Group = "B", RecordId = Guid.NewGuid() }; |
| 71 | + var history = new StudentHistory(); |
| 72 | + history.GroupStats["A"] = 100; |
| 73 | + history.Students["legacy"] = new History |
| 74 | + { |
| 75 | + Histories = new ObservableCollection<HistoryItem>( |
| 76 | + Enumerable.Range(0, 10) |
| 77 | + .Select(_ => new HistoryItem { CourseName = "数学", RecordGroup = "B" })) |
| 78 | + }; |
| 79 | + var config = CreateConfig(new FairDrawSettingsConfig |
| 80 | + { |
| 81 | + FairDraw = true, |
| 82 | + FairDrawGroup = true, |
| 83 | + FairDrawGender = false, |
| 84 | + FairDrawTime = false, |
| 85 | + ColdStartEnabled = false, |
| 86 | + EnableAvgGapProtection = false, |
| 87 | + FrequencyWeight = 0, |
| 88 | + BaseWeight = 0, |
| 89 | + GroupWeight = 1, |
| 90 | + MinWeight = 0, |
| 91 | + MaxWeight = 10 |
| 92 | + }); |
| 93 | + var students = new StudentList { Students = [groupA, groupB] }; |
| 94 | + |
| 95 | + using var host = CreateHost(config, new TestProfileService(history, students)); |
| 96 | + IAppHost.Host = host; |
| 97 | + |
| 98 | + var input = new DrawEngine().CreateStudentVerificationInput(1, [groupA, groupB], DrawSettingsType.RollCall, "数学"); |
| 99 | + |
| 100 | + Assert.True(input.Candidates.Single(candidate => candidate.RecordId == groupA.RecordId).WeightMicros |
| 101 | + > input.Candidates.Single(candidate => candidate.RecordId == groupB.RecordId).WeightMicros); |
| 102 | + } |
| 103 | + |
| 104 | + private static MainConfigModel CreateConfig(FairDrawSettingsConfig fairSettings) |
| 105 | + { |
| 106 | + return new MainConfigModel |
| 107 | + { |
| 108 | + FairDrawSettings = fairSettings, |
| 109 | + RollCallSettings = new RollCallSettingsConfig(), |
| 110 | + LotterySettings = new LotterySettingsConfig(), |
| 111 | + DefaultDrawSettings = new DefaultDrawSettingsConfig() |
| 112 | + }; |
| 113 | + } |
| 114 | + |
| 115 | + private static IHost CreateHost(MainConfigModel config, IProfileService profile) |
| 116 | + { |
| 117 | + return Host.CreateDefaultBuilder() |
| 118 | + .ConfigureServices(services => |
| 119 | + { |
| 120 | + services.AddLogging(builder => builder.SetMinimumLevel(LogLevel.None)); |
| 121 | + services.AddSingleton<IProfileService>(profile); |
| 122 | + services.AddSingleton<ConfigServiceBase>(new TestConfigService(config)); |
| 123 | + services.AddSingleton<MainConfigHandler>(); |
| 124 | + }) |
| 125 | + .Build(); |
| 126 | + } |
| 127 | + |
| 128 | + private sealed class TestConfigService(MainConfigModel config) : ConfigServiceBase |
| 129 | + { |
| 130 | + public override bool IsConfigExists<T>(T fallback) => true; |
| 131 | + public override T LoadConfig<T>(T fallback) => config is T typed ? typed : fallback; |
| 132 | + public override void SaveConfig<T>(T config) { } |
| 133 | + public override void DeleteConfig<T>(T config) { } |
| 134 | + } |
| 135 | + |
| 136 | + private sealed class TestProfileService(StudentHistory history, StudentList students) : IProfileService |
| 137 | + { |
| 138 | + public StudentList? CurrentStudentList { get; } = students; |
| 139 | + public StudentHistory? CurrentStudentHistory { get; } = history; |
| 140 | + public PrizeList? CurrentPrizeList { get; } = new(); |
| 141 | + public PrizeHistory? CurrentPrizeHistory { get; } = new(); |
| 142 | + public StudentListConfig? StudentListConfig => null; |
| 143 | + public StudentHistoryConfig? StudentHistoryConfig => null; |
| 144 | + public PrizeListConfig? PrizeListConfig => null; |
| 145 | + public PrizeHistoryConfig? PrizeHistoryConfig => null; |
| 146 | + public void LoadStudentProfile(string name, bool saveCurrent = true) { } |
| 147 | + public void LoadPrizeProfile(string name, bool saveCurrent = true) { } |
| 148 | + public void RecordStudentHistory(IReadOnlyList<Student> students, DateTime now, int requestedCount, string drawGroup = "", string drawGender = "", int drawMethod = 0, IReadOnlyDictionary<Student, double>? weights = null, string courseName = "") { } |
| 149 | + public void RecordPrizeHistory(IReadOnlyList<Prize> prizes, DateTime now, int requestedCount) { } |
| 150 | + public void ClearCurrentStudentHistory() { } |
| 151 | + public void ClearCurrentPrizeHistory() { } |
| 152 | + public void SaveProfile() { } |
| 153 | + } |
| 154 | +} |
0 commit comments