bestool-alertd 6.0.0

(Internal) BES tooling: Alert daemon
Documentation
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
use std::{
	collections::HashMap, io::Write, ops::ControlFlow, path::PathBuf, process::Stdio, sync::Arc,
	time::Duration,
};

use jiff::Timestamp;
use miette::{Context as _, IntoDiagnostic, Result, miette};
use tera::Context as TeraCtx;
use tokio::io::AsyncReadExt as _;
use tokio_postgres::types::ToSql;
use tracing::{debug, error, info, instrument, warn};

use crate::{
	EmailConfig, LogError, events::EventType, targets::ExternalTarget, templates::build_context,
};

fn enabled() -> bool {
	true
}

fn default_interval() -> String {
	"1 minute".to_string()
}

#[derive(serde::Deserialize, Debug, Clone)]
#[serde(rename_all = "kebab-case")]
pub struct NumericalThreshold {
	pub field: String,
	pub alert_at: f64,
	pub clear_at: Option<f64>,
}

#[derive(serde::Deserialize, Debug, Clone)]
#[serde(rename_all = "kebab-case")]
#[serde(untagged)]
pub enum WhenChanged {
	Boolean(bool),
	Detailed(WhenChangedConfig),
}

impl Default for WhenChanged {
	fn default() -> Self {
		WhenChanged::Boolean(false)
	}
}

#[derive(serde::Deserialize, Debug, Clone)]
#[serde(rename_all = "kebab-case")]
pub struct WhenChangedConfig {
	#[serde(default)]
	pub except: Vec<String>,
	#[serde(default)]
	pub only: Vec<String>,
}

#[derive(serde::Deserialize, Debug, Clone)]
#[serde(rename_all = "kebab-case")]
#[serde(untagged)]
pub enum AlwaysSend {
	Boolean(bool),
	Timed(AlwaysSendConfig),
}

impl Default for AlwaysSend {
	fn default() -> Self {
		AlwaysSend::Boolean(false)
	}
}

#[derive(serde::Deserialize, Debug, Clone)]
#[serde(rename_all = "kebab-case")]
pub struct AlwaysSendConfig {
	pub after: String,
	#[serde(skip)]
	pub after_duration: Duration,
}

#[derive(serde::Deserialize, Debug, Default, Clone)]
#[serde(rename_all = "kebab-case")]
pub struct AlertDefinition {
	#[serde(default, skip)]
	pub file: PathBuf,

	#[serde(default = "enabled")]
	pub enabled: bool,

	#[serde(default = "default_interval")]
	pub interval: String,

	#[serde(skip)]
	pub interval_duration: Duration,

	#[serde(default)]
	pub always_send: AlwaysSend,

	#[serde(default)]
	pub when_changed: WhenChanged,

	#[serde(default)]
	pub send: Vec<crate::targets::SendTarget>,

	/// Restrict this alert to a daemon with a particular `server_kind`. The
	/// value is an opaque string; alertd treats `central`, `facility`, or
	/// anything else identically — equality with the daemon's configured
	/// `server_kind` is the only criterion. Absence means "run anywhere",
	/// which is the implicit "all" — there's no explicit `both`/`all` value.
	///
	/// Named `server_kind` (serialised as `server-kind` in YAML) rather than
	/// `target` because alertd already uses `target` for delivery targets
	/// inside the `send:` list (e.g. `send: [{ target: external, ... }]`),
	/// and reusing the word at the top level would be confusing.
	#[serde(default)]
	pub server_kind: Option<String>,

	#[serde(flatten)]
	pub source: TicketSource,
}

/// Whether an alert with this `server_kind` filter should run on a daemon
/// configured with the given `server_kind`.
///
/// - alert `None` ("any") → always runs.
/// - alert `Some(k)`, daemon `None` → permissive: the daemon isn't
///   configured to filter, so we don't drop anything. Better to run too
///   many alerts than to silently swallow targeted ones because someone
///   forgot to set the daemon kind.
/// - alert `Some(k)`, daemon `Some(d)` → runs iff `k == d` (case-sensitive
///   string equality).
pub fn server_kind_matches(alert: Option<&str>, daemon: Option<&str>) -> bool {
	match (alert, daemon) {
		(None, _) | (_, None) => true,
		(Some(a), Some(d)) => a == d,
	}
}

#[derive(serde::Deserialize, Debug, Default, Clone)]
#[serde(untagged, deny_unknown_fields)]
pub enum TicketSource {
	Sql {
		sql: String,
		#[serde(default)]
		numerical: Vec<NumericalThreshold>,
	},
	Shell {
		shell: String,
		run: String,
	},
	Event {
		event: EventType,
	},

