Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
use super::*;
use crate::domain::ports::AgentSessionLogRepo;
use crate::domain::session_task::{LinkedTask, SessionTaskError, SessionTasks, TaskPullRequests};
use crate::testing::{InMemoryAgentSessionRepo, RecordingRealtime, test_agent_session};
/// Sessions without tasks; linking a pull request to one has nothing to do.
struct NoTasks;
impl SessionTasks for NoTasks {
fn link_task<'a>(
&'a self,
_session: AgentSessionId,
_owner: &'a MacroUserIdStr<'static>,
_task: &'a str,
) -> Pin<Box<dyn Future<Output = Result<LinkedTask, SessionTaskError>> + Send + 'a>> {
unimplemented!("pull request tests link no tasks")
}
fn task_pull_requests<'a>(
&'a self,
_owner: &'a MacroUserIdStr<'static>,
_task: &'a str,
) -> Pin<Box<dyn Future<Output = Result<TaskPullRequests, SessionTaskError>> + Send + 'a>> {
unimplemented!("pull request tests list no tasks")
}
fn link_session_pull_request<'a>(
&'a self,
_session: AgentSessionId,
_owner: &'a MacroUserIdStr<'static>,
) -> Pin<Box<dyn Future<Output = Result<(), SessionTaskError>> + Send + 'a>> {
Box::pin(async { Ok(()) })
}
}
#[test]
fn normalizes_pr_urls_and_rejects_non_pr_destinations() {
assert_eq!(
canonical_url("https://github.com/org/repo/pull/001/#discussion").unwrap(),
"https://github.com/org/repo/pull/1"
);
assert_eq!(
canonical_url("https://GITHUB.com:443/org/repo/pull/42?diff=split#discussion").unwrap(),
"https://github.com/org/repo/pull/42"
);
for invalid in [
"javascript:alert(1)",
"http://github.com/org/repo/pull/1",
"https://evil.com/org/repo/pull/1",
"https://[email protected]/org/repo/pull/1",
"https://github.com/org/repo/issues/1",
"https://github.com/org/repo/pull/0",
"https://github.com/org/repo/pull/1/files",
"https://github.com:8443/org/repo/pull/1",
"https://github.com.evil.com/org/repo/pull/1",
"https://github.com/org/repo/pull/18446744073709551616",
"https://github.com/org/repo/pull/1\n",
] {
assert!(canonical_url(invalid).is_err(), "{invalid}");
}
}
#[tokio::test]
async fn persists_and_publishes_once_and_rejects_another_owner() {
let repo = InMemoryAgentSessionRepo::new();
let session = test_agent_session(AgentSessionId::new());
repo.insert_session(session.clone());
let realtime = RecordingRealtime::new();
let service = SessionPullRequestService::new(repo.clone(), realtime.clone(), Arc::new(NoTasks));
let url = "https://github.com/org/repo/pull/123";
let other = MacroUserIdStr::try_from_email("[email protected]").unwrap();
assert!(matches!(
service
.set_pull_request(session.id, &other, url, None)
.await,
Err(AgentSessionError::Forbidden)
));
assert!(repo.list_by_session(session.id).await.unwrap().is_empty());
service
.set_pull_request(session.id, session.owner_user().unwrap(), url, None)
.await
.unwrap();
service
.set_pull_request(session.id, session.owner_user().unwrap(), url, None)
.await
.unwrap();
assert!(repo.list_by_session(session.id).await.unwrap().is_empty());
assert_eq!(
repo.get(session.id)
.await
.unwrap()
.pull_request_url
.as_deref(),
Some(url)
);
assert_eq!(realtime.updated(), [session.id]);
service
.set_pull_request(
session.id,
session.owner_user().unwrap(),
"https://github.com/org/repo/pull/124",
None,
)
.await
.unwrap();
assert!(repo.list_by_session(session.id).await.unwrap().is_empty());
assert_eq!(
repo.get(session.id)
.await
.unwrap()
.pull_request_url
.as_deref(),
Some("https://github.com/org/repo/pull/124")
);
assert_eq!(realtime.updated(), [session.id, session.id]);
}
#[tokio::test]
async fn gateway_failure_does_not_undo_the_persisted_link() {
let repo = InMemoryAgentSessionRepo::new();
let session = test_agent_session(AgentSessionId::new());
repo.insert_session(session.clone());
let service =
SessionPullRequestService::new(repo.clone(), RecordingRealtime::down(), Arc::new(NoTasks));
let url = "https://github.com/org/repo/pull/123";
service
.set_pull_request(session.id, session.owner_user().unwrap(), url, None)
.await
.unwrap();
assert_eq!(
repo.get(session.id)
.await
.unwrap()
.pull_request_url
.as_deref(),
Some(url)
);
}
#[tokio::test]
async fn superseded_claim_cannot_publish_even_an_unchanged_url() {
use crate::domain::model::ClaimOutcome;
use crate::domain::model::ReplicaId;
use crate::domain::ports::SessionOwnership;
let repo = InMemoryAgentSessionRepo::new();
let session = test_agent_session(AgentSessionId::new());
repo.insert_session(session.clone());
let replica = ReplicaId::mint();
let ClaimOutcome::Claimed(old) = repo.claim(session.id, replica).await.unwrap() else {
panic!("claim")
};
let ClaimOutcome::Claimed(current) = repo.claim(session.id, replica).await.unwrap() else {
panic!("reclaim")
};
let realtime = RecordingRealtime::new();
let service = SessionPullRequestService::new(repo.clone(), realtime.clone(), Arc::new(NoTasks));
let current_url = "https://github.com/org/repo/pull/2";
service
.set_pull_request(
session.id,
session.owner_user().unwrap(),
current_url,
Some(current),
)
.await
.unwrap();
for url in ["https://github.com/org/repo/pull/1", current_url] {
assert!(
matches!(service.set_pull_request(session.id, session.owner_user().unwrap(), url, Some(old)).await, Err(AgentSessionError::FencedOut(id)) if id == session.id)
);
}
assert_eq!(
repo.get(session.id)
.await
.unwrap()
.pull_request_url
.as_deref(),
Some(current_url)
);
assert_eq!(realtime.updated(), [session.id]);
}