- app_connections repo; POST /api/apps/connect (keys/basic): the credential goes to the secret broker over its socket and only the encrypted ref lands in the row; disconnect endpoint; /api/apps directory merged with live connection status; audit rows for connect/disconnect - Broker protocol: InvokeHttp carries a JSON body - slack.post tool (SendsExternally -> gated): marked broker_executed — the runtime skips its own grant consumption and the BROKER independently verifies + consumes the single-use grant, then calls Slack with the bot token injected; the runtime never sees the credential - Config: [broker] socket_path + [slack] base_url; e2e harness spawns the real teamclaw-broker daemon and the server hosts an e2e-only /__slack sink - SlackApp: Connection tab stores the token via the broker; connected state - Integration test: blocked while pending -> approved -> sink received exactly one post with 'Bearer xoxb-test-token' -> grant replay refused - E2E journey: connect Slack in the panel -> gated post card with preview -> sink empty while pending -> approve -> exactly one post, queue clear 133 Rust + 63 frontend tests + 21 Playwright journeys. Co-Authored-By: Claude Fable 5 <[email protected]>
199 lines
6.1 KiB
Rust
199 lines
6.1 KiB
Rust
use std::sync::Arc;
|
|
use std::time::Duration;
|
|
|
|
use serde_json::json;
|
|
use tc_domain::{
|
|
AccessPolicy, Agent, AgentId, AgentStatus, MessageRole, Role, User, UserId, Workspace,
|
|
WorkspaceId,
|
|
};
|
|
use tc_llm::ScriptedProvider;
|
|
use tc_runtime::{Runtime, RuntimeConfig};
|
|
use tc_scheduler::{next_occurrence, Scheduler};
|
|
use time::macros::datetime;
|
|
|
|
#[test]
|
|
fn next_occurrence_follows_the_cron_pattern() {
|
|
let after = datetime!(2026-06-10 08:30:00 UTC);
|
|
// Daily at 09:00.
|
|
assert_eq!(
|
|
next_occurrence("0 9 * * *", after).unwrap(),
|
|
datetime!(2026-06-10 09:00:00 UTC)
|
|
);
|
|
// Already past 09:00 today → tomorrow.
|
|
let late = datetime!(2026-06-10 09:30:00 UTC);
|
|
assert_eq!(
|
|
next_occurrence("0 9 * * *", late).unwrap(),
|
|
datetime!(2026-06-11 09:00:00 UTC)
|
|
);
|
|
// Every minute.
|
|
assert_eq!(
|
|
next_occurrence("* * * * *", after).unwrap(),
|
|
datetime!(2026-06-10 08:31:00 UTC)
|
|
);
|
|
// Mondays only (2026-06-10 is a Wednesday).
|
|
assert_eq!(
|
|
next_occurrence("0 9 * * MON", after).unwrap(),
|
|
datetime!(2026-06-15 09:00:00 UTC)
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_cron_patterns_are_errors() {
|
|
let after = datetime!(2026-06-10 08:30:00 UTC);
|
|
assert!(next_occurrence("not a cron", after).is_err());
|
|
assert!(next_occurrence("99 99 * * *", after).is_err());
|
|
}
|
|
|
|
async fn seeded(pool: &sqlx::PgPool) -> Agent {
|
|
let workspace = Workspace {
|
|
id: WorkspaceId::new(),
|
|
name: "Acme".into(),
|
|
plan: "team".into(),
|
|
};
|
|
tc_db::repo::workspaces::insert(pool, &workspace)
|
|
.await
|
|
.unwrap();
|
|
let owner = User {
|
|
id: UserId::new(),
|
|
workspace_id: workspace.id,
|
|
email: format!("{}@acme.test", UserId::new()),
|
|
role: Role::Owner,
|
|
display_name: "Owner".into(),
|
|
created_at: time::OffsetDateTime::UNIX_EPOCH,
|
|
};
|
|
tc_db::repo::users::insert(pool, &owner).await.unwrap();
|
|
let agent = Agent {
|
|
id: AgentId::new(),
|
|
workspace_id: workspace.id,
|
|
name: "Scout".into(),
|
|
job_title: "Analyst".into(),
|
|
system_prompt: String::new(),
|
|
avatar: String::new(),
|
|
accent: String::new(),
|
|
wallpaper: String::new(),
|
|
managed_by: owner.id,
|
|
status: AgentStatus::Online,
|
|
};
|
|
tc_db::repo::agents::insert(pool, &agent, &AccessPolicy::default())
|
|
.await
|
|
.unwrap();
|
|
agent
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn due_routines_fire_real_runs_exactly_once() {
|
|
let pool = tc_testkit::test_pool().await;
|
|
let agent = seeded(&pool).await;
|
|
let runtime = Runtime::new(
|
|
pool.clone(),
|
|
Arc::new(ScriptedProvider::from_toml("").unwrap()),
|
|
RuntimeConfig::basic("scripted", 1024),
|
|
);
|
|
let scheduler = Scheduler::new(pool.clone(), runtime);
|
|
|
|
// A routine that became due a minute ago.
|
|
let now = time::OffsetDateTime::now_utc();
|
|
tc_db::repo::routines::create(
|
|
&pool,
|
|
agent.id,
|
|
"Morning digest",
|
|
"* * * * *",
|
|
json!({"message": "compile the digest"}),
|
|
now - time::Duration::minutes(1),
|
|
)
|
|
.await
|
|
.unwrap();
|
|
|
|
let fired = scheduler.tick(now).await.unwrap();
|
|
assert_eq!(fired, 1);
|
|
// Claimed: an immediate second tick fires nothing.
|
|
assert_eq!(scheduler.tick(now).await.unwrap(), 0);
|
|
|
|
// The routine's clock advanced beyond now.
|
|
let routines = tc_db::repo::routines::list_by_agent(&pool, agent.id)
|
|
.await
|
|
.unwrap();
|
|
assert!(routines[0].next_run_at.unwrap() > now);
|
|
assert!(routines[0].last_run_at.is_some());
|
|
|
|
// The firing produced a REAL run in the routine's dedicated session.
|
|
let mut found = false;
|
|
for _ in 0..100 {
|
|
let sessions = tc_db::repo::sessions::list_by_agent(&pool, agent.id)
|
|
.await
|
|
.unwrap();
|
|
if let Some(session) = sessions.iter().find(|s| s.title == "⏰ Morning digest") {
|
|
let history = tc_db::repo::messages::history(&pool, session.id)
|
|
.await
|
|
.unwrap();
|
|
if history.len() == 2
|
|
&& history[0].message.role == MessageRole::User
|
|
&& history[1].message.content["text"]
|
|
.as_str()
|
|
.unwrap_or_default()
|
|
.contains("compile the digest")
|
|
{
|
|
found = true;
|
|
break;
|
|
}
|
|
}
|
|
tokio::time::sleep(Duration::from_millis(50)).await;
|
|
}
|
|
assert!(found, "routine run never landed in its session");
|
|
|
|
// Re-firing later reuses the same session instead of spamming new ones.
|
|
tc_db::repo::routines::set_next_run(&pool, routines[0].id, Some(now))
|
|
.await
|
|
.unwrap();
|
|
scheduler.tick(now).await.unwrap();
|
|
for _ in 0..100 {
|
|
let sessions = tc_db::repo::sessions::list_by_agent(&pool, agent.id)
|
|
.await
|
|
.unwrap();
|
|
let routine_sessions: Vec<_> = sessions
|
|
.iter()
|
|
.filter(|s| s.title == "⏰ Morning digest")
|
|
.collect();
|
|
assert_eq!(routine_sessions.len(), 1);
|
|
let history = tc_db::repo::messages::history(&pool, routine_sessions[0].id)
|
|
.await
|
|
.unwrap();
|
|
if history.len() == 4 {
|
|
return;
|
|
}
|
|
tokio::time::sleep(Duration::from_millis(50)).await;
|
|
}
|
|
panic!("second firing never landed");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn paused_routines_do_not_fire() {
|
|
let pool = tc_testkit::test_pool().await;
|
|
let agent = seeded(&pool).await;
|
|
let runtime = Runtime::new(
|
|
pool.clone(),
|
|
Arc::new(ScriptedProvider::from_toml("").unwrap()),
|
|
RuntimeConfig::basic("scripted", 1024),
|
|
);
|
|
let scheduler = Scheduler::new(pool.clone(), runtime);
|
|
|
|
let now = time::OffsetDateTime::now_utc();
|
|
let routine = tc_db::repo::routines::create(
|
|
&pool,
|
|
agent.id,
|
|
"Paused digest",
|
|
"* * * * *",
|
|
json!({"message": "nope"}),
|
|
now - time::Duration::minutes(1),
|
|
)
|
|
.await
|
|
.unwrap();
|
|
sqlx::query("UPDATE routines SET status = 'paused' WHERE id = $1")
|
|
.bind(routine.id)
|
|
.execute(&pool)
|
|
.await
|
|
.unwrap();
|
|
|
|
assert_eq!(scheduler.tick(now).await.unwrap(), 0);
|
|
}
|