Skip to content
Open
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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,12 @@ coval test-sets create \
coval test-cases create \
--test-set-id ts123456 \
--input "I need help with my order"

# Create a dashboard and make it the organization default
coval dashboards create \
--name "Production Metrics" \
--description "Latency and quality overview" \
--default true
```

### JSON Output for Scripting
Expand Down
38 changes: 37 additions & 1 deletion src/client/models/dashboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ pub struct Dashboard {
pub name: String,
#[serde(default)]
pub display_name: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub is_default: Option<bool>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub is_favorite: Option<bool>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub position: Option<i64>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub config: Option<serde_json::Value>,
pub create_time: DateTime<Utc>,
#[serde(skip_serializing_if = "Option::is_none")]
pub update_time: Option<DateTime<Utc>>,
Expand All @@ -31,12 +41,32 @@ pub struct Dashboard {
#[derive(Debug, Serialize, Deserialize)]
pub struct CreateDashboardRequest {
pub display_name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub is_favorite: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub is_default: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub position: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub config: Option<serde_json::Value>,
}

#[derive(Debug, Default, Serialize, Deserialize)]
pub struct UpdateDashboardRequest {
#[serde(skip_serializing_if = "Option::is_none")]
pub display_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub is_favorite: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub is_default: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub position: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub config: Option<serde_json::Value>,
}

#[derive(Debug, Deserialize)]
Expand All @@ -62,17 +92,23 @@ pub struct UpdateDashboardResponse {

impl Tabular for Dashboard {
fn headers() -> Vec<&'static str> {
vec!["ID", "NAME", "CREATED", "UPDATED"]
vec!["ID", "NAME", "DEFAULT", "CREATED", "UPDATED"]
}

fn row(&self) -> Vec<String> {
let updated = self
.update_time
.map(|t| t.format("%Y-%m-%d %H:%M").to_string())
.unwrap_or_default();
let default = if self.is_default.unwrap_or(false) {
"yes".to_string()
} else {
String::new()
};
vec![
extract_id(&self.name),
truncate(self.display_name.as_deref().unwrap_or(""), 30),
default,
self.create_time.format("%Y-%m-%d %H:%M").to_string(),
updated,
]
Expand Down
40 changes: 40 additions & 0 deletions src/commands/dashboards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ pub struct CreateArgs {
input_json: InputJsonArg,
#[arg(long)]
name: Option<String>,
#[arg(long)]
description: Option<String>,
/// Mark as a favorite (true/false).
#[arg(long)]
favorite: Option<bool>,
/// Make this the organization's default dashboard (true/false). Omit to auto-default the first dashboard.
#[arg(long)]
default: Option<bool>,
/// Ordering position (>= 0). Omit to append to the end.
#[arg(long)]
position: Option<i64>,
/// Free-form JSON config: a JSON string or @path to a file.
#[arg(long)]
config: Option<String>,
Comment on lines +65 to +78
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Add validation for position >= 0.

The doc comment at line 73 states position should be ">= 0", but there's no validation to enforce this constraint before sending the request. Users providing negative values will receive an API error instead of a clear, immediate CLI error.

Consider adding a validation helper similar to validate_widget_grid (lines 192-204):

fn validate_position(position: Option<i64>) -> Result<()> {
    if let Some(p) = position {
        if p < 0 {
            bail!("--position must be >= 0");
        }
    }
    Ok(())
}

Then call it in the Create and Update handlers before building the request.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/commands/dashboards.rs` around lines 65 - 78, Add input validation to
enforce position >= 0: implement a helper fn validate_position(position:
Option<i64>) -> Result<()> that returns an error (e.g., bail!("--position must
be >= 0")) when a Some value is negative, and call validate_position(...) from
the Create and Update command handlers before constructing the request (similar
to validate_widget_grid used at lines ~192-204) so negative --position values
are rejected client-side with a clear CLI error.

}

#[derive(Args)]
Expand All @@ -71,6 +85,20 @@ pub struct UpdateArgs {
input_json: InputJsonArg,
#[arg(long)]
name: Option<String>,
#[arg(long)]
description: Option<String>,
/// Mark as a favorite (true/false).
#[arg(long)]
favorite: Option<bool>,
/// Make this the organization's default dashboard (true/false). Setting true unsets any other default.
#[arg(long)]
default: Option<bool>,
/// Ordering position (>= 0).
#[arg(long)]
position: Option<i64>,
/// Replacement free-form JSON config: a JSON string or @path to a file.
#[arg(long)]
config: Option<String>,
Comment on lines +88 to +101
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Add validation for position >= 0.

Same issue as CreateArgs: the doc comment at line 96 specifies ">= 0" but no validation is present.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/commands/dashboards.rs` around lines 88 - 101, The position field allows
negative values despite the doc comment; update the dashboard update argument
validation to enforce position >= 0 by adding a validator for the position field
(the same approach used in CreateArgs) — either add a clap value parser/range
(e.g. value_parser with range 0..) or a small validate() check in the UpdateArgs
handling path that returns an error when position < 0; target the position field
in the dashboards.rs UpdateArgs/struct and mirror the CreateArgs validation
logic so negatives are rejected.

}

#[derive(Args)]
Expand Down Expand Up @@ -227,7 +255,13 @@ pub async fn execute(
}
DashboardCommands::Create(args) => {
let mut input = args.input_json.object()?;
let config = args.config.as_ref().map(|c| parse_config(c)).transpose()?;
input_json::insert(&mut input, "display_name", args.name)?;
input_json::insert(&mut input, "description", args.description)?;
input_json::insert(&mut input, "is_favorite", args.favorite)?;
input_json::insert(&mut input, "is_default", args.default)?;
input_json::insert(&mut input, "position", args.position)?;
input_json::insert(&mut input, "config", config)?;
let req: CreateDashboardRequest = input_json::finish(input)?;
let dashboard = client.dashboards().create(req).await?;
let dashboard_id = resource_id(&dashboard.name);
Expand All @@ -241,7 +275,13 @@ pub async fn execute(
}
DashboardCommands::Update(args) => {
let mut input = args.input_json.object()?;
let config = args.config.as_ref().map(|c| parse_config(c)).transpose()?;
input_json::insert(&mut input, "display_name", args.name)?;
input_json::insert(&mut input, "description", args.description)?;
input_json::insert(&mut input, "is_favorite", args.favorite)?;
input_json::insert(&mut input, "is_default", args.default)?;
input_json::insert(&mut input, "position", args.position)?;
input_json::insert(&mut input, "config", config)?;
let req: UpdateDashboardRequest = input_json::finish(input)?;
let dashboard = client.dashboards().update(&args.dashboard_id, req).await?;
emit_one_with_actions(
Expand Down
98 changes: 98 additions & 0 deletions tests/cli_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1880,6 +1880,104 @@ async fn test_input_json_stdin() {
.stdout(predicate::str::contains("dash123"));
}

#[tokio::test]
async fn test_dashboard_create_with_full_fields() {
let mock_server = MockServer::start().await;

// body_partial_json asserts the new flags are serialized into the request body;
// a mismatch yields no matching mock (404) and the command fails.
Mock::given(method("POST"))
.and(path("/v1/dashboards"))
.and(header("X-API-Key", "test_key"))
.and(body_partial_json(json!({
"display_name": "Ops",
"description": "desc",
"is_favorite": true,
"is_default": true,
"position": 3,
"config": {"date_preferences": {"preset": "last-7-days"}}
})))
.respond_with(ResponseTemplate::new(201).set_body_json(json!({
"dashboard": {
"name": "dashboards/dash123",
"display_name": "Ops",
"description": "desc",
"is_default": true,
"is_favorite": true,
"position": 3,
"config": {"date_preferences": {"preset": "last-7-days"}},
"create_time": "2025-01-15T10:30:00Z",
"update_time": "2025-01-15T10:30:00Z"
}
})))
.mount(&mock_server)
.await;

coval()
.arg("--api-key")
.arg("test_key")
.arg("--api-url")
.arg(mock_server.uri())
.arg("dashboards")
.arg("create")
.arg("--name")
.arg("Ops")
.arg("--description")
.arg("desc")
.arg("--favorite")
.arg("true")
.arg("--default")
.arg("true")
.arg("--position")
.arg("3")
.arg("--config")
.arg(r#"{"date_preferences":{"preset":"last-7-days"}}"#)
.assert()
.success()
.stdout(predicate::str::contains("dash123"));
}

#[tokio::test]
async fn test_dashboard_update_sets_default_and_position() {
let mock_server = MockServer::start().await;

Mock::given(method("PATCH"))
.and(path("/v1/dashboards/dash123"))
.and(header("X-API-Key", "test_key"))
.and(body_partial_json(json!({
"is_default": true,
"position": 5
})))
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
"dashboard": {
"name": "dashboards/dash123",
"display_name": "Ops",
"is_default": true,
"position": 5,
"create_time": "2025-01-15T10:30:00Z",
"update_time": "2025-01-16T10:30:00Z"
}
})))
.mount(&mock_server)
.await;

coval()
.arg("--api-key")
.arg("test_key")
.arg("--api-url")
.arg(mock_server.uri())
.arg("dashboards")
.arg("update")
.arg("dash123")
.arg("--default")
.arg("true")
.arg("--position")
.arg("5")
.assert()
.success()
.stdout(predicate::str::contains("dash123"));
}

#[test]
fn test_input_json_invalid_agent_error() {
let value = stdout_json(
Expand Down
Loading