tests: added for 'check' command
Some checks failed
Test / build (map[name:nightly]) (push) Successful in 2m30s
Test / build (map[name:stable]) (push) Successful in 2m34s
Release Please / Release-plz (push) Failing after 20s

This commit is contained in:
Paul Campbell 2024-12-18 17:21:27 +00:00
parent 6d234609e7
commit 2df105bd65
2 changed files with 183 additions and 0 deletions

182
src/tests/check.rs Normal file
View file

@ -0,0 +1,182 @@
use super::*;
use crate::{run, Command, Commands, FullCtx};
use color_eyre::eyre::Result;
use http::StatusCode;
#[test]
fn test_commands_log_flag() {
let commands = Commands {
log: true,
command: Command::Init,
};
assert!(commands.log);
let commands = Commands {
log: false,
command: Command::Init,
};
assert!(!commands.log);
}
#[test]
fn test_command_variants() {
let init = Command::Init;
assert!(matches!(init, Command::Init));
let check = Command::Check;
assert!(matches!(check, Command::Check));
let import = Command::Import;
assert!(matches!(import, Command::Import));
}
#[test]
fn test_ctx_creation() {
let path = std::path::PathBuf::from("test_path");
let ctx = Ctx::from(path.clone());
assert_eq!(ctx.fs.base(), &path);
}
#[tokio::test]
async fn test_full_ctx_clients() -> Result<()> {
let fs = kxio::fs::temp()?;
let ctx = Ctx::from(fs.base().to_path_buf());
let file_name = fs.base().join("trello-to-deck.toml");
let file = fs.file(&file_name);
file.write(
[
"[trello]",
"api_key = 'foo'",
"api_secret = 'bar'",
"board_name = 'target'",
"[nextcloud]",
"hostname = 'http://localhost:8000'",
"username = 'bob'",
"password = '1 <3 alice!'",
"board_id = 23",
]
.join("\n"),
)?;
let config = AppConfig::load(&ctx).expect("Failed to load config");
let full_ctx = FullCtx {
fs: ctx.fs,
net: ctx.net,
prt: ctx.prt,
cfg: config,
};
let _deck_client = full_ctx.deck_client();
let _trello_client = full_ctx.trello_client();
assert_peq!(
full_ctx.cfg,
AppConfig {
trello: TrelloConfig {
api_key: s!("foo").into(),
api_secret: s!("bar").into(),
board_name: s!("target").into(),
},
nextcloud: NextcloudConfig {
hostname: s!("http://localhost:8000").into(),
username: s!("bob").into(),
password: s!("1 <3 alice!").into(),
board_id: 23.into(),
}
}
);
Ok(())
}
#[tokio::test]
async fn test_run() -> Result<()> {
//given
let fs = kxio::fs::temp()?;
let _temp_fs = fs.clone();
let file_name = fs.base().join("trello-to-deck.toml");
let file = fs.file(&file_name);
file.write(
[
"[trello]",
"api_key = 'foo'",
"api_secret = 'bar'",
"board_name = 'DevProjects'",
"[nextcloud]",
"hostname = 'http://localhost:8000'",
"username = 'bob'",
"password = '1 <3 alice!'",
"board_id = 1",
]
.join("\n"),
)?;
let mock_net = kxio::net::mock();
mock_net
.on()
.get("https://api.trello.com/1/members/me/boards?lists=open")
.respond(StatusCode::OK)
.body(include_bytes!("responses/trello-member-get.json").as_slice())
.expect("trello member get");
mock_net
.on()
.get("http://localhost:8000/index.php/apps/deck/api/v1.0/boards")
.respond(StatusCode::OK)
.body(include_bytes!("responses/nextcloud-deck-get.json").as_slice())
.expect("nextcloud deck get");
mock_net
.on()
.get("http://localhost:8000/index.php/apps/deck/api/v1.0/boards/1/stacks")
.respond(StatusCode::OK)
.body(include_bytes!("responses/nextcloud-board-get.json").as_slice())
.expect("nextcloud board get");
let ctx = Ctx {
fs: fs.as_real(),
net: mock_net.into(),
prt: kxio::print::test(),
};
let prt = ctx.prt.clone();
let prt = prt.as_test().unwrap();
let commands = Commands {
log: false,
command: Command::Check,
};
//when
run(&ctx, &commands).await?;
//then
let output = prt.output();
assert_peq!(
output.trim(),
[
">> Testing Trello details...",
"<<< Trello Credentials: OKAY",
">> Trello Board: DevProjects",
"<<< Trello Board: OKAY",
"<<<< List: BRAINSTORM 🤔",
"<<<< List: TODO 📚",
"<<<< List: DOING ⚙️",
"<<<< List: DONE! 🙌🏽",
"<<<< List: Icebox ❄️",
">> Testing Nextcloud details...",
"<<< Nextcloud Boards",
"<<<< Board: Personal Board",
"<<<< Board: 4 Published: Cossmass Infinities",
"<<<< Board: Fulfilment: Cossmass Infinities",
"<<< Nextcloud Credentials: OKAY",
"<<< Nextcloud Board: Personal Board",
"<<<< Stack: Done",
"<<<< Stack: Doing",
"<<<< Stack: To do",
]
.join("\n")
);
Ok(())
}

View file

@ -9,6 +9,7 @@ use pretty_assertions::assert_eq as assert_peq;
use crate::{config::AppConfig, f, nextcloud::NextcloudConfig, s, trello::TrelloConfig, Ctx, NAME}; use crate::{config::AppConfig, f, nextcloud::NextcloudConfig, s, trello::TrelloConfig, Ctx, NAME};
mod api_result; mod api_result;
mod check;
mod config; mod config;
pub(crate) mod given; pub(crate) mod given;
mod init; mod init;