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::date_format::EpochMillis;
fn args(doc_id: &str, node_id: &str) -> UpsertDocumentArgs {
UpsertDocumentArgs {
document_id: doc_id.to_string(),
node_id: node_id.to_string(),
document_name: format!("name-{doc_id}"),
file_type: "md".to_string(),
owner_id: format!("owner-{doc_id}"),
raw_content: None,
content: format!("content {doc_id} {node_id}"),
updated_at_millis: EpochMillis::new(1_700_000_000_123).unwrap(),
sub_type: None,
properties: vec![],
}
}
#[test]
fn parent_doc_body_has_metadata_no_chunk_fields() {
let doc = parent_doc_body(&args("doc1", "n1"));
assert_eq!(doc["entity_id"], "doc1");
assert_eq!(doc["document_name"], "name-doc1");
assert_eq!(doc["owner_id"], "owner-doc1");
assert_eq!(doc["file_type"], "md");
assert_eq!(doc["updated_at_millis"], 1_700_000_000_123i64);
assert_eq!(doc["document_relation"], "document");
// Child-only fields must not be present on the parent.
assert!(doc.get("content").is_none());
assert!(doc.get("node_id").is_none());
assert!(doc.get("raw_content").is_none());
}
#[test]
fn parent_doc_body_includes_optional_sub_type() {
let mut a = args("doc1", "n1");
a.sub_type = Some("task".to_string());
let doc = parent_doc_body(&a);
assert_eq!(doc["sub_type"], "task");
}
#[test]
fn parent_doc_body_includes_properties_when_present() {
let mut a = args("doc1", "n1");
a.properties = vec![
IndexedProperty {
definition_id: "status-def".to_string(),
values: vec!["not-started".to_string()],
..Default::default()
},
IndexedProperty {
definition_id: "assignees-def".to_string(),
values: vec!["macro|[email protected]".to_string()],
..Default::default()
},
IndexedProperty {
definition_id: "due-date-def".to_string(),
date_value: Some(1_700_000_000_000),
..Default::default()
},
];
let doc = parent_doc_body(&a);
let props = doc["properties"].as_array().expect("properties array");
assert_eq!(props.len(), 3);
assert_eq!(props[0]["definition_id"], "status-def");
assert_eq!(props[0]["values"][0], "not-started");
// Empty value fields are skipped, not serialized as empty/null.
assert!(props[0].get("number_value").is_none());
assert_eq!(props[1]["values"][0], "macro|[email protected]");
assert_eq!(props[2]["date_value"], 1_700_000_000_000i64);
assert!(props[2].get("values").is_none());
}
#[test]
fn parent_doc_body_omits_properties_when_empty() {
let doc = parent_doc_body(&args("doc1", "n1"));
assert!(doc.get("properties").is_none());
}
#[test]
fn child_doc_body_has_chunk_fields_and_join_pointer() {
let mut a = args("doc1", "n1");
a.raw_content = Some("raw".to_string());
let doc = child_doc_body(&a);
assert_eq!(doc["entity_id"], "doc1");
assert_eq!(doc["node_id"], "n1");
assert_eq!(doc["content"], "content doc1 n1");
assert_eq!(doc["raw_content"], "raw");
assert_eq!(doc["document_relation"]["name"], "chunk");
assert_eq!(doc["document_relation"]["parent"], "doc1");
// Parent-only metadata must not be redundantly written to children.
assert!(doc.get("document_name").is_none());
assert!(doc.get("owner_id").is_none());
assert!(doc.get("file_type").is_none());
assert!(doc.get("sub_type").is_none());
}
#[test]
fn child_doc_body_omits_raw_content_when_none() {
let doc = child_doc_body(&args("doc1", "n1"));
assert!(doc.get("raw_content").is_none());
}
#[test]
fn parent_only_bulk_body_writes_parent_and_no_children() {
let mut a = args("doc1", "n1");
a.sub_type = Some("task".to_string());
a.properties = vec![IndexedProperty {
definition_id: "tag-def".to_string(),
values: vec!["opt-1".to_string()],
..Default::default()
}];
let body = parent_only_bulk_body(&a);
assert_eq!(body.len(), 2);
let action: serde_json::Value = serde_json::from_str(&body[0]).unwrap();
assert_eq!(action["index"]["_id"], "doc1");
assert_eq!(action["index"]["routing"], "doc1");
let doc: serde_json::Value = serde_json::from_str(&body[1]).unwrap();
assert_eq!(doc["entity_id"], "doc1");
assert_eq!(doc["document_name"], "name-doc1");
assert_eq!(doc["owner_id"], "owner-doc1");
assert_eq!(doc["file_type"], "md");
assert_eq!(doc["document_relation"], "document");
assert_eq!(doc["sub_type"], "task");
assert_eq!(doc["properties"][0]["definition_id"], "tag-def");
assert_eq!(doc["properties"][0]["values"][0], "opt-1");
// No chunk is written and no chunk fields leak onto the parent.
assert!(doc.get("content").is_none());
assert!(doc.get("node_id").is_none());
assert!(doc.get("raw_content").is_none());
}
#[test]
fn resolve_destination_defaults_to_documents_alias() {
assert_eq!(resolve_destination(None), SearchIndex::Documents.as_ref());
assert_eq!(resolve_destination(Some("documents_v2")), "documents_v2");
}
/// Builds the same bulk body the join-shape upsert would send, so we can
/// assert the exact OpenSearch wire format without standing up a cluster.
fn build_join_bulk_body(documents: &[UpsertDocumentArgs]) -> Vec<serde_json::Value> {
let mut bulk: Vec<serde_json::Value> = Vec::new();
let mut seen_parents: std::collections::HashSet<&str> = std::collections::HashSet::new();
for doc in documents {
let parent_id = doc.document_id.as_str();
if seen_parents.insert(parent_id) {
bulk.push(serde_json::json!({
"index": { "_id": parent_id, "routing": parent_id }
}));
bulk.push(parent_doc_body(doc));
}
let child_id = format!("{}:{}", doc.document_id, doc.node_id);
bulk.push(serde_json::json!({
"index": { "_id": child_id, "routing": parent_id }
}));
bulk.push(child_doc_body(doc));
}
bulk
}
#[test]
fn join_bulk_body_dedupes_parent_within_batch() {
let body = build_join_bulk_body(&[args("doc1", "n1"), args("doc1", "n2"), args("doc2", "n1")]);
// 2 parents × 2 ops + 3 children × 2 ops = 10 entries
assert_eq!(body.len(), 10);
// First op is doc1's parent index.
assert_eq!(body[0]["index"]["_id"], "doc1");
assert_eq!(body[0]["index"]["routing"], "doc1");
assert_eq!(body[1]["entity_id"], "doc1");
assert_eq!(body[1]["document_relation"], "document");
// Then doc1's first child.
assert_eq!(body[2]["index"]["_id"], "doc1:n1");
assert_eq!(body[2]["index"]["routing"], "doc1");
assert_eq!(body[3]["document_relation"]["parent"], "doc1");
// Doc1's second child — note no second parent index for doc1.
assert_eq!(body[4]["index"]["_id"], "doc1:n2");
assert_eq!(body[4]["index"]["routing"], "doc1");
// Now doc2 — new parent so we get an index op again.
assert_eq!(body[6]["index"]["_id"], "doc2");
assert_eq!(body[6]["index"]["routing"], "doc2");
assert_eq!(body[8]["index"]["_id"], "doc2:n1");
assert_eq!(body[8]["index"]["routing"], "doc2");
}
#[test]
fn parent_body_omits_sub_type_when_none_so_index_clears_it() {
// The whole point of switching from update+doc_as_upsert to full index
// writes: when sub_type becomes None, the new parent body must NOT
// include sub_type, so an `index` op will drop any previously-stored
// value rather than leaving it stale.
let doc = parent_doc_body(&args("doc1", "n1"));
assert!(doc.get("sub_type").is_none());
}
#[test]
fn child_body_keeps_content_under_cap_intact() {
let mut a = args("doc1", "n1");
a.content = "x".repeat(MAX_CHUNK_CONTENT_CHARS);
let doc = child_doc_body(&a);
assert_eq!(
doc["content"].as_str().unwrap().chars().count(),
MAX_CHUNK_CONTENT_CHARS
);
}
#[test]
fn child_body_truncates_oversized_content() {
let mut a = args("doc1", "n1");
a.content = "x".repeat(MAX_CHUNK_CONTENT_CHARS + 10);
let doc = child_doc_body(&a);
assert_eq!(
doc["content"].as_str().unwrap().chars().count(),
MAX_CHUNK_CONTENT_CHARS
);
}
#[test]
fn child_body_truncates_on_char_boundary() {
let mut a = args("doc1", "n1");
// é is 2 bytes per char, so a byte-index split would panic mid-char.
a.content = "é".repeat(MAX_CHUNK_CONTENT_CHARS + 5);
let doc = child_doc_body(&a);
assert_eq!(
doc["content"].as_str().unwrap().chars().count(),
MAX_CHUNK_CONTENT_CHARS
);
}