/ GUNRPG.Tests / WebClientPwaAssetTests.cs
WebClientPwaAssetTests.cs
1 using System.Text.Json; 2 3 namespace GUNRPG.Tests; 4 5 public sealed class WebClientPwaAssetTests 6 { 7 [Fact] 8 public void Manifest_UsesRelativeStartUrlAndScope_ForSubdirectoryHosting() 9 { 10 using var manifest = JsonDocument.Parse(File.ReadAllText(GetWebClientAssetPath("manifest.webmanifest"))); 11 12 Assert.Equal("./", manifest.RootElement.GetProperty("start_url").GetString()); 13 Assert.Equal("./", manifest.RootElement.GetProperty("scope").GetString()); 14 } 15 16 [Fact] 17 public void IndexHtml_RegistersServiceWorkerRelativeToAppBase() 18 { 19 var indexHtml = File.ReadAllText(GetWebClientAssetPath("index.html")); 20 21 Assert.Contains("navigator.serviceWorker.register('service-worker.js')", indexHtml, StringComparison.Ordinal); 22 Assert.DoesNotContain("navigator.serviceWorker.register('/service-worker.js')", indexHtml, StringComparison.Ordinal); 23 } 24 25 [Fact] 26 public void ServiceWorker_DoesNotCacheRootRelativeContentAssets() 27 { 28 var serviceWorker = File.ReadAllText(GetWebClientAssetPath("service-worker.js")); 29 30 Assert.DoesNotContain("/_content/", serviceWorker, StringComparison.Ordinal); 31 } 32 33 private static string GetWebClientAssetPath(string fileName) 34 { 35 var directory = new DirectoryInfo(AppContext.BaseDirectory); 36 37 while (directory is not null && !File.Exists(Path.Combine(directory.FullName, "global.json"))) 38 { 39 directory = directory.Parent; 40 } 41 42 if (directory is null) 43 { 44 throw new DirectoryNotFoundException("Could not locate the repository root from the test output directory."); 45 } 46 47 return Path.Combine(directory.FullName, "GUNRPG.WebClient", "wwwroot", fileName); 48 } 49 }