Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

17.6 KB
Raw Blame
use super::*;
use opensearch_query_builder::ToOpenSearchJson;

#[test]
fn test_build_bool_query_join_shape_multi_term_and() -> anyhow::Result<()> {
    let builder = DocumentQueryBuilder::new(vec!["foo".to_string(), "bar".to_string()])
        .match_type("partial")
        .page_size(20)
        .page(0)
        .user_id("alice");

    let json = builder.build_bool_query()?.build().to_json();

    let filter = json["bool"]["filter"].as_array().expect("filter array");
    assert!(
        filter.contains(&serde_json::json!({"term": {"_index": "documents"}})),
        "filter must constrain to documents index: {filter:?}"
    );
    assert!(
        filter.contains(&serde_json::json!({"term": {"document_relation": "document"}})),
        "filter must constrain to parent docs: {filter:?}"
    );
    assert!(
        filter.contains(&serde_json::json!({"term": {"owner_id": "alice"}})),
        "filter must constrain to owner: {filter:?}"
    );

    let must = json["bool"]["must"].as_array().expect("must array");
    assert_eq!(must.len(), 2, "expected one has_child per term: {must:?}");

    let term_match = |val: &serde_json::Value, expected_term: &str, expected_type: &str| {
        let has_child = &val["has_child"];
        assert_eq!(has_child["type"], "chunk");
        assert_eq!(
            has_child["query"][expected_type]["content"]["query"],
            expected_term
        );
    };
    term_match(&must[0], "foo", "match_phrase_prefix");
    term_match(&must[1], "bar", "match_phrase_prefix");

    Ok(())
}

#[test]
fn test_build_bool_query_join_shape_quoted_phrase_uses_match_phrase() -> anyhow::Result<()> {
    // A term that came from a quoted phrase has internal whitespace and
    // should fall back to match_phrase (no prefix expansion).
    let builder = DocumentQueryBuilder::new(vec!["deal review".to_string()])
        .match_type("partial")
        .user_id("alice");

    let json = builder.build_bool_query()?.build().to_json();
    let must = &json["bool"]["must"][0]["has_child"]["query"];
    assert!(
        must.get("match_phrase").is_some(),
        "quoted phrase should use match_phrase, got {must:?}"
    );
    assert_eq!(must["match_phrase"]["content"], "deal review");
    Ok(())
}

#[test]
fn test_build_bool_query_join_shape_short_term_no_prefix() -> anyhow::Result<()> {
    let builder = DocumentQueryBuilder::new(vec!["ab".to_string()])
        .match_type("partial")
        .user_id("alice");

    let json = builder.build_bool_query()?.build().to_json();
    let must = &json["bool"]["must"][0]["has_child"]["query"];
    assert!(
        must.get("match_phrase").is_some(),
        "short term should use match_phrase, got {must:?}"
    );
    Ok(())
}

#[test]
fn test_build_bool_query_join_shape_exact_match_type_uses_match_phrase() -> anyhow::Result<()> {
    let builder = DocumentQueryBuilder::new(vec!["release".to_string()])
        .match_type("exact")
        .user_id("alice");

    let json = builder.build_bool_query()?.build().to_json();
    let must = &json["bool"]["must"][0]["has_child"]["query"];
    assert!(
        must.get("match_phrase").is_some(),
        "match_type=exact should use match_phrase, got {must:?}"
    );
    Ok(())
}

#[test]
fn test_build_bool_query_join_shape_ids_only_filter() -> anyhow::Result<()> {
    let builder = DocumentQueryBuilder::new(vec!["foo".to_string()])
        .match_type("partial")
        .user_id("alice")
        .ids(vec!["doc1".to_string(), "doc2".to_string()])
        .ids_only(true);

    let json = builder.build_bool_query()?.build().to_json();
    let filter = json["bool"]["filter"].as_array().expect("filter array");
    assert!(
        filter.contains(&serde_json::json!({
            "terms": { "entity_id": ["doc1", "doc2"] }
        })),
        "ids_only should restrict to entity_id terms: {filter:?}"
    );
    assert!(
        !filter
            .iter()
            .any(|f| f.get("term").and_then(|t| t.get("owner_id")).is_some()),
        "ids_only should not include owner filter: {filter:?}"
    );
    Ok(())
}

#[test]
fn test_build_bool_query_join_shape_sub_type_filter() -> anyhow::Result<()> {
    let builder = DocumentQueryBuilder::new(vec!["foo".to_string()])
        .match_type("partial")
        .user_id("alice")
        .sub_types(vec!["task".to_string()]);

    let json = builder.build_bool_query()?.build().to_json();
    let filter = json["bool"]["filter"].as_array().expect("filter array");
    assert!(
        filter.contains(&serde_json::json!({"terms": {"sub_type": ["task"]}})),
        "sub_type filter should be on the parent: {filter:?}"
    );
    Ok(())
}

