1
//! The Options dialog against a real session: a save survives a new attach.
2

            
3
use super::options::{attached_blank, choose, next_field, opened, saves};
4
use super::report_chart::echoed;
5
use crate::app::App;
6
use crate::keymap::VimState;
7
use crate::tabs::nms_eval::double::Sent;
8
use crate::test_support::{key, press};
9
use ratatui::crossterm::event::KeyCode;
10
use rpc::{ScriptCtx, Session};
11
use server::lossless::seed;
12
use sqlx::PgPool;
13
use tvui::{Role, Theme};
14

            
15
/// Sends the last request of `sent` to `session` and routes its reply into `app`.
16
2
async fn serve_last(app: &mut App, sent: &Sent, session: &mut Session) -> anyhow::Result<()> {
17
2
    echoed(app, sent).await;
18
2
    let frame = sent.frames().pop().unwrap_or_default();
19
2
    let id: i64 = frame
20
2
        .split_whitespace()
21
2
        .nth(1)
22
2
        .and_then(|id| id.parse().ok())
23
        .ok_or_else(|| anyhow::anyhow!("no request id in {frame}"))?;
24
2
    let wire = session.handle_form(&frame).await;
25
2
    app.route_reply(id, &wire);
26
2
    Ok(())
27
2
}
28

            
29
#[sqlx::test(migrator = "server::db::MIGRATOR")]
30
async fn a_save_restores_after_a_new_attach_and_writes_only_the_changed_keys(
31
    pool: PgPool,
32
) -> anyhow::Result<()> {
33
    let _guard = server::db::install_test_pool(pool.clone());
34
    let fixture = seed(&pool).await?;
35
    let mut session = Session::new(ScriptCtx::new(fixture.user))?;
36

            
37
    let (mut app, sent) = opened([None; 5]);
38
    choose(&mut app, KeyCode::Down, 1);
39
    next_field(&mut app);
40
    choose(&mut app, KeyCode::Down, 2);
41
    for _ in 0..4 {
42
        press(&mut app, key(KeyCode::Tab));
43
    }
44
    press(&mut app, key(KeyCode::Enter));
45
    assert_eq!(saves(&app).len(), 1);
46
    serve_last(&mut app, &sent, &mut session).await?;
47
    assert!(app.pending_form.is_none() && app.overlays.is_empty());
48

            
49
    let (mut again, sent) = attached_blank();
50
    serve_last(&mut again, &sent, &mut session).await?;
51
    let turbo = Theme::preset("turbo")?;
52
    assert_eq!(again.theme.bg(Role::Desktop), turbo.bg(Role::Desktop));
53
    assert_eq!(again.keys.vim(), VimState::Insert);
54
    assert_eq!(again.keys.indicator(), "vim:insert");
55

            
56
    let colors = session
57
        .handle_form(r#"(:id 9 :form (get-config "tui-colors"))"#)
58
        .await;
59
    assert!(colors.contains("nil"), "{colors}");
60
    Ok(())
61
}