1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
use super::super::*;
impl App {
/// One `Request::SetSheepField` reply.
///
/// Reaches for no pane: the write went out on the keypress that closed
/// it, so this lands with the dashboard on screen. A success still
/// re-reads the config, using `Effect::Send` so the re-read gets
/// `Msg::Unsent` handling for free, and [`Self::on_sheep_config`]
/// drops the answer when nobody is waiting for it.
///
/// Every sentence names the field, refusals included. A close sends
/// the whole set at once, so several of these can land in a row and a
/// refusal that named only the sheep would not say which write failed.
///
/// The success sentence also names the new value when
/// [`FieldValue::safe_summary`] says that is safe: without it, setting
/// `reuse_port` to `true` and setting it back to `false` print the
/// identical sentence, which is wrong on its own rather than merely
/// incomplete.
///
/// `pending` is the shepherd's answer, not this pane's guess: it knows
/// about fields like `autostart` that `apply_group` cannot derive.
///
/// `warning` rides the same sentence rather than a second notice: the
/// write still landed, so this is one more clause about it, on the same
/// terms `pending`'s own `", and waits for..."` clause already sets.
pub(in crate::lookout::app) fn on_field_applied(
&mut self,
name: &str,
key: &str,
value: &FieldValue,
result: Result<Response, RequestError>,
) -> Effect {
match result {
Ok(Response::SheepFieldSet {
pending, warning, ..
}) => {
let key_text = match value.safe_summary() {
Some(v) => format!("{key} set to {v}"),
None => format!("{key} is set"),
};
let mut text = if pending {
format!("{name}: {key_text}, and waits for `shep reload {name}`")
} else {
format!("{name}: {key_text}")
};
if let Some(warning) = warning {
text = format!("{text}; {warning}");
}
self.notice = Some(Notice::info(text));
Effect::Send(Sent::SheepConfig {
name: name.to_owned(),
})
}
other => {
self.notice = Some(Notice::grave(format!(
"{name}: {key}: {}",
failed_reply(other)
)));
Effect::None
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::lookout::app::testing::*;
/// A reply for a write from an earlier pane can land while a new one
/// is open and being typed into. On the env screen the discarded
/// buffer is a secret the operator cannot read back.
#[test]
fn a_reply_landing_mid_edit_leaves_the_buffer_and_the_keyboard_alone() {
let mut app = fixtures::app_in_sheep_pane_with_control();
pane_to(&mut app, "autorestart");
let _ = app.update(Msg::Key(KeyPress::Cycle));
let mut batch = wire_batch(close_writing(&mut app));
let sent = batch.remove(0);
// The pane is reopened and the operator starts typing while the
// first write is still out.
let _ = app.update(Msg::Key(KeyPress::Escape));
let mut app = fixtures::app_in_sheep_pane_with_control();
pane_to(&mut app, "cwd");
let _ = app.update(Msg::Key(KeyPress::Confirm));
for typed in "/srv".chars() {
let _ = app.update(Msg::Key(KeyPress::TextChar(typed)));
}
assert_eq!(app.mode(), InputMode::Text);
let _ = app.update(Msg::Unsent { sent });
assert_eq!(app.mode(), InputMode::Text, "the keyboard is not stranded");
let typing = app
.config_pane()
.unwrap()
.typing()
.expect("the buffer survives the reply");
assert_eq!(typing.key, "cwd");
assert!(
typing.buffer.as_str().ends_with("/srv"),
"{:?}",
typing.buffer
);
}
/// A landed write asks for a re-read, and the re-read rebuilds the
/// whole `ConfigPane`, editor included.
#[test]
fn a_refresh_that_drops_an_open_editor_puts_the_keyboard_back() {
let mut app = fixtures::app_in_sheep_pane_with_control();
pane_to(&mut app, "cwd");
let _ = app.update(Msg::Key(KeyPress::Confirm));
assert_eq!(app.mode(), InputMode::Text);
refresh_config(&mut app, &["DB_HOST", "LOG_LEVEL"]);
assert_eq!(app.mode(), InputMode::Normal);
assert!(app.config_pane().unwrap().typing().is_none());
}
/// Validation runs on entry, so the set is always sendable. An integer
/// field mid-word holds the editor open rather than filing a bad value,
/// which is what `apply_typing` already does today.
#[test]
fn a_value_that_does_not_parse_never_joins_the_set() {
let mut app = fixtures::app_in_sheep_pane();
app.set_control_for_tests(Control::Allowed);
fixtures::select_field(&mut app, "max_restarts");
app.update(Msg::Key(KeyPress::Confirm));
for typed in "not a number".chars() {
app.update(Msg::Key(KeyPress::TextChar(typed)));
}
app.update(Msg::Key(KeyPress::TextApply));
assert!(app.config_pane().unwrap().edits().is_empty());
assert_eq!(app.mode(), InputMode::Text, "the editor stays open");
}
/// Nothing is armed, so nothing eats a keystroke: the next key does
/// its own job and the filed edit stays filed.
#[test]
fn a_key_after_an_edit_does_its_own_job_and_keeps_the_edit() {
let mut app = fixtures::app_in_sheep_pane_with_control();
pane_to(&mut app, "autorestart");
let before = app.config_pane().unwrap().view().cursor();
let _ = app.update(Msg::Key(KeyPress::Cycle));
assert_eq!(app.config_pane().unwrap().edits().len(), 1);
assert_eq!(app.update(Msg::Key(KeyPress::SelectDown)), Effect::None);
assert_eq!(
app.config_pane().unwrap().view().cursor(),
before + 1,
"the movement key moves"
);
assert_eq!(
app.config_pane().unwrap().edits().len(),
1,
"and does not cancel the edit"
);
}
/// A filed edit is not a question waiting for an answer, so no timer
/// takes it away: an operator who walked off must not come back to
/// work silently discarded.
#[test]
fn a_filed_edit_outlives_the_confirm_budget() {
let mut app = fixtures::app_in_sheep_pane_with_control();
pane_to(&mut app, "autorestart");
let _ = app.update(Msg::Key(KeyPress::Cycle));
let _ = app.update(Msg::Tick {
now: Instant::now() + CONFIRM_EXPIRY,
});
assert_eq!(app.config_pane().unwrap().edits().len(), 1);
}
/// The pending set is the operator's and the values are the
/// shepherd's: a re-read replaces one and keeps the other.
#[test]
fn a_refresh_replaces_the_values_and_keeps_the_edits() {
let mut app = fixtures::app_in_sheep_pane_with_control();
pane_to(&mut app, "autorestart");
let _ = app.update(Msg::Key(KeyPress::Cycle));
refresh_config(&mut app, &[]);
assert_eq!(
app.config_pane().unwrap().edits().len(),
1,
"the edit survives the rebuild"
);
let request = one_wire(close_writing(&mut app));
let Request::SetSheepField { key, .. } = request else {
panic!("{request:?}");
};
assert_eq!(key, "autorestart");
}
}