Something went wrong. Try again.
.NET ATProtocol Library - Built for Source Gen
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261using System;using System.Globalization;using System.IO;using CarpaNet.BuildTasks;using Xunit;
namespace CarpaNet.UnitTests.BuildTasks;
public class LexiconCacheTests : IDisposable{ private readonly string _tempDir;
public LexiconCacheTests() { _tempDir = Path.Combine(Path.GetTempPath(), "carpanet-test-" + Guid.NewGuid().ToString("N")); Directory.CreateDirectory(_tempDir); }
public void Dispose() { if (Directory.Exists(_tempDir)) Directory.Delete(_tempDir, recursive: true); }
[Fact] public void Store_And_TryGet_RoundTrips() { var cache = new LexiconCache(_tempDir); var nsid = "com.example.myapp.getProfile"; var json = """{"lexicon":1,"id":"com.example.myapp.getProfile"}""";
cache.Store(nsid, json); var result = cache.TryGet(nsid);
Assert.Equal(json, result); }
[Fact] public void TryGet_ReturnsNull_WhenNotCached() { var cache = new LexiconCache(_tempDir); var result = cache.TryGet("com.example.nonexistent");
Assert.Null(result); }
[Fact] public void TryGet_ReturnsNull_WhenExpired() { var cache = new LexiconCache(_tempDir, ttlHours: 1); var nsid = "com.example.myapp.getProfile"; var json = """{"lexicon":1}""";
// Write files manually with old timestamp Directory.CreateDirectory(_tempDir); File.WriteAllText(cache.GetJsonPath(nsid), json); var oldTime = DateTimeOffset.UtcNow.AddHours(-2); File.WriteAllText(cache.GetMetaPath(nsid), oldTime.ToString("O", CultureInfo.InvariantCulture));
var result = cache.TryGet(nsid);
Assert.Null(result); }
[Fact] public void TryGet_ReturnsCached_WhenNotExpired() { var cache = new LexiconCache(_tempDir, ttlHours: 24); var nsid = "com.example.myapp.getProfile"; var json = """{"lexicon":1}""";
cache.Store(nsid, json); var result = cache.TryGet(nsid);
Assert.Equal(json, result); }
[Fact] public void IsCached_ReturnsTrue_WhenValid() { var cache = new LexiconCache(_tempDir); cache.Store("com.example.test", """{"lexicon":1}""");
Assert.True(cache.IsCached("com.example.test")); }
[Fact] public void IsCached_ReturnsFalse_WhenMissing() { var cache = new LexiconCache(_tempDir); Assert.False(cache.IsCached("com.example.missing")); }
[Fact] public void Store_CreatesDirectory_IfNotExists() { var subDir = Path.Combine(_tempDir, "sub", "nested"); var cache = new LexiconCache(subDir); var nsid = "com.example.test";
cache.Store(nsid, """{"lexicon":1}""");
Assert.True(File.Exists(cache.GetJsonPath(nsid))); Assert.True(File.Exists(cache.GetMetaPath(nsid))); }
[Fact] public void GetJsonPath_ReturnsExpectedPath() { var cache = new LexiconCache(_tempDir); var expected = Path.Combine(_tempDir, "com.example.test.json");
Assert.Equal(expected, cache.GetJsonPath("com.example.test")); }
[Fact] public void ZeroTtl_ForcesRefresh() { var cache = new LexiconCache(_tempDir, ttlHours: 0); var nsid = "com.example.test"; cache.Store(nsid, """{"lexicon":1}""");
// TTL of 0 means everything is expired Assert.Null(cache.TryGet(nsid)); }
[Fact] public void StoreAuthorityManifest_And_TryGet_RoundTrips() { var cache = new LexiconCache(_tempDir); var authority = "blog.pckt"; var nsids = new System.Collections.Generic.List<string> { "blog.pckt.block.text", "blog.pckt.block.image", "blog.pckt.content", };
cache.StoreAuthorityManifest(authority, nsids); var result = cache.TryGetAuthorityManifest(authority);
Assert.NotNull(result); Assert.Equal(nsids, result); }
[Fact] public void TryGetAuthorityManifest_ReturnsNull_WhenNotCached() { var cache = new LexiconCache(_tempDir); var result = cache.TryGetAuthorityManifest("com.example.nonexistent");
Assert.Null(result); }
[Fact] public void TryGetAuthorityManifest_ReturnsNull_WhenExpired() { var cache = new LexiconCache(_tempDir, ttlHours: 1); var authority = "blog.pckt";
// Write files manually with old timestamp Directory.CreateDirectory(_tempDir); File.WriteAllText(cache.GetAuthorityJsonPath(authority), """["blog.pckt.content"]"""); var oldTime = DateTimeOffset.UtcNow.AddHours(-2); File.WriteAllText(cache.GetAuthorityMetaPath(authority), oldTime.ToString("O", CultureInfo.InvariantCulture));
var result = cache.TryGetAuthorityManifest(authority);
Assert.Null(result); }
[Fact] public void TryGetAuthorityManifest_ReturnsCached_WhenNotExpired() { var cache = new LexiconCache(_tempDir, ttlHours: 24); var authority = "blog.pckt"; var nsids = new System.Collections.Generic.List<string> { "blog.pckt.content" };
cache.StoreAuthorityManifest(authority, nsids); var result = cache.TryGetAuthorityManifest(authority);
Assert.NotNull(result); Assert.Equal(nsids, result); }
[Fact] public void GetAuthorityJsonPath_ReturnsExpectedPath() { var cache = new LexiconCache(_tempDir); var expected = Path.Combine(_tempDir, "_authority.blog.pckt.json");
Assert.Equal(expected, cache.GetAuthorityJsonPath("blog.pckt")); }
[Fact] public void GetAuthorityMetaPath_ReturnsExpectedPath() { var cache = new LexiconCache(_tempDir); var expected = Path.Combine(_tempDir, "_authority.blog.pckt.meta");
Assert.Equal(expected, cache.GetAuthorityMetaPath("blog.pckt")); }
[Fact] public void StoreAuthorityManifest_EmptyList_RoundTrips() { var cache = new LexiconCache(_tempDir); var authority = "com.empty"; var nsids = new System.Collections.Generic.List<string>();
cache.StoreAuthorityManifest(authority, nsids); var result = cache.TryGetAuthorityManifest(authority);
Assert.NotNull(result); Assert.Empty(result); }
[Fact] public void ZeroTtl_ForcesRefresh_ForAuthorityManifest() { var cache = new LexiconCache(_tempDir, ttlHours: 0); var authority = "blog.pckt"; cache.StoreAuthorityManifest(authority, new System.Collections.Generic.List<string> { "blog.pckt.content" });
Assert.Null(cache.TryGetAuthorityManifest(authority)); }
[Fact] public void AuthorityManifest_WithDid_SanitizesColonsInFilename() { var cache = new LexiconCache(_tempDir); var did = "did:plc:revjuqmkvrw6fnkxppqtszpv";
// Colons should be replaced with underscores in the filename var jsonPath = cache.GetAuthorityJsonPath(did); var metaPath = cache.GetAuthorityMetaPath(did);
Assert.DoesNotContain(":", Path.GetFileName(jsonPath)); Assert.DoesNotContain(":", Path.GetFileName(metaPath)); Assert.Contains("did_plc_", Path.GetFileName(jsonPath)); }
[Fact] public void AuthorityManifest_WithDid_RoundTrips() { var cache = new LexiconCache(_tempDir); var did = "did:plc:revjuqmkvrw6fnkxppqtszpv"; var nsids = new System.Collections.Generic.List<string> { "blog.pckt.block.text", "blog.pckt.content", };
cache.StoreAuthorityManifest(did, nsids); var result = cache.TryGetAuthorityManifest(did);
Assert.NotNull(result); Assert.Equal(nsids, result); }}