#[test]
fn test_build_bool_query_join_shape_has_inner_hits() -> anyhow::Result<()> {
    let builder = DocumentQueryBuilder::new(vec!["foo".to_string(), "bar".to_string()])
        .match_type("partial")
        .user_id("alice");

    let json = builder.build_bool_query()?.build().to_json();
    let must = json["bool"]["must"].as_array().expect("must array");
    for (idx, clause) in must.iter().enumerate() {
        let inner = &clause["has_child"]["inner_hits"];
        assert!(
            inner.is_object(),
            "expected inner_hits on has_child clause {idx}: {clause:?}"
        );
        assert_eq!(
            inner["name"],
            format!("term_{idx}"),
            "inner_hits name should be term_<idx>"
        );
        assert_eq!(
            inner["highlight"]["max_analyzer_offset"],
            serde_json::json!(super::super::HIGHLIGHT_MAX_ANALYZER_OFFSET),
            "inner_hits highlight must cap analyzed offset so an oversized \
             chunk can't fail the shard fetch: {inner:?}"
        );
    }
    Ok(())
}

#[test]
fn test_build_bool_query_name_mode_matches_document_name() -> anyhow::Result<()> {
    let builder = DocumentQueryBuilder::new(vec!["report".to_string()])
        .match_type("partial")
        .user_id("alice")
        .mode(DocumentSearchMode::Name);

    let json = builder.build_bool_query()?.build().to_json();

    // Access + index filters are unchanged in name mode.
    let filter = json["bool"]["filter"].as_array().expect("filter array");
    assert!(filter.contains(&serde_json::json!({"term": {"_index": "documents"}})));
    assert!(filter.contains(&serde_json::json!({"term": {"owner_id": "alice"}})));

    // The match clause targets the parent document_name, not child content.
    let must = json["bool"]["must"].as_array().expect("must array");
    assert_eq!(must.len(), 1, "name mode emits one title clause: {must:?}");
    assert_eq!(
        must[0]["match_phrase_prefix"]["document_name"]["query"],
        "report"
    );
    assert!(
        !json.to_string().contains("has_child"),
        "name mode must not query child content: {json}"
    );
    Ok(())
}

#[test]
fn test_build_bool_query_name_content_mode_matches_either() -> anyhow::Result<()> {
    let builder = DocumentQueryBuilder::new(vec!["report".to_string()])
        .match_type("partial")
        .user_id("alice")
        .mode(DocumentSearchMode::NameContent);

    let json = builder.build_bool_query()?.build().to_json();

    let must = json["bool"]["must"].as_array().expect("must array");
    assert_eq!(
        must.len(),
        1,
        "name_content wraps matching in one should-bool: {must:?}"
    );
    assert_eq!(must[0]["bool"]["minimum_should_match"], 1);
    let should = must[0]["bool"]["should"].as_array().expect("should array");
    assert_eq!(
        should.len(),
        2,
        "expected a name branch and a content branch: {should:?}"
    );

    // One branch matches the parent document_name…
    assert!(
        should
            .iter()
            .any(|c| c["match_phrase_prefix"]["document_name"]["query"] == "report"),
        "missing document_name branch: {should:?}"
    );
    // …the other matches child content via has_child.
    assert!(
        should
            .iter()
            .any(|c| c["bool"]["must"][0]["has_child"]["type"] == "chunk"),
        "missing has_child content branch: {should:?}"
    );
    Ok(())
}

#[test]
fn test_build_bool_query_property_filters_emit_nested() -> anyhow::Result<()> {
    let builder = DocumentQueryBuilder::new(vec!["foo".to_string()])
        .match_type("partial")
        .user_id("alice")
        .property_filters(vec![
            PropertyFilterArg {
                definition_id: "00000001-0000-0000-0000-000000000002".to_string(),
                values: vec![
                    "00000001-0000-0000-0002-000000000001".to_string(),
                    "00000001-0000-0000-0002-000000000004".to_string(),
                ],
                date_range: None,
                has_date: None,
            },
            PropertyFilterArg {
                definition_id: "00000001-0000-0000-0000-000000000001".to_string(),
                values: vec!["macro|[email protected]".to_string()],
                date_range: None,
                has_date: None,
            },
        ]);

    let json = builder.build_bool_query()?.build().to_json();
    let filter = json["bool"]["filter"].as_array().expect("filter array");

    let nested: Vec<&serde_json::Value> = filter
        .iter()
        .filter(|f| f.get("nested").is_some())
        .collect();
    assert_eq!(nested.len(), 2, "one nested clause per filter: {filter:?}");

    let status = &nested[0]["nested"];
    assert_eq!(status["path"], "properties");
    assert_eq!(status["ignore_unmapped"], true);
    let status_inner = status["query"]["bool"]["filter"]
        .as_array()
        .expect("nested bool filter");
    assert!(status_inner.contains(&serde_json::json!({
        "term": {"properties.definition_id": "00000001-0000-0000-0000-000000000002"}
    })));
    assert!(status_inner.contains(&serde_json::json!({
        "terms": {"properties.values": [
            "00000001-0000-0000-0002-000000000001",
            "00000001-0000-0000-0002-000000000004"
        ]}
    })));

    Ok(())
}

