1
//! A bytecode-only script against the real server: the edit form's notice and
2
//! the refusal of a toggle, which has no source to save.
3

            
4
use crate::test_support::{key, press};
5
use cli_core::forms::parse_loaded_reply;
6
use cli_core::transaction::submit_call;
7
use forms::SCRIPT_EDIT;
8
use rpc::{ScriptCtx, Session, compile_script};
9
use server::user::User;
10
use sqlx::PgPool;
11
use sqlx::types::Uuid;
12

            
13
use super::scripts::pending;
14
use super::scripts::{load_states, scripts_app};
15
use crate::route::ScriptRoute;
16
use ratatui::crossterm::event::KeyCode;
17

            
18
3
async fn reply(session: &mut Session, form: &str) -> String {
19
3
    session.handle_form(&format!("(:id 1 :form {form})")).await
20
3
}
21

            
22
2
async fn seed_user(pool: &PgPool) -> anyhow::Result<Uuid> {
23
2
    let user_id = Uuid::new_v4();
24
2
    sqlx::query(
25
2
        "INSERT INTO users \
26
2
         (id, user_name, email, photo, verified, user_password, user_role, database_name, created_at) \
27
2
         VALUES ($1, $2, $3, 'x', false, 'pw', 'user', 'db', NOW())",
28
2
    )
29
2
    .bind(user_id)
30
2
    .bind(format!("u{user_id}"))
31
2
    .bind(format!("{user_id}@test.com"))
32
2
    .execute(pool)
33
2
    .await?;
34
2
    Ok(user_id)
35
2
}
36

            
37
2
async fn bytecode_only(pool: &PgPool) -> anyhow::Result<(Session, String)> {
38
2
    let user = seed_user(pool).await?;
39
2
    let id = User { id: user }
40
2
        .create_script(compile_script("(+ 1 2)")?, Some("old".to_string()))
41
2
        .await?;
42
2
    Ok((Session::new(ScriptCtx::new(user))?, id.to_string()))
43
2
}
44

            
45
#[sqlx::test(migrator = "server::db::MIGRATOR")]
46
async fn the_edit_form_shows_the_no_source_notice(pool: PgPool) -> anyhow::Result<()> {
47
    let _guard = server::db::install_test_pool(pool.clone());
48
    let (mut session, id) = bytecode_only(&pool).await?;
49
    let loaded = reply(
50
        &mut session,
51
        &format!(r#"(load-form "script-edit" "{id}")"#),
52
    )
53
    .await;
54
    let (mut app, _) = scripts_app();
55

            
56
    app.deliver_script_edit_reply(Some(app.window_of(crate::view::ViewId::Scripts)), &loaded);
57

            
58
    let Some(crate::modal::Modal::Form(form)) = app.overlays.top() else {
59
        anyhow::bail!("the edit form is not open: {}", app.status);
60
    };
61
    assert!(
62
1
        form.notices.iter().any(|n| n.contains("no source")),
63
        "{:?}",
64
        form.notices
65
    );
66
    assert_eq!(form.values().text("source"), Some(""));
67
    Ok(())
68
}
69

            
70
#[sqlx::test(migrator = "server::db::MIGRATOR")]
71
async fn toggling_a_bytecode_only_script_shows_the_error_not_success(
72
    pool: PgPool,
73
) -> anyhow::Result<()> {
74
    let _guard = server::db::install_test_pool(pool.clone());
75
    let (mut session, id) = bytecode_only(&pool).await?;
76
    let (mut app, _) = scripts_app();
77
    load_states(&mut app, &[(&id, "old", true, false)])?;
78
    press(&mut app, key(KeyCode::Char('t')));
79
1
    let loading = pending(&app, |r| matches!(r, ScriptRoute::Toggle { .. }))
80
        .ok_or_else(|| anyhow::anyhow!("load not sent"))?;
81
    let loaded = reply(
82
        &mut session,
83
        &format!(r#"(load-form "script-edit" "{id}")"#),
84
    )
85
    .await;
86

            
87
    app.route_reply(loading, &loaded);
88
1
    let saving = pending(&app, |r| matches!(r, ScriptRoute::Toggled { .. }))
89
        .ok_or_else(|| anyhow::anyhow!("save not sent"))?;
90
    let mut values = parse_loaded_reply(&SCRIPT_EDIT, &loaded).map_err(anyhow::Error::msg)?;
91
    values.set("enabled", "false");
92
    let refused = reply(&mut session, &submit_call(&SCRIPT_EDIT, &values)).await;
93
    app.route_reply(
94
        saving,
95
        &refused.replacen(":id 1", &format!(":id {saving}"), 1),
96
    );
97

            
98
    assert!(
99
        app.status.starts_with("can't switch old: source:"),
100
        "{}",
101
        app.status
102
    );
103
    assert!(!app.status.contains("disabled"), "{}", app.status);
104
    Ok(())
105
}