	#[default]
	None,
}

impl AlertDefinition {
	pub fn normalise(
		mut self,
		external_targets: &HashMap<String, Vec<ExternalTarget>>,
	) -> Result<(Self, Vec<crate::targets::ResolvedTarget>)> {
		// Parse interval string into duration
		self.interval_duration = parse_interval(&self.interval)
			.wrap_err_with(|| format!("failed to parse interval: {}", self.interval))?;

		// Parse always_send after duration if configured
		if let AlwaysSend::Timed(ref mut config) = self.always_send {
			config.after_duration = parse_interval(&config.after)
				.wrap_err_with(|| format!("failed to parse always-send after: {}", config.after))?;
		}

		// Validate templates before resolving targets
		// This catches template syntax errors early
		for (idx, target) in self.send.iter().enumerate() {
			crate::templates::load_templates(target.subject(), target.template()).wrap_err_with(
				|| {
					format!(
						"validating templates for send target #{} (id: {})",
						idx + 1,
						target.id()
					)
				},
			)?;
		}

		let resolved = self
			.send
			.iter()
			.flat_map(|target| {
				let resolved_targets = target.resolve_external(external_targets);
				if resolved_targets.is_empty() {
					error!(
						file=?self.file,
						id = %target.id(),
						available_targets=?external_targets.keys().collect::<Vec<_>>(),
						"external target not found"
					);
				}
				resolved_targets
			})
			.collect();

		self.send.clear(); // Clear send targets after resolution
		Ok((self, resolved))
	}

	#[instrument(skip(self, pool, not_before, context))]
	pub async fn read_sources(
		&self,
		pool: &bestool_postgres::pool::PgPool,
		not_before: Timestamp,
		context: &mut TeraCtx,
		was_triggered: bool,
	) -> Result<ControlFlow<(), ()>> {
		match &self.source {
			TicketSource::None => {
				debug!(?self.file, "no source, skipping");
				return Ok(ControlFlow::Break(()));
			}
			TicketSource::Event { .. } => {
				// Event sources are triggered externally, not by this method
				debug!(?self.file, "event source, skipping normal execution");
				return Ok(ControlFlow::Break(()));
			}
			TicketSource::Sql { sql, numerical } => {
				let client = pool
					.get()
					.await
					.map_err(|e| miette!("getting connection from pool: {e}"))?;
				let statement = client.prepare(sql).await.into_diagnostic()?;

				let interval = bestool_postgres::pg_interval::Interval(self.interval_duration);
				let all_params: Vec<&(dyn ToSql + Sync)> = vec![&not_before, &interval];

				let rows = client
					.query(&statement, &all_params[..statement.params().len()])
					.await
					.into_diagnostic()
					.wrap_err("querying database")?;

				if rows.is_empty() {
					debug!(?self.file, "no rows returned, skipping");
					return Ok(ControlFlow::Break(()));
				}

				let context_rows = rows_to_value_map(&rows);

				// Check numerical thresholds if configured
				if !numerical.is_empty() {
					let triggered =
						check_numerical_thresholds(&context_rows, numerical, was_triggered)?;
					if !triggered {
						debug!(?self.file, "numerical thresholds not met, skipping");
						return Ok(ControlFlow::Break(()));
					}
				}

				info!(?self.file, rows=%rows.len(), "alert triggered");
				context.insert("rows", &context_rows);
			}
			TicketSource::Shell { shell, run } => {
				let mut script = tempfile::Builder::new().tempfile().into_diagnostic()?;
				write!(script.as_file_mut(), "{run}").into_diagnostic()?;

				let mut shell = tokio::process::Command::new(shell)
					.arg(script.path())
					.stdin(Stdio::null())
					.stdout(Stdio::piped())
					.spawn()
					.into_diagnostic()?;

				let mut output = Vec::new();
				let mut stdout = shell
					.stdout
					.take()
					.ok_or_else(|| miette!("getting the child stdout handle"))?;
				let output_future =
					futures::future::try_join(shell.wait(), stdout.read_to_end(&mut output));

				let Ok(res) = tokio::time::timeout(self.interval_duration, output_future).await
				else {
					warn!(?self.file, "the script timed out, skipping");
					shell.kill().await.into_diagnostic()?;
					return Ok(ControlFlow::Break(()));
				};

				let (status, output_size) = res.into_diagnostic().wrap_err("running the shell")?;

				if status.success() {
					debug!(?self.file, "the script succeeded, skipping");
					return Ok(ControlFlow::Break(()));
				}
				info!(?self.file, ?status, ?output_size, "alert triggered");

				context.insert("output", &String::from_utf8_lossy(&output));
			}
		}
		Ok(ControlFlow::Continue(()))
	}

