| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
use chrono::{DateTime, TimeDelta, Utc}; |
| 9 |
use ops_status::{Action, Event, Node, Payload, Status}; |
| 10 |
|
| 11 |
use crate::config::Series; |
| 12 |
use crate::store::Reading; |
| 13 |
|
| 14 |
|
| 15 |
pub(crate) struct SourceState { |
| 16 |
pub name: String, |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
pub allow_actions: bool, |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
pub payload: Option<Payload>, |
| 26 |
|
| 27 |
pub error: Option<String>, |
| 28 |
|
| 29 |
pub last_ok: Option<DateTime<Utc>>, |
| 30 |
|
| 31 |
pub stale_after: TimeDelta, |
| 32 |
} |
| 33 |
|
| 34 |
impl SourceState { |
| 35 |
pub(crate) fn new(name: impl Into<String>, stale_after: TimeDelta) -> Self { |
| 36 |
SourceState { |
| 37 |
name: name.into(), |
| 38 |
allow_actions: false, |
| 39 |
payload: None, |
| 40 |
error: None, |
| 41 |
last_ok: None, |
| 42 |
stale_after, |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
pub(crate) fn with_actions(mut self, allow: bool) -> Self { |
| 49 |
self.allow_actions = allow; |
| 50 |
self |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
pub(crate) fn action(&self, key: &str) -> Option<&Action> { |
| 57 |
self.payload.as_ref().and_then(|p| p.actions.get(key)) |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
pub(crate) fn age(&self, now: DateTime<Utc>) -> Option<TimeDelta> { |
| 62 |
self.payload.as_ref().map(|p| p.age(now)) |
| 63 |
} |
| 64 |
|
| 65 |
pub(crate) fn is_stale(&self, now: DateTime<Utc>) -> bool { |
| 66 |
self.age(now).is_some_and(|age| age > self.stale_after) |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
pub(crate) fn status(&self, now: DateTime<Utc>) -> Status { |
| 82 |
let Some(payload) = &self.payload else { |
| 83 |
return Status::Unknown; |
| 84 |
}; |
| 85 |
if self.error.is_some() || self.is_stale(now) { |
| 86 |
return payload.worst_status().max(Status::Degraded); |
| 87 |
} |
| 88 |
payload.worst_status() |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
pub(crate) fn summary(&self, now: DateTime<Utc>) -> String { |
| 93 |
if let Some(error) = &self.error { |
| 94 |
let last = match self.last_ok { |
| 95 |
Some(at) => crate::value::relative(at, now), |
| 96 |
None => "never".into(), |
| 97 |
}; |
| 98 |
return format!("unreachable ({error}); last ok {last}"); |
| 99 |
} |
| 100 |
let Some(payload) = &self.payload else { |
| 101 |
return "waiting for first poll".into(); |
| 102 |
}; |
| 103 |
if self.is_stale(now) { |
| 104 |
return format!( |
| 105 |
"stale: last answered {}", |
| 106 |
crate::value::duration(payload.age(now).num_seconds()) |
| 107 |
); |
| 108 |
} |
| 109 |
|
| 110 |
let failing = payload |
| 111 |
.nodes |
| 112 |
.iter() |
| 113 |
.filter(|n| n.status >= Status::Degraded) |
| 114 |
.count(); |
| 115 |
match (failing, payload.nodes.len()) { |
| 116 |
(0, 1) => "1 node ok".into(), |
| 117 |
(0, total) => format!("{total} nodes ok"), |
| 118 |
(1, _) => "1 node needs attention".into(), |
| 119 |
(n, _) => format!("{n} nodes need attention"), |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
pub(crate) fn rows(&self) -> Vec<Row<'_>> { |
| 130 |
let Some(payload) = &self.payload else { |
| 131 |
return Vec::new(); |
| 132 |
}; |
| 133 |
let mut rows = Vec::new(); |
| 134 |
for root in payload.roots() { |
| 135 |
rows.push(Row { |
| 136 |
node: root, |
| 137 |
depth: 0, |
| 138 |
}); |
| 139 |
for child_id in &root.children { |
| 140 |
if let Some(child) = payload.node(child_id) { |
| 141 |
rows.push(Row { |
| 142 |
node: child, |
| 143 |
depth: 1, |
| 144 |
}); |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
rows |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
pub(crate) fn observe(&mut self, payload: Payload, at: DateTime<Utc>) { |
| 158 |
self.payload = Some(payload); |
| 159 |
self.error = None; |
| 160 |
self.last_ok = Some(at); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
pub(crate) fn observe_error(&mut self, error: impl Into<String>) { |
| 165 |
self.error = Some(error.into()); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
pub(crate) struct Row<'a> { |
| 172 |
pub node: &'a Node, |
| 173 |
pub depth: usize, |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 186 |
pub(crate) enum Tab { |
| 187 |
|
| 188 |
|
| 189 |
Live, |
| 190 |
|
| 191 |
Logs, |
| 192 |
|
| 193 |
Store, |
| 194 |
} |
| 195 |
|
| 196 |
impl Tab { |
| 197 |
|
| 198 |
pub(crate) const ALL: [Tab; 3] = [Tab::Live, Tab::Logs, Tab::Store]; |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
pub(crate) fn titles() -> Vec<&'static str> { |
| 204 |
Tab::ALL.iter().map(|tab| tab.title()).collect() |
| 205 |
} |
| 206 |
|
| 207 |
pub(crate) fn title(self) -> &'static str { |
| 208 |
match self { |
| 209 |
Tab::Live => "live", |
| 210 |
Tab::Logs => "logs", |
| 211 |
Tab::Store => "store", |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
pub(crate) enum LiveRow<'a> { |
| 223 |
Source { |
| 224 |
index: usize, |
| 225 |
}, |
| 226 |
Node { |
| 227 |
|
| 228 |
index: usize, |
| 229 |
node: &'a Node, |
| 230 |
|
| 231 |
|
| 232 |
depth: usize, |
| 233 |
}, |
| 234 |
} |
| 235 |
|
| 236 |
impl LiveRow<'_> { |
| 237 |
|
| 238 |
pub(crate) fn source_index(&self) -> usize { |
| 239 |
match self { |
| 240 |
LiveRow::Source { index } | LiveRow::Node { index, .. } => *index, |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
pub(crate) fn node(&self) -> Option<&Node> { |
| 245 |
match self { |
| 246 |
LiveRow::Source { .. } => None, |
| 247 |
LiveRow::Node { node, .. } => Some(node), |
| 248 |
} |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
pub(crate) struct LogRow<'a> { |
| 254 |
pub source: &'a str, |
| 255 |
pub event: &'a Event, |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
pub(crate) struct StoreState { |
| 260 |
pub name: String, |
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
pub series: Vec<Series>, |
| 265 |
|
| 266 |
|
| 267 |
pub readings: Vec<Reading>, |
| 268 |
|
| 269 |
pub error: Option<String>, |
| 270 |
pub last_ok: Option<DateTime<Utc>>, |
| 271 |
} |
| 272 |
|
| 273 |
impl StoreState { |
| 274 |
pub(crate) fn new(name: impl Into<String>, series: Vec<Series>) -> Self { |
| 275 |
StoreState { |
| 276 |
name: name.into(), |
| 277 |
series, |
| 278 |
readings: Vec::new(), |
| 279 |
error: None, |
| 280 |
last_ok: None, |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
pub(crate) fn observe(&mut self, readings: Vec<Reading>, at: DateTime<Utc>) { |
| 285 |
self.readings = readings; |
| 286 |
self.error = None; |
| 287 |
self.last_ok = Some(at); |
| 288 |
} |
| 289 |
|
| 290 |
pub(crate) fn observe_error(&mut self, error: impl Into<String>) { |
| 291 |
self.error = Some(error.into()); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
|
| 296 |
pub(crate) enum StoreRow<'a> { |
| 297 |
|
| 298 |
|
| 299 |
Unavailable { store: &'a str, reason: &'a str }, |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
Missing { store: &'a str, spec: &'a Series }, |
| 304 |
|
| 305 |
Value { |
| 306 |
store: &'a str, |
| 307 |
spec: &'a Series, |
| 308 |
reading: &'a Reading, |
| 309 |
}, |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 319 |
pub(crate) enum Prompt { |
| 320 |
|
| 321 |
Pick { |
| 322 |
source: usize, |
| 323 |
keys: Vec<String>, |
| 324 |
selected: usize, |
| 325 |
}, |
| 326 |
|
| 327 |
Confirm { source: usize, key: String }, |
| 328 |
|
| 329 |
|
| 330 |
Type { |
| 331 |
source: usize, |
| 332 |
key: String, |
| 333 |
typed: String, |
| 334 |
}, |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 341 |
pub(crate) struct FireRequest { |
| 342 |
pub source: usize, |
| 343 |
pub key: String, |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
#[derive(Debug, PartialEq, Eq)] |
| 348 |
pub(crate) enum PromptStep { |
| 349 |
|
| 350 |
Idle, |
| 351 |
|
| 352 |
Fire(FireRequest), |
| 353 |
|
| 354 |
Cancelled, |
| 355 |
} |
| 356 |
|
| 357 |
pub(crate) struct Model { |
| 358 |
pub sources: Vec<SourceState>, |
| 359 |
pub stores: Vec<StoreState>, |
| 360 |
pub tab: Tab, |
| 361 |
|
| 362 |
pub selected: usize, |
| 363 |
|
| 364 |
pub logs_scroll: usize, |
| 365 |
|
| 366 |
pub store_scroll: usize, |
| 367 |
|
| 368 |
pub message: Option<String>, |
| 369 |
|
| 370 |
pub prompt: Option<Prompt>, |
| 371 |
} |
| 372 |
|
| 373 |
impl Model { |
| 374 |
pub(crate) fn new(sources: Vec<SourceState>) -> Self { |
| 375 |
Model { |
| 376 |
sources, |
| 377 |
stores: Vec::new(), |
| 378 |
tab: Tab::Live, |
| 379 |
selected: 0, |
| 380 |
logs_scroll: 0, |
| 381 |
store_scroll: 0, |
| 382 |
message: None, |
| 383 |
prompt: None, |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
pub(crate) fn with_stores(mut self, stores: Vec<StoreState>) -> Self { |
| 391 |
self.stores = stores; |
| 392 |
self |
| 393 |
} |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
pub(crate) fn open_actions(&mut self, now: DateTime<Utc>) { |
| 406 |
if self.tab != Tab::Live { |
| 407 |
return; |
| 408 |
} |
| 409 |
let rows = self.live_rows(now); |
| 410 |
let Some(row) = rows.get(self.selected) else { |
| 411 |
return; |
| 412 |
}; |
| 413 |
let index = row.source_index(); |
| 414 |
let Some(node) = row.node() else { return }; |
| 415 |
if node.actions.is_empty() { |
| 416 |
return; |
| 417 |
} |
| 418 |
let keys = node.actions.clone(); |
| 419 |
drop(rows); |
| 420 |
let Some(source) = self.sources.get(index) else { |
| 421 |
return; |
| 422 |
}; |
| 423 |
if !source.allow_actions { |
| 424 |
self.message = Some(format!( |
| 425 |
"{}: actions are read-only here (set allow_actions to enable)", |
| 426 |
source.name |
| 427 |
)); |
| 428 |
return; |
| 429 |
} |
| 430 |
self.prompt = Some(Prompt::Pick { |
| 431 |
source: index, |
| 432 |
keys, |
| 433 |
selected: 0, |
| 434 |
}); |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
pub(crate) fn prompt_move(&mut self, delta: isize) { |
| 439 |
if let Some(Prompt::Pick { keys, selected, .. }) = &mut self.prompt { |
| 440 |
if keys.is_empty() { |
| 441 |
return; |
| 442 |
} |
| 443 |
let next = *selected as isize + delta; |
| 444 |
*selected = next.clamp(0, keys.len() as isize - 1) as usize; |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
|
| 449 |
pub(crate) fn prompt_digit(&mut self, n: usize) { |
| 450 |
if let Some(Prompt::Pick { keys, selected, .. }) = &mut self.prompt |
| 451 |
&& (1..=keys.len()).contains(&n) |
| 452 |
{ |
| 453 |
*selected = n - 1; |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
pub(crate) fn prompt_push(&mut self, c: char) { |
| 459 |
if let Some(Prompt::Type { typed, .. }) = &mut self.prompt { |
| 460 |
typed.push(c); |
| 461 |
} |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
pub(crate) fn prompt_backspace(&mut self) { |
| 466 |
if let Some(Prompt::Type { typed, .. }) = &mut self.prompt { |
| 467 |
typed.pop(); |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
pub(crate) fn prompt_enter(&mut self) -> PromptStep { |
| 479 |
match self.prompt.take() { |
| 480 |
Some(Prompt::Pick { |
| 481 |
source, |
| 482 |
keys, |
| 483 |
selected, |
| 484 |
}) => { |
| 485 |
let Some(key) = keys.get(selected).cloned() else { |
| 486 |
return PromptStep::Cancelled; |
| 487 |
}; |
| 488 |
let Some(action) = self.sources.get(source).and_then(|s| s.action(&key)) else { |
| 489 |
self.message = Some(format!("{key}: no longer offered")); |
| 490 |
return PromptStep::Cancelled; |
| 491 |
}; |
| 492 |
if action.danger { |
| 493 |
self.prompt = Some(Prompt::Type { |
| 494 |
source, |
| 495 |
key, |
| 496 |
typed: String::new(), |
| 497 |
}); |
| 498 |
PromptStep::Idle |
| 499 |
} else if action.confirm { |
| 500 |
self.prompt = Some(Prompt::Confirm { source, key }); |
| 501 |
PromptStep::Idle |
| 502 |
} else { |
| 503 |
self.fire(source, key) |
| 504 |
} |
| 505 |
} |
| 506 |
Some(Prompt::Type { source, key, typed }) => { |
| 507 |
if typed == key { |
| 508 |
self.fire(source, key) |
| 509 |
} else { |
| 510 |
self.message = Some(format!("type '{key}' exactly to confirm")); |
| 511 |
self.prompt = Some(Prompt::Type { |
| 512 |
source, |
| 513 |
key, |
| 514 |
typed: String::new(), |
| 515 |
}); |
| 516 |
PromptStep::Idle |
| 517 |
} |
| 518 |
} |
| 519 |
other => { |
| 520 |
self.prompt = other; |
| 521 |
PromptStep::Idle |
| 522 |
} |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
pub(crate) fn confirm_yes(&mut self) -> PromptStep { |
| 528 |
if let Some(Prompt::Confirm { source, key }) = self.prompt.take() { |
| 529 |
self.fire(source, key) |
| 530 |
} else { |
| 531 |
PromptStep::Idle |
| 532 |
} |
| 533 |
} |
| 534 |
|
| 535 |
|
| 536 |
pub(crate) fn cancel_prompt(&mut self) -> PromptStep { |
| 537 |
if self.prompt.take().is_some() { |
| 538 |
PromptStep::Cancelled |
| 539 |
} else { |
| 540 |
PromptStep::Idle |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
fn fire(&mut self, source: usize, key: String) -> PromptStep { |
| 548 |
self.prompt = None; |
| 549 |
PromptStep::Fire(FireRequest { source, key }) |
| 550 |
} |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
pub(crate) fn rollup_order(&self, now: DateTime<Utc>) -> Vec<usize> { |
| 558 |
let mut order: Vec<usize> = (0..self.sources.len()).collect(); |
| 559 |
order.sort_by(|&a, &b| { |
| 560 |
let (sa, sb) = (self.sources[a].status(now), self.sources[b].status(now)); |
| 561 |
sb.cmp(&sa) |
| 562 |
.then_with(|| self.sources[a].name.cmp(&self.sources[b].name)) |
| 563 |
}); |
| 564 |
order |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
pub(crate) fn worst(&self, now: DateTime<Utc>) -> Status { |
| 569 |
self.sources |
| 570 |
.iter() |
| 571 |
.map(|s| s.status(now)) |
| 572 |
.max() |
| 573 |
.unwrap_or(Status::Unknown) |
| 574 |
} |
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
pub(crate) fn live_rows(&self, now: DateTime<Utc>) -> Vec<LiveRow<'_>> { |
| 584 |
let mut rows = Vec::new(); |
| 585 |
for index in self.rollup_order(now) { |
| 586 |
rows.push(LiveRow::Source { index }); |
| 587 |
for row in self.sources[index].rows() { |
| 588 |
rows.push(LiveRow::Node { |
| 589 |
index, |
| 590 |
node: row.node, |
| 591 |
depth: row.depth + 1, |
| 592 |
}); |
| 593 |
} |
| 594 |
} |
| 595 |
rows |
| 596 |
} |
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
pub(crate) fn log_rows(&self) -> Vec<LogRow<'_>> { |
| 605 |
let mut order: Vec<usize> = (0..self.sources.len()).collect(); |
| 606 |
order.sort_by(|&a, &b| self.sources[a].name.cmp(&self.sources[b].name)); |
| 607 |
|
| 608 |
let mut rows = Vec::new(); |
| 609 |
for index in order { |
| 610 |
let source = &self.sources[index]; |
| 611 |
let Some(payload) = &source.payload else { |
| 612 |
continue; |
| 613 |
}; |
| 614 |
let mut events: Vec<&Event> = payload.events.iter().collect(); |
| 615 |
events.sort_by_key(|event| std::cmp::Reverse(event.at)); |
| 616 |
rows.extend(events.into_iter().map(|event| LogRow { |
| 617 |
source: source.name.as_str(), |
| 618 |
event, |
| 619 |
})); |
| 620 |
} |
| 621 |
rows |
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
pub(crate) fn store_rows(&self) -> Vec<StoreRow<'_>> { |
| 632 |
let mut rows = Vec::new(); |
| 633 |
for store in &self.stores { |
| 634 |
if let Some(reason) = &store.error { |
| 635 |
rows.push(StoreRow::Unavailable { |
| 636 |
store: &store.name, |
| 637 |
reason, |
| 638 |
}); |
| 639 |
} |
| 640 |
for spec in &store.series { |
| 641 |
let mut any = false; |
| 642 |
for reading in store.readings.iter().filter(|r| r.series == spec.name) { |
| 643 |
any = true; |
| 644 |
rows.push(StoreRow::Value { |
| 645 |
store: &store.name, |
| 646 |
spec, |
| 647 |
reading, |
| 648 |
}); |
| 649 |
} |
| 650 |
if !any { |
| 651 |
rows.push(StoreRow::Missing { |
| 652 |
store: &store.name, |
| 653 |
spec, |
| 654 |
}); |
| 655 |
} |
| 656 |
} |
| 657 |
} |
| 658 |
rows |
| 659 |
} |
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
pub(crate) fn tab_index(&self) -> usize { |
| 664 |
Tab::ALL.iter().position(|t| *t == self.tab).unwrap_or(0) |
| 665 |
} |
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
pub(crate) fn select_tab(&mut self, index: usize) { |
| 670 |
if let Some(tab) = Tab::ALL.get(index) { |
| 671 |
self.tab = *tab; |
| 672 |
} |
| 673 |
} |
| 674 |
|
| 675 |
pub(crate) fn next_tab(&mut self) { |
| 676 |
self.select_tab((self.tab_index() + 1) % Tab::ALL.len()); |
| 677 |
} |
| 678 |
|
| 679 |
pub(crate) fn prev_tab(&mut self) { |
| 680 |
let count = Tab::ALL.len(); |
| 681 |
self.select_tab((self.tab_index() + count - 1) % count); |
| 682 |
} |
| 683 |
|
| 684 |
|
| 685 |
pub(crate) fn move_selection(&mut self, delta: isize, now: DateTime<Utc>) { |
| 686 |
match self.tab { |
| 687 |
Tab::Live => { |
| 688 |
let len = self.live_rows(now).len(); |
| 689 |
self.selected = clamped(self.selected, delta, len); |
| 690 |
} |
| 691 |
Tab::Logs => { |
| 692 |
let len = self.log_rows().len(); |
| 693 |
self.logs_scroll = clamped(self.logs_scroll, delta, len); |
| 694 |
} |
| 695 |
Tab::Store => { |
| 696 |
let len = self.store_rows().len(); |
| 697 |
self.store_scroll = clamped(self.store_scroll, delta, len); |
| 698 |
} |
| 699 |
} |
| 700 |
} |
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
pub(crate) fn clamp_selection(&mut self, now: DateTime<Utc>) { |
| 709 |
let live = self.live_rows(now).len(); |
| 710 |
self.selected = self.selected.min(live.saturating_sub(1)); |
| 711 |
let logs = self.log_rows().len(); |
| 712 |
self.logs_scroll = self.logs_scroll.min(logs.saturating_sub(1)); |
| 713 |
let store = self.store_rows().len(); |
| 714 |
self.store_scroll = self.store_scroll.min(store.saturating_sub(1)); |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
|
| 719 |
fn clamped(current: usize, delta: isize, len: usize) -> usize { |
| 720 |
if len == 0 { |
| 721 |
return 0; |
| 722 |
} |
| 723 |
let next = current as isize + delta; |
| 724 |
next.clamp(0, len as isize - 1) as usize |
| 725 |
} |
| 726 |
|
| 727 |
#[cfg(test)] |
| 728 |
mod tests { |
| 729 |
use super::*; |
| 730 |
use ops_status::{Action, Condition, Method, Node}; |
| 731 |
use std::collections::BTreeMap; |
| 732 |
|
| 733 |
fn now() -> DateTime<Utc> { |
| 734 |
"2026-07-21T18:00:00Z".parse().unwrap() |
| 735 |
} |
| 736 |
|
| 737 |
fn node(id: &str, status: Status, children: Vec<&str>) -> Node { |
| 738 |
Node { |
| 739 |
id: id.into(), |
| 740 |
kind: "tier".into(), |
| 741 |
label: id.into(), |
| 742 |
status, |
| 743 |
fields: Vec::new(), |
| 744 |
conditions: Vec::new(), |
| 745 |
children: children.into_iter().map(Into::into).collect(), |
| 746 |
actions: Vec::new(), |
| 747 |
} |
| 748 |
} |
| 749 |
|
| 750 |
fn act(label: &str, confirm: bool, danger: bool) -> Action { |
| 751 |
Action { |
| 752 |
label: label.into(), |
| 753 |
method: Method::Post, |
| 754 |
url: "/x".into(), |
| 755 |
confirm, |
| 756 |
danger, |
| 757 |
body: None, |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
|
| 762 |
|
| 763 |
fn actionable(node_actions: &[&str], declared: Vec<(&str, Action)>) -> Model { |
| 764 |
let mut n = node("tier:b", Status::Ok, vec![]); |
| 765 |
n.actions = node_actions.iter().map(ToString::to_string).collect(); |
| 766 |
let mut p = payload(now(), vec![n]); |
| 767 |
p.actions = declared |
| 768 |
.into_iter() |
| 769 |
.map(|(k, a)| (k.to_string(), a)) |
| 770 |
.collect::<BTreeMap<_, _>>(); |
| 771 |
let mut s = SourceState::new("sando", TimeDelta::seconds(60)).with_actions(true); |
| 772 |
s.observe(p, now()); |
| 773 |
let mut m = Model::new(vec![s]); |
| 774 |
|
| 775 |
|
| 776 |
m.selected = 1; |
| 777 |
m |
| 778 |
} |
| 779 |
|
| 780 |
fn event(at: DateTime<Utc>, label: &str) -> Event { |
| 781 |
Event { |
| 782 |
at, |
| 783 |
label: label.into(), |
| 784 |
status: None, |
| 785 |
detail: None, |
| 786 |
node_id: None, |
| 787 |
} |
| 788 |
} |
| 789 |
|
| 790 |
fn payload(at: DateTime<Utc>, nodes: Vec<Node>) -> Payload { |
| 791 |
let mut p = Payload::new("sando", at); |
| 792 |
p.nodes = nodes; |
| 793 |
p |
| 794 |
} |
| 795 |
|
| 796 |
fn source(name: &str, at: DateTime<Utc>, nodes: Vec<Node>) -> SourceState { |
| 797 |
let mut s = SourceState::new(name, TimeDelta::seconds(60)); |
| 798 |
s.observe(payload(at, nodes), at); |
| 799 |
s |
| 800 |
} |
| 801 |
|
| 802 |
#[test] |
| 803 |
fn a_source_never_polled_is_unknown_not_ok() { |
| 804 |
let s = SourceState::new("sando", TimeDelta::seconds(60)); |
| 805 |
assert_eq!(s.status(now()), Status::Unknown); |
| 806 |
assert_eq!(s.summary(now()), "waiting for first poll"); |
| 807 |
} |
| 808 |
|
| 809 |
#[test] |
| 810 |
fn a_fresh_healthy_source_is_ok() { |
| 811 |
let s = source("sando", now(), vec![node("tier:b", Status::Ok, vec![])]); |
| 812 |
assert_eq!(s.status(now()), Status::Ok); |
| 813 |
assert_eq!(s.summary(now()), "1 node ok"); |
| 814 |
} |
| 815 |
|
| 816 |
#[test] |
| 817 |
fn a_stale_but_green_source_is_degraded() { |
| 818 |
|
| 819 |
let s = source( |
| 820 |
"pom", |
| 821 |
now() - TimeDelta::hours(4), |
| 822 |
vec![node("backup", Status::Ok, vec![])], |
| 823 |
); |
| 824 |
assert_eq!(s.status(now()), Status::Degraded); |
| 825 |
assert!( |
| 826 |
s.summary(now()).starts_with("stale:"), |
| 827 |
"{}", |
| 828 |
s.summary(now()) |
| 829 |
); |
| 830 |
} |
| 831 |
|
| 832 |
#[test] |
| 833 |
fn staleness_never_downgrades_a_worse_status() { |
| 834 |
let mut s = source( |
| 835 |
"sando", |
| 836 |
now() - TimeDelta::hours(4), |
| 837 |
vec![node("tier:b", Status::Failed, vec![])], |
| 838 |
); |
| 839 |
assert_eq!(s.status(now()), Status::Failed); |
| 840 |
s.observe_error("connection refused"); |
| 841 |
assert_eq!(s.status(now()), Status::Failed); |
| 842 |
} |
| 843 |
|
| 844 |
#[test] |
| 845 |
fn an_unreachable_source_keeps_its_last_payload_and_says_when() { |
| 846 |
let mut s = source("bento", now(), vec![node("app:x", Status::Ok, vec![])]); |
| 847 |
s.observe_error("connection refused"); |
| 848 |
|
| 849 |
|
| 850 |
assert_eq!(s.status(now()), Status::Degraded); |
| 851 |
assert!( |
| 852 |
s.payload.is_some(), |
| 853 |
"the last known state must not go blank" |
| 854 |
); |
| 855 |
let summary = s.summary(now()); |
| 856 |
assert!(summary.contains("connection refused"), "{summary}"); |
| 857 |
assert!(summary.contains("last ok"), "{summary}"); |
| 858 |
} |
| 859 |
|
| 860 |
#[test] |
| 861 |
fn a_source_that_never_answered_and_then_failed_is_unknown() { |
| 862 |
let mut s = SourceState::new("bento", TimeDelta::seconds(60)); |
| 863 |
s.observe_error("connection refused"); |
| 864 |
assert_eq!(s.status(now()), Status::Unknown); |
| 865 |
assert!(s.summary(now()).contains("last ok never")); |
| 866 |
} |
| 867 |
|
| 868 |
#[test] |
| 869 |
fn rows_put_children_under_their_parent() { |
| 870 |
let s = source( |
| 871 |
"sando", |
| 872 |
now(), |
| 873 |
vec![ |
| 874 |
node("tier:b", Status::Ok, vec!["node:prod-1"]), |
| 875 |
node("node:prod-1", Status::Ok, vec![]), |
| 876 |
], |
| 877 |
); |
| 878 |
let rows = s.rows(); |
| 879 |
assert_eq!(rows.len(), 2); |
| 880 |
assert_eq!(rows[0].node.id, "tier:b"); |
| 881 |
assert_eq!(rows[0].depth, 0); |
| 882 |
assert_eq!(rows[1].node.id, "node:prod-1"); |
| 883 |
assert_eq!(rows[1].depth, 1); |
| 884 |
} |
| 885 |
|
| 886 |
#[test] |
| 887 |
fn a_dangling_child_reference_is_skipped_not_fatal() { |
| 888 |
let s = source( |
| 889 |
"sando", |
| 890 |
now(), |
| 891 |
vec![node("tier:b", Status::Ok, vec!["node:ghost"])], |
| 892 |
); |
| 893 |
assert_eq!(s.rows().len(), 1); |
| 894 |
} |
| 895 |
|
| 896 |
#[test] |
| 897 |
fn the_rollup_puts_the_worst_source_first() { |
| 898 |
let m = Model::new(vec![ |
| 899 |
source("aaa", now(), vec![node("n", Status::Ok, vec![])]), |
| 900 |
source("bbb", now(), vec![node("n", Status::Failed, vec![])]), |
| 901 |
source("ccc", now(), vec![node("n", Status::Degraded, vec![])]), |
| 902 |
]); |
| 903 |
let order = m.rollup_order(now()); |
| 904 |
let names: Vec<&str> = order.iter().map(|&i| m.sources[i].name.as_str()).collect(); |
| 905 |
assert_eq!(names, vec!["bbb", "ccc", "aaa"]); |
| 906 |
assert_eq!(m.worst(now()), Status::Failed); |
| 907 |
} |
| 908 |
|
| 909 |
#[test] |
| 910 |
fn an_unreachable_source_outranks_a_merely_degraded_one() { |
| 911 |
let m = Model::new(vec![ |
| 912 |
source("degraded", now(), vec![node("n", Status::Degraded, vec![])]), |
| 913 |
SourceState::new("silent", TimeDelta::seconds(60)), |
| 914 |
]); |
| 915 |
let order = m.rollup_order(now()); |
| 916 |
assert_eq!(m.sources[order[0]].name, "silent"); |
| 917 |
} |
| 918 |
|
| 919 |
#[test] |
| 920 |
fn equal_statuses_sort_by_name_so_the_order_does_not_jitter() { |
| 921 |
let m = Model::new(vec![ |
| 922 |
source("zebra", now(), vec![node("n", Status::Ok, vec![])]), |
| 923 |
source("alpha", now(), vec![node("n", Status::Ok, vec![])]), |
| 924 |
]); |
| 925 |
let order = m.rollup_order(now()); |
| 926 |
let names: Vec<&str> = order.iter().map(|&i| m.sources[i].name.as_str()).collect(); |
| 927 |
assert_eq!(names, vec!["alpha", "zebra"]); |
| 928 |
} |
| 929 |
|
| 930 |
#[test] |
| 931 |
fn the_tabs_are_fixed_and_wrap_in_both_directions() { |
| 932 |
|
| 933 |
|
| 934 |
let mut m = Model::new(vec![source("a", now(), vec![]), source("b", now(), vec![])]); |
| 935 |
assert_eq!(Tab::titles(), vec!["live", "logs", "store"]); |
| 936 |
assert_eq!(m.tab, Tab::Live); |
| 937 |
m.next_tab(); |
| 938 |
assert_eq!(m.tab, Tab::Logs); |
| 939 |
m.next_tab(); |
| 940 |
assert_eq!(m.tab, Tab::Store); |
| 941 |
m.next_tab(); |
| 942 |
assert_eq!(m.tab, Tab::Live); |
| 943 |
m.prev_tab(); |
| 944 |
assert_eq!(m.tab, Tab::Store); |
| 945 |
} |
| 946 |
|
| 947 |
#[test] |
| 948 |
fn a_tab_out_of_range_is_ignored_rather_than_clamped() { |
| 949 |
let mut m = Model::new(vec![source("a", now(), vec![])]); |
| 950 |
m.select_tab(1); |
| 951 |
assert_eq!(m.tab, Tab::Logs); |
| 952 |
m.select_tab(9); |
| 953 |
assert_eq!(m.tab, Tab::Logs, "a mistyped digit must not move the tab"); |
| 954 |
} |
| 955 |
|
| 956 |
#[test] |
| 957 |
fn the_live_rows_put_each_sources_nodes_under_it_worst_first() { |
| 958 |
let mut parent = node("tier:b", Status::Failed, vec!["node:prod-1"]); |
| 959 |
parent.children = vec!["node:prod-1".into()]; |
| 960 |
let m = Model::new(vec![ |
| 961 |
source("healthy", now(), vec![node("n", Status::Ok, vec![])]), |
| 962 |
source( |
| 963 |
"broken", |
| 964 |
now(), |
| 965 |
vec![parent, node("node:prod-1", Status::Ok, vec![])], |
| 966 |
), |
| 967 |
]); |
| 968 |
let rows = m.live_rows(now()); |
| 969 |
|
| 970 |
|
| 971 |
assert_eq!(rows.len(), 5); |
| 972 |
assert!(matches!(rows[0], LiveRow::Source { index: 1 })); |
| 973 |
assert!(matches!(rows[1], LiveRow::Node { depth: 1, .. })); |
| 974 |
assert!(matches!(rows[2], LiveRow::Node { depth: 2, .. })); |
| 975 |
assert!(matches!(rows[3], LiveRow::Source { index: 0 })); |
| 976 |
assert_eq!(rows[1].source_index(), 1, "a node knows its own source"); |
| 977 |
assert!(rows[0].node().is_none(), "a source line is not a node"); |
| 978 |
} |
| 979 |
|
| 980 |
#[test] |
| 981 |
fn selection_cannot_run_off_either_end() { |
| 982 |
let mut m = Model::new(vec![source( |
| 983 |
"sando", |
| 984 |
now(), |
| 985 |
vec![node("a", Status::Ok, vec![]), node("b", Status::Ok, vec![])], |
| 986 |
)]); |
| 987 |
m.move_selection(-5, now()); |
| 988 |
assert_eq!(m.selected, 0); |
| 989 |
m.move_selection(99, now()); |
| 990 |
|
| 991 |
assert_eq!(m.selected, 2); |
| 992 |
} |
| 993 |
|
| 994 |
#[test] |
| 995 |
fn a_shrinking_payload_pulls_the_cursor_back_in_bounds() { |
| 996 |
|
| 997 |
let mut m = Model::new(vec![source( |
| 998 |
"sando", |
| 999 |
now(), |
| 1000 |
vec![ |
| 1001 |
node("a", Status::Ok, vec![]), |
| 1002 |
node("b", Status::Ok, vec![]), |
| 1003 |
node("c", Status::Ok, vec![]), |
| 1004 |
], |
| 1005 |
)]); |
| 1006 |
m.move_selection(3, now()); |
| 1007 |
assert_eq!(m.selected, 3); |
| 1008 |
m.sources[0].observe(payload(now(), vec![node("a", Status::Ok, vec![])]), now()); |
| 1009 |
m.clamp_selection(now()); |
| 1010 |
assert_eq!(m.selected, 1); |
| 1011 |
assert!(m.live_rows(now()).get(m.selected).is_some()); |
| 1012 |
} |
| 1013 |
|
| 1014 |
#[test] |
| 1015 |
fn selection_survives_an_empty_payload() { |
| 1016 |
let mut m = Model::new(vec![SourceState::new("sando", TimeDelta::seconds(60))]); |
| 1017 |
m.sources[0].observe(payload(now(), vec![]), now()); |
| 1018 |
m.move_selection(1, now()); |
| 1019 |
|
| 1020 |
assert_eq!(m.selected, 0); |
| 1021 |
assert!(m.live_rows(now())[0].node().is_none()); |
| 1022 |
} |
| 1023 |
|
| 1024 |
#[test] |
| 1025 |
fn the_logs_group_by_source_by_name_and_run_newest_first() { |
| 1026 |
let mut a = SourceState::new("zebra", TimeDelta::seconds(60)); |
| 1027 |
let mut pz = payload(now(), vec![]); |
| 1028 |
pz.events = vec![event(now(), "z-old"), event(now(), "z-new")]; |
| 1029 |
pz.events[0].at = now() - TimeDelta::minutes(5); |
| 1030 |
a.observe(pz, now()); |
| 1031 |
|
| 1032 |
let mut b = SourceState::new("alpha", TimeDelta::seconds(60)); |
| 1033 |
let mut pa = payload(now(), vec![]); |
| 1034 |
pa.events = vec![event(now(), "a-only")]; |
| 1035 |
b.observe(pa, now()); |
| 1036 |
|
| 1037 |
let m = Model::new(vec![a, b]); |
| 1038 |
let rows = m.log_rows(); |
| 1039 |
let seen: Vec<(&str, &str)> = rows |
| 1040 |
.iter() |
| 1041 |
.map(|r| (r.source, r.event.label.as_str())) |
| 1042 |
.collect(); |
| 1043 |
assert_eq!( |
| 1044 |
seen, |
| 1045 |
vec![("alpha", "a-only"), ("zebra", "z-new"), ("zebra", "z-old"),], |
| 1046 |
"sources in name order, events newest first within each" |
| 1047 |
); |
| 1048 |
} |
| 1049 |
|
| 1050 |
fn spec(series: &str, label: &str) -> Series { |
| 1051 |
Series { |
| 1052 |
name: series.into(), |
| 1053 |
label: label.into(), |
| 1054 |
unit: Some("edges".into()), |
| 1055 |
} |
| 1056 |
} |
| 1057 |
|
| 1058 |
fn reading(series: &str, labels: &str, value: f64) -> Reading { |
| 1059 |
Reading { |
| 1060 |
series: series.into(), |
| 1061 |
labels: labels.into(), |
| 1062 |
value, |
| 1063 |
at: now(), |
| 1064 |
} |
| 1065 |
} |
| 1066 |
|
| 1067 |
#[test] |
| 1068 |
fn the_store_rows_follow_the_config_and_split_by_label_set() { |
| 1069 |
let mut store = StoreState::new( |
| 1070 |
"witchbroom", |
| 1071 |
vec![ |
| 1072 |
spec("soak.coverage_edges", "Coverage reached"), |
| 1073 |
spec("cache.size_bytes", "Cache size"), |
| 1074 |
], |
| 1075 |
); |
| 1076 |
store.observe( |
| 1077 |
vec![ |
| 1078 |
reading("soak.coverage_edges", r#"{"repo":"a"}"#, 100.0), |
| 1079 |
reading("soak.coverage_edges", r#"{"repo":"b"}"#, 200.0), |
| 1080 |
|
| 1081 |
reading("cache.hit_rate_pct", "{}", 90.0), |
| 1082 |
], |
| 1083 |
now(), |
| 1084 |
); |
| 1085 |
let m = Model::new(vec![]).with_stores(vec![store]); |
| 1086 |
let rows = m.store_rows(); |
| 1087 |
|
| 1088 |
assert_eq!(rows.len(), 3, "two label sets plus the unrecorded series"); |
| 1089 |
assert!(matches!( |
| 1090 |
rows[0], |
| 1091 |
StoreRow::Value { spec, .. } if spec.label == "Coverage reached" |
| 1092 |
)); |
| 1093 |
assert!(matches!(rows[1], StoreRow::Value { .. })); |
| 1094 |
|
| 1095 |
assert!(matches!( |
| 1096 |
rows[2], |
| 1097 |
StoreRow::Missing { spec, .. } if spec.label == "Cache size" |
| 1098 |
)); |
| 1099 |
} |
| 1100 |
|
| 1101 |
#[test] |
| 1102 |
fn a_series_the_config_never_named_is_not_a_row() { |
| 1103 |
|
| 1104 |
|
| 1105 |
let mut store = StoreState::new("witchbroom", vec![spec("named", "Named")]); |
| 1106 |
store.observe(vec![reading("unnamed", "{}", 1.0)], now()); |
| 1107 |
let m = Model::new(vec![]).with_stores(vec![store]); |
| 1108 |
let rows = m.store_rows(); |
| 1109 |
assert_eq!(rows.len(), 1); |
| 1110 |
assert!(matches!(rows[0], StoreRow::Missing { .. })); |
| 1111 |
} |
| 1112 |
|
| 1113 |
#[test] |
| 1114 |
fn an_unreadable_store_says_so_above_whatever_it_last_said() { |
| 1115 |
let mut store = StoreState::new("witchbroom", vec![spec("s", "S")]); |
| 1116 |
store.observe(vec![reading("s", "{}", 41.0)], now()); |
| 1117 |
store.observe_error("unable to open database file"); |
| 1118 |
let m = Model::new(vec![]).with_stores(vec![store]); |
| 1119 |
let rows = m.store_rows(); |
| 1120 |
|
| 1121 |
assert!( |
| 1122 |
matches!(rows[0], StoreRow::Unavailable { reason, .. } if reason.contains("open")), |
| 1123 |
"the failure leads, so old numbers are not read as current" |
| 1124 |
); |
| 1125 |
assert!( |
| 1126 |
matches!(rows[1], StoreRow::Value { reading, .. } if (reading.value - 41.0).abs() < f64::EPSILON), |
| 1127 |
"the last known values are still there" |
| 1128 |
); |
| 1129 |
} |
| 1130 |
|
| 1131 |
#[test] |
| 1132 |
fn no_configured_store_is_no_rows_rather_than_an_empty_one() { |
| 1133 |
assert!(Model::new(vec![]).store_rows().is_empty()); |
| 1134 |
} |
| 1135 |
|
| 1136 |
#[test] |
| 1137 |
fn the_store_cursor_cannot_run_off_either_end() { |
| 1138 |
let mut store = StoreState::new("witchbroom", vec![spec("a", "A"), spec("b", "B")]); |
| 1139 |
store.observe(vec![], now()); |
| 1140 |
let mut m = Model::new(vec![]).with_stores(vec![store]); |
| 1141 |
m.tab = Tab::Store; |
| 1142 |
m.move_selection(99, now()); |
| 1143 |
assert_eq!(m.store_scroll, 1); |
| 1144 |
m.move_selection(-99, now()); |
| 1145 |
assert_eq!(m.store_scroll, 0); |
| 1146 |
} |
| 1147 |
|
| 1148 |
#[test] |
| 1149 |
fn a_shrinking_store_pulls_its_cursor_back_in_bounds() { |
| 1150 |
let mut store = StoreState::new("witchbroom", vec![spec("s", "S")]); |
| 1151 |
store.observe( |
| 1152 |
vec![ |
| 1153 |
reading("s", r#"{"repo":"a"}"#, 1.0), |
| 1154 |
reading("s", r#"{"repo":"b"}"#, 2.0), |
| 1155 |
reading("s", r#"{"repo":"c"}"#, 3.0), |
| 1156 |
], |
| 1157 |
now(), |
| 1158 |
); |
| 1159 |
let mut m = Model::new(vec![]).with_stores(vec![store]); |
| 1160 |
m.tab = Tab::Store; |
| 1161 |
m.move_selection(2, now()); |
| 1162 |
assert_eq!(m.store_scroll, 2); |
| 1163 |
m.stores[0].observe(vec![reading("s", r#"{"repo":"a"}"#, 1.0)], now()); |
| 1164 |
m.clamp_selection(now()); |
| 1165 |
assert_eq!(m.store_scroll, 0); |
| 1166 |
} |
| 1167 |
|
| 1168 |
#[test] |
| 1169 |
fn a_source_that_never_answered_contributes_no_log_rows() { |
| 1170 |
let m = Model::new(vec![SourceState::new("bento", TimeDelta::seconds(60))]); |
| 1171 |
assert!(m.log_rows().is_empty()); |
| 1172 |
} |
| 1173 |
|
| 1174 |
#[test] |
| 1175 |
fn a_disabled_source_refuses_to_open_the_picker_and_says_why() { |
| 1176 |
let mut n = node("tier:b", Status::Ok, vec![]); |
| 1177 |
n.actions = vec!["rollback-b".into()]; |
| 1178 |
let mut p = payload(now(), vec![n]); |
| 1179 |
p.actions |
| 1180 |
.insert("rollback-b".into(), act("Roll back", true, true)); |
| 1181 |
|
| 1182 |
let mut s = SourceState::new("sando", TimeDelta::seconds(60)); |
| 1183 |
s.observe(p, now()); |
| 1184 |
let mut m = Model::new(vec![s]); |
| 1185 |
m.selected = 1; |
| 1186 |
|
| 1187 |
m.open_actions(now()); |
| 1188 |
assert!( |
| 1189 |
m.prompt.is_none(), |
| 1190 |
"a read-only source must not open a prompt" |
| 1191 |
); |
| 1192 |
assert!(m.message.as_deref().unwrap().contains("read-only")); |
| 1193 |
} |
| 1194 |
|
| 1195 |
#[test] |
| 1196 |
fn a_node_with_no_actions_does_nothing_on_enter() { |
| 1197 |
let mut m = actionable(&[], vec![]); |
| 1198 |
m.open_actions(now()); |
| 1199 |
assert!(m.prompt.is_none()); |
| 1200 |
assert!(m.message.is_none()); |
| 1201 |
} |
| 1202 |
|
| 1203 |
#[test] |
| 1204 |
fn a_source_line_never_opens_an_action_prompt() { |
| 1205 |
let mut m = actionable( |
| 1206 |
&["rollback-b"], |
| 1207 |
vec![("rollback-b", act("Roll back", true, true))], |
| 1208 |
); |
| 1209 |
m.selected = 0; |
| 1210 |
m.open_actions(now()); |
| 1211 |
assert!( |
| 1212 |
m.prompt.is_none(), |
| 1213 |
"a source declares no actions; its nodes do" |
| 1214 |
); |
| 1215 |
} |
| 1216 |
|
| 1217 |
#[test] |
| 1218 |
fn the_other_tabs_never_open_an_action_prompt() { |
| 1219 |
for tab in [Tab::Logs, Tab::Store] { |
| 1220 |
let mut m = actionable( |
| 1221 |
&["rollback-b"], |
| 1222 |
vec![("rollback-b", act("Roll back", true, true))], |
| 1223 |
); |
| 1224 |
m.tab = tab; |
| 1225 |
m.open_actions(now()); |
| 1226 |
assert!(m.prompt.is_none(), "{tab:?} must not fire actions"); |
| 1227 |
} |
| 1228 |
} |
| 1229 |
|
| 1230 |
#[test] |
| 1231 |
fn a_plain_action_fires_straight_from_the_picker() { |
| 1232 |
|
| 1233 |
let mut m = actionable( |
| 1234 |
&["recheck"], |
| 1235 |
vec![("recheck", act("Recheck", false, false))], |
| 1236 |
); |
| 1237 |
m.open_actions(now()); |
| 1238 |
assert!(matches!(m.prompt, Some(Prompt::Pick { .. }))); |
| 1239 |
let step = m.prompt_enter(); |
| 1240 |
assert_eq!( |
| 1241 |
step, |
| 1242 |
PromptStep::Fire(FireRequest { |
| 1243 |
source: 0, |
| 1244 |
key: "recheck".into() |
| 1245 |
}) |
| 1246 |
); |
| 1247 |
assert!(m.prompt.is_none()); |
| 1248 |
} |
| 1249 |
|
| 1250 |
#[test] |
| 1251 |
fn a_confirm_action_needs_an_explicit_y_and_enter_will_not_do() { |
| 1252 |
let mut m = actionable( |
| 1253 |
&["promote-b"], |
| 1254 |
vec![("promote-b", act("Promote", true, false))], |
| 1255 |
); |
| 1256 |
m.open_actions(now()); |
| 1257 |
assert_eq!(m.prompt_enter(), PromptStep::Idle); |
| 1258 |
assert!( |
| 1259 |
matches!(m.prompt, Some(Prompt::Confirm { .. })), |
| 1260 |
"picker enter opens the y/n guard" |
| 1261 |
); |
| 1262 |
|
| 1263 |
assert_eq!(m.prompt_enter(), PromptStep::Idle); |
| 1264 |
assert!(matches!(m.prompt, Some(Prompt::Confirm { .. }))); |
| 1265 |
|
| 1266 |
let step = m.confirm_yes(); |
| 1267 |
assert_eq!( |
| 1268 |
step, |
| 1269 |
PromptStep::Fire(FireRequest { |
| 1270 |
source: 0, |
| 1271 |
key: "promote-b".into() |
| 1272 |
}) |
| 1273 |
); |
| 1274 |
} |
| 1275 |
|
| 1276 |
#[test] |
| 1277 |
fn a_danger_action_must_be_typed_out_to_fire() { |
| 1278 |
let mut m = actionable( |
| 1279 |
&["rollback-b"], |
| 1280 |
vec![("rollback-b", act("Roll back", true, true))], |
| 1281 |
); |
| 1282 |
m.open_actions(now()); |
| 1283 |
|
| 1284 |
assert_eq!(m.prompt_enter(), PromptStep::Idle); |
| 1285 |
assert!(matches!(m.prompt, Some(Prompt::Type { .. }))); |
| 1286 |
|
| 1287 |
|
| 1288 |
for c in "rollback-a".chars() { |
| 1289 |
m.prompt_push(c); |
| 1290 |
} |
| 1291 |
assert_eq!(m.prompt_enter(), PromptStep::Idle); |
| 1292 |
assert!(m.message.as_deref().unwrap().contains("exactly")); |
| 1293 |
if let Some(Prompt::Type { typed, .. }) = &m.prompt { |
| 1294 |
assert!(typed.is_empty(), "a mismatch clears what was typed"); |
| 1295 |
} else { |
| 1296 |
panic!("still in Type after a mismatch"); |
| 1297 |
} |
| 1298 |
|
| 1299 |
|
| 1300 |
for c in "rollback-b".chars() { |
| 1301 |
m.prompt_push(c); |
| 1302 |
} |
| 1303 |
m.prompt_backspace(); |
| 1304 |
m.prompt_push('b'); |
| 1305 |
let step = m.prompt_enter(); |
| 1306 |
assert_eq!( |
| 1307 |
step, |
| 1308 |
PromptStep::Fire(FireRequest { |
| 1309 |
source: 0, |
| 1310 |
key: "rollback-b".into() |
| 1311 |
}) |
| 1312 |
); |
| 1313 |
assert!(m.prompt.is_none()); |
| 1314 |
} |
| 1315 |
|
| 1316 |
#[test] |
| 1317 |
fn esc_cancels_without_firing() { |
| 1318 |
let mut m = actionable( |
| 1319 |
&["rollback-b"], |
| 1320 |
vec![("rollback-b", act("Roll back", true, true))], |
| 1321 |
); |
| 1322 |
m.open_actions(now()); |
| 1323 |
assert_eq!(m.cancel_prompt(), PromptStep::Cancelled); |
| 1324 |
assert!(m.prompt.is_none()); |
| 1325 |
assert_eq!( |
| 1326 |
m.cancel_prompt(), |
| 1327 |
PromptStep::Idle, |
| 1328 |
"nothing to cancel twice" |
| 1329 |
); |
| 1330 |
} |
| 1331 |
|
| 1332 |
#[test] |
| 1333 |
fn the_picker_moves_and_digits_jump_within_the_nodes_actions() { |
| 1334 |
let mut m = actionable( |
| 1335 |
&["promote-b", "rollback-b"], |
| 1336 |
vec![ |
| 1337 |
("promote-b", act("Promote", true, false)), |
| 1338 |
("rollback-b", act("Roll back", true, true)), |
| 1339 |
], |
| 1340 |
); |
| 1341 |
m.open_actions(now()); |
| 1342 |
m.prompt_move(1); |
| 1343 |
if let Some(Prompt::Pick { selected, .. }) = &m.prompt { |
| 1344 |
assert_eq!(*selected, 1); |
| 1345 |
} |
| 1346 |
m.prompt_move(5); |
| 1347 |
if let Some(Prompt::Pick { selected, .. }) = &m.prompt { |
| 1348 |
assert_eq!(*selected, 1); |
| 1349 |
} |
| 1350 |
m.prompt_digit(1); |
| 1351 |
if let Some(Prompt::Pick { selected, .. }) = &m.prompt { |
| 1352 |
assert_eq!(*selected, 0); |
| 1353 |
} |
| 1354 |
m.prompt_digit(9); |
| 1355 |
if let Some(Prompt::Pick { selected, .. }) = &m.prompt { |
| 1356 |
assert_eq!(*selected, 0); |
| 1357 |
} |
| 1358 |
} |
| 1359 |
|
| 1360 |
#[test] |
| 1361 |
fn an_action_retracted_between_pick_and_confirm_does_not_fire() { |
| 1362 |
let mut m = actionable( |
| 1363 |
&["rollback-b"], |
| 1364 |
vec![("rollback-b", act("Roll back", true, true))], |
| 1365 |
); |
| 1366 |
m.open_actions(now()); |
| 1367 |
|
| 1368 |
m.sources[0].observe( |
| 1369 |
payload(now(), vec![node("tier:b", Status::Ok, vec![])]), |
| 1370 |
now(), |
| 1371 |
); |
| 1372 |
let step = m.prompt_enter(); |
| 1373 |
assert_eq!(step, PromptStep::Cancelled); |
| 1374 |
assert!(m.message.as_deref().unwrap().contains("no longer offered")); |
| 1375 |
assert!(m.prompt.is_none()); |
| 1376 |
} |
| 1377 |
|
| 1378 |
#[test] |
| 1379 |
fn a_node_needing_attention_is_counted_once() { |
| 1380 |
let mut n = node("tier:b", Status::Failed, vec![]); |
| 1381 |
n.conditions.push(Condition { |
| 1382 |
condition_type: "burn_in".into(), |
| 1383 |
status: Status::Pending, |
| 1384 |
since: None, |
| 1385 |
detail: None, |
| 1386 |
}); |
| 1387 |
let s = source("sando", now(), vec![n, node("ok", Status::Ok, vec![])]); |
| 1388 |
assert_eq!(s.summary(now()), "1 node needs attention"); |
| 1389 |
} |
| 1390 |
} |
| 1391 |
|