|
| 1 | +using System.Reflection; |
| 2 | +using Xunit; |
| 3 | + |
| 4 | +namespace ShellDocs.Tests; |
| 5 | + |
| 6 | +/* The Tokens RCL ships one static asset: tokens.css. If anyone renames the |
| 7 | + file, changes the package id, or accidentally drops the file from the |
| 8 | + build, these tests fail loud before it hits a consumer. */ |
| 9 | +public class DesignTokensTests |
| 10 | +{ |
| 11 | + private static string LoadTokensCss() |
| 12 | + { |
| 13 | + // MSBuild copies the RCL's static web assets into a predictable location |
| 14 | + // under the test project's output. Walk up from the test assembly to find |
| 15 | + // the package's wwwroot/tokens.css. |
| 16 | + var testAssembly = Assembly.GetExecutingAssembly().Location; |
| 17 | + var testDir = Path.GetDirectoryName(testAssembly)!; |
| 18 | + |
| 19 | + // Traverse the well-known static-web-asset path emitted by the Razor SDK. |
| 20 | + var candidates = new[] |
| 21 | + { |
| 22 | + Path.Combine(testDir, "wwwroot", "_content", "ShellDocs.Tokens", "tokens.css"), |
| 23 | + Path.Combine(testDir, "..", "..", "..", "..", "..", "src", "ShellDocs.Tokens", "wwwroot", "tokens.css") |
| 24 | + }; |
| 25 | + foreach (var c in candidates) |
| 26 | + { |
| 27 | + var full = Path.GetFullPath(c); |
| 28 | + if (File.Exists(full)) return File.ReadAllText(full); |
| 29 | + } |
| 30 | + throw new FileNotFoundException( |
| 31 | + $"tokens.css not found — checked: {string.Join(", ", candidates.Select(Path.GetFullPath))}"); |
| 32 | + } |
| 33 | + |
| 34 | + [Fact] |
| 35 | + public void TokensFile_Exists_AndIsNonEmpty() |
| 36 | + { |
| 37 | + var css = LoadTokensCss(); |
| 38 | + Assert.NotEmpty(css); |
| 39 | + Assert.True(css.Length > 500, "tokens.css looks unexpectedly small"); |
| 40 | + } |
| 41 | + |
| 42 | + [Theory] |
| 43 | + [InlineData("--background")] |
| 44 | + [InlineData("--foreground")] |
| 45 | + [InlineData("--primary")] |
| 46 | + [InlineData("--muted")] |
| 47 | + [InlineData("--accent")] |
| 48 | + [InlineData("--border")] |
| 49 | + [InlineData("--radius")] |
| 50 | + [InlineData("--sidebar-width")] |
| 51 | + [InlineData("--header-height")] |
| 52 | + [InlineData("--font-sans")] |
| 53 | + [InlineData("--font-mono")] |
| 54 | + [InlineData("--info")] |
| 55 | + [InlineData("--warning")] |
| 56 | + [InlineData("--error")] |
| 57 | + [InlineData("--success")] |
| 58 | + public void TokensFile_DefinesStableToken(string token) |
| 59 | + { |
| 60 | + var css = LoadTokensCss(); |
| 61 | + Assert.Contains(token + ":", css); |
| 62 | + } |
| 63 | + |
| 64 | + [Fact] |
| 65 | + public void TokensFile_DefinesLightAndDarkRoots() |
| 66 | + { |
| 67 | + var css = LoadTokensCss(); |
| 68 | + Assert.Contains(":root {", css); |
| 69 | + Assert.Contains(":root.dark {", css); |
| 70 | + } |
| 71 | + |
| 72 | + [Fact] |
| 73 | + public void TokensFile_DarkRoot_OverridesBackground() |
| 74 | + { |
| 75 | + // Both blocks must redefine --background so the .dark class actually flips it. |
| 76 | + var css = LoadTokensCss(); |
| 77 | + var darkStart = css.IndexOf(":root.dark {", StringComparison.Ordinal); |
| 78 | + Assert.True(darkStart > 0); |
| 79 | + var darkBlock = css.Substring(darkStart); |
| 80 | + Assert.Contains("--background:", darkBlock); |
| 81 | + Assert.Contains("--foreground:", darkBlock); |
| 82 | + } |
| 83 | +} |
0 commit comments