diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 148979c886b..ae897cfe707 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -526,6 +526,34 @@ jobs: - name: Run bindgen tests run: cargo ci wasm-bindings + portable_datastore: + name: Check portable datastore + runs-on: spacetimedb-new-runner-2 + env: + CARGO_TARGET_DIR: ${{ github.workspace }}/target + steps: + - uses: actions/checkout@v3 + + - uses: dsherret/rust-toolchain-file@v1 + - name: Set default rust toolchain + run: rustup default $(rustup show active-toolchain | cut -d' ' -f1) + - run: echo ::add-matcher::.github/workflows/rust_matcher.json + + - name: Install wasm target + run: rustup target add wasm32-unknown-unknown + + - name: Cache Rust dependencies + uses: Swatinem/rust-cache@v2 + with: + workspaces: ${{ github.workspace }} + shared-key: spacetimedb + # Let the smoketests job save the cache since it builds the most things + save-if: false + prefix-key: v1 + + - name: Run portable datastore checks + run: cargo ci portable-datastore + publish_checks: needs: [merge_queue_noop] if: ${{ needs.merge_queue_noop.outputs.skip != 'true' }} diff --git a/Cargo.lock b/Cargo.lock index 846dcfab3cd..95467982b49 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7930,11 +7930,13 @@ dependencies = [ "rand 0.8.5", "scoped-tls", "serde_json", + "spacetimedb-auth", "spacetimedb-bindings-macro", "spacetimedb-bindings-sys", "spacetimedb-lib", "spacetimedb-primitives", "spacetimedb-query-builder", + "spacetimedb-test-datastore", "trybuild", ] @@ -8666,6 +8668,33 @@ dependencies = [ "spacetimedb-table", ] +[[package]] +name = "spacetimedb-portable-datastore" +version = "2.7.0" +dependencies = [ + "anyhow", + "serde_json", + "spacetimedb-datastore", + "spacetimedb-expr", + "spacetimedb-lib", + "spacetimedb-primitives", + "spacetimedb-query", + "spacetimedb-schema", + "spacetimedb-table", + "thiserror 1.0.69", +] + +[[package]] +name = "spacetimedb-portable-datastore-wasm" +version = "2.7.0" +dependencies = [ + "js-sys", + "spacetimedb-lib", + "spacetimedb-portable-datastore", + "spacetimedb-primitives", + "wasm-bindgen", +] + [[package]] name = "spacetimedb-primitives" version = "2.7.0" @@ -8992,6 +9021,22 @@ dependencies = [ "thiserror 1.0.69", ] +[[package]] +name = "spacetimedb-test-datastore" +version = "2.7.0" +dependencies = [ + "anyhow", + "scopeguard", + "spacetimedb-core", + "spacetimedb-datastore", + "spacetimedb-expr", + "spacetimedb-lib", + "spacetimedb-primitives", + "spacetimedb-query", + "spacetimedb-schema", + "thiserror 1.0.69", +] + [[package]] name = "spacetimedb-testing" version = "2.7.0" diff --git a/Cargo.toml b/Cargo.toml index 842332340d8..0eb98db6ff5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,8 @@ members = [ "crates/paths", "crates/pg", "crates/physical-plan", + "crates/portable-datastore", + "crates/portable-datastore-wasm", "crates/primitives", "crates/query", "crates/runtime", @@ -39,6 +41,7 @@ members = [ "crates/standalone", "crates/subscription", "crates/table", + "crates/test-datastore", "crates/testing", "crates/update", "modules/benchmarks", @@ -159,6 +162,7 @@ spacetimedb-standalone = { path = "crates/standalone", version = "=2.7.0" } spacetimedb-subscription = { path = "crates/subscription", version = "=2.7.0" } spacetimedb-sql-parser = { path = "crates/sql-parser", version = "=2.7.0" } spacetimedb-table = { path = "crates/table", version = "=2.7.0" } +spacetimedb-test-datastore = { path = "crates/test-datastore", version = "=2.7.0" } # Prevent `ahash` from pulling in `getrandom` by disabling default features. # Modules use `getrandom02` and we need to prevent an incompatible version diff --git a/crates/bindings-csharp/BSATN.Runtime/QueryBuilder.cs b/crates/bindings-csharp/BSATN.Runtime/QueryBuilder.cs index db16bd56cd7..361c42e111b 100644 --- a/crates/bindings-csharp/BSATN.Runtime/QueryBuilder.cs +++ b/crates/bindings-csharp/BSATN.Runtime/QueryBuilder.cs @@ -187,13 +187,13 @@ public IxJoinEq Eq(IxCol other) = public override string ToString() => RefSql; } -public sealed class Table : IQuery +public sealed class TableQuery : IQuery { private readonly string tableName; private readonly TCols cols; private readonly TIxCols ixCols; - public Table(string tableName, TCols cols, TIxCols ixCols) + public TableQuery(string tableName, TCols cols, TIxCols ixCols) { this.tableName = tableName; this.cols = cols; @@ -227,7 +227,7 @@ public LeftSemiJoin L TRightCols, TRightIxCols >( - Table right, + TableQuery right, Func> on ) => new(this, right, on(ixCols, right.ixCols), whereExpr: null); @@ -236,17 +236,17 @@ public RightSemiJoin TRightCols, TRightIxCols >( - Table right, + TableQuery right, Func> on ) => new(this, right, on(ixCols, right.ixCols), leftWhereExpr: null); } public sealed class FromWhere : IQuery { - private readonly Table table; + private readonly TableQuery table; private readonly BoolExpr expr; - internal FromWhere(Table table, BoolExpr expr) + internal FromWhere(TableQuery table, BoolExpr expr) { this.table = table; this.expr = expr; @@ -274,7 +274,7 @@ public LeftSemiJoin L TRightCols, TRightIxCols >( - Table right, + TableQuery right, Func> on ) => new(table, right, on(table.IxCols, right.IxCols), expr); @@ -283,7 +283,7 @@ public RightSemiJoin TRightCols, TRightIxCols >( - Table right, + TableQuery right, Func> on ) => new(table, right, on(table.IxCols, right.IxCols), expr); } @@ -297,15 +297,15 @@ public sealed class LeftSemiJoin< TRightIxCols > : IQuery { - private readonly Table left; - private readonly Table right; + private readonly TableQuery left; + private readonly TableQuery right; private readonly string leftJoinRefSql; private readonly string rightJoinRefSql; private readonly BoolExpr? whereExpr; internal LeftSemiJoin( - Table left, - Table right, + TableQuery left, + TableQuery right, IxJoinEq join, BoolExpr? whereExpr ) @@ -408,16 +408,16 @@ public sealed class RightSemiJoin< TRightIxCols > : IQuery { - private readonly Table left; - private readonly Table right; + private readonly TableQuery left; + private readonly TableQuery right; private readonly string leftJoinRefSql; private readonly string rightJoinRefSql; private readonly BoolExpr? leftWhereExpr; private readonly BoolExpr? rightWhereExpr; internal RightSemiJoin( - Table left, - Table right, + TableQuery left, + TableQuery right, IxJoinEq join, BoolExpr? leftWhereExpr, BoolExpr? rightWhereExpr @@ -432,8 +432,8 @@ internal RightSemiJoin( } internal RightSemiJoin( - Table left, - Table right, + TableQuery left, + TableQuery right, IxJoinEq join, BoolExpr? leftWhereExpr ) diff --git a/crates/bindings-csharp/Codegen.Tests/fixtures/client/Lib.cs b/crates/bindings-csharp/Codegen.Tests/fixtures/client/Lib.cs index 9dc03ecd7f6..29ddaab8e29 100644 --- a/crates/bindings-csharp/Codegen.Tests/fixtures/client/Lib.cs +++ b/crates/bindings-csharp/Codegen.Tests/fixtures/client/Lib.cs @@ -110,10 +110,10 @@ public PublicTableIxCols(string tableName) } } - private static Table MakeTable() + private static TableQuery MakeTable() { const string tableName = "PublicTable"; - return new Table( + return new TableQuery( tableName, new PublicTableCols(tableName), new PublicTableIxCols(tableName) diff --git a/crates/bindings-csharp/Codegen.Tests/fixtures/diag/snapshots/ExtraCompilationErrors.verified.txt b/crates/bindings-csharp/Codegen.Tests/fixtures/diag/snapshots/ExtraCompilationErrors.verified.txt index 8d65f041f30..fe3df4fc7b6 100644 --- a/crates/bindings-csharp/Codegen.Tests/fixtures/diag/snapshots/ExtraCompilationErrors.verified.txt +++ b/crates/bindings-csharp/Codegen.Tests/fixtures/diag/snapshots/ExtraCompilationErrors.verified.txt @@ -345,7 +345,7 @@ SpacetimeDB.Internal.Module.RegisterClientVisibilityFilter(global::Module.MY_THI } }, {/* - public global::SpacetimeDB.Table TestDuplicateTableName() => + public global::SpacetimeDB.TableQuery TestDuplicateTableName() => new("TestDuplicateTableName", new TestDuplicateTableNameCols("TestDuplicateTableName"), new TestDuplicateTableNameIxCols("TestDuplicateTableName")); ^^^^^^^^^^^^^^^^^^^^^^^^^^ } @@ -368,7 +368,7 @@ SpacetimeDB.Internal.Module.RegisterClientVisibilityFilter(global::Module.MY_THI } }, {/* - public global::SpacetimeDB.Table TestDuplicateTableName() => + public global::SpacetimeDB.TableQuery TestDuplicateTableName() => new("TestDuplicateTableName", new TestDuplicateTableNameCols("TestDuplicateTableName"), new TestDuplicateTableNameIxCols("TestDuplicateTableName")); ^^^^^^^^^^^^^^^^^^^^^^^^^^ } @@ -391,7 +391,7 @@ SpacetimeDB.Internal.Module.RegisterClientVisibilityFilter(global::Module.MY_THI } }, {/* - public global::SpacetimeDB.Table TestDuplicateTableName() => + public global::SpacetimeDB.TableQuery TestDuplicateTableName() => new("TestDuplicateTableName", new TestDuplicateTableNameCols("TestDuplicateTableName"), new TestDuplicateTableNameIxCols("TestDuplicateTableName")); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ } @@ -414,7 +414,7 @@ SpacetimeDB.Internal.Module.RegisterClientVisibilityFilter(global::Module.MY_THI } }, {/* - public global::SpacetimeDB.Table TestDuplicateTableName() => + public global::SpacetimeDB.TableQuery TestDuplicateTableName() => new("TestDuplicateTableName", new TestDuplicateTableNameCols("TestDuplicateTableName"), new TestDuplicateTableNameIxCols("TestDuplicateTableName")); ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ } @@ -576,8 +576,8 @@ public readonly struct TestDuplicateTableNameIxCols }, {/* { - public global::SpacetimeDB.Table TestDuplicateTableName() => - ^^^^^^^^^^^^^^^^^^^^^^ + public global::SpacetimeDB.TableQuery TestDuplicateTableName() => + ^^^^^^^^^^^^^^^^^^^^^^ new("TestDuplicateTableName", new TestDuplicateTableNameCols("TestDuplicateTableName"), new TestDuplicateTableNameIxCols("TestDuplicateTableName")); */ Message: Type 'QueryBuilder' already defines a member called 'TestDuplicateTableName' with the same parameter types, diff --git a/crates/bindings-csharp/Codegen.Tests/fixtures/diag/snapshots/Module#FFI.verified.cs b/crates/bindings-csharp/Codegen.Tests/fixtures/diag/snapshots/Module#FFI.verified.cs index edb27202d22..d0f1092fbc0 100644 --- a/crates/bindings-csharp/Codegen.Tests/fixtures/diag/snapshots/Module#FFI.verified.cs +++ b/crates/bindings-csharp/Codegen.Tests/fixtures/diag/snapshots/Module#FFI.verified.cs @@ -27,7 +27,7 @@ internal TestDuplicateTableNameIxCols(string tableName) { } public readonly partial struct QueryBuilder { - public global::SpacetimeDB.Table< + public global::SpacetimeDB.TableQuery< global::InAnotherNamespace.TestDuplicateTableName, TestDuplicateTableNameCols, TestDuplicateTableNameIxCols @@ -59,7 +59,7 @@ internal PlayerIxCols(string tableName) { } public readonly partial struct QueryBuilder { - public global::SpacetimeDB.Table Player() => + public global::SpacetimeDB.TableQuery Player() => new("Player", new PlayerCols("Player"), new PlayerIxCols("Player")); } @@ -91,7 +91,7 @@ internal TestAutoIncNotIntegerIxCols(string tableName) { } public readonly partial struct QueryBuilder { - public global::SpacetimeDB.Table< + public global::SpacetimeDB.TableQuery< global::TestAutoIncNotInteger, TestAutoIncNotIntegerCols, TestAutoIncNotIntegerIxCols @@ -209,7 +209,7 @@ internal TestDefaultFieldValuesIxCols(string tableName) { } public readonly partial struct QueryBuilder { - public global::SpacetimeDB.Table< + public global::SpacetimeDB.TableQuery< global::TestDefaultFieldValues, TestDefaultFieldValuesCols, TestDefaultFieldValuesIxCols @@ -233,7 +233,7 @@ internal TestDuplicateTableNameIxCols(string tableName) { } public readonly partial struct QueryBuilder { - public global::SpacetimeDB.Table< + public global::SpacetimeDB.TableQuery< global::TestDuplicateTableName, TestDuplicateTableNameCols, TestDuplicateTableNameIxCols @@ -289,7 +289,7 @@ internal TestIndexIssuesIxCols(string tableName) public readonly partial struct QueryBuilder { - public global::SpacetimeDB.Table< + public global::SpacetimeDB.TableQuery< global::TestIndexIssues, TestIndexIssuesCols, TestIndexIssuesIxCols @@ -342,7 +342,7 @@ internal TestScheduleWithoutPrimaryKeyIxCols(string tableName) { } public readonly partial struct QueryBuilder { - public global::SpacetimeDB.Table< + public global::SpacetimeDB.TableQuery< global::TestScheduleIssues, TestScheduleWithoutPrimaryKeyCols, TestScheduleWithoutPrimaryKeyIxCols @@ -403,7 +403,7 @@ internal TestScheduleWithWrongPrimaryKeyTypeIxCols(string tableName) public readonly partial struct QueryBuilder { - public global::SpacetimeDB.Table< + public global::SpacetimeDB.TableQuery< global::TestScheduleIssues, TestScheduleWithWrongPrimaryKeyTypeCols, TestScheduleWithWrongPrimaryKeyTypeIxCols @@ -464,7 +464,7 @@ internal TestScheduleWithoutScheduleAtIxCols(string tableName) public readonly partial struct QueryBuilder { - public global::SpacetimeDB.Table< + public global::SpacetimeDB.TableQuery< global::TestScheduleIssues, TestScheduleWithoutScheduleAtCols, TestScheduleWithoutScheduleAtIxCols @@ -525,7 +525,7 @@ internal TestScheduleWithWrongScheduleAtTypeIxCols(string tableName) public readonly partial struct QueryBuilder { - public global::SpacetimeDB.Table< + public global::SpacetimeDB.TableQuery< global::TestScheduleIssues, TestScheduleWithWrongScheduleAtTypeCols, TestScheduleWithWrongScheduleAtTypeIxCols @@ -578,7 +578,7 @@ internal TestScheduleWithMissingScheduleAtFieldIxCols(string tableName) { } public readonly partial struct QueryBuilder { - public global::SpacetimeDB.Table< + public global::SpacetimeDB.TableQuery< global::TestScheduleIssues, TestScheduleWithMissingScheduleAtFieldCols, TestScheduleWithMissingScheduleAtFieldIxCols @@ -633,7 +633,7 @@ internal TestUniqueNotEquatableIxCols(string tableName) public readonly partial struct QueryBuilder { - public global::SpacetimeDB.Table< + public global::SpacetimeDB.TableQuery< global::TestUniqueNotEquatable, TestUniqueNotEquatableCols, TestUniqueNotEquatableIxCols diff --git a/crates/bindings-csharp/Codegen.Tests/fixtures/explicitnames/snapshots/Module#FFI.verified.cs b/crates/bindings-csharp/Codegen.Tests/fixtures/explicitnames/snapshots/Module#FFI.verified.cs index 76adb1188bb..49cb9fda0d4 100644 --- a/crates/bindings-csharp/Codegen.Tests/fixtures/explicitnames/snapshots/Module#FFI.verified.cs +++ b/crates/bindings-csharp/Codegen.Tests/fixtures/explicitnames/snapshots/Module#FFI.verified.cs @@ -39,7 +39,7 @@ internal DemoTableIxCols(string tableName) public readonly partial struct QueryBuilder { - public global::SpacetimeDB.Table< + public global::SpacetimeDB.TableQuery< global::DemoTable, DemoTableCols, DemoTableIxCols diff --git a/crates/bindings-csharp/Codegen.Tests/fixtures/server/Lib.cs b/crates/bindings-csharp/Codegen.Tests/fixtures/server/Lib.cs index 0a7ba3693eb..af770abe8e6 100644 --- a/crates/bindings-csharp/Codegen.Tests/fixtures/server/Lib.cs +++ b/crates/bindings-csharp/Codegen.Tests/fixtures/server/Lib.cs @@ -3,6 +3,7 @@ #pragma warning disable CA1050 // Declare types in namespaces - this is a test fixture, no need for a namespace. #pragma warning disable STDB_UNSTABLE // Enable experimental SpacetimeDB features +#pragma warning disable CS0649 // Test fixture fields are intentionally uninitialized. [SpacetimeDB.Type] public partial struct CustomStruct diff --git a/crates/bindings-csharp/Codegen.Tests/fixtures/server/snapshots/Module#FFI.verified.cs b/crates/bindings-csharp/Codegen.Tests/fixtures/server/snapshots/Module#FFI.verified.cs index da074b5e5f8..c860ff669ac 100644 --- a/crates/bindings-csharp/Codegen.Tests/fixtures/server/snapshots/Module#FFI.verified.cs +++ b/crates/bindings-csharp/Codegen.Tests/fixtures/server/snapshots/Module#FFI.verified.cs @@ -45,7 +45,7 @@ internal BTreeMultiColumnIxCols(string tableName) public readonly partial struct QueryBuilder { - internal global::SpacetimeDB.Table< + internal global::SpacetimeDB.TableQuery< global::BTreeMultiColumn, BTreeMultiColumnCols, BTreeMultiColumnIxCols @@ -100,7 +100,7 @@ internal BTreeViewsIxCols(string tableName) public readonly partial struct QueryBuilder { - internal global::SpacetimeDB.Table< + internal global::SpacetimeDB.TableQuery< global::BTreeViews, BTreeViewsCols, BTreeViewsIxCols @@ -136,7 +136,7 @@ internal MultiTable1IxCols(string tableName) public readonly partial struct QueryBuilder { - public global::SpacetimeDB.Table< + public global::SpacetimeDB.TableQuery< global::MultiTableRow, MultiTable1Cols, MultiTable1IxCols @@ -169,7 +169,7 @@ internal MultiTable2IxCols(string tableName) { } public readonly partial struct QueryBuilder { - public global::SpacetimeDB.Table< + public global::SpacetimeDB.TableQuery< global::MultiTableRow, MultiTable2Cols, MultiTable2IxCols @@ -193,7 +193,7 @@ internal PrivateTableIxCols(string tableName) { } public readonly partial struct QueryBuilder { - public global::SpacetimeDB.Table< + public global::SpacetimeDB.TableQuery< global::PrivateTable, PrivateTableCols, PrivateTableIxCols @@ -374,7 +374,7 @@ internal PublicTableIxCols(string tableName) public readonly partial struct QueryBuilder { - public global::SpacetimeDB.Table< + public global::SpacetimeDB.TableQuery< global::PublicTable, PublicTableCols, PublicTableIxCols @@ -417,7 +417,7 @@ internal RegressionMultipleUniqueIndexesHadSameNameIxCols(string tableName) { } public readonly partial struct QueryBuilder { - internal global::SpacetimeDB.Table< + internal global::SpacetimeDB.TableQuery< global::RegressionMultipleUniqueIndexesHadSameName, RegressionMultipleUniqueIndexesHadSameNameCols, RegressionMultipleUniqueIndexesHadSameNameIxCols @@ -477,7 +477,7 @@ internal SendMessageTimerIxCols(string tableName) public readonly partial struct QueryBuilder { - public global::SpacetimeDB.Table< + public global::SpacetimeDB.TableQuery< global::Timers.SendMessageTimer, SendMessageTimerCols, SendMessageTimerIxCols diff --git a/crates/bindings-csharp/Codegen/Module.cs b/crates/bindings-csharp/Codegen/Module.cs index f130a3efb05..08196ccc097 100644 --- a/crates/bindings-csharp/Codegen/Module.cs +++ b/crates/bindings-csharp/Codegen/Module.cs @@ -1013,7 +1013,7 @@ string IxColInit(ColumnDeclaration col) public readonly partial struct QueryBuilder { - {{vis}} global::SpacetimeDB.Table<{{globalRowName}}, {{colsTypeName}}, {{ixColsTypeName}}> {{accessorIdentifier}}() => + {{vis}} global::SpacetimeDB.TableQuery<{{globalRowName}}, {{colsTypeName}}, {{ixColsTypeName}}> {{accessorIdentifier}}() => new("{{tableName}}", new {{colsTypeName}}("{{tableName}}"), new {{ixColsTypeName}}("{{tableName}}")); } """; diff --git a/crates/bindings-macro/Cargo.toml b/crates/bindings-macro/Cargo.toml index 9e0094df23a..4cff333cfa0 100644 --- a/crates/bindings-macro/Cargo.toml +++ b/crates/bindings-macro/Cargo.toml @@ -6,6 +6,9 @@ license-file = "LICENSE" description = "Easy support for interacting between SpacetimeDB and Rust." rust-version.workspace = true +[features] +test-utils = [] + [lib] proc-macro = true # Benching off, because of https://bheisler.github.io/criterion.rs/book/faq.html#cargo-bench-gives-unrecognized-option-errors-for-valid-command-line-options diff --git a/crates/bindings-macro/src/lib.rs b/crates/bindings-macro/src/lib.rs index a3efdd3d2f4..00959bedb95 100644 --- a/crates/bindings-macro/src/lib.rs +++ b/crates/bindings-macro/src/lib.rs @@ -109,7 +109,7 @@ use quote::quote; use std::time::Duration; use syn::{parse::ParseStream, Attribute}; use syn::{ItemConst, ItemFn}; -use util::{cvt_attr, ok_or_compile_error}; +use util::{cvt_attr, native_test_utils_registration, ok_or_compile_error}; mod sym { /// A symbol known at compile-time against @@ -305,6 +305,9 @@ pub fn client_visibility_filter(args: StdTokenStream, item: StdTokenStream) -> S let item: ItemConst = syn::parse(item)?; let rls_ident = item.ident.clone(); let register_rls_symbol = format!("__preinit__20_register_row_level_security_{rls_ident}"); + let test_utils_registration = native_test_utils_registration(quote! { + spacetimedb::rt::register_row_level_security(#rls_ident.sql_text()) + }); Ok(quote! { #item @@ -314,6 +317,8 @@ pub fn client_visibility_filter(args: StdTokenStream, item: StdTokenStream) -> S extern "C" fn __register_client_visibility_filter() { spacetimedb::rt::register_row_level_security(#rls_ident.sql_text()) } + + #test_utils_registration }; }) }) @@ -357,6 +362,7 @@ pub fn settings(args: StdTokenStream, item: StdTokenStream) -> StdTokenStream { }, _ => unreachable!("validated above"), }; + let test_utils_registration = native_test_utils_registration(register_call.clone()); Ok(quote! { #item @@ -366,6 +372,8 @@ pub fn settings(args: StdTokenStream, item: StdTokenStream) -> StdTokenStream { extern "C" fn __register_setting() { #register_call } + + #test_utils_registration }; }) }) diff --git a/crates/bindings-macro/src/procedure.rs b/crates/bindings-macro/src/procedure.rs index 9f76e5b547f..2ecd026a745 100644 --- a/crates/bindings-macro/src/procedure.rs +++ b/crates/bindings-macro/src/procedure.rs @@ -1,6 +1,6 @@ use crate::reducer::{assert_only_lifetime_generics, extract_typed_args, generate_explicit_names_impl}; use crate::sym; -use crate::util::{check_duplicate, ident_to_litstr, match_meta}; +use crate::util::{check_duplicate, ident_to_litstr, match_meta, native_test_utils_registration}; use proc_macro2::TokenStream; use quote::quote; use syn::parse::Parser as _; @@ -86,12 +86,16 @@ pub(crate) fn procedure_impl(_args: ProcedureArgs, original_function: &ItemFn) - spacetimedb::rt::register_procedure::<_, _, #func_name>(#func_name) } }; + let test_utils_registration = native_test_utils_registration(quote! { + __register_describer() + }); let generate_explicit_names = generate_explicit_names_impl(&procedure_name.value(), func_name, explicit_name); Ok(quote! { const _: () = { #generated_describe_function + #test_utils_registration }; #[allow(non_camel_case_types)] #vis struct #func_name { _never: ::core::convert::Infallible } diff --git a/crates/bindings-macro/src/reducer.rs b/crates/bindings-macro/src/reducer.rs index ac261ced35f..0193f2033dd 100644 --- a/crates/bindings-macro/src/reducer.rs +++ b/crates/bindings-macro/src/reducer.rs @@ -1,5 +1,5 @@ use crate::sym; -use crate::util::{check_duplicate, check_duplicate_msg, ident_to_litstr, match_meta}; +use crate::util::{check_duplicate, check_duplicate_msg, ident_to_litstr, match_meta, native_test_utils_registration}; use proc_macro2::{Span, TokenStream}; use quote::{quote, quote_spanned}; use syn::parse::Parser as _; @@ -140,10 +140,14 @@ pub(crate) fn reducer_impl(args: ReducerArgs, original_function: &ItemFn) -> syn spacetimedb::rt::register_reducer::<_, #func_name>(#func_name) } }; + let test_utils_registration = native_test_utils_registration(quote! { + __register_describer() + }); Ok(quote! { const _: () = { #generated_describe_function + #test_utils_registration }; #[allow(non_camel_case_types)] #vis struct #func_name { _never: ::core::convert::Infallible } diff --git a/crates/bindings-macro/src/table.rs b/crates/bindings-macro/src/table.rs index 1cbba4c5ca1..bffc8c6fd99 100644 --- a/crates/bindings-macro/src/table.rs +++ b/crates/bindings-macro/src/table.rs @@ -1,6 +1,6 @@ use crate::sats; use crate::sym; -use crate::util::{check_duplicate, check_duplicate_msg, match_meta}; +use crate::util::{check_duplicate, check_duplicate_msg, match_meta, native_test_utils_registration}; use core::slice; use heck::ToSnakeCase; use proc_macro2::{Span, TokenStream}; @@ -589,7 +589,7 @@ impl ValidatedIndex<'_> { quote! { #[doc = #doc] #vis fn #column_ident(&self) -> #unique_ty<#tbl_token, #col_ty, __indices::#index_ident> { - #unique_ty::__NEW + #unique_ty::__new(self.__backend.clone()) } } } @@ -630,7 +630,7 @@ impl ValidatedIndex<'_> { quote! { #[doc = #doc] #vis fn #index_ident(&self) -> #handle_ty<#tbl_token, (#(#col_tys,)*), __indices::#index_ident> { - #handle_ty::__NEW + #handle_ty::__new(self.__backend.clone()) } } } @@ -677,6 +677,7 @@ impl ValidatedIndex<'_> { #vis struct #index_ident; impl spacetimedb::table::#index_kind_trait for #index_ident {} impl spacetimedb::table::Index for #index_ident { + const INDEX_NAME: &'static str = #index_name; const NUM_COLS_INDEXED: usize = #num_cols; fn index_id() -> spacetimedb::table::IndexId { static INDEX_ID: std::sync::OnceLock = std::sync::OnceLock::new(); @@ -1136,6 +1137,9 @@ pub(crate) fn table_impl(mut args: TableArgs, item: &syn::DeriveInput) -> syn::R #(const SCHEDULE: Option> = Some(#schedule);)* #table_id_from_name_func + fn __backend(&self) -> &spacetimedb::table::TableHandleBackend { + &self.__backend + } #default_fn } }; @@ -1157,12 +1161,14 @@ pub(crate) fn table_impl(mut args: TableArgs, item: &syn::DeriveInput) -> syn::R #[allow(non_camel_case_types, dead_code)] #vis trait #table_ident { #[allow(non_camel_case_types, dead_code)] - fn #table_ident(&self) -> &#tablehandle_ident; + fn #table_ident(&self) -> #tablehandle_ident; } impl #table_ident for spacetimedb::Local { #[allow(non_camel_case_types, dead_code)] - fn #table_ident(&self) -> &#tablehandle_ident { - &#tablehandle_ident {} + fn #table_ident(&self) -> #tablehandle_ident { + #tablehandle_ident { + __backend: self.__table_backend(), + } } } }; @@ -1171,12 +1177,14 @@ pub(crate) fn table_impl(mut args: TableArgs, item: &syn::DeriveInput) -> syn::R #[allow(non_camel_case_types, dead_code)] #vis trait #view_trait_ident { #[allow(non_camel_case_types, dead_code)] - fn #table_ident(&self) -> &#viewhandle_ident; + fn #table_ident(&self) -> #viewhandle_ident; } impl #view_trait_ident for spacetimedb::LocalReadOnly { #[inline] - fn #table_ident(&self) -> &#viewhandle_ident { - &#viewhandle_ident {} + fn #table_ident(&self) -> #viewhandle_ident { + #viewhandle_ident { + __backend: self.__table_backend(), + } } } }; @@ -1263,13 +1271,19 @@ pub(crate) fn table_impl(mut args: TableArgs, item: &syn::DeriveInput) -> syn::R let tablehandle_def = quote! { #[allow(non_camel_case_types)] #[non_exhaustive] - #vis struct #tablehandle_ident {} + #vis struct #tablehandle_ident { + #[doc(hidden)] + pub __backend: spacetimedb::table::TableHandleBackend, + } }; let viewhandle_def = quote! { #[allow(non_camel_case_types)] #[non_exhaustive] - #vis struct #viewhandle_ident {} + #vis struct #viewhandle_ident { + #[doc(hidden)] + pub __backend: spacetimedb::table::TableHandleBackend, + } }; let struct_name = original_struct_ident.to_string(); @@ -1281,6 +1295,13 @@ pub(crate) fn table_impl(mut args: TableArgs, item: &syn::DeriveInput) -> syn::R quote! {} }; + let test_utils_registration = native_test_utils_registration(quote! { + spacetimedb::rt::register_table::<#tablehandle_ident>(); + spacetimedb::test_utils::__register_table_name( + <#tablehandle_ident as spacetimedb::table::TableInternal>::TABLE_NAME, + ); + }); + let emission = quote! { const _: () = { #(let _ = <#field_types as spacetimedb::rt::TableColumn>::_ITEM;)* @@ -1304,7 +1325,13 @@ pub(crate) fn table_impl(mut args: TableArgs, item: &syn::DeriveInput) -> syn::R impl #viewhandle_ident { #[inline] pub fn count(&self) -> u64 { - spacetimedb::table::count::<#tablehandle_ident>() + let table_id = self + .__backend + .table_id(<#tablehandle_ident as spacetimedb::table::TableInternal>::TABLE_NAME) + .expect("table_id_from_name() call failed"); + self.__backend + .table_row_count(table_id) + .expect("datastore_table_row_count() call failed") } #(#index_accessors_ro)* @@ -1322,6 +1349,8 @@ pub(crate) fn table_impl(mut args: TableArgs, item: &syn::DeriveInput) -> syn::R } #describe_table_func + + #test_utils_registration }; }; diff --git a/crates/bindings-macro/src/util.rs b/crates/bindings-macro/src/util.rs index 41c9dad016d..13e1679815b 100644 --- a/crates/bindings-macro/src/util.rs +++ b/crates/bindings-macro/src/util.rs @@ -1,5 +1,6 @@ use proc_macro::TokenStream as StdTokenStream; use proc_macro2::{Span, TokenStream}; +use quote::quote; use syn::parse::Parse; use syn::Ident; @@ -81,6 +82,45 @@ pub(crate) fn one_of(options: &[crate::sym::Symbol]) -> String { } } +pub(crate) fn native_test_utils_registration(register_body: TokenStream) -> TokenStream { + if cfg!(feature = "test-utils") { + quote! { + #[cfg(not(target_arch = "wasm32"))] + #[used] + #[cfg_attr( + any( + target_os = "linux", + target_os = "android", + target_os = "freebsd", + target_os = "dragonfly", + target_os = "netbsd", + target_os = "openbsd", + ), + unsafe(link_section = ".init_array") + )] + #[cfg_attr( + any( + target_os = "macos", + target_os = "ios", + target_os = "tvos", + target_os = "watchos", + target_os = "visionos", + ), + unsafe(link_section = "__DATA,__mod_init_func") + )] + #[cfg_attr(target_os = "windows", unsafe(link_section = ".CRT$XCU"))] + static _STDB_TEST_UTILS_INIT: unsafe extern "C" fn() = { + unsafe extern "C" fn __stdb_test_utils_register() { + #register_body + } + __stdb_test_utils_register + }; + } + } else { + quote! {} + } +} + macro_rules! match_meta { (match $meta:ident { $($matches:tt)* }) => {{ let meta: &syn::meta::ParseNestedMeta = &$meta; diff --git a/crates/bindings-macro/src/view.rs b/crates/bindings-macro/src/view.rs index 1dc865d4b41..c948e146a3a 100644 --- a/crates/bindings-macro/src/view.rs +++ b/crates/bindings-macro/src/view.rs @@ -7,7 +7,7 @@ use syn::{FnArg, ItemFn, LitStr}; use crate::reducer::generate_explicit_names_impl; use crate::sym; -use crate::util::{check_duplicate_msg, match_meta}; +use crate::util::{check_duplicate_msg, match_meta, native_test_utils_registration}; pub(crate) struct ViewArgs { name: Option, @@ -226,6 +226,9 @@ pub(crate) fn view_impl(args: ViewArgs, original_function: &ItemFn) -> syn::Resu spacetimedb::rt::ViewRegistrar::<#ctx_ty>::register::<_, #func_name, _, _>(#func_name) } }; + let test_utils_registration = native_test_utils_registration(quote! { + __register_describer() + }); let explicit_name = args.name.as_ref(); let generate_explicit_names = generate_explicit_names_impl(&view_name, func_name, explicit_name); @@ -306,7 +309,10 @@ pub(crate) fn view_impl(args: ViewArgs, original_function: &ItemFn) -> syn::Resu Ok(quote! { #emitted_fn - const _: () = { #generated_describe_function }; + const _: () = { + #generated_describe_function + #test_utils_registration + }; #[allow(non_camel_case_types)] #vis struct #func_name { _never: ::core::convert::Infallible } diff --git a/crates/bindings-sys/src/lib.rs b/crates/bindings-sys/src/lib.rs index 0295b4616e5..1d78cf92b5f 100644 --- a/crates/bindings-sys/src/lib.rs +++ b/crates/bindings-sys/src/lib.rs @@ -85,6 +85,9 @@ pub mod raw { /// - `NOT_IN_TRANSACTION`, when called outside of a transaction. /// - `NO_SUCH_TABLE`, when `table_id` is not a known ID of a table. pub fn datastore_table_row_count(table_id: TableId, out: *mut u64) -> u16; + // pub fn datastore_table_row_count(table_id: TableId, out: *mut u64, instance_id: T) -> u16; + // // datastore.table_row_count() + // pub fn datastore_table_row_count(table_id: TableId, out: *mut u64, instance: *T) -> u16; /// Starts iteration on each row, as BSATN-encoded, of a table identified by `table_id`. /// @@ -976,6 +979,283 @@ pub mod raw { } } +#[cfg(not(target_arch = "wasm32"))] +mod native_link_stubs { + use super::raw::{BytesSink, BytesSource, RowIter}; + use super::{ColId, IndexId, TableId}; + + const NOT_IN_TRANSACTION: u16 = super::Errno::NOT_IN_TRANSACTION.code(); + const NO_SUCH_BYTES: u16 = super::Errno::NO_SUCH_BYTES.code(); + const NO_SUCH_ITER: u16 = super::Errno::NO_SUCH_ITER.code(); + + #[unsafe(no_mangle)] + pub extern "C" fn table_id_from_name(_name: *const u8, _name_len: usize, _out: *mut TableId) -> u16 { + NOT_IN_TRANSACTION + } + + #[unsafe(no_mangle)] + pub extern "C" fn index_id_from_name(_name: *const u8, _name_len: usize, _out: *mut IndexId) -> u16 { + NOT_IN_TRANSACTION + } + + #[unsafe(no_mangle)] + pub extern "C" fn datastore_table_row_count(_table_id: TableId, _out: *mut u64) -> u16 { + NOT_IN_TRANSACTION + } + + #[unsafe(no_mangle)] + pub extern "C" fn datastore_table_scan_bsatn(_table_id: TableId, _out: *mut RowIter) -> u16 { + NOT_IN_TRANSACTION + } + + #[unsafe(no_mangle)] + pub extern "C" fn datastore_index_scan_range_bsatn( + _index_id: IndexId, + _prefix_ptr: *const u8, + _prefix_len: usize, + _prefix_elems: ColId, + _rstart_ptr: *const u8, + _rstart_len: usize, + _rend_ptr: *const u8, + _rend_len: usize, + _out: *mut RowIter, + ) -> u16 { + NOT_IN_TRANSACTION + } + + #[unsafe(no_mangle)] + pub extern "C" fn datastore_btree_scan_bsatn( + index_id: IndexId, + prefix_ptr: *const u8, + prefix_len: usize, + prefix_elems: ColId, + rstart_ptr: *const u8, + rstart_len: usize, + rend_ptr: *const u8, + rend_len: usize, + out: *mut RowIter, + ) -> u16 { + datastore_index_scan_range_bsatn( + index_id, + prefix_ptr, + prefix_len, + prefix_elems, + rstart_ptr, + rstart_len, + rend_ptr, + rend_len, + out, + ) + } + + #[unsafe(no_mangle)] + pub extern "C" fn datastore_delete_by_index_scan_range_bsatn( + _index_id: IndexId, + _prefix_ptr: *const u8, + _prefix_len: usize, + _prefix_elems: ColId, + _rstart_ptr: *const u8, + _rstart_len: usize, + _rend_ptr: *const u8, + _rend_len: usize, + _out: *mut u32, + ) -> u16 { + NOT_IN_TRANSACTION + } + + #[unsafe(no_mangle)] + pub extern "C" fn datastore_delete_by_btree_scan_bsatn( + index_id: IndexId, + prefix_ptr: *const u8, + prefix_len: usize, + prefix_elems: ColId, + rstart_ptr: *const u8, + rstart_len: usize, + rend_ptr: *const u8, + rend_len: usize, + out: *mut u32, + ) -> u16 { + datastore_delete_by_index_scan_range_bsatn( + index_id, + prefix_ptr, + prefix_len, + prefix_elems, + rstart_ptr, + rstart_len, + rend_ptr, + rend_len, + out, + ) + } + + #[unsafe(no_mangle)] + pub extern "C" fn datastore_delete_all_by_eq_bsatn( + _table_id: TableId, + _rel_ptr: *const u8, + _rel_len: usize, + _out: *mut u32, + ) -> u16 { + NOT_IN_TRANSACTION + } + + #[unsafe(no_mangle)] + pub extern "C" fn row_iter_bsatn_advance(_iter: RowIter, _buffer_ptr: *mut u8, _buffer_len_ptr: *mut usize) -> i16 { + NO_SUCH_ITER as i16 + } + + #[unsafe(no_mangle)] + pub extern "C" fn row_iter_bsatn_close(_iter: RowIter) -> u16 { + NO_SUCH_ITER + } + + #[unsafe(no_mangle)] + pub extern "C" fn datastore_insert_bsatn(_table_id: TableId, _row_ptr: *mut u8, _row_len_ptr: *mut usize) -> u16 { + NOT_IN_TRANSACTION + } + + #[unsafe(no_mangle)] + pub extern "C" fn datastore_update_bsatn( + _table_id: TableId, + _index_id: IndexId, + _row_ptr: *mut u8, + _row_len_ptr: *mut usize, + ) -> u16 { + NOT_IN_TRANSACTION + } + + #[cfg(feature = "unstable")] + #[unsafe(no_mangle)] + pub extern "C" fn volatile_nonatomic_schedule_immediate( + _name: *const u8, + _name_len: usize, + _args: *const u8, + _args_len: usize, + ) { + } + + #[unsafe(no_mangle)] + pub extern "C" fn bytes_sink_write(_sink: BytesSink, _buffer_ptr: *const u8, _buffer_len_ptr: *mut usize) -> u16 { + NO_SUCH_BYTES + } + + #[unsafe(no_mangle)] + pub extern "C" fn bytes_source_read( + _source: BytesSource, + _buffer_ptr: *mut u8, + _buffer_len_ptr: *mut usize, + ) -> i16 { + NO_SUCH_BYTES as i16 + } + + #[unsafe(no_mangle)] + pub extern "C" fn console_log( + _level: u8, + _target_ptr: *const u8, + _target_len: usize, + _filename_ptr: *const u8, + _filename_len: usize, + _line_number: u32, + _message_ptr: *const u8, + _message_len: usize, + ) { + } + + #[unsafe(no_mangle)] + pub extern "C" fn console_timer_start(_name_ptr: *const u8, _name_len: usize) -> u32 { + 0 + } + + #[unsafe(no_mangle)] + pub extern "C" fn console_timer_end(_timer_id: u32) -> u16 { + 0 + } + + #[unsafe(no_mangle)] + pub extern "C" fn identity(out_ptr: *mut u8) { + if !out_ptr.is_null() { + // SAFETY: This is a native link stub used only outside the host ABI. Writing + // Identity::ZERO keeps callers that accidentally reach it deterministic. + unsafe { core::ptr::write_bytes(out_ptr, 0, 32) }; + } + } + + #[unsafe(no_mangle)] + pub extern "C" fn bytes_source_remaining_length(_source: BytesSource, _out: *mut u32) -> i16 { + NO_SUCH_BYTES as i16 + } + + #[unsafe(no_mangle)] + pub extern "C" fn get_jwt(_connection_id_ptr: *const u8, bytes_source_id: *mut BytesSource) -> u16 { + if !bytes_source_id.is_null() { + // SAFETY: The pointer is provided by the safe wrapper and points to a + // BytesSource output slot when non-null. + unsafe { bytes_source_id.write(BytesSource::INVALID) }; + } + NOT_IN_TRANSACTION + } + + #[cfg(feature = "unstable")] + #[unsafe(no_mangle)] + pub extern "C" fn procedure_sleep_until(wake_at_micros_since_unix_epoch: i64) -> i64 { + wake_at_micros_since_unix_epoch + } + + #[cfg(feature = "unstable")] + #[unsafe(no_mangle)] + pub extern "C" fn procedure_start_mut_tx(_out: *mut i64) -> u16 { + NOT_IN_TRANSACTION + } + + #[cfg(feature = "unstable")] + #[unsafe(no_mangle)] + pub extern "C" fn procedure_commit_mut_tx() -> u16 { + NOT_IN_TRANSACTION + } + + #[cfg(feature = "unstable")] + #[unsafe(no_mangle)] + pub extern "C" fn procedure_abort_mut_tx() -> u16 { + NOT_IN_TRANSACTION + } + + #[cfg(feature = "unstable")] + #[unsafe(no_mangle)] + pub extern "C" fn procedure_http_request( + _request_ptr: *const u8, + _request_len: u32, + _body_ptr: *const u8, + _body_len: u32, + _out: *mut [BytesSource; 2], + ) -> u16 { + NOT_IN_TRANSACTION + } + + #[unsafe(no_mangle)] + pub extern "C" fn datastore_index_scan_point_bsatn( + _index_id: IndexId, + _point_ptr: *const u8, + _point_len: usize, + _out: *mut RowIter, + ) -> u16 { + NOT_IN_TRANSACTION + } + + #[unsafe(no_mangle)] + pub extern "C" fn datastore_delete_by_index_scan_point_bsatn( + _index_id: IndexId, + _point_ptr: *const u8, + _point_len: usize, + _out: *mut u32, + ) -> u16 { + NOT_IN_TRANSACTION + } + + #[unsafe(no_mangle)] + pub extern "C" fn datastore_clear(_table_id: TableId, _out: *mut u64) -> u16 { + NOT_IN_TRANSACTION + } +} + /// Error values used in the safe bindings API. #[derive(Copy, Clone, PartialEq, Eq)] #[repr(transparent)] diff --git a/crates/bindings-typescript/case-conversion-test-client/src/module_bindings/index.ts b/crates/bindings-typescript/case-conversion-test-client/src/module_bindings/index.ts index 9c93137b36d..f564a293b28 100644 --- a/crates/bindings-typescript/case-conversion-test-client/src/module_bindings/index.ts +++ b/crates/bindings-typescript/case-conversion-test-client/src/module_bindings/index.ts @@ -1,7 +1,7 @@ // THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE // WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. -// This was generated using spacetimedb cli version 2.7.0 (commit de4516d0d3d943cf4c3097c4ad31e4c7735b4f5b). +// This was generated using spacetimedb cli version 2.7.0 (commit 234f6c9c54406060ee2d781af9fd727e0d790650). /* eslint-disable */ /* tslint:disable */ diff --git a/crates/bindings-typescript/package.json b/crates/bindings-typescript/package.json index 6d517d3188d..a04ca2292e4 100644 --- a/crates/bindings-typescript/package.json +++ b/crates/bindings-typescript/package.json @@ -23,7 +23,7 @@ "./src/server/polyfills.ts" ], "scripts": { - "build:js": "tsup", + "build:js": "tsup && node scripts/copy-test-utils-wasm.mjs", "build:types": "tsc -p tsconfig.build.json", "build": "pnpm -s build:js && pnpm -s build:types", "format": "prettier . --write --ignore-path ../../.prettierignore", @@ -72,6 +72,21 @@ "require": "./dist/server/index.cjs", "default": "./dist/server/index.mjs" }, + "./server/test-utils": { + "types": "./dist/server/test-utils/index.d.ts", + "import": "./dist/server/test-utils/index.mjs", + "default": "./dist/server/test-utils/index.mjs" + }, + "./server/test-utils/vitest": { + "types": "./dist/server/test-utils/vitest.d.ts", + "import": "./dist/server/test-utils/vitest.mjs", + "default": "./dist/server/test-utils/vitest.mjs" + }, + "./server/test-utils/wasm": { + "types": "./dist/server/test-utils/wasm.d.ts", + "import": "./dist/server/test-utils/wasm.mjs", + "default": "./dist/server/test-utils/wasm.mjs" + }, "./vue": { "types": "./dist/vue/index.d.ts", "import": "./dist/vue/index.mjs", diff --git a/crates/bindings-typescript/scripts/copy-test-utils-wasm.mjs b/crates/bindings-typescript/scripts/copy-test-utils-wasm.mjs new file mode 100644 index 00000000000..cf3fd2aeb53 --- /dev/null +++ b/crates/bindings-typescript/scripts/copy-test-utils-wasm.mjs @@ -0,0 +1,21 @@ +import { cpSync, existsSync, mkdirSync, rmSync } from 'node:fs'; +import { dirname, resolve } from 'node:path'; +import { fileURLToPath } from 'node:url'; + +const packageRoot = resolve(dirname(fileURLToPath(import.meta.url)), '..'); +const sourceDir = resolve( + packageRoot, + 'src/server/test-utils/portable-datastore-wasm' +); +const outputDir = resolve( + packageRoot, + 'dist/server/test-utils/portable-datastore-wasm' +); + +if (!existsSync(sourceDir)) { + throw new Error(`Missing portable datastore wasm output: ${sourceDir}`); +} + +rmSync(outputDir, { force: true, recursive: true }); +mkdirSync(dirname(outputDir), { recursive: true }); +cpSync(sourceDir, outputDir, { recursive: true }); diff --git a/crates/bindings-typescript/src/server/backend.ts b/crates/bindings-typescript/src/server/backend.ts new file mode 100644 index 00000000000..8075cebb6e4 --- /dev/null +++ b/crates/bindings-typescript/src/server/backend.ts @@ -0,0 +1,131 @@ +import * as _syscalls2_0 from 'spacetime:sys@2.0'; +import * as _syscalls2_1 from 'spacetime:sys@2.1'; + +import type { u128, u16, u256, u32 } from 'spacetime:sys@2.0'; + +export const sys = { ..._syscalls2_0, ..._syscalls2_1 }; + +export interface DatastoreBackend { + identity(): u256; + getJwtPayload(connectionId: u128): Uint8Array; + + tableIdFromName(name: string): u32; + indexIdFromName(name: string): u32; + datastoreTableRowCount(tableId: u32): u64ish; + datastoreTableScanBsatn(tableId: u32): u32; + datastoreInsertBsatn( + tableId: u32, + row: ArrayBuffer, + rowLen: number + ): Uint8Array | number | void; + datastoreDeleteAllByEqBsatn( + tableId: u32, + row: ArrayBuffer, + rowLen: number + ): u32; + datastoreIndexScanPointBsatn( + indexId: u32, + point: ArrayBuffer, + pointLen: number + ): u32; + datastoreIndexScanRangeBsatn( + indexId: u32, + prefix: ArrayBuffer, + prefixLen: u32, + prefixElems: u16, + rstartLen: u32, + rendLen: u32 + ): u32; + datastoreDeleteByIndexScanPointBsatn( + indexId: u32, + point: ArrayBuffer, + pointLen: number + ): u32; + datastoreDeleteByIndexScanRangeBsatn( + indexId: u32, + prefix: ArrayBuffer, + prefixLen: u32, + prefixElems: u16, + rstartLen: u32, + rendLen: u32 + ): u32; + datastoreUpdateBsatn( + tableId: u32, + indexId: u32, + row: ArrayBuffer, + rowLen: number + ): Uint8Array | number | void; + datastoreClear(tableId: u32): void; + + rowIterBsatnAdvance(iterId: u32, out: ArrayBuffer): number; + rowIterBsatnClose(iterId: u32): void; + + procedureStartMutTx(): bigint; + procedureCommitMutTx(): void; + procedureAbortMutTx(): void; + procedureHttpRequest( + request: Uint8Array, + body: Uint8Array | string + ): [Uint8Array, Uint8Array]; +} + +type u64ish = number | bigint; + +export const hostBackend: DatastoreBackend = { + identity: () => sys.identity(), + getJwtPayload: connectionId => sys.get_jwt_payload(connectionId), + tableIdFromName: name => sys.table_id_from_name(name), + indexIdFromName: name => sys.index_id_from_name(name), + datastoreTableRowCount: tableId => sys.datastore_table_row_count(tableId), + datastoreTableScanBsatn: tableId => sys.datastore_table_scan_bsatn(tableId), + datastoreInsertBsatn: (tableId, row, rowLen) => + sys.datastore_insert_bsatn(tableId, row, rowLen), + datastoreDeleteAllByEqBsatn: (tableId, row, rowLen) => + sys.datastore_delete_all_by_eq_bsatn(tableId, row, rowLen), + datastoreIndexScanPointBsatn: (indexId, point, pointLen) => + sys.datastore_index_scan_point_bsatn(indexId, point, pointLen), + datastoreIndexScanRangeBsatn: ( + indexId, + prefix, + prefixLen, + prefixElems, + rstartLen, + rendLen + ) => + sys.datastore_index_scan_range_bsatn( + indexId, + prefix, + prefixLen, + prefixElems, + rstartLen, + rendLen + ), + datastoreDeleteByIndexScanPointBsatn: (indexId, point, pointLen) => + sys.datastore_delete_by_index_scan_point_bsatn(indexId, point, pointLen), + datastoreDeleteByIndexScanRangeBsatn: ( + indexId, + prefix, + prefixLen, + prefixElems, + rstartLen, + rendLen + ) => + sys.datastore_delete_by_index_scan_range_bsatn( + indexId, + prefix, + prefixLen, + prefixElems, + rstartLen, + rendLen + ), + datastoreUpdateBsatn: (tableId, indexId, row, rowLen) => + sys.datastore_update_bsatn(tableId, indexId, row, rowLen), + datastoreClear: tableId => sys.datastore_clear(tableId), + rowIterBsatnAdvance: (iterId, out) => sys.row_iter_bsatn_advance(iterId, out), + rowIterBsatnClose: iterId => sys.row_iter_bsatn_close(iterId), + procedureStartMutTx: () => sys.procedure_start_mut_tx(), + procedureCommitMutTx: () => sys.procedure_commit_mut_tx(), + procedureAbortMutTx: () => sys.procedure_abort_mut_tx(), + procedureHttpRequest: (request, body) => + sys.procedure_http_request(request, body), +}; diff --git a/crates/bindings-typescript/src/server/procedures.ts b/crates/bindings-typescript/src/server/procedures.ts index 5f1867d986c..6a24a27ed7e 100644 --- a/crates/bindings-typescript/src/server/procedures.ts +++ b/crates/bindings-typescript/src/server/procedures.ts @@ -11,6 +11,7 @@ import type { ConnectionId } from '../lib/connection_id'; import { Identity } from '../lib/identity'; import type { ParamsObj, ReducerCtx } from '../lib/reducers'; import { type UntypedSchemaDef } from '../lib/schema'; +import type { TimeDuration } from '../lib/time_duration'; import type { ScheduleTableForParams } from '../lib/table_schema'; import { Timestamp } from '../lib/timestamp'; import { @@ -23,8 +24,9 @@ import { bsatnBaseSize } from '../lib/util'; import { Uuid } from '../lib/uuid'; import { httpClient, type HttpClient } from './http_internal'; import type { DbView } from './db_view'; -import { makeRandom, type Random } from './rng'; -import { callUserFunction, ReducerCtxImpl, runWithTx, sys } from './runtime'; +import { makeRandom, makeRandomFromSeed, type Random } from './rng'; +import { callUserFunction, ReducerCtxImpl } from './runtime'; +import { hostBackend, type DatastoreBackend } from './backend'; import { exportContext, registerExport, @@ -102,6 +104,7 @@ export interface ProcedureCtx { readonly http: HttpClient; readonly random: Random; withTx(body: (ctx: TransactionCtx) => T): T; + sleep(duration: TimeDuration): void; newUuidV4(): Uuid; newUuidV7(): Uuid; } @@ -112,10 +115,6 @@ export interface TransactionCtx type ITransactionCtx = TransactionCtx; -const TransactionCtxImpl = class TransactionCtx - extends ReducerCtxImpl - implements ITransactionCtx {}; - function registerProcedure< S extends UntypedSchemaDef, Params extends ParamsObj, @@ -199,25 +198,41 @@ export function callProcedure( } type IProcedureCtx = ProcedureCtx; -const ProcedureCtxImpl = class ProcedureCtx +export const ProcedureCtxImpl = class ProcedureCtx implements IProcedureCtx { #identity: Identity | undefined; #uuidCounter: { value: 0 } | undefined; #random: Random | undefined; #dbView: () => DbView; + #backend: DatastoreBackend; + #http: HttpClient; + #sleep: (duration: TimeDuration) => void; + #childSeedRandom: Random | undefined; constructor( readonly sender: Identity, readonly timestamp: Timestamp, readonly connectionId: ConnectionId | null, - dbView: () => DbView + dbView: () => DbView, + backend: DatastoreBackend = hostBackend, + http: HttpClient = httpClient, + sleep: (duration: TimeDuration) => void = () => { + throw new Error('procedure sleep is not available in this runtime'); + }, + random?: Random, + childSeedRandom?: Random ) { this.#dbView = dbView; + this.#backend = backend; + this.#http = http; + this.#sleep = sleep; + this.#random = random; + this.#childSeedRandom = childSeedRandom; } get databaseIdentity() { - return (this.#identity ??= new Identity(sys.identity())); + return (this.#identity ??= new Identity(this.#backend.identity())); } get identity() { @@ -229,20 +244,50 @@ const ProcedureCtxImpl = class ProcedureCtx } get http() { - return httpClient; + return this.#http; + } + + sleep(duration: TimeDuration): void { + this.#sleep(duration); } withTx(body: (ctx: TransactionCtx) => T): T { - return runWithTx( - timestamp => - new TransactionCtxImpl( + const txSeed = this.#childSeedRandom?.bigintInRange(0n, (1n << 64n) - 1n); + const run = () => { + const timestamp = new Timestamp(this.#backend.procedureStartMutTx()); + + try { + const ctx: ITransactionCtx = new ReducerCtxImpl( this.sender, timestamp, this.connectionId, - this.#dbView() - ) as TransactionCtx, - body - ); + this.#dbView(), + this.#backend, + undefined, + txSeed == null ? undefined : makeRandomFromSeed(txSeed) + ) as ITransactionCtx; + return body(ctx); + } catch (e) { + this.#backend.procedureAbortMutTx(); + throw e; + } + }; + + let res = run(); + try { + this.#backend.procedureCommitMutTx(); + return res; + } catch { + // ignore the commit error + } + console.warn('committing anonymous transaction failed'); + res = run(); + try { + this.#backend.procedureCommitMutTx(); + return res; + } catch (e) { + throw new Error('transaction retry failed again', { cause: e }); + } } newUuidV4(): Uuid { diff --git a/crates/bindings-typescript/src/server/rng.ts b/crates/bindings-typescript/src/server/rng.ts index 2bde81d64ad..eea1e43926f 100644 --- a/crates/bindings-typescript/src/server/rng.ts +++ b/crates/bindings-typescript/src/server/rng.ts @@ -16,7 +16,7 @@ type IntArray = | BigUint64Array; /** - * A collection of random-number-generating functions, seeded based on `ctx.timestamp`. + * A collection of random-number-generating functions. * * ## Usage * @@ -79,9 +79,11 @@ function generateFloat64(rng: RandomGenerator): number { return value; } -export function makeRandom(seed: Timestamp): Random { +export function makeRandomFromSeed( + seed: Timestamp | number | bigint | Uint8Array +): Random { // Use PCG32 to turn a 64-bit seed into a 32-bit seed, as the Rust `rand` crate does. - const rng = xoroshiro128plus(pcg32(seed.microsSinceUnixEpoch)); + const rng = xoroshiro128plus(pcg32(seedToBigInt(seed))); const random: Random = () => generateFloat64(rng); @@ -111,6 +113,24 @@ export function makeRandom(seed: Timestamp): Random { return random; } +export function makeRandom(seed: Timestamp): Random { + return makeRandomFromSeed(seed); +} + +function seedToBigInt(seed: Timestamp | number | bigint | Uint8Array): bigint { + if (typeof seed === 'bigint') return seed; + if (typeof seed === 'number') return BigInt(seed); + if (seed instanceof Uint8Array) { + let value = 0n; + const len = Math.min(seed.byteLength, 8); + for (let i = 0; i < len; i++) { + value |= BigInt(seed[i]) << BigInt(i * 8); + } + return value; + } + return seed.microsSinceUnixEpoch; +} + function isBigIntArray( array: IntArray ): array is BigInt64Array | BigUint64Array { diff --git a/crates/bindings-typescript/src/server/runtime.ts b/crates/bindings-typescript/src/server/runtime.ts index e5d782a71e3..d06108036ee 100644 --- a/crates/bindings-typescript/src/server/runtime.ts +++ b/crates/bindings-typescript/src/server/runtime.ts @@ -55,6 +55,7 @@ import { getErrorConstructor, SenderError } from './errors'; import { Range, type Bound } from './range'; import { makeRandom, type Random } from './rng'; import type { SchemaInner } from './schema'; +import { hostBackend, type DatastoreBackend } from './backend'; import { HttpRequest, HttpResponse } from '../lib/autogen/types'; const { freeze } = Object; @@ -188,9 +189,10 @@ class AuthCtxImpl implements AuthCtx { } /** If there is a connection id, look up the JWT payload from the system tables. */ - static fromSystemTables( + static fromBackend( connectionId: ConnectionId | null, - sender: Identity + sender: Identity, + backend: DatastoreBackend ): AuthCtx { if (connectionId === null) { return new AuthCtxImpl({ @@ -202,7 +204,9 @@ class AuthCtxImpl implements AuthCtx { return new AuthCtxImpl({ isInternal: false, jwtSource: () => { - const payloadBuf = sys.get_jwt_payload(connectionId.__connection_id__); + const payloadBuf = backend.getJwtPayload( + connectionId.__connection_id__ + ); if (payloadBuf.length === 0) return null; const payloadStr = new TextDecoder().decode(payloadBuf); return payloadStr; @@ -210,6 +214,14 @@ class AuthCtxImpl implements AuthCtx { senderIdentity: sender, }); } + + /** If there is a connection id, look up the JWT payload from the host system tables. */ + static fromSystemTables( + connectionId: ConnectionId | null, + sender: Identity + ): AuthCtx { + return AuthCtxImpl.fromBackend(connectionId, sender, hostBackend); + } } // Using a class expression rather than declaration keeps the class out of the @@ -222,6 +234,7 @@ export const ReducerCtxImpl = class ReducerCtx< #senderAuth: AuthCtx | undefined; #uuidCounter: { value: number } | undefined; #random: Random | undefined; + #backend: DatastoreBackend; sender: Identity; timestamp: Timestamp; connectionId: ConnectionId | null; @@ -231,13 +244,19 @@ export const ReducerCtxImpl = class ReducerCtx< sender: Identity, timestamp: Timestamp, connectionId: ConnectionId | null, - dbView: DbView + dbView: DbView, + backend: DatastoreBackend = hostBackend, + senderAuth?: AuthCtx, + random?: Random ) { Object.seal(this); this.sender = sender; this.timestamp = timestamp; this.connectionId = connectionId; this.db = dbView; + this.#backend = backend; + this.#senderAuth = senderAuth; + this.#random = random; } /** Reset the `ReducerCtx` to be used for a new transaction */ @@ -245,17 +264,22 @@ export const ReducerCtxImpl = class ReducerCtx< me: InstanceType, sender: Identity, timestamp: Timestamp, - connectionId: ConnectionId | null + connectionId: ConnectionId | null, + backend: DatastoreBackend = hostBackend, + senderAuth?: AuthCtx, + random?: Random ) { me.sender = sender; me.timestamp = timestamp; me.connectionId = connectionId; + me.#backend = backend; me.#uuidCounter = undefined; - me.#senderAuth = undefined; + me.#senderAuth = senderAuth; + me.#random = random; } get databaseIdentity() { - return (this.#identity ??= new Identity(sys.identity())); + return (this.#identity ??= new Identity(this.#backend.identity())); } get identity() { @@ -263,9 +287,10 @@ export const ReducerCtxImpl = class ReducerCtx< } get senderAuth() { - return (this.#senderAuth ??= AuthCtxImpl.fromSystemTables( + return (this.#senderAuth ??= AuthCtxImpl.fromBackend( this.connectionId, - this.sender + this.sender, + this.#backend )); } @@ -561,9 +586,10 @@ class HandlerContextImpl export function makeTableView( typespace: Typespace, - table: RawTableDefV10 + table: RawTableDefV10, + backend: DatastoreBackend = hostBackend ): Table { - const table_id = sys.table_id_from_name(table.sourceName); + const table_id = backend.tableIdFromName(table.sourceName); const rowType = typespace.types[table.productTypeRef]; if (rowType.tag !== 'Product') { throw 'impossible'; @@ -610,7 +636,11 @@ export function makeTableView( const hasAutoIncrement = sequences.length > 0; const iter = () => - tableIterator(sys.datastore_table_scan_bsatn(table_id), deserializeRow); + tableIterator( + backend.datastoreTableScanBsatn(table_id), + deserializeRow, + backend + ); const integrateGeneratedColumns = hasAutoIncrement ? (row: RowType, ret_buf: DataView) => { @@ -623,17 +653,36 @@ export function makeTableView( } : null; + const generatedColumnsView = ( + generated: Uint8Array | number | void, + fallback: DataView + ) => + generated instanceof Uint8Array + ? new DataView( + generated.buffer, + generated.byteOffset, + generated.byteLength + ) + : fallback; + const tableMethods: TableMethods = { - count: () => sys.datastore_table_row_count(table_id), + count: () => BigInt(backend.datastoreTableRowCount(table_id)), iter, [Symbol.iterator]: () => iter(), insert: row => { const buf = LEAF_BUF; BINARY_WRITER.reset(buf); serializeRow(BINARY_WRITER, row); - sys.datastore_insert_bsatn(table_id, buf.buffer, BINARY_WRITER.offset); + const generated = backend.datastoreInsertBsatn( + table_id, + buf.buffer, + BINARY_WRITER.offset + ); const ret = { ...row }; - integrateGeneratedColumns?.(ret, buf.view); + integrateGeneratedColumns?.( + ret, + generatedColumnsView(generated, buf.view) + ); return ret; }, @@ -642,14 +691,18 @@ export function makeTableView( BINARY_WRITER.reset(buf); BINARY_WRITER.writeU32(1); serializeRow(BINARY_WRITER, row); - const count = sys.datastore_delete_all_by_eq_bsatn( + const count = backend.datastoreDeleteAllByEqBsatn( table_id, buf.buffer, BINARY_WRITER.offset ); return count > 0; }, - clear: () => sys.datastore_clear(table_id), + clear: () => { + const count = BigInt(backend.datastoreTableRowCount(table_id)); + backend.datastoreClear(table_id); + return count; + }, }; const tableView = Object.assign( @@ -659,7 +712,7 @@ export function makeTableView( for (const indexDef of table.indexes) { const accessorName = indexDef.accessorName!; - const index_id = sys.index_id_from_name(indexDef.sourceName!); + const index_id = backend.indexIdFromName(indexDef.sourceName!); let column_ids: number[]; let isHashIndex = false; @@ -680,7 +733,10 @@ export function makeTableView( const columnSet = new Set(column_ids); const isUnique = table.constraints .filter(x => x.data.tag === 'Unique') - .some(x => columnSet.isSubsetOf(new Set(x.data.value.columns))); + .some(x => { + const constraintColumns = new Set(x.data.value.columns); + return [...columnSet].every(column => constraintColumns.has(column)); + }); const isPrimaryKey = isUnique && @@ -727,17 +783,17 @@ export function makeTableView( find: (colVal: IndexVal): RowType | null => { const buf = LEAF_BUF; const point_len = serializeSinglePoint(buf, colVal); - const iter_id = sys.datastore_index_scan_point_bsatn( + const iter_id = backend.datastoreIndexScanPointBsatn( index_id, buf.buffer, point_len ); - return tableIterateOne(iter_id, deserializeRow); + return tableIterateOne(iter_id, deserializeRow, backend); }, delete: (colVal: IndexVal): boolean => { const buf = LEAF_BUF; const point_len = serializeSinglePoint(buf, colVal); - const num = sys.datastore_delete_by_index_scan_point_bsatn( + const num = backend.datastoreDeleteByIndexScanPointBsatn( index_id, buf.buffer, point_len @@ -750,13 +806,16 @@ export function makeTableView( const buf = LEAF_BUF; BINARY_WRITER.reset(buf); serializeRow(BINARY_WRITER, row); - sys.datastore_update_bsatn( + const generated = backend.datastoreUpdateBsatn( table_id, index_id, buf.buffer, BINARY_WRITER.offset ); - integrateGeneratedColumns?.(row, buf.view); + integrateGeneratedColumns?.( + row, + generatedColumnsView(generated, buf.view) + ); return row; }; } @@ -770,12 +829,12 @@ export function makeTableView( } const buf = LEAF_BUF; const point_len = serializePoint(buf, colVal); - const iter_id = sys.datastore_index_scan_point_bsatn( + const iter_id = backend.datastoreIndexScanPointBsatn( index_id, buf.buffer, point_len ); - return tableIterateOne(iter_id, deserializeRow); + return tableIterateOne(iter_id, deserializeRow, backend); }, delete: (colVal: IndexVal): boolean => { if (colVal.length !== numColumns) @@ -783,7 +842,7 @@ export function makeTableView( const buf = LEAF_BUF; const point_len = serializePoint(buf, colVal); - const num = sys.datastore_delete_by_index_scan_point_bsatn( + const num = backend.datastoreDeleteByIndexScanPointBsatn( index_id, buf.buffer, point_len @@ -796,13 +855,16 @@ export function makeTableView( const buf = LEAF_BUF; BINARY_WRITER.reset(buf); serializeRow(BINARY_WRITER, row); - sys.datastore_update_bsatn( + const generated = backend.datastoreUpdateBsatn( table_id, index_id, buf.buffer, BINARY_WRITER.offset ); - integrateGeneratedColumns?.(row, buf.view); + integrateGeneratedColumns?.( + row, + generatedColumnsView(generated, buf.view) + ); return row; }; } @@ -833,33 +895,33 @@ export function makeTableView( const buf = LEAF_BUF; if (serializeSingleRange && range instanceof Range) { const args = serializeSingleRange(buf, range); - const iter_id = sys.datastore_index_scan_range_bsatn( + const iter_id = backend.datastoreIndexScanRangeBsatn( index_id, buf.buffer, ...args ); - return tableIterator(iter_id, deserializeRow); + return tableIterator(iter_id, deserializeRow, backend); } const point_len = serializeSinglePoint(buf, range); - const iter_id = sys.datastore_index_scan_point_bsatn( + const iter_id = backend.datastoreIndexScanPointBsatn( index_id, buf.buffer, point_len ); - return tableIterator(iter_id, deserializeRow); + return tableIterator(iter_id, deserializeRow, backend); }, delete: (range: any): u32 => { const buf = LEAF_BUF; if (serializeSingleRange && range instanceof Range) { const args = serializeSingleRange(buf, range); - return sys.datastore_delete_by_index_scan_range_bsatn( + return backend.datastoreDeleteByIndexScanRangeBsatn( index_id, buf.buffer, ...args ); } const point_len = serializeSinglePoint(buf, range); - return sys.datastore_delete_by_index_scan_point_bsatn( + return backend.datastoreDeleteByIndexScanPointBsatn( index_id, buf.buffer, point_len @@ -877,17 +939,17 @@ export function makeTableView( filter: (range: any[]): IteratorObject> => { const buf = LEAF_BUF; const point_len = serializePoint(buf, range); - const iter_id = sys.datastore_index_scan_point_bsatn( + const iter_id = backend.datastoreIndexScanPointBsatn( index_id, buf.buffer, point_len ); - return tableIterator(iter_id, deserializeRow); + return tableIterator(iter_id, deserializeRow, backend); }, delete: (range: any[]): u32 => { const buf = LEAF_BUF; const point_len = serializePoint(buf, range); - return sys.datastore_delete_by_index_scan_point_bsatn( + return backend.datastoreDeleteByIndexScanPointBsatn( index_id, buf.buffer, point_len @@ -939,21 +1001,21 @@ export function makeTableView( if (range.length === numColumns) { const buf = LEAF_BUF; const point_len = serializePoint(buf, range); - const iter_id = sys.datastore_index_scan_point_bsatn( + const iter_id = backend.datastoreIndexScanPointBsatn( index_id, buf.buffer, point_len ); - return tableIterator(iter_id, deserializeRow); + return tableIterator(iter_id, deserializeRow, backend); } else { const buf = LEAF_BUF; const args = serializeRange(buf, range); - const iter_id = sys.datastore_index_scan_range_bsatn( + const iter_id = backend.datastoreIndexScanRangeBsatn( index_id, buf.buffer, ...args ); - return tableIterator(iter_id, deserializeRow); + return tableIterator(iter_id, deserializeRow, backend); } }, delete: (range: any[]): u32 => { @@ -961,7 +1023,7 @@ export function makeTableView( if (range.length === numColumns) { const buf = LEAF_BUF; const point_len = serializePoint(buf, range); - return sys.datastore_delete_by_index_scan_point_bsatn( + return backend.datastoreDeleteByIndexScanPointBsatn( index_id, buf.buffer, point_len @@ -969,7 +1031,7 @@ export function makeTableView( } else { const buf = LEAF_BUF; const args = serializeRange(buf, range); - return sys.datastore_delete_by_index_scan_range_bsatn( + return backend.datastoreDeleteByIndexScanRangeBsatn( index_id, buf.buffer, ...args @@ -994,9 +1056,10 @@ export function makeTableView( function* tableIterator( id: u32, - deserialize: Deserializer + deserialize: Deserializer, + backend: DatastoreBackend = hostBackend ): Generator { - using iter = new IteratorHandle(id); + const iter = new IteratorHandle(id, backend); const iterBuf = takeBuf(); try { @@ -1008,15 +1071,20 @@ function* tableIterator( } } } finally { + iter[Symbol.dispose](); returnBuf(iterBuf); } } -function tableIterateOne(id: u32, deserialize: Deserializer): T | null { +function tableIterateOne( + id: u32, + deserialize: Deserializer, + backend: DatastoreBackend = hostBackend +): T | null { const buf = LEAF_BUF; // we only need to check for the `<= 0` case, since this function is only used // with iterators that should only have zero or one element. - const ret = advanceIterRaw(id, buf); + const ret = advanceIterRaw(id, buf, backend); if (ret !== 0) { BINARY_READER.reset(buf.view); return deserialize(BINARY_READER); @@ -1029,10 +1097,14 @@ function tableIterateOne(id: u32, deserialize: Deserializer): T | null { * `ret === 0` means the iterator was empty and has been destroyed. * `ret > 0` means the iterator yielded elements and has more to give. */ -function advanceIterRaw(id: u32, buf: ResizableBuffer): number { +function advanceIterRaw( + id: u32, + buf: ResizableBuffer, + backend: DatastoreBackend = hostBackend +): number { while (true) { try { - return 0 | sys.row_iter_bsatn_advance(id, buf.buffer); + return 0 | backend.rowIterBsatnAdvance(id, buf.buffer); } catch (e) { if (e && typeof e === 'object' && hasOwn(e, '__buffer_too_small__')) { buf.grow(e.__buffer_too_small__ as number); @@ -1072,14 +1144,17 @@ const LEAF_BUF = new ResizableBuffer(DEFAULT_BUFFER_CAPACITY); /** A class to manage the lifecycle of an iterator handle. */ class IteratorHandle implements Disposable { #id: u32 | -1; + #backend: DatastoreBackend; - static #finalizationRegistry = new FinalizationRegistry( - sys.row_iter_bsatn_close - ); + static #finalizationRegistry = new FinalizationRegistry<{ + id: u32; + backend: DatastoreBackend; + }>(({ id, backend }) => backend.rowIterBsatnClose(id)); - constructor(id: u32) { + constructor(id: u32, backend: DatastoreBackend = hostBackend) { this.#id = id; - IteratorHandle.#finalizationRegistry.register(this, id, this); + this.#backend = backend; + IteratorHandle.#finalizationRegistry.register(this, { id, backend }, this); } /** Unregister this object with the finalization registry and return the id */ @@ -1093,7 +1168,7 @@ class IteratorHandle implements Disposable { /** Call `row_iter_bsatn_advance`, returning 0 if this iterator has been exhausted. */ advance(buf: ResizableBuffer): number { if (this.#id === -1) return 0; - const ret = advanceIterRaw(this.#id, buf); + const ret = advanceIterRaw(this.#id, buf, this.#backend); if (ret <= 0) this.#detach(); return ret < 0 ? -ret : ret; } @@ -1101,7 +1176,7 @@ class IteratorHandle implements Disposable { [Symbol.dispose]() { if (this.#id >= 0) { const id = this.#detach(); - sys.row_iter_bsatn_close(id); + this.#backend.rowIterBsatnClose(id); } } } diff --git a/crates/bindings-typescript/src/server/test-utils/backend.ts b/crates/bindings-typescript/src/server/test-utils/backend.ts new file mode 100644 index 00000000000..b723a7894c3 --- /dev/null +++ b/crates/bindings-typescript/src/server/test-utils/backend.ts @@ -0,0 +1,229 @@ +import type { u128, u16, u256, u32 } from 'spacetime:sys@2.0'; +import type { DatastoreBackend } from '../backend'; +import type { + TestRuntimeContext, + TestRuntimeTarget, + TestRuntimeTx, +} from './runtime'; + +class RowIteratorRegistry { + #next = 1; + #iters = new Map(); + + add(rows: Uint8Array[]): number { + const id = this.#next++; + this.#iters.set(id, rows); + return id; + } + + advance(id: number, out: ArrayBuffer): number { + const rows = this.#iters.get(id); + if (!rows || rows.length === 0) { + this.#iters.delete(id); + return 0; + } + + const required = rows.reduce((sum, row) => sum + row.byteLength, 0); + if (required > out.byteLength) { + const err = new Error('iterator output buffer too small') as Error & { + __buffer_too_small__?: number; + }; + err.__buffer_too_small__ = required; + throw err; + } + + const dst = new Uint8Array(out); + let offset = 0; + for (const row of rows) { + dst.set(row, offset); + offset += row.byteLength; + } + this.#iters.delete(id); + return offset; + } + + close(id: number) { + this.#iters.delete(id); + } +} + +export class TestDatastoreBackend implements DatastoreBackend { + readonly #ctx: TestRuntimeContext; + readonly #target: TestRuntimeTarget; + readonly #moduleIdentity: bigint; + readonly #jwtPayloads = new Map(); + readonly #iters = new RowIteratorRegistry(); + + constructor( + ctx: TestRuntimeContext, + target: TestRuntimeTarget, + moduleIdentity: bigint + ) { + this.#ctx = ctx; + this.#target = target; + this.#moduleIdentity = moduleIdentity; + } + + withTransaction(tx: TestRuntimeTx): TestDatastoreBackend { + const next = new TestDatastoreBackend(this.#ctx, tx, this.#moduleIdentity); + for (const [connectionId, payload] of this.#jwtPayloads) { + next.#jwtPayloads.set(connectionId, payload); + } + return next; + } + + setJwtPayload(connectionId: bigint, payload: string) { + this.#jwtPayloads.set(connectionId, payload); + } + + identity(): u256 { + return this.#moduleIdentity as u256; + } + + getJwtPayload(connectionId: u128): Uint8Array { + const payload = this.#jwtPayloads.get(connectionId); + return payload ? new TextEncoder().encode(payload) : new Uint8Array(); + } + + tableIdFromName(name: string): u32 { + return this.#ctx.tableId(name) as u32; + } + + indexIdFromName(name: string): u32 { + return this.#ctx.indexId(name) as u32; + } + + datastoreTableRowCount(tableId: u32): number { + return this.#ctx.tableRowCount(this.#target, tableId); + } + + datastoreTableScanBsatn(tableId: u32): u32 { + return this.#iters.add(this.#ctx.tableRows(this.#target, tableId)) as u32; + } + + datastoreInsertBsatn(tableId: u32, row: ArrayBuffer, rowLen: number) { + return this.#ctx.insertBsatn( + this.#target, + tableId, + new Uint8Array(row, 0, rowLen) + ); + } + + datastoreDeleteAllByEqBsatn( + tableId: u32, + row: ArrayBuffer, + rowLen: number + ): u32 { + return this.#ctx.deleteAllByEqBsatn( + this.#target, + tableId, + new Uint8Array(row, 0, rowLen) + ) as u32; + } + + datastoreIndexScanPointBsatn( + indexId: u32, + point: ArrayBuffer, + pointLen: number + ): u32 { + return this.#iters.add( + this.#ctx.indexScanPointBsatn( + this.#target, + indexId, + new Uint8Array(point, 0, pointLen) + ) + ) as u32; + } + + datastoreIndexScanRangeBsatn( + indexId: u32, + prefix: ArrayBuffer, + prefixLen: u32, + prefixElems: u16, + rstartLen: u32, + rendLen: u32 + ): u32 { + return this.#iters.add( + this.#ctx.indexScanRangeBsatn( + this.#target, + indexId, + new Uint8Array(prefix, 0, prefixLen + rstartLen + rendLen), + prefixElems, + rstartLen, + rendLen + ) + ) as u32; + } + + datastoreDeleteByIndexScanPointBsatn( + indexId: u32, + point: ArrayBuffer, + pointLen: number + ): u32 { + return this.#ctx.deleteByIndexScanPointBsatn( + this.#target, + indexId, + new Uint8Array(point, 0, pointLen) + ) as u32; + } + + datastoreDeleteByIndexScanRangeBsatn( + indexId: u32, + prefix: ArrayBuffer, + prefixLen: u32, + prefixElems: u16, + rstartLen: u32, + rendLen: u32 + ): u32 { + return this.#ctx.deleteByIndexScanRangeBsatn( + this.#target, + indexId, + new Uint8Array(prefix, 0, prefixLen + rstartLen + rendLen), + prefixElems, + rstartLen, + rendLen + ) as u32; + } + + datastoreUpdateBsatn( + tableId: u32, + indexId: u32, + row: ArrayBuffer, + rowLen: number + ) { + return this.#ctx.updateBsatn( + this.#target, + tableId, + indexId, + new Uint8Array(row, 0, rowLen) + ); + } + + datastoreClear(tableId: u32): void { + this.#ctx.clearTable(this.#target, tableId); + } + + rowIterBsatnAdvance(iterId: u32, out: ArrayBuffer): number { + return this.#iters.advance(iterId, out); + } + + rowIterBsatnClose(iterId: u32): void { + this.#iters.close(iterId); + } + + procedureStartMutTx(): bigint { + return 0n; + } + + procedureCommitMutTx(): void { + throw new Error('procedureCommitMutTx is handled by ProcedureTestBackend'); + } + + procedureAbortMutTx(): void { + throw new Error('procedureAbortMutTx is handled by ProcedureTestBackend'); + } + + procedureHttpRequest(): [Uint8Array, Uint8Array] { + throw new Error('test procedure HTTP requests are handled by TestHttpClient'); + } +} diff --git a/crates/bindings-typescript/src/server/test-utils/browser.ts b/crates/bindings-typescript/src/server/test-utils/browser.ts new file mode 100644 index 00000000000..0becba1eb59 --- /dev/null +++ b/crates/bindings-typescript/src/server/test-utils/browser.ts @@ -0,0 +1,18 @@ +export function createModuleTestHarness(): never { + throw new Error( + 'spacetimedb/server/test-utils is only supported in Node tests until the Wasm datastore adapter is implemented.' + ); +} + +export const TestAuth = { + internal(): never { + throw new Error( + 'spacetimedb/server/test-utils is only supported in Node tests until the Wasm datastore adapter is implemented.' + ); + }, + fromJwtPayload(): never { + throw new Error( + 'spacetimedb/server/test-utils is only supported in Node tests until the Wasm datastore adapter is implemented.' + ); + }, +}; diff --git a/crates/bindings-typescript/src/server/test-utils/default_wasm_runtime.ts b/crates/bindings-typescript/src/server/test-utils/default_wasm_runtime.ts new file mode 100644 index 00000000000..e96f2247863 --- /dev/null +++ b/crates/bindings-typescript/src/server/test-utils/default_wasm_runtime.ts @@ -0,0 +1,240 @@ +import { createRequire } from 'node:module'; +import { + createWasmTestRuntime, + type WasmCommitMode, + type WasmPortableDatastore, + type WasmPortableDatastoreModule, + type WasmPortableTransaction, + type WasmValidatedAuth, +} from './wasm'; +import type { + WasmPortableDatastore as GeneratedPortableDatastore, + WasmPortableTransaction as GeneratedPortableTransaction, +} from './portable-datastore-wasm/spacetimedb_portable_datastore_wasm'; + +const require = createRequire(import.meta.url); +const generated = require('./portable-datastore-wasm/spacetimedb_portable_datastore_wasm.cjs') as { + WasmCommitMode: { + Normal: number; + DropEventTableRows: number; + }; + WasmPortableDatastore: new ( + rawModuleDefBsatn: Uint8Array, + moduleIdentityHex: string + ) => GeneratedPortableDatastore; +}; + +class DefaultWasmPortableDatastore implements WasmPortableDatastore { + readonly #inner: GeneratedPortableDatastore; + + constructor(rawModuleDefBsatn: Uint8Array, moduleIdentityHex: string) { + this.#inner = new generated.WasmPortableDatastore( + rawModuleDefBsatn, + moduleIdentityHex + ); + } + + tableId(name: string): number { + return this.#inner.tableId(name); + } + + indexId(name: string): number { + return this.#inner.indexId(name); + } + + beginMutTx(): WasmPortableTransaction { + return this.#inner.beginMutTx() as unknown as WasmPortableTransaction; + } + + commitTx(tx: WasmPortableTransaction, mode: WasmCommitMode): void { + this.#inner.commitTx( + tx as unknown as GeneratedPortableTransaction, + mode === 'DropEventTableRows' + ? generated.WasmCommitMode.DropEventTableRows + : generated.WasmCommitMode.Normal + ); + } + + rollbackTx(tx: WasmPortableTransaction): void { + this.#inner.rollbackTx(tx as unknown as GeneratedPortableTransaction); + } + + reset(): void { + this.#inner.reset(); + } + + tableRowCount(tableId: number): number { + return this.#inner.tableRowCount(tableId); + } + + tableRowCountTx(tx: WasmPortableTransaction, tableId: number): number { + return this.#inner.tableRowCountTx( + tx as unknown as GeneratedPortableTransaction, + tableId + ); + } + + tableRowsBsatn(tableId: number): Uint8Array[] { + return arrayFromWasmRows(this.#inner.tableRowsBsatn(tableId)); + } + + tableRowsBsatnTx(tx: WasmPortableTransaction, tableId: number): Uint8Array[] { + return arrayFromWasmRows( + this.#inner.tableRowsBsatnTx( + tx as unknown as GeneratedPortableTransaction, + tableId + ) + ); + } + + indexScanPointBsatn(indexId: number, point: Uint8Array): Uint8Array[] { + return arrayFromWasmRows(this.#inner.indexScanPointBsatn(indexId, point)); + } + + indexScanPointBsatnTx( + tx: WasmPortableTransaction, + indexId: number, + point: Uint8Array + ): Uint8Array[] { + return arrayFromWasmRows( + this.#inner.indexScanPointBsatnTx( + tx as unknown as GeneratedPortableTransaction, + indexId, + point + ) + ); + } + + indexScanRangeBsatn( + indexId: number, + prefix: Uint8Array, + prefixElems: number, + rstart: Uint8Array, + rend: Uint8Array + ): Uint8Array[] { + return arrayFromWasmRows( + this.#inner.indexScanRangeBsatn( + indexId, + prefix, + prefixElems, + rstart, + rend + ) + ); + } + + indexScanRangeBsatnTx( + tx: WasmPortableTransaction, + indexId: number, + prefix: Uint8Array, + prefixElems: number, + rstart: Uint8Array, + rend: Uint8Array + ): Uint8Array[] { + return arrayFromWasmRows( + this.#inner.indexScanRangeBsatnTx( + tx as unknown as GeneratedPortableTransaction, + indexId, + prefix, + prefixElems, + rstart, + rend + ) + ); + } + + insertBsatnGeneratedCols( + tx: WasmPortableTransaction, + tableId: number, + row: Uint8Array + ): Uint8Array { + return this.#inner.insertBsatnGeneratedCols( + tx as unknown as GeneratedPortableTransaction, + tableId, + row + ); + } + + updateBsatnGeneratedCols( + tx: WasmPortableTransaction, + tableId: number, + indexId: number, + row: Uint8Array + ): Uint8Array { + return this.#inner.updateBsatnGeneratedCols( + tx as unknown as GeneratedPortableTransaction, + tableId, + indexId, + row + ); + } + + deleteByRelBsatn( + tx: WasmPortableTransaction, + tableId: number, + relation: Uint8Array + ): number { + return this.#inner.deleteByRelBsatn( + tx as unknown as GeneratedPortableTransaction, + tableId, + relation + ); + } + + deleteByIndexScanPointBsatn( + tx: WasmPortableTransaction, + indexId: number, + point: Uint8Array + ): number { + return this.#inner.deleteByIndexScanPointBsatn( + tx as unknown as GeneratedPortableTransaction, + indexId, + point + ); + } + + deleteByIndexScanRangeBsatn( + tx: WasmPortableTransaction, + indexId: number, + prefix: Uint8Array, + prefixElems: number, + rstart: Uint8Array, + rend: Uint8Array + ): number { + return this.#inner.deleteByIndexScanRangeBsatn( + tx as unknown as GeneratedPortableTransaction, + indexId, + prefix, + prefixElems, + rstart, + rend + ); + } + + clearTable(tx: WasmPortableTransaction, tableId: number): number { + return this.#inner.clearTable( + tx as unknown as GeneratedPortableTransaction, + tableId + ); + } + + validateJwtPayload( + payload: string, + connectionIdHex: string + ): WasmValidatedAuth { + return this.#inner.validateJwtPayload(payload, connectionIdHex); + } + + runQuery(sql: string, databaseIdentityHex: string): Uint8Array[] { + return arrayFromWasmRows(this.#inner.runQuery(sql, databaseIdentityHex)); + } +} + +function arrayFromWasmRows(rows: Array): Uint8Array[] { + return rows.map(row => row as Uint8Array); +} + +export const defaultWasmTestRuntime = createWasmTestRuntime({ + WasmPortableDatastore: + DefaultWasmPortableDatastore as WasmPortableDatastoreModule['WasmPortableDatastore'], +}); diff --git a/crates/bindings-typescript/src/server/test-utils/index.ts b/crates/bindings-typescript/src/server/test-utils/index.ts new file mode 100644 index 00000000000..eb9c81492c8 --- /dev/null +++ b/crates/bindings-typescript/src/server/test-utils/index.ts @@ -0,0 +1,563 @@ +import { moduleHooks } from 'spacetime:sys@2.0'; + +import { AlgebraicType } from '../../lib/algebraic_type'; +import BinaryReader from '../../lib/binary_reader'; +import type { ConnectionId } from '../../lib/connection_id'; +import { Identity } from '../../lib/identity'; +import type { AuthCtx, JwtClaims, ReducerCtx } from '../../lib/reducers'; +import type { UntypedSchemaDef } from '../../lib/schema'; +import { Timestamp } from '../../lib/timestamp'; +import type { TimeDuration } from '../../lib/time_duration'; +import { + type HttpRequest, + type HttpResponse, +} from '../../lib/autogen/types'; +import type { Table } from '../../lib/table'; +import type { DbView } from '../db_view'; +import { + Headers, + type HttpClient, + type RequestOptions, +} from '../http_internal'; +import { + deserializeHeaders, + serializeHeaders, + serializeMethod, + SyncResponse, +} from '../http_shared'; +import { makeTableView, ReducerCtxImpl } from '../runtime'; +import type { ProcedureCtx } from '../procedures'; +import { ProcedureCtxImpl } from '../procedures'; +import { makeRandomFromSeed } from '../rng'; +import type { Schema } from '../schema'; +import type { AnonymousViewCtx, ViewCtx } from '../views'; +import { + makeQueryBuilder, + getQueryAccessorName, + toSql, + type Query, +} from '../query'; +import { TestDatastoreBackend } from './backend'; +import { + loadTestRuntime, + type TestRuntimeContext, + type TestRuntimeTx, +} from './runtime'; + +const freeze = Object.freeze; +const nextSeed = Symbol('nextSeed'); +const rawModuleDefCache = new WeakMap< + Schema, + WeakMap +>(); + +export interface ModuleTestHarness { + readonly db: DbView; + readonly clock: TestClock; + readonly rng: TestRng; + readonly moduleIdentity: Identity; + + withReducerTx(auth: TestAuth, body: (ctx: ReducerCtx) => T): T; + procedureContext(auth: TestAuth): ProcedureCtx; + procedureContextBuilder(auth: TestAuth): ProcedureContextBuilder; + viewContext(auth: TestAuth): ViewCtx; + anonymousViewContext(): AnonymousViewCtx; + runQuery(query: Query): Row[]; +} + +export class TestClock { + #now: Timestamp; + + constructor(timestamp: Timestamp = Timestamp.UNIX_EPOCH) { + this.#now = timestamp; + } + + now(): Timestamp { + return this.#now; + } + + set(timestamp: Timestamp): void { + this.#now = timestamp; + } + + advance(duration: TimeDuration): void { + this.#now = new Timestamp(this.#now.microsSinceUnixEpoch + duration.micros); + } +} + +export class TestRng { + #seed: number | bigint | Uint8Array | null; + #source: ReturnType; + + constructor(seed: number | bigint | Uint8Array | null = null) { + this.#seed = seed; + this.#source = makeRandomFromSeed(seedToBigInt(seed)); + } + + seed(): number | bigint | Uint8Array | null { + return this.#seed; + } + + setSeed(seed: number | bigint | Uint8Array): void { + this.#seed = seed; + this.#source = makeRandomFromSeed(seedToBigInt(seed)); + } + + clearSeed(): void { + this.#seed = null; + this.#source = makeRandomFromSeed(0n); + } + + [nextSeed](): bigint { + return this.#source.bigintInRange(0n, (1n << 64n) - 1n); + } +} + +function seedToBigInt(seed: number | bigint | Uint8Array | null): bigint { + if (seed == null) return 0n; + if (typeof seed === 'bigint') return seed; + if (typeof seed === 'number') return BigInt(seed); + let value = 0n; + const len = Math.min(seed.byteLength, 8); + for (let i = 0; i < len; i++) { + value |= BigInt(seed[i]) << BigInt(i * 8); + } + return value; +} + +class TestAuthImpl { + private constructor( + readonly kind: 'internal' | 'authenticated', + readonly connectionId: ConnectionId | null, + readonly jwtPayload: string | null + ) {} + + static internal(): TestAuthImpl { + return new TestAuthImpl('internal', null, null); + } + + static fromJwtPayload( + jwtPayload: string, + connectionId: ConnectionId + ): TestAuthImpl { + return new TestAuthImpl('authenticated', connectionId, jwtPayload); + } +} + +export type TestAuth = TestAuthImpl; +export const TestAuth = TestAuthImpl; + +export interface ProcedureContextBuilder { + http( + responder: ( + test: ModuleTestHarness, + req: HttpRequest, + body: Uint8Array + ) => HttpResponse + ): this; + hooks(hooks: ProcedureTestHooks): this; + build(): ProcedureCtx; +} + +export class ProcedureTestHooks { + #afterTxCommit: Array<(test: ModuleTestHarness) => void> = []; + #onSleep: Array<(test: ModuleTestHarness, wakeTime: Timestamp) => void> = + []; + + afterTxCommit(hook: (test: ModuleTestHarness) => void): this { + this.#afterTxCommit.push(hook); + return this; + } + + onSleep( + hook: (test: ModuleTestHarness, wakeTime: Timestamp) => void + ): this { + this.#onSleep.push(hook); + return this; + } + + runAfterTxCommit(test: ModuleTestHarness) { + for (const hook of this.#afterTxCommit) hook(test); + } + + runOnSleep(test: ModuleTestHarness, wakeTime: Timestamp) { + for (const hook of this.#onSleep) hook(test, wakeTime); + } +} + +export function createProcedureTestHooks() { + return new ProcedureTestHooks(); +} + +export function createModuleTestHarness( + schema: Schema, + moduleExports: Record, + opts: { + moduleIdentity?: Identity; + clock?: TestClock; + rngSeed?: bigint | number | Uint8Array | null; + } = {} +): ModuleTestHarness { + const rawModuleDef = describeModule(schema, moduleExports); + const moduleIdentity = opts.moduleIdentity ?? Identity.zero(); + const runtime = loadTestRuntime(); + const runtimeContext = runtime.createContext( + rawModuleDef, + moduleIdentity.__identity__ + ); + + return new ModuleTestHarnessImpl( + schema, + runtimeContext, + moduleIdentity, + opts.clock ?? new TestClock(), + new TestRng(opts.rngSeed ?? null) + ); +} + +function describeModule( + schema: Schema, + moduleExports: Record +): Uint8Array { + let schemaCache = rawModuleDefCache.get(schema); + if (!schemaCache) { + schemaCache = new WeakMap(); + rawModuleDefCache.set(schema, schemaCache); + } + + let rawModuleDef = schemaCache.get(moduleExports); + if (!rawModuleDef) { + const hooks = schema[moduleHooks](moduleExports); + rawModuleDef = hooks.__describe_module__(); + schemaCache.set(moduleExports, rawModuleDef); + } + return rawModuleDef; +} + +class ModuleTestHarnessImpl + implements ModuleTestHarness +{ + readonly clock: TestClock; + readonly rng: TestRng; + readonly moduleIdentity: Identity; + readonly db: DbView; + + #schema: Schema; + #runtime: TestRuntimeContext; + #backend: TestDatastoreBackend; + + constructor( + schema: Schema, + runtime: TestRuntimeContext, + moduleIdentity: Identity, + clock: TestClock, + rng: TestRng + ) { + this.#schema = schema; + this.#runtime = runtime; + this.moduleIdentity = moduleIdentity; + this.clock = clock; + this.rng = rng; + this.#backend = new TestDatastoreBackend( + runtime, + runtime, + moduleIdentity.__identity__ + ); + this.db = makeDbView(schema, this.#backend); + } + + withReducerTx(auth: TestAuth, body: (ctx: ReducerCtx) => T): T { + const tx = this.#runtime.beginTx(); + const backend = this.#backend.withTransaction(tx); + const sender = this.#sender(auth); + if (auth.jwtPayload && auth.connectionId) { + backend.setJwtPayload( + auth.connectionId.__connection_id__, + auth.jwtPayload + ); + } + + try { + const ctx = new ReducerCtxImpl( + sender, + this.clock.now(), + auth.connectionId, + makeDbView(this.#schema, backend), + backend, + this.#authCtx(auth, sender), + makeRandomFromSeed(this.rng[nextSeed]()) + ); + const ret = body(ctx as unknown as ReducerCtx); + this.#runtime.commitTx(tx, 'DropEventTableRows'); + return ret; + } catch (e) { + this.#runtime.abortTx(tx); + throw e; + } + } + + procedureContext(auth: TestAuth): ProcedureCtx { + return this.procedureContextBuilder(auth).build(); + } + + procedureContextBuilder(auth: TestAuth): ProcedureContextBuilder { + return new ProcedureContextBuilderImpl(this, auth); + } + + viewContext(auth: TestAuth): ViewCtx { + return freeze({ + sender: this.#sender(auth), + db: this.db, + from: makeQueryBuilder(this.#schema.schemaType), + }) as ViewCtx; + } + + anonymousViewContext(): AnonymousViewCtx { + return freeze({ + db: this.db, + from: makeQueryBuilder(this.#schema.schemaType), + }) as AnonymousViewCtx; + } + + runQuery(query: Query): Row[] { + const sql = toSql(query); + const accessorName = getQueryAccessorName(query); + const table = this.#schema.schemaType.tables[accessorName]; + if (!table) + throw new Error(`query source table not found: ${accessorName}`); + const rowType = this.#schema.typespace.types[table.tableDef.productTypeRef]; + const rowDeserializer = AlgebraicType.makeDeserializer( + rowType, + this.#schema.typespace + ); + return this.#runtime + .runQuery(sql, this.moduleIdentity.__identity__) + .map(row => rowDeserializer(new BinaryReader(row))) as Row[]; + } + + makeProcedureContext( + auth: TestAuth, + hooks: ProcedureTestHooks, + responder: + | (( + test: ModuleTestHarness, + req: HttpRequest, + body: Uint8Array + ) => HttpResponse) + | undefined + ): ProcedureCtx { + const sender = this.#sender(auth); + const procedureSeed = this.rng[nextSeed](); + const backend = new ProcedureTestBackend( + this, + this.#runtime, + this.#backend, + this.moduleIdentity.__identity__, + hooks + ); + if (auth.jwtPayload && auth.connectionId) { + backend.setJwtPayload( + auth.connectionId.__connection_id__, + auth.jwtPayload + ); + } + + return new ProcedureCtxImpl( + sender, + this.clock.now(), + auth.connectionId, + () => makeDbView(this.#schema, backend.currentBackend()), + backend, + makeHttpClient(this, responder), + duration => this.#sleep(duration, hooks), + makeRandomFromSeed(procedureSeed), + makeRandomFromSeed(procedureSeed ^ 0xa53d5eedc01dca11n) + ) as ProcedureCtx; + } + + #sender(auth: TestAuth): Identity { + if (auth.kind === 'internal') return this.moduleIdentity; + if (!auth.jwtPayload || !auth.connectionId) { + throw new Error( + 'authenticated test auth requires a JWT payload and connection id' + ); + } + const validated = this.#runtime.validateJwtPayload( + auth.jwtPayload, + auth.connectionId.__connection_id__ + ); + return new Identity(validated.senderHex); + } + + #sleep(duration: TimeDuration, hooks: ProcedureTestHooks): void { + const wakeTime = new Timestamp( + this.clock.now().microsSinceUnixEpoch + duration.micros + ); + hooks.runOnSleep(this, wakeTime); + if (this.clock.now().microsSinceUnixEpoch < wakeTime.microsSinceUnixEpoch) { + this.clock.set(wakeTime); + } + } + + #authCtx(auth: TestAuth, sender: Identity): AuthCtx { + if (auth.kind === 'internal') { + return freeze({ isInternal: true, hasJWT: false, jwt: null }); + } + const payload = JSON.parse(auth.jwtPayload!) as Record; + const jwt: JwtClaims = freeze({ + rawPayload: auth.jwtPayload!, + subject: payload.sub as string, + issuer: payload.iss as string, + audience: + payload.aud == null + ? [] + : typeof payload.aud === 'string' + ? [payload.aud] + : (payload.aud as string[]), + identity: sender, + fullPayload: payload as any, + }); + return freeze({ isInternal: false, hasJWT: true, jwt }); + } +} + +class ProcedureContextBuilderImpl + implements ProcedureContextBuilder +{ + #hooks = new ProcedureTestHooks(); + #responder: + | (( + test: ModuleTestHarness, + req: HttpRequest, + body: Uint8Array + ) => HttpResponse) + | undefined; + + constructor( + private readonly test: ModuleTestHarnessImpl, + private readonly auth: TestAuth + ) {} + + http( + responder: ( + test: ModuleTestHarness, + req: HttpRequest, + body: Uint8Array + ) => HttpResponse + ): this { + this.#responder = responder; + return this; + } + + hooks(hooks: ProcedureTestHooks): this { + this.#hooks = hooks; + return this; + } + + build(): ProcedureCtx { + return this.test.makeProcedureContext( + this.auth, + this.#hooks, + this.#responder + ); + } +} + +class ProcedureTestBackend< + S extends UntypedSchemaDef, +> extends TestDatastoreBackend { + #ctx: TestRuntimeContext; + #tx: TestRuntimeTx | null = null; + + constructor( + private readonly test: ModuleTestHarness, + ctx: TestRuntimeContext, + base: TestDatastoreBackend, + moduleIdentity: bigint, + private readonly hooks: ProcedureTestHooks + ) { + super(ctx, ctx, moduleIdentity); + this.#ctx = ctx; + void base; + void hooks; + } + + currentBackend(): TestDatastoreBackend { + return this.#tx ? this.withTransaction(this.#tx) : this; + } + + procedureStartMutTx(): bigint { + this.#tx = this.#ctx.beginTx(); + return this.test.clock.now().microsSinceUnixEpoch; + } + + procedureCommitMutTx(): void { + if (!this.#tx) throw new Error('no active procedure transaction'); + const tx = this.#tx; + this.#tx = null; + this.#ctx.commitTx(tx); + this.hooks.runAfterTxCommit(this.test); + } + + procedureAbortMutTx(): void { + if (!this.#tx) return; + const tx = this.#tx; + this.#tx = null; + this.#ctx.abortTx(tx); + } +} + +function makeDbView( + schema: Schema, + backend: TestDatastoreBackend +): DbView { + return freeze( + Object.fromEntries( + Object.values(schema.schemaType.tables).map(table => [ + table.accessorName, + makeTableView(schema.typespace, table.tableDef, backend) as Table, + ]) + ) + ) as DbView; +} + +function makeHttpClient( + test: ModuleTestHarness, + responder: + | (( + test: ModuleTestHarness, + req: HttpRequest, + body: Uint8Array + ) => HttpResponse) + | undefined +): HttpClient { + const encoder = new TextEncoder(); + return freeze({ + fetch(url: URL | string, init: RequestOptions = {}) { + if (!responder) { + throw new Error('no test HTTP responder configured'); + } + const headers = new Headers(init.headers as any); + const request: HttpRequest = freeze({ + method: serializeMethod(init.method), + headers: serializeHeaders(headers), + timeout: init.timeout, + uri: `${url}`, + version: { tag: 'Http11' as const }, + }); + const body = + init.body == null + ? new Uint8Array() + : typeof init.body === 'string' + ? encoder.encode(init.body) + : new Uint8Array(init.body as any); + const response = responder(test, request, body); + return new SyncResponse(null, { + status: response.code, + statusText: '', + headers: deserializeHeaders(response.headers), + version: response.version, + }); + }, + }); +} diff --git a/crates/bindings-typescript/src/server/test-utils/portable-datastore-wasm/spacetimedb_portable_datastore_wasm.cjs b/crates/bindings-typescript/src/server/test-utils/portable-datastore-wasm/spacetimedb_portable_datastore_wasm.cjs new file mode 100644 index 00000000000..9ece106efb7 --- /dev/null +++ b/crates/bindings-typescript/src/server/test-utils/portable-datastore-wasm/spacetimedb_portable_datastore_wasm.cjs @@ -0,0 +1,599 @@ + +let imports = {}; +imports['__wbindgen_placeholder__'] = module.exports; + +let cachedUint8ArrayMemory0 = null; + +function getUint8ArrayMemory0() { + if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) { + cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer); + } + return cachedUint8ArrayMemory0; +} + +function getArrayU8FromWasm0(ptr, len) { + ptr = ptr >>> 0; + return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len); +} + +let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }); + +cachedTextDecoder.decode(); + +function decodeText(ptr, len) { + return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len)); +} + +function getStringFromWasm0(ptr, len) { + ptr = ptr >>> 0; + return decodeText(ptr, len); +} + +function _assertClass(instance, klass) { + if (!(instance instanceof klass)) { + throw new Error(`expected instance of ${klass.name}`); + } +} + +function takeFromExternrefTable0(idx) { + const value = wasm.__wbindgen_export_0.get(idx); + wasm.__externref_table_dealloc(idx); + return value; +} + +let WASM_VECTOR_LEN = 0; + +function passArray8ToWasm0(arg, malloc) { + const ptr = malloc(arg.length * 1, 1) >>> 0; + getUint8ArrayMemory0().set(arg, ptr / 1); + WASM_VECTOR_LEN = arg.length; + return ptr; +} + +const cachedTextEncoder = new TextEncoder(); + +if (!('encodeInto' in cachedTextEncoder)) { + cachedTextEncoder.encodeInto = function (arg, view) { + const buf = cachedTextEncoder.encode(arg); + view.set(buf); + return { + read: arg.length, + written: buf.length + }; + } +} + +function passStringToWasm0(arg, malloc, realloc) { + + if (realloc === undefined) { + const buf = cachedTextEncoder.encode(arg); + const ptr = malloc(buf.length, 1) >>> 0; + getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf); + WASM_VECTOR_LEN = buf.length; + return ptr; + } + + let len = arg.length; + let ptr = malloc(len, 1) >>> 0; + + const mem = getUint8ArrayMemory0(); + + let offset = 0; + + for (; offset < len; offset++) { + const code = arg.charCodeAt(offset); + if (code > 0x7F) break; + mem[ptr + offset] = code; + } + + if (offset !== len) { + if (offset !== 0) { + arg = arg.slice(offset); + } + ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0; + const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len); + const ret = cachedTextEncoder.encodeInto(arg, view); + + offset += ret.written; + ptr = realloc(ptr, len, offset, 1) >>> 0; + } + + WASM_VECTOR_LEN = offset; + return ptr; +} +/** + * @enum {0 | 1} + */ +exports.WasmCommitMode = Object.freeze({ + Normal: 0, "0": "Normal", + DropEventTableRows: 1, "1": "DropEventTableRows", +}); + +const WasmPortableDatastoreFinalization = (typeof FinalizationRegistry === 'undefined') + ? { register: () => {}, unregister: () => {} } + : new FinalizationRegistry(ptr => wasm.__wbg_wasmportabledatastore_free(ptr >>> 0, 1)); + +class WasmPortableDatastore { + + __destroy_into_raw() { + const ptr = this.__wbg_ptr; + this.__wbg_ptr = 0; + WasmPortableDatastoreFinalization.unregister(this); + return ptr; + } + + free() { + const ptr = this.__destroy_into_raw(); + wasm.__wbg_wasmportabledatastore_free(ptr, 0); + } + /** + * @param {WasmPortableTransaction} tx + * @param {number} table_id + * @returns {number} + */ + clearTable(tx, table_id) { + _assertClass(tx, WasmPortableTransaction); + const ret = wasm.wasmportabledatastore_clearTable(this.__wbg_ptr, tx.__wbg_ptr, table_id); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return ret[0]; + } + /** + * @param {WasmPortableTransaction} tx + */ + rollbackTx(tx) { + _assertClass(tx, WasmPortableTransaction); + const ret = wasm.wasmportabledatastore_rollbackTx(this.__wbg_ptr, tx.__wbg_ptr); + if (ret[1]) { + throw takeFromExternrefTable0(ret[0]); + } + } + /** + * @returns {WasmPortableTransaction} + */ + beginMutTx() { + const ret = wasm.wasmportabledatastore_beginMutTx(this.__wbg_ptr); + return WasmPortableTransaction.__wrap(ret); + } + /** + * @param {number} table_id + * @returns {number} + */ + tableRowCount(table_id) { + const ret = wasm.wasmportabledatastore_tableRowCount(this.__wbg_ptr, table_id); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return ret[0]; + } + /** + * @param {number} table_id + * @returns {Array} + */ + tableRowsBsatn(table_id) { + const ret = wasm.wasmportabledatastore_tableRowsBsatn(this.__wbg_ptr, table_id); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return takeFromExternrefTable0(ret[0]); + } + /** + * @param {WasmPortableTransaction} tx + * @param {number} table_id + * @returns {number} + */ + tableRowCountTx(tx, table_id) { + _assertClass(tx, WasmPortableTransaction); + const ret = wasm.wasmportabledatastore_tableRowCountTx(this.__wbg_ptr, tx.__wbg_ptr, table_id); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return ret[0]; + } + /** + * @param {WasmPortableTransaction} tx + * @param {number} table_id + * @param {Uint8Array} relation + * @returns {number} + */ + deleteByRelBsatn(tx, table_id, relation) { + _assertClass(tx, WasmPortableTransaction); + const ptr0 = passArray8ToWasm0(relation, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.wasmportabledatastore_deleteByRelBsatn(this.__wbg_ptr, tx.__wbg_ptr, table_id, ptr0, len0); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return ret[0] >>> 0; + } + /** + * @param {WasmPortableTransaction} tx + * @param {number} table_id + * @returns {Array} + */ + tableRowsBsatnTx(tx, table_id) { + _assertClass(tx, WasmPortableTransaction); + const ret = wasm.wasmportabledatastore_tableRowsBsatnTx(this.__wbg_ptr, tx.__wbg_ptr, table_id); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return takeFromExternrefTable0(ret[0]); + } + /** + * @param {string} payload + * @param {string} connection_id_hex + * @returns {WasmValidatedAuth} + */ + validateJwtPayload(payload, connection_id_hex) { + const ptr0 = passStringToWasm0(payload, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passStringToWasm0(connection_id_hex, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.wasmportabledatastore_validateJwtPayload(this.__wbg_ptr, ptr0, len0, ptr1, len1); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return WasmValidatedAuth.__wrap(ret[0]); + } + /** + * @param {number} index_id + * @param {Uint8Array} point + * @returns {Array} + */ + indexScanPointBsatn(index_id, point) { + const ptr0 = passArray8ToWasm0(point, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.wasmportabledatastore_indexScanPointBsatn(this.__wbg_ptr, index_id, ptr0, len0); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return takeFromExternrefTable0(ret[0]); + } + /** + * @param {number} index_id + * @param {Uint8Array} prefix + * @param {number} prefix_elems + * @param {Uint8Array} rstart + * @param {Uint8Array} rend + * @returns {Array} + */ + indexScanRangeBsatn(index_id, prefix, prefix_elems, rstart, rend) { + const ptr0 = passArray8ToWasm0(prefix, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passArray8ToWasm0(rstart, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + const ptr2 = passArray8ToWasm0(rend, wasm.__wbindgen_malloc); + const len2 = WASM_VECTOR_LEN; + const ret = wasm.wasmportabledatastore_indexScanRangeBsatn(this.__wbg_ptr, index_id, ptr0, len0, prefix_elems, ptr1, len1, ptr2, len2); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return takeFromExternrefTable0(ret[0]); + } + /** + * @param {WasmPortableTransaction} tx + * @param {number} index_id + * @param {Uint8Array} point + * @returns {Array} + */ + indexScanPointBsatnTx(tx, index_id, point) { + _assertClass(tx, WasmPortableTransaction); + const ptr0 = passArray8ToWasm0(point, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.wasmportabledatastore_indexScanPointBsatnTx(this.__wbg_ptr, tx.__wbg_ptr, index_id, ptr0, len0); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return takeFromExternrefTable0(ret[0]); + } + /** + * @param {WasmPortableTransaction} tx + * @param {number} index_id + * @param {Uint8Array} prefix + * @param {number} prefix_elems + * @param {Uint8Array} rstart + * @param {Uint8Array} rend + * @returns {Array} + */ + indexScanRangeBsatnTx(tx, index_id, prefix, prefix_elems, rstart, rend) { + _assertClass(tx, WasmPortableTransaction); + const ptr0 = passArray8ToWasm0(prefix, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passArray8ToWasm0(rstart, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + const ptr2 = passArray8ToWasm0(rend, wasm.__wbindgen_malloc); + const len2 = WASM_VECTOR_LEN; + const ret = wasm.wasmportabledatastore_indexScanRangeBsatnTx(this.__wbg_ptr, tx.__wbg_ptr, index_id, ptr0, len0, prefix_elems, ptr1, len1, ptr2, len2); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return takeFromExternrefTable0(ret[0]); + } + /** + * @param {WasmPortableTransaction} tx + * @param {number} table_id + * @param {Uint8Array} row + * @returns {Uint8Array} + */ + insertBsatnGeneratedCols(tx, table_id, row) { + _assertClass(tx, WasmPortableTransaction); + const ptr0 = passArray8ToWasm0(row, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.wasmportabledatastore_insertBsatnGeneratedCols(this.__wbg_ptr, tx.__wbg_ptr, table_id, ptr0, len0); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v2; + } + /** + * @param {WasmPortableTransaction} tx + * @param {number} table_id + * @param {number} index_id + * @param {Uint8Array} row + * @returns {Uint8Array} + */ + updateBsatnGeneratedCols(tx, table_id, index_id, row) { + _assertClass(tx, WasmPortableTransaction); + const ptr0 = passArray8ToWasm0(row, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.wasmportabledatastore_updateBsatnGeneratedCols(this.__wbg_ptr, tx.__wbg_ptr, table_id, index_id, ptr0, len0); + if (ret[3]) { + throw takeFromExternrefTable0(ret[2]); + } + var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + return v2; + } + /** + * @param {WasmPortableTransaction} tx + * @param {number} index_id + * @param {Uint8Array} point + * @returns {number} + */ + deleteByIndexScanPointBsatn(tx, index_id, point) { + _assertClass(tx, WasmPortableTransaction); + const ptr0 = passArray8ToWasm0(point, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.wasmportabledatastore_deleteByIndexScanPointBsatn(this.__wbg_ptr, tx.__wbg_ptr, index_id, ptr0, len0); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return ret[0] >>> 0; + } + /** + * @param {WasmPortableTransaction} tx + * @param {number} index_id + * @param {Uint8Array} prefix + * @param {number} prefix_elems + * @param {Uint8Array} rstart + * @param {Uint8Array} rend + * @returns {number} + */ + deleteByIndexScanRangeBsatn(tx, index_id, prefix, prefix_elems, rstart, rend) { + _assertClass(tx, WasmPortableTransaction); + const ptr0 = passArray8ToWasm0(prefix, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passArray8ToWasm0(rstart, wasm.__wbindgen_malloc); + const len1 = WASM_VECTOR_LEN; + const ptr2 = passArray8ToWasm0(rend, wasm.__wbindgen_malloc); + const len2 = WASM_VECTOR_LEN; + const ret = wasm.wasmportabledatastore_deleteByIndexScanRangeBsatn(this.__wbg_ptr, tx.__wbg_ptr, index_id, ptr0, len0, prefix_elems, ptr1, len1, ptr2, len2); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return ret[0] >>> 0; + } + /** + * @param {Uint8Array} raw_module_def_bsatn + * @param {string} module_identity_hex + */ + constructor(raw_module_def_bsatn, module_identity_hex) { + const ptr0 = passArray8ToWasm0(raw_module_def_bsatn, wasm.__wbindgen_malloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passStringToWasm0(module_identity_hex, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.wasmportabledatastore_new(ptr0, len0, ptr1, len1); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + this.__wbg_ptr = ret[0] >>> 0; + WasmPortableDatastoreFinalization.register(this, this.__wbg_ptr, this); + return this; + } + reset() { + const ret = wasm.wasmportabledatastore_reset(this.__wbg_ptr); + if (ret[1]) { + throw takeFromExternrefTable0(ret[0]); + } + } + /** + * @param {string} index_name + * @returns {number} + */ + indexId(index_name) { + const ptr0 = passStringToWasm0(index_name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.wasmportabledatastore_indexId(this.__wbg_ptr, ptr0, len0); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return ret[0] >>> 0; + } + /** + * @param {string} table_name + * @returns {number} + */ + tableId(table_name) { + const ptr0 = passStringToWasm0(table_name, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ret = wasm.wasmportabledatastore_tableId(this.__wbg_ptr, ptr0, len0); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return ret[0] >>> 0; + } + /** + * @param {WasmPortableTransaction} tx + * @param {WasmCommitMode} mode + */ + commitTx(tx, mode) { + _assertClass(tx, WasmPortableTransaction); + const ret = wasm.wasmportabledatastore_commitTx(this.__wbg_ptr, tx.__wbg_ptr, mode); + if (ret[1]) { + throw takeFromExternrefTable0(ret[0]); + } + } + /** + * @param {string} sql + * @param {string} database_identity_hex + * @returns {Array} + */ + runQuery(sql, database_identity_hex) { + const ptr0 = passStringToWasm0(sql, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len0 = WASM_VECTOR_LEN; + const ptr1 = passStringToWasm0(database_identity_hex, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); + const len1 = WASM_VECTOR_LEN; + const ret = wasm.wasmportabledatastore_runQuery(this.__wbg_ptr, ptr0, len0, ptr1, len1); + if (ret[2]) { + throw takeFromExternrefTable0(ret[1]); + } + return takeFromExternrefTable0(ret[0]); + } +} +if (Symbol.dispose) WasmPortableDatastore.prototype[Symbol.dispose] = WasmPortableDatastore.prototype.free; + +exports.WasmPortableDatastore = WasmPortableDatastore; + +const WasmPortableTransactionFinalization = (typeof FinalizationRegistry === 'undefined') + ? { register: () => {}, unregister: () => {} } + : new FinalizationRegistry(ptr => wasm.__wbg_wasmportabletransaction_free(ptr >>> 0, 1)); + +class WasmPortableTransaction { + + static __wrap(ptr) { + ptr = ptr >>> 0; + const obj = Object.create(WasmPortableTransaction.prototype); + obj.__wbg_ptr = ptr; + WasmPortableTransactionFinalization.register(obj, obj.__wbg_ptr, obj); + return obj; + } + + __destroy_into_raw() { + const ptr = this.__wbg_ptr; + this.__wbg_ptr = 0; + WasmPortableTransactionFinalization.unregister(this); + return ptr; + } + + free() { + const ptr = this.__destroy_into_raw(); + wasm.__wbg_wasmportabletransaction_free(ptr, 0); + } +} +if (Symbol.dispose) WasmPortableTransaction.prototype[Symbol.dispose] = WasmPortableTransaction.prototype.free; + +exports.WasmPortableTransaction = WasmPortableTransaction; + +const WasmValidatedAuthFinalization = (typeof FinalizationRegistry === 'undefined') + ? { register: () => {}, unregister: () => {} } + : new FinalizationRegistry(ptr => wasm.__wbg_wasmvalidatedauth_free(ptr >>> 0, 1)); + +class WasmValidatedAuth { + + static __wrap(ptr) { + ptr = ptr >>> 0; + const obj = Object.create(WasmValidatedAuth.prototype); + obj.__wbg_ptr = ptr; + WasmValidatedAuthFinalization.register(obj, obj.__wbg_ptr, obj); + return obj; + } + + __destroy_into_raw() { + const ptr = this.__wbg_ptr; + this.__wbg_ptr = 0; + WasmValidatedAuthFinalization.unregister(this); + return ptr; + } + + free() { + const ptr = this.__destroy_into_raw(); + wasm.__wbg_wasmvalidatedauth_free(ptr, 0); + } + /** + * @returns {string} + */ + get senderHex() { + let deferred1_0; + let deferred1_1; + try { + const ret = wasm.wasmvalidatedauth_senderHex(this.__wbg_ptr); + deferred1_0 = ret[0]; + deferred1_1 = ret[1]; + return getStringFromWasm0(ret[0], ret[1]); + } finally { + wasm.__wbindgen_free(deferred1_0, deferred1_1, 1); + } + } + /** + * @returns {string | undefined} + */ + get connectionIdHex() { + const ret = wasm.wasmvalidatedauth_connectionIdHex(this.__wbg_ptr); + let v1; + if (ret[0] !== 0) { + v1 = getStringFromWasm0(ret[0], ret[1]).slice(); + wasm.__wbindgen_free(ret[0], ret[1] * 1, 1); + } + return v1; + } +} +if (Symbol.dispose) WasmValidatedAuth.prototype[Symbol.dispose] = WasmValidatedAuth.prototype.free; + +exports.WasmValidatedAuth = WasmValidatedAuth; + +exports.__wbg_new_1f3a344cf3123716 = function() { + const ret = new Array(); + return ret; +}; + +exports.__wbg_newfromslice_074c56947bd43469 = function(arg0, arg1) { + const ret = new Uint8Array(getArrayU8FromWasm0(arg0, arg1)); + return ret; +}; + +exports.__wbg_push_330b2eb93e4e1212 = function(arg0, arg1) { + const ret = arg0.push(arg1); + return ret; +}; + +exports.__wbg_wbindgenthrow_451ec1a8469d7eb6 = function(arg0, arg1) { + throw new Error(getStringFromWasm0(arg0, arg1)); +}; + +exports.__wbindgen_cast_2241b6af4c4b2941 = function(arg0, arg1) { + // Cast intrinsic for `Ref(String) -> Externref`. + const ret = getStringFromWasm0(arg0, arg1); + return ret; +}; + +exports.__wbindgen_init_externref_table = function() { + const table = wasm.__wbindgen_export_0; + const offset = table.grow(4); + table.set(0, undefined); + table.set(offset + 0, undefined); + table.set(offset + 1, null); + table.set(offset + 2, true); + table.set(offset + 3, false); + ; +}; + +const wasmPath = `${__dirname}/spacetimedb_portable_datastore_wasm_bg.wasm`; +const wasmBytes = require('fs').readFileSync(wasmPath); +const wasmModule = new WebAssembly.Module(wasmBytes); +const wasm = exports.__wasm = new WebAssembly.Instance(wasmModule, imports).exports; + +wasm.__wbindgen_start(); + diff --git a/crates/bindings-typescript/src/server/test-utils/portable-datastore-wasm/spacetimedb_portable_datastore_wasm.d.ts b/crates/bindings-typescript/src/server/test-utils/portable-datastore-wasm/spacetimedb_portable_datastore_wasm.d.ts new file mode 100644 index 00000000000..3431909430c --- /dev/null +++ b/crates/bindings-typescript/src/server/test-utils/portable-datastore-wasm/spacetimedb_portable_datastore_wasm.d.ts @@ -0,0 +1,45 @@ +/* tslint:disable */ +/* eslint-disable */ +export enum WasmCommitMode { + Normal = 0, + DropEventTableRows = 1, +} +export class WasmPortableDatastore { + free(): void; + [Symbol.dispose](): void; + clearTable(tx: WasmPortableTransaction, table_id: number): number; + rollbackTx(tx: WasmPortableTransaction): void; + beginMutTx(): WasmPortableTransaction; + tableRowCount(table_id: number): number; + tableRowsBsatn(table_id: number): Array; + tableRowCountTx(tx: WasmPortableTransaction, table_id: number): number; + deleteByRelBsatn(tx: WasmPortableTransaction, table_id: number, relation: Uint8Array): number; + tableRowsBsatnTx(tx: WasmPortableTransaction, table_id: number): Array; + validateJwtPayload(payload: string, connection_id_hex: string): WasmValidatedAuth; + indexScanPointBsatn(index_id: number, point: Uint8Array): Array; + indexScanRangeBsatn(index_id: number, prefix: Uint8Array, prefix_elems: number, rstart: Uint8Array, rend: Uint8Array): Array; + indexScanPointBsatnTx(tx: WasmPortableTransaction, index_id: number, point: Uint8Array): Array; + indexScanRangeBsatnTx(tx: WasmPortableTransaction, index_id: number, prefix: Uint8Array, prefix_elems: number, rstart: Uint8Array, rend: Uint8Array): Array; + insertBsatnGeneratedCols(tx: WasmPortableTransaction, table_id: number, row: Uint8Array): Uint8Array; + updateBsatnGeneratedCols(tx: WasmPortableTransaction, table_id: number, index_id: number, row: Uint8Array): Uint8Array; + deleteByIndexScanPointBsatn(tx: WasmPortableTransaction, index_id: number, point: Uint8Array): number; + deleteByIndexScanRangeBsatn(tx: WasmPortableTransaction, index_id: number, prefix: Uint8Array, prefix_elems: number, rstart: Uint8Array, rend: Uint8Array): number; + constructor(raw_module_def_bsatn: Uint8Array, module_identity_hex: string); + reset(): void; + indexId(index_name: string): number; + tableId(table_name: string): number; + commitTx(tx: WasmPortableTransaction, mode: WasmCommitMode): void; + runQuery(sql: string, database_identity_hex: string): Array; +} +export class WasmPortableTransaction { + private constructor(); + free(): void; + [Symbol.dispose](): void; +} +export class WasmValidatedAuth { + private constructor(); + free(): void; + [Symbol.dispose](): void; + readonly senderHex: string; + readonly connectionIdHex: string | undefined; +} diff --git a/crates/bindings-typescript/src/server/test-utils/portable-datastore-wasm/spacetimedb_portable_datastore_wasm_bg.wasm b/crates/bindings-typescript/src/server/test-utils/portable-datastore-wasm/spacetimedb_portable_datastore_wasm_bg.wasm new file mode 100644 index 00000000000..5a39717fefb Binary files /dev/null and b/crates/bindings-typescript/src/server/test-utils/portable-datastore-wasm/spacetimedb_portable_datastore_wasm_bg.wasm differ diff --git a/crates/bindings-typescript/src/server/test-utils/runtime.ts b/crates/bindings-typescript/src/server/test-utils/runtime.ts new file mode 100644 index 00000000000..cb57dbb8447 --- /dev/null +++ b/crates/bindings-typescript/src/server/test-utils/runtime.ts @@ -0,0 +1,82 @@ +import { defaultWasmTestRuntime } from './default_wasm_runtime'; + +export interface TestRuntime { + createContext(moduleDef: Uint8Array, moduleIdentity: bigint): TestRuntimeContext; +} + +export type TestRuntimeCommitMode = 'Normal' | 'DropEventTableRows'; + +export interface TestRuntimeContext { + reset(): void; + tableId(name: string): number; + indexId(name: string): number; + tableRowCount(target: TestRuntimeTarget, tableId: number): number; + tableRows(target: TestRuntimeTarget, tableId: number): Uint8Array[]; + insertBsatn( + target: TestRuntimeTarget, + tableId: number, + row: Uint8Array + ): Uint8Array; + deleteAllByEqBsatn( + target: TestRuntimeTarget, + tableId: number, + row: Uint8Array + ): number; + indexScanPointBsatn( + target: TestRuntimeTarget, + indexId: number, + point: Uint8Array + ): Uint8Array[]; + indexScanRangeBsatn( + target: TestRuntimeTarget, + indexId: number, + prefix: Uint8Array, + prefixElems: number, + rstartLen: number, + rendLen: number + ): Uint8Array[]; + deleteByIndexScanPointBsatn( + target: TestRuntimeTarget, + indexId: number, + point: Uint8Array + ): number; + deleteByIndexScanRangeBsatn( + target: TestRuntimeTarget, + indexId: number, + prefix: Uint8Array, + prefixElems: number, + rstartLen: number, + rendLen: number + ): number; + updateBsatn( + target: TestRuntimeTarget, + tableId: number, + indexId: number, + row: Uint8Array + ): Uint8Array; + clearTable(target: TestRuntimeTarget, tableId: number): number; + runQuery(sql: string, databaseIdentity: bigint): Uint8Array[]; + validateJwtPayload( + jwtPayload: string, + connectionId: bigint + ): { senderHex: string; connectionIdHex: string | undefined }; + beginTx(): TestRuntimeTx; + commitTx(tx: TestRuntimeTx, mode?: TestRuntimeCommitMode): void; + abortTx(tx: TestRuntimeTx): void; +} + +export type TestRuntimeTarget = TestRuntimeContext | TestRuntimeTx; + +export interface TestRuntimeTx { + readonly __testRuntimeTxBrand: unique symbol; +} + +declare global { + // Tests may override the runtime to exercise the adapter boundary. + // eslint-disable-next-line no-var + var __spacetimedbTestRuntime: TestRuntime | undefined; +} + +export function loadTestRuntime(): TestRuntime { + return globalThis.__spacetimedbTestRuntime ?? defaultWasmTestRuntime; +} diff --git a/crates/bindings-typescript/src/server/test-utils/vitest.ts b/crates/bindings-typescript/src/server/test-utils/vitest.ts new file mode 100644 index 00000000000..14a0c5cd716 --- /dev/null +++ b/crates/bindings-typescript/src/server/test-utils/vitest.ts @@ -0,0 +1,47 @@ +import type { Plugin } from 'vite'; + +export function spacetimedbModuleTestPlugin(): Plugin { + return { + name: 'spacetimedb-module-test', + enforce: 'pre', + resolveId(id) { + if (id === 'spacetime:sys@2.0' || id === 'spacetime:sys@2.1') { + return '\0spacetimedb-module-test-sys'; + } + return null; + }, + load(id) { + if (id !== '\0spacetimedb-module-test-sys') return null; + return ` + export const moduleHooks = Symbol.for('spacetimedb.moduleHooks'); + const unsupported = name => () => { + throw new Error('Unsupported SpacetimeDB host syscall in module unit test: ' + name); + }; + export const identity = unsupported('identity'); + export const get_jwt_payload = unsupported('get_jwt_payload'); + export const table_id_from_name = unsupported('table_id_from_name'); + export const index_id_from_name = unsupported('index_id_from_name'); + export const datastore_table_row_count = unsupported('datastore_table_row_count'); + export const datastore_table_scan_bsatn = unsupported('datastore_table_scan_bsatn'); + export const datastore_insert_bsatn = unsupported('datastore_insert_bsatn'); + export const datastore_delete_all_by_eq_bsatn = unsupported('datastore_delete_all_by_eq_bsatn'); + export const datastore_index_scan_point_bsatn = unsupported('datastore_index_scan_point_bsatn'); + export const datastore_index_scan_range_bsatn = unsupported('datastore_index_scan_range_bsatn'); + export const datastore_delete_by_index_scan_point_bsatn = unsupported('datastore_delete_by_index_scan_point_bsatn'); + export const datastore_delete_by_index_scan_range_bsatn = unsupported('datastore_delete_by_index_scan_range_bsatn'); + export const datastore_update_bsatn = unsupported('datastore_update_bsatn'); + export const datastore_clear = unsupported('datastore_clear'); + export const row_iter_bsatn_advance = unsupported('row_iter_bsatn_advance'); + export const row_iter_bsatn_close = unsupported('row_iter_bsatn_close'); + export const procedure_start_mut_tx = unsupported('procedure_start_mut_tx'); + export const procedure_commit_mut_tx = unsupported('procedure_commit_mut_tx'); + export const procedure_abort_mut_tx = unsupported('procedure_abort_mut_tx'); + export const procedure_http_request = unsupported('procedure_http_request'); + export const console_log = () => {}; + export const console_timer_start = () => 0; + export const console_timer_end = () => {}; + export const volatile_nonatomic_schedule_immediate = unsupported('volatile_nonatomic_schedule_immediate'); + `; + }, + }; +} diff --git a/crates/bindings-typescript/src/server/test-utils/wasm.ts b/crates/bindings-typescript/src/server/test-utils/wasm.ts new file mode 100644 index 00000000000..40fa9d3b2b9 --- /dev/null +++ b/crates/bindings-typescript/src/server/test-utils/wasm.ts @@ -0,0 +1,334 @@ +import type { + TestRuntimeCommitMode, + TestRuntimeContext, + TestRuntimeTarget, + TestRuntime, + TestRuntimeTx, +} from './runtime'; + +export type WasmCommitMode = 'Normal' | 'DropEventTableRows'; + +export interface WasmValidatedAuth { + readonly senderHex: string; + readonly connectionIdHex: string | undefined; +} + +export interface WasmPortableTransaction extends TestRuntimeTx { + readonly __wasmTxBrand?: unique symbol; +} + +export interface WasmPortableDatastore { + tableId(name: string): number; + indexId(name: string): number; + beginMutTx(): WasmPortableTransaction; + commitTx(tx: WasmPortableTransaction, mode: WasmCommitMode): void; + rollbackTx(tx: WasmPortableTransaction): void; + reset(): void; + + tableRowCount(tableId: number): number; + tableRowCountTx(tx: WasmPortableTransaction, tableId: number): number; + tableRowsBsatn(tableId: number): Uint8Array[]; + tableRowsBsatnTx(tx: WasmPortableTransaction, tableId: number): Uint8Array[]; + indexScanPointBsatn(indexId: number, point: Uint8Array): Uint8Array[]; + indexScanPointBsatnTx( + tx: WasmPortableTransaction, + indexId: number, + point: Uint8Array + ): Uint8Array[]; + indexScanRangeBsatn( + indexId: number, + prefix: Uint8Array, + prefixElems: number, + rstart: Uint8Array, + rend: Uint8Array + ): Uint8Array[]; + indexScanRangeBsatnTx( + tx: WasmPortableTransaction, + indexId: number, + prefix: Uint8Array, + prefixElems: number, + rstart: Uint8Array, + rend: Uint8Array + ): Uint8Array[]; + + insertBsatnGeneratedCols( + tx: WasmPortableTransaction, + tableId: number, + row: Uint8Array + ): Uint8Array; + updateBsatnGeneratedCols( + tx: WasmPortableTransaction, + tableId: number, + indexId: number, + row: Uint8Array + ): Uint8Array; + deleteByRelBsatn( + tx: WasmPortableTransaction, + tableId: number, + relation: Uint8Array + ): number; + deleteByIndexScanPointBsatn( + tx: WasmPortableTransaction, + indexId: number, + point: Uint8Array + ): number; + deleteByIndexScanRangeBsatn( + tx: WasmPortableTransaction, + indexId: number, + prefix: Uint8Array, + prefixElems: number, + rstart: Uint8Array, + rend: Uint8Array + ): number; + clearTable(tx: WasmPortableTransaction, tableId: number): number; + + validateJwtPayload( + payload: string, + connectionIdHex: string + ): WasmValidatedAuth; + runQuery(sql: string, databaseIdentityHex: string): Uint8Array[]; +} + +export interface WasmPortableDatastoreModule { + WasmPortableDatastore: new ( + rawModuleDefBsatn: Uint8Array, + moduleIdentityHex: string + ) => WasmPortableDatastore; +} + +export function createWasmTestRuntime( + wasm: WasmPortableDatastoreModule +): TestRuntime { + return { + createContext(moduleDef, moduleIdentity) { + return new WasmContext( + new wasm.WasmPortableDatastore( + moduleDef, + bigintToFixedHex(moduleIdentity, 32) + ) + ); + }, + }; +} + +class WasmContext implements TestRuntimeContext { + readonly #ds: WasmPortableDatastore; + + constructor(ds: WasmPortableDatastore) { + this.#ds = ds; + } + + reset(): void { + this.#ds.reset(); + } + + tableId(name: string): number { + return this.#ds.tableId(name); + } + + indexId(name: string): number { + return this.#ds.indexId(name); + } + + tableRowCount(target: TestRuntimeTarget, tableId: number): number { + return isWasmTx(target) + ? this.#ds.tableRowCountTx(target, tableId) + : this.#ds.tableRowCount(tableId); + } + + tableRows(target: TestRuntimeTarget, tableId: number): Uint8Array[] { + return isWasmTx(target) + ? this.#ds.tableRowsBsatnTx(target, tableId) + : this.#ds.tableRowsBsatn(tableId); + } + + insertBsatn( + target: TestRuntimeTarget, + tableId: number, + row: Uint8Array + ): Uint8Array { + return this.#withMutTx(target, tx => + this.#ds.insertBsatnGeneratedCols(tx, tableId, row) + ); + } + + deleteAllByEqBsatn( + target: TestRuntimeTarget, + tableId: number, + relation: Uint8Array + ): number { + return this.#withMutTx(target, tx => + this.#ds.deleteByRelBsatn(tx, tableId, relation) + ); + } + + indexScanPointBsatn( + target: TestRuntimeTarget, + indexId: number, + point: Uint8Array + ): Uint8Array[] { + return isWasmTx(target) + ? this.#ds.indexScanPointBsatnTx(target, indexId, point) + : this.#ds.indexScanPointBsatn(indexId, point); + } + + indexScanRangeBsatn( + target: TestRuntimeTarget, + indexId: number, + buffer: Uint8Array, + prefixElems: number, + rstartLen: number, + rendLen: number + ): Uint8Array[] { + const { prefix, rstart, rend } = splitRangeBuffer( + buffer, + rstartLen, + rendLen + ); + return isWasmTx(target) + ? this.#ds.indexScanRangeBsatnTx( + target, + indexId, + prefix, + prefixElems, + rstart, + rend + ) + : this.#ds.indexScanRangeBsatn( + indexId, + prefix, + prefixElems, + rstart, + rend + ); + } + + deleteByIndexScanPointBsatn( + target: TestRuntimeTarget, + indexId: number, + point: Uint8Array + ): number { + return this.#withMutTx(target, tx => + this.#ds.deleteByIndexScanPointBsatn(tx, indexId, point) + ); + } + + deleteByIndexScanRangeBsatn( + target: TestRuntimeTarget, + indexId: number, + buffer: Uint8Array, + prefixElems: number, + rstartLen: number, + rendLen: number + ): number { + const { prefix, rstart, rend } = splitRangeBuffer( + buffer, + rstartLen, + rendLen + ); + return this.#withMutTx(target, tx => + this.#ds.deleteByIndexScanRangeBsatn( + tx, + indexId, + prefix, + prefixElems, + rstart, + rend + ) + ); + } + + updateBsatn( + target: TestRuntimeTarget, + tableId: number, + indexId: number, + row: Uint8Array + ): Uint8Array { + return this.#withMutTx(target, tx => + this.#ds.updateBsatnGeneratedCols(tx, tableId, indexId, row) + ); + } + + clearTable(target: TestRuntimeTarget, tableId: number): number { + return this.#withMutTx(target, tx => this.#ds.clearTable(tx, tableId)); + } + + runQuery(sql: string, databaseIdentity: bigint): Uint8Array[] { + return this.#ds.runQuery(sql, bigintToFixedHex(databaseIdentity, 32)); + } + + validateJwtPayload( + jwtPayload: string, + connectionId: bigint + ): WasmValidatedAuth { + return this.#ds.validateJwtPayload( + jwtPayload, + bigintToFixedHex(connectionId, 16) + ); + } + + beginTx(): TestRuntimeTx { + return this.#ds.beginMutTx() as TestRuntimeTx; + } + + commitTx(tx: TestRuntimeTx, mode: TestRuntimeCommitMode = 'Normal'): void { + this.#ds.commitTx(expectWasmTx(tx), mode); + } + + abortTx(tx: TestRuntimeTx): void { + this.#ds.rollbackTx(expectWasmTx(tx)); + } + + #withMutTx( + target: TestRuntimeTarget, + body: (tx: WasmPortableTransaction) => T + ): T { + if (isWasmTx(target)) { + return body(target); + } + + const tx = this.#ds.beginMutTx(); + try { + const ret = body(tx); + this.#ds.commitTx(tx, 'Normal'); + return ret; + } catch (e) { + this.#ds.rollbackTx(tx); + throw e; + } + } +} + +function isWasmTx(target: TestRuntimeTarget): target is WasmPortableTransaction { + return !(target instanceof WasmContext); +} + +function expectWasmTx(target: TestRuntimeTarget): WasmPortableTransaction { + if (!isWasmTx(target)) { + throw new Error('operation requires an active wasm datastore transaction'); + } + return target; +} + +function splitRangeBuffer( + buffer: Uint8Array, + rstartLen: number, + rendLen: number +) { + const prefixLen = buffer.byteLength - rstartLen - rendLen; + if (prefixLen < 0) { + throw new Error('invalid index range buffer lengths'); + } + return { + prefix: buffer.subarray(0, prefixLen), + rstart: buffer.subarray(prefixLen, prefixLen + rstartLen), + rend: buffer.subarray( + prefixLen + rstartLen, + prefixLen + rstartLen + rendLen + ), + }; +} + +function bigintToFixedHex(value: bigint, bytes: number): string { + return value.toString(16).padStart(bytes * 2, '0'); +} diff --git a/crates/bindings-typescript/tests/server_test_utils_wasm.test.ts b/crates/bindings-typescript/tests/server_test_utils_wasm.test.ts new file mode 100644 index 00000000000..887633a9eb5 --- /dev/null +++ b/crates/bindings-typescript/tests/server_test_utils_wasm.test.ts @@ -0,0 +1,482 @@ +import { describe, expect, it } from 'vitest'; + +import { ConnectionId, TimeDuration } from '../src'; +import { Range, schema, table, t } from '../src/server'; +import { + createModuleTestHarness, + createProcedureTestHooks, + TestAuth, + TestClock, +} from '../src/server/test-utils'; +describe('server test-utils real wasm runtime', () => { + it('validates JWT auth through the harness datastore', () => { + const { spacetime, moduleExports } = makeModule(); + const test = createModuleTestHarness(spacetime, moduleExports); + const payload = JSON.stringify({ iss: 'issuer', sub: 'subject' }); + const connectionId = new ConnectionId(7n); + + let seen: + | { + sender: string; + authIdentity: string; + connectionId: string | undefined; + } + | undefined; + + test.withReducerTx(TestAuth.fromJwtPayload(payload, connectionId), ctx => { + seen = { + sender: ctx.sender.toHexString(), + authIdentity: ctx.senderAuth.jwt?.identity.toHexString() ?? '', + connectionId: ctx.connectionId?.toHexString(), + }; + }); + + expect(seen?.sender).toMatch(/^[0-9a-f]{64}$/); + expect(seen?.authIdentity).toBe(seen?.sender); + expect(seen?.connectionId).toBe(connectionId.toHexString()); + }); + + it('accepts JWT payloads without iat and with matching hex_identity', () => { + const basePayload = { iss: 'issuer', sub: 'subject' }; + const sender = senderForJwtPayload(basePayload); + + expect(sender).toMatch(/^[0-9a-f]{64}$/); + expect(senderForJwtPayload({ ...basePayload, hex_identity: sender })).toBe( + sender + ); + }); + + it.each([ + ['missing iss', { sub: 'subject' }], + ['missing sub', { iss: 'issuer' }], + ['empty iss', { iss: '', sub: 'subject' }], + ['empty sub', { iss: 'issuer', sub: '' }], + [ + 'non-string hex_identity', + { iss: 'issuer', sub: 'subject', hex_identity: 1 }, + ], + [ + 'mismatched hex_identity', + { iss: 'issuer', sub: 'subject', hex_identity: '00'.repeat(32) }, + ], + ])('rejects JWT payloads with %s', (_name, payload) => { + const { spacetime, moduleExports } = makeModule(); + const test = createModuleTestHarness(spacetime, moduleExports); + const connectionId = new ConnectionId(7n); + + expect(() => + test.withReducerTx( + TestAuth.fromJwtPayload(JSON.stringify(payload), connectionId), + () => {} + ) + ).toThrow(); + }); + + it('runs procedure sleep hooks and advances the test clock', () => { + const { spacetime, moduleExports } = makeModule(); + const clock = new TestClock(); + const test = createModuleTestHarness(spacetime, moduleExports, { clock }); + const hooks = createProcedureTestHooks(); + const wakeTimes: bigint[] = []; + + hooks.onSleep((test, wakeTime) => { + wakeTimes.push(wakeTime.microsSinceUnixEpoch); + test.clock.advance(TimeDuration.fromMillis(25)); + }); + + const ctx = test + .procedureContextBuilder(TestAuth.internal()) + .hooks(hooks) + .build(); + + ctx.sleep(TimeDuration.fromMillis(10)); + + expect(wakeTimes).toEqual([10_000n]); + expect(clock.now().microsSinceUnixEpoch).toBe(25_000n); + }); + + it('advances the clock to the wake time when sleep hooks do not', () => { + const { spacetime, moduleExports } = makeModule(); + const clock = new TestClock(); + const test = createModuleTestHarness(spacetime, moduleExports, { clock }); + const ctx = test.procedureContext(TestAuth.internal()); + + ctx.sleep(TimeDuration.fromMillis(10)); + + expect(clock.now().microsSinceUnixEpoch).toBe(10_000n); + }); + + it('commits procedure transactions against the real wasm backend', () => { + const { spacetime, moduleExports } = makeModule(); + const test = createModuleTestHarness(spacetime, moduleExports); + const ctx = test.procedureContext(TestAuth.internal()); + + ctx.withTx(tx => { + tx.db.person.insert({ id: 10, name: 'Committed' }); + }); + + expect([...test.db.person.iter()]).toEqual([{ id: 10, name: 'Committed' }]); + }); + + it('aborts procedure transactions when the body throws', () => { + const { spacetime, moduleExports } = makeModule(); + const test = createModuleTestHarness(spacetime, moduleExports); + const ctx = test.procedureContext(TestAuth.internal()); + + expect(() => + ctx.withTx(tx => { + tx.db.person.insert({ id: 11, name: 'Rolled back' }); + throw new Error('abort'); + }) + ).toThrow('abort'); + + expect(test.db.person.count()).toBe(0n); + }); + + it('runs afterTxCommit hooks that can interleave reducers', () => { + const { spacetime, moduleExports, addPerson } = makeModule(); + const test = createModuleTestHarness(spacetime, moduleExports); + const hooks = createProcedureTestHooks(); + + hooks.afterTxCommit(test => { + test.withReducerTx(TestAuth.internal(), ctx => { + addPerson(ctx, { id: 13, name: 'Reducer' }); + }); + }); + + const ctx = test + .procedureContextBuilder(TestAuth.internal()) + .hooks(hooks) + .build(); + + ctx.withTx(tx => { + tx.db.person.insert({ id: 12, name: 'Procedure' }); + }); + + expect([...test.db.person.iter()]).toEqual([ + { id: 12, name: 'Procedure' }, + { id: 13, name: 'Reducer' }, + ]); + }); + + it('uses the configured HTTP responder and allows interleaving before returning', () => { + const { spacetime, moduleExports, addPerson } = makeModule(); + const test = createModuleTestHarness(spacetime, moduleExports); + const ctx = test + .procedureContextBuilder(TestAuth.internal()) + .http((test, req, body) => { + expect(req.uri).toBe('https://example.com/'); + expect(new TextDecoder().decode(body)).toBe('request'); + test.withReducerTx(TestAuth.internal(), ctx => { + addPerson(ctx, { id: 14, name: 'HTTP interleave' }); + }); + return { + body: new TextEncoder().encode('response'), + code: 201, + headers: { entries: [] }, + version: { tag: 'Http11' }, + }; + }) + .build(); + + const response = ctx.http.fetch('https://example.com/', { + method: 'POST', + body: 'request', + }); + + expect(response.status).toBe(201); + expect([...test.db.person.iter()]).toEqual([ + { id: 14, name: 'HTTP interleave' }, + ]); + }); + + it('throws a deterministic error when no HTTP responder is configured', () => { + const { spacetime, moduleExports } = makeModule(); + const test = createModuleTestHarness(spacetime, moduleExports); + const ctx = test.procedureContext(TestAuth.internal()); + + expect(() => ctx.http.fetch('https://example.com/')).toThrow( + 'no test HTTP responder configured' + ); + }); + + it('can create multiple independent harnesses for the same module', () => { + const { spacetime, moduleExports } = makeModule(); + const first = createModuleTestHarness(spacetime, moduleExports); + const second = createModuleTestHarness(spacetime, moduleExports); + + first.db.person.insert({ id: 20, name: 'First' }); + second.db.person.insert({ id: 21, name: 'Second' }); + + expect([...first.db.person.iter()]).toEqual([{ id: 20, name: 'First' }]); + expect([...second.db.person.iter()]).toEqual([{ id: 21, name: 'Second' }]); + }); + + it('draws distinct deterministic RNG seeds for reducer contexts', () => { + const { spacetime, moduleExports } = makeModule(); + const test = createModuleTestHarness(spacetime, moduleExports, { + rngSeed: 123n, + }); + + let first: number | undefined; + let second: number | undefined; + test.withReducerTx(TestAuth.internal(), ctx => { + first = ctx.random.uint32(); + }); + test.withReducerTx(TestAuth.internal(), ctx => { + second = ctx.random.uint32(); + }); + + expect(first).not.toBe(second); + + test.rng.setSeed(123n); + let replayedFirst: number | undefined; + test.withReducerTx(TestAuth.internal(), ctx => { + replayedFirst = ctx.random.uint32(); + }); + + expect(replayedFirst).toBe(first); + }); + + it('draws procedure transaction seeds from a procedure-owned seed source', () => { + const { spacetime, moduleExports } = makeModule(); + const test = createModuleTestHarness(spacetime, moduleExports, { + rngSeed: 456n, + }); + const procedure = test.procedureContext(TestAuth.internal()); + + // Advance the procedure RNG; transaction seeds should be independent. + procedure.random.uint32(); + + const first = procedure.withTx(tx => tx.random.uint32()); + const second = procedure.withTx(tx => tx.random.uint32()); + expect(first).not.toBe(second); + + test.rng.setSeed(456n); + const replayedProcedure = test.procedureContext(TestAuth.internal()); + replayedProcedure.random.uint32(); + const replayedFirst = replayedProcedure.withTx(tx => tx.random.uint32()); + + expect(replayedFirst).toBe(first); + }); + + it('supports generated columns, index find/filter/update/delete, and range scans', () => { + const { spacetime, moduleExports } = makeTableHandleModule(); + const test = createModuleTestHarness(spacetime, moduleExports); + + const alpha = test.db.item.insert({ + id: 0, + name: 'Alpha', + group: 'a', + score: 10, + }); + const beta = test.db.item.insert({ + id: 0, + name: 'Beta', + group: 'b', + score: 20, + }); + const gamma = test.db.item.insert({ + id: 0, + name: 'Gamma', + group: 'a', + score: 30, + }); + + expect(alpha.id).not.toBe(0); + expect(beta.id).not.toBe(alpha.id); + expect(gamma.id).not.toBe(beta.id); + expect(test.db.item.id.find(alpha.id)).toEqual(alpha); + expect(test.db.item.name.find('Beta')).toEqual(beta); + expect( + [...test.db.item.group.filter('a')].map(row => row.name).sort() + ).toEqual(['Alpha', 'Gamma']); + expect( + [ + ...test.db.item.score.filter( + new Range( + { tag: 'included', value: 10 }, + { tag: 'included', value: 20 } + ) + ), + ].map(row => row.name) + ).toEqual(['Alpha', 'Beta']); + + const updated = test.db.item.id.update({ + ...alpha, + name: 'Alpha2', + score: 15, + }); + expect(updated).toEqual({ ...alpha, name: 'Alpha2', score: 15 }); + expect(test.db.item.name.find('Alpha')).toBeNull(); + expect(test.db.item.name.find('Alpha2')).toEqual(updated); + + expect(test.db.item.delete(updated)).toBe(true); + expect(test.db.item.id.find(updated.id)).toBeNull(); + expect(test.db.item.name.delete('Beta')).toBe(true); + expect(test.db.item.id.find(beta.id)).toBeNull(); + expect(test.db.item.group.delete('a')).toBe(1); + expect(test.db.item.count()).toBe(0n); + }); + + it('supports table clear operations', () => { + const { spacetime, moduleExports } = makeTableHandleModule(); + const test = createModuleTestHarness(spacetime, moduleExports); + test.db.item.insert({ id: 0, name: 'Second', group: 'x', score: 2 }); + test.db.item.insert({ id: 0, name: 'Third', group: 'x', score: 3 }); + + expect(test.db.item.clear()).toBe(2n); + expect(test.db.item.count()).toBe(0n); + }); + + it('surfaces unique constraint errors from table handles', () => { + const { spacetime, moduleExports } = makeTableHandleModule(); + const test = createModuleTestHarness(spacetime, moduleExports); + + test.db.item.insert({ id: 0, name: 'Duplicate', group: 'x', score: 1 }); + expect(() => + test.db.item.insert({ + id: 0, + name: 'Duplicate', + group: 'y', + score: 2, + }) + ).toThrow(); + }); + + it('runs typed queries against committed wasm datastore state', () => { + const { spacetime, moduleExports, addPerson } = makeQueryModule(); + const test = createModuleTestHarness(spacetime, moduleExports); + const viewCtx = test.viewContext(TestAuth.internal()); + + test.db.person.insert({ id: 1, name: 'Alice' }); + test.withReducerTx(TestAuth.internal(), ctx => { + addPerson(ctx, { id: 2, name: 'Bob' }); + }); + + expect(test.runQuery(viewCtx.from.person)).toEqual([ + { id: 1, name: 'Alice' }, + { id: 2, name: 'Bob' }, + ]); + expect( + test.runQuery(viewCtx.from.person.where(row => row.name.eq('Bob'))) + ).toEqual([{ id: 2, name: 'Bob' }]); + }); + + it('runs typed queries returned by views', () => { + const { spacetime, moduleExports, peopleNamedAlice } = makeQueryModule(); + const test = createModuleTestHarness(spacetime, moduleExports); + + test.db.person.insert({ id: 1, name: 'Alice' }); + test.db.person.insert({ id: 2, name: 'Bob' }); + + expect( + test.runQuery(peopleNamedAlice(test.viewContext(TestAuth.internal()), {})) + ).toEqual([{ id: 1, name: 'Alice' }]); + }); + + it('runs semijoin queries through the wasm datastore', () => { + const { spacetime, moduleExports } = makeQueryModule(); + const test = createModuleTestHarness(spacetime, moduleExports); + const viewCtx = test.viewContext(TestAuth.internal()); + + test.db.person.insert({ id: 1, name: 'Alice' }); + test.db.person.insert({ id: 2, name: 'Bob' }); + test.db.pet.insert({ id: 10, ownerId: 2, name: 'Biscuit' }); + + expect( + test.runQuery( + viewCtx.from.person.leftSemijoin(viewCtx.from.pet, (person, pet) => + person.id.eq(pet.ownerId) + ) + ) + ).toEqual([{ id: 2, name: 'Bob' }]); + }); +}); + +function makeModule() { + const person = table( + { name: 'person', public: true }, + { + id: t.u32(), + name: t.string(), + } + ); + const spacetime = schema({ person }); + const addPerson = spacetime.reducer( + { id: t.u32(), name: t.string() }, + (ctx, row) => { + ctx.db.person.insert(row); + } + ); + + return { spacetime, moduleExports: { addPerson }, addPerson }; +} + +function makeTableHandleModule() { + const item = table( + { name: 'item', public: true }, + { + id: t.u32().primaryKey().autoInc(), + name: t.string().unique(), + group: t.string().index('hash'), + score: t.u32().index('btree'), + } + ); + const spacetime = schema({ item }); + + return { spacetime, moduleExports: {} }; +} + +function makeQueryModule() { + const person = table( + { name: 'person', public: true }, + { + id: t.u32().primaryKey(), + name: t.string(), + } + ); + const pet = table( + { name: 'pet', public: true }, + { + id: t.u32().primaryKey(), + ownerId: t.u32().index('btree'), + name: t.string(), + } + ); + const spacetime = schema({ person, pet }); + const addPerson = spacetime.reducer( + { id: t.u32(), name: t.string() }, + (ctx, row) => { + ctx.db.person.insert(row); + } + ); + const peopleNamedAlice = spacetime.view( + { name: 'people_named_alice', public: true }, + t.array(person.rowType), + ctx => ctx.from.person.where(row => row.name.eq('Alice')) + ); + + return { + spacetime, + moduleExports: { addPerson, peopleNamedAlice }, + addPerson, + peopleNamedAlice, + }; +} + +function senderForJwtPayload(payload: Record): string { + const { spacetime, moduleExports } = makeModule(); + const test = createModuleTestHarness(spacetime, moduleExports); + const connectionId = new ConnectionId(7n); + let sender: string | undefined; + + test.withReducerTx( + TestAuth.fromJwtPayload(JSON.stringify(payload), connectionId), + ctx => { + sender = ctx.sender.toHexString(); + } + ); + + return sender!; +} diff --git a/crates/bindings-typescript/tests/server_test_utils_wasm_adapter.test.ts b/crates/bindings-typescript/tests/server_test_utils_wasm_adapter.test.ts new file mode 100644 index 00000000000..1828512ee3f --- /dev/null +++ b/crates/bindings-typescript/tests/server_test_utils_wasm_adapter.test.ts @@ -0,0 +1,219 @@ +import { afterEach, describe, expect, it } from 'vitest'; + +import { schema, table, t } from '../src/server'; +import { createModuleTestHarness, TestAuth } from '../src/server/test-utils'; +import { createWasmTestRuntime } from '../src/server/test-utils/wasm'; + +type CommitMode = 'Normal' | 'DropEventTableRows'; + +class FakeTx { + readonly __testRuntimeTxBrand!: unique symbol; + readonly __wasmTxBrand!: unique symbol; + + constructor(public rows: Uint8Array[]) {} +} + +class FakeWasmPortableDatastore { + static instances: FakeWasmPortableDatastore[] = []; + + rows: Uint8Array[] = []; + commitModes: CommitMode[] = []; + + constructor( + _rawModuleDef: Uint8Array, + readonly moduleIdentityHex: string + ) { + FakeWasmPortableDatastore.instances.push(this); + } + + tableId(name: string): number { + if (name !== 'person') throw new Error(`unknown table: ${name}`); + return 1; + } + + indexId(name: string): number { + if (name !== 'person_id_idx_btree') + throw new Error(`unknown index: ${name}`); + return 1; + } + + beginMutTx(): FakeTx { + return new FakeTx(this.rows.map(row => row.slice())); + } + + commitTx(tx: FakeTx, mode: CommitMode): void { + this.rows = tx.rows.map(row => row.slice()); + this.commitModes.push(mode); + } + + rollbackTx(_tx: FakeTx): void {} + + reset(): void { + this.rows = []; + this.commitModes = []; + } + + tableRowCount(_tableId: number): number { + return this.rows.length; + } + + tableRowCountTx(tx: FakeTx, _tableId: number): number { + return tx.rows.length; + } + + tableRowsBsatn(_tableId: number): Uint8Array[] { + return this.rows.map(row => row.slice()); + } + + tableRowsBsatnTx(tx: FakeTx, _tableId: number): Uint8Array[] { + return tx.rows.map(row => row.slice()); + } + + indexScanPointBsatn(): Uint8Array[] { + return []; + } + + indexScanPointBsatnTx(): Uint8Array[] { + return []; + } + + indexScanRangeBsatn(): Uint8Array[] { + return []; + } + + indexScanRangeBsatnTx(): Uint8Array[] { + return []; + } + + insertBsatnGeneratedCols( + tx: FakeTx, + _tableId: number, + row: Uint8Array + ): Uint8Array { + tx.rows.push(row.slice()); + return new Uint8Array(); + } + + updateBsatnGeneratedCols( + tx: FakeTx, + _tableId: number, + _indexId: number, + row: Uint8Array + ): Uint8Array { + tx.rows[0] = row.slice(); + return new Uint8Array(); + } + + deleteByRelBsatn(tx: FakeTx, _tableId: number, relation: Uint8Array): number { + const row = relation.subarray(4); + const before = tx.rows.length; + tx.rows = tx.rows.filter(existing => !bytesEqual(existing, row)); + return before - tx.rows.length; + } + + deleteByIndexScanPointBsatn(): number { + return 0; + } + + deleteByIndexScanRangeBsatn(): number { + return 0; + } + + clearTable(tx: FakeTx, _tableId: number): number { + const count = tx.rows.length; + tx.rows = []; + return count; + } + + validateJwtPayload(): { senderHex: string; connectionIdHex: undefined } { + return { senderHex: '00'.repeat(32), connectionIdHex: undefined }; + } + + runQuery(): Uint8Array[] { + throw new Error('fake wasm adapter test does not implement runQuery'); + } +} + +const fakeWasmModule = { WasmPortableDatastore: FakeWasmPortableDatastore }; + +afterEach(() => { + globalThis.__spacetimedbTestRuntime = undefined; + FakeWasmPortableDatastore.instances = []; +}); + +describe('server test-utils wasm runtime adapter', () => { + it('supports direct test.db writes through auto-commit transactions', () => { + const { spacetime, moduleExports } = makeModule(); + installFakeWasmRuntime(); + + const test = createModuleTestHarness(spacetime, moduleExports); + + test.db.person.insert({ id: 1, name: 'Alice' }); + + expect(test.db.person.count()).toBe(1n); + expect([...test.db.person.iter()]).toEqual([{ id: 1, name: 'Alice' }]); + expect(FakeWasmPortableDatastore.instances[0].commitModes).toEqual([ + 'Normal', + ]); + }); + + it('commits reducer transactions with event-table cleanup mode', () => { + const { spacetime, moduleExports, addPerson } = makeModule(); + installFakeWasmRuntime(); + + const test = createModuleTestHarness(spacetime, moduleExports); + + test.withReducerTx(TestAuth.internal(), ctx => { + addPerson(ctx, { id: 2, name: 'Bob' }); + }); + + expect([...test.db.person.iter()]).toEqual([{ id: 2, name: 'Bob' }]); + expect(FakeWasmPortableDatastore.instances[0].commitModes).toEqual([ + 'DropEventTableRows', + ]); + }); + + it('rolls back reducer transactions when the body throws', () => { + const { spacetime, moduleExports, addPerson } = makeModule(); + installFakeWasmRuntime(); + + const test = createModuleTestHarness(spacetime, moduleExports); + + expect(() => + test.withReducerTx(TestAuth.internal(), ctx => { + addPerson(ctx, { id: 3, name: 'Carol' }); + throw new Error('fail'); + }) + ).toThrow('fail'); + + expect(test.db.person.count()).toBe(0n); + expect(FakeWasmPortableDatastore.instances[0].commitModes).toEqual([]); + }); +}); + +function makeModule() { + const person = table( + { name: 'person', public: true }, + { + id: t.u32(), + name: t.string(), + } + ); + const spacetime = schema({ person }); + const addPerson = spacetime.reducer( + { id: t.u32(), name: t.string() }, + (ctx, row) => { + ctx.db.person.insert(row); + } + ); + + return { spacetime, moduleExports: { addPerson }, addPerson }; +} + +function installFakeWasmRuntime() { + globalThis.__spacetimedbTestRuntime = createWasmTestRuntime(fakeWasmModule); +} + +function bytesEqual(a: Uint8Array, b: Uint8Array): boolean { + return a.byteLength === b.byteLength && a.every((value, i) => value === b[i]); +} diff --git a/crates/bindings-typescript/tests/setup.ts b/crates/bindings-typescript/tests/setup.ts new file mode 100644 index 00000000000..bc9285c84a9 --- /dev/null +++ b/crates/bindings-typescript/tests/setup.ts @@ -0,0 +1,19 @@ +if (!Symbol.dispose) { + Object.defineProperty(Symbol, 'dispose', { + value: Symbol.for('Symbol.dispose'), + }); +} + +if (!Promise.withResolvers) { + Object.defineProperty(Promise, 'withResolvers', { + value: function withResolvers() { + let resolve!: (value: T | PromiseLike) => void; + let reject!: (reason?: unknown) => void; + const promise = new Promise((res, rej) => { + resolve = res; + reject = rej; + }); + return { promise, resolve, reject }; + }, + }); +} diff --git a/crates/bindings-typescript/tsup.config.ts b/crates/bindings-typescript/tsup.config.ts index 80c4af53d31..551b3806cd3 100644 --- a/crates/bindings-typescript/tsup.config.ts +++ b/crates/bindings-typescript/tsup.config.ts @@ -266,6 +266,26 @@ export default defineConfig([ }, }, + // Node module unit-test helpers. + { + entry: { + index: 'src/server/test-utils/index.ts', + vitest: 'src/server/test-utils/vitest.ts', + wasm: 'src/server/test-utils/wasm.ts', + }, + format: 'esm', + target: 'es2022', + outDir: 'dist/server/test-utils', + dts: true, + sourcemap: true, + clean: false, + platform: 'neutral', + external: [/^spacetime:sys.*$/], + treeshake: 'smallest', + outExtension, + esbuildOptions: commonEsbuildTweaks(), + }, + // TanStack subpath (SSR-friendly): dist/tanstack/index.{mjs,cjs} { entry: { index: 'src/tanstack/index.ts' }, diff --git a/crates/bindings-typescript/vitest.config.ts b/crates/bindings-typescript/vitest.config.ts index 7cfc858d47c..30d5c1b1896 100644 --- a/crates/bindings-typescript/vitest.config.ts +++ b/crates/bindings-typescript/vitest.config.ts @@ -1,12 +1,14 @@ import { fileURLToPath } from 'node:url'; import type { UserConfig } from 'vite'; import { defineConfig } from 'vitest/config'; +import { spacetimedbModuleTestPlugin } from './src/server/test-utils/vitest'; const sysMock = fileURLToPath( new URL('./tests/__mocks__/spacetime-sys.ts', import.meta.url) ); export default defineConfig({ + plugins: [spacetimedbModuleTestPlugin()], resolve: { // The `spacetime:sys@*` virtual modules are injected by the SpacetimeDB V8 // host at runtime. Tests that import `src/server/runtime.ts` need a stub so @@ -18,6 +20,7 @@ export default defineConfig({ }, test: { include: ['tests/**/*.test.ts'], + setupFiles: ['tests/setup.ts'], globals: true, environment: 'node', typecheck: { diff --git a/crates/bindings/Cargo.toml b/crates/bindings/Cargo.toml index 54c3bd3c74e..b7e2255d128 100644 --- a/crates/bindings/Cargo.toml +++ b/crates/bindings/Cargo.toml @@ -18,6 +18,7 @@ default = ["rand"] rand = ["rand08"] rand08 = ["dep:rand08", "dep:getrandom02"] unstable = ["spacetimedb-bindings-sys/unstable"] +test-utils = ["spacetimedb-bindings-macro/test-utils", "dep:spacetimedb-auth", "dep:spacetimedb-test-datastore"] [dependencies] spacetimedb-bindings-sys.workspace = true @@ -41,6 +42,10 @@ rand08 = { workspace = true, optional = true } getrandom02 = { workspace = true, optional = true, features = ["custom"] } serde_json.workspace = true +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] +spacetimedb-auth = { workspace = true, optional = true } +spacetimedb-test-datastore = { workspace = true, optional = true } + [dev-dependencies] insta.workspace = true trybuild.workspace = true diff --git a/crates/bindings/src/http.rs b/crates/bindings/src/http.rs index 4c8ea487c47..c4ab23554f0 100644 --- a/crates/bindings/src/http.rs +++ b/crates/bindings/src/http.rs @@ -5,7 +5,9 @@ //! The [`get`](HttpClient::get) helper can be used for simple `GET` requests, //! while [`send`](HttpClient::send) allows more complex requests with headers, bodies and other methods. +#[cfg(target_arch = "wasm32")] use crate::rt::{read_bytes_source_as, read_bytes_source_into}; +#[cfg(target_arch = "wasm32")] use crate::IterBuf; // Imports used only by the (still-unstable) HTTP handler machinery. #[cfg(all(feature = "unstable", feature = "rand08"))] @@ -15,20 +17,25 @@ use crate::{try_with_tx, with_tx, Timestamp, TxContext}; use bytes::Bytes; #[cfg(all(feature = "rand08", feature = "unstable"))] use rand08::RngCore; +#[cfg(target_arch = "wasm32")] +use spacetimedb_lib::bsatn; #[cfg(feature = "unstable")] use spacetimedb_lib::db::raw_def::v10::MethodOrAny; +#[cfg(any(target_arch = "wasm32", feature = "unstable"))] use spacetimedb_lib::http as st_http; #[cfg(feature = "unstable")] use spacetimedb_lib::http::{character_is_acceptable_for_route_path, ACCEPTABLE_ROUTE_PATH_CHARS_HUMAN_DESCRIPTION}; #[cfg(feature = "unstable")] use spacetimedb_lib::Identity; +use spacetimedb_lib::TimeDuration; #[cfg(all(feature = "unstable", feature = "rand08"))] use spacetimedb_lib::Uuid; -use spacetimedb_lib::{bsatn, TimeDuration}; #[cfg(all(feature = "unstable", feature = "rand08"))] use std::cell::Cell; #[cfg(all(feature = "unstable", feature = "rand08"))] use std::cell::OnceCell; +#[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] +use std::rc::Rc; #[cfg(feature = "unstable")] use std::str::FromStr; @@ -118,7 +125,7 @@ impl HandlerContext { pub(crate) fn new(timestamp: Timestamp) -> Self { Self { timestamp, - http: HttpClient {}, + http: HttpClient::host(), #[cfg(feature = "rand08")] rng: OnceCell::new(), #[cfg(feature = "rand08")] @@ -431,9 +438,36 @@ fn routes_overlap(a: &RouteSpec, b: &RouteSpec) -> bool { /// Access an `HttpClient` from within [procedures](crate::procedure) /// via [the `http` field of the `ProcedureContext`](crate::ProcedureContext::http). #[non_exhaustive] -pub struct HttpClient {} +pub struct HttpClient { + backend: HttpClientBackend, +} + +enum HttpClientBackend { + Host, + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + Test { + context: crate::test_utils::TestContext, + responder: TestHttpResponder, + }, +} + +#[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] +pub type TestHttpResponder = Rc Result>; impl HttpClient { + pub(crate) fn host() -> Self { + Self { + backend: HttpClientBackend::Host, + } + } + + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + pub(crate) fn test(context: crate::test_utils::TestContext, responder: TestHttpResponder) -> Self { + Self { + backend: HttpClientBackend::Test { context, responder }, + } + } + /// Send the HTTP request `request` and wait for its response. /// /// For simple `GET` requests with no headers, use [`HttpClient::get`] instead. @@ -484,29 +518,50 @@ impl HttpClient { /// /// ``` pub fn send>(&self, request: http::Request) -> Result { - let (request, body) = request.map(Into::into).into_parts(); - let request = convert_request(request); - let request = bsatn::to_vec(&request).expect("Failed to BSATN-serialize `spacetimedb_lib::http::Request`"); - - match spacetimedb_bindings_sys::procedure::http_request(&request, &body.into_bytes()) { - Ok((response_source, body_source)) => { - let response = read_bytes_source_as::(response_source); - let response = convert_response(response).expect("Invalid http response returned from host"); - let body = if body_source == spacetimedb_bindings_sys::raw::BytesSource::INVALID { - // Empty response body — host returns INVALID source for empty bytes - Body::from_bytes(Vec::::new()) - } else { - let mut buf = IterBuf::take(); - read_bytes_source_into(body_source, &mut buf); - Body::from_bytes(buf.clone()) - }; - - Ok(http::Response::from_parts(response, body)) - } - Err(err_source) => { - let message = read_bytes_source_as::(err_source); - Err(Error { message }) + let request = request.map(Into::into); + + match &self.backend { + HttpClientBackend::Host => { + #[cfg(target_arch = "wasm32")] + { + let (request, body) = request.into_parts(); + let request = convert_request(request); + let request = + bsatn::to_vec(&request).expect("Failed to BSATN-serialize `spacetimedb_lib::http::Request`"); + + match spacetimedb_bindings_sys::procedure::http_request(&request, &body.into_bytes()) { + Ok((response_source, body_source)) => { + let response = read_bytes_source_as::(response_source); + let response = + convert_response(response).expect("Invalid http response returned from host"); + let body = if body_source == spacetimedb_bindings_sys::raw::BytesSource::INVALID { + // Empty response body — host returns INVALID source for empty bytes + Body::from_bytes(Vec::::new()) + } else { + let mut buf = IterBuf::take(); + read_bytes_source_into(body_source, &mut buf); + Body::from_bytes(buf.clone()) + }; + + Ok(http::Response::from_parts(response, body)) + } + Err(err_source) => { + let message = read_bytes_source_as::(err_source); + Err(Error { message }) + } + } + } + + #[cfg(not(target_arch = "wasm32"))] + { + let _ = request; + Err(Error::new( + "procedure HTTP is only available in wasm or native test-utils contexts", + )) + } } + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + HttpClientBackend::Test { context, responder } => responder(context, request), } } @@ -545,6 +600,7 @@ impl HttpClient { } } +#[cfg(target_arch = "wasm32")] fn convert_request(parts: http::request::Parts) -> st_http::Request { let http::request::Parts { method, @@ -589,6 +645,7 @@ fn convert_request(parts: http::request::Parts) -> st_http::Request { } } +#[cfg(target_arch = "wasm32")] fn convert_response(response: st_http::Response) -> http::Result { let st_http::Response { headers, version, code } = response; @@ -808,6 +865,15 @@ impl std::fmt::Display for Error { impl std::error::Error for Error {} +impl Error { + /// Construct an HTTP error for tests or adapters. + pub fn new(message: impl Into) -> Self { + Self { + message: message.into(), + } + } +} + impl From for Error { fn from(err: http::Error) -> Self { Error { diff --git a/crates/bindings/src/lib.rs b/crates/bindings/src/lib.rs index 2c7faa78c6b..f1db3fcf3d0 100644 --- a/crates/bindings/src/lib.rs +++ b/crates/bindings/src/lib.rs @@ -1,11 +1,9 @@ #![doc = include_str!("../README.md")] // ^ if you are working on docs, go read the top comment of README.md please. -use core::cell::{LazyCell, OnceCell, RefCell}; +use core::cell::{Cell, LazyCell, OnceCell, RefCell}; use core::ops::Deref; use spacetimedb_lib::bsatn; -#[cfg(feature = "rand08")] -use std::cell::Cell; use std::rc::Rc; #[cfg(feature = "unstable")] @@ -19,6 +17,8 @@ mod rng; pub mod rt; #[doc(hidden)] pub mod table; +#[cfg(feature = "test-utils")] +pub mod test_utils; #[doc(hidden)] pub use spacetimedb_query_builder as query_builder; @@ -930,7 +930,18 @@ pub struct AnonymousViewContext { impl Default for AnonymousViewContext { fn default() -> Self { Self { - db: LocalReadOnly {}, + db: LocalReadOnly::__host(), + from: QueryBuilder {}, + } + } +} + +impl AnonymousViewContext { + #[doc(hidden)] + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + pub fn __test(db: LocalReadOnly) -> Self { + Self { + db, from: QueryBuilder {}, } } @@ -948,7 +959,7 @@ impl ViewContext { pub fn new(sender: Identity) -> Self { Self { sender, - db: LocalReadOnly {}, + db: LocalReadOnly::__host(), from: QueryBuilder {}, } } @@ -958,6 +969,16 @@ impl ViewContext { self.sender } + #[doc(hidden)] + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + pub fn __test(sender: Identity, db: LocalReadOnly) -> Self { + Self { + sender, + db, + from: QueryBuilder {}, + } + } + /// Obtain an [`AnonymousViewContext`] by dropping `sender`. pub fn as_anonymous(&self) -> AnonymousViewContext { AnonymousViewContext::default() @@ -992,6 +1013,9 @@ pub struct ReducerContext { sender_auth: AuthCtx, + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + module_identity: Identity, + /// Allows accessing the local database attached to a module. /// /// This slightly strange type appears to have no methods, but that is misleading. @@ -1018,7 +1042,7 @@ pub struct ReducerContext { /// /// #[reducer] /// fn find_books_by(ctx: &ReducerContext, author: String) { - /// let book: &book__TableHandle = ctx.db.book(); + /// let book: book__TableHandle = ctx.db.book(); /// /// log::debug!("looking up books by {author}..."); /// for book in book.author().filter(&author) { @@ -1042,11 +1066,13 @@ impl ReducerContext { #[doc(hidden)] pub fn __dummy() -> Self { Self { - db: Local {}, + db: Local::__host(), sender: Identity::__dummy(), timestamp: Timestamp::UNIX_EPOCH, connection_id: None, sender_auth: AuthCtx::internal(), + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + module_identity: Identity::ZERO, #[cfg(feature = "rand08")] rng: std::cell::OnceCell::new(), #[cfg(feature = "rand08")] @@ -1062,6 +1088,8 @@ impl ReducerContext { timestamp, connection_id, sender_auth: AuthCtx::from_connection_id_opt(connection_id), + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + module_identity: Identity::ZERO, #[cfg(feature = "rand08")] rng: std::cell::OnceCell::new(), #[cfg(feature = "rand08")] @@ -1069,6 +1097,31 @@ impl ReducerContext { } } + #[doc(hidden)] + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + pub fn __test( + db: Local, + sender: Identity, + sender_auth: AuthCtx, + connection_id: Option, + timestamp: Timestamp, + module_identity: Identity, + #[cfg(feature = "rand08")] rng_seed: Option, + ) -> Self { + Self { + db, + sender, + timestamp, + connection_id, + sender_auth, + module_identity, + #[cfg(feature = "rand08")] + rng: StdbRng::seeded_cell(rng_seed), + #[cfg(feature = "rand")] + counter_uuid: Cell::new(0), + } + } + /// The `Identity` of the client that invoked the reducer. pub fn sender(&self) -> Identity { self.sender @@ -1089,15 +1142,28 @@ impl ReducerContext { /// Read the current module's [`Identity`]. pub fn database_identity(&self) -> Identity { - // Hypothetically, we *could* read the module identity out of the system tables. - // However, this would be: - // - Onerous, because we have no tooling to inspect the system tables from module code. - // - Slow (at least relatively), - // because it would involve multiple host calls which hit the datastore, - // as compared to a single host call which does not. - // As such, we've just defined a host call - // which reads the module identity out of the `InstanceEnv`. - Identity::from_byte_array(spacetimedb_bindings_sys::identity()) + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + { + self.module_identity + } + + #[cfg(target_arch = "wasm32")] + { + // Hypothetically, we *could* read the module identity out of the system tables. + // However, this would be: + // - Onerous, because we have no tooling to inspect the system tables from module code. + // - Slow (at least relatively), + // because it would involve multiple host calls which hit the datastore, + // as compared to a single host call which does not. + // As such, we've just defined a host call + // which reads the module identity out of the `InstanceEnv`. + Identity::from_byte_array(spacetimedb_bindings_sys::identity()) + } + + #[cfg(all(not(feature = "test-utils"), not(target_arch = "wasm32")))] + { + panic!("ReducerContext::identity() is only available in wasm or native test-utils contexts") + } } /// Read the current module's [`Identity`]. @@ -1205,7 +1271,7 @@ fn try_with_tx( .expect("holding `&mut HandlerContext`, so should not be in a tx already; called manually elsewhere?"); let timestamp = Timestamp::from_micros_since_unix_epoch(timestamp); - let tx = ReducerContext::new(crate::Local {}, identity, connection_id, timestamp); + let tx = ReducerContext::new(crate::Local::__host(), identity, connection_id, timestamp); let tx = TxContext(tx); struct DoOnDrop(F); @@ -1265,6 +1331,24 @@ pub struct ProcedureContext { /// Will be `None` for certain scheduled procedures. connection_id: Option, + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + sender_auth: AuthCtx, + + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + module_identity: Identity, + + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + test_datastore: Option>, + + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + test_context: Option, + + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + test_hooks: Option, + + #[cfg(all(feature = "test-utils", feature = "rand08", not(target_arch = "wasm32")))] + test_child_seed_rng: Option>, + /// Methods for performing HTTP requests. pub http: crate::http::HttpClient, // TODO: Change rng? @@ -1285,7 +1369,19 @@ impl ProcedureContext { sender, timestamp, connection_id, - http: http::HttpClient {}, + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + sender_auth: AuthCtx::from_connection_id_opt(connection_id), + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + module_identity: Identity::ZERO, + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + test_datastore: None, + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + test_context: None, + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + test_hooks: None, + #[cfg(all(feature = "test-utils", feature = "rand08", not(target_arch = "wasm32")))] + test_child_seed_rng: None, + http: http::HttpClient::host(), #[cfg(feature = "rand08")] rng: std::cell::OnceCell::new(), #[cfg(feature = "rand08")] @@ -1293,6 +1389,43 @@ impl ProcedureContext { } } + #[doc(hidden)] + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + pub fn __test( + datastore: std::sync::Arc, + sender: Identity, + sender_auth: AuthCtx, + connection_id: Option, + timestamp: Timestamp, + module_identity: Identity, + http: http::HttpClient, + test_context: crate::test_utils::TestContext, + hooks: crate::test_utils::ProcedureTestHooks, + #[cfg(feature = "rand08")] rng_seed: Option, + ) -> Self { + Self { + sender, + timestamp, + connection_id, + sender_auth, + module_identity, + test_datastore: Some(datastore), + test_context: Some(test_context), + test_hooks: Some(hooks), + #[cfg(feature = "rand08")] + test_child_seed_rng: rng_seed.map(|seed| { + RefCell::new(::seed_from_u64( + seed ^ 0xa53d_5eed_c01d_ca11, + )) + }), + http, + #[cfg(feature = "rand08")] + rng: StdbRng::seeded_cell(rng_seed), + #[cfg(feature = "rand")] + counter_uuid: Cell::new(0), + } + } + /// The `Identity` of the client that invoked the procedure. pub fn sender(&self) -> Identity { self.sender @@ -1313,15 +1446,28 @@ impl ProcedureContext { /// Read the current module's [`Identity`]. pub fn database_identity(&self) -> Identity { - // Hypothetically, we *could* read the module identity out of the system tables. - // However, this would be: - // - Onerous, because we have no tooling to inspect the system tables from module code. - // - Slow (at least relatively), - // because it would involve multiple host calls which hit the datastore, - // as compared to a single host call which does not. - // As such, we've just defined a host call - // which reads the module identity out of the `InstanceEnv`. - Identity::from_byte_array(spacetimedb_bindings_sys::identity()) + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + { + self.module_identity + } + + #[cfg(target_arch = "wasm32")] + { + // Hypothetically, we *could* read the module identity out of the system tables. + // However, this would be: + // - Onerous, because we have no tooling to inspect the system tables from module code. + // - Slow (at least relatively), + // because it would involve multiple host calls which hit the datastore, + // as compared to a single host call which does not. + // As such, we've just defined a host call + // which reads the module identity out of the `InstanceEnv`. + Identity::from_byte_array(spacetimedb_bindings_sys::identity()) + } + + #[cfg(all(not(feature = "test-utils"), not(target_arch = "wasm32")))] + { + panic!("ProcedureContext::database_identity() is only available in wasm or native test-utils contexts") + } } /// Suspend execution until approximately `Timestamp`. @@ -1346,9 +1492,30 @@ impl ProcedureContext { /// ``` // TODO(procedure-sleep-until): remove this method pub fn sleep_until(&mut self, timestamp: Timestamp) { - let new_time = sys::procedure::sleep_until(timestamp.to_micros_since_unix_epoch()); - let new_time = Timestamp::from_micros_since_unix_epoch(new_time); - self.timestamp = new_time; + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + if let Some(test_context) = self.test_context.as_ref() { + if let Some(hooks) = self.test_hooks.as_mut() { + hooks.__run_on_sleep(test_context, timestamp); + } + + let wake_time = test_context.clock.now().max(timestamp); + test_context.clock.set(wake_time); + self.timestamp = wake_time; + return; + } + + #[cfg(target_arch = "wasm32")] + { + let new_time = sys::procedure::sleep_until(timestamp.to_micros_since_unix_epoch()); + let new_time = Timestamp::from_micros_since_unix_epoch(new_time); + self.timestamp = new_time; + } + + #[cfg(all(not(feature = "test-utils"), not(target_arch = "wasm32")))] + { + let _ = timestamp; + panic!("ProcedureContext::sleep_until() is only available in wasm or native test-utils contexts") + } } /// Acquire a mutable transaction @@ -1411,7 +1578,143 @@ impl ProcedureContext { /// callers should avoid writing to any captured mutable state within `body`, /// This includes interior mutability through types like [`std::cell::Cell`]. pub fn try_with_tx(&mut self, body: impl Fn(&TxContext) -> Result) -> Result { - try_with_tx(body, self.sender(), self.connection_id()) + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + if let Some(datastore) = self.test_datastore.clone() { + #[cfg(feature = "rand08")] + let tx_rng_seed = self + .test_child_seed_rng + .as_ref() + .map(|rng| rand08::RngCore::next_u64(&mut *rng.borrow_mut())); + + let run = || { + use core::mem; + + let test_tx = std::rc::Rc::new(datastore.begin_mut_tx()); + let tx = ReducerContext::__test( + Local::__test_tx(test_tx.clone()), + self.sender, + self.sender_auth.clone(), + self.connection_id, + self.timestamp, + self.module_identity, + #[cfg(feature = "rand08")] + tx_rng_seed, + ); + let tx = TxContext(tx); + + struct DoOnDrop(F); + impl Drop for DoOnDrop { + fn drop(&mut self) { + (self.0)(); + } + } + + let rollback_tx = test_tx.clone(); + let rollback_guard = DoOnDrop(move || { + rollback_tx + .rollback() + .expect("should have a pending mutable test transaction") + }); + let res = body(&tx); + mem::forget(rollback_guard); + (res, test_tx) + }; + + let (mut res, tx) = run(); + match res { + Ok(_) if tx.commit().is_err() => { + log::warn!("committing test anonymous transaction failed"); + let (retry_res, retry_tx) = run(); + res = retry_res; + match res { + Ok(_) => { + retry_tx.commit().expect("test transaction retry failed again"); + if let (Some(hooks), Some(test_context)) = + (self.test_hooks.as_mut(), self.test_context.as_ref()) + { + hooks.__run_after_tx_commit(test_context); + } + } + Err(_) => retry_tx + .rollback() + .expect("should have a pending mutable test transaction"), + } + } + Ok(_) => { + if let (Some(hooks), Some(test_context)) = (self.test_hooks.as_mut(), self.test_context.as_ref()) { + hooks.__run_after_tx_commit(test_context); + } + } + Err(_) => tx.rollback().expect("should have a pending mutable test transaction"), + } + + return res; + } + + #[cfg(target_arch = "wasm32")] + { + let abort = || { + sys::procedure::procedure_abort_mut_tx() + .expect("should have a pending mutable anon tx as `procedure_start_mut_tx` preceded") + }; + + let run = || { + // Start the transaction. + + use core::mem; + let timestamp = sys::procedure::procedure_start_mut_tx().expect( + "holding `&mut ProcedureContext`, so should not be in a tx already; called manually elsewhere?", + ); + let timestamp = Timestamp::from_micros_since_unix_epoch(timestamp); + + // We've resumed, so let's do the work, but first prepare the context. + let tx = ReducerContext::new(Local::__host(), self.sender, self.connection_id, timestamp); + let tx = TxContext(tx); + + // Guard the execution of `body` with a scope-guard that `abort`s on panic. + // Wasmtime now supports unwinding, so we need to protect against that. + // We're not using `scopeguard::guard` here to avoid an extra dependency. + struct DoOnDrop(F); + impl Drop for DoOnDrop { + fn drop(&mut self) { + (self.0)(); + } + } + let abort_guard = DoOnDrop(abort); + let res = body(&tx); + // Defuse the bomb. + mem::forget(abort_guard); + res + }; + + let mut res = run(); + + // Commit or roll back? + match res { + Ok(_) if sys::procedure::procedure_commit_mut_tx().is_err() => { + // Tried to commit, but couldn't. Retry once. + log::warn!("committing anonymous transaction failed"); + // NOTE(procedure,centril): there's no actual guarantee that `body` + // does the exact same as the time before, as the timestamps differ + // and due to interior mutability. + res = run(); + match res { + Ok(_) => sys::procedure::procedure_commit_mut_tx().expect("transaction retry failed again"), + Err(_) => abort(), + } + } + Ok(_) => {} + Err(_) => abort(), + } + + res + } + + #[cfg(not(target_arch = "wasm32"))] + { + let _ = body; + panic!("ProcedureContext::try_with_tx() is only available in wasm or native test-utils contexts") + } } /// Create a new random [`Uuid`] `v4` using the built-in RNG. @@ -1542,11 +1845,44 @@ impl DbContext for ViewContext { /// The `#[table]` macro uses the trait system to add table accessors to this type. /// These are generated methods that allow you to access specific tables. #[non_exhaustive] -pub struct Local {} +#[derive(Clone)] +pub struct Local { + read_only: LocalReadOnly, +} + +impl Local { + #[doc(hidden)] + pub fn __host() -> Self { + Self { + read_only: LocalReadOnly::__host(), + } + } + + #[doc(hidden)] + pub fn __table_backend(&self) -> table::TableHandleBackend { + self.read_only.__table_backend() + } + + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + fn __test(datastore: std::sync::Arc) -> Self { + Self { + read_only: LocalReadOnly::__test(datastore), + } + } + + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + fn __test_tx(tx: std::rc::Rc) -> Self { + Self { + read_only: LocalReadOnly { + backend: table::LocalBackend::TestTx { tx }, + }, + } + } +} impl Local { fn get_read_only(&self) -> &LocalReadOnly { - &LocalReadOnly {} + &self.read_only } } @@ -1563,25 +1899,25 @@ pub trait CtxDbRead { impl CtxDbRead for TxContext { fn db_read_only(&self) -> &LocalReadOnly { - &LocalReadOnly {} + self.0.db.get_read_only() } } impl CtxDbRead for ReducerContext { fn db_read_only(&self) -> &LocalReadOnly { - &LocalReadOnly {} + self.db.get_read_only() } } impl CtxDbRead for ViewContext { fn db_read_only(&self) -> &LocalReadOnly { - &LocalReadOnly {} + &self.db } } impl CtxDbRead for AnonymousViewContext { fn db_read_only(&self) -> &LocalReadOnly { - &LocalReadOnly {} + &self.db } } @@ -1598,13 +1934,13 @@ pub trait CtxDbWrite: CtxDbRead { impl CtxDbWrite for TxContext { fn db(&self) -> &Local { - &Local {} + &self.0.db } } impl CtxDbWrite for ReducerContext { fn db(&self) -> &Local { - &Local {} + &self.db } } @@ -1880,8 +2216,12 @@ impl AuthCtx { Self::new(true, || None) } - /// Creates an [`AuthCtx`] using the json claims from a [JWT]. - /// This can be used to write unit tests. + /// Creates an [`AuthCtx`] using already-validated JSON claims from a [JWT]. + /// + /// This is infallible because host reducer calls receive claims only after + /// the server has validated them. Native unit tests should use + /// [`test_utils::TestAuth::from_jwt_payload`](crate::test_utils::TestAuth::from_jwt_payload) + /// so invalid test payloads are rejected before constructing a reducer context. /// /// [JWT]: https://en.wikipedia.org/wiki/JSON_Web_Token pub fn from_jwt_payload(jwt_payload: String) -> AuthCtx { @@ -1976,7 +2316,32 @@ impl JwtClaims { } /// The read-only version of [`Local`] #[non_exhaustive] -pub struct LocalReadOnly {} +#[derive(Clone)] +pub struct LocalReadOnly { + backend: table::LocalBackend, +} + +impl LocalReadOnly { + #[doc(hidden)] + pub fn __host() -> Self { + Self { + backend: table::LocalBackend::Host, + } + } + + #[doc(hidden)] + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + fn __test(datastore: std::sync::Arc) -> Self { + Self { + backend: table::LocalBackend::Test { datastore }, + } + } + + #[doc(hidden)] + pub fn __table_backend(&self) -> table::TableHandleBackend { + self.backend.as_table_handle_backend() + } +} // #[cfg(target_arch = "wasm32")] // #[global_allocator] diff --git a/crates/bindings/src/log_stopwatch.rs b/crates/bindings/src/log_stopwatch.rs index 91eebf46650..5b81e6ddefb 100644 --- a/crates/bindings/src/log_stopwatch.rs +++ b/crates/bindings/src/log_stopwatch.rs @@ -1,20 +1,28 @@ /// TODO(this PR): docs pub struct LogStopwatch { + #[cfg(target_arch = "wasm32")] stopwatch_id: u32, } impl LogStopwatch { + #[cfg(target_arch = "wasm32")] pub fn new(name: &str) -> Self { let name = name.as_bytes(); let id = unsafe { spacetimedb_bindings_sys::raw::console_timer_start(name.as_ptr(), name.len()) }; Self { stopwatch_id: id } } + #[cfg(not(target_arch = "wasm32"))] + pub fn new(_name: &str) -> Self { + Self {} + } + pub fn end(self) { // just drop self } } +#[cfg(target_arch = "wasm32")] impl std::ops::Drop for LogStopwatch { fn drop(&mut self) { unsafe { diff --git a/crates/bindings/src/rng.rs b/crates/bindings/src/rng.rs index 12cd78fe009..93a354a7fe7 100644 --- a/crates/bindings/src/rng.rs +++ b/crates/bindings/src/rng.rs @@ -145,6 +145,25 @@ pub struct StdbRng { } impl StdbRng { + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + pub(crate) fn seeded_cell(seed: Option) -> std::cell::OnceCell { + let cell = std::cell::OnceCell::new(); + if let Some(seed) = seed { + cell.set(Self::seed_from_u64(seed)) + .ok() + .expect("new OnceCell should accept seeded test rng"); + } + cell + } + + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + fn seed_from_u64(seed: u64) -> Self { + Self { + rng: StdRng::seed_from_u64(seed).into(), + _marker: PhantomData, + } + } + /// Seeds a [`StdbRng`] from a timestamp. fn seed_from_ts(timestamp: Timestamp) -> Self { Self { diff --git a/crates/bindings/src/rt.rs b/crates/bindings/src/rt.rs index 7d456d73fde..1416c790cda 100644 --- a/crates/bindings/src/rt.rs +++ b/crates/bindings/src/rt.rs @@ -9,7 +9,9 @@ use spacetimedb_lib::db::raw_def::v10::{ }; pub use spacetimedb_lib::db::raw_def::v9::Lifecycle as LifecycleReducer; use spacetimedb_lib::db::raw_def::v9::{RawIndexAlgorithm, TableType, ViewResultHeader}; -use spacetimedb_lib::de::{self, Deserialize, DeserializeOwned, Error as _, SeqProductAccess}; +#[cfg(any(target_arch = "wasm32", feature = "unstable"))] +use spacetimedb_lib::de::DeserializeOwned; +use spacetimedb_lib::de::{self, Deserialize, Error as _, SeqProductAccess}; use spacetimedb_lib::sats::typespace::TypespaceBuilder; use spacetimedb_lib::sats::{impl_deserialize, impl_serialize, ProductTypeElement}; use spacetimedb_lib::ser::{Serialize, SerializeSeqProduct}; @@ -447,6 +449,19 @@ impl ViewRegistrar { { register_view::(view) } + + #[cfg(feature = "test-utils")] + #[inline] + #[doc(hidden)] + pub fn register_for_tests<'a, A, I, T, V>(view: V) + where + A: Args<'a>, + T: ViewReturn, + I: FnInfo, + V: View<'a, A, T>, + { + register_view_for_tests::(view) + } } impl ViewRegistrar { @@ -460,6 +475,19 @@ impl ViewRegistrar { { register_anonymous_view::(view) } + + #[cfg(feature = "test-utils")] + #[inline] + #[doc(hidden)] + pub fn register_for_tests<'a, A, I, T, V>(view: V) + where + A: Args<'a>, + T: ViewReturn, + I: FnInfo, + V: AnonymousView<'a, A, T>, + { + register_anonymous_view_for_tests::(view) + } } /// Assert that a reducer type-checks with a given type. @@ -810,6 +838,21 @@ pub fn register_reducer<'a, A: Args<'a>, I: FnInfo>(_: impl }) } +#[cfg(feature = "test-utils")] +#[doc(hidden)] +pub fn register_reducer_for_tests<'a, A: Args<'a>, I: FnInfo>(_: impl Reducer<'a, A>) { + register_describer(|module| { + let params = A::schema::(&mut module.inner); + if let Some(lifecycle) = I::LIFECYCLE { + module.inner.add_lifecycle_reducer(lifecycle, I::NAME, params); + } else { + module.inner.add_reducer(I::NAME, params); + } + + module.inner.add_explicit_names(I::explicit_names()); + }) +} + pub fn register_procedure<'a, A, Ret, I>(_: impl Procedure<'a, A, Ret>) where A: Args<'a>, @@ -826,6 +869,23 @@ where }) } +#[cfg(all(feature = "test-utils", feature = "unstable"))] +#[doc(hidden)] +pub fn register_procedure_for_tests<'a, A, Ret, I>(_: impl Procedure<'a, A, Ret>) +where + A: Args<'a>, + Ret: SpacetimeType + Serialize, + I: FnInfo, +{ + register_describer(|module| { + let params = A::schema::(&mut module.inner); + let ret_ty = ::make_type(&mut module.inner); + module.inner.add_procedure(I::NAME, params, ret_ty); + + module.inner.add_explicit_names(I::explicit_names()); + }) +} + /// Registers a describer for the view `I` with arguments `A` and return type `Vec`. pub fn register_view<'a, A, I, T>(_: impl View<'a, A, T>) where @@ -850,6 +910,25 @@ where }) } +#[cfg(feature = "test-utils")] +#[doc(hidden)] +pub fn register_view_for_tests<'a, A, I, T>(_: impl View<'a, A, T>) +where + A: Args<'a>, + I: FnInfo, + T: ViewReturn, +{ + register_describer(|module| { + let params = A::schema::(&mut module.inner); + let return_type = I::return_type(&mut module.inner).unwrap(); + module + .inner + .add_view(I::NAME, module.views.len(), true, false, params, return_type); + + module.inner.add_explicit_names(I::explicit_names()); + }) +} + #[cfg(feature = "unstable")] pub fn register_http_handler(name: &'static str, handler: HttpHandlerFn) { register_describer(move |module| { @@ -894,6 +973,25 @@ where }) } +#[cfg(feature = "test-utils")] +#[doc(hidden)] +pub fn register_anonymous_view_for_tests<'a, A, I, T>(_: impl AnonymousView<'a, A, T>) +where + A: Args<'a>, + I: FnInfo, + T: ViewReturn, +{ + register_describer(|module| { + let params = A::schema::(&mut module.inner); + let return_type = I::return_type(&mut module.inner).unwrap(); + module + .inner + .add_view(I::NAME, module.views_anon.len(), true, true, params, return_type); + + module.inner.add_explicit_names(I::explicit_names()); + }) +} + /// Registers a row-level security policy. pub fn register_row_level_security(sql: &'static str) { register_describer(|module| { @@ -959,6 +1057,16 @@ static VIEWS: OnceLock> = OnceLock::new(); pub type AnonymousFn = fn(AnonymousViewContext, &[u8]) -> Vec; static ANONYMOUS_VIEWS: OnceLock> = OnceLock::new(); +#[cfg(feature = "test-utils")] +#[doc(hidden)] +pub fn module_def_for_tests() -> RawModuleDef { + let mut module = ModuleBuilder::default(); + for describer in &mut *DESCRIBERS.lock().unwrap() { + describer(&mut module) + } + RawModuleDef::V10(module.inner.finish()) +} + /// Called by the host when the module is initialized /// to describe the module into a serialized form that is returned. /// @@ -1053,7 +1161,7 @@ extern "C" fn __call_reducer__( // Assemble the `ReducerContext`. let timestamp = Timestamp::from_micros_since_unix_epoch(timestamp as i64); - let ctx = ReducerContext::new(crate::Local {}, sender, conn_id, timestamp); + let ctx = ReducerContext::new(crate::Local::__host(), sender, conn_id, timestamp); // Fetch reducer function. let reducers = REDUCERS.get().unwrap(); @@ -1401,6 +1509,37 @@ macro_rules! __make_register_reftype { extern "C" fn __register_describer() { $crate::rt::register_reftype::<$ty>() } + + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + #[used] + #[cfg_attr( + any( + target_os = "linux", + target_os = "android", + target_os = "freebsd", + target_os = "dragonfly", + target_os = "netbsd", + target_os = "openbsd", + ), + unsafe(link_section = ".init_array") + )] + #[cfg_attr( + any( + target_os = "macos", + target_os = "ios", + target_os = "tvos", + target_os = "watchos", + target_os = "visionos", + ), + unsafe(link_section = "__DATA,__mod_init_func") + )] + #[cfg_attr(target_os = "windows", unsafe(link_section = ".CRT$XCU"))] + static _STDB_TEST_UTILS_INIT: unsafe extern "C" fn() = { + unsafe extern "C" fn __stdb_test_utils_register() { + $crate::rt::register_reftype::<$ty>() + } + __stdb_test_utils_register + }; }; }; } @@ -1421,7 +1560,7 @@ pub fn volatile_nonatomic_schedule_immediate<'de, A: Args<'de>, R: Reducer<'de, /// /// Panics if the bytes from `source` fail to deserialize as `T`. /// The type name of `T` will be included in the panic message. -#[cfg_attr(not(feature = "unstable"), allow(unused))] +#[cfg(any(target_arch = "wasm32", feature = "unstable"))] pub(crate) fn read_bytes_source_as(source: BytesSource) -> T { let mut buf = IterBuf::take(); read_bytes_source_into(source, &mut buf); diff --git a/crates/bindings/src/table.rs b/crates/bindings/src/table.rs index ec18f601509..d6bb3446310 100644 --- a/crates/bindings/src/table.rs +++ b/crates/bindings/src/table.rs @@ -11,6 +11,323 @@ use spacetimedb_lib::{ use spacetimedb_lib::{FilterableValue, IndexScanRangeBoundsTerminator}; pub use spacetimedb_primitives::{ColId, IndexId}; +#[doc(hidden)] +#[derive(Clone)] +pub enum LocalBackend { + Host, + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + Test { + datastore: std::sync::Arc, + }, + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + TestTx { + tx: std::rc::Rc, + }, +} + +impl LocalBackend { + #[doc(hidden)] + pub fn as_table_handle_backend(&self) -> TableHandleBackend { + match self { + Self::Host => TableHandleBackend::Host, + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + Self::Test { datastore } => TableHandleBackend::Test { + datastore: datastore.clone(), + }, + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + Self::TestTx { tx } => TableHandleBackend::TestTx { tx: tx.clone() }, + } + } +} + +#[doc(hidden)] +#[derive(Clone)] +pub enum TableHandleBackend { + Host, + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + Test { + datastore: std::sync::Arc, + }, + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + TestTx { + tx: std::rc::Rc, + }, +} + +impl TableHandleBackend { + pub fn table_id(&self, table_name: &str) -> Result { + match self { + Self::Host => { + #[cfg(target_arch = "wasm32")] + { + sys::table_id_from_name(table_name) + } + #[cfg(not(target_arch = "wasm32"))] + { + let _ = table_name; + Err(sys::Errno::HOST_CALL_FAILURE) + } + } + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + Self::Test { datastore, .. } => datastore.table_id(table_name).map_err(|_| sys::Errno::NO_SUCH_TABLE), + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + Self::TestTx { tx } => tx.table_id(table_name).map_err(|_| sys::Errno::NO_SUCH_TABLE), + } + } + + pub fn index_id(&self, index_name: &str) -> Result { + match self { + Self::Host => { + #[cfg(target_arch = "wasm32")] + { + sys::index_id_from_name(index_name) + } + #[cfg(not(target_arch = "wasm32"))] + { + let _ = index_name; + Err(sys::Errno::HOST_CALL_FAILURE) + } + } + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + Self::Test { datastore, .. } => datastore.index_id(index_name).map_err(|_| sys::Errno::NO_SUCH_INDEX), + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + Self::TestTx { tx } => tx.index_id(index_name).map_err(|_| sys::Errno::NO_SUCH_INDEX), + } + } + + fn event_table_available(&self) -> bool { + match self { + Self::Host => true, + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + Self::Test { .. } => false, + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + Self::TestTx { .. } => true, + } + } + + fn assert_event_table_available(&self, table_name: &str) { + assert!( + self.event_table_available(), + "event table `{table_name}` can only be used through a ReducerContext or transaction context" + ); + } + + fn insert_bsatn(&self, table_id: TableId, row: &mut [u8]) -> Result, sys::Errno> { + match self { + Self::Host => { + #[cfg(target_arch = "wasm32")] + { + sys::datastore_insert_bsatn(table_id, row).map(<[u8]>::to_vec) + } + #[cfg(not(target_arch = "wasm32"))] + { + let _ = (table_id, row); + Err(sys::Errno::HOST_CALL_FAILURE) + } + } + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + Self::Test { datastore, .. } => datastore.insert_bsatn_generated_cols(table_id, row).map_err(|err| { + err.insert_errno_code() + .and_then(sys::Errno::from_code) + .unwrap_or(sys::Errno::HOST_CALL_FAILURE) + }), + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + Self::TestTx { tx } => tx.insert_bsatn_generated_cols(table_id, row).map_err(|err| { + err.insert_errno_code() + .and_then(sys::Errno::from_code) + .unwrap_or(sys::Errno::HOST_CALL_FAILURE) + }), + } + } + + fn table_scan_bsatn(&self, table_id: TableId) -> Result { + match self { + Self::Host => { + #[cfg(target_arch = "wasm32")] + { + sys::datastore_table_scan_bsatn(table_id).map(TableIterInner::Host) + } + #[cfg(not(target_arch = "wasm32"))] + { + let _ = table_id; + Err(sys::Errno::HOST_CALL_FAILURE) + } + } + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + Self::Test { datastore, .. } => datastore + .table_rows_bsatn(table_id) + .map(|rows| TableIterInner::Test(rows.into_iter())) + .map_err(|_| sys::Errno::HOST_CALL_FAILURE), + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + Self::TestTx { tx } => tx + .table_rows_bsatn(table_id) + .map(|rows| TableIterInner::Test(rows.into_iter())) + .map_err(|_| sys::Errno::HOST_CALL_FAILURE), + } + } + + pub fn table_row_count(&self, table_id: TableId) -> Result { + match self { + Self::Host => { + #[cfg(target_arch = "wasm32")] + { + sys::datastore_table_row_count(table_id) + } + #[cfg(not(target_arch = "wasm32"))] + { + let _ = table_id; + Err(sys::Errno::HOST_CALL_FAILURE) + } + } + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + Self::Test { datastore, .. } => datastore + .table_row_count(table_id) + .map_err(|_| sys::Errno::NO_SUCH_TABLE), + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + Self::TestTx { tx } => tx.table_row_count(table_id).map_err(|_| sys::Errno::NO_SUCH_TABLE), + } + } + + fn index_scan_point_bsatn(&self, index_id: IndexId, point: &[u8]) -> Result { + match self { + Self::Host => { + #[cfg(target_arch = "wasm32")] + { + sys::datastore_index_scan_point_bsatn(index_id, point).map(TableIterInner::Host) + } + #[cfg(not(target_arch = "wasm32"))] + { + let _ = (index_id, point); + Err(sys::Errno::HOST_CALL_FAILURE) + } + } + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + Self::Test { datastore, .. } => datastore + .index_scan_point_bsatn(index_id, point) + .map(|rows| TableIterInner::Test(rows.into_iter())) + .map_err(|_| sys::Errno::HOST_CALL_FAILURE), + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + Self::TestTx { tx } => tx + .index_scan_point_bsatn(index_id, point) + .map(|rows| TableIterInner::Test(rows.into_iter())) + .map_err(|_| sys::Errno::HOST_CALL_FAILURE), + } + } + + fn index_scan_range_bsatn( + &self, + index_id: IndexId, + prefix: &[u8], + prefix_elems: ColId, + rstart: &[u8], + rend: &[u8], + ) -> Result { + match self { + Self::Host => { + #[cfg(target_arch = "wasm32")] + { + sys::datastore_index_scan_range_bsatn(index_id, prefix, prefix_elems, rstart, rend) + .map(TableIterInner::Host) + } + #[cfg(not(target_arch = "wasm32"))] + { + let _ = (index_id, prefix, prefix_elems, rstart, rend); + Err(sys::Errno::HOST_CALL_FAILURE) + } + } + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + Self::Test { datastore, .. } => datastore + .index_scan_range_bsatn(index_id, prefix, prefix_elems, rstart, rend) + .map(|rows| TableIterInner::Test(rows.into_iter())) + .map_err(|_| sys::Errno::HOST_CALL_FAILURE), + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + Self::TestTx { tx } => tx + .index_scan_range_bsatn(index_id, prefix, prefix_elems, rstart, rend) + .map(|rows| TableIterInner::Test(rows.into_iter())) + .map_err(|_| sys::Errno::HOST_CALL_FAILURE), + } + } + + fn delete_by_index_scan_point_bsatn(&self, index_id: IndexId, point: &[u8]) -> Result { + match self { + Self::Host => { + #[cfg(target_arch = "wasm32")] + { + sys::datastore_delete_by_index_scan_point_bsatn(index_id, point) + } + #[cfg(not(target_arch = "wasm32"))] + { + let _ = (index_id, point); + Err(sys::Errno::HOST_CALL_FAILURE) + } + } + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + Self::Test { datastore, .. } => datastore + .delete_by_index_scan_point_bsatn(index_id, point) + .map_err(|_| sys::Errno::HOST_CALL_FAILURE), + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + Self::TestTx { tx } => tx + .delete_by_index_scan_point_bsatn(index_id, point) + .map_err(|_| sys::Errno::HOST_CALL_FAILURE), + } + } + + fn delete_by_index_scan_range_bsatn( + &self, + index_id: IndexId, + prefix: &[u8], + prefix_elems: ColId, + rstart: &[u8], + rend: &[u8], + ) -> Result { + match self { + Self::Host => { + #[cfg(target_arch = "wasm32")] + { + sys::datastore_delete_by_index_scan_range_bsatn(index_id, prefix, prefix_elems, rstart, rend) + } + #[cfg(not(target_arch = "wasm32"))] + { + let _ = (index_id, prefix, prefix_elems, rstart, rend); + Err(sys::Errno::HOST_CALL_FAILURE) + } + } + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + Self::Test { datastore, .. } => datastore + .delete_by_index_scan_range_bsatn(index_id, prefix, prefix_elems, rstart, rend) + .map_err(|_| sys::Errno::HOST_CALL_FAILURE), + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + Self::TestTx { tx } => tx + .delete_by_index_scan_range_bsatn(index_id, prefix, prefix_elems, rstart, rend) + .map_err(|_| sys::Errno::HOST_CALL_FAILURE), + } + } + + fn update_bsatn(&self, table_id: TableId, index_id: IndexId, row: &mut [u8]) -> Result, sys::Errno> { + match self { + Self::Host => { + #[cfg(target_arch = "wasm32")] + { + sys::datastore_update_bsatn(table_id, index_id, row).map(<[u8]>::to_vec) + } + #[cfg(not(target_arch = "wasm32"))] + { + let _ = (table_id, index_id, row); + Err(sys::Errno::HOST_CALL_FAILURE) + } + } + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + Self::Test { datastore, .. } => datastore + .update_bsatn_generated_cols(table_id, index_id, row) + .map_err(|_| sys::Errno::HOST_CALL_FAILURE), + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + Self::TestTx { tx } => tx + .update_bsatn_generated_cols(table_id, index_id, row) + .map_err(|_| sys::Errno::HOST_CALL_FAILURE), + } + } +} + /// Implemented for every `TableHandle` struct generated by the [`table`](macro@crate::table) macro. /// Contains methods that are present for every table, regardless of what unique constraints /// and indexes are present. @@ -26,7 +343,23 @@ pub trait Table: TableInternal + ExplicitNames { /// This reads datastore metadata, so it runs in constant time. /// It also takes into account modifications by the current transaction. fn count(&self) -> u64 { - count::() + if Self::IS_EVENT { + self.__backend().assert_event_table_available(Self::TABLE_NAME); + } + + let table_id = self + .__backend() + .table_id(Self::TABLE_NAME) + .expect("table_id_from_name() call failed"); + if Self::IS_EVENT { + self.__backend() + .table_row_count(table_id) + .expect("event_table_row_count() call failed") + } else { + self.__backend() + .table_row_count(table_id) + .expect("datastore_table_row_count() call failed") + } } /// Iterate over all rows of the table. @@ -38,8 +371,22 @@ pub trait Table: TableInternal + ExplicitNames { /// (This keeps track of changes made to the table since the start of this reducer invocation. For example, if rows have been deleted since the start of this reducer invocation, those rows will not be returned by `iter`. Similarly, inserted rows WILL be returned.) #[inline] fn iter(&self) -> impl Iterator { - let table_id = Self::table_id(); - let iter = sys::datastore_table_scan_bsatn(table_id).expect("datastore_table_scan_bsatn() call failed"); + if Self::IS_EVENT { + self.__backend().assert_event_table_available(Self::TABLE_NAME); + } + + let table_id = self + .__backend() + .table_id(Self::TABLE_NAME) + .expect("table_id_from_name() call failed"); + let iter = self.__backend(); + let iter = if Self::IS_EVENT { + iter.table_scan_bsatn(table_id) + .expect("event_table_scan_bsatn() call failed") + } else { + iter.table_scan_bsatn(table_id) + .expect("datastore_table_scan_bsatn() call failed") + }; TableIter::new(iter) } @@ -87,7 +434,7 @@ pub trait Table: TableInternal + ExplicitNames { /// inserting an exact duplicate of an already-present row will return `Ok`. #[track_caller] fn try_insert(&self, row: Self::Row) -> Result> { - insert::(row, IterBuf::take()) + insert::(self, row, IterBuf::take()) } /// Deletes a row equal to `row` from the table. @@ -146,6 +493,9 @@ pub trait TableInternal: Sized { /// Returns the ID of this table. fn table_id() -> TableId; + #[doc(hidden)] + fn __backend(&self) -> &TableHandleBackend; + fn get_default_col_values() -> Vec; } @@ -332,12 +682,18 @@ pub trait PrimaryKey {} /// /// pub struct UniqueColumn { + backend: TableHandleBackend, _marker: PhantomData<(Tbl, ColType, Col)>, } impl> UniqueColumn { #[doc(hidden)] - pub const __NEW: Self = Self { _marker: PhantomData }; + pub fn __new(backend: TableHandleBackend) -> Self { + Self { + backend, + _marker: PhantomData, + } + } /// Finds and returns the row where the value in the unique column matches the supplied `col_val`, /// or `None` if no such row is present in the database state. @@ -352,7 +708,7 @@ impl> UniqueColumn &'a Col::ColType: FilterableValue, { - find::(col_val.borrow()) + find::(&self.backend, col_val.borrow()) } /// Deletes the row where the value in the unique column matches the supplied `col_val`, @@ -366,11 +722,21 @@ impl> UniqueColumn (bool, IterBuf) { - let index_id = Col::index_id(); + if Tbl::IS_EVENT { + self.backend.assert_event_table_available(Tbl::TABLE_NAME); + } + + let index_id = self + .backend + .index_id(Col::INDEX_NAME) + .expect("index_id_from_name() call failed"); let point = IterBuf::serialize(col_val).unwrap(); - let n_del = sys::datastore_delete_by_index_scan_point_bsatn(index_id, &point).unwrap_or_else(|e| { - panic!("unique: unexpected error from datastore_delete_by_index_scan_point_bsatn: {e}") - }); + let n_del = if Tbl::IS_EVENT { + self.backend.delete_by_index_scan_point_bsatn(index_id, &point) + } else { + self.backend.delete_by_index_scan_point_bsatn(index_id, &point) + } + .unwrap_or_else(|e| panic!("unique: unexpected error from datastore_delete_by_index_scan_point_bsatn: {e}")); (n_del > 0, point) } @@ -393,8 +759,16 @@ impl> UniqueColumn(Col::index_id(), new_row, buf) + let index_id = self + .backend + .index_id(Col::INDEX_NAME) + .expect("index_id_from_name() call failed"); + update::(&self.backend, index_id, new_row, buf) } /// Inserts `new_row` into the table, first checking for an existing @@ -414,7 +788,7 @@ impl> UniqueColumn(new_row, buf) + insert_with_backend::(&self.backend, new_row, buf) } /// Inserts `new_row` into the table, first checking for an existing @@ -431,12 +805,27 @@ impl> UniqueColumn>(col_val: &Col::ColType) -> Option { +fn find>( + backend: &TableHandleBackend, + col_val: &Col::ColType, +) -> Option { + if Tbl::IS_EVENT { + backend.assert_event_table_available(Tbl::TABLE_NAME); + } + // Find the row with a match. - let index_id = Col::index_id(); + let index_id = backend + .index_id(Col::INDEX_NAME) + .expect("index_id_from_name() call failed"); let point = IterBuf::serialize(col_val).unwrap(); - let iter = datastore_index_scan_point_bsatn(index_id, &point); + let iter = if Tbl::IS_EVENT { + backend + .index_scan_point_bsatn(index_id, &point) + .unwrap_or_else(|e| panic!("unexpected error from `datastore_index_scan_point_bsatn`: {e}")) + } else { + datastore_index_scan_point_bsatn(backend, index_id, &point) + }; let mut iter = TableIter::new_with_buf(iter, point); // We will always find either 0 or 1 rows here due to the unique constraint. @@ -450,8 +839,9 @@ fn find>(col_val: &Col::ColType) -> /// See `sys::datastore_index_scan_point_bsatn`. /// Panics when the aforementioned errors. -fn datastore_index_scan_point_bsatn(index_id: IndexId, point: &[u8]) -> sys::RowIter { - sys::datastore_index_scan_point_bsatn(index_id, point) +fn datastore_index_scan_point_bsatn(backend: &TableHandleBackend, index_id: IndexId, point: &[u8]) -> TableIterInner { + backend + .index_scan_point_bsatn(index_id, point) .unwrap_or_else(|e| panic!("unexpected error from `datastore_index_scan_point_bsatn`: {e}")) } @@ -466,25 +856,34 @@ fn datastore_index_scan_point_bsatn(index_id: IndexId, point: &[u8]) -> sys::Row /// This is because read-only indexes still need [`Table`] metadata. /// The view handle itself deliberately does not implement `Table`. pub struct UniqueColumnReadOnly { + backend: TableHandleBackend, _marker: PhantomData<(Tbl, ColType, Col)>, } impl> UniqueColumnReadOnly { #[doc(hidden)] - pub const __NEW: Self = Self { _marker: PhantomData }; + pub fn __new(backend: TableHandleBackend) -> Self { + Self { + backend, + _marker: PhantomData, + } + } #[inline] pub fn find(&self, col_val: impl Borrow) -> Option where for<'a> &'a Col::ColType: FilterableValue, { - find::(col_val.borrow()) + find::(&self.backend, col_val.borrow()) } } /// Information about the `index_id` of an index /// and the number of columns the index indexes. pub trait Index { + /// The generated runtime name of this index. + const INDEX_NAME: &'static str; + /// The number of columns the index indexes. /// /// Used to determine whether a scan for e.g., `(a, b)`, @@ -554,12 +953,18 @@ pub trait IndexIsPointed: Index {} /// ``` /// pub struct PointIndex { + backend: TableHandleBackend, _marker: PhantomData<(Tbl, IndexType, Idx)>, } impl PointIndex { #[doc(hidden)] - pub const __NEW: Self = Self { _marker: PhantomData }; + pub fn __new(backend: TableHandleBackend) -> Self { + Self { + backend, + _marker: PhantomData, + } + } /// Returns an iterator over all rows in the database state /// where the indexed column(s) equal `point`. @@ -600,7 +1005,7 @@ impl PointIndex where P: WithPointArg, { - filter_point::(point) + filter_point::(&self.backend, point) } /// Deletes all rows in the database state @@ -641,9 +1046,17 @@ impl PointIndex where P: WithPointArg, { - let index_id = Idx::index_id(); + if Tbl::IS_EVENT { + self.backend.assert_event_table_available(Tbl::TABLE_NAME); + } + + let index_id = self + .backend + .index_id(Idx::INDEX_NAME) + .expect("index_id_from_name() call failed"); point.with_point_arg(|point| { - sys::datastore_delete_by_index_scan_point_bsatn(index_id, point) + self.backend + .delete_by_index_scan_point_bsatn(index_id, point) .unwrap_or_else(|e| panic!("unexpected error from `datastore_delete_by_index_scan_point_bsatn`: {e}")) .into() }) @@ -654,13 +1067,23 @@ impl PointIndex /// /// The type parameter `K` is either `()` or [`SingleBound`] /// and is used to workaround the orphan rule. -fn filter_point(point: impl WithPointArg) -> impl Iterator +fn filter_point( + backend: &TableHandleBackend, + point: P, +) -> impl Iterator + use where Tbl: Table, Idx: IndexIsPointed, + P: WithPointArg, { - let index_id = Idx::index_id(); - let iter = point.with_point_arg(|point| datastore_index_scan_point_bsatn(index_id, point)); + if Tbl::IS_EVENT { + backend.assert_event_table_available(Tbl::TABLE_NAME); + } + + let index_id = backend + .index_id(Idx::INDEX_NAME) + .expect("index_id_from_name() call failed"); + let iter = point.with_point_arg(|point| datastore_index_scan_point_bsatn(backend, index_id, point)); TableIter::new(iter) } @@ -674,18 +1097,24 @@ where /// This is because read-only indexes still need [`Table`] metadata. /// The view handle itself deliberately does not implement `Table`. pub struct PointIndexReadOnly { + backend: TableHandleBackend, _marker: PhantomData<(Tbl, IndexType, Idx)>, } impl PointIndexReadOnly { #[doc(hidden)] - pub const __NEW: Self = Self { _marker: PhantomData }; + pub fn __new(backend: TableHandleBackend) -> Self { + Self { + backend, + _marker: PhantomData, + } + } pub fn filter(&self, point: P) -> impl Iterator + use where P: WithPointArg, { - filter_point::(point) + filter_point::(&self.backend, point) } } @@ -791,12 +1220,18 @@ pub trait IndexIsRanged: Index {} /// ``` /// pub struct RangedIndex { + backend: TableHandleBackend, _marker: PhantomData<(Tbl, IndexType, Idx)>, } impl RangedIndex { #[doc(hidden)] - pub const __NEW: Self = Self { _marker: PhantomData }; + pub fn __new(backend: TableHandleBackend) -> Self { + Self { + backend, + _marker: PhantomData, + } + } /// Returns an iterator over all rows in the database state where the indexed column(s) match the bounds `b`. /// @@ -877,7 +1312,7 @@ impl RangedIndex where B: IndexScanRangeBounds, { - filter::(b) + filter::(&self.backend, b) } /// Deletes all rows in the database state where the indexed column(s) match the bounds `b`. @@ -951,10 +1386,18 @@ impl RangedIndex where B: IndexScanRangeBounds, { - let index_id = Idx::index_id(); + if Tbl::IS_EVENT { + self.backend.assert_event_table_available(Tbl::TABLE_NAME); + } + + let index_id = self + .backend + .index_id(Idx::INDEX_NAME) + .expect("index_id_from_name() call failed"); if const { is_point_scan::() } { b.with_point_arg(|point| { - sys::datastore_delete_by_index_scan_point_bsatn(index_id, point) + self.backend + .delete_by_index_scan_point_bsatn(index_id, point) .unwrap_or_else(|e| { panic!("unexpected error from `datastore_delete_by_index_scan_point_bsatn`: {e}") }) @@ -963,7 +1406,8 @@ impl RangedIndex } else { let args = b.get_range_args(); let (prefix, prefix_elems, rstart, rend) = args.args_for_syscall(); - sys::datastore_delete_by_index_scan_range_bsatn(index_id, prefix, prefix_elems, rstart, rend) + self.backend + .delete_by_index_scan_range_bsatn(index_id, prefix, prefix_elems, rstart, rend) .unwrap_or_else(|e| panic!("unexpected error from `datastore_delete_by_index_scan_range_bsatn`: {e}")) .into() } @@ -974,20 +1418,30 @@ impl RangedIndex /// /// The type parameter `K` is either `()` or [`SingleBound`] /// and is used to workaround the orphan rule. -fn filter(b: B) -> impl Iterator +fn filter( + backend: &TableHandleBackend, + b: B, +) -> impl Iterator + use where Tbl: Table, Idx: Index, B: IndexScanRangeBounds, { - let index_id = Idx::index_id(); + if Tbl::IS_EVENT { + backend.assert_event_table_available(Tbl::TABLE_NAME); + } + + let index_id = backend + .index_id(Idx::INDEX_NAME) + .expect("index_id_from_name() call failed"); let iter = if const { is_point_scan::() } { - b.with_point_arg(|point| datastore_index_scan_point_bsatn(index_id, point)) + b.with_point_arg(|point| datastore_index_scan_point_bsatn(backend, index_id, point)) } else { let args = b.get_range_args(); let (prefix, prefix_elems, rstart, rend) = args.args_for_syscall(); - sys::datastore_index_scan_range_bsatn(index_id, prefix, prefix_elems, rstart, rend) + backend + .index_scan_range_bsatn(index_id, prefix, prefix_elems, rstart, rend) .unwrap_or_else(|e| panic!("unexpected error from `datastore_index_scan_range_bsatn`: {e}")) }; @@ -1004,18 +1458,24 @@ where /// This is because read-only indexes still need [`Table`] metadata. /// The view handle itself deliberately does not implement `Table`. pub struct RangedIndexReadOnly { + backend: TableHandleBackend, _marker: PhantomData<(Tbl, IndexType, Idx)>, } impl RangedIndexReadOnly { #[doc(hidden)] - pub const __NEW: Self = Self { _marker: PhantomData }; + pub fn __new(backend: TableHandleBackend) -> Self { + Self { + backend, + _marker: PhantomData, + } + } pub fn filter(&self, b: B) -> impl Iterator + use where B: IndexScanRangeBounds, { - filter::(b) + filter::(&self.backend, b) } } @@ -1347,17 +1807,33 @@ impl SequenceTrigger for crate::sats::u256 { /// Insert a row of type `T` into the table identified by `table_id`. #[track_caller] -fn insert(mut row: T::Row, mut buf: IterBuf) -> Result> { - let table_id = T::table_id(); +fn insert(table: &T, row: T::Row, buf: IterBuf) -> Result> { + insert_with_backend::(table.__backend(), row, buf) +} + +/// Insert a row of type `T` using `backend`. +#[track_caller] +fn insert_with_backend( + backend: &TableHandleBackend, + mut row: T::Row, + mut buf: IterBuf, +) -> Result> { + if T::IS_EVENT { + backend.assert_event_table_available(T::TABLE_NAME); + } + + let table_id = backend + .table_id(T::TABLE_NAME) + .expect("table_id_from_name() call failed"); // Encode the row as bsatn into the buffer `buf`. buf.clear(); buf.serialize_into(&row).unwrap(); // Insert row into table. // When table has an auto-incrementing column, we must re-decode the changed `buf`. - let res = sys::datastore_insert_bsatn(table_id, &mut buf).map(|gen_cols| { + let res = backend.insert_bsatn(table_id, &mut buf).map(|gen_cols| { // Let the caller handle any generated columns written back by `sys::datastore_insert_bsatn` to `buf`. - T::integrate_generated_columns(&mut row, gen_cols); + T::integrate_generated_columns(&mut row, &gen_cols); row }); res.map_err(|e| { @@ -1374,17 +1850,23 @@ fn insert(mut row: T::Row, mut buf: IterBuf) -> Result(index_id: IndexId, mut row: T::Row, mut buf: IterBuf) -> T::Row { - let table_id = T::table_id(); +fn update(backend: &TableHandleBackend, index_id: IndexId, mut row: T::Row, mut buf: IterBuf) -> T::Row { + if T::IS_EVENT { + backend.assert_event_table_available(T::TABLE_NAME); + } + + let table_id = backend + .table_id(T::TABLE_NAME) + .expect("table_id_from_name() call failed"); // Encode the row as bsatn into the buffer `buf`. buf.clear(); buf.serialize_into(&row).unwrap(); // Insert row into table. // When table has an auto-incrementing column, we must re-decode the changed `buf`. - let res = sys::datastore_update_bsatn(table_id, index_id, &mut buf).map(|gen_cols| { + let res = backend.update_bsatn(table_id, index_id, &mut buf).map(|gen_cols| { // Let the caller handle any generated columns written back by `sys::datastore_update_bsatn` to `buf`. - T::integrate_generated_columns(&mut row, gen_cols); + T::integrate_generated_columns(&mut row, &gen_cols); row }); @@ -1393,9 +1875,32 @@ fn update(index_id: IndexId, mut row: T::Row, mut buf: IterBuf) -> T:: } /// A table iterator which yields values of the `TableType` corresponding to the table. +enum TableIterInner { + #[cfg(target_arch = "wasm32")] + Host(sys::RowIter), + #[cfg(not(target_arch = "wasm32"))] + #[allow(dead_code)] + Host, + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + Test(std::vec::IntoIter>), +} + +impl TableIterInner { + fn is_exhausted(&self) -> bool { + match self { + #[cfg(target_arch = "wasm32")] + Self::Host(iter) => iter.is_exhausted(), + #[cfg(not(target_arch = "wasm32"))] + Self::Host => true, + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + Self::Test(iter) => iter.as_slice().is_empty(), + } + } +} + struct TableIter { /// The underlying source of our `Buffer`s. - inner: sys::RowIter, + inner: TableIterInner, /// The current position in the buffer, from which `deserializer` can read. reader: Cursor, @@ -1405,12 +1910,12 @@ struct TableIter { impl TableIter { #[inline] - fn new(iter: sys::RowIter) -> Self { + fn new(iter: TableIterInner) -> Self { TableIter::new_with_buf(iter, IterBuf::take()) } #[inline] - fn new_with_buf(iter: sys::RowIter, mut buf: IterBuf) -> Self { + fn new_with_buf(iter: TableIterInner, mut buf: IterBuf) -> Self { buf.clear(); TableIter { inner: iter, @@ -1436,14 +1941,26 @@ impl Iterator for TableIter { } // Don't fetch the next chunk if there is none. - if self.inner.is_exhausted() { - return None; + match &mut self.inner { + #[cfg(target_arch = "wasm32")] + TableIterInner::Host(iter) => { + if iter.is_exhausted() { + return None; + } + + // Otherwise, try to fetch the next chunk while reusing the buffer. + self.reader.buf.clear(); + self.reader.pos.set(0); + iter.read(&mut self.reader.buf); + } + #[cfg(not(target_arch = "wasm32"))] + TableIterInner::Host => return None, + #[cfg(all(feature = "test-utils", not(target_arch = "wasm32")))] + TableIterInner::Test(iter) => { + let row = iter.next()?; + return Some(bsatn::from_slice(&row).expect("Failed to decode row!")); + } } - - // Otherwise, try to fetch the next chunk while reusing the buffer. - self.reader.buf.clear(); - self.reader.pos.set(0); - self.inner.read(&mut self.reader.buf); } } } diff --git a/crates/bindings/src/test_utils.rs b/crates/bindings/src/test_utils.rs new file mode 100644 index 00000000000..613d9d7d189 --- /dev/null +++ b/crates/bindings/src/test_utils.rs @@ -0,0 +1,609 @@ +//! Utilities for testing SpacetimeDB modules without a running host. +//! +//! Enabled via the `test-utils` feature. In your module crate: +//! +//! ```toml +//! [dev-dependencies] +//! spacetimedb = { features = ["test-utils"] } +//! ``` +//! +//! Table names are registered before the test runner starts via platform init +//! sections (`.init_array` on ELF, `__mod_init_func` on Mach-O, `.CRT$XCU` +//! on Windows). No explicit setup or initialization call is needed. + +use std::sync::Mutex; + +use spacetimedb_lib::RawModuleDef; + +#[cfg(not(target_arch = "wasm32"))] +pub use spacetimedb_test_datastore::{TestDatastore, TestDatastoreError}; + +/// A deterministic clock for module unit tests. +#[cfg(not(target_arch = "wasm32"))] +pub struct TestClock { + now: std::rc::Rc>, +} + +#[cfg(not(target_arch = "wasm32"))] +impl TestClock { + /// Create a clock initialized to `timestamp`. + pub fn new(timestamp: crate::Timestamp) -> Self { + Self { + now: std::rc::Rc::new(std::cell::Cell::new(timestamp)), + } + } + + /// Return the current test timestamp. + pub fn now(&self) -> crate::Timestamp { + self.now.get() + } + + /// Set the current test timestamp. + pub fn set(&self, timestamp: crate::Timestamp) { + self.now.set(timestamp); + } + + /// Advance the current test timestamp by `duration`. + pub fn advance(&self, duration: crate::TimeDuration) { + self.set( + self.now() + .checked_add(duration) + .expect("advancing test clock overflowed Timestamp"), + ); + } +} + +#[cfg(not(target_arch = "wasm32"))] +impl Default for TestClock { + fn default() -> Self { + Self::new(crate::Timestamp::UNIX_EPOCH) + } +} + +#[cfg(not(target_arch = "wasm32"))] +impl Clone for TestClock { + fn clone(&self) -> Self { + Self { now: self.now.clone() } + } +} + +/// A deterministic RNG seed source for module unit tests. +#[cfg(all(feature = "rand08", not(target_arch = "wasm32")))] +pub struct TestRng { + state: std::rc::Rc>, +} + +#[cfg(all(feature = "rand08", not(target_arch = "wasm32")))] +struct TestRngState { + explicit_seed: Option, + rng: rand08::rngs::StdRng, +} + +#[cfg(all(feature = "rand08", not(target_arch = "wasm32")))] +impl TestRng { + /// Create a deterministic test RNG seed source initialized to `seed`. + pub fn new(seed: u64) -> Self { + Self { + state: std::rc::Rc::new(std::cell::RefCell::new(TestRngState { + explicit_seed: Some(seed), + rng: ::seed_from_u64(seed), + })), + } + } + + /// Return the optional root seed used by the test seed source. + pub fn seed(&self) -> Option { + self.state.borrow().explicit_seed + } + + /// Reset the seed source to `seed`. + /// + /// Future reducer and procedure contexts will be seeded by drawing from this + /// deterministic source. Existing contexts are not affected. + pub fn set_seed(&self, seed: u64) { + *self.state.borrow_mut() = TestRngState { + explicit_seed: Some(seed), + rng: ::seed_from_u64(seed), + }; + } + + /// Clear the configured seed and restore root seed `0`. + pub fn clear_seed(&self) { + *self.state.borrow_mut() = TestRngState { + explicit_seed: None, + rng: ::seed_from_u64(0), + }; + } + + pub(crate) fn next_seed(&self) -> u64 { + rand08::RngCore::next_u64(&mut self.state.borrow_mut().rng) + } +} + +#[cfg(all(feature = "rand08", not(target_arch = "wasm32")))] +impl Default for TestRng { + fn default() -> Self { + Self { + state: std::rc::Rc::new(std::cell::RefCell::new(TestRngState { + explicit_seed: None, + rng: ::seed_from_u64(0), + })), + } + } +} + +#[cfg(all(feature = "rand08", not(target_arch = "wasm32")))] +impl Clone for TestRng { + fn clone(&self) -> Self { + Self { + state: self.state.clone(), + } + } +} + +/// Authentication mode for a test reducer call. +#[cfg(not(target_arch = "wasm32"))] +pub enum TestAuth { + /// An internal reducer call with no connection and no JWT. + Internal, + /// An authenticated client reducer call with a validated JWT payload. + Authenticated { + jwt_payload: String, + connection_id: crate::ConnectionId, + sender: crate::Identity, + }, +} + +#[cfg(not(target_arch = "wasm32"))] +impl TestAuth { + /// Create auth for an internal reducer call. + pub fn internal() -> Self { + Self::Internal + } + + /// Create auth for an authenticated reducer call from a validated JWT payload. + pub fn from_jwt_payload( + jwt_payload: impl Into, + connection_id: crate::ConnectionId, + ) -> Result { + let jwt_payload = jwt_payload.into(); + let claims: serde_json::Value = serde_json::from_str(&jwt_payload).map_err(TestAuthError::InvalidPayload)?; + let sender = validate_test_jwt_claims(&claims).map_err(TestAuthError::InvalidClaims)?; + Ok(Self::Authenticated { + jwt_payload, + connection_id, + sender, + }) + } + + fn into_parts( + self, + internal_identity: crate::Identity, + ) -> (crate::AuthCtx, Option, crate::Identity) { + match self { + Self::Internal => (crate::AuthCtx::internal(), None, internal_identity), + Self::Authenticated { + jwt_payload, + connection_id, + sender, + } => ( + crate::AuthCtx::from_jwt_payload(jwt_payload), + Some(connection_id), + sender, + ), + } + } +} + +#[cfg(not(target_arch = "wasm32"))] +fn validate_test_jwt_claims(claims: &serde_json::Value) -> anyhow::Result { + let issuer = required_claim(claims, "iss")?; + let subject = required_claim(claims, "sub")?; + + if issuer.len() > 128 { + anyhow::bail!("Issuer too long: {issuer:?}"); + } + if subject.len() > 128 { + anyhow::bail!("Subject too long: {subject:?}"); + } + if issuer.is_empty() { + anyhow::bail!("Issuer empty"); + } + if subject.is_empty() { + anyhow::bail!("Subject empty"); + } + + let computed_identity = crate::Identity::from_claims(issuer, subject); + if let Some(token_identity) = claims.get("hex_identity") { + let token_identity: crate::Identity = serde_json::from_value(token_identity.clone()) + .map_err(|err| anyhow::anyhow!("invalid hex_identity claim: {err}"))?; + if token_identity != computed_identity { + anyhow::bail!( + "Identity mismatch: token identity {token_identity:?} does not match computed identity {computed_identity:?}", + ); + } + } + + Ok(computed_identity) +} + +#[cfg(not(target_arch = "wasm32"))] +fn required_claim<'a>(claims: &'a serde_json::Value, name: &str) -> anyhow::Result<&'a str> { + claims + .get(name) + .ok_or_else(|| anyhow::anyhow!("Missing `{name}` claim"))? + .as_str() + .ok_or_else(|| anyhow::anyhow!("Claim `{name}` must be a string")) +} + +/// Errors returned when constructing test reducer authentication. +#[cfg(not(target_arch = "wasm32"))] +#[derive(Debug)] +pub enum TestAuthError { + InvalidPayload(serde_json::Error), + InvalidClaims(anyhow::Error), +} + +#[cfg(not(target_arch = "wasm32"))] +impl std::fmt::Display for TestAuthError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::InvalidPayload(error) => write!(f, "invalid JWT payload JSON: {error}"), + Self::InvalidClaims(error) => write!(f, "invalid JWT claims: {error}"), + } + } +} + +#[cfg(not(target_arch = "wasm32"))] +impl std::error::Error for TestAuthError {} + +/// Errors returned when executing typed test queries. +#[cfg(not(target_arch = "wasm32"))] +#[derive(Debug)] +pub enum TestQueryError { + Datastore(TestDatastoreError), + Decode(spacetimedb_lib::sats::algebraic_value::de::ValueDeserializeError), +} + +#[cfg(not(target_arch = "wasm32"))] +impl std::fmt::Display for TestQueryError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Self::Datastore(error) => write!(f, "{error}"), + Self::Decode(error) => write!(f, "query row decode error: {error:?}"), + } + } +} + +#[cfg(not(target_arch = "wasm32"))] +impl std::error::Error for TestQueryError {} + +#[cfg(not(target_arch = "wasm32"))] +impl From for TestQueryError { + fn from(error: TestDatastoreError) -> Self { + Self::Datastore(error) + } +} + +/// Hooks invoked at procedure transaction boundaries in native unit tests. +#[cfg(not(target_arch = "wasm32"))] +#[derive(Default)] +pub struct ProcedureTestHooks { + after_tx_commit: Vec anyhow::Result<()>>>, + on_sleep: Vec anyhow::Result<()>>>, +} + +#[cfg(not(target_arch = "wasm32"))] +impl ProcedureTestHooks { + /// Create an empty hook set. + pub fn new() -> Self { + Self::default() + } + + /// Add a hook that runs after each successful procedure transaction commit. + /// + /// Hook failures panic after the transaction has already committed. + pub fn after_tx_commit(mut self, hook: impl FnMut(&TestContext) -> anyhow::Result<()> + 'static) -> Self { + self.after_tx_commit.push(Box::new(hook)); + self + } + + /// Add a hook that runs when a test procedure sleeps. + /// + /// Hook failures panic before the procedure resumes. + pub fn on_sleep( + mut self, + hook: impl FnMut(&TestContext, crate::Timestamp) -> anyhow::Result<()> + 'static, + ) -> Self { + self.on_sleep.push(Box::new(hook)); + self + } + + #[doc(hidden)] + pub fn __run_after_tx_commit(&mut self, ctx: &TestContext) { + for hook in &mut self.after_tx_commit { + hook(ctx).unwrap_or_else(|err| panic!("procedure test after_tx_commit hook failed: {err}")); + } + } + + #[doc(hidden)] + pub fn __run_on_sleep(&mut self, ctx: &TestContext, wake_time: crate::Timestamp) { + for hook in &mut self.on_sleep { + hook(ctx, wake_time).unwrap_or_else(|err| panic!("procedure test on_sleep hook failed: {err}")); + } + } +} + +/// Builder for a native unit-test procedure context. +#[cfg(all(feature = "unstable", not(target_arch = "wasm32")))] +pub struct ProcedureContextBuilder<'a> { + test: &'a TestContext, + auth: TestAuth, + hooks: ProcedureTestHooks, + http_responder: crate::http::TestHttpResponder, +} + +#[cfg(all(feature = "unstable", not(target_arch = "wasm32")))] +impl<'a> ProcedureContextBuilder<'a> { + /// Install the HTTP responder used by this procedure context. + pub fn http( + mut self, + responder: impl Fn(&TestContext, crate::http::Request) -> Result + + 'static, + ) -> Self { + self.http_responder = std::rc::Rc::new(responder); + self + } + + /// Install transaction hooks used by this procedure context. + pub fn hooks(mut self, hooks: ProcedureTestHooks) -> Self { + self.hooks = hooks; + self + } + + /// Build the procedure context. + pub fn build(self) -> crate::ProcedureContext { + self.test + .procedure_context_with_options(self.auth, self.hooks, self.http_responder) + } +} + +/// A native unit-test context with an in-memory module datastore. +#[cfg(not(target_arch = "wasm32"))] +pub struct TestContext { + pub db: crate::Local, + pub clock: TestClock, + #[cfg(feature = "rand08")] + pub rng: TestRng, + pub identity: crate::Identity, + datastore: std::sync::Arc, +} + +#[cfg(not(target_arch = "wasm32"))] +impl Clone for TestContext { + fn clone(&self) -> Self { + Self { + db: self.db.clone(), + clock: self.clock.clone(), + #[cfg(feature = "rand08")] + rng: self.rng.clone(), + identity: self.identity, + datastore: self.datastore.clone(), + } + } +} + +#[cfg(not(target_arch = "wasm32"))] +impl TestContext { + /// Create a test context from the module definition registered in this test binary. + pub fn new() -> Result { + Self::from_module_def(module_def()) + } + + /// Create a test context from `raw`. + pub fn from_module_def(raw: RawModuleDef) -> Result { + let datastore = std::sync::Arc::new(TestDatastore::from_module_def(raw)?); + Ok(Self { + db: crate::Local::__test(datastore.clone()), + clock: TestClock::default(), + #[cfg(feature = "rand08")] + rng: TestRng::default(), + identity: crate::Identity::ZERO, + datastore, + }) + } + + /// The underlying in-memory datastore. + pub fn datastore(&self) -> &std::sync::Arc { + &self.datastore + } + + /// Execute a typed querybuilder query against the current test database. + pub fn run_query(&self, query: Q) -> Result, TestQueryError> + where + Q: crate::Query, + Row: crate::SpacetimeType + crate::DeserializeOwned, + { + let rows = self.datastore.run_select_query(&query.into_sql(), self.identity)?; + rows.into_iter() + .map(|row| { + ::deserialize( + spacetimedb_lib::sats::algebraic_value::de::ValueDeserializer::new( + spacetimedb_lib::AlgebraicValue::product(row), + ), + ) + .map_err(TestQueryError::Decode) + }) + .collect() + } + + /// Create a [`ViewContext`](crate::ViewContext) backed by this test context's datastore. + pub fn view_context(&self, auth: TestAuth) -> crate::ViewContext { + let (_, _, sender) = auth.into_parts(self.identity); + crate::ViewContext::__test(sender, crate::LocalReadOnly::__test(self.datastore.clone())) + } + + /// Create an [`AnonymousViewContext`](crate::AnonymousViewContext) backed by this test context's datastore. + pub fn anonymous_view_context(&self) -> crate::AnonymousViewContext { + crate::AnonymousViewContext::__test(crate::LocalReadOnly::__test(self.datastore.clone())) + } + + #[cfg(feature = "unstable")] + fn default_http_responder() -> crate::http::TestHttpResponder { + std::rc::Rc::new(|_, _| Err(crate::http::Error::new("no test HTTP responder configured"))) + } + + /// Run `body` with a reducer context backed by a single mutable transaction. + /// + /// The transaction commits when `body` returns `Ok`, rolls back when `body` + /// returns `Err`, and rolls back during unwinding if `body` panics. + pub fn with_reducer_tx( + &self, + auth: TestAuth, + body: impl FnOnce(&crate::ReducerContext) -> Result, + ) -> Result { + with_reducer_tx( + &self.datastore, + self.identity, + self.clock.now(), + #[cfg(feature = "rand08")] + Some(self.rng.next_seed()), + auth, + body, + ) + } + + /// Create a procedure context backed by this test context's datastore. + #[cfg(feature = "unstable")] + pub fn procedure_context(&self, auth: TestAuth) -> crate::ProcedureContext { + self.procedure_context_builder(auth).build() + } + + /// Create a builder for a procedure context backed by this test context's datastore. + #[cfg(feature = "unstable")] + pub fn procedure_context_builder(&self, auth: TestAuth) -> ProcedureContextBuilder<'_> { + ProcedureContextBuilder { + test: self, + auth, + hooks: ProcedureTestHooks::new(), + http_responder: Self::default_http_responder(), + } + } + + #[cfg(feature = "unstable")] + fn procedure_context_with_options( + &self, + auth: TestAuth, + hooks: ProcedureTestHooks, + http_responder: crate::http::TestHttpResponder, + ) -> crate::ProcedureContext { + let (auth, connection_id, sender) = auth.into_parts(self.identity); + crate::ProcedureContext::__test( + self.datastore.clone(), + sender, + auth, + connection_id, + self.clock.now(), + self.identity, + crate::http::HttpClient::test(self.clone(), http_responder), + self.clone(), + hooks, + #[cfg(feature = "rand08")] + Some(self.rng.next_seed()), + ) + } +} + +#[cfg(not(target_arch = "wasm32"))] +fn with_reducer_tx( + datastore: &std::sync::Arc, + identity: crate::Identity, + timestamp: crate::Timestamp, + #[cfg(feature = "rand08")] rng_seed: Option, + auth: TestAuth, + body: impl FnOnce(&crate::ReducerContext) -> Result, +) -> Result { + use core::mem; + + let test_tx = std::rc::Rc::new(datastore.begin_mut_tx()); + let rollback_tx = test_tx.clone(); + + struct DoOnDrop(F); + impl Drop for DoOnDrop { + fn drop(&mut self) { + (self.0)(); + } + } + + let rollback_guard = DoOnDrop(move || { + rollback_tx + .rollback() + .expect("should have a pending mutable test transaction") + }); + + let (auth, connection_id, sender) = auth.into_parts(identity); + let ctx = crate::ReducerContext::__test( + crate::Local::__test_tx(test_tx.clone()), + sender, + auth, + connection_id, + timestamp, + identity, + #[cfg(feature = "rand08")] + rng_seed, + ); + + let res = body(&ctx); + mem::forget(rollback_guard); + match res { + Ok(value) => { + test_tx + .commit() + .expect("committing mutable test reducer transaction failed"); + Ok(value) + } + Err(error) => { + test_tx + .rollback() + .expect("should have a pending mutable test transaction"); + Err(error) + } + } +} + +static TABLE_NAMES: Mutex> = Mutex::new(Vec::new()); + +/// Returns the name of every table defined in this binary via the `#[table]` +/// macro. Names are registered before `main()` runs, so this is safe to call +/// from any test without setup. +/// +/// # Example +/// +/// ```rust +/// use spacetimedb::test_utils::all_table_names; +/// +/// #[test] +/// fn check_tables() { +/// let names = all_table_names(); +/// assert!(names.contains(&"my_table")); +/// } +/// ``` +pub fn all_table_names() -> Vec<&'static str> { + TABLE_NAMES.lock().unwrap().clone() +} + +/// Returns the raw module definition registered by this native test binary. +/// +/// This is the same versioned `RawModuleDef` shape that a module serializes +/// from `__describe_module__`, but it is built directly from the native +/// registrations emitted by the module macros. +pub fn module_def() -> RawModuleDef { + crate::rt::module_def_for_tests() +} + +/// Called by init functions generated by the `#[table]` macro. +/// Not intended for direct use. +#[doc(hidden)] +pub fn __register_table_name(name: &'static str) { + TABLE_NAMES.lock().unwrap().push(name); +} diff --git a/crates/bindings/tests/test_utils.rs b/crates/bindings/tests/test_utils.rs new file mode 100644 index 00000000000..25f316ad24e --- /dev/null +++ b/crates/bindings/tests/test_utils.rs @@ -0,0 +1,953 @@ +use spacetimedb::spacetimedb_lib::RawModuleDef; +use spacetimedb::test_utils::{TestAuth, TestQueryError}; +use spacetimedb::{reducer, table, AnonymousViewContext, Query, ReducerContext, Table, Timestamp, ViewContext}; + +#[table(accessor = test_utils_user, public)] +#[derive(Debug, PartialEq, Eq)] +pub struct TestUtilsUser { + #[primary_key] + id: u64, + name: String, +} + +#[table(accessor = test_utils_event, public, event)] +#[derive(Debug, PartialEq, Eq)] +pub struct TestUtilsEvent { + #[primary_key] + id: u64, + #[index(btree)] + message: String, +} + +#[reducer] +pub fn add_test_utils_user(_ctx: &ReducerContext, id: u64, name: String) { + let _ = (id, name); +} + +fn query_test_utils_users_by_name(from: spacetimedb::QueryBuilder, name: &str) -> impl Query { + from.test_utils_user() + .r#where(|user| user.name.eq(name.to_owned())) + .build() +} + +fn test_utils_user_by_id_view(ctx: &AnonymousViewContext, id: u64) -> Option { + ctx.db.test_utils_user().id().find(id) +} + +fn test_utils_sender_view(ctx: &ViewContext) -> spacetimedb::Identity { + ctx.sender() +} + +// You can run these with `cargo test -p spacetimedb --features test-utils --test test_utils` + +#[test] +fn module_def_includes_native_test_registrations() { + let mut table_names = spacetimedb::test_utils::all_table_names(); + table_names.sort_unstable(); + assert!(table_names.contains(&"test_utils_user")); + + let RawModuleDef::V10(module) = spacetimedb::test_utils::module_def() else { + panic!("test utils should return a v10 raw module def"); + }; + + let tables = module.tables().expect("tables section should be present"); + assert!(tables + .iter() + .any(|table| table.source_name.as_ref() == "test_utils_user")); + assert!(tables + .iter() + .any(|table| table.source_name.as_ref() == "test_utils_event")); + + let reducers = module.reducers().expect("reducers section should be present"); + assert!(reducers + .iter() + .any(|reducer| reducer.source_name.as_ref() == "add_test_utils_user")); +} + +#[test] +fn test_datastore_initializes_from_native_test_registrations() { + let datastore = spacetimedb::test_utils::TestDatastore::from_module_def(spacetimedb::test_utils::module_def()) + .expect("test datastore should initialize"); + + assert!(datastore.table_id("test_utils_user").is_ok()); + assert!(datastore.table_id("test_utils_event").is_ok()); +} + +#[test] +fn test_context_supports_basic_table_insert_and_iter() { + let ctx = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + let table = ctx.db.test_utils_user(); + + let row = TestUtilsUser { + id: 1, + name: "Ada".to_owned(), + }; + + assert_eq!(table.count(), 0); + assert_eq!( + table.insert(row), + TestUtilsUser { + id: 1, + name: "Ada".to_owned(), + } + ); + assert_eq!(table.count(), 1); + assert_eq!( + table.iter().collect::>(), + vec![TestUtilsUser { + id: 1, + name: "Ada".to_owned(), + }] + ); +} + +#[test] +fn test_context_run_query_returns_typed_rows() { + let test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + test.db.test_utils_user().insert(TestUtilsUser { + id: 1, + name: "Ada".to_owned(), + }); + test.db.test_utils_user().insert(TestUtilsUser { + id: 2, + name: "Grace".to_owned(), + }); + + let rows: Vec = test + .run_query(spacetimedb::QueryBuilder {}.test_utils_user().build()) + .expect("query should execute"); + + assert_eq!( + rows, + vec![ + TestUtilsUser { + id: 1, + name: "Ada".to_owned(), + }, + TestUtilsUser { + id: 2, + name: "Grace".to_owned(), + }, + ] + ); +} + +#[test] +fn test_context_run_query_supports_query_returning_view_pattern() { + let test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + test.db.test_utils_user().insert(TestUtilsUser { + id: 1, + name: "Ada".to_owned(), + }); + test.db.test_utils_user().insert(TestUtilsUser { + id: 2, + name: "Grace".to_owned(), + }); + + let query = query_test_utils_users_by_name(spacetimedb::QueryBuilder {}, "Ada"); + let rows: Vec = test.run_query(query).expect("query should execute"); + + assert_eq!( + rows, + vec![TestUtilsUser { + id: 1, + name: "Ada".to_owned(), + }] + ); +} + +#[test] +fn test_context_run_query_decode_mismatch_returns_error() { + #[derive(Debug, spacetimedb::SpacetimeType)] + struct WrongRow { + id: u64, + name: u64, + } + + let test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + test.db.test_utils_user().insert(TestUtilsUser { + id: 1, + name: "Ada".to_owned(), + }); + + let query = spacetimedb::RawQuery::::new(r#"SELECT * FROM "test_utils_user""#.to_owned()); + let err = test.run_query(query).unwrap_err(); + + assert!(matches!(err, TestQueryError::Decode(_))); +} + +#[test] +fn test_context_run_query_sees_reducer_committed_state() { + let test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + test.with_reducer_tx::<_, ()>(TestAuth::internal(), |ctx| { + ctx.db.test_utils_user().insert(TestUtilsUser { + id: 3, + name: "Reducer committed".to_owned(), + }); + Ok(()) + }) + .expect("transaction should commit"); + + let rows: Vec = test + .run_query(spacetimedb::QueryBuilder {}.test_utils_user().build()) + .expect("query should execute"); + + assert_eq!( + rows, + vec![TestUtilsUser { + id: 3, + name: "Reducer committed".to_owned(), + }] + ); +} + +#[test] +fn test_context_anonymous_view_context_uses_test_backed_db() { + let test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + test.db.test_utils_user().insert(TestUtilsUser { + id: 4, + name: "View row".to_owned(), + }); + + let ctx = test.anonymous_view_context(); + assert_eq!( + test_utils_user_by_id_view(&ctx, 4), + Some(TestUtilsUser { + id: 4, + name: "View row".to_owned(), + }) + ); +} + +#[test] +fn test_context_view_context_derives_sender_from_auth() { + let test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + let payload = r#"{"iss":"view-issuer","sub":"view-subject"}"#; + let expected_sender = spacetimedb::Identity::from_claims("view-issuer", "view-subject"); + let connection_id = spacetimedb::ConnectionId::from_u128(8); + + let ctx = test.view_context( + TestAuth::from_jwt_payload(payload, connection_id).expect("JWT payload should be valid for tests"), + ); + + assert_eq!(test_utils_sender_view(&ctx), expected_sender); +} + +#[test] +fn with_reducer_tx_uses_test_clock_and_internal_auth() { + let mut test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + test.identity = spacetimedb::Identity::from_claims("module-issuer", "module-subject"); + let timestamp = Timestamp::from_micros_since_unix_epoch(42); + test.clock.set(timestamp); + + test.with_reducer_tx::<_, ()>(TestAuth::internal(), |ctx| { + assert_eq!(ctx.timestamp, timestamp); + assert_eq!(ctx.identity(), test.identity); + assert_eq!(ctx.sender(), test.identity); + assert_eq!(ctx.connection_id(), None); + assert!(ctx.sender_auth().is_internal()); + Ok(()) + }) + .expect("transaction should commit"); +} + +#[test] +fn with_reducer_tx_derives_sender_from_jwt_auth() { + let test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + let payload = r#"{"iss":"issuer","sub":"subject"}"#; + let expected_sender = spacetimedb::Identity::from_claims("issuer", "subject"); + let connection_id = spacetimedb::ConnectionId::from_u128(7); + + test.with_reducer_tx::<_, ()>( + TestAuth::from_jwt_payload(payload, connection_id).expect("JWT payload should be valid for tests"), + |ctx| { + assert_eq!(ctx.sender(), expected_sender); + assert_eq!(ctx.identity(), test.identity); + assert_eq!(ctx.connection_id(), Some(connection_id)); + assert!(!ctx.sender_auth().is_internal()); + assert_eq!(ctx.sender_auth().jwt().unwrap().identity(), expected_sender); + Ok(()) + }, + ) + .expect("transaction should commit"); +} + +#[test] +fn test_auth_rejects_invalid_jwt_payload() { + let connection_id = spacetimedb::ConnectionId::from_u128(7); + + assert!(TestAuth::from_jwt_payload(r#"{"iss":"issuer","sub":"subject"}"#, connection_id).is_ok()); + assert!(TestAuth::from_jwt_payload(r#"{"sub":"subject"}"#, connection_id).is_err()); + assert!(TestAuth::from_jwt_payload(r#"{"iss":"","sub":"subject","iat":0}"#, connection_id).is_err()); + + let mismatched_identity = spacetimedb::Identity::ONE; + let payload = format!(r#"{{"hex_identity":"{mismatched_identity}","iss":"issuer","sub":"subject","iat":0}}"#); + assert!(TestAuth::from_jwt_payload(payload, connection_id).is_err()); +} + +#[cfg(feature = "rand08")] +#[test] +fn with_reducer_tx_draws_distinct_seeds_from_test_rng() { + let test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + test.rng.set_seed(123); + + let first = test + .with_reducer_tx::<_, ()>(TestAuth::internal(), |ctx| { + Ok((ctx.random::(), ctx.random::())) + }) + .expect("transaction should commit"); + let second = test + .with_reducer_tx::<_, ()>(TestAuth::internal(), |ctx| { + Ok((ctx.random::(), ctx.random::())) + }) + .expect("transaction should commit"); + + assert_ne!(first, second); + + test.rng.set_seed(123); + let replayed_first = test + .with_reducer_tx::<_, ()>(TestAuth::internal(), |ctx| { + Ok((ctx.random::(), ctx.random::())) + }) + .expect("transaction should commit"); + + assert_eq!(first, replayed_first); +} + +#[cfg(feature = "rand08")] +#[test] +fn with_reducer_tx_rng_defaults_to_root_seed_zero() { + let first_test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + let first = first_test + .with_reducer_tx::<_, ()>(TestAuth::internal(), |ctx| { + Ok((ctx.random::(), ctx.random::())) + }) + .expect("transaction should commit"); + let second = first_test + .with_reducer_tx::<_, ()>(TestAuth::internal(), |ctx| { + Ok((ctx.random::(), ctx.random::())) + }) + .expect("transaction should commit"); + + assert_ne!(first, second); + + let second_test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + let replayed_first = second_test + .with_reducer_tx::<_, ()>(TestAuth::internal(), |ctx| { + Ok((ctx.random::(), ctx.random::())) + }) + .expect("transaction should commit"); + + assert_eq!(first, replayed_first); + + first_test.rng.set_seed(789); + let seeded = first_test + .with_reducer_tx::<_, ()>(TestAuth::internal(), |ctx| { + Ok((ctx.random::(), ctx.random::())) + }) + .expect("transaction should commit"); + first_test.rng.clear_seed(); + let default_seeded = first_test + .with_reducer_tx::<_, ()>(TestAuth::internal(), |ctx| { + Ok((ctx.random::(), ctx.random::())) + }) + .expect("transaction should commit"); + + assert_ne!(seeded, default_seeded); + assert_eq!(first, default_seeded); +} + +#[test] +fn with_reducer_tx_uses_test_backed_db() { + let test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + test.with_reducer_tx::<_, ()>(TestAuth::internal(), |ctx| { + ctx.db.test_utils_user().insert(TestUtilsUser { + id: 10, + name: "Grace".to_owned(), + }); + Ok(()) + }) + .expect("transaction should commit"); + + assert_eq!(test.db.test_utils_user().count(), 1); + assert_eq!( + test.db.test_utils_user().iter().collect::>(), + vec![TestUtilsUser { + id: 10, + name: "Grace".to_owned(), + }] + ); +} + +#[test] +fn with_reducer_tx_commits_on_ok() { + let test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + + test.with_reducer_tx::<_, ()>(TestAuth::internal(), |ctx| { + ctx.db.test_utils_user().insert(TestUtilsUser { + id: 12, + name: "Committed".to_owned(), + }); + assert_eq!(ctx.db.test_utils_user().count(), 1); + Ok(()) + }) + .expect("transaction should commit"); + + assert_eq!( + test.db.test_utils_user().iter().collect::>(), + vec![TestUtilsUser { + id: 12, + name: "Committed".to_owned(), + }] + ); +} + +#[test] +fn with_reducer_tx_rolls_back_on_err() { + let test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + + let res: Result<(), &'static str> = test.with_reducer_tx(TestAuth::internal(), |ctx| { + ctx.db.test_utils_user().insert(TestUtilsUser { + id: 13, + name: "Rolled back".to_owned(), + }); + assert_eq!(ctx.db.test_utils_user().count(), 1); + Err("rollback") + }); + + assert_eq!(res, Err("rollback")); + assert_eq!(test.db.test_utils_user().count(), 0); +} + +#[test] +fn with_reducer_tx_rolls_back_on_panic() { + let test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + + let panic = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + let _: Result<(), ()> = test.with_reducer_tx(TestAuth::internal(), |ctx| { + ctx.db.test_utils_user().insert(TestUtilsUser { + id: 14, + name: "Panicked".to_owned(), + }); + assert_eq!(ctx.db.test_utils_user().count(), 1); + panic!("force rollback"); + }); + })); + + assert!(panic.is_err()); + assert_eq!(test.db.test_utils_user().count(), 0); +} + +#[test] +fn with_reducer_tx_event_table_rows_are_transaction_scoped() { + let test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + + test.with_reducer_tx::<_, ()>(TestAuth::internal(), |ctx| { + ctx.db.test_utils_event().insert(TestUtilsEvent { + id: 1, + message: "event".to_owned(), + }); + + assert_eq!(ctx.db.test_utils_event().count(), 1); + assert_eq!( + ctx.db.test_utils_event().iter().collect::>(), + vec![TestUtilsEvent { + id: 1, + message: "event".to_owned(), + }] + ); + assert_eq!( + ctx.db.test_utils_event().id().find(1), + Some(TestUtilsEvent { + id: 1, + message: "event".to_owned(), + }) + ); + assert_eq!( + ctx.db.test_utils_event().message().filter("event").collect::>(), + vec![TestUtilsEvent { + id: 1, + message: "event".to_owned(), + }] + ); + Ok(()) + }) + .expect("transaction should commit"); + + let table_id = test.datastore().table_id("test_utils_event").unwrap(); + assert_eq!(test.datastore().table_row_count(table_id).unwrap(), 0); + assert!(test.datastore().table_rows(table_id).unwrap().is_empty()); +} + +#[test] +fn with_reducer_tx_event_table_rows_are_isolated_between_transactions() { + let test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + + test.with_reducer_tx::<_, ()>(TestAuth::internal(), |ctx| { + ctx.db.test_utils_event().insert(TestUtilsEvent { + id: 1, + message: "first".to_owned(), + }); + assert_eq!( + ctx.db.test_utils_event().iter().collect::>(), + vec![TestUtilsEvent { + id: 1, + message: "first".to_owned(), + }] + ); + Ok(()) + }) + .expect("first transaction should commit"); + + test.with_reducer_tx::<_, ()>(TestAuth::internal(), |ctx| { + assert_eq!(ctx.db.test_utils_event().count(), 0); + ctx.db.test_utils_event().insert(TestUtilsEvent { + id: 2, + message: "second".to_owned(), + }); + assert_eq!( + ctx.db.test_utils_event().iter().collect::>(), + vec![TestUtilsEvent { + id: 2, + message: "second".to_owned(), + }] + ); + Ok(()) + }) + .expect("second transaction should commit"); +} + +#[test] +fn with_reducer_tx_event_table_constraints_are_transaction_scoped() { + let test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + + test.with_reducer_tx::<_, ()>(TestAuth::internal(), |ctx| { + ctx.db.test_utils_event().insert(TestUtilsEvent { + id: 1, + message: "first".to_owned(), + }); + assert!(ctx + .db + .test_utils_event() + .try_insert(TestUtilsEvent { + id: 1, + message: "duplicate".to_owned(), + }) + .is_err()); + Ok(()) + }) + .expect("first transaction should commit"); + + test.with_reducer_tx::<_, ()>(TestAuth::internal(), |ctx| { + ctx.db.test_utils_event().insert(TestUtilsEvent { + id: 1, + message: "second".to_owned(), + }); + assert_eq!( + ctx.db.test_utils_event().id().find(1), + Some(TestUtilsEvent { + id: 1, + message: "second".to_owned(), + }) + ); + Ok(()) + }) + .expect("second transaction should commit"); +} + +#[test] +fn event_table_access_outside_reducer_transaction_panics() { + let test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + + let insert = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + test.db.test_utils_event().insert(TestUtilsEvent { + id: 1, + message: "outside".to_owned(), + }); + })); + assert!(insert.is_err()); + + let count = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + test.db.test_utils_event().count(); + })); + assert!(count.is_err()); + + let iter = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + let _ = test.db.test_utils_event().iter().collect::>(); + })); + assert!(iter.is_err()); +} + +#[test] +fn test_context_supports_unique_index_find_update_and_delete() { + let test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + + test.db.test_utils_user().insert(TestUtilsUser { + id: 11, + name: "Alice".to_owned(), + }); + + assert_eq!( + test.db.test_utils_user().id().find(11), + Some(TestUtilsUser { + id: 11, + name: "Alice".to_owned(), + }) + ); + + assert_eq!( + test.db.test_utils_user().id().update(TestUtilsUser { + id: 11, + name: "Alice2".to_owned(), + }), + TestUtilsUser { + id: 11, + name: "Alice2".to_owned(), + } + ); + + assert_eq!( + test.db.test_utils_user().iter().collect::>(), + vec![TestUtilsUser { + id: 11, + name: "Alice2".to_owned(), + }] + ); + + assert!(test.db.test_utils_user().id().delete(11)); + assert_eq!(test.db.test_utils_user().count(), 0); +} + +#[cfg(feature = "unstable")] +#[test] +fn procedure_context_try_with_tx_commits() { + let test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + let mut procedure = test.procedure_context(TestAuth::internal()); + + procedure + .try_with_tx::<_, ()>(|tx| { + tx.db.test_utils_user().insert(TestUtilsUser { + id: 21, + name: "Committed".to_owned(), + }); + assert_eq!(tx.db.test_utils_user().count(), 1); + Ok(()) + }) + .expect("transaction should commit"); + + assert_eq!( + test.db.test_utils_user().iter().collect::>(), + vec![TestUtilsUser { + id: 21, + name: "Committed".to_owned(), + }] + ); +} + +#[cfg(feature = "unstable")] +#[test] +fn procedure_context_try_with_tx_rolls_back_on_err() { + let test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + let mut procedure = test.procedure_context(TestAuth::internal()); + + let res: Result<(), &'static str> = procedure.try_with_tx(|tx| { + tx.db.test_utils_user().insert(TestUtilsUser { + id: 22, + name: "Rolled back".to_owned(), + }); + assert_eq!(tx.db.test_utils_user().count(), 1); + Err("rollback") + }); + + assert_eq!(res, Err("rollback")); + assert_eq!(test.db.test_utils_user().count(), 0); +} + +#[cfg(feature = "unstable")] +#[test] +fn procedure_context_transactions_can_be_interleaved() { + let test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + let mut procedure = test.procedure_context(TestAuth::internal()); + + procedure + .try_with_tx::<_, ()>(|tx| { + tx.db.test_utils_user().insert(TestUtilsUser { + id: 23, + name: "First tx".to_owned(), + }); + Ok(()) + }) + .expect("first transaction should commit"); + + test.db.test_utils_user().insert(TestUtilsUser { + id: 24, + name: "Between txs".to_owned(), + }); + + procedure + .try_with_tx::<_, ()>(|tx| { + assert_eq!(tx.db.test_utils_user().count(), 2); + tx.db.test_utils_user().id().update(TestUtilsUser { + id: 23, + name: "Second tx".to_owned(), + }); + Ok(()) + }) + .expect("second transaction should commit"); + + let mut rows = test.db.test_utils_user().iter().collect::>(); + rows.sort_by_key(|row| row.id); + assert_eq!( + rows, + vec![ + TestUtilsUser { + id: 23, + name: "Second tx".to_owned(), + }, + TestUtilsUser { + id: 24, + name: "Between txs".to_owned(), + }, + ] + ); +} + +#[cfg(all(feature = "unstable", feature = "rand08"))] +#[test] +fn procedure_context_transactions_draw_distinct_child_rng_seeds() { + let test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + test.rng.set_seed(123); + let mut procedure = test.procedure_context(TestAuth::internal()); + + let first = procedure + .try_with_tx::<_, ()>(|tx| Ok((tx.random::(), tx.random::()))) + .expect("first transaction should commit"); + let second = procedure + .try_with_tx::<_, ()>(|tx| Ok((tx.random::(), tx.random::()))) + .expect("second transaction should commit"); + + assert_ne!(first, second); + + test.rng.set_seed(123); + let mut replayed_procedure = test.procedure_context(TestAuth::internal()); + let replayed_first = replayed_procedure + .try_with_tx::<_, ()>(|tx| Ok((tx.random::(), tx.random::()))) + .expect("replayed first transaction should commit"); + + assert_eq!(first, replayed_first); +} + +#[cfg(feature = "unstable")] +#[test] +fn procedure_context_after_tx_commit_hook_can_interleave_reducer() { + fn procedure_with_two_transactions(ctx: &mut spacetimedb::ProcedureContext) -> Result<(), ()> { + ctx.try_with_tx::<_, ()>(|tx| { + tx.db.test_utils_user().insert(TestUtilsUser { + id: 25, + name: "First procedure tx".to_owned(), + }); + Ok(()) + })?; + + ctx.try_with_tx::<_, ()>(|tx| { + assert_eq!(tx.db.test_utils_user().count(), 2); + tx.db.test_utils_user().id().update(TestUtilsUser { + id: 25, + name: "Second procedure tx".to_owned(), + }); + Ok(()) + })?; + + Ok(()) + } + + let test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + let ran = std::rc::Rc::new(std::cell::Cell::new(false)); + let hooks = spacetimedb::test_utils::ProcedureTestHooks::new().after_tx_commit({ + let ran = ran.clone(); + move |hook_ctx| { + if ran.replace(true) { + return Ok(()); + } + + hook_ctx + .with_reducer_tx(TestAuth::internal(), |ctx| { + ctx.db.test_utils_user().insert(TestUtilsUser { + id: 26, + name: "Interleaved reducer".to_owned(), + }); + Ok::<_, ()>(()) + }) + .expect("interleaved reducer should commit"); + Ok(()) + } + }); + let mut procedure = test + .procedure_context_builder(TestAuth::internal()) + .hooks(hooks) + .build(); + + procedure_with_two_transactions(&mut procedure).expect("procedure should succeed"); + + let mut rows = test.db.test_utils_user().iter().collect::>(); + rows.sort_by_key(|row| row.id); + assert_eq!( + rows, + vec![ + TestUtilsUser { + id: 25, + name: "Second procedure tx".to_owned(), + }, + TestUtilsUser { + id: 26, + name: "Interleaved reducer".to_owned(), + }, + ] + ); +} + +#[cfg(feature = "unstable")] +#[test] +fn procedure_context_uses_test_http_responder() { + let test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + let seen_request = std::rc::Rc::new(std::cell::RefCell::new(None)); + + let procedure = test + .procedure_context_builder(TestAuth::internal()) + .http({ + let seen_request = seen_request.clone(); + move |_test, request| { + seen_request.replace(Some((request.method().as_str().to_owned(), request.uri().to_string()))); + Ok(spacetimedb::http::Response::builder() + .status(201) + .header("x-test", "yes") + .body(spacetimedb::http::Body::from("created")) + .expect("test response should be valid")) + } + }) + .build(); + let response = procedure + .http + .get("https://example.invalid/create") + .expect("test HTTP responder should return a response"); + + assert_eq!(response.status(), 201); + assert_eq!(response.headers().get("x-test").unwrap(), "yes"); + assert_eq!(response.into_body().into_string().unwrap(), "created"); + assert_eq!( + seen_request.borrow().as_ref(), + Some(&("GET".to_owned(), "https://example.invalid/create".to_owned())) + ); +} + +#[cfg(feature = "unstable")] +#[test] +fn procedure_context_http_responder_can_interleave_reducer() { + let test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + + let procedure = test + .procedure_context_builder(TestAuth::internal()) + .http(|test, _request| { + test.with_reducer_tx(TestAuth::internal(), |ctx| { + ctx.db.test_utils_user().insert(TestUtilsUser { + id: 27, + name: "HTTP interleaved reducer".to_owned(), + }); + Ok::<_, ()>(()) + }) + .expect("interleaved reducer should commit"); + + Ok(spacetimedb::http::Response::builder() + .status(200) + .body(spacetimedb::http::Body::empty()) + .expect("test response should be valid")) + }) + .build(); + procedure + .http + .get("https://example.invalid/interleave") + .expect("test HTTP responder should return a response"); + + assert_eq!( + test.db.test_utils_user().iter().collect::>(), + vec![TestUtilsUser { + id: 27, + name: "HTTP interleaved reducer".to_owned(), + }] + ); +} + +#[cfg(feature = "unstable")] +#[test] +fn procedure_context_sleep_advances_test_clock() { + let test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + let start = Timestamp::from_micros_since_unix_epoch(100); + let wake_time = Timestamp::from_micros_since_unix_epoch(500); + test.clock.set(start); + let mut procedure = test.procedure_context(TestAuth::internal()); + + procedure.sleep_until(wake_time); + + assert_eq!(procedure.timestamp, wake_time); + assert_eq!(test.clock.now(), wake_time); +} + +#[cfg(feature = "unstable")] +#[test] +fn procedure_context_on_sleep_hook_can_interleave_reducer() { + let test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + let wake_time = Timestamp::from_micros_since_unix_epoch(500); + let seen_wake_time = std::rc::Rc::new(std::cell::Cell::new(None)); + let hooks = spacetimedb::test_utils::ProcedureTestHooks::new().on_sleep({ + let seen_wake_time = seen_wake_time.clone(); + move |test, wake_time| { + seen_wake_time.set(Some(wake_time)); + test.with_reducer_tx(TestAuth::internal(), |ctx| { + ctx.db.test_utils_user().insert(TestUtilsUser { + id: 28, + name: "Sleep interleaved reducer".to_owned(), + }); + Ok::<_, ()>(()) + }) + .expect("interleaved reducer should commit"); + Ok(()) + } + }); + let mut procedure = test + .procedure_context_builder(TestAuth::internal()) + .hooks(hooks) + .build(); + + procedure.sleep_until(wake_time); + + assert_eq!(seen_wake_time.get(), Some(wake_time)); + assert_eq!(procedure.timestamp, wake_time); + assert_eq!( + test.db.test_utils_user().iter().collect::>(), + vec![TestUtilsUser { + id: 28, + name: "Sleep interleaved reducer".to_owned(), + }] + ); +} + +#[cfg(feature = "unstable")] +#[test] +fn procedure_context_sleep_does_not_move_clock_back_after_hook() { + let test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + let wake_time = Timestamp::from_micros_since_unix_epoch(500); + let later_time = Timestamp::from_micros_since_unix_epoch(900); + let hooks = spacetimedb::test_utils::ProcedureTestHooks::new().on_sleep(move |test, _wake_time| { + test.clock.set(later_time); + Ok(()) + }); + let mut procedure = test + .procedure_context_builder(TestAuth::internal()) + .hooks(hooks) + .build(); + + procedure.sleep_until(wake_time); + + assert_eq!(procedure.timestamp, later_time); + assert_eq!(test.clock.now(), later_time); +} diff --git a/crates/bindings/tests/ui/views.stderr b/crates/bindings/tests/ui/views.stderr index 2b0c0dd6033..cb4372091aa 100644 --- a/crates/bindings/tests/ui/views.stderr +++ b/crates/bindings/tests/ui/views.stderr @@ -101,34 +101,56 @@ error[E0276]: impl has stricter requirements than trait | = note: this error originates in the attribute macro `view` (in Nightly builds, run with -Z macro-backtrace for more info) -error[E0599]: no method named `iter` found for reference `&test__ViewHandle` in the current scope +error[E0599]: no method named `iter` found for struct `test__ViewHandle` in the current scope --> tests/ui/views.rs:17:34 | + 5 | #[table(accessor = test)] + | ----------------------- method `iter` not found for this struct +... 17 | for _ in read_only.db.test().iter() {} - | ^^^^ method not found in `&test__ViewHandle` + | ^^^^ method not found in `test__ViewHandle` | = help: items from traits can only be used if the trait is implemented and in scope = note: the following traits define an item `iter`, perhaps you need to implement one of them: candidate #1: `bitflags::traits::Flags` - candidate #2: `spacetimedb::Table` - -error[E0599]: no method named `insert` found for reference `&test__ViewHandle` in the current scope + candidate #2: `icu_provider::baked::DataStore` + candidate #3: `petgraph::visit::traversal::Walker` + candidate #4: `spacetimedb::Table` + candidate #5: `spacetimedb_datastore::locking_tx_datastore::state_view::StateView` + candidate #6: `spacetimedb_table::table_index::index::Index` + candidate #7: `strum::IntoEnumIterator` + candidate #8: `strum::VariantIterator` + candidate #9: `toml_edit::table::TableLike` + +error[E0599]: no method named `insert` found for struct `test__ViewHandle` in the current scope --> tests/ui/views.rs:24:25 | + 5 | #[table(accessor = test)] + | ----------------------- method `insert` not found for this struct +... 24 | read_only.db.test().insert(Test { id: 0, x: 0 }); - | ^^^^^^ method not found in `&test__ViewHandle` + | ^^^^^^ method not found in `test__ViewHandle` | = help: items from traits can only be used if the trait is implemented and in scope = note: the following traits define an item `insert`, perhaps you need to implement one of them: candidate #1: `bitflags::traits::Flags` candidate #2: `ppv_lite86::types::Vec2` candidate #3: `ppv_lite86::types::Vec4` - candidate #4: `similar::algorithms::DiffHook` - candidate #5: `spacetimedb::Table` - -error[E0599]: no method named `try_insert` found for reference `&test__ViewHandle` in the current scope + candidate #4: `serde_with::duplicate_key_impls::error_on_duplicate::PreventDuplicateInsertsMap` + candidate #5: `serde_with::duplicate_key_impls::error_on_duplicate::PreventDuplicateInsertsSet` + candidate #6: `serde_with::duplicate_key_impls::first_value_wins::DuplicateInsertsFirstWinsMap` + candidate #7: `similar::algorithms::DiffHook` + candidate #8: `spacetimedb::Table` + candidate #9: `spacetimedb_table::table_index::index::Index` + candidate #10: `toml_edit::table::TableLike` + candidate #11: `wasmtime_environ::compile::CacheStore` + +error[E0599]: no method named `try_insert` found for struct `test__ViewHandle` in the current scope --> tests/ui/views.rs:31:25 | + 5 | #[table(accessor = test)] + | ----------------------- method `try_insert` not found for this struct +... 31 | read_only.db.test().try_insert(Test { id: 0, x: 0 }); | ^^^^^^^^^^ | @@ -142,16 +164,20 @@ help: there is a method `try_into` with a similar name, but with different argum | fn try_into(self) -> Result; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -error[E0599]: no method named `delete` found for reference `&test__ViewHandle` in the current scope +error[E0599]: no method named `delete` found for struct `test__ViewHandle` in the current scope --> tests/ui/views.rs:38:25 | + 5 | #[table(accessor = test)] + | ----------------------- method `delete` not found for this struct +... 38 | read_only.db.test().delete(Test { id: 0, x: 0 }); - | ^^^^^^ method not found in `&test__ViewHandle` + | ^^^^^^ method not found in `test__ViewHandle` | = help: items from traits can only be used if the trait is implemented and in scope = note: the following traits define an item `delete`, perhaps you need to implement one of them: candidate #1: `similar::algorithms::DiffHook` candidate #2: `spacetimedb::Table` + candidate #3: `spacetimedb_table::table_index::index::Index` error[E0599]: no method named `delete` found for struct `UniqueColumnReadOnly` in the current scope --> tests/ui/views.rs:45:30 diff --git a/crates/datastore/Cargo.toml b/crates/datastore/Cargo.toml index 86cb2aca0b6..b6cde25a91a 100644 --- a/crates/datastore/Cargo.toml +++ b/crates/datastore/Cargo.toml @@ -8,17 +8,17 @@ rust-version.workspace = true [dependencies] spacetimedb-data-structures.workspace = true -spacetimedb-lib = { workspace = true, features = ["serde", "metrics_impls"] } -spacetimedb-commitlog.workspace = true -spacetimedb-durability.workspace = true -spacetimedb-metrics.workspace = true +spacetimedb-lib = { workspace = true, features = ["serde"] } +spacetimedb-commitlog = { workspace = true, optional = true } +spacetimedb-durability = { workspace = true, optional = true } +spacetimedb-metrics = { workspace = true, optional = true } spacetimedb-primitives.workspace = true -spacetimedb-paths.workspace = true +spacetimedb-paths = { workspace = true, optional = true } spacetimedb-sats = { workspace = true, features = ["serde"] } spacetimedb-schema.workspace = true spacetimedb-table.workspace = true -spacetimedb-snapshot.workspace = true -spacetimedb-execution.workspace = true +spacetimedb-snapshot = { workspace = true, optional = true } +spacetimedb-execution = { workspace = true, optional = true } anyhow = { workspace = true, features = ["backtrace"] } bytes.workspace = true @@ -30,7 +30,7 @@ lazy_static.workspace = true log.workspace = true once_cell.workspace = true parking_lot.workspace = true -prometheus.workspace = true +prometheus = { workspace = true, optional = true } smallvec.workspace = true strum.workspace = true thiserror.workspace = true @@ -39,7 +39,17 @@ thin-vec.workspace = true [features] # Print a warning when doing an unindexed `iter_by_col_range` on a large table. unindexed_iter_by_col_range_warn = [] -default = ["unindexed_iter_by_col_range_warn"] +metrics = ["dep:prometheus", "dep:spacetimedb-metrics", "spacetimedb-lib/metrics_impls"] +durability = [ + "dep:spacetimedb-commitlog", + "dep:spacetimedb-durability", + "dep:spacetimedb-paths", + "dep:spacetimedb-snapshot", + "metrics", +] +execution = ["dep:spacetimedb-execution"] +portable = [] +default = ["durability", "execution", "unindexed_iter_by_col_range_warn"] # Enable test helpers and utils test = ["spacetimedb-commitlog/test", "spacetimedb-schema/test"] diff --git a/crates/datastore/src/error.rs b/crates/datastore/src/error.rs index 596c0ca26de..bb56267e366 100644 --- a/crates/datastore/src/error.rs +++ b/crates/datastore/src/error.rs @@ -6,6 +6,7 @@ use spacetimedb_sats::product_value::InvalidFieldError; use spacetimedb_sats::raw_identifier::RawIdentifier; use spacetimedb_sats::{AlgebraicType, AlgebraicValue}; use spacetimedb_schema::def::error::LibError; +#[cfg(feature = "durability")] use spacetimedb_snapshot::SnapshotError; use spacetimedb_table::{ bflatn_to, read_column, @@ -25,6 +26,7 @@ pub enum DatastoreError { Sequence(#[from] SequenceError), #[error(transparent)] // Box the inner [`SnapshotError`] to keep Clippy quiet about large `Err` variants. + #[cfg(feature = "durability")] Snapshot(#[from] Box), // TODO(cloutiertyler): should this be a TableError? I couldn't get it to compile #[error("Error reading a value from a table through BSATN: {0}")] @@ -162,6 +164,7 @@ impl From for DatastoreError { } } +#[cfg(feature = "durability")] impl From for DatastoreError { fn from(e: SnapshotError) -> Self { DatastoreError::Snapshot(Box::new(e)) diff --git a/crates/datastore/src/execution_context.rs b/crates/datastore/src/execution_context.rs index f2e24a5876e..afe8e5fcc96 100644 --- a/crates/datastore/src/execution_context.rs +++ b/crates/datastore/src/execution_context.rs @@ -1,11 +1,16 @@ +#[cfg(feature = "durability")] use std::sync::Arc; use bytes::Bytes; use derive_more::Display; +#[cfg(feature = "durability")] use spacetimedb_commitlog::{payload::txdata, Varchar}; use spacetimedb_lib::{ConnectionId, Identity, Timestamp}; +#[cfg(feature = "durability")] use spacetimedb_sats::{bsatn, raw_identifier::RawIdentifier}; -use spacetimedb_schema::{identifier::Identifier, reducer_name::ReducerName}; +#[cfg(feature = "durability")] +use spacetimedb_schema::identifier::Identifier; +use spacetimedb_schema::reducer_name::ReducerName; /// Represents the context under which a database runtime method is executed. /// In particular it provides details about the currently executing txn to runtime operations. @@ -40,6 +45,7 @@ pub struct ReducerContext { pub arg_bsatn: Bytes, } +#[cfg(feature = "durability")] impl From for txdata::Inputs { fn from( ReducerContext { @@ -71,6 +77,7 @@ impl From for txdata::Inputs { } } +#[cfg(feature = "durability")] impl TryFrom<&txdata::Inputs> for ReducerContext { type Error = bsatn::DecodeError; @@ -111,6 +118,7 @@ pub enum Workload { impl Workload { /// Returns a reducer workload with no arguments to the reducer /// and the current timestamp. + #[allow(deprecated)] pub fn reducer_no_args(name: ReducerName, id: Identity, conn_id: ConnectionId) -> Self { Self::Reducer(ReducerContext { name, diff --git a/crates/datastore/src/lib.rs b/crates/datastore/src/lib.rs index c266b2559ee..d2650575e91 100644 --- a/crates/datastore/src/lib.rs +++ b/crates/datastore/src/lib.rs @@ -1,3 +1,4 @@ +#[cfg(feature = "metrics")] pub mod db_metrics; pub mod error; pub mod execution_context; diff --git a/crates/datastore/src/locking_tx_datastore/committed_state.rs b/crates/datastore/src/locking_tx_datastore/committed_state.rs index 46e2e60131f..4653eee620a 100644 --- a/crates/datastore/src/locking_tx_datastore/committed_state.rs +++ b/crates/datastore/src/locking_tx_datastore/committed_state.rs @@ -1,3 +1,5 @@ +#![cfg_attr(not(feature = "metrics"), allow(dead_code, unused_imports, unused_variables))] + use super::{ datastore::Result, delete_table::DeleteTable, @@ -6,8 +8,10 @@ use super::{ tx_state::{IndexIdMap, PendingSchemaChange, TxState}, IterByColEqTx, }; +#[cfg(feature = "metrics")] +use crate::db_metrics::DB_METRICS; +use crate::traits::TxOffset; use crate::{ - db_metrics::DB_METRICS, error::TableError, execution_context::ExecutionContext, locking_tx_datastore::{ @@ -37,7 +41,6 @@ use crate::{ use anyhow::anyhow; use core::{convert::Infallible, ops::RangeBounds}; use spacetimedb_data_structures::map::{HashMap, HashSet, IntMap, IntSet}; -use spacetimedb_durability::TxOffset; use spacetimedb_lib::{db::auth::StTableType, Identity}; use spacetimedb_primitives::{ColList, IndexId, TableId}; use spacetimedb_sats::memory_usage::MemoryUsage; @@ -252,6 +255,7 @@ impl CommittedState { pub(super) fn bootstrap_system_tables(&mut self, database_identity: Identity) -> Result<()> { // NOTE: the `rdb_num_table_rows` metric is used by the query optimizer, // and therefore has performance implications and must not be disabled. + #[cfg(feature = "metrics")] let with_label_values = |table_id: TableId, table_name: &str| { DB_METRICS .rdb_num_table_rows @@ -268,6 +272,7 @@ impl CommittedState { for schema in ref_schemas { let table_id = schema.table_id; // Metric for this system table. + #[cfg(feature = "metrics")] with_label_values(table_id, &schema.table_name).set(0); let row = StTableRow { @@ -296,6 +301,7 @@ impl CommittedState { // Insert the meta-row into the in-memory ST_COLUMNS. st_columns.insert(pool, blob_store, &row)?; // Increment row count for st_columns. + #[cfg(feature = "metrics")] with_label_values(ST_COLUMN_ID, ST_COLUMN_NAME).inc(); } @@ -315,6 +321,7 @@ impl CommittedState { // Insert the meta-row into the in-memory ST_CONSTRAINTS. st_constraints.insert(pool, blob_store, &row)?; // Increment row count for st_constraints. + #[cfg(feature = "metrics")] with_label_values(ST_CONSTRAINT_ID, ST_CONSTRAINT_NAME).inc(); } @@ -328,6 +335,7 @@ impl CommittedState { // Insert the meta-row into the in-memory ST_INDEXES. st_indexes.insert(pool, blob_store, &row)?; // Increment row count for st_indexes. + #[cfg(feature = "metrics")] with_label_values(ST_INDEX_ID, ST_INDEX_NAME).inc(); } @@ -382,6 +390,7 @@ impl CommittedState { // Insert the meta-row into the in-memory ST_SEQUENCES. st_sequences.insert(pool, blob_store, &row)?; // Increment row count for st_sequences + #[cfg(feature = "metrics")] with_label_values(ST_SEQUENCE_ID, ST_SEQUENCE_NAME).inc(); } @@ -978,6 +987,7 @@ impl CommittedState { ) } + #[cfg(feature = "metrics")] pub fn report_data_size(&self, database_identity: Identity) { use crate::db_metrics::data_size::DATA_SIZE_METRICS; diff --git a/crates/datastore/src/locking_tx_datastore/datastore.rs b/crates/datastore/src/locking_tx_datastore/datastore.rs index ffc662f3ce5..b26a0f79d37 100644 --- a/crates/datastore/src/locking_tx_datastore/datastore.rs +++ b/crates/datastore/src/locking_tx_datastore/datastore.rs @@ -1,11 +1,18 @@ +#![cfg_attr(not(feature = "metrics"), allow(dead_code))] + use super::{ - committed_state::CommittedState, mut_tx::MutTxId, sequence::SequencesState, state_view::StateView, tx::TxId, - tx_state::TxState, + committed_state::CommittedState, mut_tx::MutTxId, sequence::SequencesState, state_view::StateView, time::Instant, + tx::TxId, tx_state::TxState, }; +#[cfg(feature = "metrics")] +use crate::db_metrics::DB_METRICS; use crate::execution_context::{Workload, WorkloadType}; +#[cfg(feature = "durability")] use crate::locking_tx_datastore::replay::{build_sequence_state, ErrorBehavior, Replay}; +#[cfg(feature = "durability")] +use crate::system_tables::system_table_schema; +use crate::traits::TxOffset; use crate::{ - db_metrics::DB_METRICS, error::{DatastoreError, TableError}, locking_tx_datastore::{ state_view::{IterByColEqMutTx, IterByColRangeMutTx, IterMutTx}, @@ -16,8 +23,8 @@ use crate::{ use crate::{ execution_context::ExecutionContext, system_tables::{ - read_hash_from_col, read_identity_from_col, system_table_schema, StClientRow, StModuleFields, StModuleRow, - StTableFields, ST_CLIENT_ID, ST_MODULE_ID, ST_TABLE_ID, + read_hash_from_col, read_identity_from_col, StClientRow, StModuleFields, StModuleRow, StTableFields, + ST_CLIENT_ID, ST_MODULE_ID, ST_TABLE_ID, }, traits::{ DataRow, IsolationLevel, Metadata, MutTx, MutTxDatastore, Program, RowTypeForTable, Tx, TxData, TxDatastore, @@ -27,7 +34,6 @@ use anyhow::anyhow; use core::ops::RangeBounds; use parking_lot::{Mutex, RwLock}; use spacetimedb_data_structures::map::{HashCollectionExt, HashMap}; -use spacetimedb_durability::TxOffset; use spacetimedb_lib::{db::auth::StAccess, metrics::ExecutionMetrics}; use spacetimedb_lib::{ConnectionId, Identity}; use spacetimedb_primitives::{ColId, ColList, ConstraintId, IndexId, SequenceId, TableId, ViewId}; @@ -38,6 +44,7 @@ use spacetimedb_schema::{ reducer_name::ReducerName, schema::{ColumnSchema, ConstraintSchema, IndexSchema, SequenceSchema, TableSchema}, }; +#[cfg(feature = "durability")] use spacetimedb_snapshot::{BoxedPendingSnapshot, DynSnapshotRepo, ReconstructedSnapshot}; use spacetimedb_table::{ indexes::RowPointer, @@ -46,7 +53,7 @@ use spacetimedb_table::{ }; use std::borrow::Cow; use std::sync::Arc; -use std::time::{Duration, Instant}; +use std::time::Duration; pub type Result = std::result::Result; @@ -116,7 +123,8 @@ impl Locking { commit_state.bootstrap_system_tables(database_identity)?; // The database tables are now initialized with the correct data. // Now we have to build our in memory structures. - build_sequence_state(&datastore, &mut commit_state)?; + let sequence_state = commit_state.build_sequence_state()?; + *datastore.sequence_state.lock() = sequence_state; // We don't want to build indexes here; we'll build those later, // in `rebuild_state_after_replay`. @@ -133,6 +141,7 @@ impl Locking { /// The provided closure will be called for each transaction found in the /// history, the parameter is the transaction's offset. The closure is called /// _before_ the transaction is applied to the database state. + #[cfg(feature = "durability")] pub fn replay(&self, progress: F, error_behavior: ErrorBehavior) -> Replay<'_, F> { let committed_state = self.committed_state.write(); Replay::new(self.database_identity, committed_state, progress, error_behavior) @@ -148,6 +157,7 @@ impl Locking { /// - Notably, **do not** construct indexes or sequences. /// This should be done by [`Self::rebuild_state_after_replay`], /// after replaying the suffix of the commitlog. + #[cfg(feature = "durability")] pub fn restore_from_snapshot(snapshot: ReconstructedSnapshot, page_pool: PagePool) -> Result { let ReconstructedSnapshot { database_identity, @@ -227,6 +237,7 @@ impl Locking { /// /// Returns an error if [`DynSnapshotRepo::create_snapshot`] returns an /// error. + #[cfg(feature = "durability")] pub fn take_snapshot(&self, repo: &DynSnapshotRepo) -> Result> { Self::take_snapshot_internal(&self.committed_state, repo)? .map(|(_offset, snap)| snap.sync_all()) @@ -253,6 +264,7 @@ impl Locking { self.committed_state.read().datastore_memory_bytes() } + #[cfg(feature = "durability")] pub fn take_snapshot_internal( committed_state: &RwLock, repo: &DynSnapshotRepo, @@ -779,6 +791,7 @@ impl TxMetrics { } /// Reports the metrics for `reducer` using `get_exec_counter` to retrieve the metrics counters. + #[cfg(feature = "metrics")] pub fn report<'a, R: MetricsRecorder + 'a>( &self, tx_data: Option<&TxData>, @@ -980,6 +993,14 @@ impl Locking { tx.rollback_downgrade(workload) } + #[cfg(feature = "portable")] + pub fn rebuild_sequence_state_from_committed(&self) -> Result<()> { + let mut committed_state = self.committed_state.write(); + let sequence_state = committed_state.build_sequence_state()?; + *self.sequence_state.lock() = sequence_state; + Ok(()) + } + /// This method only updates the in-memory `committed_state`. /// For durability, see `RelationalDB::commit_tx_downgrade`. pub fn commit_mut_tx_downgrade(&self, tx: MutTxId, workload: Workload) -> (TxData, TxMetrics, TxId) { diff --git a/crates/datastore/src/locking_tx_datastore/mod.rs b/crates/datastore/src/locking_tx_datastore/mod.rs index 2eebaf4e619..90c14daed6e 100644 --- a/crates/datastore/src/locking_tx_datastore/mod.rs +++ b/crates/datastore/src/locking_tx_datastore/mod.rs @@ -8,7 +8,10 @@ mod sequence; pub mod state_view; pub use state_view::{IterByColEqTx, IterByColRangeTx}; pub mod delete_table; +#[cfg(feature = "durability")] mod replay; +mod time; +#[cfg(feature = "durability")] pub use replay::{apply_history, ApplyHistoryCounters, ErrorBehavior, Replay}; mod tx; pub use tx::{NumDistinctValues, TxId}; diff --git a/crates/datastore/src/locking_tx_datastore/mut_tx.rs b/crates/datastore/src/locking_tx_datastore/mut_tx.rs index d230c40c7fe..11c0f3e5a95 100644 --- a/crates/datastore/src/locking_tx_datastore/mut_tx.rs +++ b/crates/datastore/src/locking_tx_datastore/mut_tx.rs @@ -4,10 +4,12 @@ use super::{ delete_table::DeleteTable, sequence::{Sequence, SequencesState}, state_view::{IterByColEqMutTx, IterByColRangeMutTx, IterMutTx, StateView}, + time::{now_timestamp, Instant}, tx::TxId, tx_state::{IndexIdMap, PendingSchemaChange, TxState, TxTableForInsertion}, SharedMutexGuard, SharedWriteGuard, }; +use crate::traits::TxOffset; use crate::{ error::ViewError, system_tables::{ @@ -38,7 +40,7 @@ use core::{cell::RefCell, iter, mem, ops::RangeBounds}; use itertools::Either; use smallvec::SmallVec; use spacetimedb_data_structures::map::{HashMap, HashSet, IntMap}; -use spacetimedb_durability::TxOffset; +#[cfg(feature = "execution")] use spacetimedb_execution::{dml::MutDatastore, Datastore, DeltaStore, Row}; use spacetimedb_lib::{ db::raw_def::v9::RawSql, @@ -74,11 +76,7 @@ use spacetimedb_table::{ }, table_index::{IndexCannotSeekRange, IndexKey, IndexSeekRangeResult, PointOrRange, TableIndex}, }; -use std::{ - marker::PhantomData, - sync::Arc, - time::{Duration, Instant}, -}; +use std::{marker::PhantomData, sync::Arc, time::Duration}; #[derive(Clone, Debug, Eq, PartialEq, Hash)] pub struct ViewCallInfo { @@ -613,6 +611,7 @@ impl MutTxId { } } +#[cfg(feature = "execution")] impl Datastore for MutTxId { type TableIter<'a> = IterMutTx<'a> @@ -676,6 +675,7 @@ impl Datastore for MutTxId { /// Note, deltas are evaluated using read-only transactions, not mutable ones. /// Nevertheless this contract is still required for query evaluation. +#[cfg(feature = "execution")] impl DeltaStore for MutTxId { fn num_inserts(&self, _: TableId) -> usize { 0 @@ -718,6 +718,7 @@ impl DeltaStore for MutTxId { } } +#[cfg(feature = "execution")] impl MutDatastore for MutTxId { fn insert_product_value(&mut self, table_id: TableId, row: &ProductValue) -> anyhow::Result { Ok(match self.insert_via_serialize_bsatn(table_id, row)?.1 { @@ -2849,7 +2850,7 @@ impl MutTxId { /// This is invoked when calling a view, but not subscribing to it. /// Such is the case for the sql http api. pub fn update_view_timestamp(&mut self, call: ViewCallInfo, args: ViewInstanceArgs) -> Result<()> { - self.update_view_timestamp_at(call, args, Timestamp::now()) + self.update_view_timestamp_at(call, args, now_timestamp()) } /// Updates the `last_used` timestamp for a materialized view argument to an explicit value. @@ -2870,12 +2871,13 @@ impl MutTxId { /// Increment this subscriber's refcount for a materialized view argument. pub fn subscribe_view(&mut self, call: ViewCallInfo, args: ViewInstanceArgs, subscriber: Identity) -> Result<()> { + let now = now_timestamp(); let mut state = self .get_view_instance_cloned(&call) - .unwrap_or_else(|| ViewInstanceState::new(args, Timestamp::now())); + .unwrap_or_else(|| ViewInstanceState::new(args, now)); state.args = args; *state.active_subscribers.entry(subscriber).or_default() += 1; - state.last_used = Timestamp::now(); + state.last_used = now; self.view_instances.set(call, state); Ok(()) } @@ -2891,7 +2893,7 @@ impl MutTxId { if *count == 0 { state.active_subscribers.remove(&subscriber); } - state.last_used = Timestamp::now(); + state.last_used = now_timestamp(); self.view_instances.set(call, state); } @@ -2928,8 +2930,8 @@ impl MutTxId { max_duration: Duration, batch_size: usize, ) -> Result { - let start = std::time::Instant::now(); - let expiration_threshold = Timestamp::now() - expiration_duration; + let start = Instant::now(); + let expiration_threshold = now_timestamp() - expiration_duration; let is_expired = |state: &ViewInstanceState| !state.has_subscribers() && state.last_used < expiration_threshold; let mut cleaned = 0; let batch_size = batch_size.max(1); diff --git a/crates/datastore/src/locking_tx_datastore/time.rs b/crates/datastore/src/locking_tx_datastore/time.rs new file mode 100644 index 00000000000..cb4a0263246 --- /dev/null +++ b/crates/datastore/src/locking_tx_datastore/time.rs @@ -0,0 +1,36 @@ +#[cfg(target_arch = "wasm32")] +use std::time::Duration; + +use spacetimedb_lib::Timestamp; + +#[cfg(not(target_arch = "wasm32"))] +pub use std::time::Instant; + +#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))] +#[allow(deprecated)] +pub fn now_timestamp() -> Timestamp { + Timestamp::now() +} + +#[cfg(all(target_arch = "wasm32", target_os = "unknown"))] +pub fn now_timestamp() -> Timestamp { + Timestamp::UNIX_EPOCH +} + +// `std::time::Instant::now` panics on `wasm32-unknown-unknown`. The portable +// datastore does not report production metrics, so zero-duration timings are +// sufficient for wasm module unit tests. +#[cfg(target_arch = "wasm32")] +#[derive(Clone, Copy, Debug)] +pub struct Instant; + +#[cfg(target_arch = "wasm32")] +impl Instant { + pub fn now() -> Self { + Self + } + + pub fn elapsed(&self) -> Duration { + Duration::ZERO + } +} diff --git a/crates/datastore/src/locking_tx_datastore/tx.rs b/crates/datastore/src/locking_tx_datastore/tx.rs index 4af060ed826..1eb421e5b0c 100644 --- a/crates/datastore/src/locking_tx_datastore/tx.rs +++ b/crates/datastore/src/locking_tx_datastore/tx.rs @@ -2,25 +2,29 @@ use super::{ committed_state::CommittedState, datastore::{Result, TxMetrics}, state_view::{IterByColRangeTx, StateView}, + time::Instant, IterByColEqTx, SharedReadGuard, }; -use crate::{error::IndexError, execution_context::ExecutionContext}; -use spacetimedb_durability::TxOffset; +#[cfg(feature = "execution")] +use crate::error::IndexError; +use crate::execution_context::ExecutionContext; +use crate::traits::TxOffset; +#[cfg(feature = "execution")] use spacetimedb_execution::Datastore; use spacetimedb_lib::metrics::ExecutionMetrics; -use spacetimedb_primitives::{ColList, IndexId, TableId}; +#[cfg(feature = "execution")] +use spacetimedb_primitives::IndexId; +use spacetimedb_primitives::{ColList, TableId}; use spacetimedb_sats::AlgebraicValue; use spacetimedb_schema::{reducer_name::ReducerName, schema::TableSchema}; -use spacetimedb_table::{ - table::{IndexScanPointIter, IndexScanRangeIter, TableAndIndex, TableScanIter}, - table_index::IndexCannotSeekRange, -}; +use spacetimedb_table::table::TableScanIter; +#[cfg(feature = "execution")] +use spacetimedb_table::table::{IndexScanPointIter, IndexScanRangeIter, TableAndIndex}; +#[cfg(feature = "execution")] +use spacetimedb_table::table_index::IndexCannotSeekRange; use std::sync::Arc; use std::{future, num::NonZeroU64}; -use std::{ - ops::RangeBounds, - time::{Duration, Instant}, -}; +use std::{ops::RangeBounds, time::Duration}; /// A read-only transaction with a shared lock on the committed state. pub struct TxId { @@ -33,6 +37,7 @@ pub struct TxId { pub metrics: ExecutionMetrics, } +#[cfg(feature = "execution")] impl Datastore for TxId { type TableIter<'a> = TableScanIter<'a> @@ -125,6 +130,7 @@ impl StateView for TxId { } impl TxId { + #[cfg(feature = "execution")] fn with_index<'a, R>( &'a self, table_id: TableId, diff --git a/crates/datastore/src/system_tables.rs b/crates/datastore/src/system_tables.rs index c1eeb6aa6ea..193003ffe35 100644 --- a/crates/datastore/src/system_tables.rs +++ b/crates/datastore/src/system_tables.rs @@ -948,6 +948,7 @@ fn st_column_accessor_schema() -> TableSchema { /// whereas user tables are reinstantiated with a schema computed from the snapshotted system tables. /// /// This must be kept in sync with the set of system tables. +#[cfg(feature = "durability")] pub(crate) fn system_table_schema(table_id: TableId) -> Option { match table_id { ST_TABLE_ID => Some(st_table_schema()), diff --git a/crates/datastore/src/traits.rs b/crates/datastore/src/traits.rs index 22e12b98271..4ba73623d63 100644 --- a/crates/datastore/src/traits.rs +++ b/crates/datastore/src/traits.rs @@ -9,7 +9,7 @@ use crate::execution_context::Workload; use crate::system_tables::ST_TABLE_ID; use spacetimedb_data_structures::map::IntSet; use spacetimedb_data_structures::small_map::SmallHashMap; -use spacetimedb_durability::TxOffset; +pub type TxOffset = u64; use spacetimedb_lib::{hash_bytes, Identity}; use spacetimedb_primitives::*; use spacetimedb_sats::hash::Hash; diff --git a/crates/expr/src/lib.rs b/crates/expr/src/lib.rs index e27a3b735e3..42114e617ff 100644 --- a/crates/expr/src/lib.rs +++ b/crates/expr/src/lib.rs @@ -83,7 +83,9 @@ pub(crate) fn type_proj(input: RelExpr, proj: ast::Project, vars: &Relvars) -> T // These types determine the size of each stack frame during type checking. // Changing their sizes will require updating the recursion limit to avoid stack overflows. +#[cfg(not(target_arch = "wasm32"))] const _: () = assert!(size_of::>() == 64); +#[cfg(not(target_arch = "wasm32"))] const _: () = assert!(size_of::() == 32); fn _type_expr(vars: &Relvars, expr: SqlExpr, expected: Option<&AlgebraicType>, depth: usize) -> TypingResult { diff --git a/crates/portable-datastore-wasm/Cargo.toml b/crates/portable-datastore-wasm/Cargo.toml new file mode 100644 index 00000000000..a408357e823 --- /dev/null +++ b/crates/portable-datastore-wasm/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "spacetimedb-portable-datastore-wasm" +version.workspace = true +edition.workspace = true +publish = false +description = "Wasm adapter for the SpacetimeDB portable module test datastore." +rust-version.workspace = true + +[lib] +name = "spacetimedb_portable_datastore_wasm" +path = "src/lib.rs" +crate-type = ["cdylib", "rlib"] +bench = false + +[dependencies] +spacetimedb-lib.workspace = true +spacetimedb-portable-datastore = { path = "../portable-datastore", version = "=2.7.0" } +spacetimedb-primitives.workspace = true + +js-sys = "0.3.81" +wasm-bindgen = "0.2.104" + +[lints] +workspace = true diff --git a/crates/portable-datastore-wasm/src/lib.rs b/crates/portable-datastore-wasm/src/lib.rs new file mode 100644 index 00000000000..9f4338ea6a1 --- /dev/null +++ b/crates/portable-datastore-wasm/src/lib.rs @@ -0,0 +1,361 @@ +//! Wasm adapter for the portable module-test datastore. +//! +//! This crate is intentionally thin: `spacetimedb-portable-datastore` owns the +//! datastore semantics, and this crate only maps those operations to a JS/Wasm ABI. + +use std::sync::atomic::{AtomicU32, Ordering}; + +use js_sys::{Array, Uint8Array}; +use spacetimedb_lib::{bsatn, ConnectionId, Identity, RawModuleDef}; +use spacetimedb_portable_datastore::{ + CommitMode, PortableDatastore, PortableDatastoreError, PortableTransaction, ValidatedAuth, +}; +use spacetimedb_primitives::{ColId, IndexId, TableId}; +use wasm_bindgen::prelude::*; + +static NEXT_DATASTORE_ID: AtomicU32 = AtomicU32::new(1); + +#[wasm_bindgen] +#[derive(Clone, Copy)] +pub enum WasmCommitMode { + Normal, + DropEventTableRows, +} + +impl From for CommitMode { + fn from(mode: WasmCommitMode) -> Self { + match mode { + WasmCommitMode::Normal => Self::Normal, + WasmCommitMode::DropEventTableRows => Self::DropEventTableRows, + } + } +} + +#[wasm_bindgen] +pub struct WasmValidatedAuth { + sender: Identity, + connection_id: Option, +} + +#[wasm_bindgen] +impl WasmValidatedAuth { + #[wasm_bindgen(getter, js_name = senderHex)] + pub fn sender_hex(&self) -> String { + self.sender.to_hex().to_string() + } + + #[wasm_bindgen(getter, js_name = connectionIdHex)] + pub fn connection_id_hex(&self) -> Option { + self.connection_id.map(|id| id.to_hex().to_string()) + } +} + +impl From for WasmValidatedAuth { + fn from(auth: ValidatedAuth) -> Self { + Self { + sender: auth.sender, + connection_id: auth.connection_id, + } + } +} + +#[wasm_bindgen] +pub struct WasmPortableDatastore { + id: u32, + inner: PortableDatastore, +} + +#[wasm_bindgen] +impl WasmPortableDatastore { + #[wasm_bindgen(constructor)] + pub fn new(raw_module_def_bsatn: &[u8], module_identity_hex: &str) -> Result { + let raw = bsatn::from_slice::(raw_module_def_bsatn).map_err(to_js_error)?; + let module_identity = Identity::from_hex(module_identity_hex).map_err(to_js_error)?; + let inner = PortableDatastore::from_module_def(raw, module_identity).map_err(to_js_error)?; + Ok(Self { + id: NEXT_DATASTORE_ID.fetch_add(1, Ordering::Relaxed), + inner, + }) + } + + #[wasm_bindgen(js_name = tableId)] + pub fn table_id(&self, table_name: &str) -> Result { + self.inner.table_id(table_name).map(|id| id.0).map_err(to_js_error) + } + + #[wasm_bindgen(js_name = indexId)] + pub fn index_id(&self, index_name: &str) -> Result { + self.inner.index_id(index_name).map(|id| id.0).map_err(to_js_error) + } + + #[wasm_bindgen(js_name = beginMutTx)] + pub fn begin_mut_tx(&self) -> WasmPortableTransaction { + WasmPortableTransaction { + datastore_id: self.id, + inner: Some(self.inner.begin_mut_tx()), + } + } + + #[wasm_bindgen(js_name = commitTx)] + pub fn commit_tx(&self, tx: &mut WasmPortableTransaction, mode: WasmCommitMode) -> Result<(), JsValue> { + self.check_tx(tx)?; + let tx = tx.take()?; + self.inner.commit_tx(tx, mode.into()).map_err(to_js_error) + } + + #[wasm_bindgen(js_name = rollbackTx)] + pub fn rollback_tx(&self, tx: &mut WasmPortableTransaction) -> Result<(), JsValue> { + self.check_tx(tx)?; + let tx = tx.take()?; + self.inner.rollback_tx(tx).map_err(to_js_error) + } + + pub fn reset(&mut self) -> Result<(), JsValue> { + self.inner.reset().map_err(to_js_error) + } + + #[wasm_bindgen(js_name = tableRowCount)] + pub fn table_row_count(&self, table_id: u32) -> Result { + self.inner + .table_row_count(None, TableId(table_id)) + .map(|count| count as f64) + .map_err(to_js_error) + } + + #[wasm_bindgen(js_name = tableRowCountTx)] + pub fn table_row_count_tx(&self, tx: &WasmPortableTransaction, table_id: u32) -> Result { + self.check_tx(tx)?; + self.inner + .table_row_count(Some(tx.inner_ref()?), TableId(table_id)) + .map(|count| count as f64) + .map_err(to_js_error) + } + + #[wasm_bindgen(js_name = tableRowsBsatn)] + pub fn table_rows_bsatn(&self, table_id: u32) -> Result { + self.inner + .table_rows_bsatn(None, TableId(table_id)) + .map(rows_to_js) + .map_err(to_js_error) + } + + #[wasm_bindgen(js_name = tableRowsBsatnTx)] + pub fn table_rows_bsatn_tx(&self, tx: &WasmPortableTransaction, table_id: u32) -> Result { + self.check_tx(tx)?; + self.inner + .table_rows_bsatn(Some(tx.inner_ref()?), TableId(table_id)) + .map(rows_to_js) + .map_err(to_js_error) + } + + #[wasm_bindgen(js_name = indexScanPointBsatn)] + pub fn index_scan_point_bsatn(&self, index_id: u32, point: &[u8]) -> Result { + self.inner + .index_scan_point_bsatn(None, IndexId(index_id), point) + .map(rows_to_js) + .map_err(to_js_error) + } + + #[wasm_bindgen(js_name = indexScanPointBsatnTx)] + pub fn index_scan_point_bsatn_tx( + &self, + tx: &WasmPortableTransaction, + index_id: u32, + point: &[u8], + ) -> Result { + self.check_tx(tx)?; + self.inner + .index_scan_point_bsatn(Some(tx.inner_ref()?), IndexId(index_id), point) + .map(rows_to_js) + .map_err(to_js_error) + } + + #[wasm_bindgen(js_name = indexScanRangeBsatn)] + pub fn index_scan_range_bsatn( + &self, + index_id: u32, + prefix: &[u8], + prefix_elems: u16, + rstart: &[u8], + rend: &[u8], + ) -> Result { + self.inner + .index_scan_range_bsatn(None, IndexId(index_id), prefix, ColId(prefix_elems), rstart, rend) + .map(rows_to_js) + .map_err(to_js_error) + } + + #[wasm_bindgen(js_name = indexScanRangeBsatnTx)] + pub fn index_scan_range_bsatn_tx( + &self, + tx: &WasmPortableTransaction, + index_id: u32, + prefix: &[u8], + prefix_elems: u16, + rstart: &[u8], + rend: &[u8], + ) -> Result { + self.check_tx(tx)?; + self.inner + .index_scan_range_bsatn( + Some(tx.inner_ref()?), + IndexId(index_id), + prefix, + ColId(prefix_elems), + rstart, + rend, + ) + .map(rows_to_js) + .map_err(to_js_error) + } + + #[wasm_bindgen(js_name = insertBsatnGeneratedCols)] + pub fn insert_bsatn_generated_cols( + &self, + tx: &mut WasmPortableTransaction, + table_id: u32, + row: &[u8], + ) -> Result, JsValue> { + self.check_tx(tx)?; + self.inner + .insert_bsatn_generated_cols(tx.inner_mut()?, TableId(table_id), row) + .map_err(to_js_error) + } + + #[wasm_bindgen(js_name = updateBsatnGeneratedCols)] + pub fn update_bsatn_generated_cols( + &self, + tx: &mut WasmPortableTransaction, + table_id: u32, + index_id: u32, + row: &[u8], + ) -> Result, JsValue> { + self.check_tx(tx)?; + self.inner + .update_bsatn_generated_cols(tx.inner_mut()?, TableId(table_id), IndexId(index_id), row) + .map_err(to_js_error) + } + + #[wasm_bindgen(js_name = deleteByRelBsatn)] + pub fn delete_by_rel_bsatn( + &self, + tx: &mut WasmPortableTransaction, + table_id: u32, + relation: &[u8], + ) -> Result { + self.check_tx(tx)?; + self.inner + .delete_by_rel_bsatn(tx.inner_mut()?, TableId(table_id), relation) + .map_err(to_js_error) + } + + #[wasm_bindgen(js_name = deleteByIndexScanPointBsatn)] + pub fn delete_by_index_scan_point_bsatn( + &self, + tx: &mut WasmPortableTransaction, + index_id: u32, + point: &[u8], + ) -> Result { + self.check_tx(tx)?; + self.inner + .delete_by_index_scan_point_bsatn(tx.inner_mut()?, IndexId(index_id), point) + .map_err(to_js_error) + } + + #[wasm_bindgen(js_name = deleteByIndexScanRangeBsatn)] + pub fn delete_by_index_scan_range_bsatn( + &self, + tx: &mut WasmPortableTransaction, + index_id: u32, + prefix: &[u8], + prefix_elems: u16, + rstart: &[u8], + rend: &[u8], + ) -> Result { + self.check_tx(tx)?; + self.inner + .delete_by_index_scan_range_bsatn( + tx.inner_mut()?, + IndexId(index_id), + prefix, + ColId(prefix_elems), + rstart, + rend, + ) + .map_err(to_js_error) + } + + #[wasm_bindgen(js_name = clearTable)] + pub fn clear_table(&self, tx: &mut WasmPortableTransaction, table_id: u32) -> Result { + self.check_tx(tx)?; + self.inner + .clear_table(tx.inner_mut()?, TableId(table_id)) + .map(|count| count as f64) + .map_err(to_js_error) + } + + #[wasm_bindgen(js_name = validateJwtPayload)] + pub fn validate_jwt_payload(&self, payload: &str, connection_id_hex: &str) -> Result { + let connection_id = ConnectionId::from_hex(connection_id_hex).map_err(to_js_error)?; + self.inner + .validate_jwt_payload(payload, connection_id) + .map(Into::into) + .map_err(to_js_error) + } + + #[wasm_bindgen(js_name = runQuery)] + pub fn run_query(&self, sql: &str, database_identity_hex: &str) -> Result { + let database_identity = Identity::from_hex(database_identity_hex).map_err(to_js_error)?; + self.inner + .run_query_bsatn(sql, database_identity) + .map(rows_to_js) + .map_err(to_js_error) + } + + fn check_tx(&self, tx: &WasmPortableTransaction) -> Result<(), JsValue> { + if tx.datastore_id == self.id { + Ok(()) + } else { + Err(JsValue::from_str("transaction belongs to a different datastore")) + } + } +} + +#[wasm_bindgen] +pub struct WasmPortableTransaction { + datastore_id: u32, + inner: Option, +} + +impl WasmPortableTransaction { + fn inner_ref(&self) -> Result<&PortableTransaction, JsValue> { + self.inner + .as_ref() + .ok_or_else(|| JsValue::from_str("transaction already finished")) + } + + fn inner_mut(&mut self) -> Result<&mut PortableTransaction, JsValue> { + self.inner + .as_mut() + .ok_or_else(|| JsValue::from_str("transaction already finished")) + } + + fn take(&mut self) -> Result { + self.inner + .take() + .ok_or_else(|| JsValue::from_str("transaction already finished")) + } +} + +fn rows_to_js(rows: Vec>) -> Array { + rows.into_iter() + .map(|row| JsValue::from(Uint8Array::from(row.as_slice()))) + .collect() +} + +fn to_js_error(error: impl std::fmt::Display) -> JsValue { + JsValue::from_str(&error.to_string()) +} + +#[allow(dead_code)] +fn _assert_error_is_send_sync(_: &PortableDatastoreError) {} diff --git a/crates/portable-datastore/Cargo.toml b/crates/portable-datastore/Cargo.toml new file mode 100644 index 00000000000..9349b4ff9ae --- /dev/null +++ b/crates/portable-datastore/Cargo.toml @@ -0,0 +1,31 @@ +[package] +name = "spacetimedb-portable-datastore" +version.workspace = true +edition.workspace = true +license-file = "LICENSE" +description = "Wasm-compatible in-memory datastore support for SpacetimeDB module unit tests." +rust-version.workspace = true + +[lib] +name = "spacetimedb_portable_datastore" +path = "src/lib.rs" +bench = false + +[dependencies] +spacetimedb-datastore = { path = "../datastore", version = "=2.7.0", default-features = false, features = ["portable", "execution"] } +spacetimedb-expr.workspace = true +spacetimedb-lib.workspace = true +spacetimedb-primitives.workspace = true +spacetimedb-query.workspace = true +spacetimedb-schema.workspace = true +spacetimedb-table.workspace = true + +anyhow.workspace = true +serde_json.workspace = true +thiserror.workspace = true + +[dev-dependencies] +spacetimedb-lib = { workspace = true, features = ["test"] } + +[lints] +workspace = true diff --git a/crates/portable-datastore/LICENSE b/crates/portable-datastore/LICENSE new file mode 100644 index 00000000000..39332526b0c --- /dev/null +++ b/crates/portable-datastore/LICENSE @@ -0,0 +1,731 @@ +SPACETIMEDB BUSINESS SOURCE LICENSE AGREEMENT + +Business Source License 1.1 + +Parameters + +Licensor: Clockwork Laboratories, Inc. +Licensed Work: SpacetimeDB 2.1.0 + The Licensed Work is + (c) 2023 Clockwork Laboratories, Inc. + +Additional Use Grant: You may make use of the Licensed Work provided your + application or service uses the Licensed Work with no + more than one SpacetimeDB instance in production and + provided that you do not use the Licensed Work for a + Database Service. + + A “Database Service” is a commercial offering that + allows third parties (other than your employees and + contractors) to access the functionality of the + Licensed Work by creating tables whose schemas are + controlled by such third parties. + +Change Date: 2031-03-20 + +Change License: GNU Affero General Public License v3.0 with a linking + exception + +For information about alternative licensing arrangements for the Software, +please visit: https://spacetimedb.com + +Notice + +The Business Source License (this document, or the “License”) is not an Open +Source license. However, the Licensed Work will eventually be made available +under an Open Source License, as stated in this License. + +License text copyright (c) 2017 MariaDB Corporation Ab, All Rights Reserved. +“Business Source License” is a trademark of MariaDB Corporation Ab. + +----------------------------------------------------------------------------- + +Business Source License 1.1 + +Terms + +The Licensor hereby grants you the right to copy, modify, create derivative +works, redistribute, and make non-production use of the Licensed Work. The +Licensor may make an Additional Use Grant, above, permitting limited +production use. + +Effective on the Change Date, or the fourth anniversary of the first publicly +available distribution of a specific version of the Licensed Work under this +License, whichever comes first, the Licensor hereby grants you rights under +the terms of the Change License, and the rights granted in the paragraph +above terminate. + +If your use of the Licensed Work does not comply with the requirements +currently in effect as described in this License, you must purchase a +commercial license from the Licensor, its affiliated entities, or authorized +resellers, or you must refrain from using the Licensed Work. + +All copies of the original and modified Licensed Work, and derivative works +of the Licensed Work, are subject to this License. This License applies +separately for each version of the Licensed Work and the Change Date may vary +for each version of the Licensed Work released by Licensor. + +You must conspicuously display this License on each original or modified copy +of the Licensed Work. If you receive the Licensed Work in original or +modified form from a third party, the terms and conditions set forth in this +License apply to your use of that work. + +Any use of the Licensed Work in violation of this License will automatically +terminate your rights under this License for the current and all other +versions of the Licensed Work. + +This License does not grant you any right in any trademark or logo of +Licensor or its affiliates (provided that you may use a trademark or logo of +Licensor as expressly required by this License). + +TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +TITLE. + +MariaDB hereby grants you permission to use this License’s text to license +your works, and to refer to it using the trademark “Business Source License”, +as long as you comply with the Covenants of Licensor below. + +Covenants of Licensor + +In consideration of the right to use this License’s text and the “Business +Source License” name and trademark, Licensor covenants to MariaDB, and to all +other recipients of the licensed work to be provided by Licensor: + +1. To specify as the Change License the GPL Version 2.0 or any later version, + or a license that is compatible with GPL Version 2.0 or a later version, + where “compatible” means that software provided under the Change License can + be included in a program with software provided under GPL Version 2.0 or a + later version. Licensor may specify additional Change Licenses without + limitation. + +2. To either: (a) specify an additional grant of rights to use that does not + impose any additional restriction on the right granted in this License, as + the Additional Use Grant; or (b) insert the text “None”. + +3. To specify a Change Date. + +4. Not to modify this License in any other way. + +----------------------------------------------------------------------------- + +Copyright (C) 2023 Clockwork Laboratories, Inc. + +This program is free software: you can redistribute it and/or modify it under +the terms of the GNU Affero General Public License, version 3, as published +by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +details. + +You should have received a copy of the GNU Affero General Public License +along with this program; if not, see . + +Additional permission under GNU GPL version 3 section 7 + +If you modify this Program, or any covered work, by linking or combining it +with SpacetimeDB (or a modified version of that library), containing parts +covered by the terms of the AGPL v3.0, the licensors of this Program grant +you additional permission to convey the resulting work. + +Additional permission under GNU AGPL version 3 section 13 + +If you modify this Program, or any covered work, by linking or combining it +with SpacetimeDB (or a modified version of that library), containing parts +covered by the terms of the AGPL v3.0, the licensors of this Program grant +you additional permission that, notwithstanding any other provision of this +License, you need not prominently offer all users interacting with your +modified version remotely through a computer network an opportunity to +receive the Corresponding Source of your version from a network server at no +charge, if your version supports such interaction. This permission does not +waive or modify any other obligations or terms of the AGPL v3.0, except for +the specific requirement set forth in section 13. + +A copy of the AGPL v3.0 license is reproduced below. + + GNU AFFERO GENERAL PUBLIC LICENSE + Version 3, 19 November 2007 + +Copyright © 2007 Free Software Foundation, Inc. +Everyone is permitted to copy and distribute verbatim copies of this license +document, but changing it is not allowed. + +Preamble +The GNU Affero General Public License is a free, copyleft license for +software and other kinds of works, specifically designed to ensure +cooperation with the community in the case of network server software. + +The licenses for most software and other practical works are designed to take +away your freedom to share and change the works. By contrast, our General +Public Licenses are intended to guarantee your freedom to share and change +all versions of a program--to make sure it remains free software for all its +users. + +When we speak of free software, we are referring to freedom, not price. Our +General Public Licenses are designed to make sure that you have the freedom +to distribute copies of free software (and charge for them if you wish), that +you receive source code or can get it if you want it, that you can change the +software or use pieces of it in new free programs, and that you know you can +do these things. + +Developers that use our General Public Licenses protect your rights with two +steps: (1) assert copyright on the software, and (2) offer you this License +which gives you legal permission to copy, distribute and/or modify the +software. + +A secondary benefit of defending all users' freedom is that improvements made +in alternate versions of the program, if they receive widespread use, become +available for other developers to incorporate. Many developers of free +software are heartened and encouraged by the resulting cooperation. However, +in the case of software used on network servers, this result may fail to come +about. The GNU General Public License permits making a modified version and +letting the public access it on a server without ever releasing its source +code to the public. + +The GNU Affero General Public License is designed specifically to ensure +that, in such cases, the modified source code becomes available to the +community. It requires the operator of a network server to provide the source +code of the modified version running there to the users of that server. +Therefore, public use of a modified version, on a publicly accessible server, +gives the public access to the source code of the modified version. + +An older license, called the Affero General Public License and published by +Affero, was designed to accomplish similar goals. This is a different +license, not a version of the Affero GPL, but Affero has released a new +version of the Affero GPL which permits relicensing under this license. + +The precise terms and conditions for copying, distribution and modification +follow. + +TERMS AND CONDITIONS +0. Definitions. +"This License" refers to version 3 of the GNU Affero General Public License. + +"Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + +"The Program" refers to any copyrightable work licensed under this License. +Each licensee is addressed as "you". "Licensees" and "recipients" may be +individuals or organizations. + +To "modify" a work means to copy from or adapt all or part of the work in a +fashion requiring copyright permission, other than the making of an exact +copy. The resulting work is called a "modified version" of the earlier work +or a work "based on" the earlier work. + +A "covered work" means either the unmodified Program or a work based on the +Program. + +To "propagate" a work means to do anything with it that, without permission, +would make you directly or secondarily liable for infringement under +applicable copyright law, except executing it on a computer or modifying a +private copy. Propagation includes copying, distribution (with or without +modification), making available to the public, and in some countries other +activities as well. + +To "convey" a work means any kind of propagation that enables other parties +to make or receive copies. Mere interaction with a user through a computer +network, with no transfer of a copy, is not conveying. + +An interactive user interface displays "Appropriate Legal Notices" to the +extent that it includes a convenient and prominently visible feature that (1) +displays an appropriate copyright notice, and (2) tells the user that there +is no warranty for the work (except to the extent that warranties are +provided), that licensees may convey the work under this License, and how to +view a copy of this License. If the interface presents a list of user +commands or options, such as a menu, a prominent item in the list meets this +criterion. + +1. Source Code. +The "source code" for a work means the preferred form of the work for making +modifications to it. "Object code" means any non-source form of a work. + +A "Standard Interface" means an interface that either is an official standard +defined by a recognized standards body, or, in the case of interfaces +specified for a particular programming language, one that is widely used +among developers working in that language. + +The "System Libraries" of an executable work include anything, other than the +work as a whole, that (a) is included in the normal form of packaging a Major +Component, but which is not part of that Major Component, and (b) serves only +to enable use of the work with that Major Component, or to implement a +Standard Interface for which an implementation is available to the public in +source code form. A "Major Component", in this context, means a major +essential component (kernel, window system, and so on) of the specific +operating system (if any) on which the executable work runs, or a compiler +used to produce the work, or an object code interpreter used to run it. + +The "Corresponding Source" for a work in object code form means all the +source code needed to generate, install, and (for an executable work) run the +object code and to modify the work, including scripts to control those +activities. However, it does not include the work's System Libraries, or +general-purpose tools or generally available free programs which are used +unmodified in performing those activities but which are not part of the work. +For example, Corresponding Source includes interface definition files +associated with source files for the work, and the source code for shared +libraries and dynamically linked subprograms that the work is specifically +designed to require, such as by intimate data communication or control flow +between those subprograms and other parts of the work. + +The Corresponding Source need not include anything that users can regenerate +automatically from other parts of the Corresponding Source. + +The Corresponding Source for a work in source code form is that same work. + +2. Basic Permissions. +All rights granted under this License are granted for the term of copyright +on the Program, and are irrevocable provided the stated conditions are met. +This License explicitly affirms your unlimited permission to run the +unmodified Program. The output from running a covered work is covered by this +License only if the output, given its content, constitutes a covered work. +This License acknowledges your rights of fair use or other equivalent, as +provided by copyright law. + +You may make, run and propagate covered works that you do not convey, without +conditions so long as your license otherwise remains in force. You may convey +covered works to others for the sole purpose of having them make +modifications exclusively for you, or provide you with facilities for running +those works, provided that you comply with the terms of this License in +conveying all material for which you do not control copyright. Those thus +making or running the covered works for you must do so exclusively on your +behalf, under your direction and control, on terms that prohibit them from +making any copies of your copyrighted material outside their relationship +with you. + +Conveying under any other circumstances is permitted solely under the +conditions stated below. Sublicensing is not allowed; section 10 makes it +unnecessary. + +3. Protecting Users' Legal Rights From Anti-Circumvention Law. +No covered work shall be deemed part of an effective technological measure +under any applicable law fulfilling obligations under article 11 of the WIPO +copyright treaty adopted on 20 December 1996, or similar laws prohibiting or +restricting circumvention of such measures. + +When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention is +effected by exercising rights under this License with respect to the covered +work, and you disclaim any intention to limit operation or modification of +the work as a means of enforcing, against the work's users, your or third +parties' legal rights to forbid circumvention of technological measures. + +4. Conveying Verbatim Copies. +You may convey verbatim copies of the Program's source code as you receive +it, in any medium, provided that you conspicuously and appropriately publish +on each copy an appropriate copyright notice; keep intact all notices stating +that this License and any non-permissive terms added in accord with section 7 +apply to the code; keep intact all notices of the absence of any warranty; +and give all recipients a copy of this License along with the Program. + +You may charge any price or no price for each copy that you convey, and you +may offer support or warranty protection for a fee. + +5. Conveying Modified Source Versions. +You may convey a work based on the Program, or the modifications to produce +it from the Program, in the form of source code under the terms of section 4, +provided that you also meet all of these conditions: + +a) The work must carry prominent notices stating that you modified it, and +giving a relevant date. +b) The work must carry prominent notices stating that it is released under +this License and any conditions added under section 7. This requirement +modifies the requirement in section 4 to "keep intact all notices". +c) You must license the entire work, as a whole, under this License to anyone +who comes into possession of a copy. This License will therefore apply, along +with any applicable section 7 additional terms, to the whole of the work, and +all its parts, regardless of how they are packaged. This License gives no +permission to license the work in any other way, but it does not invalidate +such permission if you have separately received it. +d) If the work has interactive user interfaces, each must display Appropriate +Legal Notices; however, if the Program has interactive interfaces that do not +display Appropriate Legal Notices, your work need not make them do so. +A compilation of a covered work with other separate and independent works, +which are not by their nature extensions of the covered work, and which are +not combined with it such as to form a larger program, in or on a volume of a +storage or distribution medium, is called an "aggregate" if the compilation +and its resulting copyright are not used to limit the access or legal rights +of the compilation's users beyond what the individual works permit. Inclusion +of a covered work in an aggregate does not cause this License to apply to the +other parts of the aggregate. + +6. Conveying Non-Source Forms. +You may convey a covered work in object code form under the terms of sections +4 and 5, provided that you also convey the machine-readable Corresponding +Source under the terms of this License, in one of these ways: + +a) Convey the object code in, or embodied in, a physical product (including a +physical distribution medium), accompanied by the Corresponding Source fixed +on a durable physical medium customarily used for software interchange. +b) Convey the object code in, or embodied in, a physical product (including a +physical distribution medium), accompanied by a written offer, valid for at +least three years and valid for as long as you offer spare parts or customer +support for that product model, to give anyone who possesses the object code +either (1) a copy of the Corresponding Source for all the software in the +product that is covered by this License, on a durable physical medium +customarily used for software interchange, for a price no more than your +reasonable cost of physically performing this conveying of source, or (2) +access to copy the Corresponding Source from a network server at no charge. +c) Convey individual copies of the object code with a copy of the written +offer to provide the Corresponding Source. This alternative is allowed only +occasionally and noncommercially, and only if you received the object code +with such an offer, in accord with subsection 6b. +d) Convey the object code by offering access from a designated place (gratis +or for a charge), and offer equivalent access to the Corresponding Source in +the same way through the same place at no further charge. You need not +require recipients to copy the Corresponding Source along with the object +code. If the place to copy the object code is a network server, the +Corresponding Source may be on a different server (operated by you or a third +party) that supports equivalent copying facilities, provided you maintain +clear directions next to the object code saying where to find the +Corresponding Source. Regardless of what server hosts the Corresponding +Source, you remain obligated to ensure that it is available for as long as +needed to satisfy these requirements. +e) Convey the object code using peer-to-peer transmission, provided you +inform other peers where the object code and Corresponding Source of the work +are being offered to the general public at no charge under subsection 6d. +A separable portion of the object code, whose source code is excluded from +the Corresponding Source as a System Library, need not be included in +conveying the object code work. + +A "User Product" is either (1) a "consumer product", which means any tangible +personal property which is normally used for personal, family, or household +purposes, or (2) anything designed or sold for incorporation into a dwelling. +In determining whether a product is a consumer product, doubtful cases shall +be resolved in favor of coverage. For a particular product received by a +particular user, "normally used" refers to a typical or common use of that +class of product, regardless of the status of the particular user or of the +way in which the particular user actually uses, or expects or is expected to +use, the product. A product is a consumer product regardless of whether the +product has substantial commercial, industrial or non-consumer uses, unless +such uses represent the only significant mode of use of the product. + +"Installation Information" for a User Product means any methods, procedures, +authorization keys, or other information required to install and execute +modified versions of a covered work in that User Product from a modified +version of its Corresponding Source. The information must suffice to ensure +that the continued functioning of the modified object code is in no case +prevented or interfered with solely because modification has been made. + +If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as part of +a transaction in which the right of possession and use of the User Product is +transferred to the recipient in perpetuity or for a fixed term (regardless of +how the transaction is characterized), the Corresponding Source conveyed +under this section must be accompanied by the Installation Information. But +this requirement does not apply if neither you nor any third party retains +the ability to install modified object code on the User Product (for example, +the work has been installed in ROM). + +The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates for +a work that has been modified or installed by the recipient, or for the User +Product in which it has been modified or installed. Access to a network may +be denied when the modification itself materially and adversely affects the +operation of the network or violates the rules and protocols for +communication across the network. + +Corresponding Source conveyed, and Installation Information provided, in +accord with this section must be in a format that is publicly documented (and +with an implementation available to the public in source code form), and must +require no special password or key for unpacking, reading or copying. + +7. Additional Terms. +"Additional permissions" are terms that supplement the terms of this License +by making exceptions from one or more of its conditions. Additional +permissions that are applicable to the entire Program shall be treated as +though they were included in this License, to the extent that they are valid +under applicable law. If additional permissions apply only to part of the +Program, that part may be used separately under those permissions, but the +entire Program remains governed by this License without regard to the +additional permissions. + +When you convey a copy of a covered work, you may at your option remove any +additional permissions from that copy, or from any part of it. (Additional +permissions may be written to require their own removal in certain cases when +you modify the work.) You may place additional permissions on material, added +by you to a covered work, for which you have or can give appropriate +copyright permission. + +Notwithstanding any other provision of this License, for material you add to +a covered work, you may (if authorized by the copyright holders of that +material) supplement the terms of this License with terms: + +a) Disclaiming warranty or limiting liability differently from the terms of +sections 15 and 16 of this License; or +b) Requiring preservation of specified reasonable legal notices or author +attributions in that material or in the Appropriate Legal Notices displayed +by works containing it; or +c) Prohibiting misrepresentation of the origin of that material, or requiring +that modified versions of such material be marked in reasonable ways as +different from the original version; or +d) Limiting the use for publicity purposes of names of licensors or authors +of the material; or +e) Declining to grant rights under trademark law for use of some trade names, +trademarks, or service marks; or +f) Requiring indemnification of licensors and authors of that material by +anyone who conveys the material (or modified versions of it) with contractual +assumptions of liability to the recipient, for any liability that these +contractual assumptions directly impose on those licensors and authors. +All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is governed +by this License along with a term that is a further restriction, you may +remove that term. If a license document contains a further restriction but +permits relicensing or conveying under this License, you may add to a covered +work material governed by the terms of that license document, provided that +the further restriction does not survive such relicensing or conveying. + +If you add terms to a covered work in accord with this section, you must +place, in the relevant source files, a statement of the additional terms that +apply to those files, or a notice indicating where to find the applicable +terms. + +Additional terms, permissive or non-permissive, may be stated in the form of +a separately written license, or stated as exceptions; the above requirements +apply either way. + +8. Termination. +You may not propagate or modify a covered work except as expressly provided +under this License. Any attempt otherwise to propagate or modify it is void, +and will automatically terminate your rights under this License (including +any patent licenses granted under the third paragraph of section 11). + +However, if you cease all violation of this License, then your license from a +particular copyright holder is reinstated (a) provisionally, unless and until +the copyright holder explicitly and finally terminates your license, and (b) +permanently, if the copyright holder fails to notify you of the violation by +some reasonable means prior to 60 days after the cessation. + +Moreover, your license from a particular copyright holder is reinstated +permanently if the copyright holder notifies you of the violation by some +reasonable means, this is the first time you have received notice of +violation of this License (for any work) from that copyright holder, and you +cure the violation prior to 30 days after your receipt of the notice. + +Termination of your rights under this section does not terminate the licenses +of parties who have received copies or rights from you under this License. If +your rights have been terminated and not permanently reinstated, you do not +qualify to receive new licenses for the same material under section 10. + +9. Acceptance Not Required for Having Copies. +You are not required to accept this License in order to receive or run a copy +of the Program. Ancillary propagation of a covered work occurring solely as a +consequence of using peer-to-peer transmission to receive a copy likewise +does not require acceptance. However, nothing other than this License grants +you permission to propagate or modify any covered work. These actions +infringe copyright if you do not accept this License. Therefore, by modifying +or propagating a covered work, you indicate your acceptance of this License +to do so. + +10. Automatic Licensing of Downstream Recipients. +Each time you convey a covered work, the recipient automatically receives a +license from the original licensors, to run, modify and propagate that work, +subject to this License. You are not responsible for enforcing compliance by +third parties with this License. + +An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered work +results from an entity transaction, each party to that transaction who +receives a copy of the work also receives whatever licenses to the work the +party's predecessor in interest had or could give under the previous +paragraph, plus a right to possession of the Corresponding Source of the work +from the predecessor in interest, if the predecessor has it or can get it +with reasonable efforts. + +You may not impose any further restrictions on the exercise of the rights +granted or affirmed under this License. For example, you may not impose a +license fee, royalty, or other charge for exercise of rights granted under +this License, and you may not initiate litigation (including a cross-claim or +counterclaim in a lawsuit) alleging that any patent claim is infringed by +making, using, selling, offering for sale, or importing the Program or any +portion of it. + +11. Patents. +A "contributor" is a copyright holder who authorizes use under this License +of the Program or a work on which the Program is based. The work thus +licensed is called the contributor's "contributor version". + +A contributor's "essential patent claims" are all patent claims owned or +controlled by the contributor, whether already acquired or hereafter +acquired, that would be infringed by some manner, permitted by this License, +of making, using, or selling its contributor version, but do not include +claims that would be infringed only as a consequence of further modification +of the contributor version. For purposes of this definition, "control" +includes the right to grant patent sublicenses in a manner consistent with +the requirements of this License. + +Each contributor grants you a non-exclusive, worldwide, royalty-free patent +license under the contributor's essential patent claims, to make, use, sell, +offer for sale, import and otherwise run, modify and propagate the contents +of its contributor version. + +In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent (such +as an express permission to practice a patent or covenant not to sue for +patent infringement). To "grant" such a patent license to a party means to +make such an agreement or commitment not to enforce a patent against the +party. + +If you convey a covered work, knowingly relying on a patent license, and the +Corresponding Source of the work is not available for anyone to copy, free of +charge and under the terms of this License, through a publicly available +network server or other readily accessible means, then you must either (1) +cause the Corresponding Source to be so available, or (2) arrange to deprive +yourself of the benefit of the patent license for this particular work, or +(3) arrange, in a manner consistent with the requirements of this License, to +extend the patent license to downstream recipients. "Knowingly relying" means +you have actual knowledge that, but for the patent license, your conveying +the covered work in a country, or your recipient's use of the covered work in +a country, would infringe one or more identifiable patents in that country +that you have reason to believe are valid. + +If, pursuant to or in connection with a single transaction or arrangement, +you convey, or propagate by procuring conveyance of, a covered work, and +grant a patent license to some of the parties receiving the covered work +authorizing them to use, propagate, modify or convey a specific copy of the +covered work, then the patent license you grant is automatically extended to +all recipients of the covered work and works based on it. + +A patent license is "discriminatory" if it does not include within the scope +of its coverage, prohibits the exercise of, or is conditioned on the +non-exercise of one or more of the rights that are specifically granted under +this License. You may not convey a covered work if you are a party to an +arrangement with a third party that is in the business of distributing +software, under which you make payment to the third party based on the extent +of your activity of conveying the work, and under which the third party +grants, to any of the parties who would receive the covered work from you, a +discriminatory patent license (a) in connection with copies of the covered +work conveyed by you (or copies made from those copies), or (b) primarily for +and in connection with specific products or compilations that contain the +covered work, unless you entered into that arrangement, or that patent +license was granted, prior to 28 March 2007. + +Nothing in this License shall be construed as excluding or limiting any +implied license or other defenses to infringement that may otherwise be +available to you under applicable patent law. + +12. No Surrender of Others' Freedom. +If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not excuse +you from the conditions of this License. If you cannot convey a covered work +so as to satisfy simultaneously your obligations under this License and any +other pertinent obligations, then as a consequence you may not convey it at +all. For example, if you agree to terms that obligate you to collect a +royalty for further conveying from those to whom you convey the Program, the +only way you could satisfy both those terms and this License would be to +refrain entirely from conveying the Program. + +13. Remote Network Interaction; Use with the GNU General Public License. +Notwithstanding any other provision of this License, if you modify the +Program, your modified version must prominently offer all users interacting +with it remotely through a computer network (if your version supports such +interaction) an opportunity to receive the Corresponding Source of your +version by providing access to the Corresponding Source from a network server +at no charge, through some standard or customary means of facilitating +copying of software. This Corresponding Source shall include the +Corresponding Source for any work covered by version 3 of the GNU General +Public License that is incorporated pursuant to the following paragraph. + +Notwithstanding any other provision of this License, you have permission to +link or combine any covered work with a work licensed under version 3 of the +GNU General Public License into a single combined work, and to convey the +resulting work. The terms of this License will continue to apply to the part +which is the covered work, but the work with which it is combined will remain +governed by version 3 of the GNU General Public License. + +14. Revised Versions of this License. +The Free Software Foundation may publish revised and/or new versions of the +GNU Affero General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies that a certain numbered version of the GNU Affero General Public +License "or any later version" applies to it, you have the option of +following the terms and conditions either of that numbered version or of any +later version published by the Free Software Foundation. If the Program does +not specify a version number of the GNU Affero General Public License, you +may choose any version ever published by the Free Software Foundation. + +If the Program specifies that a proxy can decide which future versions of the +GNU Affero General Public License can be used, that proxy's public statement +of acceptance of a version permanently authorizes you to choose that version +for the Program. + +Later license versions may give you additional or different permissions. +However, no additional obligations are imposed on any author or copyright +holder as a result of your choosing to follow a later version. + +15. Disclaimer of Warranty. +THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE +LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, +EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE +ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. +SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY +SERVICING, REPAIR OR CORRECTION. + +16. Limitation of Liability. +IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL +ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE +PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE +OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR +DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR +A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH +HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + +17. Interpretation of Sections 15 and 16. +If the disclaimer of warranty and limitation of liability provided above +cannot be given local legal effect according to their terms, reviewing courts +shall apply local law that most closely approximates an absolute waiver of +all civil liability in connection with the Program, unless a warranty or +assumption of liability accompanies a copy of the Program in return for a +fee. + +END OF TERMS AND CONDITIONS + +How to Apply These Terms to Your New Programs +If you develop a new program, and you want it to be of the greatest possible +use to the public, the best way to achieve this is to make it free software +which everyone can redistribute and change under these terms. + +To do so, attach the following notices to the program. It is safest to attach +them to the start of each source file to most effectively state the exclusion +of warranty; and each file should have at least the "copyright" line and a +pointer to where the full notice is found. + +SpacetimeDB: A database which replaces your server. +Copyright (C) 2023 Clockwork Laboratories, Inc. + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as +published by the Free Software Foundation, either version 3 of the +License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . +Also add information on how to contact you by electronic and paper mail. + +If your software can interact with users remotely through a computer network, +you should also make sure that it provides a way for users to get its source. +For example, if your program is a web application, its interface could +display a "Source" link that leads users to an archive of the code. There are +many ways you could offer source, and different solutions will be better for +different programs; see section 13 for the specific requirements. + +You should also get your employer (if you work as a programmer) or school, if +any, to sign a "copyright disclaimer" for the program, if necessary. For more +information on this, and how to apply and follow the GNU AGPL, see +. diff --git a/crates/portable-datastore/src/lib.rs b/crates/portable-datastore/src/lib.rs new file mode 100644 index 00000000000..788b6a7ae08 --- /dev/null +++ b/crates/portable-datastore/src/lib.rs @@ -0,0 +1,964 @@ +//! Wasm-compatible in-memory datastore support for SpacetimeDB module unit tests. + +use std::collections::HashMap; +use std::sync::Arc; + +use anyhow::Context; +use spacetimedb_datastore::error::{DatastoreError, IndexError, SequenceError}; +use spacetimedb_datastore::execution_context::Workload; +use spacetimedb_datastore::locking_tx_datastore::datastore::Locking; +use spacetimedb_datastore::locking_tx_datastore::state_view::StateView; +use spacetimedb_datastore::locking_tx_datastore::IndexScanPointOrRange; +use spacetimedb_datastore::system_tables::{StRowLevelSecurityFields, ST_ROW_LEVEL_SECURITY_ID}; +use spacetimedb_datastore::traits::{IsolationLevel, MutTx, MutTxDatastore, Tx, TxDatastore}; +use spacetimedb_lib::bsatn::{DecodeError, EncodeError, ToBsatn}; +use spacetimedb_lib::identity::AuthCtx; +use spacetimedb_lib::metrics::ExecutionMetrics; +use spacetimedb_lib::{AlgebraicValue, ConnectionId, Identity, ProductType, ProductValue, RawModuleDef}; +use spacetimedb_primitives::{ColId, IndexId, TableId}; +use spacetimedb_schema::def::ModuleDef; +use spacetimedb_schema::error::ValidationErrors; +use spacetimedb_schema::schema::{Schema, TableOrViewSchema, TableSchema}; +use spacetimedb_table::page_pool::PagePool; +use thiserror::Error; + +/// Commit-time behavior for a pending transaction. +#[derive(Debug, Clone, Copy, Eq, PartialEq)] +pub enum CommitMode { + /// Commit all rows normally. + Normal, + /// Drop event-table rows before committing to shared state. + DropEventTableRows, +} + +/// Auth state validated for a test caller. +#[derive(Debug, Clone, Eq, PartialEq)] +pub struct ValidatedAuth { + pub sender: Identity, + pub connection_id: Option, +} + +/// A [`Locking`] datastore initialized from a module definition for portable unit tests. +pub struct PortableDatastore { + datastore: Locking, + module_def: ModuleDef, + raw_module_def: RawModuleDef, + module_identity: Identity, + event_table_ids: Vec, + table_ids: HashMap, TableId>, + index_ids: HashMap, IndexId>, +} + +/// A single pending mutable transaction. +pub struct PortableTransaction { + datastore: Locking, + tx: Option<::MutTx>, +} + +impl PortableDatastore { + /// Create an in-memory datastore initialized with the tables in `raw`. + pub fn from_module_def(raw: RawModuleDef, module_identity: Identity) -> Result { + let module_def = ModuleDef::try_from(raw.clone())?; + let datastore = Locking::bootstrap(module_identity, PagePool::new(None))?; + let mut tx = datastore.begin_mut_tx(IsolationLevel::Serializable, Workload::Internal); + let mut event_table_ids = Vec::new(); + let mut table_ids = HashMap::new(); + let mut index_ids = HashMap::new(); + + let result = (|| { + for table in module_def.tables() { + let schema = TableSchema::from_module_def(&module_def, table, (), TableId::SENTINEL); + let table_id = datastore.create_table_mut_tx(&mut tx, schema)?; + table_ids.insert(table.name.to_string().into_boxed_str(), table_id); + table_ids.insert(table.accessor_name.to_string().into_boxed_str(), table_id); + + for index in table.indexes.values() { + let Some(index_id) = datastore.index_id_from_name_mut_tx(&tx, index.name.as_ref())? else { + return Err(PortableDatastoreError::MissingIndex(index.name.as_ref().into())); + }; + index_ids.insert(index.name.to_string().into_boxed_str(), index_id); + index_ids.insert(index.source_name.to_string().into_boxed_str(), index_id); + if let Some(accessor_name) = &index.accessor_name { + index_ids.insert(accessor_name.to_string().into_boxed_str(), index_id); + } + } + + if table.is_event { + event_table_ids.push(table_id); + } + } + Ok::<_, PortableDatastoreError>(()) + })(); + + if let Err(err) = result { + let _ = datastore.rollback_mut_tx(tx); + return Err(err); + } + + datastore.commit_mut_tx(tx)?; + + Ok(Self { + datastore, + module_def, + raw_module_def: raw, + module_identity, + event_table_ids, + table_ids, + index_ids, + }) + } + + /// The validated module definition used to initialize this datastore. + pub fn module_def(&self) -> &ModuleDef { + &self.module_def + } + + /// Resolve a table name to its datastore id. + pub fn table_id(&self, table_name: &str) -> Result { + self.table_ids + .get(table_name) + .copied() + .ok_or_else(|| PortableDatastoreError::MissingTable(table_name.into())) + } + + /// Resolve an index name to its datastore id. + pub fn index_id(&self, index_name: &str) -> Result { + self.index_ids + .get(index_name) + .copied() + .ok_or_else(|| PortableDatastoreError::MissingIndex(index_name.into())) + } + + /// Begin an explicit mutable transaction. + pub fn begin_mut_tx(&self) -> PortableTransaction { + PortableTransaction { + datastore: self.datastore.clone(), + tx: Some( + self.datastore + .begin_mut_tx(IsolationLevel::Serializable, Workload::Internal), + ), + } + } + + /// Commit this transaction. + pub fn commit_tx(&self, mut tx: PortableTransaction, mode: CommitMode) -> Result<(), PortableDatastoreError> { + let mut inner = tx.take()?; + if mode == CommitMode::DropEventTableRows { + for table_id in &self.event_table_ids { + inner.clear_table(*table_id)?; + } + } + self.datastore.commit_mut_tx(inner)?; + Ok(()) + } + + /// Roll back this transaction. + pub fn rollback_tx(&self, mut tx: PortableTransaction) -> Result<(), PortableDatastoreError> { + if let Some(inner) = tx.tx.take() { + let _ = self.datastore.rollback_mut_tx(inner); + self.datastore.rebuild_sequence_state_from_committed()?; + } + Ok(()) + } + + /// Reset the datastore to the post-bootstrap empty module state. + pub fn reset(&mut self) -> Result<(), PortableDatastoreError> { + *self = Self::from_module_def(self.raw_module_def.clone(), self.module_identity)?; + Ok(()) + } + + /// Return the number of rows in `table_id`. + pub fn table_row_count( + &self, + tx: Option<&PortableTransaction>, + table_id: TableId, + ) -> Result { + match tx { + Some(tx) => self.with_tx(tx, |tx| { + self.datastore + .iter_mut_tx(tx, table_id) + .map(|rows| rows.count() as u64) + .map_err(Into::into) + }), + None => { + let tx = self.datastore.begin_tx(Workload::Internal); + let result = self.datastore.iter_tx(&tx, table_id).map(|rows| rows.count() as u64); + let _ = self.datastore.release_tx(tx); + result.map_err(Into::into) + } + } + } + + /// Collect every row in `table_id` as BSATN-encoded row bytes. + pub fn table_rows_bsatn( + &self, + tx: Option<&PortableTransaction>, + table_id: TableId, + ) -> Result>, PortableDatastoreError> { + match tx { + Some(tx) => self.with_tx(tx, |tx| { + self.datastore + .iter_mut_tx(tx, table_id)? + .map(|row_ref| row_ref.to_bsatn_vec()) + .collect::, _>>() + .map_err(Into::into) + }), + None => { + let tx = self.datastore.begin_tx(Workload::Internal); + let result = (|| { + let rows = self.datastore.iter_tx(&tx, table_id)?; + rows.map(|row_ref| row_ref.to_bsatn_vec()) + .collect::, _>>() + .map_err(PortableDatastoreError::from) + })(); + let _ = self.datastore.release_tx(tx); + result + } + } + } + + /// Collect rows matching a point index scan as BSATN-encoded row bytes. + pub fn index_scan_point_bsatn( + &self, + tx: Option<&PortableTransaction>, + index_id: IndexId, + point: &[u8], + ) -> Result>, PortableDatastoreError> { + match tx { + Some(tx) => self.with_tx(tx, |tx| collect_point_scan(tx, index_id, point)), + None => self.with_rollback_tx(|tx| collect_point_scan(tx, index_id, point)), + } + } + + /// Collect rows matching a range index scan as BSATN-encoded row bytes. + pub fn index_scan_range_bsatn( + &self, + tx: Option<&PortableTransaction>, + index_id: IndexId, + prefix: &[u8], + prefix_elems: ColId, + rstart: &[u8], + rend: &[u8], + ) -> Result>, PortableDatastoreError> { + match tx { + Some(tx) => self.with_tx(tx, |tx| { + collect_range_scan(tx, index_id, prefix, prefix_elems, rstart, rend) + }), + None => self.with_rollback_tx(|tx| collect_range_scan(tx, index_id, prefix, prefix_elems, rstart, rend)), + } + } + + /// Insert a BSATN-encoded row and return BSATN-encoded generated columns. + pub fn insert_bsatn_generated_cols( + &self, + tx: &mut PortableTransaction, + table_id: TableId, + row: &[u8], + ) -> Result, PortableDatastoreError> { + self.with_tx_mut(tx, |tx| { + let (generated_cols, row_ref, _) = self.datastore.insert_mut_tx(tx, table_id, row)?; + Ok(row_ref.project_product(&generated_cols)?.to_bsatn_vec()?) + }) + } + + /// Update a BSATN-encoded row by matching the existing row through `index_id`. + pub fn update_bsatn_generated_cols( + &self, + tx: &mut PortableTransaction, + table_id: TableId, + index_id: IndexId, + row: &[u8], + ) -> Result, PortableDatastoreError> { + self.with_tx_mut(tx, |tx| { + let (generated_cols, row_ref, _) = self.datastore.update_mut_tx(tx, table_id, index_id, row)?; + Ok(row_ref.project_product(&generated_cols)?.to_bsatn_vec()?) + }) + } + + /// Delete rows matching a BSATN-encoded relation. + pub fn delete_by_rel_bsatn( + &self, + tx: &mut PortableTransaction, + table_id: TableId, + relation: &[u8], + ) -> Result { + self.with_tx_mut(tx, |tx| { + let row_ty = self.datastore.row_type_for_table_mut_tx(tx, table_id)?; + let rows = decode_relation(&row_ty, relation)?; + Ok(self.datastore.delete_by_rel_mut_tx(tx, table_id, rows)) + }) + } + + /// Delete rows matching a point index scan. + pub fn delete_by_index_scan_point_bsatn( + &self, + tx: &mut PortableTransaction, + index_id: IndexId, + point: &[u8], + ) -> Result { + self.with_tx_mut(tx, |tx| { + let (table_id, _, iter) = tx.index_scan_point(index_id, point)?; + let rows_to_delete = iter.map(|row_ref| row_ref.pointer()).collect::>(); + Ok(self.datastore.delete_mut_tx(tx, table_id, rows_to_delete)) + }) + } + + /// Delete rows matching a range index scan. + pub fn delete_by_index_scan_range_bsatn( + &self, + tx: &mut PortableTransaction, + index_id: IndexId, + prefix: &[u8], + prefix_elems: ColId, + rstart: &[u8], + rend: &[u8], + ) -> Result { + self.with_tx_mut(tx, |tx| { + let (table_id, iter) = tx.index_scan_range(index_id, prefix, prefix_elems, rstart, rend)?; + let rows_to_delete = match iter { + IndexScanPointOrRange::Point(_, iter) => iter.map(|row_ref| row_ref.pointer()).collect::>(), + IndexScanPointOrRange::Range(iter) => iter.map(|row_ref| row_ref.pointer()).collect::>(), + }; + Ok(self.datastore.delete_mut_tx(tx, table_id, rows_to_delete)) + }) + } + + /// Clear all rows from a table. + pub fn clear_table(&self, tx: &mut PortableTransaction, table_id: TableId) -> Result { + self.with_tx_mut(tx, |tx| Ok(tx.clear_table(table_id)?)) + } + + /// Validate JWT payload claims and derive sender identity. + pub fn validate_jwt_payload( + &self, + payload: &str, + connection_id: ConnectionId, + ) -> Result { + let claims: serde_json::Value = serde_json::from_str(payload)?; + let sender = validate_test_jwt_claims(&claims)?; + Ok(ValidatedAuth { + sender, + connection_id: Some(connection_id), + }) + } + + /// Run a typed querybuilder SQL query and return BSATN-encoded result rows. + pub fn run_query_bsatn( + &self, + sql: &str, + database_identity: Identity, + ) -> Result>, PortableDatastoreError> { + self.with_rollback_tx(|tx| { + let auth = AuthCtx::for_current(database_identity); + let viewer = SchemaViewer::new(tx, &auth); + let statement = + spacetimedb_query::compile_sql_stmt(sql, &viewer, &auth).map_err(PortableDatastoreError::Query)?; + let spacetimedb_expr::statement::Statement::Select(select) = statement else { + return Err(PortableDatastoreError::NonSelectQuery); + }; + let mut metrics = ExecutionMetrics::default(); + let rows = spacetimedb_query::execute_select_stmt(&auth, select, tx, &mut metrics, Ok) + .map_err(PortableDatastoreError::Query)?; + rows.into_iter() + .map(|row| row.to_bsatn_vec().map_err(PortableDatastoreError::from)) + .collect() + }) + } + + fn with_rollback_tx( + &self, + f: impl FnOnce(&::MutTx) -> Result, + ) -> Result { + let tx = self + .datastore + .begin_mut_tx(IsolationLevel::Serializable, Workload::Internal); + let result = f(&tx); + let _ = self.datastore.rollback_mut_tx(tx); + self.datastore.rebuild_sequence_state_from_committed()?; + result + } + + fn with_tx( + &self, + tx: &PortableTransaction, + f: impl FnOnce(&::MutTx) -> Result, + ) -> Result { + let tx = tx + .tx + .as_ref() + .ok_or(PortableDatastoreError::TransactionAlreadyFinished)?; + f(tx) + } + + fn with_tx_mut( + &self, + tx: &mut PortableTransaction, + f: impl FnOnce(&mut ::MutTx) -> Result, + ) -> Result { + let tx = tx + .tx + .as_mut() + .ok_or(PortableDatastoreError::TransactionAlreadyFinished)?; + f(tx) + } +} + +impl PortableTransaction { + fn take(&mut self) -> Result<::MutTx, PortableDatastoreError> { + self.tx.take().ok_or(PortableDatastoreError::TransactionAlreadyFinished) + } +} + +impl Drop for PortableTransaction { + fn drop(&mut self) { + if let Some(tx) = self.tx.take() { + let _ = self.datastore.rollback_mut_tx(tx); + let _ = self.datastore.rebuild_sequence_state_from_committed(); + } + } +} + +/// Errors returned by [`PortableDatastore`]. +#[derive(Debug, Error)] +pub enum PortableDatastoreError { + #[error("invalid module definition: {0}")] + ModuleDef(#[from] ValidationErrors), + #[error("datastore error: {0}")] + Datastore(#[from] DatastoreError), + #[error("missing table `{0}`")] + MissingTable(Box), + #[error("missing index `{0}`")] + MissingIndex(Box), + #[error("invalid generated column projection: {0}")] + InvalidProjection(#[from] spacetimedb_lib::sats::product_value::InvalidFieldError), + #[error("BSATN encode error: {0}")] + BsatnEncode(#[from] EncodeError), + #[error("BSATN decode error: {0}")] + BsatnDecode(#[from] DecodeError), + #[error("invalid JWT payload: {0}")] + InvalidJwtPayload(#[from] serde_json::Error), + #[error("invalid JWT claims: {0}")] + InvalidJwtClaims(#[from] anyhow::Error), + #[error("query error: {0}")] + Query(anyhow::Error), + #[error("only SELECT queries are supported")] + NonSelectQuery, + #[error("invalid relation buffer: {0}")] + InvalidRelation(anyhow::Error), + #[error("transaction already finished")] + TransactionAlreadyFinished, +} + +impl PortableDatastoreError { + /// Convert a datastore insertion error to the public syscall errno shape. + pub fn insert_errno_code(&self) -> Option { + match self { + Self::Datastore(DatastoreError::Index(IndexError::UniqueConstraintViolation(_))) => { + Some(spacetimedb_primitives::errno::UNIQUE_ALREADY_EXISTS.get()) + } + Self::Datastore(DatastoreError::Sequence(SequenceError::UnableToAllocate(_))) => { + Some(spacetimedb_primitives::errno::AUTO_INC_OVERFLOW.get()) + } + _ => None, + } + } +} + +fn collect_point_scan( + tx: &::MutTx, + index_id: IndexId, + point: &[u8], +) -> Result>, PortableDatastoreError> { + let (_, _, iter) = tx.index_scan_point(index_id, point)?; + iter.map(|row_ref| row_ref.to_bsatn_vec()) + .collect::, _>>() + .map_err(Into::into) +} + +fn collect_range_scan( + tx: &::MutTx, + index_id: IndexId, + prefix: &[u8], + prefix_elems: ColId, + rstart: &[u8], + rend: &[u8], +) -> Result>, PortableDatastoreError> { + let (_, iter) = tx.index_scan_range(index_id, prefix, prefix_elems, rstart, rend)?; + match iter { + IndexScanPointOrRange::Point(_, iter) => iter + .map(|row_ref| row_ref.to_bsatn_vec()) + .collect::, _>>() + .map_err(Into::into), + IndexScanPointOrRange::Range(iter) => iter + .map(|row_ref| row_ref.to_bsatn_vec()) + .collect::, _>>() + .map_err(Into::into), + } +} + +fn decode_relation(row_ty: &ProductType, relation: &[u8]) -> Result, PortableDatastoreError> { + let Some((row_count, mut relation)) = relation.split_first_chunk::<4>() else { + return Err(PortableDatastoreError::InvalidRelation(anyhow::anyhow!( + "relation buffer missing row count prefix" + ))); + }; + let row_count = u32::from_le_bytes(*row_count); + (0..row_count) + .map(|_| spacetimedb_lib::bsatn::decode(row_ty, &mut relation).map_err(PortableDatastoreError::from)) + .collect() +} + +struct SchemaViewer<'a, T> { + tx: &'a T, + auth: &'a AuthCtx, +} + +impl<'a, T> SchemaViewer<'a, T> { + fn new(tx: &'a T, auth: &'a AuthCtx) -> Self { + Self { tx, auth } + } +} + +impl spacetimedb_expr::check::SchemaView for SchemaViewer<'_, T> { + fn table_id(&self, name: &str) -> Option { + self.tx + .table_id_from_name_or_alias(name) + .ok() + .flatten() + .and_then(|table_id| self.schema_for_table(table_id)) + .filter(|schema| self.auth.has_read_access(schema.table_access)) + .map(|schema| schema.table_id) + } + + fn schema_for_table(&self, table_id: TableId) -> Option> { + self.tx + .get_schema(table_id) + .filter(|schema| self.auth.has_read_access(schema.table_access)) + .map(Arc::clone) + .map(TableOrViewSchema::from) + .map(Arc::new) + } + + fn rls_rules_for_table(&self, table_id: TableId) -> anyhow::Result>> { + self.tx + .iter_by_col_eq( + ST_ROW_LEVEL_SECURITY_ID, + StRowLevelSecurityFields::TableId, + &AlgebraicValue::from(table_id), + )? + .map(|row| { + row.read_col::(StRowLevelSecurityFields::Sql) + .with_context(|| { + format!( + "Failed to read value from the `sql` column of `st_row_level_security` for table_id `{table_id}`" + ) + }) + .and_then(|sql| { + sql.into_string().map_err(|_| { + anyhow::anyhow!( + "Failed to read string from the `sql` column of `st_row_level_security` for table_id `{table_id}`" + ) + }) + }) + }) + .collect() + } +} + +fn validate_test_jwt_claims(claims: &serde_json::Value) -> anyhow::Result { + let issuer = required_claim(claims, "iss")?; + let subject = required_claim(claims, "sub")?; + + if issuer.len() > 128 { + anyhow::bail!("Issuer too long: {issuer:?}"); + } + if subject.len() > 128 { + anyhow::bail!("Subject too long: {subject:?}"); + } + if issuer.is_empty() { + anyhow::bail!("Issuer empty"); + } + if subject.is_empty() { + anyhow::bail!("Subject empty"); + } + + let computed_identity = Identity::from_claims(issuer, subject); + if let Some(token_identity) = claims.get("hex_identity") { + let token_identity = token_identity + .as_str() + .ok_or_else(|| anyhow::anyhow!("Claim `hex_identity` must be a string")) + .and_then(|hex| { + Identity::from_hex(hex).map_err(|err| anyhow::anyhow!("invalid hex_identity claim: {err}")) + })?; + if token_identity != computed_identity { + anyhow::bail!( + "Identity mismatch: token identity {token_identity:?} does not match computed identity {computed_identity:?}", + ); + } + } + + Ok(computed_identity) +} + +fn required_claim<'a>(claims: &'a serde_json::Value, name: &str) -> anyhow::Result<&'a str> { + claims + .get(name) + .ok_or_else(|| anyhow::anyhow!("Missing `{name}` claim"))? + .as_str() + .ok_or_else(|| anyhow::anyhow!("Claim `{name}` must be a string")) +} + +#[cfg(test)] +mod tests { + use spacetimedb_lib::bsatn; + use spacetimedb_lib::db::raw_def::v10::RawModuleDefV10Builder; + use spacetimedb_lib::db::raw_def::v9::{btree, RawModuleDefV9Builder}; + use spacetimedb_lib::{AlgebraicType, ProductType}; + + use super::*; + + fn raw_module_def() -> RawModuleDef { + let mut builder = RawModuleDefV9Builder::new(); + builder + .build_table_with_new_type( + "person", + ProductType::from([("id", AlgebraicType::I64), ("value", AlgebraicType::I64)]), + true, + ) + .with_unique_constraint(0) + .with_index_no_accessor_name(btree(0)); + builder + .build_table_with_new_type( + "pet", + ProductType::from([ + ("id", AlgebraicType::I64), + ("owner_id", AlgebraicType::I64), + ("name", AlgebraicType::String), + ]), + true, + ) + .with_unique_constraint(0) + .with_index_no_accessor_name(btree(0)); + + RawModuleDef::V9(builder.finish()) + } + + fn event_module_def() -> RawModuleDef { + let mut builder = RawModuleDefV10Builder::new(); + builder + .build_table_with_new_type( + "person", + ProductType::from([("id", AlgebraicType::I64), ("value", AlgebraicType::I64)]), + true, + ) + .finish(); + builder + .build_table_with_new_type( + "event", + ProductType::from([("id", AlgebraicType::I64), ("value", AlgebraicType::I64)]), + true, + ) + .with_event(true) + .finish(); + + RawModuleDef::V10(builder.finish()) + } + + fn counter_module_def() -> RawModuleDef { + let mut builder = RawModuleDefV9Builder::new(); + builder + .build_table_with_new_type( + "counter", + ProductType::from([("id", AlgebraicType::U64), ("name", AlgebraicType::String)]), + true, + ) + .with_auto_inc_primary_key(0) + .with_index_no_accessor_name(btree(0)); + + RawModuleDef::V9(builder.finish()) + } + + fn datastore(raw: RawModuleDef) -> PortableDatastore { + PortableDatastore::from_module_def(raw, Identity::ZERO).unwrap() + } + + fn insert_person(ds: &PortableDatastore, id: i64, value: i64) { + let table_id = ds.table_id("person").unwrap(); + let mut tx = ds.begin_mut_tx(); + ds.insert_bsatn_generated_cols(&mut tx, table_id, &bsatn::to_vec(&(id, value)).unwrap()) + .unwrap(); + ds.commit_tx(tx, CommitMode::Normal).unwrap(); + } + + fn insert_pet(ds: &PortableDatastore, id: i64, owner_id: i64, name: &str) { + let table_id = ds.table_id("pet").unwrap(); + let mut tx = ds.begin_mut_tx(); + ds.insert_bsatn_generated_cols(&mut tx, table_id, &bsatn::to_vec(&(id, owner_id, name)).unwrap()) + .unwrap(); + ds.commit_tx(tx, CommitMode::Normal).unwrap(); + } + + fn relation_row(row: &(i64, i64)) -> Vec { + let mut bytes = bsatn::to_vec(&1_u32).unwrap(); + bytes.extend(bsatn::to_vec(row).unwrap()); + bytes + } + + #[test] + fn from_module_def_creates_distinct_databases() { + let first = datastore(raw_module_def()); + let second = datastore(raw_module_def()); + + insert_person(&first, 1, 10); + + let first_table = first.table_id("person").unwrap(); + let second_table = second.table_id("person").unwrap(); + assert_eq!(first.table_row_count(None, first_table).unwrap(), 1); + assert_eq!(second.table_row_count(None, second_table).unwrap(), 0); + } + + #[test] + fn resolves_tables_and_indexes() { + let ds = datastore(raw_module_def()); + + assert!(ds.table_id("person").is_ok()); + assert!(ds.index_id("person_id_idx_btree").is_ok()); + } + + #[test] + fn insert_scan_and_index_point_scan_work() { + let ds = datastore(raw_module_def()); + let table_id = ds.table_id("person").unwrap(); + let index_id = ds.index_id("person_id_idx_btree").unwrap(); + + insert_person(&ds, 1, 10); + insert_person(&ds, 2, 20); + + assert_eq!(ds.table_row_count(None, table_id).unwrap(), 2); + assert_eq!(ds.table_rows_bsatn(None, table_id).unwrap().len(), 2); + assert_eq!( + ds.index_scan_point_bsatn(None, index_id, &bsatn::to_vec(&1_i64).unwrap()) + .unwrap() + .len(), + 1 + ); + } + + #[test] + fn delete_update_and_clear_work() { + let ds = datastore(raw_module_def()); + let table_id = ds.table_id("person").unwrap(); + let index_id = ds.index_id("person_id_idx_btree").unwrap(); + insert_person(&ds, 1, 10); + insert_person(&ds, 2, 20); + + let mut tx = ds.begin_mut_tx(); + assert_eq!( + ds.delete_by_rel_bsatn(&mut tx, table_id, &relation_row(&(1, 10))) + .unwrap(), + 1 + ); + ds.update_bsatn_generated_cols(&mut tx, table_id, index_id, &bsatn::to_vec(&(2_i64, 22_i64)).unwrap()) + .unwrap(); + ds.commit_tx(tx, CommitMode::Normal).unwrap(); + + assert_eq!(ds.table_row_count(None, table_id).unwrap(), 1); + + let mut tx = ds.begin_mut_tx(); + assert_eq!(ds.clear_table(&mut tx, table_id).unwrap(), 1); + ds.commit_tx(tx, CommitMode::Normal).unwrap(); + assert_eq!(ds.table_row_count(None, table_id).unwrap(), 0); + } + + #[test] + fn unique_constraints_are_enforced() { + let ds = datastore(raw_module_def()); + let table_id = ds.table_id("person").unwrap(); + let mut tx = ds.begin_mut_tx(); + + ds.insert_bsatn_generated_cols(&mut tx, table_id, &bsatn::to_vec(&(1_i64, 10_i64)).unwrap()) + .unwrap(); + let err = ds + .insert_bsatn_generated_cols(&mut tx, table_id, &bsatn::to_vec(&(1_i64, 20_i64)).unwrap()) + .unwrap_err(); + + assert_eq!( + err.insert_errno_code(), + Some(spacetimedb_primitives::errno::UNIQUE_ALREADY_EXISTS.get()) + ); + } + + #[test] + fn auto_inc_sequences_are_materialized() { + let ds = datastore(counter_module_def()); + let table_id = ds.table_id("counter").unwrap(); + let mut tx = ds.begin_mut_tx(); + + let first = ds + .insert_bsatn_generated_cols(&mut tx, table_id, &bsatn::to_vec(&(0_u64, "one")).unwrap()) + .unwrap(); + let second = ds + .insert_bsatn_generated_cols(&mut tx, table_id, &bsatn::to_vec(&(0_u64, "two")).unwrap()) + .unwrap(); + + assert!(!first.is_empty()); + assert!(!second.is_empty()); + assert_ne!(first, second); + } + + #[test] + fn rollback_discards_rows_and_sequence_state() { + let ds = datastore(counter_module_def()); + let table_id = ds.table_id("counter").unwrap(); + + let mut rolled_back = ds.begin_mut_tx(); + let first = ds + .insert_bsatn_generated_cols(&mut rolled_back, table_id, &bsatn::to_vec(&(0_u64, "one")).unwrap()) + .unwrap(); + ds.rollback_tx(rolled_back).unwrap(); + + let mut committed = ds.begin_mut_tx(); + let second = ds + .insert_bsatn_generated_cols(&mut committed, table_id, &bsatn::to_vec(&(0_u64, "one")).unwrap()) + .unwrap(); + ds.commit_tx(committed, CommitMode::Normal).unwrap(); + + assert_eq!(first, second); + assert_eq!(ds.table_row_count(None, table_id).unwrap(), 1); + } + + #[test] + fn transactional_reads_see_pending_writes() { + let ds = datastore(raw_module_def()); + let table_id = ds.table_id("person").unwrap(); + let mut tx = ds.begin_mut_tx(); + + ds.insert_bsatn_generated_cols(&mut tx, table_id, &bsatn::to_vec(&(1_i64, 10_i64)).unwrap()) + .unwrap(); + + assert_eq!(ds.table_row_count(Some(&tx), table_id).unwrap(), 1); + ds.commit_tx(tx, CommitMode::Normal).unwrap(); + assert_eq!(ds.table_row_count(None, table_id).unwrap(), 1); + } + + #[test] + fn event_table_rows_are_dropped_on_reducer_commit() { + let ds = datastore(event_module_def()); + let person = ds.table_id("person").unwrap(); + let event = ds.table_id("event").unwrap(); + let mut tx = ds.begin_mut_tx(); + + ds.insert_bsatn_generated_cols(&mut tx, person, &bsatn::to_vec(&(1_i64, 10_i64)).unwrap()) + .unwrap(); + ds.insert_bsatn_generated_cols(&mut tx, event, &bsatn::to_vec(&(1_i64, 99_i64)).unwrap()) + .unwrap(); + assert_eq!(ds.table_row_count(Some(&tx), event).unwrap(), 1); + ds.commit_tx(tx, CommitMode::DropEventTableRows).unwrap(); + + assert_eq!(ds.table_row_count(None, person).unwrap(), 1); + assert_eq!(ds.table_row_count(None, event).unwrap(), 0); + } + + #[test] + fn invalid_module_def_returns_validation_error() { + let mut builder = RawModuleDefV9Builder::new(); + builder.build_table("broken", spacetimedb_lib::sats::AlgebraicTypeRef(999)); + + let err = match PortableDatastore::from_module_def(RawModuleDef::V9(builder.finish()), Identity::ZERO) { + Ok(_) => panic!("invalid module definition unexpectedly succeeded"), + Err(err) => err, + }; + assert!(matches!(err, PortableDatastoreError::ModuleDef(_))); + } + + #[test] + fn jwt_payload_validation() { + let ds = datastore(raw_module_def()); + let connection_id = ConnectionId::ZERO; + let expected = Identity::from_claims("issuer", "subject"); + let payload = r#"{"iss":"issuer","sub":"subject"}"#; + + let auth = ds.validate_jwt_payload(payload, connection_id).unwrap(); + assert_eq!(auth.sender, expected); + assert_eq!(auth.connection_id, Some(connection_id)); + + assert!(ds.validate_jwt_payload(r#"{"sub":"subject"}"#, connection_id).is_err()); + let mismatch = format!( + r#"{{"iss":"issuer","sub":"subject","hex_identity":"{}"}}"#, + Identity::ZERO.to_hex() + ); + assert!(ds.validate_jwt_payload(&mismatch, connection_id).is_err()); + } + + #[test] + fn run_query_selects_committed_rows() { + let ds = datastore(raw_module_def()); + insert_person(&ds, 1, 10); + insert_person(&ds, 2, 20); + + let rows = ds + .run_query_bsatn(r#"SELECT "person".* FROM "person""#, Identity::ZERO) + .unwrap(); + let decoded = rows + .into_iter() + .map(|row| bsatn::from_slice::<(i64, i64)>(&row).unwrap()) + .collect::>(); + + assert_eq!(decoded, vec![(1, 10), (2, 20)]); + } + + #[test] + fn run_query_applies_where_filters() { + let ds = datastore(raw_module_def()); + insert_person(&ds, 1, 10); + insert_person(&ds, 2, 20); + + let rows = ds + .run_query_bsatn( + r#"SELECT "person".* FROM "person" WHERE "person"."value" = 20"#, + Identity::ZERO, + ) + .unwrap(); + let decoded = rows + .into_iter() + .map(|row| bsatn::from_slice::<(i64, i64)>(&row).unwrap()) + .collect::>(); + + assert_eq!(decoded, vec![(2, 20)]); + } + + #[test] + fn run_query_supports_querybuilder_semijoin_sql() { + let ds = datastore(raw_module_def()); + insert_person(&ds, 1, 10); + insert_person(&ds, 2, 20); + insert_pet(&ds, 1, 2, "Biscuit"); + + let rows = ds + .run_query_bsatn( + r#"SELECT "person".* FROM "pet" JOIN "person" ON "person"."id" = "pet"."owner_id""#, + Identity::ZERO, + ) + .unwrap(); + let decoded = rows + .into_iter() + .map(|row| bsatn::from_slice::<(i64, i64)>(&row).unwrap()) + .collect::>(); + + assert_eq!(decoded, vec![(2, 20)]); + } + + #[test] + fn run_query_rejects_non_select_and_invalid_sql() { + let ds = datastore(raw_module_def()); + + assert!(matches!( + ds.run_query_bsatn(r#"INSERT INTO "person" ("id", "value") VALUES (1, 10)"#, Identity::ZERO), + Err(PortableDatastoreError::NonSelectQuery) + )); + assert!(matches!( + ds.run_query_bsatn("SELECT FROM", Identity::ZERO), + Err(PortableDatastoreError::Query(_)) + )); + } +} diff --git a/crates/runtime/src/sim_std.rs b/crates/runtime/src/sim_std.rs index 59293f1635b..d2370eec911 100644 --- a/crates/runtime/src/sim_std.rs +++ b/crates/runtime/src/sim_std.rs @@ -207,6 +207,10 @@ unsafe extern "C" fn getentropy(buf: *mut u8, buflen: usize) -> i32 { if buflen > 256 { return -1; } + if in_simulation() { + eprintln!("warning: randomness requested; delegating to host OS"); + eprintln!("{}", std::backtrace::Backtrace::force_capture()); + } match unsafe { getrandom(buf, buflen, 0) } { -1 => -1, _ => 0, diff --git a/crates/sats/Cargo.toml b/crates/sats/Cargo.toml index 27696f34ac6..6089713fa90 100644 --- a/crates/sats/Cargo.toml +++ b/crates/sats/Cargo.toml @@ -60,14 +60,12 @@ serde = { workspace = true, optional = true } smallvec.workspace = true thiserror.workspace = true uuid.workspace = true +blake3 = { workspace = true, optional = true } [target.'cfg(not(all(target_arch = "wasm32", target_os = "unknown")))'.dependencies] uuid = { workspace = true, features = ["v4", "v7"] } rand = { workspace = true, features = ["std"] } -# For the `blake3` feature. -blake3 = { workspace = true, optional = true } - [dev-dependencies] ahash.workspace = true bytes.workspace = true diff --git a/crates/table/src/fixed_bit_set.rs b/crates/table/src/fixed_bit_set.rs index a805dc087d7..37c90b65831 100644 --- a/crates/table/src/fixed_bit_set.rs +++ b/crates/table/src/fixed_bit_set.rs @@ -59,8 +59,9 @@ mod internal_unsafe { use super::{BitBlock, DefaultBitBlock}; use crate::{static_assert_align, static_assert_size}; + #[cfg(not(target_pointer_width = "32"))] + use core::mem; use core::{ - mem, ptr::NonNull, slice::{from_raw_parts, from_raw_parts_mut}, }; diff --git a/crates/table/src/page.rs b/crates/table/src/page.rs index cfb66d97f10..052792dd78c 100644 --- a/crates/table/src/page.rs +++ b/crates/table/src/page.rs @@ -36,7 +36,7 @@ use super::{ blob_store::{BlobHash, BlobStore}, fixed_bit_set::FixedBitSet, fixed_bit_set::IterSet, - indexes::{max_rows_in_page, Byte, Bytes, PageOffset, PAGE_HEADER_SIZE, PAGE_SIZE}, + indexes::{max_rows_in_page, Byte, Bytes, PageOffset}, static_assert_size, table::BlobNumBytes, var_len::{is_granule_offset_aligned, VarLenGranule, VarLenGranuleHeader, VarLenMembers, VarLenRef}, @@ -337,7 +337,7 @@ impl MemoryUsage for PageHeader { } } -static_assert_size!(PageHeader, PAGE_HEADER_SIZE); +static_assert_size!(PageHeader, super::indexes::PAGE_HEADER_SIZE); impl PageHeader { /// Returns a new `PageHeader` proper a [`Page`] for holding at most `max_rows_in_page` rows. @@ -457,7 +457,7 @@ impl MemoryUsage for Page { } } -static_assert_size!(Page, PAGE_SIZE); +static_assert_size!(Page, super::indexes::PAGE_SIZE); /// A mutable view of the fixed-len section of a [`Page`]. pub struct FixedView<'page> { diff --git a/crates/table/src/util.rs b/crates/table/src/util.rs index ca74c4167e8..441f6ebb41b 100644 --- a/crates/table/src/util.rs +++ b/crates/table/src/util.rs @@ -17,6 +17,7 @@ pub const fn range_move(r: Range, by: usize) -> Range { #[macro_export] macro_rules! static_assert_size { ($ty:ty, $size:expr) => { + #[cfg(not(target_pointer_width = "32"))] const _: [(); $size] = [(); ::core::mem::size_of::<$ty>()]; }; } diff --git a/crates/test-datastore/Cargo.toml b/crates/test-datastore/Cargo.toml new file mode 100644 index 00000000000..01fd7e6b82c --- /dev/null +++ b/crates/test-datastore/Cargo.toml @@ -0,0 +1,31 @@ +[package] +name = "spacetimedb-test-datastore" +version.workspace = true +edition.workspace = true +license-file = "LICENSE" +description = "In-memory datastore support for SpacetimeDB module unit tests." +rust-version.workspace = true + +[lib] +name = "spacetimedb_test_datastore" +path = "src/lib.rs" +bench = false + +[dependencies] +spacetimedb_core = { package = "spacetimedb-core", path = "../core", version = "=2.7.0", features = ["test"] } +spacetimedb-datastore.workspace = true +spacetimedb-expr.workspace = true +spacetimedb-lib.workspace = true +spacetimedb-primitives.workspace = true +spacetimedb-query.workspace = true +spacetimedb-schema.workspace = true + +anyhow.workspace = true +scopeguard.workspace = true +thiserror.workspace = true + +[dev-dependencies] +spacetimedb-lib = { workspace = true, features = ["test"] } + +[lints] +workspace = true diff --git a/crates/test-datastore/LICENSE b/crates/test-datastore/LICENSE new file mode 100644 index 00000000000..39332526b0c --- /dev/null +++ b/crates/test-datastore/LICENSE @@ -0,0 +1,731 @@ +SPACETIMEDB BUSINESS SOURCE LICENSE AGREEMENT + +Business Source License 1.1 + +Parameters + +Licensor: Clockwork Laboratories, Inc. +Licensed Work: SpacetimeDB 2.1.0 + The Licensed Work is + (c) 2023 Clockwork Laboratories, Inc. + +Additional Use Grant: You may make use of the Licensed Work provided your + application or service uses the Licensed Work with no + more than one SpacetimeDB instance in production and + provided that you do not use the Licensed Work for a + Database Service. + + A “Database Service” is a commercial offering that + allows third parties (other than your employees and + contractors) to access the functionality of the + Licensed Work by creating tables whose schemas are + controlled by such third parties. + +Change Date: 2031-03-20 + +Change License: GNU Affero General Public License v3.0 with a linking + exception + +For information about alternative licensing arrangements for the Software, +please visit: https://spacetimedb.com + +Notice + +The Business Source License (this document, or the “License”) is not an Open +Source license. However, the Licensed Work will eventually be made available +under an Open Source License, as stated in this License. + +License text copyright (c) 2017 MariaDB Corporation Ab, All Rights Reserved. +“Business Source License” is a trademark of MariaDB Corporation Ab. + +----------------------------------------------------------------------------- + +Business Source License 1.1 + +Terms + +The Licensor hereby grants you the right to copy, modify, create derivative +works, redistribute, and make non-production use of the Licensed Work. The +Licensor may make an Additional Use Grant, above, permitting limited +production use. + +Effective on the Change Date, or the fourth anniversary of the first publicly +available distribution of a specific version of the Licensed Work under this +License, whichever comes first, the Licensor hereby grants you rights under +the terms of the Change License, and the rights granted in the paragraph +above terminate. + +If your use of the Licensed Work does not comply with the requirements +currently in effect as described in this License, you must purchase a +commercial license from the Licensor, its affiliated entities, or authorized +resellers, or you must refrain from using the Licensed Work. + +All copies of the original and modified Licensed Work, and derivative works +of the Licensed Work, are subject to this License. This License applies +separately for each version of the Licensed Work and the Change Date may vary +for each version of the Licensed Work released by Licensor. + +You must conspicuously display this License on each original or modified copy +of the Licensed Work. If you receive the Licensed Work in original or +modified form from a third party, the terms and conditions set forth in this +License apply to your use of that work. + +Any use of the Licensed Work in violation of this License will automatically +terminate your rights under this License for the current and all other +versions of the Licensed Work. + +This License does not grant you any right in any trademark or logo of +Licensor or its affiliates (provided that you may use a trademark or logo of +Licensor as expressly required by this License). + +TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON +AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, +EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND +TITLE. + +MariaDB hereby grants you permission to use this License’s text to license +your works, and to refer to it using the trademark “Business Source License”, +as long as you comply with the Covenants of Licensor below. + +Covenants of Licensor + +In consideration of the right to use this License’s text and the “Business +Source License” name and trademark, Licensor covenants to MariaDB, and to all +other recipients of the licensed work to be provided by Licensor: + +1. To specify as the Change License the GPL Version 2.0 or any later version, + or a license that is compatible with GPL Version 2.0 or a later version, + where “compatible” means that software provided under the Change License can + be included in a program with software provided under GPL Version 2.0 or a + later version. Licensor may specify additional Change Licenses without + limitation. + +2. To either: (a) specify an additional grant of rights to use that does not + impose any additional restriction on the right granted in this License, as + the Additional Use Grant; or (b) insert the text “None”. + +3. To specify a Change Date. + +4. Not to modify this License in any other way. + +----------------------------------------------------------------------------- + +Copyright (C) 2023 Clockwork Laboratories, Inc. + +This program is free software: you can redistribute it and/or modify it under +the terms of the GNU Affero General Public License, version 3, as published +by the Free Software Foundation. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +details. + +You should have received a copy of the GNU Affero General Public License +along with this program; if not, see . + +Additional permission under GNU GPL version 3 section 7 + +If you modify this Program, or any covered work, by linking or combining it +with SpacetimeDB (or a modified version of that library), containing parts +covered by the terms of the AGPL v3.0, the licensors of this Program grant +you additional permission to convey the resulting work. + +Additional permission under GNU AGPL version 3 section 13 + +If you modify this Program, or any covered work, by linking or combining it +with SpacetimeDB (or a modified version of that library), containing parts +covered by the terms of the AGPL v3.0, the licensors of this Program grant +you additional permission that, notwithstanding any other provision of this +License, you need not prominently offer all users interacting with your +modified version remotely through a computer network an opportunity to +receive the Corresponding Source of your version from a network server at no +charge, if your version supports such interaction. This permission does not +waive or modify any other obligations or terms of the AGPL v3.0, except for +the specific requirement set forth in section 13. + +A copy of the AGPL v3.0 license is reproduced below. + + GNU AFFERO GENERAL PUBLIC LICENSE + Version 3, 19 November 2007 + +Copyright © 2007 Free Software Foundation, Inc. +Everyone is permitted to copy and distribute verbatim copies of this license +document, but changing it is not allowed. + +Preamble +The GNU Affero General Public License is a free, copyleft license for +software and other kinds of works, specifically designed to ensure +cooperation with the community in the case of network server software. + +The licenses for most software and other practical works are designed to take +away your freedom to share and change the works. By contrast, our General +Public Licenses are intended to guarantee your freedom to share and change +all versions of a program--to make sure it remains free software for all its +users. + +When we speak of free software, we are referring to freedom, not price. Our +General Public Licenses are designed to make sure that you have the freedom +to distribute copies of free software (and charge for them if you wish), that +you receive source code or can get it if you want it, that you can change the +software or use pieces of it in new free programs, and that you know you can +do these things. + +Developers that use our General Public Licenses protect your rights with two +steps: (1) assert copyright on the software, and (2) offer you this License +which gives you legal permission to copy, distribute and/or modify the +software. + +A secondary benefit of defending all users' freedom is that improvements made +in alternate versions of the program, if they receive widespread use, become +available for other developers to incorporate. Many developers of free +software are heartened and encouraged by the resulting cooperation. However, +in the case of software used on network servers, this result may fail to come +about. The GNU General Public License permits making a modified version and +letting the public access it on a server without ever releasing its source +code to the public. + +The GNU Affero General Public License is designed specifically to ensure +that, in such cases, the modified source code becomes available to the +community. It requires the operator of a network server to provide the source +code of the modified version running there to the users of that server. +Therefore, public use of a modified version, on a publicly accessible server, +gives the public access to the source code of the modified version. + +An older license, called the Affero General Public License and published by +Affero, was designed to accomplish similar goals. This is a different +license, not a version of the Affero GPL, but Affero has released a new +version of the Affero GPL which permits relicensing under this license. + +The precise terms and conditions for copying, distribution and modification +follow. + +TERMS AND CONDITIONS +0. Definitions. +"This License" refers to version 3 of the GNU Affero General Public License. + +"Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + +"The Program" refers to any copyrightable work licensed under this License. +Each licensee is addressed as "you". "Licensees" and "recipients" may be +individuals or organizations. + +To "modify" a work means to copy from or adapt all or part of the work in a +fashion requiring copyright permission, other than the making of an exact +copy. The resulting work is called a "modified version" of the earlier work +or a work "based on" the earlier work. + +A "covered work" means either the unmodified Program or a work based on the +Program. + +To "propagate" a work means to do anything with it that, without permission, +would make you directly or secondarily liable for infringement under +applicable copyright law, except executing it on a computer or modifying a +private copy. Propagation includes copying, distribution (with or without +modification), making available to the public, and in some countries other +activities as well. + +To "convey" a work means any kind of propagation that enables other parties +to make or receive copies. Mere interaction with a user through a computer +network, with no transfer of a copy, is not conveying. + +An interactive user interface displays "Appropriate Legal Notices" to the +extent that it includes a convenient and prominently visible feature that (1) +displays an appropriate copyright notice, and (2) tells the user that there +is no warranty for the work (except to the extent that warranties are +provided), that licensees may convey the work under this License, and how to +view a copy of this License. If the interface presents a list of user +commands or options, such as a menu, a prominent item in the list meets this +criterion. + +1. Source Code. +The "source code" for a work means the preferred form of the work for making +modifications to it. "Object code" means any non-source form of a work. + +A "Standard Interface" means an interface that either is an official standard +defined by a recognized standards body, or, in the case of interfaces +specified for a particular programming language, one that is widely used +among developers working in that language. + +The "System Libraries" of an executable work include anything, other than the +work as a whole, that (a) is included in the normal form of packaging a Major +Component, but which is not part of that Major Component, and (b) serves only +to enable use of the work with that Major Component, or to implement a +Standard Interface for which an implementation is available to the public in +source code form. A "Major Component", in this context, means a major +essential component (kernel, window system, and so on) of the specific +operating system (if any) on which the executable work runs, or a compiler +used to produce the work, or an object code interpreter used to run it. + +The "Corresponding Source" for a work in object code form means all the +source code needed to generate, install, and (for an executable work) run the +object code and to modify the work, including scripts to control those +activities. However, it does not include the work's System Libraries, or +general-purpose tools or generally available free programs which are used +unmodified in performing those activities but which are not part of the work. +For example, Corresponding Source includes interface definition files +associated with source files for the work, and the source code for shared +libraries and dynamically linked subprograms that the work is specifically +designed to require, such as by intimate data communication or control flow +between those subprograms and other parts of the work. + +The Corresponding Source need not include anything that users can regenerate +automatically from other parts of the Corresponding Source. + +The Corresponding Source for a work in source code form is that same work. + +2. Basic Permissions. +All rights granted under this License are granted for the term of copyright +on the Program, and are irrevocable provided the stated conditions are met. +This License explicitly affirms your unlimited permission to run the +unmodified Program. The output from running a covered work is covered by this +License only if the output, given its content, constitutes a covered work. +This License acknowledges your rights of fair use or other equivalent, as +provided by copyright law. + +You may make, run and propagate covered works that you do not convey, without +conditions so long as your license otherwise remains in force. You may convey +covered works to others for the sole purpose of having them make +modifications exclusively for you, or provide you with facilities for running +those works, provided that you comply with the terms of this License in +conveying all material for which you do not control copyright. Those thus +making or running the covered works for you must do so exclusively on your +behalf, under your direction and control, on terms that prohibit them from +making any copies of your copyrighted material outside their relationship +with you. + +Conveying under any other circumstances is permitted solely under the +conditions stated below. Sublicensing is not allowed; section 10 makes it +unnecessary. + +3. Protecting Users' Legal Rights From Anti-Circumvention Law. +No covered work shall be deemed part of an effective technological measure +under any applicable law fulfilling obligations under article 11 of the WIPO +copyright treaty adopted on 20 December 1996, or similar laws prohibiting or +restricting circumvention of such measures. + +When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention is +effected by exercising rights under this License with respect to the covered +work, and you disclaim any intention to limit operation or modification of +the work as a means of enforcing, against the work's users, your or third +parties' legal rights to forbid circumvention of technological measures. + +4. Conveying Verbatim Copies. +You may convey verbatim copies of the Program's source code as you receive +it, in any medium, provided that you conspicuously and appropriately publish +on each copy an appropriate copyright notice; keep intact all notices stating +that this License and any non-permissive terms added in accord with section 7 +apply to the code; keep intact all notices of the absence of any warranty; +and give all recipients a copy of this License along with the Program. + +You may charge any price or no price for each copy that you convey, and you +may offer support or warranty protection for a fee. + +5. Conveying Modified Source Versions. +You may convey a work based on the Program, or the modifications to produce +it from the Program, in the form of source code under the terms of section 4, +provided that you also meet all of these conditions: + +a) The work must carry prominent notices stating that you modified it, and +giving a relevant date. +b) The work must carry prominent notices stating that it is released under +this License and any conditions added under section 7. This requirement +modifies the requirement in section 4 to "keep intact all notices". +c) You must license the entire work, as a whole, under this License to anyone +who comes into possession of a copy. This License will therefore apply, along +with any applicable section 7 additional terms, to the whole of the work, and +all its parts, regardless of how they are packaged. This License gives no +permission to license the work in any other way, but it does not invalidate +such permission if you have separately received it. +d) If the work has interactive user interfaces, each must display Appropriate +Legal Notices; however, if the Program has interactive interfaces that do not +display Appropriate Legal Notices, your work need not make them do so. +A compilation of a covered work with other separate and independent works, +which are not by their nature extensions of the covered work, and which are +not combined with it such as to form a larger program, in or on a volume of a +storage or distribution medium, is called an "aggregate" if the compilation +and its resulting copyright are not used to limit the access or legal rights +of the compilation's users beyond what the individual works permit. Inclusion +of a covered work in an aggregate does not cause this License to apply to the +other parts of the aggregate. + +6. Conveying Non-Source Forms. +You may convey a covered work in object code form under the terms of sections +4 and 5, provided that you also convey the machine-readable Corresponding +Source under the terms of this License, in one of these ways: + +a) Convey the object code in, or embodied in, a physical product (including a +physical distribution medium), accompanied by the Corresponding Source fixed +on a durable physical medium customarily used for software interchange. +b) Convey the object code in, or embodied in, a physical product (including a +physical distribution medium), accompanied by a written offer, valid for at +least three years and valid for as long as you offer spare parts or customer +support for that product model, to give anyone who possesses the object code +either (1) a copy of the Corresponding Source for all the software in the +product that is covered by this License, on a durable physical medium +customarily used for software interchange, for a price no more than your +reasonable cost of physically performing this conveying of source, or (2) +access to copy the Corresponding Source from a network server at no charge. +c) Convey individual copies of the object code with a copy of the written +offer to provide the Corresponding Source. This alternative is allowed only +occasionally and noncommercially, and only if you received the object code +with such an offer, in accord with subsection 6b. +d) Convey the object code by offering access from a designated place (gratis +or for a charge), and offer equivalent access to the Corresponding Source in +the same way through the same place at no further charge. You need not +require recipients to copy the Corresponding Source along with the object +code. If the place to copy the object code is a network server, the +Corresponding Source may be on a different server (operated by you or a third +party) that supports equivalent copying facilities, provided you maintain +clear directions next to the object code saying where to find the +Corresponding Source. Regardless of what server hosts the Corresponding +Source, you remain obligated to ensure that it is available for as long as +needed to satisfy these requirements. +e) Convey the object code using peer-to-peer transmission, provided you +inform other peers where the object code and Corresponding Source of the work +are being offered to the general public at no charge under subsection 6d. +A separable portion of the object code, whose source code is excluded from +the Corresponding Source as a System Library, need not be included in +conveying the object code work. + +A "User Product" is either (1) a "consumer product", which means any tangible +personal property which is normally used for personal, family, or household +purposes, or (2) anything designed or sold for incorporation into a dwelling. +In determining whether a product is a consumer product, doubtful cases shall +be resolved in favor of coverage. For a particular product received by a +particular user, "normally used" refers to a typical or common use of that +class of product, regardless of the status of the particular user or of the +way in which the particular user actually uses, or expects or is expected to +use, the product. A product is a consumer product regardless of whether the +product has substantial commercial, industrial or non-consumer uses, unless +such uses represent the only significant mode of use of the product. + +"Installation Information" for a User Product means any methods, procedures, +authorization keys, or other information required to install and execute +modified versions of a covered work in that User Product from a modified +version of its Corresponding Source. The information must suffice to ensure +that the continued functioning of the modified object code is in no case +prevented or interfered with solely because modification has been made. + +If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as part of +a transaction in which the right of possession and use of the User Product is +transferred to the recipient in perpetuity or for a fixed term (regardless of +how the transaction is characterized), the Corresponding Source conveyed +under this section must be accompanied by the Installation Information. But +this requirement does not apply if neither you nor any third party retains +the ability to install modified object code on the User Product (for example, +the work has been installed in ROM). + +The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates for +a work that has been modified or installed by the recipient, or for the User +Product in which it has been modified or installed. Access to a network may +be denied when the modification itself materially and adversely affects the +operation of the network or violates the rules and protocols for +communication across the network. + +Corresponding Source conveyed, and Installation Information provided, in +accord with this section must be in a format that is publicly documented (and +with an implementation available to the public in source code form), and must +require no special password or key for unpacking, reading or copying. + +7. Additional Terms. +"Additional permissions" are terms that supplement the terms of this License +by making exceptions from one or more of its conditions. Additional +permissions that are applicable to the entire Program shall be treated as +though they were included in this License, to the extent that they are valid +under applicable law. If additional permissions apply only to part of the +Program, that part may be used separately under those permissions, but the +entire Program remains governed by this License without regard to the +additional permissions. + +When you convey a copy of a covered work, you may at your option remove any +additional permissions from that copy, or from any part of it. (Additional +permissions may be written to require their own removal in certain cases when +you modify the work.) You may place additional permissions on material, added +by you to a covered work, for which you have or can give appropriate +copyright permission. + +Notwithstanding any other provision of this License, for material you add to +a covered work, you may (if authorized by the copyright holders of that +material) supplement the terms of this License with terms: + +a) Disclaiming warranty or limiting liability differently from the terms of +sections 15 and 16 of this License; or +b) Requiring preservation of specified reasonable legal notices or author +attributions in that material or in the Appropriate Legal Notices displayed +by works containing it; or +c) Prohibiting misrepresentation of the origin of that material, or requiring +that modified versions of such material be marked in reasonable ways as +different from the original version; or +d) Limiting the use for publicity purposes of names of licensors or authors +of the material; or +e) Declining to grant rights under trademark law for use of some trade names, +trademarks, or service marks; or +f) Requiring indemnification of licensors and authors of that material by +anyone who conveys the material (or modified versions of it) with contractual +assumptions of liability to the recipient, for any liability that these +contractual assumptions directly impose on those licensors and authors. +All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is governed +by this License along with a term that is a further restriction, you may +remove that term. If a license document contains a further restriction but +permits relicensing or conveying under this License, you may add to a covered +work material governed by the terms of that license document, provided that +the further restriction does not survive such relicensing or conveying. + +If you add terms to a covered work in accord with this section, you must +place, in the relevant source files, a statement of the additional terms that +apply to those files, or a notice indicating where to find the applicable +terms. + +Additional terms, permissive or non-permissive, may be stated in the form of +a separately written license, or stated as exceptions; the above requirements +apply either way. + +8. Termination. +You may not propagate or modify a covered work except as expressly provided +under this License. Any attempt otherwise to propagate or modify it is void, +and will automatically terminate your rights under this License (including +any patent licenses granted under the third paragraph of section 11). + +However, if you cease all violation of this License, then your license from a +particular copyright holder is reinstated (a) provisionally, unless and until +the copyright holder explicitly and finally terminates your license, and (b) +permanently, if the copyright holder fails to notify you of the violation by +some reasonable means prior to 60 days after the cessation. + +Moreover, your license from a particular copyright holder is reinstated +permanently if the copyright holder notifies you of the violation by some +reasonable means, this is the first time you have received notice of +violation of this License (for any work) from that copyright holder, and you +cure the violation prior to 30 days after your receipt of the notice. + +Termination of your rights under this section does not terminate the licenses +of parties who have received copies or rights from you under this License. If +your rights have been terminated and not permanently reinstated, you do not +qualify to receive new licenses for the same material under section 10. + +9. Acceptance Not Required for Having Copies. +You are not required to accept this License in order to receive or run a copy +of the Program. Ancillary propagation of a covered work occurring solely as a +consequence of using peer-to-peer transmission to receive a copy likewise +does not require acceptance. However, nothing other than this License grants +you permission to propagate or modify any covered work. These actions +infringe copyright if you do not accept this License. Therefore, by modifying +or propagating a covered work, you indicate your acceptance of this License +to do so. + +10. Automatic Licensing of Downstream Recipients. +Each time you convey a covered work, the recipient automatically receives a +license from the original licensors, to run, modify and propagate that work, +subject to this License. You are not responsible for enforcing compliance by +third parties with this License. + +An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered work +results from an entity transaction, each party to that transaction who +receives a copy of the work also receives whatever licenses to the work the +party's predecessor in interest had or could give under the previous +paragraph, plus a right to possession of the Corresponding Source of the work +from the predecessor in interest, if the predecessor has it or can get it +with reasonable efforts. + +You may not impose any further restrictions on the exercise of the rights +granted or affirmed under this License. For example, you may not impose a +license fee, royalty, or other charge for exercise of rights granted under +this License, and you may not initiate litigation (including a cross-claim or +counterclaim in a lawsuit) alleging that any patent claim is infringed by +making, using, selling, offering for sale, or importing the Program or any +portion of it. + +11. Patents. +A "contributor" is a copyright holder who authorizes use under this License +of the Program or a work on which the Program is based. The work thus +licensed is called the contributor's "contributor version". + +A contributor's "essential patent claims" are all patent claims owned or +controlled by the contributor, whether already acquired or hereafter +acquired, that would be infringed by some manner, permitted by this License, +of making, using, or selling its contributor version, but do not include +claims that would be infringed only as a consequence of further modification +of the contributor version. For purposes of this definition, "control" +includes the right to grant patent sublicenses in a manner consistent with +the requirements of this License. + +Each contributor grants you a non-exclusive, worldwide, royalty-free patent +license under the contributor's essential patent claims, to make, use, sell, +offer for sale, import and otherwise run, modify and propagate the contents +of its contributor version. + +In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent (such +as an express permission to practice a patent or covenant not to sue for +patent infringement). To "grant" such a patent license to a party means to +make such an agreement or commitment not to enforce a patent against the +party. + +If you convey a covered work, knowingly relying on a patent license, and the +Corresponding Source of the work is not available for anyone to copy, free of +charge and under the terms of this License, through a publicly available +network server or other readily accessible means, then you must either (1) +cause the Corresponding Source to be so available, or (2) arrange to deprive +yourself of the benefit of the patent license for this particular work, or +(3) arrange, in a manner consistent with the requirements of this License, to +extend the patent license to downstream recipients. "Knowingly relying" means +you have actual knowledge that, but for the patent license, your conveying +the covered work in a country, or your recipient's use of the covered work in +a country, would infringe one or more identifiable patents in that country +that you have reason to believe are valid. + +If, pursuant to or in connection with a single transaction or arrangement, +you convey, or propagate by procuring conveyance of, a covered work, and +grant a patent license to some of the parties receiving the covered work +authorizing them to use, propagate, modify or convey a specific copy of the +covered work, then the patent license you grant is automatically extended to +all recipients of the covered work and works based on it. + +A patent license is "discriminatory" if it does not include within the scope +of its coverage, prohibits the exercise of, or is conditioned on the +non-exercise of one or more of the rights that are specifically granted under +this License. You may not convey a covered work if you are a party to an +arrangement with a third party that is in the business of distributing +software, under which you make payment to the third party based on the extent +of your activity of conveying the work, and under which the third party +grants, to any of the parties who would receive the covered work from you, a +discriminatory patent license (a) in connection with copies of the covered +work conveyed by you (or copies made from those copies), or (b) primarily for +and in connection with specific products or compilations that contain the +covered work, unless you entered into that arrangement, or that patent +license was granted, prior to 28 March 2007. + +Nothing in this License shall be construed as excluding or limiting any +implied license or other defenses to infringement that may otherwise be +available to you under applicable patent law. + +12. No Surrender of Others' Freedom. +If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not excuse +you from the conditions of this License. If you cannot convey a covered work +so as to satisfy simultaneously your obligations under this License and any +other pertinent obligations, then as a consequence you may not convey it at +all. For example, if you agree to terms that obligate you to collect a +royalty for further conveying from those to whom you convey the Program, the +only way you could satisfy both those terms and this License would be to +refrain entirely from conveying the Program. + +13. Remote Network Interaction; Use with the GNU General Public License. +Notwithstanding any other provision of this License, if you modify the +Program, your modified version must prominently offer all users interacting +with it remotely through a computer network (if your version supports such +interaction) an opportunity to receive the Corresponding Source of your +version by providing access to the Corresponding Source from a network server +at no charge, through some standard or customary means of facilitating +copying of software. This Corresponding Source shall include the +Corresponding Source for any work covered by version 3 of the GNU General +Public License that is incorporated pursuant to the following paragraph. + +Notwithstanding any other provision of this License, you have permission to +link or combine any covered work with a work licensed under version 3 of the +GNU General Public License into a single combined work, and to convey the +resulting work. The terms of this License will continue to apply to the part +which is the covered work, but the work with which it is combined will remain +governed by version 3 of the GNU General Public License. + +14. Revised Versions of this License. +The Free Software Foundation may publish revised and/or new versions of the +GNU Affero General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies that a certain numbered version of the GNU Affero General Public +License "or any later version" applies to it, you have the option of +following the terms and conditions either of that numbered version or of any +later version published by the Free Software Foundation. If the Program does +not specify a version number of the GNU Affero General Public License, you +may choose any version ever published by the Free Software Foundation. + +If the Program specifies that a proxy can decide which future versions of the +GNU Affero General Public License can be used, that proxy's public statement +of acceptance of a version permanently authorizes you to choose that version +for the Program. + +Later license versions may give you additional or different permissions. +However, no additional obligations are imposed on any author or copyright +holder as a result of your choosing to follow a later version. + +15. Disclaimer of Warranty. +THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE +LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, +EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE +ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. +SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY +SERVICING, REPAIR OR CORRECTION. + +16. Limitation of Liability. +IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL +ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS THE +PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE +OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR +DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR +A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH +HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. + +17. Interpretation of Sections 15 and 16. +If the disclaimer of warranty and limitation of liability provided above +cannot be given local legal effect according to their terms, reviewing courts +shall apply local law that most closely approximates an absolute waiver of +all civil liability in connection with the Program, unless a warranty or +assumption of liability accompanies a copy of the Program in return for a +fee. + +END OF TERMS AND CONDITIONS + +How to Apply These Terms to Your New Programs +If you develop a new program, and you want it to be of the greatest possible +use to the public, the best way to achieve this is to make it free software +which everyone can redistribute and change under these terms. + +To do so, attach the following notices to the program. It is safest to attach +them to the start of each source file to most effectively state the exclusion +of warranty; and each file should have at least the "copyright" line and a +pointer to where the full notice is found. + +SpacetimeDB: A database which replaces your server. +Copyright (C) 2023 Clockwork Laboratories, Inc. + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU Affero General Public License as +published by the Free Software Foundation, either version 3 of the +License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Affero General Public License for more details. + +You should have received a copy of the GNU Affero General Public License +along with this program. If not, see . +Also add information on how to contact you by electronic and paper mail. + +If your software can interact with users remotely through a computer network, +you should also make sure that it provides a way for users to get its source. +For example, if your program is a web application, its interface could +display a "Source" link that leads users to an archive of the code. There are +many ways you could offer source, and different solutions will be better for +different programs; see section 13 for the specific requirements. + +You should also get your employer (if you work as a programmer) or school, if +any, to sign a "copyright disclaimer" for the program, if necessary. For more +information on this, and how to apply and follow the GNU AGPL, see +. diff --git a/crates/test-datastore/src/lib.rs b/crates/test-datastore/src/lib.rs new file mode 100644 index 00000000000..0ab51672a9f --- /dev/null +++ b/crates/test-datastore/src/lib.rs @@ -0,0 +1,673 @@ +//! In-memory datastore support for SpacetimeDB module unit tests. + +#[cfg(target_arch = "wasm32")] +compile_error!("spacetimedb-test-datastore is only supported for native module tests"); + +use std::cell::RefCell; +use std::sync::Arc; + +use spacetimedb_core::db::relational_db::{MutTx, RelationalDB, Tx}; +use spacetimedb_core::db::sql::ast::SchemaViewer; +use spacetimedb_core::error::{DBError, DatastoreError, IndexError, SequenceError}; +use spacetimedb_core::estimation::{check_row_limit, estimate_rows_scanned}; +use spacetimedb_datastore::locking_tx_datastore::IndexScanPointOrRange; +use spacetimedb_datastore::{execution_context::Workload, traits::IsolationLevel}; +use spacetimedb_expr::statement::Statement; +use spacetimedb_lib::bsatn::EncodeError; +use spacetimedb_lib::bsatn::ToBsatn; +use spacetimedb_lib::identity::AuthCtx; +use spacetimedb_lib::metrics::ExecutionMetrics; +use spacetimedb_lib::{Identity, ProductValue, RawModuleDef}; +use spacetimedb_primitives::{ColId, IndexId, TableId}; +use spacetimedb_query::{compile_sql_stmt, execute_select_stmt}; +use spacetimedb_schema::def::ModuleDef; +use spacetimedb_schema::error::ValidationErrors; +use spacetimedb_schema::schema::{Schema, TableSchema}; +use thiserror::Error; + +/// A [`RelationalDB`] initialized from a module definition for native unit tests. +#[derive(Debug)] +pub struct TestDatastore { + db: Arc, + module_def: ModuleDef, +} + +/// A single pending mutable transaction for procedure unit tests. +pub struct TestTransaction { + db: Arc, + tx: RefCell>, +} + +impl TestDatastore { + /// Create an in-memory datastore initialized with the tables in `raw`. + pub fn from_module_def(raw: RawModuleDef) -> Result { + let module_def = ModuleDef::try_from(raw)?; + let test_db = spacetimedb_core::db::relational_db::tests_utils::TestDB::in_memory()?; + let db = test_db.db; + + spacetimedb_core::db::relational_db::tests_utils::with_auto_commit(&db, |tx| { + for table in module_def.tables() { + let schema = TableSchema::from_module_def(&module_def, table, (), TableId::SENTINEL); + db.create_table(tx, schema)?; + } + Ok::<(), TestDatastoreError>(()) + })?; + + Ok(Self { db, module_def }) + } + + /// The underlying in-memory relational database. + pub fn relational_db(&self) -> &Arc { + &self.db + } + + /// The validated module definition used to initialize this datastore. + pub fn module_def(&self) -> &ModuleDef { + &self.module_def + } + + /// Resolve a table name to its datastore id. + pub fn table_id(&self, table_name: &str) -> Result { + let id = spacetimedb_core::db::relational_db::tests_utils::with_read_only(&self.db, |tx| { + self.db.table_id_from_name(tx, table_name) + })?; + + id.ok_or_else(|| TestDatastoreError::MissingTable(table_name.into())) + } + + /// Resolve an index name to its datastore id. + pub fn index_id(&self, index_name: &str) -> Result { + self.with_auto_commit(|tx| { + let id = self.db.index_id_from_name(tx, index_name)?; + id.ok_or_else(|| TestDatastoreError::MissingIndex(index_name.into())) + }) + } + + /// Execute a read-only operation against the datastore. + pub fn with_read_only(&self, f: impl FnOnce(&mut Tx) -> T) -> T { + spacetimedb_core::db::relational_db::tests_utils::with_read_only(&self.db, f) + } + + /// Execute a mutable operation and commit it if `f` succeeds. + pub fn with_auto_commit( + &self, + f: impl FnOnce(&mut MutTx) -> Result, + ) -> Result { + spacetimedb_core::db::relational_db::tests_utils::with_auto_commit(&self.db, f) + } + + /// Begin an explicit mutable transaction. + /// + /// The transaction must be committed or rolled back. If it is dropped while + /// still pending, it rolls back. + pub fn begin_mut_tx(&self) -> TestTransaction { + TestTransaction { + db: self.db.clone(), + tx: RefCell::new(Some(spacetimedb_core::db::relational_db::tests_utils::begin_mut_tx( + &self.db, + ))), + } + } + + /// Insert a BSATN-encoded row into `table_id`. + pub fn insert_bsatn(&self, table_id: TableId, row: &[u8]) -> Result, TestDatastoreError> { + self.insert_bsatn_generated_cols(table_id, row) + } + + /// Insert a BSATN-encoded row and return BSATN-encoded generated columns. + pub fn insert_bsatn_generated_cols(&self, table_id: TableId, row: &[u8]) -> Result, TestDatastoreError> { + self.with_auto_commit(|tx| { + let (generated_cols, row_ref, _) = self.db.insert(tx, table_id, row)?; + Ok(row_ref.project_product(&generated_cols)?.to_bsatn_vec()?) + }) + } + + /// Return the number of rows in `table_id`. + pub fn table_row_count(&self, table_id: TableId) -> Result { + self.with_auto_commit(|tx| { + self.db + .table_row_count_mut(tx, table_id) + .ok_or(TestDatastoreError::MissingTableId(table_id)) + }) + } + + /// Collect every row in `table_id` as [`ProductValue`]s. + pub fn table_rows(&self, table_id: TableId) -> Result, TestDatastoreError> { + spacetimedb_core::db::relational_db::tests_utils::with_read_only(&self.db, |tx| { + let rows = self + .db + .iter(tx, table_id)? + .map(|row_ref| row_ref.to_product_value()) + .collect(); + Ok(rows) + }) + } + + /// Collect every row in `table_id` as BSATN-encoded row bytes. + pub fn table_rows_bsatn(&self, table_id: TableId) -> Result>, TestDatastoreError> { + spacetimedb_core::db::relational_db::tests_utils::with_read_only(&self.db, |tx| { + let rows = self + .db + .iter(tx, table_id)? + .map(|row_ref| row_ref.to_bsatn_vec()) + .collect::, _>>()?; + Ok(rows) + }) + } + + /// Collect rows matching a point index scan as BSATN-encoded row bytes. + pub fn index_scan_point_bsatn(&self, index_id: IndexId, point: &[u8]) -> Result>, TestDatastoreError> { + self.with_auto_commit(|tx| { + let (_, _, iter) = self.db.index_scan_point(tx, index_id, point)?; + iter.map(|row_ref| row_ref.to_bsatn_vec()) + .collect::, _>>() + .map_err(TestDatastoreError::from) + }) + } + + /// Collect rows matching a range index scan as BSATN-encoded row bytes. + pub fn index_scan_range_bsatn( + &self, + index_id: IndexId, + prefix: &[u8], + prefix_elems: ColId, + rstart: &[u8], + rend: &[u8], + ) -> Result>, TestDatastoreError> { + self.with_auto_commit(|tx| { + let (_, iter) = self + .db + .index_scan_range(tx, index_id, prefix, prefix_elems, rstart, rend)?; + match iter { + IndexScanPointOrRange::Point(_, iter) => iter + .map(|row_ref| row_ref.to_bsatn_vec()) + .collect::, _>>() + .map_err(TestDatastoreError::from), + IndexScanPointOrRange::Range(iter) => iter + .map(|row_ref| row_ref.to_bsatn_vec()) + .collect::, _>>() + .map_err(TestDatastoreError::from), + } + }) + } + + /// Delete rows matching a point index scan. + pub fn delete_by_index_scan_point_bsatn(&self, index_id: IndexId, point: &[u8]) -> Result { + self.with_auto_commit(|tx| { + let (table_id, _, iter) = self.db.index_scan_point(tx, index_id, point)?; + let rows_to_delete = iter.map(|row_ref| row_ref.pointer()).collect::>(); + Ok(self.db.delete(tx, table_id, rows_to_delete)) + }) + } + + /// Delete rows matching a range index scan. + pub fn delete_by_index_scan_range_bsatn( + &self, + index_id: IndexId, + prefix: &[u8], + prefix_elems: ColId, + rstart: &[u8], + rend: &[u8], + ) -> Result { + self.with_auto_commit(|tx| { + let (table_id, iter) = self + .db + .index_scan_range(tx, index_id, prefix, prefix_elems, rstart, rend)?; + let rows_to_delete = match iter { + IndexScanPointOrRange::Point(_, iter) => iter.map(|row_ref| row_ref.pointer()).collect::>(), + IndexScanPointOrRange::Range(iter) => iter.map(|row_ref| row_ref.pointer()).collect::>(), + }; + Ok(self.db.delete(tx, table_id, rows_to_delete)) + }) + } + + /// Update a BSATN-encoded row by matching the existing row through `index_id`. + pub fn update_bsatn_generated_cols( + &self, + table_id: TableId, + index_id: IndexId, + row: &[u8], + ) -> Result, TestDatastoreError> { + self.with_auto_commit(|tx| { + let (generated_cols, row_ref, _) = self.db.update(tx, table_id, index_id, row)?; + Ok(row_ref.project_product(&generated_cols)?.to_bsatn_vec()?) + }) + } + + /// Execute a read-only SQL select against the current datastore state. + pub fn run_select_query( + &self, + sql: &str, + database_identity: Identity, + ) -> Result, TestDatastoreError> { + let auth = AuthCtx::for_current(database_identity); + let (tx, stmt) = self.db.with_auto_rollback( + self.db.begin_mut_tx(IsolationLevel::Serializable, Workload::Sql), + |tx| compile_sql_stmt(sql, &SchemaViewer::new(tx, &auth), &auth).map_err(TestDatastoreError::Query), + )?; + + let Statement::Select(stmt) = stmt else { + let _ = self.db.rollback_mut_tx(tx); + return Err(TestDatastoreError::NonSelectQuery); + }; + + let (tx_data, tx_metrics_mut, tx) = self.db.commit_tx_downgrade(tx, Workload::Sql); + self.db.report_mut_tx_metrics(None, tx_metrics_mut, Some(tx_data)); + + let db = self.db.clone(); + let mut tx = scopeguard::guard(tx, |tx| { + let (_, metrics, reducer) = db.release_tx(tx); + db.report_read_tx_metrics(reducer, metrics); + }); + + let mut metrics = ExecutionMetrics::default(); + let rows = execute_select_stmt( + &auth, + stmt, + &spacetimedb_core::subscription::tx::DeltaTx::from(&*tx), + &mut metrics, + |plan| { + check_row_limit( + &[&plan], + &self.db, + &tx, + |plan, tx| plan.plan_iter().map(|plan| estimate_rows_scanned(tx, plan)).sum(), + &auth, + )?; + Ok(plan) + }, + ) + .map_err(TestDatastoreError::Query)?; + + tx.metrics.merge(metrics); + Ok(rows) + } +} + +impl TestTransaction { + fn with_mut_tx( + &self, + f: impl FnOnce(&mut MutTx) -> Result, + ) -> Result { + let mut tx = self.tx.borrow_mut(); + let tx = tx.as_mut().ok_or(TestDatastoreError::TransactionAlreadyFinished)?; + f(tx) + } + + /// Commit this transaction. + pub fn commit(&self) -> Result<(), TestDatastoreError> { + let tx = self + .tx + .borrow_mut() + .take() + .ok_or(TestDatastoreError::TransactionAlreadyFinished)?; + self.db.commit_tx(tx)?; + Ok(()) + } + + /// Roll back this transaction. + pub fn rollback(&self) -> Result<(), TestDatastoreError> { + let Some(tx) = self.tx.borrow_mut().take() else { + return Ok(()); + }; + let _ = self.db.rollback_mut_tx(tx); + Ok(()) + } + + /// Resolve a table name to its datastore id inside this transaction. + pub fn table_id(&self, table_name: &str) -> Result { + self.with_mut_tx(|tx| { + let id = self.db.table_id_from_name_mut(tx, table_name)?; + id.ok_or_else(|| TestDatastoreError::MissingTable(table_name.into())) + }) + } + + /// Resolve an index name to its datastore id inside this transaction. + pub fn index_id(&self, index_name: &str) -> Result { + self.with_mut_tx(|tx| { + let id = self.db.index_id_from_name_mut(tx, index_name)?; + id.ok_or_else(|| TestDatastoreError::MissingIndex(index_name.into())) + }) + } + + /// Insert a BSATN-encoded row and return BSATN-encoded generated columns. + pub fn insert_bsatn_generated_cols(&self, table_id: TableId, row: &[u8]) -> Result, TestDatastoreError> { + self.with_mut_tx(|tx| { + let (generated_cols, row_ref, _) = self.db.insert(tx, table_id, row)?; + Ok(row_ref.project_product(&generated_cols)?.to_bsatn_vec()?) + }) + } + + /// Return the number of rows in `table_id`. + pub fn table_row_count(&self, table_id: TableId) -> Result { + self.with_mut_tx(|tx| { + self.db + .table_row_count_mut(tx, table_id) + .ok_or(TestDatastoreError::MissingTableId(table_id)) + }) + } + + /// Collect every row in `table_id` as BSATN-encoded row bytes. + pub fn table_rows_bsatn(&self, table_id: TableId) -> Result>, TestDatastoreError> { + self.with_mut_tx(|tx| { + let rows = self + .db + .iter_mut(tx, table_id)? + .map(|row_ref| row_ref.to_bsatn_vec()) + .collect::, _>>()?; + Ok(rows) + }) + } + + /// Collect rows matching a point index scan as BSATN-encoded row bytes. + pub fn index_scan_point_bsatn(&self, index_id: IndexId, point: &[u8]) -> Result>, TestDatastoreError> { + self.with_mut_tx(|tx| { + let (_, _, iter) = self.db.index_scan_point(tx, index_id, point)?; + iter.map(|row_ref| row_ref.to_bsatn_vec()) + .collect::, _>>() + .map_err(TestDatastoreError::from) + }) + } + + /// Collect rows matching a range index scan as BSATN-encoded row bytes. + pub fn index_scan_range_bsatn( + &self, + index_id: IndexId, + prefix: &[u8], + prefix_elems: ColId, + rstart: &[u8], + rend: &[u8], + ) -> Result>, TestDatastoreError> { + self.with_mut_tx(|tx| { + let (_, iter) = self + .db + .index_scan_range(tx, index_id, prefix, prefix_elems, rstart, rend)?; + match iter { + IndexScanPointOrRange::Point(_, iter) => iter + .map(|row_ref| row_ref.to_bsatn_vec()) + .collect::, _>>() + .map_err(TestDatastoreError::from), + IndexScanPointOrRange::Range(iter) => iter + .map(|row_ref| row_ref.to_bsatn_vec()) + .collect::, _>>() + .map_err(TestDatastoreError::from), + } + }) + } + + /// Delete rows matching a point index scan. + pub fn delete_by_index_scan_point_bsatn(&self, index_id: IndexId, point: &[u8]) -> Result { + self.with_mut_tx(|tx| { + let (table_id, _, iter) = self.db.index_scan_point(tx, index_id, point)?; + let rows_to_delete = iter.map(|row_ref| row_ref.pointer()).collect::>(); + Ok(self.db.delete(tx, table_id, rows_to_delete)) + }) + } + + /// Delete rows matching a range index scan. + pub fn delete_by_index_scan_range_bsatn( + &self, + index_id: IndexId, + prefix: &[u8], + prefix_elems: ColId, + rstart: &[u8], + rend: &[u8], + ) -> Result { + self.with_mut_tx(|tx| { + let (table_id, iter) = self + .db + .index_scan_range(tx, index_id, prefix, prefix_elems, rstart, rend)?; + let rows_to_delete = match iter { + IndexScanPointOrRange::Point(_, iter) => iter.map(|row_ref| row_ref.pointer()).collect::>(), + IndexScanPointOrRange::Range(iter) => iter.map(|row_ref| row_ref.pointer()).collect::>(), + }; + Ok(self.db.delete(tx, table_id, rows_to_delete)) + }) + } + + /// Update a BSATN-encoded row by matching the existing row through `index_id`. + pub fn update_bsatn_generated_cols( + &self, + table_id: TableId, + index_id: IndexId, + row: &[u8], + ) -> Result, TestDatastoreError> { + self.with_mut_tx(|tx| { + let (generated_cols, row_ref, _) = self.db.update(tx, table_id, index_id, row)?; + Ok(row_ref.project_product(&generated_cols)?.to_bsatn_vec()?) + }) + } +} + +impl Drop for TestTransaction { + fn drop(&mut self) { + if let Some(tx) = self.tx.get_mut().take() { + let _ = self.db.rollback_mut_tx(tx); + } + } +} + +/// Errors returned by [`TestDatastore`]. +#[derive(Debug, Error)] +pub enum TestDatastoreError { + #[error("invalid module definition: {0}")] + ModuleDef(#[from] ValidationErrors), + #[error("database error: {0}")] + Database(#[from] DBError), + #[error("missing table `{0}`")] + MissingTable(Box), + #[error("missing index `{0}`")] + MissingIndex(Box), + #[error("missing table id `{0:?}`")] + MissingTableId(TableId), + #[error("invalid generated column projection: {0}")] + InvalidProjection(#[from] spacetimedb_lib::sats::product_value::InvalidFieldError), + #[error("BSATN encode error: {0}")] + BsatnEncode(#[from] EncodeError), + #[error("query error: {0}")] + Query(anyhow::Error), + #[error("test query must be a SELECT statement")] + NonSelectQuery, + #[error("transaction already finished")] + TransactionAlreadyFinished, +} + +impl TestDatastoreError { + /// Convert a datastore insertion error to the public syscall errno shape. + pub fn insert_errno_code(&self) -> Option { + match self { + Self::Database(DBError::Datastore(DatastoreError::Index(IndexError::UniqueConstraintViolation(_)))) => { + Some(spacetimedb_primitives::errno::UNIQUE_ALREADY_EXISTS.get()) + } + Self::Database(DBError::Sequence2(SequenceError::UnableToAllocate(_))) => { + Some(spacetimedb_primitives::errno::AUTO_INC_OVERFLOW.get()) + } + _ => None, + } + } +} + +#[cfg(test)] +mod tests { + use spacetimedb_lib::db::raw_def::v9::{btree, RawModuleDefV9Builder}; + use spacetimedb_lib::{bsatn, AlgebraicType, ProductType, RawModuleDef}; + + use super::*; + + fn raw_module_def() -> RawModuleDef { + let mut builder = RawModuleDefV9Builder::new(); + builder + .build_table_with_new_type( + "person", + ProductType::from([("id", AlgebraicType::I64), ("value", AlgebraicType::I64)]), + true, + ) + .with_unique_constraint(0) + .with_index_no_accessor_name(btree(0)); + builder + .build_table_with_new_type( + "pet", + ProductType::from([ + ("id", AlgebraicType::I64), + ("owner_id", AlgebraicType::I64), + ("name", AlgebraicType::String), + ]), + true, + ) + .with_unique_constraint(0) + .with_index_no_accessor_name(btree(0)); + + RawModuleDef::V9(builder.finish()) + } + + #[test] + fn from_module_def_creates_distinct_databases() { + let first = TestDatastore::from_module_def(raw_module_def()).unwrap(); + let second = TestDatastore::from_module_def(raw_module_def()).unwrap(); + + let first_table = first.table_id("person").unwrap(); + let second_table = second.table_id("person").unwrap(); + + first + .insert_bsatn(first_table, &bsatn::to_vec(&(1_i64, 0_i64)).unwrap()) + .unwrap(); + + assert_eq!(first.table_rows(first_table).unwrap().len(), 1); + assert_eq!(second.table_rows(second_table).unwrap().len(), 0); + } + + #[test] + fn resolves_tables_and_indexes() { + let datastore = TestDatastore::from_module_def(raw_module_def()).unwrap(); + + assert!(datastore.table_id("person").is_ok()); + assert!(datastore.index_id("person_id_idx_btree").is_ok()); + } + + #[test] + fn unique_constraints_are_enforced() { + let datastore = TestDatastore::from_module_def(raw_module_def()).unwrap(); + let table_id = datastore.table_id("person").unwrap(); + let first = bsatn::to_vec(&(1_i64, 0_i64)).unwrap(); + let duplicate = bsatn::to_vec(&(1_i64, 1_i64)).unwrap(); + + datastore.insert_bsatn(table_id, &first).unwrap(); + + assert!(datastore.insert_bsatn(table_id, &duplicate).is_err()); + } + + #[test] + fn invalid_module_def_returns_validation_error() { + let mut builder = RawModuleDefV9Builder::new(); + builder.build_table("broken", spacetimedb_lib::sats::AlgebraicTypeRef(999)); + + let err = TestDatastore::from_module_def(RawModuleDef::V9(builder.finish())).unwrap_err(); + assert!(matches!(err, TestDatastoreError::ModuleDef(_))); + } + + #[test] + fn run_select_query_returns_rows() { + let datastore = TestDatastore::from_module_def(raw_module_def()).unwrap(); + let person = datastore.table_id("person").unwrap(); + datastore + .insert_bsatn(person, &bsatn::to_vec(&(1_i64, 10_i64)).unwrap()) + .unwrap(); + datastore + .insert_bsatn(person, &bsatn::to_vec(&(2_i64, 20_i64)).unwrap()) + .unwrap(); + + let rows = datastore + .run_select_query(r#"SELECT * FROM "person""#, Identity::ZERO) + .unwrap(); + + assert_eq!(rows.len(), 2); + } + + #[test] + fn run_select_query_filters_rows() { + let datastore = TestDatastore::from_module_def(raw_module_def()).unwrap(); + let person = datastore.table_id("person").unwrap(); + datastore + .insert_bsatn(person, &bsatn::to_vec(&(1_i64, 10_i64)).unwrap()) + .unwrap(); + datastore + .insert_bsatn(person, &bsatn::to_vec(&(2_i64, 20_i64)).unwrap()) + .unwrap(); + + let rows = datastore + .run_select_query(r#"SELECT * FROM "person" WHERE "person"."value" = 20"#, Identity::ZERO) + .unwrap(); + + assert_eq!(rows, vec![ProductValue::from([2_i64.into(), 20_i64.into()])]); + } + + #[test] + fn run_select_query_supports_joins() { + let datastore = TestDatastore::from_module_def(raw_module_def()).unwrap(); + let person = datastore.table_id("person").unwrap(); + let pet = datastore.table_id("pet").unwrap(); + datastore + .insert_bsatn(person, &bsatn::to_vec(&(1_i64, 10_i64)).unwrap()) + .unwrap(); + datastore + .insert_bsatn(person, &bsatn::to_vec(&(2_i64, 20_i64)).unwrap()) + .unwrap(); + datastore + .insert_bsatn(pet, &bsatn::to_vec(&(1_i64, 2_i64, "Mochi")).unwrap()) + .unwrap(); + + let rows = datastore + .run_select_query( + r#"SELECT "person".* FROM "person" JOIN "pet" ON "person"."id" = "pet"."owner_id""#, + Identity::ZERO, + ) + .unwrap(); + + assert_eq!(rows, vec![ProductValue::from([2_i64.into(), 20_i64.into()])]); + } + + #[test] + fn run_select_query_rejects_non_select_sql() { + let datastore = TestDatastore::from_module_def(raw_module_def()).unwrap(); + let err = datastore + .run_select_query(r#"INSERT INTO "person" ("id", "value") VALUES (1, 10)"#, Identity::ZERO) + .unwrap_err(); + + assert!(matches!(err, TestDatastoreError::NonSelectQuery)); + } + + #[test] + fn run_select_query_returns_query_errors() { + let datastore = TestDatastore::from_module_def(raw_module_def()).unwrap(); + let err = datastore + .run_select_query(r#"SELECT * FROM "missing""#, Identity::ZERO) + .unwrap_err(); + + assert!(matches!(err, TestDatastoreError::Query(_))); + } + + #[test] + fn auto_inc_sequences_are_materialized() { + let mut builder = RawModuleDefV9Builder::new(); + builder + .build_table_with_new_type( + "counter", + ProductType::from([("id", AlgebraicType::U64), ("name", AlgebraicType::String)]), + true, + ) + .with_auto_inc_primary_key(0) + .with_index_no_accessor_name(btree(0)); + + let datastore = TestDatastore::from_module_def(RawModuleDef::V9(builder.finish())).unwrap(); + let table_id = datastore.table_id("counter").unwrap(); + + datastore + .insert_bsatn(table_id, &bsatn::to_vec(&(0_u64, "one")).unwrap()) + .unwrap(); + datastore + .insert_bsatn(table_id, &bsatn::to_vec(&(0_u64, "two")).unwrap()) + .unwrap(); + + assert_eq!(datastore.table_rows(table_id).unwrap().len(), 2); + } +} diff --git a/modules/benchmarks-ts/package.json b/modules/benchmarks-ts/package.json index 27c251622dc..efac040b758 100644 --- a/modules/benchmarks-ts/package.json +++ b/modules/benchmarks-ts/package.json @@ -13,5 +13,8 @@ "license": "ISC", "dependencies": { "spacetimedb": "workspace:^" + }, + "devDependencies": { + "typescript": "~5.6.2" } } diff --git a/modules/benchmarks-ts/src/synthetic.ts b/modules/benchmarks-ts/src/synthetic.ts index 7c00cf533a3..feb331d10f2 100644 --- a/modules/benchmarks-ts/src/synthetic.ts +++ b/modules/benchmarks-ts/src/synthetic.ts @@ -44,7 +44,7 @@ export const insert_unique_0_u32_u64_str = spacetimedb.reducer( { name: 'insert_unique_0_u32_u64_str' }, { id: t.u32(), age: t.u64(), name: t.string() }, (ctx, { id, age, name }) => { - ctx.db.unique0U32U64Str.insert({ id, name, age }); + ctx.db.unique_0_u32_u64_str.insert({ id, name, age }); } ); @@ -52,7 +52,7 @@ export const insert_no_index_u32_u64_str = spacetimedb.reducer( { name: 'insert_no_index_u32_u64_str' }, { id: t.u32(), age: t.u64(), name: t.string() }, (ctx, { id, age, name }) => { - ctx.db.noIndexU32U64Str.insert({ id, name, age }); + ctx.db.no_index_u32_u64_str.insert({ id, name, age }); } ); @@ -60,7 +60,7 @@ export const insert_btree_each_column_u32_u64_str = spacetimedb.reducer( { name: 'insert_btree_each_column_u32_u64_str' }, { id: t.u32(), age: t.u64(), name: t.string() }, (ctx, { id, age, name }) => { - ctx.db.btreeEachColumnU32U64Str.insert({ id, name, age }); + ctx.db.btree_each_column_u32_u64_str.insert({ id, name, age }); } ); @@ -68,7 +68,7 @@ export const insert_unique_0_u32_u64_u64 = spacetimedb.reducer( { name: 'insert_unique_0_u32_u64_u64' }, { id: t.u32(), x: t.u64(), y: t.u64() }, (ctx, { id, x, y }) => { - ctx.db.unique0U32U64U64.insert({ id, x, y }); + ctx.db.unique_0_u32_u64_u64.insert({ id, x, y }); } ); @@ -76,7 +76,7 @@ export const insert_no_index_u32_u64_u64 = spacetimedb.reducer( { name: 'insert_no_index_u32_u64_u64' }, { id: t.u32(), x: t.u64(), y: t.u64() }, (ctx, { id, x, y }) => { - ctx.db.noIndexU32U64U64.insert({ id, x, y }); + ctx.db.no_index_u32_u64_u64.insert({ id, x, y }); } ); @@ -84,7 +84,7 @@ export const insert_btree_each_column_u32_u64_u64 = spacetimedb.reducer( { name: 'insert_btree_each_column_u32_u64_u64' }, { id: t.u32(), x: t.u64(), y: t.u64() }, (ctx, { id, x, y }) => { - ctx.db.btreeEachColumnU32U64U64.insert({ id, x, y }); + ctx.db.btree_each_column_u32_u64_u64.insert({ id, x, y }); } ); @@ -95,7 +95,7 @@ export const insert_bulk_unique_0_u32_u64_u64 = spacetimedb.reducer( { locs: t.array(unique_0_u32_u64_u64_tRow) }, (ctx, { locs }) => { for (const loc of locs) { - ctx.db.unique0U32U64U64.insert(loc); + ctx.db.unique_0_u32_u64_u64.insert(loc); } } ); @@ -105,7 +105,7 @@ export const insert_bulk_no_index_u32_u64_u64 = spacetimedb.reducer( { locs: t.array(no_index_u32_u64_u64_tRow) }, (ctx, { locs }) => { for (const loc of locs) { - ctx.db.noIndexU32U64U64.insert(loc); + ctx.db.no_index_u32_u64_u64.insert(loc); } } ); @@ -115,7 +115,7 @@ export const insert_bulk_btree_each_column_u32_u64_u64 = spacetimedb.reducer( { locs: t.array(btree_each_column_u32_u64_u64_tRow) }, (ctx, { locs }) => { for (const loc of locs) { - ctx.db.btreeEachColumnU32U64U64.insert(loc); + ctx.db.btree_each_column_u32_u64_u64.insert(loc); } } ); @@ -125,7 +125,7 @@ export const insert_bulk_unique_0_u32_u64_str = spacetimedb.reducer( { people: t.array(unique_0_u32_u64_str_tRow) }, (ctx, { people }) => { for (const p of people) { - ctx.db.unique0U32U64Str.insert(p); + ctx.db.unique_0_u32_u64_str.insert(p); } } ); @@ -135,7 +135,7 @@ export const insert_bulk_no_index_u32_u64_str = spacetimedb.reducer( { people: t.array(no_index_u32_u64_str_tRow) }, (ctx, { people }) => { for (const p of people) { - ctx.db.noIndexU32U64Str.insert(p); + ctx.db.no_index_u32_u64_str.insert(p); } } ); @@ -145,7 +145,7 @@ export const insert_bulk_btree_each_column_u32_u64_str = spacetimedb.reducer( { people: t.array(btree_each_column_u32_u64_str_tRow) }, (ctx, { people }) => { for (const p of people) { - ctx.db.btreeEachColumnU32U64Str.insert(p); + ctx.db.btree_each_column_u32_u64_str.insert(p); } } ); @@ -163,13 +163,13 @@ export const update_bulk_unique_0_u32_u64_u64 = spacetimedb.reducer( { row_count: t.u32() }, (ctx, { row_count }) => { let hit = 0; - for (const loc of ctx.db.unique0U32U64U64.iter()) { + for (const loc of ctx.db.unique_0_u32_u64_u64.iter()) { if (hit == row_count) { break; } hit += 1; - ctx.db.unique0U32U64U64.id?.update({ + ctx.db.unique_0_u32_u64_u64.id?.update({ id: loc.id, x: loc.x + 1n, y: loc.y, @@ -185,13 +185,13 @@ export const update_bulk_unique_0_u32_u64_str = spacetimedb.reducer( { row_count: t.u32() }, (ctx, { row_count }) => { let hit = 0; - for (const p of ctx.db.unique0U32U64Str.iter()) { + for (const p of ctx.db.unique_0_u32_u64_str.iter()) { if (hit == row_count) { break; } hit += 1; - ctx.db.unique0U32U64Str.id?.update({ + ctx.db.unique_0_u32_u64_str.id?.update({ id: p.id, age: p.age + 1n, name: p.name, @@ -207,7 +207,7 @@ export const update_bulk_unique_0_u32_u64_str = spacetimedb.reducer( export const iterate_unique_0_u32_u64_str = spacetimedb.reducer( { name: 'iterate_unique_0_u32_u64_str' }, ctx => { - for (const x of ctx.db.unique0U32U64Str.iter()) { + for (const x of ctx.db.unique_0_u32_u64_str.iter()) { blackBox(x); } } @@ -216,7 +216,7 @@ export const iterate_unique_0_u32_u64_str = spacetimedb.reducer( export const iterate_unique_0_u32_u64_u64 = spacetimedb.reducer( { name: 'iterate_unique_0_u32_u64_u64' }, ctx => { - for (const x of ctx.db.unique0U32U64U64.iter()) { + for (const x of ctx.db.unique_0_u32_u64_u64.iter()) { blackBox(x); } } @@ -228,7 +228,7 @@ export const filter_unique_0_u32_u64_str_by_id = spacetimedb.reducer( { name: 'filter_unique_0_u32_u64_str_by_id' }, { id: t.u32() }, (ctx, { id }) => { - blackBox(ctx.db.unique0U32U64Str.id?.find(id)); + blackBox(ctx.db.unique_0_u32_u64_str.id?.find(id)); } ); @@ -236,7 +236,7 @@ export const filter_no_index_u32_u64_str_by_id = spacetimedb.reducer( { name: 'filter_no_index_u32_u64_str_by_id' }, { id: t.u32() }, (ctx, { id }) => { - for (const r of ctx.db.noIndexU32U64Str.iter()) { + for (const r of ctx.db.no_index_u32_u64_str.iter()) { if (r.id == id) { blackBox(r); } @@ -248,7 +248,7 @@ export const filter_btree_each_column_u32_u64_str_by_id = spacetimedb.reducer( { name: 'filter_btree_each_column_u32_u64_str_by_id' }, { id: t.u32() }, (ctx, { id }) => { - const idIndex = ctx.db.btreeEachColumnU32U64Str.id; + const idIndex = ctx.db.btree_each_column_u32_u64_str.id; if (idIndex) { for (const r of idIndex.filter(id)) { blackBox(r); @@ -261,7 +261,7 @@ export const filter_unique_0_u32_u64_str_by_name = spacetimedb.reducer( { name: 'filter_unique_0_u32_u64_str_by_name' }, { name: t.string() }, (ctx, { name }) => { - for (const r of ctx.db.unique0U32U64Str.iter()) { + for (const r of ctx.db.unique_0_u32_u64_str.iter()) { if (r.name == name) { blackBox(r); } @@ -273,7 +273,7 @@ export const filter_no_index_u32_u64_str_by_name = spacetimedb.reducer( { name: 'filter_no_index_u32_u64_str_by_name' }, { name: t.string() }, (ctx, { name }) => { - for (const r of ctx.db.noIndexU32U64Str.iter()) { + for (const r of ctx.db.no_index_u32_u64_str.iter()) { if (r.name == name) { blackBox(r); } @@ -285,7 +285,7 @@ export const filter_btree_each_column_u32_u64_str_by_name = spacetimedb.reducer( { name: 'filter_btree_each_column_u32_u64_str_by_name' }, { name: t.string() }, (ctx, { name }) => { - const nameIndex = ctx.db.btreeEachColumnU32U64Str.name; + const nameIndex = ctx.db.btree_each_column_u32_u64_str.name; if (nameIndex) { for (const r of nameIndex.filter(name)) { blackBox(r); @@ -298,7 +298,7 @@ export const filter_unique_0_u32_u64_u64_by_id = spacetimedb.reducer( { name: 'filter_unique_0_u32_u64_u64_by_id' }, { id: t.u32() }, (ctx, { id }) => { - blackBox(ctx.db.unique0U32U64U64.id?.find(id)); + blackBox(ctx.db.unique_0_u32_u64_u64.id?.find(id)); } ); @@ -306,7 +306,7 @@ export const filter_no_index_u32_u64_u64_by_id = spacetimedb.reducer( { name: 'filter_no_index_u32_u64_u64_by_id' }, { id: t.u32() }, (ctx, { id }) => { - for (const r of ctx.db.noIndexU32U64U64.iter()) { + for (const r of ctx.db.no_index_u32_u64_u64.iter()) { if (r.id == id) { blackBox(r); } @@ -318,7 +318,7 @@ export const filter_btree_each_column_u32_u64_u64_by_id = spacetimedb.reducer( { name: 'filter_btree_each_column_u32_u64_u64_by_id' }, { id: t.u32() }, (ctx, { id }) => { - const idIndex = ctx.db.btreeEachColumnU32U64U64.id; + const idIndex = ctx.db.btree_each_column_u32_u64_u64.id; if (idIndex) { for (const r of idIndex.filter(id)) { blackBox(r); @@ -331,7 +331,7 @@ export const filter_unique_0_u32_u64_u64_by_x = spacetimedb.reducer( { name: 'filter_unique_0_u32_u64_u64_by_x' }, { x: t.u64() }, (ctx, { x }) => { - for (const r of ctx.db.unique0U32U64U64.iter()) { + for (const r of ctx.db.unique_0_u32_u64_u64.iter()) { if (r.x == x) { blackBox(r); } @@ -343,7 +343,7 @@ export const filter_no_index_u32_u64_u64_by_x = spacetimedb.reducer( { name: 'filter_no_index_u32_u64_u64_by_x' }, { x: t.u64() }, (ctx, { x }) => { - for (const r of ctx.db.noIndexU32U64U64.iter()) { + for (const r of ctx.db.no_index_u32_u64_u64.iter()) { if (r.x == x) { blackBox(r); } @@ -355,7 +355,7 @@ export const filter_btree_each_column_u32_u64_u64_by_x = spacetimedb.reducer( { name: 'filter_btree_each_column_u32_u64_u64_by_x' }, { x: t.u64() }, (ctx, { x }) => { - const xIndex = ctx.db.btreeEachColumnU32U64U64.x; + const xIndex = ctx.db.btree_each_column_u32_u64_u64.x; if (xIndex) { for (const r of xIndex.filter(x)) { blackBox(r); @@ -368,7 +368,7 @@ export const filter_unique_0_u32_u64_u64_by_y = spacetimedb.reducer( { name: 'filter_unique_0_u32_u64_u64_by_y' }, { y: t.u64() }, (ctx, { y }) => { - for (const r of ctx.db.unique0U32U64U64.iter()) { + for (const r of ctx.db.unique_0_u32_u64_u64.iter()) { if (r.y == y) { blackBox(r); } @@ -380,7 +380,7 @@ export const filter_no_index_u32_u64_u64_by_y = spacetimedb.reducer( { name: 'filter_no_index_u32_u64_u64_by_y' }, { y: t.u64() }, (ctx, { y }) => { - for (const r of ctx.db.noIndexU32U64U64.iter()) { + for (const r of ctx.db.no_index_u32_u64_u64.iter()) { if (r.y == y) { blackBox(r); } @@ -392,7 +392,7 @@ export const filter_btree_each_column_u32_u64_u64_by_y = spacetimedb.reducer( { name: 'filter_btree_each_column_u32_u64_u64_by_y' }, { y: t.u64() }, (ctx, { y }) => { - const yIndex = ctx.db.btreeEachColumnU32U64U64.y; + const yIndex = ctx.db.btree_each_column_u32_u64_u64.y; if (yIndex) { for (const r of yIndex.filter(y)) { blackBox(r); @@ -409,7 +409,7 @@ export const delete_unique_0_u32_u64_str_by_id = spacetimedb.reducer( { name: 'delete_unique_0_u32_u64_str_by_id' }, { id: t.u32() }, (ctx, { id }) => { - ctx.db.unique0U32U64Str.id?.delete(id); + ctx.db.unique_0_u32_u64_str.id?.delete(id); } ); @@ -417,7 +417,7 @@ export const delete_unique_0_u32_u64_u64_by_id = spacetimedb.reducer( { name: 'delete_unique_0_u32_u64_u64_by_id' }, { id: t.u32() }, (ctx, { id }) => { - ctx.db.unique0U32U64U64.id?.delete(id); + ctx.db.unique_0_u32_u64_u64.id?.delete(id); } ); @@ -464,7 +464,7 @@ export const clear_table_btree_each_column_u32_u64_u64 = spacetimedb.reducer( export const count_unique_0_u32_u64_str = spacetimedb.reducer( { name: 'count_unique_0_u32_u64_str' }, ctx => { - const count = ctx.db.unique0U32U64Str.count(); + const count = ctx.db.unique_0_u32_u64_str.count(); console.info!(`COUNT: ${count}`); } ); @@ -472,7 +472,7 @@ export const count_unique_0_u32_u64_str = spacetimedb.reducer( export const count_no_index_u32_u64_str = spacetimedb.reducer( { name: 'count_no_index_u32_u64_str' }, ctx => { - const count = ctx.db.noIndexU32U64Str.count(); + const count = ctx.db.no_index_u32_u64_str.count(); console.info!(`COUNT: ${count}`); } ); @@ -480,7 +480,7 @@ export const count_no_index_u32_u64_str = spacetimedb.reducer( export const count_btree_each_column_u32_u64_str = spacetimedb.reducer( { name: 'count_btree_each_column_u32_u64_str' }, ctx => { - const count = ctx.db.btreeEachColumnU32U64Str.count(); + const count = ctx.db.btree_each_column_u32_u64_str.count(); console.info!(`COUNT: ${count}`); } ); @@ -488,7 +488,7 @@ export const count_btree_each_column_u32_u64_str = spacetimedb.reducer( export const count_unique_0_u32_u64_u64 = spacetimedb.reducer( { name: 'count_unique_0_u32_u64_u64' }, ctx => { - const count = ctx.db.unique0U32U64U64.count(); + const count = ctx.db.unique_0_u32_u64_u64.count(); console.info!(`COUNT: ${count}`); } ); @@ -496,7 +496,7 @@ export const count_unique_0_u32_u64_u64 = spacetimedb.reducer( export const count_no_index_u32_u64_u64 = spacetimedb.reducer( { name: 'count_no_index_u32_u64_u64' }, ctx => { - const count = ctx.db.noIndexU32U64U64.count(); + const count = ctx.db.no_index_u32_u64_u64.count(); console.info!(`COUNT: ${count}`); } ); @@ -504,7 +504,7 @@ export const count_no_index_u32_u64_u64 = spacetimedb.reducer( export const count_btree_each_column_u32_u64_u64 = spacetimedb.reducer( { name: 'count_btree_each_column_u32_u64_u64' }, ctx => { - const count = ctx.db.btreeEachColumnU32U64U64.count(); + const count = ctx.db.btree_each_column_u32_u64_u64.count(); console.info!(`COUNT: ${count}`); } ); diff --git a/modules/module-test-ts/package.json b/modules/module-test-ts/package.json index 4e964319a75..f405605b5f8 100644 --- a/modules/module-test-ts/package.json +++ b/modules/module-test-ts/package.json @@ -9,5 +9,8 @@ }, "dependencies": { "spacetimedb": "workspace:^" + }, + "devDependencies": { + "typescript": "~5.6.2" } } diff --git a/modules/module-test/src/lib.rs b/modules/module-test/src/lib.rs index fc1851b21b0..4925d39dd19 100644 --- a/modules/module-test/src/lib.rs +++ b/modules/module-test/src/lib.rs @@ -73,7 +73,7 @@ pub struct TestD { // uses internal apis that should not be used by user code #[allow(dead_code)] // false positive -const fn get_table_access(_: impl Fn(&spacetimedb::Local) -> &Tbl + Copy) -> TableAccess { +const fn get_table_access(_: impl Fn(&spacetimedb::Local) -> Tbl + Copy) -> TableAccess { ::TABLE_ACCESS } diff --git a/modules/sdk-test-case-conversion-ts/package.json b/modules/sdk-test-case-conversion-ts/package.json index 326c97ec7fc..7eb851915e4 100644 --- a/modules/sdk-test-case-conversion-ts/package.json +++ b/modules/sdk-test-case-conversion-ts/package.json @@ -13,5 +13,8 @@ "license": "ISC", "dependencies": { "spacetimedb": "workspace:^" + }, + "devDependencies": { + "typescript": "~5.6.2" } } diff --git a/modules/sdk-test-connect-disconnect-ts/package.json b/modules/sdk-test-connect-disconnect-ts/package.json index cd7b60b0e47..adafb10fcaa 100644 --- a/modules/sdk-test-connect-disconnect-ts/package.json +++ b/modules/sdk-test-connect-disconnect-ts/package.json @@ -13,5 +13,8 @@ "license": "ISC", "dependencies": { "spacetimedb": "workspace:^" + }, + "devDependencies": { + "typescript": "~5.6.2" } } diff --git a/modules/sdk-test-procedure-ts/package.json b/modules/sdk-test-procedure-ts/package.json index c8b4bc4ba89..de52ac1aad1 100644 --- a/modules/sdk-test-procedure-ts/package.json +++ b/modules/sdk-test-procedure-ts/package.json @@ -9,5 +9,8 @@ }, "dependencies": { "spacetimedb": "workspace:^" + }, + "devDependencies": { + "typescript": "~5.6.2" } } diff --git a/modules/sdk-test-ts/package.json b/modules/sdk-test-ts/package.json index 4e964319a75..f405605b5f8 100644 --- a/modules/sdk-test-ts/package.json +++ b/modules/sdk-test-ts/package.json @@ -9,5 +9,8 @@ }, "dependencies": { "spacetimedb": "workspace:^" + }, + "devDependencies": { + "typescript": "~5.6.2" } } diff --git a/modules/sdk-test-ts/src/index.ts b/modules/sdk-test-ts/src/index.ts index 257086782f0..e463fc6fba6 100644 --- a/modules/sdk-test-ts/src/index.ts +++ b/modules/sdk-test-ts/src/index.ts @@ -118,7 +118,10 @@ function tbl( delete_by?: [string, keyof Row]; }, row: Row -): TableWithReducers>> { +): TableWithReducers< + Accessor, + ReturnType> +> { const t = table({ public: true }, row); return { table: t, @@ -148,7 +151,7 @@ function tbl( if (ops.update_non_pk_by) { const [reducer, col] = ops.update_non_pk_by; exports[reducer] = spacetimedb.reducer(row, (ctx, args) => { - (ctx.db[accessor] as any)[col].delete(args[col as any]); + (ctx.db[accessor] as any)[col].delete((args as any)[col]); (ctx.db[accessor] as any).insert({ ...args }); }); } @@ -157,7 +160,7 @@ function tbl( exports[reducer] = spacetimedb.reducer( { [col]: row[col] }, (ctx, args) => { - (ctx.db[accessor] as any)[col].delete(args[col as any]); + (ctx.db[accessor] as any)[col].delete((args as any)[col]); } ); } @@ -248,21 +251,9 @@ const singleValTables = { // Tables holding a Vec of various types. const vecTables = { vecU8: tbl('vecU8', { insert: 'insert_vec_u8' }, { n: t.array(t.u8()) }), - vecU16: tbl( - 'vecU16', - { insert: 'insert_vec_u16' }, - { n: t.array(t.u16()) } - ), - vecU32: tbl( - 'vecU32', - { insert: 'insert_vec_u32' }, - { n: t.array(t.u32()) } - ), - vecU64: tbl( - 'vecU64', - { insert: 'insert_vec_u64' }, - { n: t.array(t.u64()) } - ), + vecU16: tbl('vecU16', { insert: 'insert_vec_u16' }, { n: t.array(t.u16()) }), + vecU32: tbl('vecU32', { insert: 'insert_vec_u32' }, { n: t.array(t.u32()) }), + vecU64: tbl('vecU64', { insert: 'insert_vec_u64' }, { n: t.array(t.u64()) }), vecU128: tbl( 'vecU128', { insert: 'insert_vec_u128' }, @@ -275,21 +266,9 @@ const vecTables = { ), vecI8: tbl('vecI8', { insert: 'insert_vec_i8' }, { n: t.array(t.i8()) }), - vecI16: tbl( - 'vecI16', - { insert: 'insert_vec_i16' }, - { n: t.array(t.i16()) } - ), - vecI32: tbl( - 'vecI32', - { insert: 'insert_vec_i32' }, - { n: t.array(t.i32()) } - ), - vecI64: tbl( - 'vecI64', - { insert: 'insert_vec_i64' }, - { n: t.array(t.i64()) } - ), + vecI16: tbl('vecI16', { insert: 'insert_vec_i16' }, { n: t.array(t.i16()) }), + vecI32: tbl('vecI32', { insert: 'insert_vec_i32' }, { n: t.array(t.i32()) }), + vecI64: tbl('vecI64', { insert: 'insert_vec_i64' }, { n: t.array(t.i64()) }), vecI128: tbl( 'vecI128', { insert: 'insert_vec_i128' }, @@ -307,16 +286,8 @@ const vecTables = { { b: t.array(t.bool()) } ), - vecF32: tbl( - 'vecF32', - { insert: 'insert_vec_f32' }, - { f: t.array(t.f32()) } - ), - vecF64: tbl( - 'vecF64', - { insert: 'insert_vec_f64' }, - { f: t.array(t.f64()) } - ), + vecF32: tbl('vecF32', { insert: 'insert_vec_f32' }, { f: t.array(t.f32()) }), + vecF64: tbl('vecF64', { insert: 'insert_vec_f64' }, { f: t.array(t.f64()) }), vecString: tbl( 'vecString', diff --git a/package.json b/package.json index a97c459e51c..955f0f2f964 100644 --- a/package.json +++ b/package.json @@ -30,14 +30,5 @@ "typescript": "~5.6.2", "typescript-eslint": "^8.18.2", "vitest": "^3.2.4" - }, - "pnpm": { - "onlyBuiltDependencies": [ - "bun" - ], - "ignoredBuiltDependencies": [ - "@parcel/watcher", - "esbuild" - ] } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 95a507ad3af..a3da29d0719 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -272,7 +272,7 @@ importers: version: 3.9.2(react-dom@18.3.1(react@18.3.1))(react@18.3.1) '@signalwire/docusaurus-plugin-llms-txt': specifier: ^1.2.2 - version: 1.2.2(@docusaurus/core@3.9.2(@mdx-js/react@3.1.1(@types/react@18.3.23)(react@18.3.1))(react-dom@18.3.1(react@18.3.1))(react@18.3.1)(typescript@5.6.3)) + version: 1.2.2(@docusaurus/core@3.9.2(@mdx-js/react@3.1.1(@types/react@18.3.23)(react@18.3.1))(debug@4.4.3)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)(typescript@5.6.3)) '@types/react': specifier: ^18.3.23 version: 18.3.23 @@ -288,24 +288,40 @@ importers: spacetimedb: specifier: workspace:^ version: link:../../crates/bindings-typescript + devDependencies: + typescript: + specifier: ~5.6.2 + version: 5.6.3 modules/module-test-ts: dependencies: spacetimedb: specifier: workspace:^ version: link:../../crates/bindings-typescript + devDependencies: + typescript: + specifier: ~5.6.2 + version: 5.6.3 modules/sdk-test-case-conversion-ts: dependencies: spacetimedb: specifier: workspace:^ version: link:../../crates/bindings-typescript + devDependencies: + typescript: + specifier: ~5.6.2 + version: 5.6.3 modules/sdk-test-connect-disconnect-ts: dependencies: spacetimedb: specifier: workspace:^ version: link:../../crates/bindings-typescript + devDependencies: + typescript: + specifier: ~5.6.2 + version: 5.6.3 modules/sdk-test-procedural-view-pk-ts: dependencies: @@ -313,19 +329,27 @@ importers: specifier: workspace:^ version: link:../../crates/bindings-typescript - modules/sdk-test-view-pk-ts: + modules/sdk-test-procedure-ts: dependencies: spacetimedb: specifier: workspace:^ version: link:../../crates/bindings-typescript + devDependencies: + typescript: + specifier: ~5.6.2 + version: 5.6.3 - modules/sdk-test-procedure-ts: + modules/sdk-test-ts: dependencies: spacetimedb: specifier: workspace:^ version: link:../../crates/bindings-typescript + devDependencies: + typescript: + specifier: ~5.6.2 + version: 5.6.3 - modules/sdk-test-ts: + modules/sdk-test-view-pk-ts: dependencies: spacetimedb: specifier: workspace:^ @@ -486,6 +510,9 @@ importers: typescript: specifier: ^5.9.3 version: 5.9.3 + vitest: + specifier: ^3.2.4 + version: 3.2.4(@types/debug@4.1.12)(@types/node@24.3.0)(jiti@2.6.1)(jsdom@26.1.0)(sass@1.97.3)(terser@5.43.1)(tsx@4.21.0)(yaml@2.8.2) templates/hangman-react-ts: dependencies: @@ -13909,6 +13936,7 @@ packages: tsconfck@2.1.2: resolution: {integrity: sha512-ghqN1b0puy3MhhviwO2kGF8SeMDNhEbnKxjK7h6+fvY9JAxqvXi8y5NAHSQv687OVboS2uZIByzGd45/YxrRHg==} engines: {node: ^14.13.1 || ^16 || >=18} + deprecated: unmaintained hasBin: true peerDependencies: typescript: ^4.3.5 || ^5.0.0 @@ -13919,6 +13947,7 @@ packages: tsconfck@3.1.6: resolution: {integrity: sha512-ks6Vjr/jEw0P1gmOVwutM3B7fWxoWBL2KRDb1JfqGVawBmO5UsvmWOQFGHBPl5yxYz4eERr19E6L7NMv+Fej4w==} engines: {node: ^18 || >=20} + deprecated: unmaintained hasBin: true peerDependencies: typescript: ^5.0.0 @@ -14736,10 +14765,6 @@ packages: resolution: {integrity: sha512-o8qghlI8NZHU1lLPrpi2+Uq7abh4GGPpYANlalzWxyWteJOCsr/P+oPBA49TOLu5FTZO4d3F9MnWJfiMo4BkmA==} engines: {node: '>=18'} - watchpack@2.5.0: - resolution: {integrity: sha512-e6vZvY6xboSwLz2GD36c16+O/2Z6fKvIf4pOXptw2rY9MVwE/TXc6RGqxD3I3x0a28lwBY7DE+76uTPSsBrrCA==} - engines: {node: '>=10.13.0'} - watchpack@2.5.1: resolution: {integrity: sha512-Zn5uXdcFNIA1+1Ei5McRd+iRzfhENPCe7LeABkJtNulSxjma+l7ltNx55BWZkRlwRnpOgHqxnjyaDgJnNXnqzg==} engines: {node: '>=10.13.0'} @@ -15641,7 +15666,7 @@ snapshots: '@babel/helper-module-transforms': 7.28.6(@babel/core@7.29.0) '@babel/helpers': 7.28.6 '@babel/parser': 7.29.7 - '@babel/template': 7.28.6 + '@babel/template': 7.29.7 '@babel/traverse': 7.29.7 '@babel/types': 7.29.7 '@jridgewell/remapping': 2.3.5 @@ -15783,8 +15808,8 @@ snapshots: dependencies: '@babel/core': 7.28.6 '@babel/helper-module-imports': 7.28.6 - '@babel/helper-validator-identifier': 7.28.5 - '@babel/traverse': 7.28.6 + '@babel/helper-validator-identifier': 7.29.7 + '@babel/traverse': 7.29.7 transitivePeerDependencies: - supports-color @@ -15792,8 +15817,8 @@ snapshots: dependencies: '@babel/core': 7.29.0 '@babel/helper-module-imports': 7.28.6 - '@babel/helper-validator-identifier': 7.28.5 - '@babel/traverse': 7.28.6 + '@babel/helper-validator-identifier': 7.29.7 + '@babel/traverse': 7.29.7 transitivePeerDependencies: - supports-color @@ -15870,8 +15895,8 @@ snapshots: '@babel/helpers@7.28.6': dependencies: - '@babel/template': 7.28.6 - '@babel/types': 7.28.6 + '@babel/template': 7.29.7 + '@babel/types': 7.29.7 '@babel/parser@7.28.3': dependencies: @@ -21104,7 +21129,7 @@ snapshots: '@sideway/pinpoint@2.0.0': {} - '@signalwire/docusaurus-plugin-llms-txt@1.2.2(@docusaurus/core@3.9.2(@mdx-js/react@3.1.1(@types/react@18.3.23)(react@18.3.1))(react-dom@18.3.1(react@18.3.1))(react@18.3.1)(typescript@5.6.3))': + '@signalwire/docusaurus-plugin-llms-txt@1.2.2(@docusaurus/core@3.9.2(@mdx-js/react@3.1.1(@types/react@18.3.23)(react@18.3.1))(debug@4.4.3)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)(typescript@5.6.3))': dependencies: '@docusaurus/core': 3.9.2(@mdx-js/react@3.1.1(@types/react@18.3.23)(react@18.3.1))(debug@4.4.3)(react-dom@18.3.1(react@18.3.1))(react@18.3.1)(typescript@5.6.3) fs-extra: 11.3.2 @@ -23577,9 +23602,9 @@ snapshots: '@opentelemetry/api': 1.9.0 zod: 4.3.5 - ajv-formats@2.1.1(ajv@8.17.1): + ajv-formats@2.1.1(ajv@8.18.0): optionalDependencies: - ajv: 8.17.1 + ajv: 8.18.0 ajv-formats@3.0.1(ajv@8.17.1): optionalDependencies: @@ -23593,9 +23618,9 @@ snapshots: dependencies: ajv: 6.12.6 - ajv-keywords@5.1.0(ajv@8.17.1): + ajv-keywords@5.1.0(ajv@8.18.0): dependencies: - ajv: 8.17.1 + ajv: 8.18.0 fast-deep-equal: 3.1.3 ajv@6.12.6: @@ -27240,8 +27265,8 @@ snapshots: magicast@0.5.1: dependencies: - '@babel/parser': 7.28.6 - '@babel/types': 7.28.6 + '@babel/parser': 7.29.7 + '@babel/types': 7.29.7 source-map-js: 1.2.1 make-dir@4.0.0: @@ -30727,9 +30752,9 @@ snapshots: schema-utils@4.3.3: dependencies: '@types/json-schema': 7.0.15 - ajv: 8.17.1 - ajv-formats: 2.1.1(ajv@8.17.1) - ajv-keywords: 5.1.0(ajv@8.17.1) + ajv: 8.18.0 + ajv-formats: 2.1.1(ajv@8.18.0) + ajv-keywords: 5.1.0(ajv@8.18.0) scule@1.3.0: {} @@ -31631,7 +31656,7 @@ snapshots: tsx@4.21.0: dependencies: - esbuild: 0.27.2 + esbuild: 0.27.3 get-tsconfig: 4.10.1 optionalDependencies: fsevents: 2.3.3 @@ -32536,11 +32561,6 @@ snapshots: dependencies: xml-name-validator: 5.0.0 - watchpack@2.5.0: - dependencies: - glob-to-regexp: 0.4.1 - graceful-fs: 4.2.11 - watchpack@2.5.1: dependencies: glob-to-regexp: 0.4.1 @@ -32683,7 +32703,7 @@ snapshots: schema-utils: 4.3.3 tapable: 2.3.0 terser-webpack-plugin: 5.3.14(webpack@5.102.0) - watchpack: 2.5.0 + watchpack: 2.5.1 webpack-sources: 3.3.3 transitivePeerDependencies: - '@swc/core' diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index 723fd0500b8..d97a0fb6656 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -28,4 +28,13 @@ packages: - 'modules/sdk-test-ts' - 'modules/sdk-test-case-conversion-ts' - 'docs' +allowBuilds: + '@parcel/watcher': false + better-sqlite3: false + bun: true + core-js: false + core-js-pure: false + esbuild: false + lmdb: false + msgpackr-extract: false minimumReleaseAge: 1440 diff --git a/sdks/rust/tests/case-conversion-client/src/module_bindings/mod.rs b/sdks/rust/tests/case-conversion-client/src/module_bindings/mod.rs index 3d533eef2d8..06c0cf0b6f8 100644 --- a/sdks/rust/tests/case-conversion-client/src/module_bindings/mod.rs +++ b/sdks/rust/tests/case-conversion-client/src/module_bindings/mod.rs @@ -1,7 +1,7 @@ // THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE // WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. -// This was generated using spacetimedb cli version 2.7.0 (commit de4516d0d3d943cf4c3097c4ad31e4c7735b4f5b). +// This was generated using spacetimedb cli version 2.7.0 (commit 234f6c9c54406060ee2d781af9fd727e0d790650). #![allow(unused, clippy::all)] use spacetimedb_sdk::__codegen::{self as __sdk, __lib, __sats, __ws}; diff --git a/sdks/rust/tests/connect_disconnect_client/src/module_bindings/mod.rs b/sdks/rust/tests/connect_disconnect_client/src/module_bindings/mod.rs index bae06001c05..7003fa6dc3d 100644 --- a/sdks/rust/tests/connect_disconnect_client/src/module_bindings/mod.rs +++ b/sdks/rust/tests/connect_disconnect_client/src/module_bindings/mod.rs @@ -1,7 +1,7 @@ // THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE // WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. -// This was generated using spacetimedb cli version 2.7.0 (commit de4516d0d3d943cf4c3097c4ad31e4c7735b4f5b). +// This was generated using spacetimedb cli version 2.7.0 (commit 234f6c9c54406060ee2d781af9fd727e0d790650). #![allow(unused, clippy::all)] use spacetimedb_sdk::__codegen::{self as __sdk, __lib, __sats, __ws}; diff --git a/sdks/rust/tests/event-table-client/src/module_bindings/mod.rs b/sdks/rust/tests/event-table-client/src/module_bindings/mod.rs index 76a9f02533b..eae923a7d96 100644 --- a/sdks/rust/tests/event-table-client/src/module_bindings/mod.rs +++ b/sdks/rust/tests/event-table-client/src/module_bindings/mod.rs @@ -1,7 +1,7 @@ // THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE // WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. -// This was generated using spacetimedb cli version 2.7.0 (commit de4516d0d3d943cf4c3097c4ad31e4c7735b4f5b). +// This was generated using spacetimedb cli version 2.7.0 (commit 234f6c9c54406060ee2d781af9fd727e0d790650). #![allow(unused, clippy::all)] use spacetimedb_sdk::__codegen::{self as __sdk, __lib, __sats, __ws}; diff --git a/sdks/rust/tests/procedural-view-pk-client/src/module_bindings/mod.rs b/sdks/rust/tests/procedural-view-pk-client/src/module_bindings/mod.rs index b76570b7389..686e9439d5b 100644 --- a/sdks/rust/tests/procedural-view-pk-client/src/module_bindings/mod.rs +++ b/sdks/rust/tests/procedural-view-pk-client/src/module_bindings/mod.rs @@ -1,7 +1,7 @@ // THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE // WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. -// This was generated using spacetimedb cli version 2.7.0 (commit de4516d0d3d943cf4c3097c4ad31e4c7735b4f5b). +// This was generated using spacetimedb cli version 2.7.0 (commit 234f6c9c54406060ee2d781af9fd727e0d790650). #![allow(unused, clippy::all)] use spacetimedb_sdk::__codegen::{self as __sdk, __lib, __sats, __ws}; diff --git a/sdks/rust/tests/procedure-client/src/module_bindings/mod.rs b/sdks/rust/tests/procedure-client/src/module_bindings/mod.rs index 916180a1ed9..744b9ef4509 100644 --- a/sdks/rust/tests/procedure-client/src/module_bindings/mod.rs +++ b/sdks/rust/tests/procedure-client/src/module_bindings/mod.rs @@ -1,7 +1,7 @@ // THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE // WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. -// This was generated using spacetimedb cli version 2.7.0 (commit de4516d0d3d943cf4c3097c4ad31e4c7735b4f5b). +// This was generated using spacetimedb cli version 2.7.0 (commit 234f6c9c54406060ee2d781af9fd727e0d790650). #![allow(unused, clippy::all)] use spacetimedb_sdk::__codegen::{self as __sdk, __lib, __sats, __ws}; diff --git a/sdks/rust/tests/procedure-concurrency-client/src/module_bindings/mod.rs b/sdks/rust/tests/procedure-concurrency-client/src/module_bindings/mod.rs index 49eed3610a2..720a1dc167c 100644 --- a/sdks/rust/tests/procedure-concurrency-client/src/module_bindings/mod.rs +++ b/sdks/rust/tests/procedure-concurrency-client/src/module_bindings/mod.rs @@ -1,7 +1,7 @@ // THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE // WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. -// This was generated using spacetimedb cli version 2.7.0 (commit de4516d0d3d943cf4c3097c4ad31e4c7735b4f5b). +// This was generated using spacetimedb cli version 2.7.0 (commit 234f6c9c54406060ee2d781af9fd727e0d790650). #![allow(unused, clippy::all)] use spacetimedb_sdk::__codegen::{self as __sdk, __lib, __sats, __ws}; diff --git a/sdks/rust/tests/test-client/src/module_bindings/mod.rs b/sdks/rust/tests/test-client/src/module_bindings/mod.rs index 56cb87d3587..4b24ea91234 100644 --- a/sdks/rust/tests/test-client/src/module_bindings/mod.rs +++ b/sdks/rust/tests/test-client/src/module_bindings/mod.rs @@ -1,7 +1,7 @@ // THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE // WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. -// This was generated using spacetimedb cli version 2.7.0 (commit 77ed3b1e0586e3bf1cac7ed32f1887263d7eb816). +// This was generated using spacetimedb cli version 2.7.0 (commit 234f6c9c54406060ee2d781af9fd727e0d790650). #![allow(unused, clippy::all)] use spacetimedb_sdk::__codegen::{self as __sdk, __lib, __sats, __ws}; diff --git a/sdks/rust/tests/test.rs b/sdks/rust/tests/test.rs index af7c535824b..41383ea93c8 100644 --- a/sdks/rust/tests/test.rs +++ b/sdks/rust/tests/test.rs @@ -1,8 +1,31 @@ #[cfg(feature = "browser")] use std::path::Path; +#[cfg(feature = "browser")] +use std::process::{Command, Stdio}; +#[cfg(feature = "browser")] +use std::sync::OnceLock; use spacetimedb_testing::sdk::{Test, TestBuilder}; +#[cfg(feature = "browser")] +fn node_websocket_flag() -> &'static str { + static FLAG: OnceLock<&'static str> = OnceLock::new(); + FLAG.get_or_init(|| { + let supports_experimental_flag = Command::new("node") + .args(["--experimental-websocket", "-e", ""]) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status() + .is_ok_and(|status| status.success()); + + if supports_experimental_flag { + "--experimental-websocket " + } else { + "" + } + }) +} + fn platform_test_builder(client_project: &str, run_selector: Option<&str>) -> TestBuilder { let builder = Test::builder(); let builder = builder.with_client(client_project); @@ -72,7 +95,7 @@ fn platform_test_builder(client_project: &str, run_selector: Option<&str>) -> Te }})().catch((e) => {{ console.error(e); process.exit(1); }});" ); let node_script = shlex::try_quote(&node_script).expect("inline Node script should be shell-quotable"); - let run_command = format!("node --experimental-websocket -e {node_script}"); + let run_command = format!("node {}-e {node_script}", node_websocket_flag()); builder .with_compile_command(compile_command) diff --git a/sdks/rust/tests/view-client/src/module_bindings/mod.rs b/sdks/rust/tests/view-client/src/module_bindings/mod.rs index 89af2c506a9..28b875670ab 100644 --- a/sdks/rust/tests/view-client/src/module_bindings/mod.rs +++ b/sdks/rust/tests/view-client/src/module_bindings/mod.rs @@ -1,7 +1,7 @@ // THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE // WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. -// This was generated using spacetimedb cli version 2.7.0 (commit de4516d0d3d943cf4c3097c4ad31e4c7735b4f5b). +// This was generated using spacetimedb cli version 2.7.0 (commit 234f6c9c54406060ee2d781af9fd727e0d790650). #![allow(unused, clippy::all)] use spacetimedb_sdk::__codegen::{self as __sdk, __lib, __sats, __ws}; diff --git a/sdks/rust/tests/view-pk-client/src/module_bindings/mod.rs b/sdks/rust/tests/view-pk-client/src/module_bindings/mod.rs index 5795f7826ad..f1988aab9f8 100644 --- a/sdks/rust/tests/view-pk-client/src/module_bindings/mod.rs +++ b/sdks/rust/tests/view-pk-client/src/module_bindings/mod.rs @@ -1,7 +1,7 @@ // THIS FILE IS AUTOMATICALLY GENERATED BY SPACETIMEDB. EDITS TO THIS FILE // WILL NOT BE SAVED. MODIFY TABLES IN YOUR MODULE SOURCE CODE INSTEAD. -// This was generated using spacetimedb cli version 2.7.0 (commit de4516d0d3d943cf4c3097c4ad31e4c7735b4f5b). +// This was generated using spacetimedb cli version 2.7.0 (commit 234f6c9c54406060ee2d781af9fd727e0d790650). #![allow(unused, clippy::all)] use spacetimedb_sdk::__codegen::{self as __sdk, __lib, __sats, __ws}; diff --git a/templates/chat-console-rs/spacetimedb/Cargo.toml b/templates/chat-console-rs/spacetimedb/Cargo.toml index 541a0cee506..f698206c09a 100644 --- a/templates/chat-console-rs/spacetimedb/Cargo.toml +++ b/templates/chat-console-rs/spacetimedb/Cargo.toml @@ -12,3 +12,9 @@ crate-type = ["cdylib"] [dependencies] spacetimedb = { path = "../../../crates/bindings" } log.workspace = true + +[dev-dependencies] +# Enables `spacetimedb::test_utils::ALL_TABLE_NAMES`, a link-time slice +# containing every table name registered by the `#[table]` macro. +# Only active during `cargo test` — has no effect on the WASM build. +spacetimedb = { path = "../../../crates/bindings", features = ["test-utils", "unstable"] } diff --git a/templates/chat-console-rs/spacetimedb/src/lib.rs b/templates/chat-console-rs/spacetimedb/src/lib.rs index e3917229efc..050aa4a12ad 100644 --- a/templates/chat-console-rs/spacetimedb/src/lib.rs +++ b/templates/chat-console-rs/spacetimedb/src/lib.rs @@ -92,3 +92,334 @@ pub fn identity_disconnected(ctx: &ReducerContext) { log::warn!("Disconnect event for unknown user with identity {:?}", ctx.sender()); } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_validate_name() { + assert_eq!(validate_name("Alice".to_string()), Ok("Alice".to_string())); + assert_eq!( + validate_name("".to_string()), + Err("Names must not be empty".to_string()) + ); + } + + /// Verify that all expected tables are registered in this module. + /// + /// `ALL_TABLE_NAMES` is a distributed slice populated at link time by the + /// `#[table]` macro — no setup or initialization required. + #[test] + fn all_tables_are_registered() { + let names = spacetimedb::test_utils::all_table_names(); + assert!(names.contains(&"user"), "expected table 'user', got: {names:?}"); + assert!(names.contains(&"message"), "expected table 'message', got: {names:?}"); + assert_eq!(names.len(), 2, "unexpected extra tables: {names:?}"); + } + + #[test] + fn module_def_is_registered() { + let spacetimedb::spacetimedb_lib::RawModuleDef::V10(module) = spacetimedb::test_utils::module_def() else { + panic!("expected v10 module definition"); + }; + + let tables = module.tables().expect("expected tables section"); + assert!(tables.iter().any(|table| table.source_name.as_ref() == "user")); + assert!(tables.iter().any(|table| table.source_name.as_ref() == "message")); + assert_eq!(tables.len(), 2, "unexpected extra tables: {tables:?}"); + + let reducers = module.reducers().expect("expected reducers section"); + for expected in [ + "set_name", + "send_message", + "init", + "identity_connected", + "identity_disconnected", + ] { + assert!( + reducers.iter().any(|reducer| reducer.source_name.as_ref() == expected), + "expected reducer '{expected}', got: {reducers:?}", + ); + } + } + + #[test] + fn test_context_can_insert_and_read_chat_rows() { + let ctx = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + let sender = Identity::ZERO; + + ctx.db.user().insert(User { + identity: sender, + name: Some("Alice".to_string()), + online: true, + }); + ctx.db.message().insert(Message { + sender, + sent: Timestamp::UNIX_EPOCH, + text: "Hello, SpacetimeDB!".to_string(), + }); + + assert_eq!(ctx.db.user().count(), 1); + assert_eq!(ctx.db.message().count(), 1); + + let users = ctx.db.user().iter().collect::>(); + assert_eq!(users.len(), 1); + assert_eq!(users[0].identity, sender); + assert_eq!(users[0].name.as_deref(), Some("Alice")); + assert!(users[0].online); + + let messages = ctx.db.message().iter().collect::>(); + assert_eq!(messages.len(), 1); + assert_eq!(messages[0].sender, sender); + assert_eq!(messages[0].sent, Timestamp::UNIX_EPOCH); + assert_eq!(messages[0].text, "Hello, SpacetimeDB!"); + } + + #[test] + fn test_context_can_make_two_independent_dbs() { + let ctx1 = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + let ctx2 = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + let sender = Identity::ZERO; + + ctx1.db.user().insert(User { + identity: sender, + name: Some("Alice".to_string()), + online: true, + }); + ctx2.db.message().insert(Message { + sender, + sent: Timestamp::UNIX_EPOCH, + text: "Hello, SpacetimeDB!".to_string(), + }); + + assert_eq!(ctx1.db.user().count(), 1); + assert_eq!(ctx2.db.user().count(), 0); + assert_eq!(ctx1.db.message().count(), 0); + assert_eq!(ctx2.db.message().count(), 1); + + let users = ctx1.db.user().iter().collect::>(); + assert_eq!(users.len(), 1); + assert_eq!(users[0].identity, sender); + assert_eq!(users[0].name.as_deref(), Some("Alice")); + assert!(users[0].online); + + let messages = ctx2.db.message().iter().collect::>(); + assert_eq!(messages.len(), 1); + assert_eq!(messages[0].sender, sender); + assert_eq!(messages[0].sent, Timestamp::UNIX_EPOCH); + assert_eq!(messages[0].text, "Hello, SpacetimeDB!"); + + assert_eq!(ctx2.db.user().iter().count(), 0); + assert_eq!(ctx1.db.message().iter().count(), 0); + } + + #[test] + fn test_context_can_call_reducer_with_clock_timestamp() { + let test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + let timestamp = Timestamp::from_micros_since_unix_epoch(123); + test.clock.set(timestamp); + + test.with_reducer_tx::<_, String>(spacetimedb::test_utils::TestAuth::internal(), |ctx| { + send_message(ctx, "Hello from a reducer".to_string()) + }) + .expect("send_message should succeed"); + + let messages = test.db.message().iter().collect::>(); + assert_eq!(messages.len(), 1); + assert_eq!(messages[0].sender, Identity::ZERO); + assert_eq!(messages[0].sent, timestamp); + assert_eq!(messages[0].text, "Hello from a reducer"); + } + + #[test] + fn test_reducer_with_jwt() { + // These are minimal claims to construct a valid JWT payload for tests. + let payload = r#"{"iss":"issuer","sub":"subject","iat":0}"#; + let expected_sender = spacetimedb::Identity::from_claims("issuer", "subject"); + let connection_id = spacetimedb::ConnectionId::from_u128(7); + + let test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + let timestamp = Timestamp::from_micros_since_unix_epoch(123); + test.clock.set(timestamp); + + let auth = spacetimedb::test_utils::TestAuth::from_jwt_payload(payload, connection_id) + .expect("JWT payload should be valid for tests"); + test.with_reducer_tx::<_, String>(auth, |ctx| send_message(ctx, "Hello from a reducer".to_string())) + .expect("send_message should succeed"); + + let messages = test.db.message().iter().collect::>(); + assert_eq!(messages.len(), 1); + assert_eq!(messages[0].sender, expected_sender); + assert_eq!(messages[0].sent, timestamp); + assert_eq!(messages[0].text, "Hello from a reducer"); + } + + #[test] + fn test_procedure_context_try_with_tx_commits_chat_rows() { + let test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + let timestamp = Timestamp::from_micros_since_unix_epoch(456); + test.clock.set(timestamp); + + let mut ctx = test.procedure_context(spacetimedb::test_utils::TestAuth::internal()); + ctx.try_with_tx::<_, String>(|tx| { + identity_connected(tx); + set_name(tx, "Alice".to_string())?; + send_message(tx, "Hello from a procedure tx".to_string())?; + Ok(()) + }) + .expect("procedure transaction should commit"); + + let user = test + .db + .user() + .identity() + .find(Identity::ZERO) + .expect("user should exist"); + assert_eq!(user.name.as_deref(), Some("Alice")); + assert!(user.online); + + let messages = test.db.message().iter().collect::>(); + assert_eq!(messages.len(), 1); + assert_eq!(messages[0].sender, Identity::ZERO); + assert_eq!(messages[0].sent, timestamp); + assert_eq!(messages[0].text, "Hello from a procedure tx"); + } + + fn fetch_status_message(ctx: &mut spacetimedb::ProcedureContext) -> Result { + let response = ctx + .http + .get("https://example.invalid/status") + .map_err(|err| err.to_string())?; + + if !response.status().is_success() { + return Err(format!("status endpoint returned {}", response.status())); + } + + response.into_body().into_string().map_err(|err| err.to_string()) + } + + fn online_users_query(from: spacetimedb::QueryBuilder) -> impl spacetimedb::Query { + from.user().r#where(|user| user.online).build() + } + + #[test] + fn test_context_can_run_typed_query() { + let test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + let alice = Identity::from_claims("issuer", "alice"); + let bob = Identity::from_claims("issuer", "bob"); + + test.db.user().insert(User { + identity: alice, + name: Some("Alice".to_string()), + online: true, + }); + test.db.user().insert(User { + identity: bob, + name: Some("Bob".to_string()), + online: false, + }); + + let rows: Vec = test + .run_query(spacetimedb::QueryBuilder {}.user().build()) + .expect("typed query should execute"); + + assert_eq!(rows.len(), 2); + assert!(rows.iter().any(|user| user.identity == alice)); + assert!(rows.iter().any(|user| user.identity == bob)); + } + + #[test] + fn test_context_can_run_filtered_query() { + let test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + let alice = Identity::from_claims("issuer", "alice"); + let bob = Identity::from_claims("issuer", "bob"); + + test.db.user().insert(User { + identity: alice, + name: Some("Alice".to_string()), + online: true, + }); + test.db.user().insert(User { + identity: bob, + name: Some("Bob".to_string()), + online: false, + }); + + let rows: Vec = test + .run_query(spacetimedb::QueryBuilder {}.user().r#where(|user| user.online).build()) + .expect("filtered query should execute"); + + assert_eq!(rows.len(), 1); + assert_eq!(rows[0].identity, alice); + assert_eq!(rows[0].name.as_deref(), Some("Alice")); + assert!(rows[0].online); + } + + #[test] + fn test_context_can_run_query_returning_view_pattern() { + let test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + let sender = Identity::ZERO; + test.db.user().insert(User { + identity: sender, + name: Some("Alice".to_string()), + online: true, + }); + + let rows: Vec = test + .run_query(online_users_query(spacetimedb::QueryBuilder {})) + .expect("query-returning view pattern should execute"); + + assert_eq!(rows.len(), 1); + assert_eq!(rows[0].identity, sender); + assert_eq!(rows[0].name.as_deref(), Some("Alice")); + assert!(rows[0].online); + } + + #[test] + fn test_procedure_context_can_mock_http() { + let test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + + let mut ctx = test + .procedure_context_builder(spacetimedb::test_utils::TestAuth::internal()) + .http(|_, request| { + assert_eq!(request.method().as_str(), "GET"); + assert_eq!(request.uri().to_string(), "https://example.invalid/status"); + + Ok(spacetimedb::http::Response::builder() + .status(200) + .header("content-type", "text/plain") + .body(spacetimedb::http::Body::from("chat service is healthy")) + .expect("test response should be valid")) + }) + .build(); + let message = fetch_status_message(&mut ctx).expect("mock HTTP call should succeed"); + + assert_eq!(message, "chat service is healthy"); + } + + #[test] + fn test_update() { + let test = spacetimedb::test_utils::TestContext::new().expect("test context should initialize"); + + let sender = Identity::ZERO; + + test.db.user().insert(User { + identity: sender, + name: Some("Alice".to_string()), + online: true, + }); + assert_eq!(test.db.user().iter().count(), 1); + + test.db.user().identity().update(User { + identity: sender, + name: Some("Alice2".to_string()), + online: true, + }); + assert_eq!(test.db.user().iter().count(), 1); + let user = test.db.user().identity().find(sender).expect("user should exist"); + assert_eq!(user.identity, sender); + assert_eq!(user.name.as_deref(), Some("Alice2")); + assert!(user.online); + } +} diff --git a/templates/chat-react-ts/spacetimedb/package.json b/templates/chat-react-ts/spacetimedb/package.json index c8a3cdbdaa4..486903fc9d8 100644 --- a/templates/chat-react-ts/spacetimedb/package.json +++ b/templates/chat-react-ts/spacetimedb/package.json @@ -5,12 +5,14 @@ "scripts": { "build": "cargo build -p spacetimedb-standalone && cargo run -p spacetimedb-cli -- build", "generate-ts": "cargo build -p spacetimedb-standalone && cargo run -p spacetimedb-cli -- generate --lang typescript --out-dir ts-codegen", - "publish": "cargo run -p spacetimedb-cli -- publish" + "publish": "cargo run -p spacetimedb-cli -- publish", + "test": "vitest run" }, "dependencies": { "spacetimedb": "workspace:^" }, "devDependencies": { - "typescript": "^5.9.3" + "typescript": "^5.9.3", + "vitest": "^3.2.4" } } diff --git a/templates/chat-react-ts/spacetimedb/src/index.test.ts b/templates/chat-react-ts/spacetimedb/src/index.test.ts new file mode 100644 index 00000000000..6d22cd5c5fe --- /dev/null +++ b/templates/chat-react-ts/spacetimedb/src/index.test.ts @@ -0,0 +1,77 @@ +import { describe, expect, it } from 'vitest'; +import { ConnectionId, Identity } from 'spacetimedb'; +import { + createModuleTestHarness, + TestAuth, +} from 'spacetimedb/server/test-utils'; + +import spacetime, * as moduleExports from './index'; + +function testAuth(subject: string, connectionId: bigint) { + return TestAuth.fromJwtPayload( + JSON.stringify({ iss: 'chat-template-test', sub: subject }), + new ConnectionId(connectionId) + ); +} + +describe('chat module unit tests', () => { + it('can test connect, set_name, and send_message reducers directly', () => { + const test = createModuleTestHarness(spacetime, moduleExports); + let alice: Identity | undefined; + + test.withReducerTx(testAuth('alice', 1n), ctx => { + alice = ctx.sender; + moduleExports.onConnect(ctx); + moduleExports.set_name(ctx, { name: 'Alice' }); + moduleExports.send_message(ctx, { text: 'hello' }); + }); + + expect(alice).toBeDefined(); + const user = test.db.user.identity.find(alice); + expect(user?.identity.toHexString()).toBe(alice.toHexString()); + expect(user?.name).toBe('Alice'); + expect(user?.online).toBe(true); + expect([...test.db.message.iter()].map(message => message.text)).toEqual([ + 'hello', + ]); + }); + + it('can use a procedure context transaction in tests', () => { + const test = createModuleTestHarness(spacetime, moduleExports); + const ctx = test.procedureContext(testAuth('alice', 1n)); + let alice: Identity | undefined; + + ctx.withTx(tx => { + alice = tx.sender; + moduleExports.onConnect(tx); + moduleExports.set_name(tx, { name: 'Alice' }); + }); + + expect(alice).toBeDefined(); + expect(test.db.user.identity.find(alice)?.name).toBe('Alice'); + }); + + it('can run typed queries against committed test state', () => { + const test = createModuleTestHarness(spacetime, moduleExports); + const aliceAuth = testAuth('alice', 1n); + const bobAuth = testAuth('bob', 2n); + + test.withReducerTx(aliceAuth, ctx => { + moduleExports.onConnect(ctx); + moduleExports.send_message(ctx, { text: 'hello from alice' }); + }); + test.withReducerTx(bobAuth, ctx => { + moduleExports.onConnect(ctx); + moduleExports.send_message(ctx, { text: 'hello from bob' }); + }); + + const viewCtx = test.viewContext(aliceAuth); + const aliceMessages = test.runQuery( + viewCtx.from.message.where(message => message.text.eq('hello from alice')) + ); + + expect(aliceMessages.map(message => message.text)).toEqual([ + 'hello from alice', + ]); + }); +}); diff --git a/templates/chat-react-ts/spacetimedb/src/test/setup.ts b/templates/chat-react-ts/spacetimedb/src/test/setup.ts new file mode 100644 index 00000000000..4fdcb8be9be --- /dev/null +++ b/templates/chat-react-ts/spacetimedb/src/test/setup.ts @@ -0,0 +1,13 @@ +if (!Symbol.dispose) { + Object.defineProperty(Symbol, 'dispose', { + value: Symbol.for('Symbol.dispose'), + }); +} + +if (!Set.prototype.isSubsetOf) { + Object.defineProperty(Set.prototype, 'isSubsetOf', { + value: function isSubsetOf(this: Set, other: Set) { + return [...this].every(value => other.has(value)); + }, + }); +} diff --git a/templates/chat-react-ts/spacetimedb/vitest.config.ts b/templates/chat-react-ts/spacetimedb/vitest.config.ts new file mode 100644 index 00000000000..2db859158ff --- /dev/null +++ b/templates/chat-react-ts/spacetimedb/vitest.config.ts @@ -0,0 +1,11 @@ +import { defineConfig } from 'vitest/config'; +import { spacetimedbModuleTestPlugin } from 'spacetimedb/server/test-utils/vitest'; + +export default defineConfig({ + cacheDir: 'node_modules/.vite-module-tests', + plugins: [spacetimedbModuleTestPlugin()], + test: { + environment: 'node', + setupFiles: ['src/test/setup.ts'], + }, +}); diff --git a/tools/ci/README.md b/tools/ci/README.md index 78147590f46..e503f251dfe 100644 --- a/tools/ci/README.md +++ b/tools/ci/README.md @@ -70,6 +70,21 @@ Usage: wasm-bindings - `--help`: Print help (see a summary with '-h') +### `portable-datastore` + +Checks the portable datastore wasm boundary + +Ensures the table, datastore portable feature, portable datastore crate, and wasm adapter compile for wasm32-unknown-unknown, then runs the portable datastore unit tests natively. + +**Usage:** +```bash +Usage: portable-datastore +``` + +**Options:** + +- `--help`: Print help (see a summary with '-h') + ### `dlls` Deprecated; use `cargo regen csharp dlls`. diff --git a/tools/ci/src/main.rs b/tools/ci/src/main.rs index 39f0f5a2e42..3d20b4a5da3 100644 --- a/tools/ci/src/main.rs +++ b/tools/ci/src/main.rs @@ -8,6 +8,7 @@ use std::ffi::OsStr; use std::ffi::OsString; use std::path::Path; use std::path::PathBuf; +use std::process::{Command, Stdio}; use std::{env, fs}; const README_PATH: &str = "tools/ci/README.md"; @@ -103,6 +104,31 @@ fn git_tracked_files(pathspec: &str) -> Result> { Ok(output.lines().map(PathBuf::from).collect()) } +fn executable_on_path(name: &str) -> bool { + let Some(paths) = env::var_os("PATH") else { + return false; + }; + + env::split_paths(&paths).any(|path| path.join(name).is_file()) +} + +fn have_emscripten() -> bool { + executable_on_path("emcc") || executable_on_path("emcc.bat") +} + +fn node_has_browser_websocket() -> bool { + let Ok(status) = Command::new("node") + .args(["-e", "if (typeof globalThis.WebSocket !== 'function') process.exit(1)"]) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .status() + else { + return false; + }; + + status.success() +} + fn package_json_string_value(package_json: &Value, key: &str) -> Option { package_json.get(key)?.as_str().map(str::to_owned) } @@ -309,6 +335,11 @@ enum CiCmd { /// /// Runs tests for the codegen crate and builds a test module with the wasm bindings. WasmBindings, + /// Checks the portable datastore wasm boundary + /// + /// Ensures the table, datastore portable feature, portable datastore crate, and wasm adapter + /// compile for wasm32-unknown-unknown, then runs the portable datastore unit tests natively. + PortableDatastore, /// Deprecated; use `cargo regen csharp dlls`. /// /// Builds and packs C# DLLs and NuGet packages for local Unity workflows. @@ -488,8 +519,7 @@ fn main() -> Result<()> { // Exclude smoketests from `cargo test --all` since they require pre-built binaries. // Smoketests have their own dedicated command: `cargo ci smoketests` - cmd!( - "cargo", + let mut test_args: Vec = [ "test", "--all", "--exclude", @@ -501,9 +531,16 @@ fn main() -> Result<()> { "--", "--test-threads=2", "--skip", - "unreal" - ) - .run()?; + "unreal", + ] + .into_iter() + .map(OsString::from) + .collect(); + if !have_emscripten() { + eprintln!("Skipping C++ integration tests because `emcc` was not found in PATH"); + test_args.extend(["--skip", "cpp"].into_iter().map(OsString::from)); + } + cmd("cargo", test_args).run()?; // Bindings snapshot tests rely on the unstable feature, // as they compile and test APIs which are gated behind that feature, // e.g. procedures, HTTP handlers. @@ -513,7 +550,7 @@ fn main() -> Result<()> { "-p", "spacetimedb", "--features", - "unstable", + "unstable,test-utils", "--", "--test-threads=2", ) @@ -533,8 +570,7 @@ fn main() -> Result<()> { ) .run()?; // SDK procedure tests intentionally make localhost HTTP requests. - cmd!( - "cargo", + let mut sdk_test_args: Vec = [ "test", "-p", "spacetimedb-sdk", @@ -543,23 +579,40 @@ fn main() -> Result<()> { "--", "--test-threads=2", "--skip", - "unreal" - ) - .run()?; + "unreal", + ] + .into_iter() + .map(OsString::from) + .collect(); + if !have_emscripten() { + eprintln!("Skipping C++ SDK tests because `emcc` was not found in PATH"); + sdk_test_args.extend(["--skip", "cpp"].into_iter().map(OsString::from)); + } + cmd("cargo", sdk_test_args).env("CI", "true").run()?; // Run the same SDK suite against wasm/browser test clients. - cmd!( - "cargo", - "test", - "-p", - "spacetimedb-sdk", - "--features", - "allow_loopback_http_for_tests,browser", - "--", - "--test-threads=2", - "--skip", - "unreal" - ) - .run()?; + if node_has_browser_websocket() { + let mut browser_sdk_test_args: Vec = [ + "test", + "-p", + "spacetimedb-sdk", + "--features", + "allow_loopback_http_for_tests,browser", + "--", + "--test-threads=2", + "--skip", + "unreal", + ] + .into_iter() + .map(OsString::from) + .collect(); + if !have_emscripten() { + eprintln!("Skipping C++ SDK browser tests because `emcc` was not found in PATH"); + browser_sdk_test_args.extend(["--skip", "cpp"].into_iter().map(OsString::from)); + } + cmd("cargo", browser_sdk_test_args).env("CI", "true").run()?; + } else { + eprintln!("Skipping SDK browser tests because `node` does not provide `globalThis.WebSocket`"); + } // TODO: This should check for a diff at the start. If there is one, we should alert the user // that we're disabling diff checks because they have a dirty git repo, and to re-run in a clean one // if they want those checks. @@ -672,6 +725,49 @@ fn main() -> Result<()> { cmd!(cli_path, "build", "--module-path", "modules/module-test",).run()?; } + Some(CiCmd::PortableDatastore) => { + cmd!( + "cargo", + "check", + "-p", + "spacetimedb-table", + "--target", + "wasm32-unknown-unknown" + ) + .run()?; + cmd!( + "cargo", + "check", + "-p", + "spacetimedb-datastore", + "--no-default-features", + "--features", + "portable", + "--target", + "wasm32-unknown-unknown" + ) + .run()?; + cmd!( + "cargo", + "check", + "-p", + "spacetimedb-portable-datastore", + "--target", + "wasm32-unknown-unknown" + ) + .run()?; + cmd!( + "cargo", + "check", + "-p", + "spacetimedb-portable-datastore-wasm", + "--target", + "wasm32-unknown-unknown" + ) + .run()?; + cmd!("cargo", "test", "-p", "spacetimedb-portable-datastore").run()?; + } + Some(CiCmd::Dlls) => { eprintln!("warning: `cargo ci dlls` is deprecated; use `cargo regen csharp dlls` instead"); cmd!("cargo", "regen", "csharp", "dlls").run()?;