Skip to content
Merged
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
178 changes: 169 additions & 9 deletions src/features/sessions/input/new_session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,82 @@ mod tests {
handle_key(&mut state, key(KeyCode::Char('b'))),
NewSessionInputResult::Edited
);
assert_eq!(state.new_session_path, "abc");
assert_eq!(state.new_session_cursor, 2);
handle_key(&mut state, key(KeyCode::Backspace));
assert_eq!(state.new_session_path, "ac");
handle_key(&mut state, key(KeyCode::End));
assert_eq!(state.new_session_cursor, 2);
assert_eq!(
(state.new_session_path.as_str(), state.new_session_cursor),
("abc", 2)
);

assert_eq!(
handle_key(&mut state, key(KeyCode::Left)),
NewSessionInputResult::Edited
);
assert_eq!(
(state.new_session_path.as_str(), state.new_session_cursor),
("abc", 1)
);
assert_eq!(
handle_key(&mut state, key(KeyCode::Right)),
NewSessionInputResult::Edited
);
assert_eq!(
(state.new_session_path.as_str(), state.new_session_cursor),
("abc", 2)
);

assert_eq!(
handle_key(&mut state, key(KeyCode::Home)),
NewSessionInputResult::Edited
);
assert_eq!(
(state.new_session_path.as_str(), state.new_session_cursor),
("abc", 0)
);
assert_eq!(
handle_key(&mut state, key(KeyCode::Left)),
NewSessionInputResult::Edited
);
assert_eq!(
(state.new_session_path.as_str(), state.new_session_cursor),
("abc", 0)
);
assert_eq!(
handle_key(&mut state, key(KeyCode::Backspace)),
NewSessionInputResult::Edited
);
assert_eq!(
(state.new_session_path.as_str(), state.new_session_cursor),
("abc", 0)
);

assert_eq!(
handle_key(&mut state, key(KeyCode::End)),
NewSessionInputResult::Edited
);
assert_eq!(
(state.new_session_path.as_str(), state.new_session_cursor),
("abc", 3)
);
assert_eq!(
handle_key(&mut state, key(KeyCode::Right)),
NewSessionInputResult::Edited
);
assert_eq!(
(state.new_session_path.as_str(), state.new_session_cursor),
("abc", 3)
);

assert_eq!(
handle_key(&mut state, key(KeyCode::Left)),
NewSessionInputResult::Edited
);
assert_eq!(
handle_key(&mut state, key(KeyCode::Backspace)),
NewSessionInputResult::Edited
);
assert_eq!(
(state.new_session_path.as_str(), state.new_session_cursor),
("ac", 1)
);
}

#[test]
Expand All @@ -104,6 +174,30 @@ mod tests {
],
});

assert_eq!(
handle_key(&mut state, key(KeyCode::Up)),
NewSessionInputResult::MovedCompletion
);
assert_eq!(
state
.new_session_completion
.as_ref()
.unwrap()
.selected_index,
1
);
assert_eq!(
handle_key(&mut state, key(KeyCode::Down)),
NewSessionInputResult::MovedCompletion
);
assert_eq!(
state
.new_session_completion
.as_ref()
.unwrap()
.selected_index,
0
);
assert_eq!(
handle_key(&mut state, key(KeyCode::Down)),
NewSessionInputResult::MovedCompletion
Expand All @@ -116,43 +210,109 @@ mod tests {
.selected_index,
1
);
assert_eq!(
handle_key(&mut state, key(KeyCode::Up)),
NewSessionInputResult::MovedCompletion
);
assert_eq!(
state
.new_session_completion
.as_ref()
.unwrap()
.selected_index,
0
);
assert_eq!(
handle_key(&mut state, key(KeyCode::Tab)),
NewSessionInputResult::AcceptCompletion
);

state.new_session_completion = Some(PathCompletionState {
query: "missing".into(),
selected_index: 0,
results: Vec::new(),
});
assert_eq!(
handle_key(&mut state, key(KeyCode::Up)),
NewSessionInputResult::MovedCompletion
);
assert_eq!(
state
.new_session_completion
.as_ref()
.unwrap()
.selected_index,
0
);
assert_eq!(
handle_key(&mut state, key(KeyCode::Down)),
NewSessionInputResult::MovedCompletion
);
assert_eq!(
state
.new_session_completion
.as_ref()
.unwrap()
.selected_index,
0
);

accept_completion(&mut state, "/b/".into());
assert_eq!(state.new_session_path, "/b/");
assert_eq!(state.new_session_cursor, 3);
assert!(state.new_session_completion.is_none());
}

#[test]
fn cancel_and_submit_return_data_only_intents() {
let mut state = SessionsState::new();
state.new_session_path = "relative/path".into();
state.new_session_path = " ../project/./ ".into();
state.new_session_cursor = state.new_session_path.len();
let original_path = state.new_session_path.clone();
let original_cursor = state.new_session_cursor;

assert_eq!(
handle_key(&mut state, key(KeyCode::Enter)),
NewSessionInputResult::Submit {
raw_path: "relative/path".into()
raw_path: " ../project/./ ".into()
}
);
assert_eq!(state.new_session_path, original_path);
assert_eq!(state.new_session_cursor, original_cursor);

assert_eq!(
handle_key(&mut state, key(KeyCode::Esc)),
NewSessionInputResult::Cancel
);
assert_eq!(state.new_session_path, original_path);
assert_eq!(state.new_session_cursor, original_cursor);
}

#[test]
fn control_character_is_not_inserted() {
let mut state = SessionsState::new();
state.new_session_path = "project".into();
state.new_session_cursor = 3;

assert_eq!(
handle_key(
&mut state,
KeyEvent::new(KeyCode::Char('n'), KeyModifiers::CONTROL)
),
NewSessionInputResult::NotHandled
);
assert!(state.new_session_path.is_empty());
assert_eq!(
(state.new_session_path.as_str(), state.new_session_cursor),
("project", 3)
);

assert_eq!(
handle_key(&mut state, key(KeyCode::Delete)),
NewSessionInputResult::NotHandled
);
assert_eq!(
(state.new_session_path.as_str(), state.new_session_cursor),
("project", 3)
);
}
}
19 changes: 11 additions & 8 deletions src/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2450,17 +2450,20 @@ mod model_popup_tests {
let mut app = App::new();
app.connection.conn = ConnState::Connected;
app.navigation.popup = Popup::NewSession;
app.sessions.new_session_path = "/repo".into();
app.sessions.new_session_path = "/repo/./project/..".into();
app.sessions.new_session_cursor = app.sessions.new_session_path.len();
app.profiles.active_profile_id = Some("coder-delegate".into());
let mut effects = TestEffects::default();
effects.extend(handle_new_session_popup_key(&mut app, key(KeyCode::Enter)));

assert!(matches!(
effects.next_command(),
Some(Command::NewSession { profile_id: Some(profile_id), .. })
if profile_id == "coder-delegate"
));
let effects = handle_new_session_popup_key(&mut app, key(KeyCode::Enter));

assert_eq!(app.navigation.popup, Popup::None);
assert_eq!(
effects,
vec![Effect::Command(Command::NewSession {
cwd: Some("/repo".into()),
profile_id: Some("coder-delegate".into()),
})]
);
}

#[test]
Expand Down
Loading