#[test]
fn test_build_bool_query_property_filter_empty_values_skipped() -> anyhow::Result<()> {
    let builder = DocumentQueryBuilder::new(vec!["foo".to_string()])
        .match_type("partial")
        .user_id("alice")
        .property_filters(vec![PropertyFilterArg {
            definition_id: "00000001-0000-0000-0000-000000000002".to_string(),
            values: vec![],
            date_range: None,
            has_date: None,
        }]);

    let json = builder.build_bool_query()?.build().to_json();
    let filter = json["bool"]["filter"].as_array().expect("filter array");
    assert!(
        !filter.iter().any(|f| f.get("nested").is_some()),
        "empty-values filter should emit no nested clause: {filter:?}"
    );
    Ok(())
}

#[test]
fn test_build_bool_query_tag_filter_emits_flat_terms() -> anyhow::Result<()> {
    let builder = DocumentQueryBuilder::new(vec!["foo".to_string()])
        .match_type("partial")
        .user_id("alice")
        .tag_option_ids(vec![
            "00000001-0000-0000-0003-000000000001".to_string(),
            "00000001-0000-0000-0003-000000000002".to_string(),
        ]);

    let json = builder.build_bool_query()?.build().to_json();
    let filter = json["bool"]["filter"].as_array().expect("filter array");

    let nested: Vec<&serde_json::Value> = filter
        .iter()
        .filter(|f| f.get("nested").is_some())
        .collect();
    assert_eq!(
        nested.len(),
        1,
        "tags collapse to one nested clause: {filter:?}"
    );

    let tags = &nested[0]["nested"];
    assert_eq!(tags["path"], "properties");
    assert_eq!(tags["ignore_unmapped"], true);
    // No definition_id term: match is on the globally-unique option ids alone.
    assert_eq!(
        tags["query"],
        serde_json::json!({
            "terms": {"properties.values": [
                "00000001-0000-0000-0003-000000000001",
                "00000001-0000-0000-0003-000000000002"
            ]}
        })
    );

    Ok(())
}

#[test]
fn test_build_bool_query_tag_filter_match_all_emits_per_option_musts() -> anyhow::Result<()> {
    let builder = DocumentQueryBuilder::new(vec!["foo".to_string()])
        .match_type("partial")
        .user_id("alice")
        .tag_option_ids(vec![
            "00000001-0000-0000-0003-000000000001".to_string(),
            "00000001-0000-0000-0003-000000000002".to_string(),
        ])
        .match_all_tags(true);

    let json = builder.build_bool_query()?.build().to_json();
    let filter = json["bool"]["filter"].as_array().expect("filter array");

    // Match-all wraps one nested clause per option id inside a bool.must, so
    // an entity must hold every id (possibly across nested property entries).
    let tag_bool: Vec<&serde_json::Value> = filter
        .iter()
        .filter(|f| f["bool"]["must"].is_array())
        .collect();
    assert_eq!(tag_bool.len(), 1, "one bool wrapper for tags: {filter:?}");

    let musts = tag_bool[0]["bool"]["must"].as_array().expect("must array");
    assert_eq!(musts.len(), 2, "one nested clause per option id");
    for (must, option_id) in musts.iter().zip([
        "00000001-0000-0000-0003-000000000001",
        "00000001-0000-0000-0003-000000000002",
    ]) {
        let nested = &must["nested"];
        assert_eq!(nested["path"], "properties");
        assert_eq!(nested["ignore_unmapped"], true);
        assert_eq!(
            nested["query"],
            serde_json::json!({"terms": {"properties.values": [option_id]}})
        );
    }

    Ok(())
}

