Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions crates/testing/src/sdk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,9 @@ fn split_command_string(command: &str) -> (String, Vec<String>) {
// Note: this function is memoized to ensure we only compile each client once.
fn compile_client(compile_command: &str, client_project: &str) {
let client_project = client_project.to_owned();
let compile_command = compile_command.to_owned();

memoized!(|client_project: String| -> () {
memoized!(|(client_project, compile_command): (String, String)| -> () {
let (exe, args) = split_command_string(compile_command);

let output = cmd(exe, args)
Expand All @@ -378,7 +379,14 @@ fn compile_client(compile_command: &str, client_project: &str) {
fn run_client(run_command: &str, client_project: &str, db_name: &str) {
let (exe, args) = split_command_string(run_command);

let output = cmd(exe, args)
let is_wasm32_unknown_unknown = run_command.contains("--target wasm32-unknown-unknown");

let mut command = cmd(exe, args);
if is_wasm32_unknown_unknown {
command = command.env("CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER", "wasm-bindgen-test-runner");
}

let output = command
.dir(client_project)
.env(TEST_CLIENT_PROJECT_ENV_VAR, client_project)
.env(TEST_DB_NAME_ENV_VAR, db_name)
Expand Down
37 changes: 33 additions & 4 deletions sdks/rust/tests/test-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,42 @@ license-file = "LICENSE"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
default = ["native"]

# Builds the existing CLI test client.
native = [
"dep:test-counter",
"dep:tokio",
"dep:env_logger",
"dep:rand",
]

# Builds the wasm-bindgen-test based client tests.
wasm = [
"spacetimedb-sdk/web",
"dep:wasm-bindgen-test",
"dep:wasm-bindgen-futures",
"dep:js-sys",
]

[[bin]]
name = "test-client"
path = "src/main.rs"
required-features = ["native"]

[dependencies]
spacetimedb-sdk = { path = "../.." }
test-counter = { path = "../test-counter" }
tokio.workspace = true
anyhow.workspace = true
env_logger.workspace = true
rand.workspace = true

test-counter = { path = "../test-counter", optional = true }
tokio = { workspace = true, optional = true }
env_logger = { workspace = true, optional = true }
rand = { workspace = true, optional = true }

wasm-bindgen-test = { version = "0.3", optional = true }
wasm-bindgen-futures = { version = "0.4", optional = true }
js-sys = { version = "0.3", optional = true }

[lints]
workspace = true
28 changes: 28 additions & 0 deletions sdks/rust/tests/test-client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
mod wasm_tests {
mod module_bindings;

use module_bindings::{DbConnection, RemoteDbContext};
use wasm_bindgen_test::wasm_bindgen_test;

const LOCALHOST: &str = "http://localhost:3000";

fn db_name_or_panic() -> String {
std::env::var("SPACETIME_SDK_TEST_DB_NAME").expect("Failed to read db name from env")
}

#[wasm_bindgen_test]
fn wasm_smoke_connect_and_disconnect() {
let name = db_name_or_panic();

let conn = DbConnection::builder()
.with_module_name(name)
.with_uri(LOCALHOST)
.build()
.expect("Failed to build DbConnection");

// Basic smoke: immediately disconnect. If the wasm build is broken (missing web feature
// plumbing), this tends to fail before here.
conn.disconnect().expect("disconnect failed");
}
}
18 changes: 18 additions & 0 deletions sdks/rust/tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@ macro_rules! declare_tests_with_suffix {
.build()
}

#[test]
fn wasm_smoke() {
Test::builder()
.with_name("wasm-smoke")
.with_module(MODULE)
.with_client(CLIENT)
.with_language("rust")
.with_bindings_dir("src/module_bindings")
.with_compile_command(
"cargo test --target wasm32-unknown-unknown --no-default-features --features wasm --no-run",
)
.with_run_command(
"cargo test --target wasm32-unknown-unknown --no-default-features --features wasm",
)
.build()
.run();
}

#[test]
fn insert_primitive() {
make_test("insert-primitive").run();
Expand Down
Loading