ocre-cli 0.2.0

Command-line tool for Ocre: create, generate, migrate, run and deploy apps.
use super::*;

fn session(ids: &[&str]) -> Session {
    Session {
        authenticated: true,
        email: Some("a@b.c".into()),
        accounts: ids.iter().map(|id| Account { id: (*id).into(), name: format!("{id} name") }).collect(),
    }
}

#[test]
fn single_account_needs_no_account_id() {
    assert_eq!(pick_account(&session(&["a1"]), None).unwrap(), None);
    assert_eq!(pick_account(&session(&["a1"]), Some("a1")).unwrap(), Some("a1".into()));
}

#[test]
fn several_accounts_require_a_listed_choice() {
    let s = session(&["a1", "a2"]);
    assert_eq!(pick_account(&s, Some("a2")).unwrap(), Some("a2".into()));
    let err = pick_account(&s, None).unwrap_err();
    assert_eq!(err.hint.unwrap(), "pass --account-id with one of: a1 (a1 name), a2 (a2 name)");
    let err = pick_account(&s, Some("zz")).unwrap_err();
    assert!(err.message.contains("`zz`"));
}

#[test]
fn no_accounts_is_an_error() {
    assert!(pick_account(&session(&[]), None).is_err());
}

#[test]
fn parses_cf_whoami_json() {
    let json = r#"{"authenticated":true,"authSource":"oauth","tokenValid":true,"email":"x@y.z","accounts":[{"id":"1","name":"One","type":"standard","settings":{},"created_on":"2020-01-01"}]}"#;
    let parsed: Session = serde_json::from_str(json).unwrap();
    assert_eq!(
        parsed,
        Session {
            authenticated: true,
            email: Some("x@y.z".into()),
            accounts: vec![Account { id: "1".into(), name: "One".into() }]
        }
    );
    let logged_out: Session = serde_json::from_str(r#"{"authenticated":false,"error":"Not logged in"}"#).unwrap();
    assert!(!logged_out.authenticated);
}

#[test]
fn reads_the_api_code_of_cf_error_boxes() {
    let stderr =
        "๐ŸŠโ˜๏ธ cf ยท v1.0.0-beta.5\nโ”Œ APIError\nโ”‚ [10006] The specified bucket does not exist.\nโ”‚ 404 Not Found\nโ””\n";
    assert_eq!(api_code(stderr), Some(10006));
    assert_eq!(error_box(stderr), "โ”Œ APIError\nโ”‚ [10006] The specified bucket does not exist.\nโ”‚ 404 Not Found\nโ””");
    assert_eq!(api_code("โ”Œ Error\nโ”‚ [not a code] and [42x]\nโ””"), None);
    assert_eq!(error_box("  plain failure \n"), "plain failure");
}

#[test]
fn pending_migrations_come_from_the_wrangler_table() {
    let output = "\n โ›…๏ธ wrangler 4.144.0\nโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€\nResource location: local \n\nMigrations to be applied:\n\
                  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”\nโ”‚ Name              โ”‚\nโ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค\n\
                  โ”‚ 0001_create_t.sql โ”‚\nโ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค\nโ”‚ 0002_x.sql        โ”‚\nโ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜\n";
    assert_eq!(pending_migrations(output), ["0001_create_t.sql", "0002_x.sql"]);
    assert!(pending_migrations("โœ… No migrations to apply!\n").is_empty());
}