#[test]
fn test_build_bool_query_tag_filter_empty_skipped() -> anyhow::Result<()> {
    let builder = DocumentQueryBuilder::new(vec!["foo".to_string()])
        .match_type("partial")
        .user_id("alice")
        .tag_option_ids(vec![]);

    let json = builder.build_bool_query()?.build().to_json();
    let filter = json["bool"]["filter"].as_array().expect("filter array");
    assert!(
        !filter.iter().any(|f| f.get("nested").is_some()),
        "empty tag_option_ids should emit no nested clause: {filter:?}"
    );
    Ok(())
}

#[test]
fn document_index_deserializes_parent_shape() {
    // Parent docs carry only parent-level metadata in `_source`; the
    // matching chunks' node_id / raw_content come via `inner_hits`.
    // Deserialization has to succeed (otherwise the unified search
    // response 500s on any parent hit).
    let parent = serde_json::json!({
        "entity_id": "00000000-0000-0000-0000-000000000001",
        "document_name": "Q3 Planning",
        "owner_id": "macro|[email protected]",
        "file_type": "pdf",
        "updated_at_millis": 1779000000000_i64,
        "document_relation": "document",
    });

    let doc: DocumentIndex =
        serde_json::from_value(parent).expect("parent _source should deserialize");
    assert_eq!(doc.document_name, "Q3 Planning");
    assert_eq!(doc.owner_id, "macro|[email protected]");
}

#[test]
fn date_property_filters_constrain_candidates_before_search_pagination() -> anyhow::Result<()> {
    let start =
        chrono::DateTime::parse_from_rfc3339("2025-12-31T23:59:59Z")?.with_timezone(&chrono::Utc);
    let end =
        chrono::DateTime::parse_from_rfc3339("2026-01-01T00:00:00Z")?.with_timezone(&chrono::Utc);
    let arg = PropertyFilterArg {
        definition_id: "00000001-0000-0000-0000-000000000004".into(),
        values: vec![],
        date_range: Some(PropertyDateRange {
            start: Some(start),
            end: Some(end),
        }),
        has_date: Some(true),
    };
    let query = DocumentQueryBuilder::new(vec!["foo".into()])
        .user_id("alice")
        .property_filters(vec![arg.clone()])
        .build_bool_query()?
        .build()
        .to_json();
    let filter = query["bool"]["filter"].as_array().unwrap();
    let range = filter.iter().find(|v| v["bool"].is_object()).unwrap();
    let nested = &range["bool"]["filter"][0]["nested"]["query"]["bool"]["filter"];
    assert_eq!(
        nested[0]["term"]["properties.definition_id"],
        arg.definition_id
    );
    assert_eq!(
        nested[1]["range"]["properties.date_value"]["gte"],
        start.timestamp_millis()
    );
    assert_eq!(
        nested[1]["range"]["properties.date_value"]["lt"],
        end.timestamp_millis()
    );
    assert_eq!(
        range["bool"]["filter"][1]["nested"]["query"]["bool"]["filter"][1]["exists"]["field"],
        "properties.date_value"
    );
    let missing = DocumentQueryBuilder::new(vec!["foo".into()])
        .user_id("alice")
        .property_filters(vec![PropertyFilterArg {
            date_range: None,
            has_date: Some(false),
            ..arg
        }])
        .build_bool_query()?
        .build()
        .to_json();
    let missing_clause = missing["bool"]["filter"]
        .as_array()
        .unwrap()
        .iter()
        .find(|v| v["bool"].is_object())
        .unwrap();
    assert_eq!(
        missing_clause["bool"]["must_not"][0]["nested"]["query"]["bool"]["filter"][1]["exists"]["field"],
        "properties.date_value"
    );
    Ok(())
}

#[test]
fn open_date_ranges_only_emit_supplied_search_bounds() -> anyhow::Result<()> {
    let boundary =
        chrono::DateTime::parse_from_rfc3339("2026-10-21T00:00:00Z")?.with_timezone(&chrono::Utc);
    for (start, end, expected, absent) in [
        (Some(boundary), None, "gte", "lt"),
        (None, Some(boundary), "lt", "gte"),
    ] {
        let query = build_property_filter(&PropertyFilterArg {
            definition_id: "00000001-0000-0000-0000-000000000004".to_owned(),
            values: vec![],
            date_range: Some(PropertyDateRange { start, end }),
            has_date: None,
        })
        .unwrap()
        .to_json();
        let range = &query["bool"]["filter"][0]["nested"]["query"]["bool"]["filter"][1]["range"]["properties.date_value"];
        assert_eq!(range[expected], boundary.timestamp_millis());
        assert!(range.get(absent).is_none());
    }
    Ok(())
}