Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions cli/src/commands/rate_limit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,47 @@ fn format_timestamp(timestamp: i32) -> String {

dt.format("%Y-%m-%d %H:%M:%S %Z").to_string()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_format_timestamp_valid() {
// 2024-01-15 00:00:00 UTC
let timestamp = 1705276800;
let result = format_timestamp(timestamp);

// Should contain date components (timezone-independent check)
assert!(result.contains("2024-01-"));
assert!(result.contains(":"));
}

#[test]
fn test_format_timestamp_epoch() {
// Unix epoch: 1970-01-01 00:00:00 UTC
let timestamp = 0;
let result = format_timestamp(timestamp);

// Should contain 1970 (timezone-independent check)
assert!(result.contains("1970-"));
}

#[test]
fn test_format_timestamp_format() {
let timestamp = 1705276800;
let result = format_timestamp(timestamp);

// Should match format: YYYY-MM-DD HH:MM:SS TZ
let parts: Vec<&str> = result.split_whitespace().collect();
assert_eq!(parts.len(), 3, "Expected 3 parts: date, time, timezone");

// Date format check
let date_parts: Vec<&str> = parts[0].split('-').collect();
assert_eq!(date_parts.len(), 3, "Date should have 3 parts");

// Time format check
let time_parts: Vec<&str> = parts[1].split(':').collect();
assert_eq!(time_parts.len(), 3, "Time should have 3 parts");
}
}
31 changes: 31 additions & 0 deletions cli/src/commands/watching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,3 +182,34 @@ fn parse_issue_id_or_key(input: &str) -> anyhow::Result<IssueIdOrKey> {
Ok(IssueIdOrKey::Key(IssueKey::from_str(input)?))
}
}

#[cfg(test)]
mod tests {
use super::*;
use backlog_core::identifier::Identifier;

#[test]
fn test_parse_issue_id_or_key_numeric_id() {
let result = parse_issue_id_or_key("123").unwrap();
match result {
IssueIdOrKey::Id(id) => assert_eq!(id.value(), 123),
IssueIdOrKey::Key(_) => panic!("Expected Id variant"),
}
}

#[test]
fn test_parse_issue_id_or_key_string_key() {
let result = parse_issue_id_or_key("PROJECT-456").unwrap();
match result {
IssueIdOrKey::Key(key) => assert_eq!(key.to_string(), "PROJECT-456"),
IssueIdOrKey::Id(_) => panic!("Expected Key variant"),
}
}

#[test]
fn test_parse_issue_id_or_key_invalid_format() {
// Invalid key format (no hyphen with number)
let result = parse_issue_id_or_key("invalid");
assert!(result.is_err());
}
}
26 changes: 26 additions & 0 deletions cli/src/commands/webhook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,3 +493,29 @@ async fn delete_webhook(

Ok(())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_escape_csv_no_special_chars() {
let input = "simple text";
let result = escape_csv(input);
assert_eq!(result, "simple text");
}

#[test]
fn test_escape_csv_with_comma() {
let input = "hello, world";
let result = escape_csv(input);
assert_eq!(result, "\"hello, world\"");
}

#[test]
fn test_escape_csv_with_quotes() {
let input = "say \"hello\"";
let result = escape_csv(input);
assert_eq!(result, "\"say \"\"hello\"\"\"");
}
}
80 changes: 80 additions & 0 deletions cli/tests/star_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use clap::{Parser, Subcommand};

#[derive(Parser, Debug)]
struct Cli {
#[clap(subcommand)]
command: StarCommands,
}

#[derive(Subcommand, Debug)]
enum StarCommands {
Add {
#[clap(subcommand)]
target: StarTarget,
},
}

#[derive(Subcommand, Debug, PartialEq)]
enum StarTarget {
Issue { issue_id: u32 },
Comment { issue_id: u32, comment_id: u32 },
Wiki { wiki_id: u32 },
Pr { pr_id: u32 },
PrComment { pr_comment_id: u32 },
}

#[test]
fn test_star_add_issue() {
let args = Cli::try_parse_from(["prog", "add", "issue", "123"]).unwrap();
match args.command {
StarCommands::Add { target } => {
assert_eq!(target, StarTarget::Issue { issue_id: 123 });
}
}
}

#[test]
fn test_star_add_comment() {
let args = Cli::try_parse_from(["prog", "add", "comment", "100", "200"]).unwrap();
match args.command {
StarCommands::Add { target } => {
assert_eq!(
target,
StarTarget::Comment {
issue_id: 100,
comment_id: 200
}
);
}
}
}

#[test]
fn test_star_add_wiki() {
let args = Cli::try_parse_from(["prog", "add", "wiki", "456"]).unwrap();
match args.command {
StarCommands::Add { target } => {
assert_eq!(target, StarTarget::Wiki { wiki_id: 456 });
}
}
}

#[test]
fn test_star_add_pr() {
let args = Cli::try_parse_from(["prog", "add", "pr", "789"]).unwrap();
match args.command {
StarCommands::Add { target } => {
assert_eq!(target, StarTarget::Pr { pr_id: 789 });
}
}
}

#[test]
fn test_star_add_pr_comment() {
let args = Cli::try_parse_from(["prog", "add", "pr-comment", "321"]).unwrap();
match args.command {
StarCommands::Add { target } => {
assert_eq!(target, StarTarget::PrComment { pr_comment_id: 321 });
}
}
}
49 changes: 49 additions & 0 deletions cli/tests/watching_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use clap::{Args, Parser, Subcommand};

#[derive(Parser, Debug)]
struct Cli {
#[clap(subcommand)]
command: WatchingSubcommand,
}

#[derive(Subcommand, Debug)]
enum WatchingSubcommand {
Get(GetWatchingArgs),
Add(AddWatchingArgs),
}

#[derive(Args, Debug, Clone)]
struct GetWatchingArgs {
watching_id: u32,
}

#[derive(Args, Debug, Clone)]
struct AddWatchingArgs {
issue: String,
#[arg(short, long)]
note: Option<String>,
}

#[test]
fn test_watching_get_command() {
let args = Cli::try_parse_from(["prog", "get", "12345"]).unwrap();
match args.command {
WatchingSubcommand::Get(args) => {
assert_eq!(args.watching_id, 12345);
}
_ => panic!("Expected Get command"),
}
}

#[test]
fn test_watching_add_with_note() {
let args =
Cli::try_parse_from(["prog", "add", "PROJECT-123", "--note", "Important issue"]).unwrap();
match args.command {
WatchingSubcommand::Add(args) => {
assert_eq!(args.issue, "PROJECT-123");
assert_eq!(args.note, Some("Important issue".to_string()));
}
_ => panic!("Expected Add command"),
}
}
Loading