Repository navigation
C# module unit testing - #6501
lisandroct wants to merge 31 commits into
Conversation
8d6a5eb to
756a2e3
Compare
6e02c8c to
1fe54be
Compare
f7dab74 to
c73a019
Compare
JasonAtClockwork
left a comment
There was a problem hiding this comment.
I haven't completed a full review but found something important enough to reach out on straight-away. It looks like we have a regression for sub-modules with this change because they are not getting built with the wasm flag so they're becoming managed and then fail during publish.
I had AI whip up a quick small repro to keep it small to describe:
Repro. This is the docs example, inside the project spacetime init --lang csharp creates for .NET 10. Pack the PR's packages into a local feed first:
dotnet pack crates/bindings-csharp/BSATN.Runtime -c Release -p:Version=2.16.0-pr -o feed
dotnet pack crates/bindings-csharp/Runtime -c Release -p:Version=2.16.0-pr -o feedNuGet.config add `feed` as a package source
spacetimedb/
StdbModule.csproj from `spacetime init`, plus the docs' ProjectReference
Lib.cs
libraries/AuthLib/
AuthLib.csproj
Lib.cs
Files
spacetimedb/StdbModule.csproj: spacetime init output with SpacetimeDB.Runtime at 2.16.0-pr, plus the ItemGroup from the docs.
<ItemGroup>
<ProjectReference Include="libraries/AuthLib/AuthLib.csproj" />
<!-- Prevent the root project from also compiling the library's source. -->
<Compile Remove="libraries/**/*.cs" />
</ItemGroup>spacetimedb/Lib.cs
using SpacetimeDB;
[assembly: Namespace(typeof(AuthLib.Marker), Accessor = "MyAuth", Name = "auth_data")]
public static partial class Module
{
[Reducer]
public static void AddAuthUser(ReducerContext ctx, uint id)
{
AuthLib.Functions.Add(ctx.As.MyAuth, id);
Log.Info($"Auth users: {AuthLib.Functions.Count(ctx.As.MyAuth)}");
}
}spacetimedb/libraries/AuthLib/AuthLib.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SpacetimeDB.Runtime" Version="2.16.0-pr" />
</ItemGroup>
</Project>spacetimedb/libraries/AuthLib/Lib.cs
using SpacetimeDB;
namespace AuthLib;
public class Marker { }
[Table(Accessor = "User", Name = "auth_users", Public = true)]
public partial struct User
{
[PrimaryKey]
public uint Id;
public uint Score;
}
public static partial class Functions
{
public static void Insert(ReducerContext ctx, uint id) =>
ctx.Db.User.Insert(new User { Id = id, Score = 42 });
public static ulong Count(ReducerContext ctx) => ctx.Db.User.Count;
[Reducer]
public static void Add(ReducerContext ctx, uint id) => Insert(ctx, id);
}Result
$ spacetime publish -p spacetimedb submodule-docs
...
Build finished successfully.
Error: Local module schema inspection failed:
Error: wasm trap while calling "__preinit__10_init_csharp"
...
9: S_P_CoreLib_Internal_Runtime_CompilerHelpers_ThrowHelpers__ThrowTypeLoadException
10: StdbModule.wasm!AuthLib_ModuleRegistration__Register
11: StdbModule_ModuleRegistration__RegisterModule
79e41d0 to
7f18777
Compare
C# modules can now be unit tested natively. The new SpacetimeDB.Testing package runs reducers, procedures and views against a native test datastore through the spacetimedb-test-datastore-ffi C ABI. Includes codegen and runtime support, the Testing library and its tests, templates, docs, CI packaging and release flow. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Decode each row from a slice of its declared length (FramedRows), so malformed results always throw InvalidDataException. Used by RunQuery and the managed TableAccessor.Iter. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
After an internal error stops the native worker, later calls report INTERNAL_ERROR with the original message (including the panic message) instead of DATASTORE_CLOSED, so C# no longer reports a panic as a disposed datastore. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
HttpClient.Send validates the request and clamps its timeout the same way in both builds, before the managed transaction check, matching WASM. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Enable package validation so pack fails when the WASM assembly lacks API the managed one exposes. Existing intended differences are listed in CompatibilitySuppressions.xml. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5547a26 to
3c8728c
Compare
Description of Changes
Depends on: #6499 and #6500
Adds unit testing for C# modules. Tests call reducers, procedures, views and typed queries directly, in-process, against the real datastore.
New
SpacetimeDB.TestingNuGet package (only .NET 10).New Rust crate
crates/test-datastore-ffi. A cdylib (publish = false) that exposes test-datastore through a versioned C ABI. Important: this was 100% implemented by LLMs.Changes to
Runtimeto . The net10 package now contains two assemblies:In the managed build, table and index handles go through the fixture's transaction. A handle used after its transaction or fixture has ended throws.
Codegenemits the same WASM output whenSPACETIMEDB_WASM_AOTis defined (spacetime publishandspacetime build). When managed (test environment) it generates no module of its own, instead it adds a global using for the root module's namespace.Adds module testing to the
chat-console-cstemplate.Updates
spacetime initto copy the test package from a module only for .NET 10 and sets the SpacetimeDB.Testing version to the Runtime version.Updates documentation.
CI and release (this were also fully implemented by an LLM):
csharp-testing.ymlworkflow builds the native bridge and packs the packages.SpacetimeDB.Testingfrom the same tested package set asSpacetimeDB.RuntimeandSpacetimeDB.BSATN.Runtime.Important note:
test-datastore-ffiwas 100% implemented by LLMs.API and ABI breaking changes
No breaking changes. All the new stuff affects testing only.
Rollback safety impact
n/a.
Expected complexity level and risk
Possibly a 4/5.
#if. A mistake in a conditional block could change WASM output, and the managed-only tests wouldn't notice. It's not the most robust thing in the world. If it starts becoming a problem or a pain to maintain, I'll have to think of something better.Testing
These cover the new Testing project, the release and packaging, the new
test-datastore-ffiand more...