use kwaak::commands::Command;
use kwaak::frontend::{DiffVariant, UIEvent, UserInputCommand, ui};
use kwaak::test_utils::integration::{IntegrationContext, setup_integration};
use kwaak::{assert_command_done, git};
#[test_log::test(tokio::test(flavor = "multi_thread"))]
async fn test_diff() {
let IntegrationContext {
mut app,
uuid,
repository,
mut terminal,
workdir,
repository_guard: _repository_guard,
handler_guard,
} = setup_integration().await.unwrap();
app.dispatch_command(
uuid,
Command::Chat {
message: "hello".to_string(),
},
);
assert_command_done!(app, uuid);
app.send_ui_event(UIEvent::UserInputCommand(
uuid,
UserInputCommand::Diff(DiffVariant::Show),
));
assert_command_done!(app, uuid);
app.dispatch_command(
uuid,
Command::Exec {
cmd: swiftide::traits::Command::write_file("hello.txt", "world"),
},
);
assert_command_done!(app, uuid);
app.send_ui_event(UIEvent::UserInputCommand(
uuid,
UserInputCommand::Diff(DiffVariant::Show),
));
assert_command_done!(app, uuid);
terminal.draw(|f| ui(f, f.area(), &mut app)).unwrap();
insta::assert_snapshot!(terminal.backend());
app.send_ui_event(UIEvent::UserInputCommand(
uuid,
UserInputCommand::Diff(DiffVariant::Pull),
));
assert_command_done!(app, uuid);
let current_branch = git::util::main_branch(&workdir);
assert_eq!(¤t_branch, &repository.config().git.main_branch);
let branch_name = app.current_chat().branch_name.as_deref().unwrap();
let output = tokio::process::Command::new("git")
.arg("checkout")
.arg(branch_name)
.current_dir(&workdir)
.output()
.await
.unwrap();
dbg!(&output);
let output = tokio::process::Command::new("git")
.arg("status")
.current_dir(&workdir)
.output()
.await
.unwrap();
dbg!(&output);
let output = tokio::process::Command::new("git")
.arg("branch")
.current_dir(&workdir)
.output()
.await
.unwrap();
dbg!(&output);
let content = std::fs::read_to_string(workdir.join("hello.txt")).unwrap();
assert_eq!(content, "world\n");
app.handle_single_event(&UIEvent::ScrollDown).await;
terminal.draw(|f| ui(f, f.area(), &mut app)).unwrap();
insta::assert_snapshot!("diff pulled", terminal.backend());
drop(handler_guard);
}