Skip to content

Commit

Permalink
fix: After clearing the cache app should self-heal (#142)
Browse files Browse the repository at this point in the history
  • Loading branch information
timonv authored Jan 11, 2025
1 parent e9a07b7 commit 978648b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 32 deletions.
20 changes: 11 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,6 @@ async fn main() -> Result<()> {
let config = Config::load(&args.config_path).await?;
let repository = repository::Repository::from_config(config);

if panic::catch_unwind(|| {
storage::get_redb(&repository);
})
.is_err()
{
eprintln!("Failed to load database; are you running more than one kwaak on a project?");
std::process::exit(1);
}

fs::create_dir_all(repository.config().cache_dir()).await?;
fs::create_dir_all(repository.config().log_dir()).await?;

Expand Down Expand Up @@ -168,6 +159,17 @@ async fn start_agent(mut repository: repository::Repository, initial_message: &s
async fn start_tui(repository: &repository::Repository, args: &cli::Args) -> Result<()> {
::tracing::info!("Loaded configuration: {:?}", repository.config());

// Before starting the TUI, check if there is already a kwaak running on the project
// TODO: This is not very reliable. Potentially redb needs to be reconsidered
if panic::catch_unwind(|| {
storage::get_redb(&repository);
})
.is_err()
{
eprintln!("Failed to load database; are you running more than one kwaak on a project?");
std::process::exit(1);
}

// Setup terminal
let mut terminal = init_tui()?;

Expand Down
84 changes: 61 additions & 23 deletions tests/cli_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use predicates::prelude::*;
use std::process::Command;
use tempfile::TempDir;
struct Context {
cmd: Command,
dir: TempDir,
}

Expand All @@ -14,34 +13,57 @@ fn setup() -> Context {
.unwrap();

let mut cmd = Command::cargo_bin("kwaak").unwrap();
cmd.arg("init").current_dir(&dir);
cmd.current_dir(&dir);

Context { cmd, dir }
Context { dir }
}

impl Context {
fn cmd(&mut self) -> Command {
let mut cmd = Command::cargo_bin("kwaak").unwrap();
cmd.current_dir(&self.dir);
cmd
}

fn with_git(self) -> Self {
Command::new("git")
.arg("init")
.current_dir(&self.dir)
.assert()
.success();

Command::new("git")
.args([
"remote",
"add",
"origin",
"https://github.com/bosun-ai/kwaak",
])
.current_dir(&self.dir)
.assert()
.success();

self
}

fn with_config(self) -> Self {
// Copies over kwaak.toml to the tempdir
Command::new("cp")
.args(["kwaak.toml", self.dir.path().to_str().unwrap()])
.assert()
.success();

self
}
}

#[test_log::test(tokio::test)]
async fn test_creates_a_new_init_file() {
let mut context = setup();
Command::new("git")
.arg("init")
.current_dir(&context.dir)
.assert()
.success();

// Add a remote
Command::new("git")
.args([
"remote",
"add",
"origin",
"https://github.com/bosun-ai/kwaak",
])
.current_dir(&context.dir)
.assert()
.success();
let mut context = setup().with_git();

context
.cmd
.cmd()
.arg("init")
.assert()
.stdout(predicate::str::contains("Initialized kwaak project"))
.success();
Expand All @@ -54,7 +76,8 @@ async fn test_creates_a_new_init_file() {
async fn test_fails_if_not_git() {
let mut context = setup();
context
.cmd
.cmd()
.arg("init")
.assert()
.failure()
.stderr(predicate::str::contains("Not a git repository"));
Expand All @@ -69,3 +92,18 @@ async fn test_fails_config_present() {
.failure()
.stderr(predicate::str::contains("already exists"));
}

#[test_log::test(tokio::test)]
async fn test_print_config() {
let mut context = setup().with_git().with_config();

context.cmd().arg("print-config").assert().success();
}

#[test_log::test(tokio::test)]
async fn test_self_fixing_after_clear_cache() {
let mut context = setup().with_git().with_config();

context.cmd().arg("clear-cache").assert().success();
context.cmd().arg("print-config").assert().success();
}

0 comments on commit 978648b

Please sign in to comment.