feat: dreport-service + dreport-ffi + nuget packages
Some checks failed
CI / rust (push) Failing after 40s
CI / frontend (push) Failing after 1m53s
CI / wasm (push) Successful in 1m45s
CI / publish-crates (push) Has been skipped
CI / publish-npm (push) Has been skipped

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:
2026-04-28 16:19:47 +03:00
parent 92583141c9
commit 2db5929e39
44 changed files with 3377 additions and 252 deletions

View File

@@ -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>

View 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);
}
}

View File

@@ -0,0 +1,31 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<IsPackable>false</IsPackable>
<LangVersion>latest</LangVersion>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<!-- Tests run on the host RID; ensure native dylibs ship next to the test asm. -->
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Dreport.Service\Dreport.Service.csproj" />
</ItemGroup>
<!-- Sample TTF for FontParseException tests; copied from the workspace assets dir. -->
<ItemGroup>
<None Include="..\..\..\..\dreport-service\assets\fonts\NotoSans-Regular.ttf"
Link="fixtures/NotoSans-Regular.ttf"
CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,236 @@
using System.Text.Json;
using Dreport.Service;
using Xunit;
namespace Dreport.Service.Tests;
public class LayoutEngineTests
{
private const string Template = """
{
"id": "csharp",
"name": "C# 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 from C#"
}
]
}
}
""";
private const string Data = "{}";
private static byte[] LoadFixtureFont() =>
File.ReadAllBytes(Path.Combine(AppContext.BaseDirectory, "fixtures", "NotoSans-Regular.ttf"));
// ---------------------------------------------------------------------
// Lifecycle
// ---------------------------------------------------------------------
[Fact]
public void Construct_DefaultEngine_HasEmbeddedFonts()
{
using var engine = new LayoutEngine();
Assert.True(engine.FontFamilyCount >= 1);
}
[Fact]
public void CreateEmpty_StartsWithoutFonts()
{
using var engine = LayoutEngine.CreateEmpty();
Assert.Equal(0, engine.FontFamilyCount);
}
[Fact]
public void NativeVersion_ReturnsNonEmpty()
{
var v = LayoutEngine.NativeVersion;
Assert.False(string.IsNullOrEmpty(v));
Assert.Contains('.', v);
}
[Fact]
public void Dispose_TwiceIsSafe()
{
var engine = new LayoutEngine();
engine.Dispose();
engine.Dispose();
}
[Fact]
public void Operations_AfterDispose_Throw()
{
var engine = new LayoutEngine();
engine.Dispose();
Assert.Throws<ObjectDisposedException>(() => engine.RenderPdf(Template, Data));
Assert.Throws<ObjectDisposedException>(() => engine.ListFontFamilies());
}
// ---------------------------------------------------------------------
// Font registry
// ---------------------------------------------------------------------
[Fact]
public void RegisterFont_ValidBytes_IncreasesCount()
{
using var engine = LayoutEngine.CreateEmpty();
engine.RegisterFont(LoadFixtureFont());
Assert.Equal(1, engine.FontFamilyCount);
}
[Fact]
public void RegisterFont_InvalidBytes_ThrowsFontParseException()
{
using var engine = LayoutEngine.CreateEmpty();
var ex = Assert.Throws<FontParseException>(() =>
engine.RegisterFont(new byte[] { 1, 2, 3, 4 }));
Assert.Equal(Native.ERR_FONT_PARSE_FAILED, ex.Code);
}
[Fact]
public void RegisterFontsDirectory_NonExisting_ThrowsFontDirectoryNotFound()
{
using var engine = LayoutEngine.CreateEmpty();
Assert.Throws<FontDirectoryNotFoundException>(() =>
engine.RegisterFontsDirectory("/no/such/dreport/test/path/xyz"));
}
[Fact]
public void RegisterFontsDirectory_ValidDir_LoadsCount()
{
var fixturesDir = Path.Combine(AppContext.BaseDirectory, "fixtures");
Assert.True(Directory.Exists(fixturesDir));
using var engine = LayoutEngine.CreateEmpty();
var count = engine.RegisterFontsDirectory(fixturesDir);
Assert.True(count >= 1);
}
[Fact]
public void GetFontBytes_KnownVariant_ReturnsBytes()
{
using var engine = new LayoutEngine();
var bytes = engine.GetFontBytes("Noto Sans", 400, false);
Assert.NotNull(bytes);
Assert.True(bytes!.Length > 1000);
}
[Fact]
public void GetFontBytes_UnknownVariant_ReturnsNull()
{
using var engine = new LayoutEngine();
Assert.Null(engine.GetFontBytes("DoesNotExist", 400, false));
}
[Fact]
public void ListFontFamilies_ContainsNotoSans()
{
using var engine = new LayoutEngine();
var families = engine.ListFontFamilies();
Assert.Contains(families, f => f.Family.ToLowerInvariant().Contains("noto"));
}
// ---------------------------------------------------------------------
// Render pipeline
// ---------------------------------------------------------------------
[Fact]
public void ComputeLayout_ValidInputs_ReturnsLayoutJson()
{
using var engine = new LayoutEngine();
var json = engine.ComputeLayout(Template, Data);
using var doc = JsonDocument.Parse(json);
Assert.True(doc.RootElement.TryGetProperty("pages", out var pages));
Assert.Equal(JsonValueKind.Array, pages.ValueKind);
Assert.True(pages.GetArrayLength() >= 1);
}
[Fact]
public void ComputeLayout_InvalidTemplate_ThrowsInvalidTemplate()
{
using var engine = new LayoutEngine();
Assert.Throws<InvalidTemplateException>(() => engine.ComputeLayout("{not json", Data));
}
[Fact]
public void ComputeLayout_InvalidData_ThrowsInvalidData()
{
using var engine = new LayoutEngine();
Assert.Throws<InvalidDataException>(() => engine.ComputeLayout(Template, "{not json"));
}
[Fact]
public void RenderPdf_ValidInputs_ReturnsPdfBytes()
{
using var engine = new LayoutEngine();
var pdf = engine.RenderPdf(Template, Data);
Assert.True(pdf.Length > 100);
Assert.Equal((byte)'%', pdf[0]);
Assert.Equal((byte)'P', pdf[1]);
Assert.Equal((byte)'D', pdf[2]);
Assert.Equal((byte)'F', pdf[3]);
}
[Fact]
public void RenderPdf_InvalidTemplate_ThrowsInvalidTemplate()
{
using var engine = new LayoutEngine();
Assert.Throws<InvalidTemplateException>(() => engine.RenderPdf("{not json", Data));
}
// ---------------------------------------------------------------------
// Concurrency
// ---------------------------------------------------------------------
[Fact]
public void RenderPdf_Parallel_ProducesPdfs()
{
using var engine = new LayoutEngine();
var success = 0;
Parallel.For(0, 16, _ =>
{
var pdf = engine.RenderPdf(Template, Data);
if (pdf.Length > 100 && pdf[0] == (byte)'%')
{
Interlocked.Increment(ref success);
}
});
Assert.Equal(16, success);
}
// ---------------------------------------------------------------------
// Error code stability (matches Rust ServiceError::code() contract)
// ---------------------------------------------------------------------
[Fact]
public void ErrorCode_InvalidTemplate_IsMinusOne()
{
var ex = new InvalidTemplateException("x");
Assert.Equal(-1, ex.Code);
}
[Fact]
public void ErrorCode_FontParseFailed_IsMinusThree()
{
var ex = new FontParseException("x");
Assert.Equal(-3, ex.Code);
}
}