|
| 1 | +using SignsOfAI.Core; |
| 2 | +using SignsOfAI.Core.Reporting; |
| 3 | +using SignsOfAI.Core.Rules; |
| 4 | + |
| 5 | +namespace SignsOfAI.Core.Tests; |
| 6 | + |
| 7 | +/// <summary> |
| 8 | +/// The project tells contributors that adding a language is adding a file. These are the tests that |
| 9 | +/// make that true rather than aspirational — and that stop a language without a pack being reported |
| 10 | +/// as though it had one. |
| 11 | +/// </summary> |
| 12 | +public class RulePackLoaderTests |
| 13 | +{ |
| 14 | + [Fact] |
| 15 | + public void Every_pack_file_in_the_project_is_discoverable_without_a_build_edit() |
| 16 | + { |
| 17 | + // The packs were listed one by one in the .csproj, so a translator had to edit the build to |
| 18 | + // be heard at all. A wildcard replaced that; this test is what keeps it a wildcard. |
| 19 | + Assert.Contains("en", RulePackLoader.Languages); |
| 20 | + Assert.Contains("es", RulePackLoader.Languages); |
| 21 | + |
| 22 | + var onDisk = Directory |
| 23 | + .GetFiles(ProjectPacksDirectory(), "rules.*.json") |
| 24 | + .Select(f => Path.GetFileNameWithoutExtension(f)!.Split('.')[1]) |
| 25 | + .Order(StringComparer.Ordinal); |
| 26 | + |
| 27 | + Assert.Equal(onDisk, RulePackLoader.Languages.Order(StringComparer.Ordinal)); |
| 28 | + } |
| 29 | + |
| 30 | + [Fact] |
| 31 | + public void A_language_with_no_pack_says_which_pack_it_actually_used() |
| 32 | + { |
| 33 | + // Silently loading English while the result claimed the requested language turned "nothing |
| 34 | + // fired" into a finding, when nothing had been looked for. |
| 35 | + // |
| 36 | + // "zz" throughout, never a real code: this suite must keep passing on the day somebody |
| 37 | + // contributes the language it uses as its example, and picking "fr" would make a welcome |
| 38 | + // pull request look like a regression. |
| 39 | + var (_, language) = RulePackLoader.Resolve("zz"); |
| 40 | + |
| 41 | + Assert.Equal("en", language); |
| 42 | + Assert.False(RulePackLoader.Available("zz")); |
| 43 | + } |
| 44 | + |
| 45 | + [Fact] |
| 46 | + public void A_language_with_a_pack_reports_itself() |
| 47 | + { |
| 48 | + Assert.Equal("es", RulePackLoader.Resolve("es").Language); |
| 49 | + Assert.True(RulePackLoader.Available("es")); |
| 50 | + } |
| 51 | + |
| 52 | + [Fact] |
| 53 | + public void The_result_keeps_the_two_languages_apart() |
| 54 | + { |
| 55 | + var result = new AiWritingAnalyzer().Analyze("Le texte est court mais suffisant.", "zz"); |
| 56 | + |
| 57 | + // The text is in the language asked for. The rules that read it were not. |
| 58 | + Assert.Equal("zz", result.Language); |
| 59 | + Assert.Equal("en", result.RulePackLanguage); |
| 60 | + } |
| 61 | + |
| 62 | + [Fact] |
| 63 | + public void The_report_refuses_to_present_an_English_reading_as_a_result_in_that_language() |
| 64 | + { |
| 65 | + var result = new AiWritingAnalyzer().Analyze( |
| 66 | + "La rédaction académique exige de la précision et une structure claire.", "zz"); |
| 67 | + |
| 68 | + var report = EvidenceReport.ToMarkdown(result); |
| 69 | + |
| 70 | + Assert.Contains("no rule pack for", report); |
| 71 | + Assert.Contains("nothing was looked for", report); |
| 72 | + } |
| 73 | + |
| 74 | + [Fact] |
| 75 | + public void A_language_that_has_a_pack_carries_no_such_warning() |
| 76 | + { |
| 77 | + var result = new AiWritingAnalyzer().Analyze( |
| 78 | + "La redacción académica exige precisión y una estructura clara.", "es"); |
| 79 | + |
| 80 | + Assert.DoesNotContain("no rule pack for", EvidenceReport.ToMarkdown(result)); |
| 81 | + } |
| 82 | + |
| 83 | + /// <summary>The packs as they sit in the repository, not as the build happened to embed them.</summary> |
| 84 | + private static string ProjectPacksDirectory() |
| 85 | + { |
| 86 | + var dir = new DirectoryInfo(AppContext.BaseDirectory); |
| 87 | + while (dir is not null && !File.Exists(Path.Combine(dir.FullName, "SignsOfAI.slnx"))) |
| 88 | + dir = dir.Parent; |
| 89 | + |
| 90 | + Assert.NotNull(dir); |
| 91 | + return Path.Combine(dir!.FullName, "src", "SignsOfAI.Core", "Rules", "Packs"); |
| 92 | + } |
| 93 | +} |
0 commit comments