1use std::io::{Write, stdout};
7
8use anyhow::Result;
9use chrono::{DateTime, Utc};
10use comfy_table::presets::UTF8_FULL;
11use comfy_table::{Cell, CellAlignment, Color, ContentArrangement, Table};
12use ironflow_sdk::types::{
13 ApiKeyResponse, ApiKeyScope, ArtifactResponse, AuditLogEntry, CreateApiKeyResponse,
14 KeyVersionsResponse, RunDetailResponse, RunResponse, RunStatus, ScopeEntry, SecretResponse,
15 StatsResponse, StepResponse, StepStatus, UserResponse, WorkflowDetailResponse, WorkflowSummary,
16};
17use serde::Serialize;
18use serde_json::to_string_pretty;
19use uuid::Uuid;
20
21fn status_color(status: &RunStatus) -> Color {
23 match status {
24 RunStatus::Completed => Color::Green,
25 RunStatus::Failed => Color::Red,
26 RunStatus::Running => Color::Blue,
27 RunStatus::Pending => Color::Yellow,
28 RunStatus::Cancelled => Color::Grey,
29 RunStatus::AwaitingApproval => Color::Magenta,
30 RunStatus::Retrying => Color::Cyan,
31 RunStatus::Warning => Color::DarkYellow,
32 RunStatus::Sleeping => Color::DarkCyan,
33 }
34}
35
36fn step_status_color(status: &StepStatus) -> Color {
38 match status {
39 StepStatus::Completed => Color::Green,
40 StepStatus::Failed => Color::Red,
41 StepStatus::Running => Color::Blue,
42 StepStatus::Pending => Color::Yellow,
43 StepStatus::Skipped => Color::Grey,
44 StepStatus::AwaitingApproval => Color::Magenta,
45 StepStatus::Rejected => Color::Red,
46 }
47}
48
49fn format_datetime(dt: &DateTime<Utc>) -> String {
51 dt.format("%Y-%m-%d %H:%M:%S").to_string()
52}
53
54fn format_optional_datetime(dt: &Option<DateTime<Utc>>) -> String {
56 dt.as_ref().map_or("-".to_string(), format_datetime)
57}
58
59fn format_duration_ms(ms: i64) -> String {
61 if ms < 1000 {
62 return format!("{ms}ms");
63 }
64 let secs = ms / 1000;
65 if secs < 60 {
66 return format!("{secs}s");
67 }
68 let mins = secs / 60;
69 let remaining_secs = secs % 60;
70 if mins < 60 {
71 return format!("{mins}m {remaining_secs}s");
72 }
73 let hours = mins / 60;
74 let remaining_mins = mins % 60;
75 format!("{hours}h {remaining_mins}m")
76}
77
78fn base_table() -> Table {
80 let mut table = Table::new();
81 table
82 .load_preset(UTF8_FULL)
83 .set_content_arrangement(ContentArrangement::Dynamic);
84 table
85}
86
87pub fn render_output<W: Write, T: Serialize>(
93 writer: &mut W,
94 json_mode: bool,
95 value: &T,
96 table_fn: impl FnOnce() -> Table,
97) -> Result<()> {
98 if json_mode {
99 let json = to_string_pretty(value)?;
100 writeln!(writer, "{json}")?;
101 } else {
102 writeln!(writer, "{}", table_fn())?;
103 }
104 Ok(())
105}
106
107pub fn print_output<T: Serialize>(
113 json_mode: bool,
114 value: &T,
115 table_fn: impl FnOnce() -> Table,
116) -> Result<()> {
117 render_output(&mut stdout().lock(), json_mode, value, table_fn)
118}
119
120pub fn print_json<T: Serialize>(value: &T) -> Result<()> {
129 let json = to_string_pretty(value)?;
130 writeln!(stdout().lock(), "{json}")?;
131 Ok(())
132}
133
134const COST_WARNING_RATIO: f64 = 0.8;
137
138fn format_cost(cost_usd: f64, max_cost_usd: Option<f64>) -> String {
142 match max_cost_usd {
143 Some(cap) => format!("${cost_usd:.4} / ${cap:.2}"),
144 None => format!("${cost_usd:.4}"),
145 }
146}
147
148fn cost_color(cost_usd: f64, max_cost_usd: Option<f64>) -> Option<Color> {
154 let cap = max_cost_usd?;
155
156 if cap <= 0.0 {
157 return (cost_usd > 0.0).then_some(Color::Red);
158 }
159
160 let ratio = cost_usd / cap;
161 if ratio >= 1.0 {
162 Some(Color::Red)
163 } else if ratio >= COST_WARNING_RATIO {
164 Some(Color::Yellow)
165 } else {
166 None
167 }
168}
169
170fn cost_cell(cost_usd: f64, max_cost_usd: Option<f64>) -> Cell {
172 let cell = Cell::new(format_cost(cost_usd, max_cost_usd));
173 match cost_color(cost_usd, max_cost_usd) {
174 Some(color) => cell.fg(color),
175 None => cell,
176 }
177}
178
179pub fn runs_table(runs: &[RunResponse]) -> Table {
180 let mut table = base_table();
181 table.set_header(vec![
182 "ID",
183 "Workflow",
184 "Status",
185 "Triggered by",
186 "Duration",
187 "Cost",
188 "Created",
189 "Started",
190 ]);
191
192 for run in runs {
193 let status_cell = Cell::new(run.status)
194 .fg(status_color(&run.status))
195 .set_alignment(CellAlignment::Center);
196
197 table.add_row(vec![
198 Cell::new(run.id.to_string().split('-').next().unwrap_or("")),
199 Cell::new(&run.workflow_name),
200 status_cell,
201 Cell::new(&run.created_by.label),
202 Cell::new(format_duration_ms(run.duration_ms)),
203 cost_cell(run.cost_usd, run.max_cost_usd),
204 Cell::new(format_datetime(&run.created_at)),
205 Cell::new(format_optional_datetime(&run.started_at)),
206 ]);
207 }
208
209 table
210}
211
212pub fn run_detail_table(detail: &RunDetailResponse) -> Table {
214 let run = &detail.run;
215 let mut table = base_table();
216 table.set_header(vec!["Field", "Value"]);
217
218 let status_cell = Cell::new(run.status).fg(status_color(&run.status));
219
220 table.add_row(vec![Cell::new("ID"), Cell::new(run.id)]);
221 table.add_row(vec![Cell::new("Workflow"), Cell::new(&run.workflow_name)]);
222 table.add_row(vec![Cell::new("Status"), status_cell]);
223 table.add_row(vec![
224 Cell::new("Trigger"),
225 Cell::new(format!("{:?}", run.trigger)),
226 ]);
227 table.add_row(vec![
228 Cell::new("Triggered by"),
229 Cell::new(&run.created_by.label),
230 ]);
231 table.add_row(vec![
232 Cell::new("Duration"),
233 Cell::new(format_duration_ms(run.duration_ms)),
234 ]);
235 table.add_row(vec![
236 Cell::new("Cost"),
237 cost_cell(run.cost_usd, run.max_cost_usd),
238 ]);
239 table.add_row(vec![
240 Cell::new("Created"),
241 Cell::new(format_datetime(&run.created_at)),
242 ]);
243 table.add_row(vec![
244 Cell::new("Started"),
245 Cell::new(format_optional_datetime(&run.started_at)),
246 ]);
247 table.add_row(vec![
248 Cell::new("Completed"),
249 Cell::new(format_optional_datetime(&run.completed_at)),
250 ]);
251 table.add_row(vec![
252 Cell::new("Retries"),
253 Cell::new(format!("{}/{}", run.retry_count, run.max_retries)),
254 ]);
255
256 if let Some(ref error) = run.error {
257 table.add_row(vec![Cell::new("Error"), Cell::new(error).fg(Color::Red)]);
258 }
259
260 if !detail.steps.is_empty() {
261 table.add_row(vec![
262 Cell::new("Steps"),
263 Cell::new(format!("{} step(s)", detail.steps.len())),
264 ]);
265 }
266
267 table
268}
269
270fn format_artifacts(artifacts: &[ArtifactResponse]) -> String {
274 if artifacts.is_empty() {
275 return "-".to_string();
276 }
277
278 let total: i64 = artifacts.iter().map(|artifact| artifact.size_bytes).sum();
279 format!("{} ({})", artifacts.len(), format_bytes(total))
280}
281
282fn format_bytes(bytes: i64) -> String {
284 const UNITS: [&str; 5] = ["B", "KB", "MB", "GB", "TB"];
285
286 if bytes < 1024 {
287 return format!("{bytes} B");
288 }
289
290 let mut value = bytes as f64;
291 let mut unit = 0;
292 while value >= 1024.0 && unit < UNITS.len() - 1 {
293 value /= 1024.0;
294 unit += 1;
295 }
296
297 let decimals = if value < 10.0 { 1 } else { 0 };
298 format!("{value:.decimals$} {}", UNITS[unit])
299}
300
301pub fn steps_table(steps: &[StepResponse]) -> Table {
303 let mut table = base_table();
304 table.set_header(vec![
305 "ID",
306 "Name",
307 "Status",
308 "Attempt",
309 "Duration",
310 "Cost",
311 "Artifacts",
312 "Started",
313 "Completed",
314 ]);
315
316 for step in steps {
317 let color = step_status_color(&step.status);
318
319 table.add_row(vec![
320 Cell::new(step.id.to_string().split('-').next().unwrap_or("")),
321 Cell::new(&step.name),
322 Cell::new(step.status)
323 .fg(color)
324 .set_alignment(CellAlignment::Center),
325 Cell::new(step.attempt).set_alignment(CellAlignment::Center),
326 Cell::new(format_duration_ms(step.duration_ms)),
327 Cell::new(format!("${:.4}", step.cost_usd)),
328 Cell::new(format_artifacts(&step.artifacts)).set_alignment(CellAlignment::Center),
329 Cell::new(format_optional_datetime(&step.started_at)),
330 Cell::new(format_optional_datetime(&step.completed_at)),
331 ]);
332 }
333
334 table
335}
336
337pub fn workflows_table(workflows: &[WorkflowSummary]) -> Table {
339 let mut table = base_table();
340 table.set_header(vec!["Name", "Category", "Version"]);
341
342 for wf in workflows {
343 table.add_row(vec![
344 Cell::new(&wf.name),
345 Cell::new(wf.category.as_deref().unwrap_or("-")),
346 Cell::new(wf.version.as_deref().unwrap_or("-")),
347 ]);
348 }
349
350 table
351}
352
353pub fn workflow_detail_table(detail: &WorkflowDetailResponse) -> Table {
355 let mut table = base_table();
356 table.set_header(vec!["Field", "Value"]);
357
358 table.add_row(vec![Cell::new("Name"), Cell::new(&detail.name)]);
359 table.add_row(vec![
360 Cell::new("Description"),
361 Cell::new(&detail.description),
362 ]);
363 table.add_row(vec![
364 Cell::new("Category"),
365 Cell::new(detail.category.as_deref().unwrap_or("-")),
366 ]);
367 table.add_row(vec![
368 Cell::new("Version"),
369 Cell::new(detail.version.as_deref().unwrap_or("-")),
370 ]);
371
372 if !detail.sub_workflows.is_empty() {
373 let names: Vec<&str> = detail
374 .sub_workflows
375 .iter()
376 .map(|s| s.name.as_str())
377 .collect();
378 table.add_row(vec![
379 Cell::new("Sub-workflows"),
380 Cell::new(names.join(", ")),
381 ]);
382 }
383
384 table
385}
386
387pub fn stats_table(stats: &StatsResponse) -> Table {
389 let mut table = base_table();
390 table.set_header(vec!["Metric", "Value"]);
391
392 table.add_row(vec![Cell::new("Total runs"), Cell::new(stats.total_runs)]);
393 table.add_row(vec![
394 Cell::new("Completed"),
395 Cell::new(stats.completed_runs).fg(Color::Green),
396 ]);
397 table.add_row(vec![
398 Cell::new("Failed"),
399 Cell::new(stats.failed_runs).fg(Color::Red),
400 ]);
401 table.add_row(vec![
402 Cell::new("Cancelled"),
403 Cell::new(stats.cancelled_runs).fg(Color::Grey),
404 ]);
405 table.add_row(vec![
406 Cell::new("Active"),
407 Cell::new(stats.active_runs).fg(Color::Blue),
408 ]);
409 table.add_row(vec![
410 Cell::new("Success rate"),
411 Cell::new(format!("{:.1}%", stats.success_rate_percent)),
412 ]);
413 table.add_row(vec![
414 Cell::new("Total cost"),
415 Cell::new(format!("${:.4}", stats.total_cost_usd)),
416 ]);
417 table.add_row(vec![
418 Cell::new("Total duration"),
419 Cell::new(format_duration_ms(stats.total_duration_ms)),
420 ]);
421
422 table
423}
424
425fn format_versions(versions: &[i32]) -> String {
427 if versions.is_empty() {
428 return "-".to_string();
429 }
430 versions
431 .iter()
432 .map(|v| v.to_string())
433 .collect::<Vec<_>>()
434 .join(", ")
435}
436
437#[derive(Debug, Serialize)]
451pub struct Deleted {
452 pub kind: &'static str,
454 pub id: String,
456 pub deleted: bool,
458}
459
460impl Deleted {
461 pub fn new(kind: &'static str, id: impl Into<String>) -> Self {
463 Self {
464 kind,
465 id: id.into(),
466 deleted: true,
467 }
468 }
469}
470
471pub fn deleted_table(deleted: &Deleted) -> Table {
473 let mut table = base_table();
474 table.set_header(vec!["Deleted", "ID"]);
475 table.add_row(vec![Cell::new(deleted.kind), Cell::new(&deleted.id)]);
476 table
477}
478
479pub fn report_deletion(json_mode: bool, kind: &'static str, id: impl Into<String>) -> Result<()> {
496 let deleted = Deleted::new(kind, id);
497 print_output(json_mode, &deleted, || deleted_table(&deleted))
498}
499
500pub fn secrets_table(secrets: &[SecretResponse]) -> Table {
505 let mut table = base_table();
506 table.set_header(vec!["Key", "Created", "Updated"]);
507
508 for secret in secrets {
509 table.add_row(vec![
510 Cell::new(&secret.key),
511 Cell::new(format_datetime(&secret.created_at)),
512 Cell::new(format_datetime(&secret.updated_at)),
513 ]);
514 }
515
516 table
517}
518
519fn format_scopes(scopes: &[ApiKeyScope]) -> String {
521 scopes
522 .iter()
523 .map(ToString::to_string)
524 .collect::<Vec<_>>()
525 .join(", ")
526}
527
528pub fn key_versions_table(status: &KeyVersionsResponse) -> Table {
530 let mut table = base_table();
531 table.set_header(vec!["Property", "Versions"]);
532
533 table.add_row(vec![
534 Cell::new("Active"),
535 Cell::new(status.active).fg(Color::Green),
536 ]);
537 table.add_row(vec![
538 Cell::new("Configured"),
539 Cell::new(format_versions(&status.configured)),
540 ]);
541 table.add_row(vec![
542 Cell::new("In use"),
543 Cell::new(format_versions(&status.in_use)),
544 ]);
545 table.add_row(vec![
546 Cell::new("Missing"),
547 Cell::new(format_versions(&status.missing)).fg(if status.missing.is_empty() {
548 Color::Grey
549 } else {
550 Color::Red
551 }),
552 ]);
553 table.add_row(vec![
554 Cell::new("Retirable"),
555 Cell::new(format_versions(&status.retirable)).fg(if status.retirable.is_empty() {
556 Color::Grey
557 } else {
558 Color::Yellow
559 }),
560 ]);
561
562 table
563}
564
565pub fn api_keys_table(keys: &[ApiKeyResponse]) -> Table {
569 let mut table = base_table();
570 table.set_header(vec![
571 "ID",
572 "Name",
573 "Prefix",
574 "Scopes",
575 "Active",
576 "Rate limit",
577 "Last used",
578 "Expires",
579 "Created",
580 ]);
581
582 for key in keys {
583 let active = Cell::new(if key.is_active { "yes" } else { "no" })
584 .fg(if key.is_active {
585 Color::Green
586 } else {
587 Color::Grey
588 })
589 .set_alignment(CellAlignment::Center);
590
591 let rate_limit = key
592 .rate_limit_override
593 .map(|v| v.to_string())
594 .unwrap_or_else(|| "-".to_string());
595
596 table.add_row(vec![
597 Cell::new(key.id),
598 Cell::new(&key.name),
599 Cell::new(&key.key_prefix),
600 Cell::new(format_scopes(&key.scopes)),
601 active,
602 Cell::new(rate_limit),
603 Cell::new(format_optional_datetime(&key.last_used_at)),
604 Cell::new(format_optional_datetime(&key.expires_at)),
605 Cell::new(format_datetime(&key.created_at)),
606 ]);
607 }
608
609 table
610}
611
612pub fn created_api_key_table(key: &CreateApiKeyResponse) -> Table {
618 let mut table = base_table();
619 table.set_header(vec!["Field", "Value"]);
620
621 table.add_row(vec![Cell::new("ID"), Cell::new(key.id)]);
622 table.add_row(vec![Cell::new("Name"), Cell::new(&key.name)]);
623 table.add_row(vec![
624 Cell::new("Key"),
625 Cell::new(&key.key).fg(Color::Yellow),
626 ]);
627 table.add_row(vec![Cell::new("Prefix"), Cell::new(&key.key_prefix)]);
628 table.add_row(vec![
629 Cell::new("Scopes"),
630 Cell::new(format_scopes(&key.scopes)),
631 ]);
632 if let Some(override_val) = key.rate_limit_override {
633 table.add_row(vec![
634 Cell::new("Rate limit"),
635 Cell::new(format!("{override_val} req/min")),
636 ]);
637 }
638 table.add_row(vec![
639 Cell::new("Expires"),
640 Cell::new(format_optional_datetime(&key.expires_at)),
641 ]);
642 table.add_row(vec![
643 Cell::new("Created"),
644 Cell::new(format_datetime(&key.created_at)),
645 ]);
646
647 table
648}
649
650pub fn scopes_table(scopes: &[ScopeEntry]) -> Table {
652 let mut table = base_table();
653 table.set_header(vec!["Value", "Label", "Description"]);
654
655 for scope in scopes {
656 table.add_row(vec![
657 Cell::new(&scope.value),
658 Cell::new(&scope.label),
659 Cell::new(&scope.description),
660 ]);
661 }
662
663 table
664}
665
666pub fn users_table(users: &[UserResponse]) -> Table {
668 let mut table = base_table();
669 table.set_header(vec!["ID", "Username", "Email", "Admin", "Created"]);
670
671 for user in users {
672 let admin = Cell::new(if user.is_admin { "yes" } else { "no" })
673 .fg(if user.is_admin {
674 Color::Magenta
675 } else {
676 Color::Grey
677 })
678 .set_alignment(CellAlignment::Center);
679
680 table.add_row(vec![
681 Cell::new(user.id),
682 Cell::new(&user.username),
683 Cell::new(&user.email),
684 admin,
685 Cell::new(format_datetime(&user.created_at)),
686 ]);
687 }
688
689 table
690}
691
692fn short_id(id: Uuid) -> String {
694 id.to_string()
695 .split('-')
696 .next()
697 .unwrap_or_default()
698 .to_string()
699}
700
701fn format_optional_id(id: &Option<Uuid>) -> String {
703 id.map_or_else(|| "-".to_string(), short_id)
704}
705
706pub fn audit_logs_table(entries: &[AuditLogEntry]) -> Table {
711 let mut table = base_table();
712 table.set_header(vec!["ID", "Type", "Run", "Step", "User", "Created"]);
713
714 for entry in entries {
715 table.add_row(vec![
716 Cell::new(short_id(entry.id)),
717 Cell::new(entry.event_type.to_string()),
718 Cell::new(format_optional_id(&entry.run_id)),
719 Cell::new(format_optional_id(&entry.step_id)),
720 Cell::new(format_optional_id(&entry.user_id)),
721 Cell::new(format_datetime(&entry.created_at)),
722 ]);
723 }
724
725 table
726}
727
728#[cfg(test)]
729mod tests {
730 use std::collections::HashMap;
731 use std::slice;
732
733 use ironflow_sdk::types::{ApiKeyScope, CreatedBy, CreatedByKind, EventKind, TriggerKind};
734 use serde_json::{Map, Value};
735
736 use super::*;
737
738 fn run_fixture(created_by: CreatedBy) -> RunResponse {
740 let now = Utc::now();
741 RunResponse {
742 id: Uuid::now_v7(),
743 workflow_name: "deploy".to_string(),
744 status: RunStatus::Completed,
745 trigger: TriggerKind::Api,
746 error: None,
747 retry_count: 0,
748 max_retries: 0,
749 cost_usd: 0.0,
750 duration_ms: 0,
751 created_at: now,
752 updated_at: now,
753 started_at: None,
754 completed_at: None,
755 handler_version: None,
756 labels: HashMap::new(),
757 scheduled_at: None,
758 created_by,
759 idempotency_key: None,
760 max_cost_usd: None,
761 }
762 }
763
764 #[test]
765 fn format_cost_without_cap_shows_amount_only() {
766 assert_eq!(format_cost(0.1234, None), "$0.1234");
767 }
768
769 #[test]
770 fn format_cost_with_cap_shows_both_amounts() {
771 assert_eq!(format_cost(0.18, Some(2.0)), "$0.1800 / $2.00");
772 }
773
774 #[test]
775 fn cost_color_is_absent_without_a_cap() {
776 assert_eq!(cost_color(999.0, None), None);
777 }
778
779 #[test]
780 fn cost_color_warns_past_the_threshold_and_alerts_at_the_cap() {
781 assert_eq!(cost_color(1.0, Some(2.0)), None); assert_eq!(cost_color(1.6, Some(2.0)), Some(Color::Yellow)); assert_eq!(cost_color(1.99, Some(2.0)), Some(Color::Yellow));
784 assert_eq!(cost_color(2.0, Some(2.0)), Some(Color::Red)); assert_eq!(cost_color(2.5, Some(2.0)), Some(Color::Red)); }
787
788 #[test]
789 fn cost_color_handles_a_zero_cap() {
790 assert_eq!(cost_color(0.0, Some(0.0)), None);
791 assert_eq!(cost_color(0.01, Some(0.0)), Some(Color::Red));
792 }
793
794 fn artifact(name: &str, size_bytes: i64) -> ArtifactResponse {
795 ArtifactResponse {
796 id: Uuid::now_v7(),
797 step_id: Uuid::now_v7(),
798 name: name.to_string(),
799 content_type: "text/plain".to_string(),
800 size_bytes,
801 sha256: "0".repeat(64),
802 created_at: Utc::now(),
803 }
804 }
805
806 #[test]
807 fn format_bytes_keeps_raw_bytes_below_one_kilobyte() {
808 assert_eq!(format_bytes(0), "0 B");
809 assert_eq!(format_bytes(1023), "1023 B");
810 }
811
812 #[test]
813 fn format_bytes_switches_units_at_each_boundary() {
814 assert_eq!(format_bytes(1024), "1.0 KB");
815 assert_eq!(format_bytes(1024 * 1024), "1.0 MB");
816 assert_eq!(format_bytes(1024 * 1024 * 1024), "1.0 GB");
817 }
818
819 #[test]
820 fn format_bytes_drops_the_decimal_past_ten() {
821 assert_eq!(format_bytes(145_408), "142 KB");
822 }
823
824 #[test]
825 fn format_artifacts_shows_a_dash_when_there_are_none() {
826 assert_eq!(format_artifacts(&[]), "-");
827 }
828
829 #[test]
830 fn format_artifacts_shows_the_count_and_total_size() {
831 let artifacts = vec![artifact("a.txt", 1024), artifact("b.txt", 1024)];
832 assert_eq!(format_artifacts(&artifacts), "2 (2.0 KB)");
833 }
834
835 #[test]
836 fn format_duration_ms_millis() {
837 assert_eq!(format_duration_ms(500), "500ms");
838 assert_eq!(format_duration_ms(0), "0ms");
839 }
840
841 #[test]
842 fn format_duration_ms_seconds() {
843 assert_eq!(format_duration_ms(5000), "5s");
844 assert_eq!(format_duration_ms(59000), "59s");
845 }
846
847 #[test]
848 fn format_duration_ms_minutes() {
849 assert_eq!(format_duration_ms(60000), "1m 0s");
850 assert_eq!(format_duration_ms(125000), "2m 5s");
851 }
852
853 #[test]
854 fn format_duration_ms_hours() {
855 assert_eq!(format_duration_ms(3_600_000), "1h 0m");
856 assert_eq!(format_duration_ms(5_400_000), "1h 30m");
857 }
858
859 #[test]
860 fn format_optional_datetime_none() {
861 assert_eq!(format_optional_datetime(&None), "-");
862 }
863
864 #[test]
865 fn format_optional_datetime_some() {
866 let dt = "2026-06-02T14:30:00Z".parse::<DateTime<Utc>>().unwrap();
867 assert_eq!(format_optional_datetime(&Some(dt)), "2026-06-02 14:30:00");
868 }
869
870 #[test]
871 fn status_colors_are_distinct() {
872 let statuses = [
873 RunStatus::Completed,
874 RunStatus::Failed,
875 RunStatus::Running,
876 RunStatus::Pending,
877 RunStatus::Cancelled,
878 RunStatus::AwaitingApproval,
879 RunStatus::Retrying,
880 ];
881
882 let colors: Vec<Color> = statuses.iter().map(status_color).collect();
883 for (i, c1) in colors.iter().enumerate() {
884 for (j, c2) in colors.iter().enumerate() {
885 if i != j {
886 assert_ne!(c1, c2, "status colors must be distinct");
887 }
888 }
889 }
890 }
891
892 #[test]
893 fn empty_runs_table_has_header() {
894 let table = runs_table(&[]);
895 let output = table.to_string();
896 assert!(output.contains("ID"));
897 assert!(output.contains("Workflow"));
898 assert!(output.contains("Status"));
899 assert!(output.contains("Triggered by"));
900 }
901
902 #[test]
903 fn runs_table_renders_the_author_label() {
904 let run = run_fixture(CreatedBy {
905 kind: CreatedByKind::ApiKey,
906 id: Some(Uuid::now_v7()),
907 label: "ci-deploy (alice)".to_string(),
908 });
909
910 let output = runs_table(slice::from_ref(&run)).to_string();
911 assert!(
912 output.contains("ci-deploy (alice)"),
913 "author missing from:\n{output}"
914 );
915 }
916
917 #[test]
918 fn run_detail_table_renders_the_author_label() {
919 let detail = RunDetailResponse {
920 run: run_fixture(CreatedBy {
921 kind: CreatedByKind::System,
922 id: None,
923 label: "/hooks/github".to_string(),
924 }),
925 steps: Vec::new(),
926 payload: Value::Object(Map::new()),
927 };
928
929 let output = run_detail_table(&detail).to_string();
930 assert!(output.contains("Triggered by"));
931 assert!(
932 output.contains("/hooks/github"),
933 "author missing from:\n{output}"
934 );
935 }
936
937 #[test]
938 fn empty_workflows_table_has_header() {
939 let table = workflows_table(&[]);
940 let output = table.to_string();
941 assert!(output.contains("Name"));
942 assert!(output.contains("Category"));
943 }
944
945 fn secret_fixture(key: &str) -> SecretResponse {
948 let now = Utc::now();
949 SecretResponse {
950 id: Uuid::now_v7(),
951 key: key.to_string(),
952 created_at: now,
953 updated_at: now,
954 }
955 }
956
957 #[test]
958 fn empty_secrets_table_has_header() {
959 let output = secrets_table(&[]).to_string();
960 assert!(output.contains("Key"));
961 assert!(output.contains("Created"));
962 assert!(output.contains("Updated"));
963 }
964
965 #[test]
966 fn secrets_table_renders_the_key() {
967 let secret = secret_fixture("workflows/inbox/gmail_token");
968 let output = secrets_table(slice::from_ref(&secret)).to_string();
969 assert!(output.contains("workflows/inbox/gmail_token"), "{output}");
970 }
971
972 #[test]
975 fn a_secret_response_carries_no_value_at_all() {
976 let secret = secret_fixture("db/password");
977 let json = serde_json::to_string(&secret).unwrap();
978 assert!(!json.contains("value"), "{json}");
979 }
980
981 fn api_key_fixture() -> ApiKeyResponse {
984 ApiKeyResponse {
985 id: Uuid::now_v7(),
986 name: "ci-deploy".to_string(),
987 key_prefix: "ifk_abcd".to_string(),
988 scopes: vec![ApiKeyScope::RunsRead, ApiKeyScope::RunsWrite],
989 is_active: true,
990 created_at: Utc::now(),
991 expires_at: None,
992 last_used_at: None,
993 rate_limit_override: None,
994 }
995 }
996
997 #[test]
998 fn empty_api_keys_table_has_header() {
999 let output = api_keys_table(&[]).to_string();
1000 for header in ["ID", "Name", "Prefix", "Scopes", "Active"] {
1001 assert!(output.contains(header), "missing {header} in {output}");
1002 }
1003 }
1004
1005 #[test]
1006 fn api_keys_table_joins_the_scopes() {
1007 let key = api_key_fixture();
1008 let output = api_keys_table(slice::from_ref(&key)).to_string();
1009 assert!(output.contains("runs_read, runs_write"), "{output}");
1010 assert!(output.contains("ifk_abcd"), "{output}");
1011 }
1012
1013 #[test]
1014 fn created_api_key_table_shows_the_raw_key() {
1015 let created = CreateApiKeyResponse {
1016 id: Uuid::now_v7(),
1017 name: "ci-deploy".to_string(),
1018 key: "ifk_full_raw_key".to_string(),
1019 key_prefix: "ifk_full".to_string(),
1020 scopes: vec![ApiKeyScope::Admin],
1021 created_at: Utc::now(),
1022 expires_at: None,
1023 rate_limit_override: None,
1024 };
1025
1026 let output = created_api_key_table(&created).to_string();
1027 assert!(output.contains("ifk_full_raw_key"), "{output}");
1028 }
1029
1030 #[test]
1031 fn empty_scopes_table_has_header() {
1032 let output = scopes_table(&[]).to_string();
1033 assert!(output.contains("Value"));
1034 assert!(output.contains("Description"));
1035 }
1036
1037 fn user_fixture(is_admin: bool) -> UserResponse {
1040 let now = Utc::now();
1041 UserResponse {
1042 id: Uuid::now_v7(),
1043 username: "alice".to_string(),
1044 email: "alice@example.com".to_string(),
1045 is_admin,
1046 created_at: now,
1047 updated_at: now,
1048 }
1049 }
1050
1051 #[test]
1052 fn empty_users_table_has_header() {
1053 let output = users_table(&[]).to_string();
1054 for header in ["ID", "Username", "Email", "Admin", "Created"] {
1055 assert!(output.contains(header), "missing {header} in {output}");
1056 }
1057 }
1058
1059 #[test]
1060 fn users_table_spells_out_the_role() {
1061 let admin = user_fixture(true);
1062 assert!(
1063 users_table(slice::from_ref(&admin))
1064 .to_string()
1065 .contains("yes")
1066 );
1067
1068 let member = user_fixture(false);
1069 assert!(
1070 users_table(slice::from_ref(&member))
1071 .to_string()
1072 .contains("no")
1073 );
1074 }
1075
1076 #[test]
1079 fn empty_audit_logs_table_has_header() {
1080 let output = audit_logs_table(&[]).to_string();
1081 for header in ["ID", "Type", "Run", "Step", "User", "Created"] {
1082 assert!(output.contains(header), "missing {header} in {output}");
1083 }
1084 }
1085
1086 #[test]
1087 fn audit_logs_table_omits_the_payload() {
1088 let entry = AuditLogEntry {
1089 id: Uuid::now_v7(),
1090 event_type: EventKind::RunCreated,
1091 payload: Value::Object(Map::new()),
1092 run_id: Some(Uuid::now_v7()),
1093 step_id: None,
1094 user_id: None,
1095 created_at: Utc::now(),
1096 };
1097
1098 let output = audit_logs_table(slice::from_ref(&entry)).to_string();
1099 assert!(output.contains("run_created"), "{output}");
1100 assert!(output.contains('-'), "{output}");
1102 }
1103
1104 #[test]
1105 fn format_optional_id_shortens_and_falls_back() {
1106 assert_eq!(format_optional_id(&None), "-");
1107 let id = Uuid::now_v7();
1108 let short = format_optional_id(&Some(id));
1109 assert_eq!(short, id.to_string().split('-').next().unwrap());
1110 }
1111
1112 #[test]
1115 fn deleted_table_reports_the_kind_and_id() {
1116 let deleted = Deleted::new("secret", "db/password");
1117 let output = deleted_table(&deleted).to_string();
1118 assert!(output.contains("secret"), "{output}");
1119 assert!(output.contains("db/password"), "{output}");
1120
1121 let json = serde_json::to_string(&deleted).unwrap();
1122 assert!(json.contains(r#""deleted":true"#), "{json}");
1123 }
1124}