use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use crate::flight::Flight;
use crate::pipeline::PipelineName;
#[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)]
pub struct Queued {
pub flight: Flight,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub pipeline: Option<PipelineName>,
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub flags: BTreeMap<String, bool>,
}
impl Queued {
#[must_use]
pub fn new(
flight: Flight,
pipeline: Option<PipelineName>,
flags: BTreeMap<String, bool>,
) -> Self {
Self {
flight,
pipeline,
flags,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::agent::AgentName;
use crate::flight::{ItineraryId, Origin};
fn flight() -> Flight {
Flight::new(
ItineraryId::generate(),
Origin::Human,
AgentName::new("analyst"),
"look at 4821",
8,
)
}
#[test]
fn flags_survive_a_round_trip_through_storage() {
let mut flags = BTreeMap::new();
flags.insert("run_e2e".to_owned(), true);
flags.insert("draft_pr".to_owned(), false);
let queued = Queued::new(flight(), Some(PipelineName::new("development")), flags);
let json = serde_json::to_string(&queued).expect("serialises");
let read: Queued = serde_json::from_str(&json).expect("deserialises");
assert_eq!(read, queued);
assert_eq!(
read.flags.get("run_e2e"),
Some(&true),
"the operator set this and the run must see it"
);
assert_eq!(
read.flags.get("draft_pr"),
Some(&false),
"a flag left at its default is still a decision, and is recorded as one"
);
}
#[test]
fn a_bare_agent_trigger_carries_no_pipeline_and_no_flags() {
let queued = Queued::new(flight(), None, BTreeMap::new());
let json = serde_json::to_string(&queued).expect("serialises");
assert!(!json.contains("pipeline"), "{json}");
assert!(!json.contains("flags"), "{json}");
}
}