tauri-apps / tauri

Build smaller, faster, and more secure desktop applications with a web frontend.
https://tauri.app
Apache License 2.0
82.08k stars 2.47k forks source link

[bug] unable to reference gamestate during tests #8714

Closed SvenKeimpema closed 7 months ago

SvenKeimpema commented 7 months ago

Describe the bug

I'm currently trying to add unit tests to my tauri application with tauri's tauri::test::mock_builder(). However i am currently having some issues cloning/accessing the gamestate.

The reason why i don't to give out my game information with tauri's commands(tauri::command) is due to it being pretty usafe since end-users can also call these command's with js(in the browser console).

I've also tried to get the state by calling window.state:

let app: App<MockRuntime> = create_app(tauri::test::mock_builder());
let window: Window<MockRuntime> = app.get_window("main").unwrap();        
let state = window.state<GameState>();

However this give me the error:

state() called before manage() for given type

Is there a possible way around this without creating commands to do this? Or is it simply not implemented yet?

Reproduction

No response

Expected behavior

No response

Full tauri info output

[✔] Environment
    - OS: Ubuntu 22.04 X64
    ✔ webkit2gtk-4.0: 2.42.4
    ✔ rsvg2: 2.52.5
    ✔ rustc: 1.75.0 (82e1608df 2023-12-21)
    ✔ cargo: 1.75.0 (1d8b05cdd 2023-11-20)
    ✔ rustup: 1.26.0 (5af9b9484 2023-04-05)
    ✔ Rust toolchain: stable-x86_64-unknown-linux-gnu (default)
    - node: 20.10.0
    - yarn: 1.22.19
    - npm: 10.2.3
    - bun: 1.0.3

[-] Packages
    - tauri [RUST]: 1.5.4
    - tauri-build [RUST]: 1.5.1
    - wry [RUST]: 0.24.7
    - tao [RUST]: 0.16.7
    - @tauri-apps/api [NPM]: 1.5.2 (outdated, latest: 1.5.3)
    - @tauri-apps/cli [NPM]: 1.5.9

[-] App
    - build-type: bundle
    - CSP: unset
    - distDir: ../dist
    - devPath: http://localhost:8000/index.html
    - bundler: Vite

Stack trace