	pub async fn execute(
		&self,
		ctx: Arc<InternalContext>,
		email: Option<&EmailConfig>,
		dry_run: bool,
		resolved_targets: &[crate::targets::ResolvedTarget],
	) -> Result<()> {
		info!(?self.file, "executing alert");

		let now = Timestamp::now();
		let not_before = now - self.interval_duration;
		info!(?now, ?not_before, interval=?self.interval_duration, "date range for alert");

		let mut tera_ctx = build_context(self, now);
		if self
			.read_sources(&ctx.pg_pool, not_before, &mut tera_ctx, false)
			.await?
			.is_break()
		{
			return Ok(());
		}

		for target in resolved_targets {
			if let Err(err) = target.send(self, &mut tera_ctx, email, &ctx, dry_run).await {
				error!("sending: {}", LogError(&err));
			}
		}

		Ok(())
	}
}

#[derive(Debug, Clone)]
pub struct InternalContext {
	pub pg_pool: bestool_postgres::pool::PgPool,
	pub http_client: reqwest::Client,
	pub canopy_client: Option<Arc<crate::canopy::CanopyClient>>,
}

fn rows_to_value_map(
	rows: &[tokio_postgres::Row],
) -> Vec<serde_json::Map<String, serde_json::Value>> {
	rows.iter()
		.map(|row| {
			let mut map = serde_json::Map::new();
			for (idx, column) in row.columns().iter().enumerate() {
				let value = bestool_postgres::stringify::postgres_to_json_value(row, idx);
				map.insert(column.name().to_string(), value);
			}
			map
		})
		.collect()
}

fn check_numerical_thresholds(
	rows: &[serde_json::Map<String, serde_json::Value>],
	thresholds: &[NumericalThreshold],
	was_triggered: bool,
) -> Result<bool> {
	for threshold in thresholds {
		for row in rows {
			let value = match row.get(&threshold.field) {
				Some(serde_json::Value::Number(n)) => n
					.as_f64()
					.ok_or_else(|| miette!("field '{}' is not a valid number", threshold.field))?,
				Some(_) => {
					return Err(miette!(
						"field '{}' exists but is not a number",
						threshold.field
					));
				}
				None => {
					return Err(miette!(
						"field '{}' not found in query results",
						threshold.field
					));
				}
			};

			// Determine if we're checking for "above" or "below" based on clear_at
			let is_inverted = threshold
				.clear_at
				.is_some_and(|clear| clear > threshold.alert_at);

			if was_triggered {
				// Already triggered, check if we should clear
				if let Some(clear_at) = threshold.clear_at {
					let should_clear = if is_inverted {
						// Inverted: clear when value >= clear_at
						value >= clear_at
					} else {
						// Normal: clear when value <= clear_at
						value <= clear_at
					};

					if should_clear {
						// This threshold has cleared, continue checking others
						continue;
					} else {
						// Still above/below clear threshold, remain triggered
						return Ok(true);
					}
				} else {
					// No clear_at specified, check alert_at threshold
					let still_triggered = if is_inverted {
						value <= threshold.alert_at
					} else {
						value >= threshold.alert_at
					};

					if still_triggered {
						return Ok(true);
					}
				}
			} else {
				// Not yet triggered, check if we should trigger
				let should_trigger = if is_inverted {
					// Inverted: trigger when value <= alert_at
					value <= threshold.alert_at
				} else {
					// Normal: trigger when value >= alert_at
					value >= threshold.alert_at
				};

				if should_trigger {
					return Ok(true);
				}
			}
		}
	}

	Ok(false)
}

