use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use crate::connectors::BackendParams;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Harness {
Tm,
Tcode,
}
impl From<&BackendParams> for Harness {
fn from(params: &BackendParams) -> Self {
match params {
BackendParams::Tm { .. } => Harness::Tm,
BackendParams::Tcode { .. } => Harness::Tcode,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum WorkstreamStatus {
Proposed,
InProgress,
Blocked,
Complete,
Delivered,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum Priority {
P0,
P1,
P2,
P3,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Workstream {
pub id: String,
pub ticket_ref: String,
pub title: String,
pub assigned_harness: Harness,
pub session_ids: Vec<String>,
pub status: WorkstreamStatus,
pub created_at: DateTime<Utc>,
pub target_date: Option<DateTime<Utc>>,
pub priority: Priority,
pub blockers: Vec<String>,
pub dependencies: Vec<String>,
pub last_updated: DateTime<Utc>,
}
#[derive(Debug, Clone)]
pub struct NewWorkstream {
pub ticket_ref: String,
pub title: String,
pub assigned_harness: Harness,
pub priority: Priority,
pub target_date: Option<DateTime<Utc>>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn harness_from_backend_params_tm_and_tcode() {
let tm = BackendParams::Tm {
repo_url: "https://example/repo".into(),
git_ref: "main".into(),
runtime: None,
ephemeral: false,
};
let tcode = BackendParams::Tcode {
project: std::path::PathBuf::from("/tmp/proj"),
};
assert_eq!(Harness::from(&tm), Harness::Tm);
assert_eq!(Harness::from(&tcode), Harness::Tcode);
}
#[test]
fn harness_serde_round_trip() {
for h in [Harness::Tm, Harness::Tcode] {
let json = serde_json::to_string(&h).unwrap();
let back: Harness = serde_json::from_str(&json).unwrap();
assert_eq!(h, back);
}
assert_eq!(serde_json::to_string(&Harness::Tm).unwrap(), "\"tm\"");
assert_eq!(serde_json::to_string(&Harness::Tcode).unwrap(), "\"tcode\"");
}
#[test]
fn workstream_status_serde_round_trip() {
let statuses = [
WorkstreamStatus::Proposed,
WorkstreamStatus::InProgress,
WorkstreamStatus::Blocked,
WorkstreamStatus::Complete,
WorkstreamStatus::Delivered,
];
for s in statuses {
let json = serde_json::to_string(&s).unwrap();
let back: WorkstreamStatus = serde_json::from_str(&json).unwrap();
assert_eq!(s, back);
}
assert_eq!(
serde_json::to_string(&WorkstreamStatus::InProgress).unwrap(),
"\"in_progress\""
);
}
#[test]
fn priority_orders_p0_highest() {
assert!(Priority::P0 < Priority::P1);
assert!(Priority::P1 < Priority::P2);
assert!(Priority::P2 < Priority::P3);
}
#[test]
fn workstream_serde_round_trip() {
let now = Utc::now();
let ws = Workstream {
id: "ws-1".into(),
ticket_ref: "#3008".into(),
title: "heterogeneous ledger".into(),
assigned_harness: Harness::Tm,
session_ids: vec!["sess-1".into()],
status: WorkstreamStatus::InProgress,
created_at: now,
target_date: None,
priority: Priority::P1,
blockers: vec![],
dependencies: vec![],
last_updated: now,
};
let json = serde_json::to_string(&ws).unwrap();
let back: Workstream = serde_json::from_str(&json).unwrap();
assert_eq!(ws, back);
}
}