state() called before manage() for given type
thread 'test::move_test::move_gen_tests::test_moves_for_square' panicked at /home/sven/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tauri-1.5.4/src/lib.rs:900:8:
state() called before manage() for given type
stack backtrace:
   0:     0x55bf228a03ec - std::backtrace_rs::backtrace::libunwind::trace::ha637c64ce894333a
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/../../backtrace/src/backtrace/libunwind.rs:104:5
   1:     0x55bf228a03ec - std::backtrace_rs::backtrace::trace_unsynchronized::h47f62dea28e0c88d
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x55bf228a03ec - std::sys_common::backtrace::_print_fmt::h9eef0abe20ede486
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/sys_common/backtrace.rs:67:5
   3:     0x55bf228a03ec - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hed7f999df88cc644
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x55bf228cc8e0 - core::fmt::rt::Argument::fmt::h1539a9308b8d058d
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/core/src/fmt/rt.rs:142:9
   5:     0x55bf228cc8e0 - core::fmt::write::h3a39390d8560d9c9
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/core/src/fmt/mod.rs:1120:17
   6:     0x55bf2289d91f - std::io::Write::write_fmt::h040b369f3a0f9cbc
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/io/mod.rs:1762:15
   7:     0x55bf228a01d4 - std::sys_common::backtrace::_print::h894006fb5c6f3d45
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x55bf228a01d4 - std::sys_common::backtrace::print::h23a2d212c6fff936
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x55bf228a1e97 - std::panicking::default_hook::{{closure}}::h8a1d2ee00185001a
  10:     0x55bf228a1b83 - std::panicking::default_hook::h6038f2eba384e475
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/panicking.rs:289:9
  11:     0x55bf22218797 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h0ad024c9bdc6d768
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/alloc/src/boxed.rs:2021:9
  12:     0x55bf22218797 - test::test_main::{{closure}}::h7500b54257359db6
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/test/src/lib.rs:138:21
  13:     0x55bf228a24a8 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h1f8f335eaa9cfaee
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/alloc/src/boxed.rs:2021:9
  14:     0x55bf228a24a8 - std::panicking::rust_panic_with_hook::h2b5517d590cab22e
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/panicking.rs:783:13
  15:     0x55bf228a21fe - std::panicking::begin_panic_handler::{{closure}}::h233112c06e0ef43e
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/panicking.rs:657:13
  16:     0x55bf228a08b6 - std::sys_common::backtrace::__rust_end_short_backtrace::h6e893f24d7ebbff8
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/sys_common/backtrace.rs:170:18
  17:     0x55bf228a1f62 - rust_begin_unwind
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/panicking.rs:645:5
  18:     0x55bf221b57e5 - core::panicking::panic_fmt::hbf0e066aabfa482c
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/core/src/panicking.rs:72:14
  19:     0x55bf221b57a3 - core::panicking::panic_display::h68e6012a1c287ca5
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/core/src/panicking.rs:178:5
  20:     0x55bf221b57a3 - core::panicking::panic_str::h1ca8e3b0d2d70f80
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/core/src/panicking.rs:152:5
  21:     0x55bf221b57a3 - core::option::expect_failed::h1ae4bac6b7606972
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/core/src/option.rs:1985:5
  22:     0x55bf221cfe12 - core::option::Option<T>::expect::h769fda1ca6b50173
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/core/src/option.rs:894:21
  23:     0x55bf221c2054 - tauri::Manager::state::h98cd401ab9d59469
                               at /home/sven/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tauri-1.5.4/src/lib.rs:895:5
  24:     0x55bf221bf247 - chess::test::move_test::move_gen_tests::test_moves_for_square::hb71b50468982b6b6
                               at /home/sven/Desktop/tauri/chess/src-tauri/src/test/move_test.rs:36:44
  25:     0x55bf221cb237 - chess::test::move_test::move_gen_tests::test_moves_for_square::{{closure}}::hb910dba6aa52d2a1
                               at /home/sven/Desktop/tauri/chess/src-tauri/src/test/move_test.rs:33:31
  26:     0x55bf221dffe6 - core::ops::function::FnOnce::call_once::h2165580b0f09b359
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/core/src/ops/function.rs:250:5
  27:     0x55bf2221ddef - core::ops::function::FnOnce::call_once::h2b5d431d5046312d
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/core/src/ops/function.rs:250:5
  28:     0x55bf2221ddef - test::__rust_begin_short_backtrace::h7714669af176e207
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/test/src/lib.rs:628:18
  29:     0x55bf2221ce05 - test::run_test_in_process::{{closure}}::h85218797dcbcec12
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/test/src/lib.rs:651:60
  30:     0x55bf2221ce05 - <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once::h76c106ea31c14d7b
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/core/src/panic/unwind_safe.rs:272:9
  31:     0x55bf2221ce05 - std::panicking::try::do_call::h3bd5d0f6620ee946
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/panicking.rs:552:40
  32:     0x55bf2221ce05 - std::panicking::try::h232e535e4744fe31
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/panicking.rs:516:19
  33:     0x55bf2221ce05 - std::panic::catch_unwind::h3ce3bd6ca8dbac68
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/panic.rs:142:14
  34:     0x55bf2221ce05 - test::run_test_in_process::hc97b257c9621286a
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/test/src/lib.rs:651:27
  35:     0x55bf2221ce05 - test::run_test::{{closure}}::h2aa798823a438d38
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/test/src/lib.rs:574:43
  36:     0x55bf221e50c6 - test::run_test::{{closure}}::h87d231c78fb59832
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/test/src/lib.rs:602:41
  37:     0x55bf221e50c6 - std::sys_common::backtrace::__rust_begin_short_backtrace::he6df152596625789
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/sys_common/backtrace.rs:154:18
  38:     0x55bf221ea177 - std::thread::Builder::spawn_unchecked_::{{closure}}::{{closure}}::ha94be15d1e0acf36
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/thread/mod.rs:529:17
  39:     0x55bf221ea177 - <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once::h9dc6bd6372f16606
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/core/src/panic/unwind_safe.rs:272:9
  40:     0x55bf221ea177 - std::panicking::try::do_call::h9057517f80303cd4
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/panicking.rs:552:40
  41:     0x55bf221ea177 - std::panicking::try::he3a30b11ee45340c
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/panicking.rs:516:19
  42:     0x55bf221ea177 - std::panic::catch_unwind::h410458cfd3528b8b
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/panic.rs:142:14
  43:     0x55bf221ea177 - std::thread::Builder::spawn_unchecked_::{{closure}}::h72ac3bdb567b737b
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/thread/mod.rs:528:30
  44:     0x55bf221ea177 - core::ops::function::FnOnce::call_once{{vtable.shim}}::h38f34da3654a8344
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/core/src/ops/function.rs:250:5
  45:     0x55bf228a7115 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::hc7eafaff61e32df9
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/alloc/src/boxed.rs:2007:9
  46:     0x55bf228a7115 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h6ba4a5de48dd2304
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/alloc/src/boxed.rs:2007:9
  47:     0x55bf228a7115 - std::sys::unix::thread::Thread::new::thread_start::he469335aef763e45
                               at /rustc/82e1608dfa6e0b5569232559e3d385fea5a93112/library/std/src/sys/unix/thread.rs:108:17
  48:     0x7f8f2a294ac3 - start_thread
                               at ./nptl/pthread_create.c:442:8
  49:     0x7f8f2a326850 - __GI___clone3
                               at ./misc/../sysdeps/unix/sysv/linux/x86_64/clone3.S:81
  50:                0x0 - <unknown>

