mirror of
https://github.com/duhanbalci/dreport.git
synced 2026-07-02 02:49:16 +00:00
feat: dreport-service + dreport-ffi + nuget packages
Extract orchestration (font registry + render pipeline) from the Axum backend into a standalone dreport-service crate. Backend becomes a thin HTTP adapter on top. Add dreport-ffi (cdylib) exposing the service through a stable C ABI with opaque handles, byte buffers, and thread-local error reporting. Build Dreport.Service + Dreport.AspNetCore NuGet packages under bindings/dotnet/, packing the host RID native binary via a generated nuspec. justfile recipes (nuget-publish, nuget-publish-all) build, pack, and push to the Gitea NuGet registry in one shot. Test coverage: 47 Rust + 38 C# (xUnit + WebApplicationFactory). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<IsPackable>false</IsPackable>
|
||||
<LangVersion>latest</LangVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="9.0.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.2" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Dreport.AspNetCore\Dreport.AspNetCore.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
173
bindings/dotnet/tests/Dreport.AspNetCore.Tests/EndpointTests.cs
Normal file
173
bindings/dotnet/tests/Dreport.AspNetCore.Tests/EndpointTests.cs
Normal file
@@ -0,0 +1,173 @@
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using Dreport.AspNetCore;
|
||||
using Dreport.Service;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc.Testing;
|
||||
using Microsoft.AspNetCore.TestHost;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Xunit;
|
||||
|
||||
namespace Dreport.AspNetCore.Tests;
|
||||
|
||||
/// <summary>
|
||||
/// Spins up an in-memory ASP.NET Core host for each test using TestServer
|
||||
/// directly, so we don't need a Program.cs entry point. Verifies the
|
||||
/// stock /api endpoints behave the same as the original Axum backend.
|
||||
/// </summary>
|
||||
public class EndpointTests
|
||||
{
|
||||
private const string Template = """
|
||||
{
|
||||
"id": "aspnet-test",
|
||||
"name": "AspNet Test",
|
||||
"page": { "width": 210, "height": 297 },
|
||||
"fonts": ["Noto Sans"],
|
||||
"root": {
|
||||
"id": "root",
|
||||
"type": "container",
|
||||
"position": { "type": "flow" },
|
||||
"size": { "width": { "type": "auto" }, "height": { "type": "auto" } },
|
||||
"direction": "column",
|
||||
"gap": 5,
|
||||
"padding": { "top": 15, "right": 15, "bottom": 15, "left": 15 },
|
||||
"align": "stretch",
|
||||
"justify": "start",
|
||||
"style": {},
|
||||
"children": [
|
||||
{
|
||||
"id": "title",
|
||||
"type": "static_text",
|
||||
"position": { "type": "flow" },
|
||||
"size": { "width": { "type": "auto" }, "height": { "type": "auto" } },
|
||||
"style": { "fontSize": 14, "fontWeight": "bold" },
|
||||
"content": "Hello"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
private static HttpClient Build(string prefix = "/api")
|
||||
{
|
||||
var builder = new WebHostBuilder()
|
||||
.ConfigureServices(s => s.AddRouting().AddDreport())
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UseRouting();
|
||||
app.UseEndpoints(e => e.MapDreportEndpoints(prefix));
|
||||
});
|
||||
var server = new TestServer(builder);
|
||||
return server.CreateClient();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Health_Returns_Ok()
|
||||
{
|
||||
var client = Build();
|
||||
var resp = await client.GetAsync("/api/health");
|
||||
Assert.Equal(HttpStatusCode.OK, resp.StatusCode);
|
||||
var json = await resp.Content.ReadFromJsonAsync<JsonElement>();
|
||||
Assert.Equal("ok", json.GetProperty("status").GetString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Render_ReturnsPdf()
|
||||
{
|
||||
var client = Build();
|
||||
var payload = new { template = JsonDocument.Parse(Template).RootElement, data = new { } };
|
||||
var resp = await client.PostAsJsonAsync("/api/render", payload);
|
||||
Assert.Equal(HttpStatusCode.OK, resp.StatusCode);
|
||||
Assert.Equal("application/pdf", resp.Content.Headers.ContentType?.MediaType);
|
||||
var bytes = await resp.Content.ReadAsByteArrayAsync();
|
||||
Assert.True(bytes.Length > 100);
|
||||
Assert.Equal((byte)'%', bytes[0]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Render_InvalidTemplate_Returns400()
|
||||
{
|
||||
var client = Build();
|
||||
var payload = new { template = "not a template", data = new { } };
|
||||
var resp = await client.PostAsJsonAsync("/api/render", payload);
|
||||
Assert.Equal(HttpStatusCode.BadRequest, resp.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task Layout_ReturnsJson()
|
||||
{
|
||||
var client = Build();
|
||||
var payload = new { template = JsonDocument.Parse(Template).RootElement, data = new { } };
|
||||
var resp = await client.PostAsJsonAsync("/api/layout", payload);
|
||||
Assert.Equal(HttpStatusCode.OK, resp.StatusCode);
|
||||
var json = await resp.Content.ReadFromJsonAsync<JsonElement>();
|
||||
Assert.True(json.TryGetProperty("pages", out var pages));
|
||||
Assert.Equal(JsonValueKind.Array, pages.ValueKind);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ListFonts_IncludesNotoSans()
|
||||
{
|
||||
var client = Build();
|
||||
var resp = await client.GetAsync("/api/fonts");
|
||||
Assert.Equal(HttpStatusCode.OK, resp.StatusCode);
|
||||
var families = await resp.Content.ReadFromJsonAsync<JsonElement[]>();
|
||||
Assert.NotNull(families);
|
||||
Assert.Contains(families!, f => f.GetProperty("family").GetString()!.ToLowerInvariant().Contains("noto"));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetFontBytes_KnownVariant_ReturnsTtf()
|
||||
{
|
||||
var client = Build();
|
||||
var resp = await client.GetAsync("/api/fonts/Noto%20Sans/400/false");
|
||||
Assert.Equal(HttpStatusCode.OK, resp.StatusCode);
|
||||
Assert.Equal("font/ttf", resp.Content.Headers.ContentType?.MediaType);
|
||||
var bytes = await resp.Content.ReadAsByteArrayAsync();
|
||||
Assert.True(bytes.Length > 1000);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task GetFontBytes_Unknown_Returns404()
|
||||
{
|
||||
var client = Build();
|
||||
var resp = await client.GetAsync("/api/fonts/DoesNotExist/400/false");
|
||||
Assert.Equal(HttpStatusCode.NotFound, resp.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task CustomPrefix_RemapsAllEndpoints()
|
||||
{
|
||||
var client = Build("/dreport/api");
|
||||
var resp = await client.GetAsync("/dreport/api/health");
|
||||
Assert.Equal(HttpStatusCode.OK, resp.StatusCode);
|
||||
var oldRoute = await client.GetAsync("/api/health");
|
||||
Assert.Equal(HttpStatusCode.NotFound, oldRoute.StatusCode);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public async Task ManualUsage_LayoutEngine_FromDi()
|
||||
{
|
||||
// Sanity: AddDreport without MapDreportEndpoints still hands the engine
|
||||
// out via DI so users can plug it into their own controllers.
|
||||
var builder = new WebHostBuilder()
|
||||
.ConfigureServices(s => s.AddRouting().AddDreport())
|
||||
.Configure(app =>
|
||||
{
|
||||
app.UseRouting();
|
||||
app.UseEndpoints(e => e.MapGet("/custom",
|
||||
(LayoutEngine engine) => Results.Json(new { count = engine.FontFamilyCount })));
|
||||
});
|
||||
using var server = new TestServer(builder);
|
||||
var client = server.CreateClient();
|
||||
var resp = await client.GetAsync("/custom");
|
||||
Assert.Equal(HttpStatusCode.OK, resp.StatusCode);
|
||||
var json = await resp.Content.ReadFromJsonAsync<JsonElement>();
|
||||
Assert.True(json.GetProperty("count").GetInt32() >= 1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user