use serde::{Deserialize, Serialize};
use crate::types::{CollectiveId, Timestamp};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Collective {
pub id: CollectiveId,
pub name: String,
pub owner_id: Option<String>,
pub embedding_dimension: u16,
pub created_at: Timestamp,
pub updated_at: Timestamp,
}
impl Collective {
pub fn new(name: impl Into<String>, embedding_dimension: u16) -> Self {
let now = Timestamp::now();
Self {
id: CollectiveId::new(),
name: name.into(),
owner_id: None,
embedding_dimension,
created_at: now,
updated_at: now,
}
}
pub fn with_owner(
name: impl Into<String>,
owner_id: impl Into<String>,
embedding_dimension: u16,
) -> Self {
let mut collective = Self::new(name, embedding_dimension);
collective.owner_id = Some(owner_id.into());
collective
}
}
#[derive(Clone, Debug)]
pub struct CollectiveStats {
pub experience_count: u64,
pub storage_bytes: u64,
pub oldest_experience: Option<Timestamp>,
pub newest_experience: Option<Timestamp>,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_collective_new() {
let collective = Collective::new("test-project", 384);
assert_eq!(collective.name, "test-project");
assert_eq!(collective.embedding_dimension, 384);
assert!(collective.owner_id.is_none());
assert!(collective.created_at == collective.updated_at);
}
#[test]
fn test_collective_with_owner() {
let collective = Collective::with_owner("test-project", "user-1", 768);
assert_eq!(collective.name, "test-project");
assert_eq!(collective.owner_id.as_deref(), Some("user-1"));
assert_eq!(collective.embedding_dimension, 768);
}
#[test]
fn test_collective_postcard_roundtrip() {
let collective = Collective::new("roundtrip-test", 384);
let bytes = postcard::to_stdvec(&collective).unwrap();
let restored: Collective = postcard::from_bytes(&bytes).unwrap();
assert_eq!(collective.id, restored.id);
assert_eq!(collective.name, restored.name);
assert_eq!(collective.owner_id, restored.owner_id);
assert_eq!(collective.embedding_dimension, restored.embedding_dimension);
assert_eq!(collective.created_at, restored.created_at);
assert_eq!(collective.updated_at, restored.updated_at);
}
#[test]
fn test_collective_postcard_roundtrip_with_owner() {
let collective = Collective::with_owner("owned-project", "tenant-42", 768);
let bytes = postcard::to_stdvec(&collective).unwrap();
let restored: Collective = postcard::from_bytes(&bytes).unwrap();
assert_eq!(collective.id, restored.id);
assert_eq!(collective.owner_id, restored.owner_id);
}
}