use super::*;
use crate::store::{StoreRoute, UpsertExternal};
async fn register(
store: &TaskStore,
node: &str,
projects: &[&str],
agents: &[&str],
seen: DateTime<Utc>,
) {
registry::write(
store,
&Entry {
node: node.into(),
projects: projects.iter().map(ToString::to_string).collect(),
agents: agents.iter().map(ToString::to_string).collect(),
max_parallel: 2,
version: None,
witness: false,
seen: seen.to_rfc3339(),
},
)
.await
.unwrap();
}
async fn running_on_a(store: &TaskStore) -> Task {
let id = store
.upsert_external(&UpsertExternal {
repo: "o/r",
ext_id: "7",
title: "Moved task",
body: "",
author: "user",
url: "",
labels: &[],
origin: "github",
})
.await
.unwrap();
store
.store_route(&StoreRoute {
id,
agent: "claude",
model: Some("m"),
complexity: "medium",
estimate: 0,
reason: "test",
profile: "{}",
})
.await
.unwrap();
store
.update_status_and_fields(
id,
TaskStatus::InProgress,
&[
("owner_node", serde_json::json!("a")),
("owner_term", serde_json::json!(3)),
("lease_until", serde_json::json!("2099-01-01T00:00:00Z")),
("branch", serde_json::json!("gh-issue-7-moved-task")),
],
)
.await
.unwrap();
store.get(id).await.unwrap()
}
async fn cluster() -> (TaskStore, Task) {
let store = TaskStore::open_memory().await.unwrap();
let now = Utc::now();
register(&store, "a", &["o/r"], &["claude"], now).await;
register(&store, "b", &["o/r"], &["claude"], now).await;
register(&store, "c", &["o/other"], &["claude"], now).await;
register(&store, "d", &["o/r"], &["codex"], now).await;
let task = running_on_a(&store).await;
(store, task)
}
#[tokio::test]
async fn a_move_to_a_named_node_sets_the_owner_clears_the_lease_and_routes_the_task() {
let (store, task) = cluster().await;
let moved = move_task(&store, &task, Some("b"), Utc::now())
.await
.unwrap();
assert_eq!(
moved,
Moved {
from: Some("a".into()),
from_online: true,
to: "b".into(),
to_online: true,
branch: "gh-issue-7-moved-task".into(),
}
);
let after = store.get(task.id).await.unwrap();
assert_eq!(after.status, TaskStatus::Routed);
assert_eq!(after.owner_node.as_deref(), Some("b"));
assert_eq!(after.owner_term, None);
assert_eq!(after.lease_until, None);
assert_eq!(after.agent.as_deref(), Some("claude"));
assert_eq!(after.branch, "gh-issue-7-moved-task");
assert_eq!(store.moved_from("a").await.unwrap(), vec![task.id]);
}
#[tokio::test]
async fn a_move_to_a_node_that_cannot_run_the_task_is_refused_and_changes_nothing() {
let (store, task) = cluster().await;
for (node, why) in [
("c", "c does not host o/r"),
("d", "d has no claude agent"),
("zz", "no node named zz"),
("a", "already on a"),
] {
let err = move_task(&store, &task, Some(node), Utc::now())
.await
.unwrap_err()
.to_string();
assert!(err.contains(why), "{node}: {err}");
}
let after = store.get(task.id).await.unwrap();
assert_eq!(after.status, TaskStatus::InProgress);
assert_eq!(after.owner_node.as_deref(), Some("a"));
assert_eq!(after.lease_until, task.lease_until);
assert!(store.moved_from("a").await.unwrap().is_empty());
}
#[tokio::test]
async fn a_move_with_no_node_picks_an_eligible_one_other_than_the_owner() {
let (store, task) = cluster().await;
let moved = move_task(&store, &task, None, Utc::now()).await.unwrap();
assert_eq!(moved.to, "b");
assert_eq!(
store.get(task.id).await.unwrap().owner_node.as_deref(),
Some("b")
);
}
#[tokio::test]
async fn a_move_with_no_node_fails_when_no_other_node_can_run_it() {
let store = TaskStore::open_memory().await.unwrap();
let now = Utc::now();
register(&store, "a", &["o/r"], &["claude"], now).await;
register(&store, "c", &["o/other"], &["claude"], now).await;
register(
&store,
"off",
&["o/r"],
&["claude"],
now - chrono::Duration::hours(1),
)
.await;
let task = running_on_a(&store).await;
let err = move_task(&store, &task, None, now)
.await
.unwrap_err()
.to_string();
assert!(
err.contains("no other online node hosts o/r with claude"),
"{err}"
);
assert_eq!(
store.get(task.id).await.unwrap().status,
TaskStatus::InProgress
);
}
#[tokio::test]
async fn a_done_task_is_refused() {
let (store, task) = cluster().await;
store
.update_status(task.id, TaskStatus::Done)
.await
.unwrap();
let done = store.get(task.id).await.unwrap();
let err = move_task(&store, &done, Some("b"), Utc::now())
.await
.unwrap_err()
.to_string();
assert!(err.contains("is done"), "{err}");
assert_eq!(store.get(task.id).await.unwrap().status, TaskStatus::Done);
assert!(!store.move_to_node(task.id, "b", Some("a")).await.unwrap());
assert_eq!(store.get(task.id).await.unwrap().status, TaskStatus::Done);
}
#[tokio::test]
async fn a_move_to_an_offline_node_says_the_task_waits() {
let (store, task) = cluster().await;
register(
&store,
"b",
&["o/r"],
&["claude"],
Utc::now() - chrono::Duration::hours(1),
)
.await;
let moved = move_task(&store, &task, Some("b"), Utc::now())
.await
.unwrap();
assert!(!moved.to_online);
}