Something went wrong. Try again.
.NET ATProtocol Library - Built for Source Gen
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668using CarpaNet.Generation;using CarpaNet.Models;using Xunit;
namespace CarpaNet.UnitTests.Generation;
public class XrpcEndpointGeneratorTests{ [Fact] public void GenerateQueryEndpoint_WithParameters_EmitsHttpGetAndFromQuery() { var (byNamespace, registry) = CreateQueryWithParameters(); var options = new GeneratorOptions { EmitXrpcEndpoints = true };
var result = XrpcEndpointGenerator.Generate(byNamespace, registry, options);
Assert.Contains("[Microsoft.AspNetCore.Mvc.HttpGet(\"/xrpc/app.bsky.actor.getProfile\")]", result); Assert.Contains("[Microsoft.AspNetCore.Mvc.FromQuery(Name = \"actor\")]", result); Assert.Contains("string actor", result); Assert.Contains("GetProfileAsync", result); }
[Fact] public void GenerateQueryEndpoint_NoParameters_EmitsOnlyCancellationToken() { var (byNamespace, registry) = CreateQueryNoParameters(); var options = new GeneratorOptions { EmitXrpcEndpoints = true };
var result = XrpcEndpointGenerator.Generate(byNamespace, registry, options);
Assert.Contains("[Microsoft.AspNetCore.Mvc.HttpGet(\"/xrpc/app.bsky.actor.getSuggestions\")]", result); Assert.Contains("System.Threading.CancellationToken cancellationToken", result); Assert.DoesNotContain("FromQuery", result); }
[Fact] public void GenerateProcedureEndpoint_WithInputAndOutput_EmitsHttpPostAndFromBody() { var (byNamespace, registry) = CreateProcedureWithInputAndOutput(); var options = new GeneratorOptions { EmitXrpcEndpoints = true };
var result = XrpcEndpointGenerator.Generate(byNamespace, registry, options);
Assert.Contains("[Microsoft.AspNetCore.Mvc.HttpPost(\"/xrpc/com.atproto.repo.createRecord\")]", result); Assert.Contains("[Microsoft.AspNetCore.Mvc.FromBody]", result); Assert.Contains("CreateRecordInput input", result); Assert.Contains("Ok<", result); }
[Fact] public void GenerateProcedureEndpoint_NoOutput_ReturnsOkWithoutGenericType() { var (byNamespace, registry) = CreateProcedureNoOutput(); var options = new GeneratorOptions { EmitXrpcEndpoints = true };
var result = XrpcEndpointGenerator.Generate(byNamespace, registry, options);
Assert.Contains("[Microsoft.AspNetCore.Mvc.HttpPost(\"/xrpc/com.atproto.repo.deleteRecord\")]", result); Assert.Contains("Results<Microsoft.AspNetCore.Http.HttpResults.Ok, CarpaNet.AspNetCore.ATErrorResult>", result); }
[Fact] public void GenerateProcedureEndpoint_NoInput_OmitsFromBody() { var (byNamespace, registry) = CreateProcedureNoInput(); var options = new GeneratorOptions { EmitXrpcEndpoints = true };
var result = XrpcEndpointGenerator.Generate(byNamespace, registry, options);
Assert.Contains("[Microsoft.AspNetCore.Mvc.HttpPost(\"/xrpc/com.atproto.server.refreshSession\")]", result); Assert.DoesNotContain("FromBody", result); Assert.Contains("System.Threading.CancellationToken cancellationToken", result); }
[Fact] public void ControllerGrouping_SameGroup_ProducesOneController() { var (byNamespace, registry) = CreateMultipleEndpointsSameGroup(); var options = new GeneratorOptions { EmitXrpcEndpoints = true };
var result = XrpcEndpointGenerator.Generate(byNamespace, registry, options);
// Should have exactly one ActorController var controllerCount = CountOccurrences(result, "class ActorController"); Assert.Equal(1, controllerCount);
// Both methods should be present Assert.Contains("GetProfileAsync", result); Assert.Contains("GetPreferencesAsync", result); }
[Fact] public void ControllerGrouping_DifferentGroups_ProducesSeparateControllers() { var (byNamespace, registry) = CreateEndpointsDifferentGroups(); var options = new GeneratorOptions { EmitXrpcEndpoints = true };
var result = XrpcEndpointGenerator.Generate(byNamespace, registry, options);
Assert.Contains("class ActorController", result); Assert.Contains("class RepoController", result); }
[Fact] public void EmitXrpcEndpoints_False_GeneratesNothing() { var (byNamespace, registry) = CreateQueryWithParameters(); var options = new GeneratorOptions { EmitXrpcEndpoints = false };
// The Generate method is only called when EmitXrpcEndpoints is true, // but we can verify the option exists Assert.False(options.EmitXrpcEndpoints); }
[Fact] public void RefInputType_ResolvesCorrectly() { var (byNamespace, registry) = CreateProcedureWithRefInput(); var options = new GeneratorOptions { EmitXrpcEndpoints = true };
var result = XrpcEndpointGenerator.Generate(byNamespace, registry, options);
// The ref input should resolve to the referenced type Assert.Contains("FromBody", result); Assert.Contains("input", result); }
[Fact] public void ArrayQueryParameter_GeneratesList() { var (byNamespace, registry) = CreateQueryWithArrayParameter(); var options = new GeneratorOptions { EmitXrpcEndpoints = true };
var result = XrpcEndpointGenerator.Generate(byNamespace, registry, options);
Assert.Contains("System.Collections.Generic.List<string>", result); Assert.Contains("FromQuery", result); }
[Fact] public void GetControllerGroup_ReturnsFirstThreeSegments() { Assert.Equal("app.bsky.actor", XrpcEndpointGenerator.GetControllerGroup("app.bsky.actor.getProfile")); Assert.Equal("com.atproto.server", XrpcEndpointGenerator.GetControllerGroup("com.atproto.server.createSession")); Assert.Null(XrpcEndpointGenerator.GetControllerGroup("too.short")); }
[Fact] public void GetControllerName_ReturnsPascalCaseWithSuffix() { Assert.Equal("ActorController", XrpcEndpointGenerator.GetControllerName("app.bsky.actor")); Assert.Equal("ServerController", XrpcEndpointGenerator.GetControllerName("com.atproto.server")); }
[Fact] public void GetMethodName_ReturnsLastSegmentPascalCase() { Assert.Equal("GetProfile", XrpcEndpointGenerator.GetMethodName("app.bsky.actor.getProfile")); Assert.Equal("CreateRecord", XrpcEndpointGenerator.GetMethodName("com.atproto.repo.createRecord")); }
[Fact] public void OptionalQueryParameters_AreNullableWithDefault() { var (byNamespace, registry) = CreateQueryWithOptionalParameters(); var options = new GeneratorOptions { EmitXrpcEndpoints = true };
var result = XrpcEndpointGenerator.Generate(byNamespace, registry, options);
Assert.Contains("long?", result); Assert.Contains("= default", result); }
[Fact] public void ControllerNamespace_IncludesXrpcPrefix() { var (byNamespace, registry) = CreateQueryWithParameters(); var options = new GeneratorOptions { EmitXrpcEndpoints = true };
var result = XrpcEndpointGenerator.Generate(byNamespace, registry, options);
Assert.Contains("namespace Xrpc.AppBsky.Actor", result); }
[Fact] public void Controller_IsAbstractPartial() { var (byNamespace, registry) = CreateQueryWithParameters(); var options = new GeneratorOptions { EmitXrpcEndpoints = true };
var result = XrpcEndpointGenerator.Generate(byNamespace, registry, options);
Assert.Contains("public abstract partial class ActorController", result); }
#region Test Helpers
private static (Dictionary<string, List<(string Nsid, LexiconDocument Doc)>>, TypeRegistry) CreateQueryWithParameters() { var registry = new TypeRegistry(); var doc = new LexiconDocument { Id = "app.bsky.actor.getProfile", Defs = new Dictionary<string, LexiconDefinition> { ["main"] = new LexiconDefinition { Type = "query", Description = "Get detailed profile view of an actor.", Parameters = new LexiconDefinition { Type = "params", Properties = new Dictionary<string, LexiconDefinition> { ["actor"] = new LexiconDefinition { Type = "string", Format = "at-identifier" } }, RequiredRaw = CreateJsonArray("actor"), }, Output = new LexiconIO { Encoding = "application/json", Schema = new LexiconDefinition { Type = "object", Properties = new Dictionary<string, LexiconDefinition> { ["did"] = new LexiconDefinition { Type = "string", Format = "did" }, ["handle"] = new LexiconDefinition { Type = "string", Format = "handle" }, } } } } } };
registry.RegisterDocument(doc); var byNamespace = GroupByNamespace(doc, registry); return (byNamespace, registry); }
private static (Dictionary<string, List<(string Nsid, LexiconDocument Doc)>>, TypeRegistry) CreateQueryNoParameters() { var registry = new TypeRegistry(); var doc = new LexiconDocument { Id = "app.bsky.actor.getSuggestions", Defs = new Dictionary<string, LexiconDefinition> { ["main"] = new LexiconDefinition { Type = "query", Output = new LexiconIO { Encoding = "application/json", Schema = new LexiconDefinition { Type = "object", Properties = new Dictionary<string, LexiconDefinition> { ["actors"] = new LexiconDefinition { Type = "array", Items = new LexiconDefinition { Type = "string" } } } } } } } };
registry.RegisterDocument(doc); var byNamespace = GroupByNamespace(doc, registry); return (byNamespace, registry); }
private static (Dictionary<string, List<(string Nsid, LexiconDocument Doc)>>, TypeRegistry) CreateProcedureWithInputAndOutput() { var registry = new TypeRegistry(); var doc = new LexiconDocument { Id = "com.atproto.repo.createRecord", Defs = new Dictionary<string, LexiconDefinition> { ["main"] = new LexiconDefinition { Type = "procedure", Input = new LexiconIO { Encoding = "application/json", Schema = new LexiconDefinition { Type = "object", Properties = new Dictionary<string, LexiconDefinition> { ["repo"] = new LexiconDefinition { Type = "string", Format = "at-identifier" }, ["collection"] = new LexiconDefinition { Type = "string", Format = "nsid" }, } } }, Output = new LexiconIO { Encoding = "application/json", Schema = new LexiconDefinition { Type = "object", Properties = new Dictionary<string, LexiconDefinition> { ["uri"] = new LexiconDefinition { Type = "string", Format = "at-uri" }, ["cid"] = new LexiconDefinition { Type = "string", Format = "cid" }, } } } } } };
registry.RegisterDocument(doc); var byNamespace = GroupByNamespace(doc, registry); return (byNamespace, registry); }
private static (Dictionary<string, List<(string Nsid, LexiconDocument Doc)>>, TypeRegistry) CreateProcedureNoOutput() { var registry = new TypeRegistry(); var doc = new LexiconDocument { Id = "com.atproto.repo.deleteRecord", Defs = new Dictionary<string, LexiconDefinition> { ["main"] = new LexiconDefinition { Type = "procedure", Input = new LexiconIO { Encoding = "application/json", Schema = new LexiconDefinition { Type = "object", Properties = new Dictionary<string, LexiconDefinition> { ["repo"] = new LexiconDefinition { Type = "string", Format = "at-identifier" }, } } } } } };
registry.RegisterDocument(doc); var byNamespace = GroupByNamespace(doc, registry); return (byNamespace, registry); }
private static (Dictionary<string, List<(string Nsid, LexiconDocument Doc)>>, TypeRegistry) CreateProcedureNoInput() { var registry = new TypeRegistry(); var doc = new LexiconDocument { Id = "com.atproto.server.refreshSession", Defs = new Dictionary<string, LexiconDefinition> { ["main"] = new LexiconDefinition { Type = "procedure", Output = new LexiconIO { Encoding = "application/json", Schema = new LexiconDefinition { Type = "object", Properties = new Dictionary<string, LexiconDefinition> { ["accessJwt"] = new LexiconDefinition { Type = "string" }, ["refreshJwt"] = new LexiconDefinition { Type = "string" }, } } } } } };
registry.RegisterDocument(doc); var byNamespace = GroupByNamespace(doc, registry); return (byNamespace, registry); }
private static (Dictionary<string, List<(string Nsid, LexiconDocument Doc)>>, TypeRegistry) CreateMultipleEndpointsSameGroup() { var registry = new TypeRegistry(); var doc1 = new LexiconDocument { Id = "app.bsky.actor.getProfile", Defs = new Dictionary<string, LexiconDefinition> { ["main"] = new LexiconDefinition { Type = "query", Parameters = new LexiconDefinition { Type = "params", Properties = new Dictionary<string, LexiconDefinition> { ["actor"] = new LexiconDefinition { Type = "string" } }, RequiredRaw = CreateJsonArray("actor"), }, Output = new LexiconIO { Encoding = "application/json", Schema = new LexiconDefinition { Type = "object", Properties = new() } } } } };
var doc2 = new LexiconDocument { Id = "app.bsky.actor.getPreferences", Defs = new Dictionary<string, LexiconDefinition> { ["main"] = new LexiconDefinition { Type = "query", Output = new LexiconIO { Encoding = "application/json", Schema = new LexiconDefinition { Type = "object", Properties = new() } } } } };
registry.RegisterDocument(doc1); registry.RegisterDocument(doc2); var byNamespace = GroupByNamespace(new[] { doc1, doc2 }, registry); return (byNamespace, registry); }
private static (Dictionary<string, List<(string Nsid, LexiconDocument Doc)>>, TypeRegistry) CreateEndpointsDifferentGroups() { var registry = new TypeRegistry(); var doc1 = new LexiconDocument { Id = "app.bsky.actor.getProfile", Defs = new Dictionary<string, LexiconDefinition> { ["main"] = new LexiconDefinition { Type = "query", Parameters = new LexiconDefinition { Type = "params", Properties = new Dictionary<string, LexiconDefinition> { ["actor"] = new LexiconDefinition { Type = "string" } }, RequiredRaw = CreateJsonArray("actor"), }, Output = new LexiconIO { Encoding = "application/json", Schema = new LexiconDefinition { Type = "object", Properties = new() } } } } };
var doc2 = new LexiconDocument { Id = "com.atproto.repo.createRecord", Defs = new Dictionary<string, LexiconDefinition> { ["main"] = new LexiconDefinition { Type = "procedure", Input = new LexiconIO { Encoding = "application/json", Schema = new LexiconDefinition { Type = "object", Properties = new Dictionary<string, LexiconDefinition> { ["repo"] = new LexiconDefinition { Type = "string" } } } }, Output = new LexiconIO { Encoding = "application/json", Schema = new LexiconDefinition { Type = "object", Properties = new() } } } } };
registry.RegisterDocument(doc1); registry.RegisterDocument(doc2); var byNamespace = GroupByNamespace(new[] { doc1, doc2 }, registry); return (byNamespace, registry); }
private static (Dictionary<string, List<(string Nsid, LexiconDocument Doc)>>, TypeRegistry) CreateProcedureWithRefInput() { var registry = new TypeRegistry();
// Register the referenced type first var defsDoc = new LexiconDocument { Id = "com.atproto.repo.defs", Defs = new Dictionary<string, LexiconDefinition> { ["recordInput"] = new LexiconDefinition { Type = "object", Properties = new Dictionary<string, LexiconDefinition> { ["repo"] = new LexiconDefinition { Type = "string" } } } } };
var doc = new LexiconDocument { Id = "com.atproto.repo.applyWrites", Defs = new Dictionary<string, LexiconDefinition> { ["main"] = new LexiconDefinition { Type = "procedure", Input = new LexiconIO { Encoding = "application/json", Schema = new LexiconDefinition { Type = "ref", Ref = "com.atproto.repo.defs#recordInput" } }, Output = new LexiconIO { Encoding = "application/json", Schema = new LexiconDefinition { Type = "object", Properties = new() } } } } };
registry.RegisterDocument(defsDoc); registry.RegisterDocument(doc); var byNamespace = GroupByNamespace(new[] { defsDoc, doc }, registry); return (byNamespace, registry); }
private static (Dictionary<string, List<(string Nsid, LexiconDocument Doc)>>, TypeRegistry) CreateQueryWithArrayParameter() { var registry = new TypeRegistry(); var doc = new LexiconDocument { Id = "app.bsky.actor.getProfiles", Defs = new Dictionary<string, LexiconDefinition> { ["main"] = new LexiconDefinition { Type = "query", Parameters = new LexiconDefinition { Type = "params", Properties = new Dictionary<string, LexiconDefinition> { ["actors"] = new LexiconDefinition { Type = "array", Items = new LexiconDefinition { Type = "string", Format = "at-identifier" } } }, RequiredRaw = CreateJsonArray("actors"), }, Output = new LexiconIO { Encoding = "application/json", Schema = new LexiconDefinition { Type = "object", Properties = new() } } } } };
registry.RegisterDocument(doc); var byNamespace = GroupByNamespace(doc, registry); return (byNamespace, registry); }
private static (Dictionary<string, List<(string Nsid, LexiconDocument Doc)>>, TypeRegistry) CreateQueryWithOptionalParameters() { var registry = new TypeRegistry(); var doc = new LexiconDocument { Id = "app.bsky.feed.getTimeline", Defs = new Dictionary<string, LexiconDefinition> { ["main"] = new LexiconDefinition { Type = "query", Parameters = new LexiconDefinition { Type = "params", Properties = new Dictionary<string, LexiconDefinition> { ["limit"] = new LexiconDefinition { Type = "integer" }, ["cursor"] = new LexiconDefinition { Type = "string" }, }, }, Output = new LexiconIO { Encoding = "application/json", Schema = new LexiconDefinition { Type = "object", Properties = new() } } } } };
registry.RegisterDocument(doc); var byNamespace = GroupByNamespace(doc, registry); return (byNamespace, registry); }
private static Dictionary<string, List<(string Nsid, LexiconDocument Doc)>> GroupByNamespace( LexiconDocument doc, TypeRegistry registry) { return GroupByNamespace(new[] { doc }, registry); }
private static Dictionary<string, List<(string Nsid, LexiconDocument Doc)>> GroupByNamespace( LexiconDocument[] docs, TypeRegistry registry) { var result = new Dictionary<string, List<(string, LexiconDocument)>>(); foreach (var doc in docs) { var ns = Utilities.NsidHelper.ToNamespace(doc.Id); if (!result.TryGetValue(ns, out var list)) { list = new List<(string, LexiconDocument)>(); result[ns] = list; } list.Add((doc.Id, doc)); } return result; }
private static System.Text.Json.JsonElement CreateJsonArray(params string[] values) { var json = System.Text.Json.JsonSerializer.Serialize(values); return System.Text.Json.JsonDocument.Parse(json).RootElement.Clone(); }
private static int CountOccurrences(string source, string substring) { int count = 0; int index = 0; while ((index = source.IndexOf(substring, index, StringComparison.Ordinal)) != -1) { count++; index += substring.Length; } return count; }
#endregion}