use kanban_service::{open_context, AppConfig, KanbanOperations, KanbanResult};
use tempfile::tempdir;
#[tokio::test(flavor = "multi_thread")]
async fn test_open_context_json_end_to_end() -> KanbanResult<()> {
let dir = tempdir().unwrap();
let path = dir.path().join("board.json");
{
let mut ctx = open_context(path.to_str().unwrap(), AppConfig::default()).await?;
ctx.create_board("Board1".into(), None)?;
ctx.save().await?;
}
let ctx = open_context(path.to_str().unwrap(), AppConfig::default()).await?;
let boards = ctx.boards()?;
assert_eq!(boards.len(), 1);
assert_eq!(boards[0].name, "Board1");
Ok(())
}
#[cfg(feature = "sqlite")]
mod sqlite_tests {
use super::*;
use kanban_persistence_sqlite::SqliteStore;
#[tokio::test(flavor = "multi_thread")]
async fn test_open_context_sqlite_end_to_end() -> KanbanResult<()> {
let dir = tempdir().unwrap();
let path = dir.path().join("board.sqlite");
{
let mut ctx = open_context(path.to_str().unwrap(), AppConfig::default()).await?;
ctx.create_board("Board1".into(), None)?;
}
let ctx = open_context(path.to_str().unwrap(), AppConfig::default()).await?;
let boards = ctx.boards()?;
assert_eq!(boards.len(), 1);
assert_eq!(boards[0].name, "Board1");
Ok(())
}
#[tokio::test(flavor = "multi_thread")]
async fn test_open_context_auto_detects_backend_from_magic_bytes() -> KanbanResult<()> {
let dir = tempdir().unwrap();
let path = dir.path().join("noext");
SqliteStore::open(path.to_str().unwrap()).await.unwrap();
let mut ctx = open_context(path.to_str().unwrap(), AppConfig::default()).await?;
ctx.create_board("B".into(), None)?;
let boards = ctx.boards()?;
assert_eq!(boards.len(), 1);
assert_eq!(boards[0].name, "B");
Ok(())
}
}
#[tokio::test(flavor = "multi_thread")]
async fn test_open_deferred_context_executes_immediately() {
use kanban_domain::commands::{BoardCommand, Command, CreateBoard};
use kanban_domain::InMemoryStore;
use std::sync::Arc;
let mut ctx = kanban_service::KanbanContext::open_deferred(
Arc::new(InMemoryStore::new()),
kanban_service::AppConfig::default(),
);
let cmd = Command::Board(BoardCommand::Create(CreateBoard {
id: uuid::Uuid::new_v4(),
name: "Test".into(),
card_prefix: None,
position: 0,
}));
ctx.execute(vec![cmd]).expect("execute should succeed");
assert_eq!(ctx.boards().unwrap().len(), 1);
}
#[tokio::test(flavor = "multi_thread")]
async fn test_open_context_new_file_starts_empty() -> KanbanResult<()> {
let dir = tempdir().unwrap();
let path = dir.path().join("new.json");
let ctx = open_context(path.to_str().unwrap(), AppConfig::default()).await?;
assert!(ctx.boards()?.is_empty());
Ok(())
}