Additional context

No response

amrbashir commented 7 months ago

state() called before manage() for given type

This error is kind of self-explanatory, you're trying to retrieve a state using .state() before storing it using .manage(). You don't need a command to store a state, you can just do app.manage(YourState)

SvenKeimpema commented 7 months ago

@amrbashir Hey, sorry forgot to add the code how i create the app:

pub fn create_app<R: tauri::Runtime>(builder: tauri::Builder<R>) -> tauri::App<R> {
    let app_state = TauriStateHolder(
        Mutex::new(Game { ..Default::default() }),
    );

    // init the game so we can make moves, see the board, ect.
    app_state.0.lock().unwrap().init_game();

    builder
        .manage(app_state)
        .invoke_handler(tauri::generate_handler![
            get_board, select_square, move_piece, undo_move, check_game_won
        ])
        .build(tauri::generate_context!())
        .expect("failed to build app")
}

In here i do call manage however it seems to not accessible/forgotten whenever i use create_app in a test:

#[test]
fn test_make_move() {
        let app: App<MockRuntime> = create_app(tauri::test::mock_builder());
        let window: Window<MockRuntime> = app.get_window("main").unwrap();
        call_make_move(&window, r#"{"startSq": 52, "destSq": 36}"#);

        //error: `state() called before manage() for given type`
        let game_state = window.state::<ChessGameState>();
        println!("{:?}", get_bitboard(&window).unwrap());
}
amrbashir commented 7 months ago

You called manage on TauriStateHelper type but you're trying to retreive ChessGameState which is not the same type you stored and that's why it is failing.

SvenKeimpema commented 7 months ago

for anyone who has this issue in the future instead of getting the state which you stored, you need to get the tauri state:

let app: App<MockRuntime> = create_app(tauri::test::mock_builder());
let window: Window<MockRuntime> = app.get_window("main").unwrap();
window.state::<TauriStateHolder>()