|
| 1 | +namespace MoEmbed.CodeGeneration; |
| 2 | + |
| 3 | +public class LongestCommonSubstringLengthTest |
| 4 | +{ |
| 5 | + [Test] |
| 6 | + [Arguments("abc", "abc", 3)] |
| 7 | + [Arguments("abc", "def", 0)] |
| 8 | + [Arguments("abcdef", "xcdey", 3)] // "cde" |
| 9 | + [Arguments("", "abc", 0)] |
| 10 | + [Arguments("abc", "", 0)] |
| 11 | + [Arguments("afreecatv", "vafreeca", 7)] // "afreeca" |
| 12 | + [Arguments("sooplive", "vafreeca", 1)] // "a" |
| 13 | + public async Task ReturnsExpectedLength(string a, string b, int expected) |
| 14 | + { |
| 15 | + var result = HostNameResolver.LongestCommonSubstringLength(a, b); |
| 16 | + await Assert.That(result).IsEqualTo(expected); |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +public class HostNameSimilarityTest |
| 21 | +{ |
| 22 | + [Test] |
| 23 | + [Arguments("afreecatv", "v.afree.ca", 7)] // normalized: "afreecatv" vs "vafreeca" -> "afreeca" (7) |
| 24 | + [Arguments("sooplive", "v.afree.ca", 1)] // normalized: "sooplive" vs "vafreeca" -> "a" (1) |
| 25 | + [Arguments("sooplive", "vod.sooplive.com", 8)] // normalized: "sooplive" vs "vodsooplivecom" -> "sooplive" (8) |
| 26 | + [Arguments("afreecatv", "vod.afreecatv.com", 9)] // normalized: "afreecatv" vs "vodafreecatvcom" -> "afreecatv" (9) |
| 27 | + [Arguments("my-service", "my-service.example.com", 9)] // hyphens stripped from both |
| 28 | + public async Task ReturnsExpectedSimilarity(string providerName, string hostName, int expected) |
| 29 | + { |
| 30 | + var result = HostNameResolver.HostNameSimilarity(providerName, hostName); |
| 31 | + await Assert.That(result).IsEqualTo(expected); |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +public class HostNameResolverTest |
| 36 | +{ |
| 37 | + [Test] |
| 38 | + public async Task NoConflict_AllHostNamesPreserved() |
| 39 | + { |
| 40 | + var entries = new[] |
| 41 | + { |
| 42 | + new ProviderHostEntry { ProviderName = "youtube", CandidateHostNames = ["youtube.com", "youtu.be"] }, |
| 43 | + new ProviderHostEntry { ProviderName = "vimeo", CandidateHostNames = ["vimeo.com"] } |
| 44 | + }; |
| 45 | + |
| 46 | + var result = HostNameResolver.Resolve(entries); |
| 47 | + |
| 48 | + await Assert.That(result["youtube"]).IsEquivalentTo(new[] { "youtube.com", "youtu.be" }); |
| 49 | + await Assert.That(result["vimeo"]).IsEquivalentTo(new[] { "vimeo.com" }); |
| 50 | + } |
| 51 | + |
| 52 | + [Test] |
| 53 | + public async Task ConflictingHost_AssignedToMoreSimilarProvider() |
| 54 | + { |
| 55 | + // Real-world case: afreecatv and sooplive both claim v.afree.ca |
| 56 | + var entries = new[] |
| 57 | + { |
| 58 | + new ProviderHostEntry |
| 59 | + { |
| 60 | + ProviderName = "afreecatv", |
| 61 | + CandidateHostNames = ["vod.afreecatv.com", "afreecatv.com", "v.afree.ca", "afree.ca", "play.afreecatv.com"] |
| 62 | + }, |
| 63 | + new ProviderHostEntry |
| 64 | + { |
| 65 | + ProviderName = "sooplive", |
| 66 | + CandidateHostNames = ["vod.sooplive.com", "sooplive.com", "v.afree.ca", "afree.ca", "play.sooplive.com"] |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + var result = HostNameResolver.Resolve(entries); |
| 71 | + |
| 72 | + // v.afree.ca and afree.ca should go to afreecatv (higher similarity) |
| 73 | + await Assert.That(result["afreecatv"]).Contains("v.afree.ca"); |
| 74 | + await Assert.That(result["afreecatv"]).Contains("afree.ca"); |
| 75 | + await Assert.That(result["sooplive"]).DoesNotContain("v.afree.ca"); |
| 76 | + await Assert.That(result["sooplive"]).DoesNotContain("afree.ca"); |
| 77 | + |
| 78 | + // Each provider's own hosts are unaffected |
| 79 | + await Assert.That(result["afreecatv"]).Contains("vod.afreecatv.com"); |
| 80 | + await Assert.That(result["sooplive"]).Contains("vod.sooplive.com"); |
| 81 | + await Assert.That(result["sooplive"]).Contains("play.sooplive.com"); |
| 82 | + } |
| 83 | + |
| 84 | + [Test] |
| 85 | + public async Task ConflictingHost_TieBreaksAlphabetically() |
| 86 | + { |
| 87 | + // Both providers have equal similarity to the shared host |
| 88 | + var entries = new[] |
| 89 | + { |
| 90 | + new ProviderHostEntry |
| 91 | + { |
| 92 | + ProviderName = "bravo", |
| 93 | + CandidateHostNames = ["shared.example.com"] |
| 94 | + }, |
| 95 | + new ProviderHostEntry |
| 96 | + { |
| 97 | + ProviderName = "alpha", |
| 98 | + CandidateHostNames = ["shared.example.com"] |
| 99 | + } |
| 100 | + }; |
| 101 | + |
| 102 | + var result = HostNameResolver.Resolve(entries); |
| 103 | + |
| 104 | + // Equal similarity -> alphabetical tie-break -> "alpha" wins |
| 105 | + await Assert.That(result["alpha"]).Contains("shared.example.com"); |
| 106 | + await Assert.That(result["bravo"]).DoesNotContain("shared.example.com"); |
| 107 | + } |
| 108 | + |
| 109 | + [Test] |
| 110 | + public async Task ThreeProviders_ConflictResolvedCorrectly() |
| 111 | + { |
| 112 | + var entries = new[] |
| 113 | + { |
| 114 | + new ProviderHostEntry |
| 115 | + { |
| 116 | + ProviderName = "foobar", |
| 117 | + CandidateHostNames = ["foobar.com", "api.foo.com"] |
| 118 | + }, |
| 119 | + new ProviderHostEntry |
| 120 | + { |
| 121 | + ProviderName = "foobaz", |
| 122 | + CandidateHostNames = ["foobaz.net", "api.foo.com"] |
| 123 | + }, |
| 124 | + new ProviderHostEntry |
| 125 | + { |
| 126 | + ProviderName = "qux", |
| 127 | + CandidateHostNames = ["qux.io", "api.foo.com"] |
| 128 | + } |
| 129 | + }; |
| 130 | + |
| 131 | + var result = HostNameResolver.Resolve(entries); |
| 132 | + |
| 133 | + // "api.foo.com" -> normalized "apifoocom" |
| 134 | + // "foobar" LCS with "apifoocom" = "foo" (3) |
| 135 | + // "foobaz" LCS with "apifoocom" = "foo" (3) |
| 136 | + // "qux" LCS with "apifoocom" = 0 |
| 137 | + // Tie between foobar and foobaz -> alphabetical -> "foobar" wins |
| 138 | + await Assert.That(result["foobar"]).Contains("api.foo.com"); |
| 139 | + await Assert.That(result["foobaz"]).DoesNotContain("api.foo.com"); |
| 140 | + await Assert.That(result["qux"]).DoesNotContain("api.foo.com"); |
| 141 | + |
| 142 | + // Unique hosts unaffected |
| 143 | + await Assert.That(result["foobar"]).Contains("foobar.com"); |
| 144 | + await Assert.That(result["foobaz"]).Contains("foobaz.net"); |
| 145 | + await Assert.That(result["qux"]).Contains("qux.io"); |
| 146 | + } |
| 147 | + |
| 148 | + [Test] |
| 149 | + public async Task EmptyCandidates_ReturnsEmptyList() |
| 150 | + { |
| 151 | + var entries = new[] |
| 152 | + { |
| 153 | + new ProviderHostEntry { ProviderName = "empty", CandidateHostNames = [] } |
| 154 | + }; |
| 155 | + |
| 156 | + var result = HostNameResolver.Resolve(entries); |
| 157 | + |
| 158 | + await Assert.That(result["empty"]).IsEmpty(); |
| 159 | + } |
| 160 | +} |
0 commit comments