using System.Text.RegularExpressions; using Febe.Browser.Targets; namespace Febe.Tests.Browser; [TestClass] public sealed class TargetMapTests { private const string Sample = """ { "site": "prova", "note": "Mappa di esempio", // comments are allowed "targets": { "viewer-next": { "role": "button", "name": "^avanti$" }, "viewer-close": { "role": "button|link", "name": "chiudi", "index": -1 }, "profile-stories": { "role": "link", "href": "/stories/{username}/", "hypothesis": "H11" }, "profile-link": { "href": "/{username}/", "index": 2 } } } """; [TestMethod] public void ParsesRolesNamesShapesAndIndexes() { var map = TargetMap.Parse(Sample, "bersagli.prova.json"); Assert.AreEqual("prova", map.Site); Assert.AreEqual(4, map.Targets.Count); CollectionAssert.AreEqual(new[] { "button", "link" }, map.Get("viewer-close").Roles.ToArray()); Assert.AreEqual(-1, map.Get("viewer-close").Index); Assert.AreEqual("/stories/{username}/", map.Get("profile-stories").HrefPattern); Assert.AreEqual("H11", map.Get("profile-stories").Hypothesis); Assert.AreEqual(2, map.Get("profile-link").Index); Assert.AreEqual(0, map.Get("profile-link").Roles.Count); Assert.ThrowsExactly(() => map.Get("non-esiste")); } [TestMethod] public void LinkShapesBecomeAnchoredPathExpressions() { var anyUser = TargetMap.HrefPatternToRegex("/{username}/"); Assert.AreEqual("^\\/[^/]+\\/$", anyUser); Assert.IsTrue(Regex.IsMatch("/alice/", anyUser)); Assert.IsFalse(Regex.IsMatch("/alice/tagged/", anyUser)); var alice = TargetMap.HrefPatternToRegex("/{username}/", new Dictionary { ["username"] = "a.lice" }); Assert.IsTrue(Regex.IsMatch("/a.lice/", alice)); Assert.IsFalse(Regex.IsMatch("/aXlice/", alice), "Parameter values are escaped."); var stories = TargetMap.HrefPatternToRegex("/stories/**"); Assert.IsTrue(Regex.IsMatch("/stories/alice/123/", stories)); Assert.IsFalse(Regex.IsMatch("/alice/stories/", stories)); var oneSegment = TargetMap.HrefPatternToRegex("/p/*/"); Assert.IsTrue(Regex.IsMatch("/p/ABC123/", oneSegment)); Assert.IsFalse(Regex.IsMatch("/p/ABC/123/", oneSegment)); } [TestMethod] public void NamePatternsTakeEscapedParameters() { var expanded = TargetMap.ExpandNamePattern("^foto profilo di {username}$", new Dictionary { ["username"] = "a.b" }); Assert.IsTrue(Regex.IsMatch("foto profilo di a.b", expanded)); Assert.IsFalse(Regex.IsMatch("foto profilo di aXb", expanded)); Assert.AreEqual("^x .+$", TargetMap.ExpandNamePattern("^x {missing}$", null)); } [TestMethod] [DataRow("""{ "targets": { "a": { "role": "button", "x": 10, "y": 20 } } }""", DisplayName = "Coordinates are refused")] [DataRow("""{ "targets": { "a": { "role": "button", "selector": ".x1ab2c" } } }""", DisplayName = "Class selectors are refused")] [DataRow("""{ "targets": { "a": { "index": 1 } } }""", DisplayName = "Role, name or href is required")] [DataRow("""{ "targets": { "a": { "href": "stories/" } } }""", DisplayName = "href is a path")] [DataRow("""{ "targets": { "A_b": { "role": "button" } } }""", DisplayName = "Identifiers are kebab-case")] [DataRow("""{ "targets": { "a": { "name": "([" } } }""", DisplayName = "Invalid name expression")] [DataRow("""{ "targets": {} }""", DisplayName = "Empty map")] [DataRow("""{ "targets": """, DisplayName = "Broken JSON")] public void InvalidMapsAreRefused(string json) => Assert.ThrowsExactly(() => TargetMap.Parse(json, "bersagli.test.json")); [TestMethod] public void TheExpressionCarriesTheSpecAsJson() { var map = TargetMap.Parse(Sample); var expression = TargetResolver.BuildExpression(map.Get("profile-stories"), new Dictionary { ["username"] = "alice" }); StringAssert.Contains(expression, "\"roles\":[\"link\"]"); StringAssert.Contains(expression, "stories"); Assert.IsFalse(expression.Contains("__SPEC__", StringComparison.Ordinal)); } }