Lines
94.19 %
Functions
100 %
Branches
use clap::Parser;
use cli_core::template as ops;
use sqlx::PgPool;
use super::*;
use crate::flat::tests::seed_user;
use crate::{Cli, Command};
const USER: &str = "00000000-0000-0000-0000-000000000001";
const SOURCE: &str = r#"(set-draft-note "Lunch")"#;
fn parse(args: &[&str]) -> Result<TemplateCmd, clap::Error> {
let argv = ["nomisync", "--userid", USER, "template"]
.into_iter()
.chain(args.iter().copied());
match Cli::try_parse_from(argv)?.cmd {
Command::Template(cmd) => Ok(cmd),
other => panic!("expected template, got {other:?}"),
}
fn source_of(args: &[&str]) -> anyhow::Result<SourceArgs> {
match parse(&[&["create"], args].concat())? {
TemplateCmd::Create { source, .. } => Ok(source),
other => anyhow::bail!("expected create, got {other:?}"),
#[test]
fn list_and_delete_parse() -> anyhow::Result<()> {
assert!(matches!(parse(&["list"])?, TemplateCmd::List));
let id = Uuid::new_v4();
match parse(&["delete", &id.to_string(), "--yes"])? {
TemplateCmd::Delete { id: parsed, yes } => assert_eq!((parsed, yes), (id, true)),
other => anyhow::bail!("expected delete, got {other:?}"),
Ok(())
fn create_takes_a_name_and_a_source() -> anyhow::Result<()> {
match parse(&["create", "--name", "Lunch", "--source-file", "lunch.nms"])? {
TemplateCmd::Create { name, source } => {
assert_eq!(name.as_deref(), Some("Lunch"));
assert_eq!(source.source_file, Some(PathBuf::from("lunch.nms")));
fn edit_takes_an_id_and_optional_fields() -> anyhow::Result<()> {
match parse(&["edit", &id.to_string()])? {
TemplateCmd::Edit {
id: parsed,
name,
source,
} => {
assert_eq!(parsed, id);
assert_eq!(name, None);
assert_eq!(source.read(std::io::empty())?, None);
other => anyhow::bail!("expected edit, got {other:?}"),
assert!(parse(&["edit"]).is_err());
fn test_takes_an_id_or_a_source_but_not_both() -> anyhow::Result<()> {
let id = Uuid::new_v4().to_string();
assert!(matches!(
parse(&["test", &id])?,
TemplateCmd::Test { id: Some(_), .. }
));
parse(&["test", "--source", SOURCE])?,
TemplateCmd::Test { id: None, .. }
assert!(parse(&["test", &id, "--source", SOURCE]).is_err());
fn a_file_and_inline_text_cannot_both_be_given() {
assert!(parse(&["create", "--source", SOURCE, "--source-file", "a.nms"]).is_err());
fn the_source_comes_from_inline_text() -> anyhow::Result<()> {
let source = source_of(&["--source", SOURCE])?;
assert_eq!(source.read(std::io::empty())?.as_deref(), Some(SOURCE));
fn a_dash_reads_the_source_from_stdin() -> anyhow::Result<()> {
for args in [["--source", "-"], ["--source-file", "-"]] {
let source = source_of(&args)?;
assert_eq!(source.read(SOURCE.as_bytes())?.as_deref(), Some(SOURCE));
fn the_source_comes_from_a_file() -> anyhow::Result<()> {
let path = std::env::temp_dir().join(format!("lunch-{}.nms", Uuid::new_v4()));
std::fs::write(&path, SOURCE)?;
let source = source_of(&["--source-file", &path.display().to_string()])?;
let read = source.read(std::io::empty());
std::fs::remove_file(&path)?;
assert_eq!(read?.as_deref(), Some(SOURCE));
fn a_missing_file_is_an_argument_error() -> anyhow::Result<()> {
let source = source_of(&["--source-file", "/nonexistent/lunch.nms"])?;
source.read(std::io::empty()),
Err(CommandError::Argument(_))
fn a_required_source_that_is_missing_is_refused() {
SourceArgs::default().required(std::io::empty()),
async fn seeded_template(pool: &PgPool) -> anyhow::Result<(Session, Uuid)> {
let user = seed_user(pool).await?;
let mut session = Session::new(rpc::ScriptCtx::new(user))?;
let id = ops::create(&mut session, Some("Lunch"), SOURCE).await?;
Ok((session, Uuid::parse_str(&id)?))
#[sqlx::test(migrator = "server::db::MIGRATOR")]
async fn a_declined_template_delete_keeps_the_template(pool: PgPool) -> anyhow::Result<()> {
let _guard = server::db::install_test_pool(pool.clone());
let (mut session, id) = seeded_template(&pool).await?;
let deleted =
delete_template_confirmed(&mut session, id, false, true, "n\n".as_bytes()).await?;
assert!(!deleted);
assert_eq!(ops::list(&mut session).await?.len(), 1);
async fn a_confirmed_template_delete_removes_the_template(pool: PgPool) -> anyhow::Result<()> {
delete_template_confirmed(&mut session, id, false, true, "y\n".as_bytes()).await?;
assert!(deleted);
assert!(ops::list(&mut session).await?.is_empty());
async fn template_delete_with_yes_needs_no_input(pool: PgPool) -> anyhow::Result<()> {
let deleted = delete_template_confirmed(&mut session, id, true, false, "".as_bytes()).await?;
async fn template_delete_without_a_terminal_or_yes_is_refused(pool: PgPool) -> anyhow::Result<()> {
let refused = delete_template_confirmed(&mut session, id, false, false, "y\n".as_bytes()).await;
assert!(
matches!(refused, Err(CommandError::Argument(_))),
"{refused:?}"
);