fn parse_interval(s: &str) -> Result<Duration> {
	let s = s.trim();

	// Try to parse as a simple number (seconds)
	if let Ok(secs) = s.parse::<u64>() {
		return Ok(Duration::from_secs(secs));
	}

	// Parse with units
	let parts: Vec<&str> = s.split_whitespace().collect();
	if parts.len() != 2 {
		return Err(miette!(
			"interval must be in format '<number> <unit>' or just '<seconds>'"
		));
	}

	let value: u64 = parts[0]
		.parse()
		.into_diagnostic()
		.wrap_err("interval value must be a number")?;
	let unit = parts[1].to_lowercase();

	let duration = match unit.as_str() {
		"second" | "seconds" | "s" | "sec" | "secs" => Duration::from_secs(value),
		"minute" | "minutes" | "m" | "min" | "mins" => Duration::from_secs(value * 60),
		"hour" | "hours" | "h" | "hr" | "hrs" => Duration::from_secs(value * 3600),
		"day" | "days" | "d" => Duration::from_secs(value * 86400),
		_ => {
			return Err(miette!(
				"unknown interval unit: {}, expected: seconds, minutes, hours, or days",
				unit
			));
		}
	};

	Ok(duration)
}

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

	#[test]
	fn test_alert_with_event_source() {
		let yaml = r#"
event: source-error
send:
  - id: test-target
    subject: Test
    template: Test template
"#;
		let alert: AlertDefinition = serde_yaml::from_str(yaml).unwrap();
		assert!(matches!(alert.source, TicketSource::Event { .. }));
		if let TicketSource::Event { event } = alert.source {
			assert_eq!(event, EventType::SourceError);
		}
	}

	#[test]
	fn test_parse_interval() {
		assert_eq!(parse_interval("60").unwrap(), Duration::from_secs(60));
		assert_eq!(parse_interval("1 minute").unwrap(), Duration::from_secs(60));
		assert_eq!(
			parse_interval("5 minutes").unwrap(),
			Duration::from_secs(300)
		);
		assert_eq!(
			parse_interval("2 hours").unwrap(),
			Duration::from_secs(7200)
		);
		assert_eq!(parse_interval("1 day").unwrap(), Duration::from_secs(86400));
		assert_eq!(
			parse_interval("30 seconds").unwrap(),
			Duration::from_secs(30)
		);
	}

	#[test]
	fn test_default_interval() {
		let yaml = r#"
sql: "SELECT 1"
send:
  - id: test
    subject: Test
    template: Test
"#;
		let alert: AlertDefinition = serde_yaml::from_str(yaml).unwrap();
		assert_eq!(alert.interval, "1 minute");
	}

	#[test]
	fn test_default_always_send() {
		let yaml = r#"
sql: "SELECT 1"
send:
  - id: test
    subject: Test
    template: Test
"#;
		let alert: AlertDefinition = serde_yaml::from_str(yaml).unwrap();
		assert!(matches!(alert.always_send, AlwaysSend::Boolean(false)));
	}

	#[test]
	fn test_always_send_true() {
		let yaml = r#"
always-send: true
sql: "SELECT 1"
send:
  - id: test
    subject: Test
    template: Test
"#;
		let alert: AlertDefinition = serde_yaml::from_str(yaml).unwrap();
		assert!(matches!(alert.always_send, AlwaysSend::Boolean(true)));
	}

	#[test]
	fn test_always_send_timed() {
		let yaml = r#"
always-send:
  after: 8h
sql: "SELECT 1"
send:
  - id: test
    subject: Test
    template: Test
"#;
		let alert: AlertDefinition = serde_yaml::from_str(yaml).unwrap();
		match alert.always_send {
			AlwaysSend::Timed(config) => {
				assert_eq!(config.after, "8h");
			}
			_ => panic!("Expected AlwaysSend::Timed"),
		}
	}

	#[test]
	fn test_always_send_timed_normalised() {
		let yaml = r#"
always-send:
  after: 8 hours
sql: "SELECT 1"
send:
  - id: test
    subject: Test
    template: Test
"#;
		let alert: AlertDefinition = serde_yaml::from_str(yaml).unwrap();
		let external_targets = std::collections::HashMap::new();
		let (normalised, _) = alert.normalise(&external_targets).unwrap();

		match normalised.always_send {
			AlwaysSend::Timed(config) => {
				assert_eq!(config.after, "8 hours");
				assert_eq!(
					config.after_duration,
					std::time::Duration::from_secs(8 * 3600)
				);
			}
			_ => panic!("Expected AlwaysSend::Timed"),
		}
	}

	#[test]
	fn test_numerical_threshold_normal() {
		let yaml = r#"
sql: "SELECT 1"
numerical:
  - field: count
    alert-at: 100
    clear-at: 50
send:
  - id: test
    subject: Test
    template: Test
"#;
		let alert: AlertDefinition = serde_yaml::from_str(yaml).unwrap();
		if let TicketSource::Sql { numerical, .. } = &alert.source {
			assert_eq!(numerical.len(), 1);
			assert_eq!(numerical[0].field, "count");
			assert_eq!(numerical[0].alert_at, 100.0);
			assert_eq!(numerical[0].clear_at, Some(50.0));
		} else {
			panic!("Expected Sql source");
		}
	}

	#[test]
	fn test_numerical_threshold_inverted() {
		let yaml = r#"
sql: "SELECT 1"
numerical:
  - field: free_space
    alert-at: 10
    clear-at: 50
send:
  - id: test
    subject: Test
    template: Test
"#;
		let alert: AlertDefinition = serde_yaml::from_str(yaml).unwrap();
		if let TicketSource::Sql { numerical, .. } = &alert.source {
			assert_eq!(numerical.len(), 1);
			assert_eq!(numerical[0].field, "free_space");
			assert_eq!(numerical[0].alert_at, 10.0);
			assert_eq!(numerical[0].clear_at, Some(50.0));
		} else {
			panic!("Expected Sql source");
		}
	}

	#[test]
	fn test_numerical_threshold_no_clear() {
		let yaml = r#"
sql: "SELECT 1"
numerical:
  - field: errors
    alert-at: 5
send:
  - id: test
    subject: Test
    template: Test
"#;
		let alert: AlertDefinition = serde_yaml::from_str(yaml).unwrap();
		if let TicketSource::Sql { numerical, .. } = &alert.source {
			assert_eq!(numerical.len(), 1);
			assert_eq!(numerical[0].field, "errors");
			assert_eq!(numerical[0].alert_at, 5.0);
			assert_eq!(numerical[0].clear_at, None);
		} else {
			panic!("Expected Sql source");
		}
	}

	#[test]
	fn test_check_numerical_thresholds_normal_trigger() {
		let mut row = serde_json::Map::new();
		row.insert("count".to_string(), serde_json::Value::Number(150.into()));
		let rows = vec![row];

		let threshold = NumericalThreshold {
			field: "count".to_string(),
			alert_at: 100.0,
			clear_at: Some(50.0),
		};

		// Not triggered yet, value 150 >= alert_at 100, should trigger
		let result =
			check_numerical_thresholds(&rows, std::slice::from_ref(&threshold), false).unwrap();
		assert!(result);

		// Already triggered, value 150 > clear_at 50, should stay triggered
		let result =
			check_numerical_thresholds(&rows, std::slice::from_ref(&threshold), true).unwrap();
		assert!(result);

		// Already triggered, value 30 <= clear_at 50, should clear
		let mut row = serde_json::Map::new();
		row.insert("count".to_string(), serde_json::Value::Number(30.into()));
		let rows = vec![row];
		let result = check_numerical_thresholds(&rows, &[threshold], true).unwrap();
		assert!(!result);
	}

	#[test]
	fn test_check_numerical_thresholds_inverted_trigger() {
		let mut row = serde_json::Map::new();
		row.insert(
			"free_space".to_string(),
			serde_json::Value::Number(5.into()),
		);
		let rows = vec![row];

		let threshold = NumericalThreshold {
			field: "free_space".to_string(),
			alert_at: 10.0,
			clear_at: Some(50.0), // Inverted because clear_at > alert_at
		};

		// Not triggered yet, value 5 <= alert_at 10, should trigger (inverted)
		let result =
			check_numerical_thresholds(&rows, std::slice::from_ref(&threshold), false).unwrap();
		assert!(result);

		// Already triggered, value 5 < clear_at 50, should stay triggered
		let result =
			check_numerical_thresholds(&rows, std::slice::from_ref(&threshold), true).unwrap();
		assert!(result);

		// Already triggered, value 60 >= clear_at 50, should clear
		let mut row = serde_json::Map::new();
		row.insert(
			"free_space".to_string(),
			serde_json::Value::Number(60.into()),
		);
		let rows = vec![row];
		let result = check_numerical_thresholds(&rows, &[threshold], true).unwrap();
		assert!(!result);
	}

	#[test]
	fn test_check_numerical_thresholds_no_clear_at() {
		let threshold = NumericalThreshold {
			field: "errors".to_string(),
			alert_at: 5.0,
			clear_at: None,
		};

		// Trigger when >= 5
		let mut row = serde_json::Map::new();
		row.insert("errors".to_string(), serde_json::Value::Number(10.into()));
		let rows = vec![row];
		let result =
			check_numerical_thresholds(&rows, std::slice::from_ref(&threshold), false).unwrap();
		assert!(result);

		// Still triggered when >= 5
		let result =
			check_numerical_thresholds(&rows, std::slice::from_ref(&threshold), true).unwrap();
		assert!(result);

		// Clear when < 5
		let mut row = serde_json::Map::new();
		row.insert("errors".to_string(), serde_json::Value::Number(3.into()));
		let rows = vec![row];
		let result = check_numerical_thresholds(&rows, &[threshold], true).unwrap();
		assert!(!result);
	}

	#[test]
	fn test_when_changed_boolean_true() {
		let yaml = r#"
sql: "SELECT 1"
when-changed: true
send:
  - id: test
    subject: Test
    template: Test
"#;
		let alert: AlertDefinition = serde_yaml::from_str(yaml).unwrap();
		assert!(matches!(alert.when_changed, WhenChanged::Boolean(true)));
	}

	#[test]
	fn test_when_changed_boolean_false() {
		let yaml = r#"
sql: "SELECT 1"
when-changed: false
send:
  - id: test
    subject: Test
    template: Test
"#;
		let alert: AlertDefinition = serde_yaml::from_str(yaml).unwrap();
		assert!(matches!(alert.when_changed, WhenChanged::Boolean(false)));
	}

	#[test]
	fn test_when_changed_default() {
		let yaml = r#"
sql: "SELECT 1"
send:
  - id: test
    subject: Test
    template: Test
"#;
		let alert: AlertDefinition = serde_yaml::from_str(yaml).unwrap();
		assert!(matches!(alert.when_changed, WhenChanged::Boolean(false)));
	}

	#[test]
	fn test_when_changed_except() {
		let yaml = r#"
sql: "SELECT 1"
when-changed:
  except: [created_at, updated_at]
send:
  - id: test
    subject: Test
    template: Test
"#;
		let alert: AlertDefinition = serde_yaml::from_str(yaml).unwrap();
		match &alert.when_changed {
			WhenChanged::Detailed(config) => {
				assert_eq!(config.except, vec!["created_at", "updated_at"]);
				assert!(config.only.is_empty());
			}
			_ => panic!("Expected Detailed variant"),
		}
	}

	#[test]
	fn test_when_changed_only() {
		let yaml = r#"
sql: "SELECT 1"
when-changed:
  only: [error, message]
send:
  - id: test
    subject: Test
    template: Test
"#;
		let alert: AlertDefinition = serde_yaml::from_str(yaml).unwrap();
		match &alert.when_changed {
			WhenChanged::Detailed(config) => {
				assert_eq!(config.only, vec!["error", "message"]);
				assert!(config.except.is_empty());
			}
			_ => panic!("Expected Detailed variant"),
		}
	}

	#[test]
	fn server_kind_defaults_to_none_when_absent() {
		let yaml = "sql: \"SELECT 1\"\nsend:\n  - id: x\n    subject: s\n    template: t\n";
		let alert: AlertDefinition = serde_yaml::from_str(yaml).unwrap();
		assert_eq!(alert.server_kind, None);
	}

	#[test]
	fn server_kind_parses_as_arbitrary_string() {
		// alertd doesn't enforce a vocabulary — `central` / `facility` are
		// just what bestool-tamanu happens to pass through. Whatever string
		// the daemon's `server_kind` is configured with is what alerts match
		// against.
		for text in ["central", "facility", "kiosk", "edge"] {
			let yaml = format!(
				"sql: \"SELECT 1\"\nserver-kind: {text}\nsend:\n  - id: x\n    subject: s\n    template: t\n"
			);
			let alert: AlertDefinition = serde_yaml::from_str(&yaml).unwrap();
			assert_eq!(alert.server_kind.as_deref(), Some(text));
		}
	}

	#[test]
	fn server_kind_matches_truth_table() {
		// Absent on the alert → always runs.
		assert!(server_kind_matches(None, Some("central")));
		assert!(server_kind_matches(None, Some("facility")));
		assert!(server_kind_matches(None, None));

		// Absent on the daemon → permissive: every alert applies.
		assert!(server_kind_matches(Some("central"), None));
		assert!(server_kind_matches(Some("facility"), None));

		// Both present → equality.
		assert!(server_kind_matches(Some("central"), Some("central")));
		assert!(!server_kind_matches(Some("central"), Some("facility")));
		assert!(!server_kind_matches(Some("facility"), Some("central")));

		// Match is case-sensitive.
		assert!(!server_kind_matches(Some("Central"), Some("central")));
	}
}