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.

233 KB
Raw Blame
use crate::{
    domain::models::{DynamicSort, DynamicSortValue, SoupDocumentServerFacts},
    outbound::pg_soup_repo::{
        expanded::{
            by_ids::{expanded_soup_by_ids, expanded_soup_by_ids_with_projection},
            dynamic::{
                ExpandedDynamicCursorArgs, expanded_dynamic_cursor_soup,
                expanded_dynamic_cursor_soup_with_projection,
            },
        },
        populate_properties,
    },
};
use filter_ast::Expr;
use item_filters::{
    PropertyFilter,
    ast::{
        EntityFilterAst, chat::ChatLiteral, date::DateLiteral, document::DocumentLiteral,
        project::ProjectLiteral,
    },
};
use macro_db_migrator::MACRO_DB_MIGRATIONS;
use macro_user_id::{cowlike::CowLike, user_id::MacroUserIdStr};
use model_entity::EntityType;
use models_pagination::Identify;
use models_pagination::{Frecency, PaginateOn, Query, SimpleSortMethod};
use models_soup::item::SoupItem;
use sqlx::{PgPool, Pool, Postgres};
use std::collections::HashSet;
use std::sync::Arc;
use system_properties::{StatusOption, SystemPropertyKey};
use uuid::Uuid;

/// The unfiltered soup pages used to be served by two hand-written queries in
/// `expanded::by_cursor`. They are now answered by the dynamic builder with an
/// empty filter, which folds to the same predicates. These helpers keep the
/// old call shape so the suite below goes on asserting the same behaviour
/// against the replacement.
async fn expanded_generic_cursor_soup(
    db: &PgPool,
    user_id: MacroUserIdStr<'_>,
    limit: u16,
    cursor: Query<Uuid, SimpleSortMethod, ()>,
) -> Result<Vec<SoupItem<()>>, sqlx::Error> {
    expanded_dynamic_cursor_soup(
        db,
        ExpandedDynamicCursorArgs {
            user_id,
            limit,
            cursor: DynamicSort::from_timestamp_query(
                cursor.map_filter(|_| EntityFilterAst::default()),
            ),
            exclude_frecency: false,
        },
    )
    .await
}

async fn no_frecency_expanded_generic_soup(
    db: &PgPool,
    user_id: MacroUserIdStr<'_>,
    limit: u16,
    cursor: Query<Uuid, SimpleSortMethod, Frecency>,
) -> Result<Vec<SoupItem<()>>, sqlx::Error> {
    expanded_dynamic_cursor_soup(
        db,
        ExpandedDynamicCursorArgs {
            user_id,
            limit,
            cursor: DynamicSort::from_timestamp_query(
                cursor.map_filter(|_| EntityFilterAst::default()),
            ),
            exclude_frecency: true,
        },
    )
    .await
}

macro_rules! unwrap_enum {
    // Base case: single variant
    ($value:expr, $variant:path) => {
        match $value {
            $variant(inner) => inner,
            _ => panic!(
                "called `unwrap_enum!` on variant that didn't match `{}`",
                stringify!($variant)
            ),
        }
    };

    // Recursive case: peel off first variant and recurse
    ($value:expr, $variant:path => $($rest:path)=>+) => {
        match $value {
            $variant(inner) => unwrap_enum!(inner, $($rest)=>+),
            _ => panic!(
                "called `unwrap_enum!` on nested variant that didn't match `{} => ...`",
                stringify!($variant)
            ),
        }
    };
}
// 2 items have no viewing history, so they should be last in the response when sorting by viewed_at
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("mixed_items_expanded")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_viewed_at_orders_nulls_last(pool: Pool<Postgres>) -> anyhow::Result<()> {
    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
    let items = expanded_generic_cursor_soup(
        &pool,
        user_id.copied(),
        20,
        Query::Sort(SimpleSortMethod::ViewedAt, ()),
    )
    .await?;

    assert_eq!(items.len(), 13, "Should get 13 total items");

    // Make sure we got only the items with a history entry.
    let returned_ids: HashSet<Uuid> = items.iter().map(|item| item.id()).collect();

    let expected_ids: HashSet<Uuid> = [
        "22222222-0000-0000-0000-000000000000", // chat-standalone
        "11111111-bbbb-bbbb-bbbb-bbbbbbbbbbbb", // doc-in-B
        "11111111-dddd-dddd-dddd-dddddddddddd", // doc-in-D
        "cccccccc-ffff-ffff-ffff-ffffffffffff", // project-C
        "dddddddd-ffff-ffff-ffff-ffffffffffff", // project-D
        "11111111-cccc-cccc-cccc-cccccccccccc", // doc-in-C
        "22222222-bbbb-bbbb-bbbb-bbbbbbbbbbbb", // chat-in-B
        "aaaaaaaa-ffff-ffff-ffff-ffffffffffff", // project-A
        "22222222-aaaa-aaaa-aaaa-aaaaaaaaaaaa", // chat-in-A
        "11111111-0000-0000-0000-000000000000", // doc-standalone
        "22222222-cccc-cccc-cccc-cccccccccccc", // chat-in-C
        "bbbbbbbb-ffff-ffff-ffff-ffffffffffff", // project-B
        "11111111-aaaa-aaaa-aaaa-aaaaaaaaaaaa", // doc-in-A
    ]
    .iter()
    .map(|&s| Uuid::parse_str(s).unwrap())
    .collect();

    assert_eq!(
        returned_ids, expected_ids,
        "Should get the right set of items that have been viewed"
    );

    // Check that items are ordered by their UserHistory.updatedAt timestamp.
    let ordered_ids: Vec<Uuid> = items.iter().map(|item| item.id()).collect();

    let expected_order: Vec<Uuid> = vec![
        Uuid::parse_str("11111111-bbbb-bbbb-bbbb-bbbbbbbbbbbb").unwrap(), // doc-in-B - 2024-01-10
        Uuid::parse_str("22222222-0000-0000-0000-000000000000").unwrap(), // chat-standalone - 2024-01-09
        Uuid::parse_str("11111111-aaaa-aaaa-aaaa-aaaaaaaaaaaa").unwrap(), // doc-in-A - 2024-01-08
        Uuid::parse_str("22222222-aaaa-aaaa-aaaa-aaaaaaaaaaaa").unwrap(), // chat-in-A - 2024-01-07
        Uuid::parse_str("11111111-0000-0000-0000-000000000000").unwrap(), // doc-standalone - 2024-01-06
        Uuid::parse_str("11111111-dddd-dddd-dddd-dddddddddddd").unwrap(), // doc-in-D - 2023-01-05
        Uuid::parse_str("22222222-cccc-cccc-cccc-cccccccccccc").unwrap(), // chat-in-C - 2023-01-04
        // Null viewed_at items, tiebroken by id DESC:
        Uuid::parse_str("dddddddd-ffff-ffff-ffff-ffffffffffff").unwrap(), // project-D - null
        Uuid::parse_str("cccccccc-ffff-ffff-ffff-ffffffffffff").unwrap(), // project-C - null
        Uuid::parse_str("bbbbbbbb-ffff-ffff-ffff-ffffffffffff").unwrap(), // project-B - null
        Uuid::parse_str("aaaaaaaa-ffff-ffff-ffff-ffffffffffff").unwrap(), // project-A - null
        Uuid::parse_str("22222222-bbbb-bbbb-bbbb-bbbbbbbbbbbb").unwrap(), // chat-in-B - null
        Uuid::parse_str("11111111-cccc-cccc-cccc-cccccccccccc").unwrap(), // doc-in-C - null
    ];
    assert_eq!(
        ordered_ids, expected_order,
        "Wrong item order based on UserHistory"
    );

    // Map for easier lookup when checking item details
    let items_map: std::collections::HashMap<Uuid, &SoupItem> =
        items.iter().map(|item| (item.id(), item)).collect();

    // Check a standalone item that is still present
    let chat_uuid = Uuid::parse_str("22222222-0000-0000-0000-000000000000").unwrap(); // chat-standalone
    if let Some(SoupItem::Chat(chat)) = items_map.get(&chat_uuid) {
        assert_eq!(chat.name, "Standalone Chat");
        assert_eq!(chat.project_id, None, "Standalone shouldn't have project");
    } else {
        panic!("Missing chat-standalone");
    }

    // Check an item with both inherited and direct access that is still present
    let doc_uuid = Uuid::parse_str("11111111-bbbb-bbbb-bbbb-bbbbbbbbbbbb").unwrap(); // doc-in-B
    if let Some(SoupItem::Document(doc)) = items_map.get(&doc_uuid) {
        assert_eq!(doc.name, "Document in B");
        assert_eq!(
            doc.project_id.as_ref(),
            Some(&Uuid::parse_str("bbbbbbbb-ffff-ffff-ffff-ffffffffffff").unwrap()), // project-B
            "Wrong project on mixed access doc"
        );
    } else {
        panic!("Missing doc-in-B");
    }

    Ok(())
}

// testing that the cursor based pagination works
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("mixed_items_expanded")
    )
)]
async fn test_get_user_items_expanded_cursor(pool: Pool<Postgres>) -> anyhow::Result<()> {
    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
    let result = expanded_generic_cursor_soup(
        &pool,
        user_id.copied(),
        1,
        Query::Sort(SimpleSortMethod::ViewedAt, ()),
    )
    .await?
    .into_iter()
    .paginate_on(1, SimpleSortMethod::ViewedAt)
    .into_page();
    let items = result.items;

    assert_eq!(items.len(), 1, "Should get 1 item");

    match &items[0] {
        SoupItem::Document(doc) => {
            let expected_uuid = Uuid::parse_str("11111111-bbbb-bbbb-bbbb-bbbbbbbbbbbb").unwrap(); // doc-in-B
            assert_eq!(
                doc.id, expected_uuid,
                "First item should be document with ID doc-in-B"
            );
        }
        _ => panic!("First item should be a document"),
    }

    let items = expanded_generic_cursor_soup(
        &pool,
        user_id.copied(),
        1,
        Query::new(
            result.next_cursor.map(|s| {
                let decoded = s.decode_json().unwrap();
                models_pagination::Cursor {
                    id: decoded.id,
                    limit: decoded.limit,
                    val: decoded.val,
                    filter: decoded.filter,
                }
            }),
            SimpleSortMethod::ViewedAt,
            (),
        ),
    )
    .await?;

    assert_eq!(items.len(), 1, "Should get 1 item");

    match &items[0] {
        SoupItem::Chat(chat) => {
            let expected_uuid = Uuid::parse_str("22222222-0000-0000-0000-000000000000").unwrap(); // chat-standalone
            assert_eq!(
                chat.id, expected_uuid,
                "Second item should be chat with ID chat-standalone"
            );
        }
        _ => panic!("Second item should be a chat"),
    }

    Ok(())
}

// testing the sorting methods work as expected
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("sorting_expanded_items")
    )
)]
async fn test_expanded_generic_sorting_methods(pool: Pool<Postgres>) -> anyhow::Result<()> {
    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();

    // --- Helper to extract IDs for easy comparison ---
    let get_item_ids =
        |items: &[SoupItem]| -> Vec<Uuid> { items.iter().map(|item| item.id()).collect() };

    // --- Case 1: Test SortMethod::LastViewed ---
    // Should FILTER to only the 3 items with a history entry.
    {
        let items = expanded_generic_cursor_soup(
            &pool,
            user_id.copied(),
            10,
            Query::Sort(SimpleSortMethod::ViewedAt, ()),
        )
        .await?;
        assert_eq!(
            items.len(),
            6,
            "LastViewed should filter to only 6 viewed items"
        );

        let item_ids = get_item_ids(&items);
        let expected_ids: Vec<Uuid> = [
            "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", // doc-A
            "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb", // doc-B
            "aaaaaaaa-cccc-cccc-cccc-cccccccccccc", // chat-A
            "bbbbbbbb-ffff-ffff-ffff-ffffffffffff", // project-B (epoch, tiebroken by id DESC)
            "bbbbbbbb-cccc-cccc-cccc-cccccccccccc", // chat-B (epoch, tiebroken by id DESC)
            "aaaaaaaa-ffff-ffff-ffff-ffffffffffff", // project-A (epoch, tiebroken by id DESC)
        ]
        .iter()
        .map(|&s| Uuid::parse_str(s).unwrap())
        .collect();
        assert_eq!(
            item_ids, expected_ids,
            "Failed to sort correctly by LastViewed"
        );
    }

    // --- Case 2: Test SortMethod::UpdatedAt ---
    // Should return all 4 accessible items.
    {
        let items = expanded_generic_cursor_soup(
            &pool,
            user_id.copied(),
            10,
            Query::Sort(SimpleSortMethod::UpdatedAt, ()),
        )
        .await?;
        assert_eq!(items.len(), 6, "UpdatedAt should return all 6 items");

        let item_ids = get_item_ids(&items);
        let expected_ids: Vec<Uuid> = [
            "aaaaaaaa-cccc-cccc-cccc-cccccccccccc", // chat-A
            "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", // doc-A
            "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb", // doc-B
            "bbbbbbbb-cccc-cccc-cccc-cccccccccccc", // chat-B
            "bbbbbbbb-ffff-ffff-ffff-ffffffffffff", // project-B
            "aaaaaaaa-ffff-ffff-ffff-ffffffffffff", // project-A
        ]
        .iter()
        .map(|&s| Uuid::parse_str(s).unwrap())
        .collect();
        assert_eq!(
            item_ids, expected_ids,
            "Failed to sort correctly by UpdatedAt"
        );
    }

    // --- Case 3: Test SortMethod::CreatedAt ---
    // Should return all 4 accessible items.
    {
        let items = expanded_generic_cursor_soup(
            &pool,
            user_id.copied(),
            10,
            Query::Sort(SimpleSortMethod::CreatedAt, ()),
        )
        .await?;
        assert_eq!(items.len(), 6, "CreatedAt should return all 6 items");

        let item_ids = get_item_ids(&items);
        let expected_ids: Vec<Uuid> = [
            "bbbbbbbb-cccc-cccc-cccc-cccccccccccc", // chat-B
            "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb", // doc-B
            "aaaaaaaa-cccc-cccc-cccc-cccccccccccc", // chat-A
            "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", // doc-A
            "bbbbbbbb-ffff-ffff-ffff-ffffffffffff", // project-B
            "aaaaaaaa-ffff-ffff-ffff-ffffffffffff", // project-A
        ]
        .iter()
        .map(|&s| Uuid::parse_str(s).unwrap())
        .collect();
        assert_eq!(
            item_ids, expected_ids,
            "Failed to sort correctly by CreatedAt"
        );
    }

    Ok(())
}

// Test that expanded_soup_by_ids includes items with implicit access
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("mixed_items_expanded")
    )
)]
async fn test_expanded_soup_by_ids(pool: Pool<Postgres>) {
    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();

    // Request specific items, including some we have implicit access to through projects
    let entities = [
        EntityType::Document.with_entity_str("11111111-aaaa-aaaa-aaaa-aaaaaaaaaaaa"), // doc-in-A
        EntityType::Chat.with_entity_str("22222222-bbbb-bbbb-bbbb-bbbbbbbbbbbb"),     // chat-in-B
        EntityType::Document.with_entity_str("11111111-0000-0000-0000-000000000000"), // doc-standalone
        EntityType::Chat.with_entity_str("22222222-0000-0000-0000-000000000000"), // chat-standalone
        EntityType::Project.with_entity_str("aaaaaaaa-ffff-ffff-ffff-ffffffffffff"), // project-A - Should be ignored in expanded soup
    ];

    let items = expanded_soup_by_ids(&pool, user_id, &entities)
        .await
        .unwrap();

    // Should get 4 items (projects are excluded from expanded soup)
    assert_eq!(items.len(), 4, "Should get 4 items (excluding project)");

    // Verify we can access items through project inheritance
    // doc-in-A is in project-A which user-1 has access to
    let expected_doc_id = Uuid::parse_str("11111111-aaaa-aaaa-aaaa-aaaaaaaaaaaa").unwrap(); // doc-in-A
    let expected_project_id = Uuid::parse_str("aaaaaaaa-ffff-ffff-ffff-ffffffffffff").unwrap(); // project-A
    let doc = items
        .iter()
        .find_map(|x| match x {
            SoupItem::Document(soup_document) if soup_document.id == expected_doc_id => {
                Some(soup_document)
            }
            _ => None,
        })
        .expect("The document should exist");
    assert_eq!(doc.name, "Document in A");
    assert_eq!(doc.project_id, Some(expected_project_id));

    // chat-in-B is in project-B which is a child of project-A
    let expected_chat_id = Uuid::parse_str("22222222-bbbb-bbbb-bbbb-bbbbbbbbbbbb").unwrap(); // chat-in-B
    let expected_project_id = Uuid::parse_str("bbbbbbbb-ffff-ffff-ffff-ffffffffffff").unwrap(); // project-B
    let chat = items
        .iter()
        .find_map(|x| match x {
            SoupItem::Chat(soup_chat) if soup_chat.id == expected_chat_id => Some(soup_chat),
            _ => None,
        })
        .expect("The chat should exist");
    assert_eq!(chat.name, "Chat in B");
    assert_eq!(chat.model.as_deref(), Some("gpt-4o"));
    assert_eq!(chat.project_id.as_ref(), Some(&expected_project_id));
}

#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("mixed_items_expanded")
    )
)]
async fn it_should_be_empty(pool: Pool<Postgres>) {
    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
    // Test with non-existent items
    let non_existent_entities = [
        EntityType::Document.with_entity_str("00000000-0000-0000-0000-000000000000"), // non-existent-doc
        EntityType::Chat.with_entity_str("00000000-0000-0000-000000000001"), // non-existent-chat
    ];

    let empty_result = expanded_soup_by_ids(&pool, user_id.copied(), &non_existent_entities)
        .await
        .unwrap();
    assert_eq!(
        empty_result.len(),
        0,
        "Should return empty for non-existent items"
    );

    // Test with only projects (should return empty)
    let project_only_entities = [
        EntityType::Project.with_entity_str("aaaaaaaa-ffff-ffff-ffff-ffffffffffff"), // project-A
        EntityType::Project.with_entity_str("bbbbbbbb-ffff-ffff-ffff-ffffffffffff"), // project-B
    ];

    let project_result = expanded_soup_by_ids(&pool, user_id, &project_only_entities)
        .await
        .unwrap();
    assert_eq!(
        project_result.len(),
        0,
        "Should return empty for project-only request"
    );
}

// Test that no_frecency_expanded_generic_soup excludes items with frecency records
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("no_frecency_items")
    )
)]
async fn test_no_frecency_expanded_filters_out_frecency_items(
    pool: Pool<Postgres>,
) -> anyhow::Result<()> {
    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();

    // Test with UpdatedAt sort - should return only items without frecency
    let items = no_frecency_expanded_generic_soup(
        &pool,
        user_id.copied(),
        20,
        Query::Sort(SimpleSortMethod::UpdatedAt, Frecency),
    )
    .await?;

    // Should get 5 items (2 docs + 2 chats + 1 project without frecency)
    assert_eq!(
        items.len(),
        5,
        "Should only return items without frecency records"
    );

    // Verify the returned items are the ones WITHOUT frecency
    let returned_ids: HashSet<Uuid> = items.iter().map(|item| item.id()).collect();

    let expected_ids: HashSet<Uuid> = [
        "44444444-4444-4444-4444-444444444444", // doc-no-frecency-1
        "55555555-5555-5555-5555-555555555555", // doc-no-frecency-2
        "66666666-6666-6666-6666-666666666666", // chat-no-frecency-1
        "77777777-7777-7777-7777-777777777777", // chat-no-frecency-2
        "88888888-8888-8888-8888-888888888888", // project-no-frecency
    ]
    .iter()
    .map(|&s| Uuid::parse_str(s).unwrap())
    .collect();

    assert_eq!(
        returned_ids, expected_ids,
        "Should only get items without frecency records"
    );

    // Verify none of the frecency items are returned
    let frecency_items = [
        Uuid::parse_str("44444444-ffff-ffff-ffff-ffffffffffff").unwrap(), // doc-with-frecency-1
        Uuid::parse_str("55555555-ffff-ffff-ffff-ffffffffffff").unwrap(), // doc-with-frecency-2
        Uuid::parse_str("66666666-ffff-ffff-ffff-ffffffffffff").unwrap(), // chat-with-frecency-1
        Uuid::parse_str("88888888-ffff-ffff-ffff-ffffffffffff").unwrap(), // project-with-frecency
    ];
    for frecency_id in &frecency_items {
        assert!(
            !returned_ids.contains(frecency_id),
            "Should not return item with frecency: {}",
            frecency_id
        );
    }

    Ok(())
}

// Test sorting methods work correctly for no_frecency query
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("no_frecency_items")
    )
)]
async fn test_no_frecency_expanded_sorting_methods(pool: Pool<Postgres>) -> anyhow::Result<()> {
    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();

    let get_item_ids =
        |items: &[SoupItem]| -> Vec<Uuid> { items.iter().map(|item| item.id()).collect() };

    // Test UpdatedAt sorting
    {
        let items = no_frecency_expanded_generic_soup(
            &pool,
            user_id.copied(),
            20,
            Query::Sort(SimpleSortMethod::UpdatedAt, Frecency),
        )
        .await?;
        assert_eq!(items.len(), 5, "UpdatedAt should return 5 items");

        let item_ids = get_item_ids(&items);
        // Ordered by updatedAt DESC: doc-no-frecency-1 (2/12), doc-no-frecency-2 (2/11),
        // chat-no-frecency-1 (2/08), chat-no-frecency-2 (2/07), project-no-frecency (1/01)
        let expected_ids = vec![
            Uuid::parse_str("44444444-4444-4444-4444-444444444444").unwrap(), // doc-no-frecency-1
            Uuid::parse_str("55555555-5555-5555-5555-555555555555").unwrap(), // doc-no-frecency-2
            Uuid::parse_str("66666666-6666-6666-6666-666666666666").unwrap(), // chat-no-frecency-1
            Uuid::parse_str("77777777-7777-7777-7777-777777777777").unwrap(), // chat-no-frecency-2
            Uuid::parse_str("88888888-8888-8888-8888-888888888888").unwrap(), // project-no-frecency
        ];
        assert_eq!(
            item_ids, expected_ids,
            "Failed to sort correctly by UpdatedAt"
        );
    }

    // Test CreatedAt sorting
    {
        let items = no_frecency_expanded_generic_soup(
            &pool,
            user_id.copied(),
            20,
            Query::Sort(SimpleSortMethod::CreatedAt, Frecency),
        )
        .await?;
        assert_eq!(items.len(), 5, "CreatedAt should return 5 items");

        let item_ids = get_item_ids(&items);
        // Ordered by createdAt DESC: chat-no-frecency-2 (1/18), chat-no-frecency-1 (1/17),
        // doc-no-frecency-2 (1/14), doc-no-frecency-1 (1/13), project-no-frecency (1/01)
        let expected_ids = vec![
            Uuid::parse_str("77777777-7777-7777-7777-777777777777").unwrap(), // chat-no-frecency-2
            Uuid::parse_str("66666666-6666-6666-6666-666666666666").unwrap(), // chat-no-frecency-1
            Uuid::parse_str("55555555-5555-5555-5555-555555555555").unwrap(), // doc-no-frecency-2
            Uuid::parse_str("44444444-4444-4444-4444-444444444444").unwrap(), // doc-no-frecency-1
            Uuid::parse_str("88888888-8888-8888-8888-888888888888").unwrap(), // project-no-frecency
        ];
        assert_eq!(
            item_ids, expected_ids,
            "Failed to sort correctly by CreatedAt"
        );
    }

    // Test ViewedAt sorting
    {
        let items = no_frecency_expanded_generic_soup(
            &pool,
            user_id.copied(),
            20,
            Query::Sort(SimpleSortMethod::ViewedAt, Frecency),
        )
        .await?;
        assert_eq!(items.len(), 5, "ViewedAt should return 5 items");

        let item_ids = get_item_ids(&items);
        // Ordered by UserHistory.updatedAt DESC: doc-no-frecency-1 (3/17), doc-no-frecency-2 (3/16),
        // chat-no-frecency-1 (3/15), chat-no-frecency-2 (3/14), project-no-frecency (3/13)
        let expected_ids = vec![
            Uuid::parse_str("44444444-4444-4444-4444-444444444444").unwrap(), // doc-no-frecency-1
            Uuid::parse_str("55555555-5555-5555-5555-555555555555").unwrap(), // doc-no-frecency-2
            Uuid::parse_str("66666666-6666-6666-6666-666666666666").unwrap(), // chat-no-frecency-1
            Uuid::parse_str("77777777-7777-7777-7777-777777777777").unwrap(), // chat-no-frecency-2
            Uuid::parse_str("88888888-8888-8888-8888-888888888888").unwrap(), // project-no-frecency
        ];
        assert_eq!(
            item_ids, expected_ids,
            "Failed to sort correctly by ViewedAt"
        );
    }

    Ok(())
}

// Test cursor-based pagination for no_frecency query
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("no_frecency_items")
    )
)]
async fn test_no_frecency_expanded_cursor_pagination(pool: Pool<Postgres>) -> anyhow::Result<()> {
    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();

    // Get first page with limit of 2
    let result = no_frecency_expanded_generic_soup(
        &pool,
        user_id.copied(),
        2,
        Query::Sort(SimpleSortMethod::UpdatedAt, Frecency),
    )
    .await?
    .into_iter()
    .paginate_on(2, SimpleSortMethod::UpdatedAt)
    .filter_on(Frecency)
    .into_page();

    assert_eq!(result.items.len(), 2, "Should get 2 items in first page");

    // First two items should be the most recently updated
    match &result.items[0] {
        SoupItem::Document(doc) => {
            assert_eq!(
                doc.id,
                Uuid::parse_str("44444444-4444-4444-4444-444444444444").unwrap(),
                "First item should be doc-no-frecency-1"
            );
        }
        _ => panic!("First item should be a document"),
    }

    match &result.items[1] {
        SoupItem::Document(doc) => {
            assert_eq!(
                doc.id,
                Uuid::parse_str("55555555-5555-5555-5555-555555555555").unwrap(),
                "Second item should be doc-no-frecency-2"
            );
        }
        _ => panic!("Second item should be a document"),
    }

    // Get second page using cursor
    let items = no_frecency_expanded_generic_soup(
        &pool,
        user_id.copied(),
        2,
        Query::new(
            result.next_cursor.map(|s| {
                let decoded = s.decode_json().unwrap();
                models_pagination::Cursor {
                    id: decoded.id,
                    limit: decoded.limit,
                    val: decoded.val,
                    filter: decoded.filter,
                }
            }),
            SimpleSortMethod::UpdatedAt,
            Frecency,
        ),
    )
    .await?;

    assert_eq!(items.len(), 2, "Should get 2 items in second page");

    // Next two items should be the chats
    match &items[0] {
        SoupItem::Chat(chat) => {
            assert_eq!(
                chat.id,
                Uuid::parse_str("66666666-6666-6666-6666-666666666666").unwrap(),
                "Third item should be chat-no-frecency-1"
            );
        }
        _ => panic!("Third item should be a chat"),
    }

    match &items[1] {
        SoupItem::Chat(chat) => {
            assert_eq!(
                chat.id,
                Uuid::parse_str("77777777-7777-7777-7777-777777777777").unwrap(),
                "Fourth item should be chat-no-frecency-2"
            );
        }
        _ => panic!("Fourth item should be a chat"),
    }

    Ok(())
}

#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("mixed_items_expanded")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn empty_ast_returns_same_as_static_query(db: PgPool) {
    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
    let ast_res = expanded_dynamic_cursor_soup(
        &db,
        ExpandedDynamicCursorArgs {
            user_id: user_id.clone(),
            limit: 20,
            cursor: Query::Sort(
                SimpleSortMethod::CreatedAt.into(),
                EntityFilterAst::mock_empty(),
            ),
            exclude_frecency: false,
        },
    )
    .await
    .unwrap();
    let static_res = expanded_generic_cursor_soup(
        &db,
        user_id.copied(),
        20,
        Query::Sort(SimpleSortMethod::CreatedAt, ()),
    )
    .await
    .unwrap();

    // Compare the IDs since SoupItem doesn't implement PartialEq
    let ast_ids: Vec<Uuid> = ast_res.iter().map(|item| item.id()).collect();

    let static_ids: Vec<Uuid> = static_res.iter().map(|item| item.id()).collect();

    assert_eq!(ast_ids, static_ids);
}

// ============================================================================
// EntityFilter Tests with UUID-based fixture
// ============================================================================

// Test filtering by document file type
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_filter_by_document_file_type(db: PgPool) -> anyhow::Result<()> {
    use item_filters::{DocumentFilters, EntityFilters};

    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();

    // Filter for only PDF documents
    let entity_filters = EntityFilters {
        document_filters: DocumentFilters {
            file_types: vec!["pdf".to_string()],
            ..Default::default()
        },
        ..Default::default()
    };

    let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

    let items = expanded_dynamic_cursor_soup(
        &db,
        ExpandedDynamicCursorArgs {
            user_id: user_id.copied(),
            limit: 20,
            cursor: Query::Sort(SimpleSortMethod::CreatedAt.into(), filters),
            exclude_frecency: false,
        },
    )
    .await?;

    // Should get PDF documents (filtered), and all chats and all projects
    let mut pdf_count = 0;
    let mut chat_count = 0;
    let mut project_count = 0;

    for item in &items {
        match item {
            SoupItem::Document(doc) => {
                assert_eq!(
                    doc.file_type.as_deref(),
                    Some("pdf"),
                    "All documents should be PDFs"
                );
                pdf_count += 1;
            }
            SoupItem::Chat(_) => {
                chat_count += 1;
            }
            SoupItem::Project(_) => {
                project_count += 1;
            }
            _ => {
                unimplemented!("encountered an unexpected value");
            }
        }
    }

    // Should get 4 accessible PDF documents (doc-in-C is MD, doc-isolated not accessible)
    assert_eq!(pdf_count, 4, "Should get 4 PDF documents");
    // Should get all 4 accessible chats
    assert_eq!(chat_count, 4, "Should get all chats");
    // Should get all 2 accessible projects
    assert_eq!(project_count, 4, "Should get all projects");

    Ok(())
}

// Test filtering by specific document IDs
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_filter_by_document_ids(db: PgPool) -> anyhow::Result<()> {
    use item_filters::{DocumentFilters, EntityFilters};

    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();

    // Filter for specific document IDs
    let entity_filters = EntityFilters {
        document_filters: DocumentFilters {
            document_ids: vec![
                "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa".to_string(),
                "eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee".to_string(),
            ],
            ..Default::default()
        },
        ..Default::default()
    };

    let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

    let items = expanded_dynamic_cursor_soup(
        &db,
        ExpandedDynamicCursorArgs {
            user_id: user_id.copied(),
            limit: 20,
            cursor: Query::Sort(SimpleSortMethod::CreatedAt.into(), filters),
            exclude_frecency: false,
        },
    )
    .await?;

    // Should get 2 documents (filtered), all chats, and all projects
    let mut document_ids: HashSet<Uuid> = HashSet::new();
    let mut chat_count = 0;
    let mut project_count = 0;

    for item in &items {
        match item {
            SoupItem::Document(d) => {
                document_ids.insert(d.id);
            }
            SoupItem::Chat(_) => {
                chat_count += 1;
            }
            SoupItem::Project(_) => {
                project_count += 1;
            }
            _ => unimplemented!("encountered unexpected entity"),
        }
    }

    assert_eq!(document_ids.len(), 2, "Should get exactly 2 documents");
    assert_eq!(chat_count, 4, "Should get all chats");
    assert_eq!(project_count, 4, "Should get all projects");

    let returned_ids = document_ids;

    let expected_ids: HashSet<Uuid> = [
        "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
        "eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee",
    ]
    .iter()
    .map(|&s| Uuid::parse_str(s).unwrap())
    .collect();

    assert_eq!(
        returned_ids, expected_ids,
        "Should get the correct documents"
    );

    Ok(())
}

// Test filtering by project ID (documents)
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_filter_documents_by_project_id(db: PgPool) -> anyhow::Result<()> {
    use item_filters::{DocumentFilters, EntityFilters};

    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();

    // Filter documents in project-A only
    let entity_filters = EntityFilters {
        document_filters: DocumentFilters {
            project_ids: vec!["11111111-1111-1111-1111-111111111111".to_string()],
            ..Default::default()
        },
        ..Default::default()
    };

    let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

    let items = expanded_dynamic_cursor_soup(
        &db,
        ExpandedDynamicCursorArgs {
            user_id: user_id.copied(),
            limit: 20,
            cursor: Query::Sort(SimpleSortMethod::CreatedAt.into(), filters),
            exclude_frecency: false,
        },
    )
    .await?;

    // Should get 1 document in project-A, all chats, and all projects
    let mut doc_count = 0;
    let mut chat_count = 0;
    let mut project_count = 0;
    let mut found_doc_id = None;

    for item in &items {
        match item {
            SoupItem::Document(doc) => {
                assert_eq!(
                    doc.project_id.as_ref(),
                    Some(&Uuid::parse_str("11111111-1111-1111-1111-111111111111").unwrap())
                );
                found_doc_id = Some(doc.id);
                doc_count += 1;
            }
            SoupItem::Chat(_) => {
                chat_count += 1;
            }
            SoupItem::Project(_) => {
                project_count += 1;
            }
            _ => unimplemented!("encounted unexpected entity"),
        }
    }

    assert_eq!(doc_count, 1, "Should get 1 document in project-A");
    assert_eq!(
        found_doc_id.as_ref(),
        Some(&Uuid::parse_str("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa").unwrap())
    );
    assert_eq!(chat_count, 4, "Should get all chats");
    assert_eq!(project_count, 4, "Should get all projects");

    Ok(())
}

// Test filtering chats by project ID
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_filter_chats_by_project_id(db: PgPool) -> anyhow::Result<()> {
    use item_filters::{ChatFilters, EntityFilters};

    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();

    // Filter chats in project-B
    let entity_filters = EntityFilters {
        chat_filters: ChatFilters {
            project_ids: vec!["22222222-2222-2222-2222-222222222222".to_string()],
            ..Default::default()
        },
        ..Default::default()
    };

    let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

    let items = expanded_dynamic_cursor_soup(
        &db,
        ExpandedDynamicCursorArgs {
            user_id: user_id.copied(),
            limit: 20,
            cursor: Query::Sort(SimpleSortMethod::CreatedAt.into(), filters),
            exclude_frecency: false,
        },
    )
    .await?;

    // Should get 1 chat in project-B, all documents, and all projects
    let mut doc_count = 0;
    let mut chat_count = 0;
    let mut project_count = 0;
    let mut found_chat_id = None;

    for item in &items {
        match item {
            SoupItem::Document(_) => {
                doc_count += 1;
            }
            SoupItem::Chat(chat) => {
                assert_eq!(
                    chat.project_id.as_ref(),
                    Some(&Uuid::parse_str("22222222-2222-2222-2222-222222222222").unwrap())
                );
                found_chat_id = Some(chat.id);
                chat_count += 1;
            }
            SoupItem::Project(_) => {
                project_count += 1;
            }
            _ => unimplemented!("encounted unexpected entity"),
        }
    }

    assert_eq!(chat_count, 1, "Should get 1 chat in project-B");
    assert_eq!(
        found_chat_id.as_ref(),
        Some(&Uuid::parse_str("b2b2b2b2-b2b2-b2b2-b2b2-b2b2b2b2b2b2").unwrap())
    );
    assert_eq!(doc_count, 5, "Should get all documents");
    assert_eq!(project_count, 4, "Should get all projects");

    Ok(())
}

// Test filtering by specific chat IDs
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_filter_by_chat_ids(db: PgPool) -> anyhow::Result<()> {
    use item_filters::{ChatFilters, EntityFilters};

    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();

    // Filter for specific chat IDs
    let entity_filters = EntityFilters {
        chat_filters: ChatFilters {
            chat_ids: vec![
                "a1a1a1a1-a1a1-a1a1-a1a1-a1a1a1a1a1a1".to_string(),
                "d4d4d4d4-d4d4-d4d4-d4d4-d4d4d4d4d4d4".to_string(),
            ],
            ..Default::default()
        },
        ..Default::default()
    };

    let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

    let items = expanded_dynamic_cursor_soup(
        &db,
        ExpandedDynamicCursorArgs {
            user_id: user_id.copied(),
            limit: 20,
            cursor: Query::Sort(SimpleSortMethod::CreatedAt.into(), filters),
            exclude_frecency: false,
        },
    )
    .await?;

    for item in &items {
        if let SoupItem::Chat(chat) = item {
            assert_eq!(chat.model.as_deref(), Some("gpt-4o"));
        }
    }

    // Should get 2 chats (filtered), all documents, and all projects
    let mut chat_ids: HashSet<Uuid> = HashSet::new();
    let mut doc_count = 0;
    let mut project_count = 0;

    for item in &items {
        match item {
            SoupItem::Chat(c) => {
                chat_ids.insert(c.id);
            }
            SoupItem::Document(_) => {
                doc_count += 1;
            }
            SoupItem::Project(_) => {
                project_count += 1;
            }
            _ => unimplemented!("encounted unexpected entity"),
        }
    }

    assert_eq!(chat_ids.len(), 2, "Should get exactly 2 chats");
    assert_eq!(doc_count, 5, "Should get all documents");
    assert_eq!(project_count, 4, "Should get all projects");

    let returned_ids = chat_ids;

    let expected_ids: HashSet<Uuid> = [
        "a1a1a1a1-a1a1-a1a1-a1a1-a1a1a1a1a1a1",
        "d4d4d4d4-d4d4-d4d4-d4d4-d4d4d4d4d4d4",
    ]
    .iter()
    .map(|&s| Uuid::parse_str(s).unwrap())
    .collect();

    assert_eq!(returned_ids, expected_ids, "Should get the correct chats");

    Ok(())
}

// Test filtering projects by specific project IDs
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_filter_by_project_ids(db: PgPool) -> anyhow::Result<()> {
    use item_filters::{EntityFilters, ProjectFilters};

    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();

    // Filter for specific project IDs
    let entity_filters = EntityFilters {
        project_filters: ProjectFilters {
            project_ids: vec![
                "11111111-1111-1111-1111-111111111111".to_string(),
                "44444444-4444-4444-4444-444444444444".to_string(),
            ],
            ..Default::default()
        },
        ..Default::default()
    };

    let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

    let items = expanded_dynamic_cursor_soup(
        &db,
        ExpandedDynamicCursorArgs {
            user_id: user_id.copied(),
            limit: 20,
            cursor: Query::Sort(SimpleSortMethod::CreatedAt.into(), filters),
            exclude_frecency: false,
        },
    )
    .await?;

    // Should get 2 projects (filtered), all documents, and all chats
    let mut project_ids: HashSet<Uuid> = HashSet::new();
    let mut doc_count = 0;
    let mut chat_count = 0;

    for item in &items {
        match item {
            SoupItem::Project(p) => {
                project_ids.insert(p.id);
            }
            SoupItem::Document(_) => {
                doc_count += 1;
            }
            SoupItem::Chat(_) => {
                chat_count += 1;
            }
            _ => unimplemented!("encounted unexpected entity"),
        }
    }

    assert_eq!(
        project_ids.len(),
        1,
        "Should get exactly 1 project (project-B, child of project-A)"
    );
    assert_eq!(doc_count, 5, "Should get all documents");
    assert_eq!(chat_count, 4, "Should get all chats");

    let returned_ids = project_ids;

    let expected_ids: HashSet<Uuid> = [
        "22222222-2222-2222-2222-222222222222", // Project B (parentId = project-A)
    ]
    .iter()
    .map(|&s| Uuid::parse_str(s).unwrap())
    .collect();

    assert_eq!(
        returned_ids, expected_ids,
        "Should get project-B (child of project-A)"
    );

    Ok(())
}

// Test filtering projects by specific project IDs with include_root=true
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_filter_by_project_ids_include_root(db: PgPool) -> anyhow::Result<()> {
    use item_filters::{EntityFilters, ProjectFilters};

    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();

    let entity_filters = EntityFilters {
        project_filters: ProjectFilters {
            project_ids: vec![
                "11111111-1111-1111-1111-111111111111".to_string(),
                "44444444-4444-4444-4444-444444444444".to_string(),
            ],
            include_root: true,
            ..Default::default()
        },
        ..Default::default()
    };

    let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

    let items = expanded_dynamic_cursor_soup(
        &db,
        ExpandedDynamicCursorArgs {
            user_id: user_id.copied(),
            limit: 20,
            cursor: Query::Sort(SimpleSortMethod::CreatedAt.into(), filters),
            exclude_frecency: false,
        },
    )
    .await?;

    let project_ids: HashSet<Uuid> = items
        .iter()
        .filter_map(|item| match item {
            SoupItem::Project(p) => Some(p.id),
            _ => None,
        })
        .collect();

    let expected_ids: HashSet<Uuid> = [
        "11111111-1111-1111-1111-111111111111", // Project A (self)
        "22222222-2222-2222-2222-222222222222", // Project B (child of A)
        "44444444-4444-4444-4444-444444444444", // Project D (self)
    ]
    .iter()
    .map(|&s| Uuid::parse_str(s).unwrap())
    .collect();

    assert_eq!(
        project_ids, expected_ids,
        "include_root=true should match the projects themselves plus their direct children"
    );

    Ok(())
}

// The frontend's "all folders" filter is `pf: !(pid = nil)`. Root projects
// have a NULL parentId, so the negated equality must not drop them.
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_negated_project_id_filter_includes_root_projects(db: PgPool) -> anyhow::Result<()> {
    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();

    // Mirror the folders-view request: docs and chats excluded via nil-id
    // literals, projects included via a negated nil parent-id literal.
    let filters = EntityFilterAst {
        document_filter: Some(Arc::new(Expr::Literal(DocumentLiteral::Id(Uuid::nil())))),
        chat_filter: Some(Arc::new(Expr::Literal(ChatLiteral::ChatId(Uuid::nil())))),
        project_filter: Some(Arc::new(Expr::is_not(Expr::Literal(
            ProjectLiteral::ProjectId(Uuid::nil()),
        )))),
        ..Default::default()
    };

    let items = expanded_dynamic_cursor_soup(
        &db,
        ExpandedDynamicCursorArgs {
            user_id: user_id.copied(),
            limit: 20,
            cursor: Query::Sort(SimpleSortMethod::CreatedAt.into(), filters),
            exclude_frecency: false,
        },
    )
    .await?;

    let project_ids: HashSet<Uuid> = items
        .iter()
        .map(|item| match item {
            SoupItem::Project(p) => p.id,
            other => panic!("expected only projects, got {other:?}"),
        })
        .collect();

    let expected_ids: HashSet<Uuid> = [
        "11111111-1111-1111-1111-111111111111", // Project A (root, NULL parentId)
        "22222222-2222-2222-2222-222222222222", // Project B (child of A)
        "33333333-3333-3333-3333-333333333333", // Project C (child of B)
        "44444444-4444-4444-4444-444444444444", // Project D (root, NULL parentId)
    ]
    .iter()
    .map(|&s| Uuid::parse_str(s).unwrap())
    .collect();

    assert_eq!(
        project_ids, expected_ids,
        "negated pid filter should include root (NULL-parent) projects"
    );

    Ok(())
}

// Test combined filters across multiple entity types
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_combined_entity_filters(db: PgPool) -> anyhow::Result<()> {
    use item_filters::{ChatFilters, DocumentFilters, EntityFilters, ProjectFilters};

    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();

    // Filter for:
    // - Documents in project-A OR project-B
    // - Chat with ID chat-standalone
    // - Project with ID project-D
    let entity_filters = EntityFilters {
        document_filters: DocumentFilters {
            project_ids: vec![
                "11111111-1111-1111-1111-111111111111".to_string(),
                "22222222-2222-2222-2222-222222222222".to_string(),
            ],
            ..Default::default()
        },
        chat_filters: ChatFilters {
            chat_ids: vec!["d4d4d4d4-d4d4-d4d4-d4d4-d4d4d4d4d4d4".to_string()],
            ..Default::default()
        },
        project_filters: ProjectFilters {
            project_ids: vec!["44444444-4444-4444-4444-444444444444".to_string()],
            ..Default::default()
        },
        ..Default::default()
    };

    let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

    let items = expanded_dynamic_cursor_soup(
        &db,
        ExpandedDynamicCursorArgs {
            user_id: user_id.copied(),
            limit: 20,
            cursor: Query::Sort(SimpleSortMethod::CreatedAt.into(), filters),
            exclude_frecency: false,
        },
    )
    .await?;

    // Should get: doc-in-A, doc-in-B, chat-standalone = 3 items
    // Note: project filter for project-D returns 0 projects (no children of project-D exist)
    assert_eq!(
        items.len(),
        3,
        "Should get 3 items total (2 docs + 1 chat, no projects match parentId filter)"
    );

    let mut doc_count = 0;
    let mut chat_count = 0;
    let mut project_count = 0;

    for item in &items {
        match item {
            SoupItem::Document(doc) => {
                doc_count += 1;
                let doc_a = Uuid::parse_str("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa").unwrap();
                let doc_b = Uuid::parse_str("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb").unwrap();
                assert!(
                    doc.id == doc_a || doc.id == doc_b,
                    "Document should be in project-A or project-B"
                );
            }
            SoupItem::Chat(chat) => {
                chat_count += 1;
                assert_eq!(
                    chat.id,
                    Uuid::parse_str("d4d4d4d4-d4d4-d4d4-d4d4-d4d4d4d4d4d4").unwrap(),
                    "Should only get standalone chat"
                );
            }
            SoupItem::Project(_project) => {
                project_count += 1;
            }
            _ => unimplemented!("encounted unexpected entity"),
        }
    }

    assert_eq!(doc_count, 2, "Should get 2 documents");
    assert_eq!(chat_count, 1, "Should get 1 chat");
    assert_eq!(
        project_count, 0,
        "Should get 0 projects (no children of project-D)"
    );

    Ok(())
}

// Test filtering by multiple criteria on documents (AND logic)
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_multiple_filter_criteria_documents(db: PgPool) -> anyhow::Result<()> {
    use item_filters::{DocumentFilters, EntityFilters};

    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();

    // Filter for documents: specific IDs AND in specific projects AND specific file type
    // This uses AND logic across different filter criteria
    let entity_filters = EntityFilters {
        document_filters: DocumentFilters {
            document_ids: vec![
                "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa".to_string(),
                "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb".to_string(),
            ],
            project_ids: vec![
                "11111111-1111-1111-1111-111111111111".to_string(),
                "22222222-2222-2222-2222-222222222222".to_string(),
            ],
            file_types: vec!["pdf".to_string()],
            ..Default::default()
        },
        ..Default::default()
    };

    let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

    let items = expanded_dynamic_cursor_soup(
        &db,
        ExpandedDynamicCursorArgs {
            user_id: user_id.copied(),
            limit: 20,
            cursor: Query::Sort(SimpleSortMethod::CreatedAt.into(), filters),
            exclude_frecency: false,
        },
    )
    .await?;

    // Should get 2 documents matching all criteria, all chats, and all projects
    let mut document_ids: HashSet<Uuid> = HashSet::new();
    let mut chat_count = 0;
    let mut project_count = 0;

    for item in &items {
        match item {
            SoupItem::Document(d) => {
                document_ids.insert(d.id);
            }
            SoupItem::Chat(_) => {
                chat_count += 1;
            }
            SoupItem::Project(_) => {
                project_count += 1;
            }
            _ => unimplemented!("encounted unexpected entity"),
        }
    }

    assert_eq!(
        document_ids.len(),
        2,
        "Should get 2 documents matching all criteria"
    );
    assert_eq!(chat_count, 4, "Should get all chats");
    assert_eq!(project_count, 4, "Should get all projects");

    let returned_ids = document_ids;

    let expected_ids: HashSet<Uuid> = [
        "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
        "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb",
    ]
    .iter()
    .map(|&s| Uuid::parse_str(s).unwrap())
    .collect();

    assert_eq!(returned_ids, expected_ids);

    Ok(())
}

// Test that inaccessible items are filtered out
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_filters_respect_access_control(db: PgPool) -> anyhow::Result<()> {
    use item_filters::{DocumentFilters, EntityFilters};

    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();

    // Try to request doc-isolated (which user doesn't have access to)
    let entity_filters = EntityFilters {
        document_filters: DocumentFilters {
            document_ids: vec!["ffffffff-ffff-ffff-ffff-ffffffffffff".to_string()],
            ..Default::default()
        },
        ..Default::default()
    };

    let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

    let items = expanded_dynamic_cursor_soup(
        &db,
        ExpandedDynamicCursorArgs {
            user_id: user_id.copied(),
            limit: 20,
            cursor: Query::Sort(SimpleSortMethod::CreatedAt.into(), filters),
            exclude_frecency: false,
        },
    )
    .await?;

    // Should get 0 documents (inaccessible), but all chats and all projects
    let mut doc_count = 0;
    let mut chat_count = 0;
    let mut project_count = 0;

    for item in &items {
        match item {
            SoupItem::Document(_) => {
                doc_count += 1;
            }
            SoupItem::Chat(_) => {
                chat_count += 1;
            }
            SoupItem::Project(_) => {
                project_count += 1;
            }
            _ => unimplemented!("encounted unexpected entity"),
        }
    }

    assert_eq!(
        doc_count, 0,
        "Should not return inaccessible documents even when filtered"
    );
    assert_eq!(chat_count, 4, "Should get all chats");
    assert_eq!(project_count, 4, "Should get all projects");

    Ok(())
}

// Test filtering by owner
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_filter_by_owner(db: PgPool) -> anyhow::Result<()> {
    use item_filters::{DocumentFilters, EntityFilters};

    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();

    // Filter documents by owner
    let entity_filters = EntityFilters {
        document_filters: DocumentFilters {
            owners: vec!["macro|[email protected]".to_string()],
            ..Default::default()
        },
        ..Default::default()
    };

    let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

    let items = expanded_dynamic_cursor_soup(
        &db,
        ExpandedDynamicCursorArgs {
            user_id: user_id.copied(),
            limit: 20,
            cursor: Query::Sort(SimpleSortMethod::CreatedAt.into(), filters),
            exclude_frecency: false,
        },
    )
    .await?;

    // Should get 5 documents owned by user-1, all chats, and all projects
    let mut doc_count = 0;
    let mut chat_count = 0;
    let mut project_count = 0;

    for item in &items {
        match item {
            SoupItem::Document(doc) => {
                assert_eq!(
                    doc.owner_id.principal_id(),
                    "macro|[email protected]",
                    "All documents should be owned by user-1"
                );
                doc_count += 1;
            }
            SoupItem::Chat(_) => {
                chat_count += 1;
            }
            SoupItem::Project(_) => {
                project_count += 1;
            }
            _ => unimplemented!("encounted unexpected entity"),
        }
    }

    assert_eq!(doc_count, 5, "Should get 5 documents owned by user-1");
    assert_eq!(chat_count, 4, "Should get all chats");
    assert_eq!(project_count, 4, "Should get all projects");

    Ok(())
}

// Test filtering for non-existent items returns empty
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_filter_non_existent_items(db: PgPool) -> anyhow::Result<()> {
    use item_filters::{ChatFilters, EntityFilters};

    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();

    // Filter for non-existent chat IDs
    let entity_filters = EntityFilters {
        chat_filters: ChatFilters {
            chat_ids: vec!["00000000-0000-0000-0000-000000000000".to_string()],
            ..Default::default()
        },
        ..Default::default()
    };

    let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

    let items = expanded_dynamic_cursor_soup(
        &db,
        ExpandedDynamicCursorArgs {
            user_id: user_id.copied(),
            limit: 20,
            cursor: Query::Sort(SimpleSortMethod::CreatedAt.into(), filters),
            exclude_frecency: false,
        },
    )
    .await?;

    // Should get 0 chats (non-existent), but all documents and all projects
    let mut doc_count = 0;
    let mut chat_count = 0;
    let mut project_count = 0;

    for item in &items {
        match item {
            SoupItem::Document(_) => {
                doc_count += 1;
            }
            SoupItem::Chat(_) => {
                chat_count += 1;
            }
            SoupItem::Project(_) => {
                project_count += 1;
            }
            _ => unimplemented!("encounted unexpected entity"),
        }
    }

    assert_eq!(chat_count, 0, "Should return 0 chats for non-existent IDs");
    assert_eq!(doc_count, 5, "Should get all documents");
    assert_eq!(project_count, 4, "Should get all projects");

    Ok(())
}

// Test cursor-based pagination with document filters
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_cursor_pagination_with_document_filter(db: PgPool) -> anyhow::Result<()> {
    use item_filters::{DocumentFilters, EntityFilters};

    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();

    // Filter for only PDF documents
    let entity_filters = EntityFilters {
        document_filters: DocumentFilters {
            file_types: vec!["pdf".to_string()],
            ..Default::default()
        },
        ..Default::default()
    };

    let filters = EntityFilterAst::new_from_filters(entity_filters.clone())?.unwrap();

    // First page - get 3 items
    let result = expanded_dynamic_cursor_soup(
        &db,
        ExpandedDynamicCursorArgs {
            user_id: user_id.copied(),
            limit: 3,
            cursor: Query::Sort(SimpleSortMethod::CreatedAt.into(), filters.clone()),
            exclude_frecency: false,
        },
    )
    .await?
    .into_iter()
    .paginate_on(3, SimpleSortMethod::CreatedAt)
    .into_page();

    let first_page_items = result.items;
    assert_eq!(first_page_items.len(), 3, "First page should have 3 items");

    // Verify any documents on first page are PDFs
    for item in &first_page_items {
        if let SoupItem::Document(doc) = item {
            assert_eq!(
                doc.file_type.as_deref(),
                Some("pdf"),
                "All documents should be PDFs"
            );
        }
    }

    // Get second page using cursor
    let next_cursor = result.next_cursor.expect("Should have a next cursor");
    let cursor_decoded = next_cursor.decode_json()?;

    let filters_for_cursor = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

    let second_page_items = expanded_dynamic_cursor_soup(
        &db,
        ExpandedDynamicCursorArgs {
            user_id: user_id.copied(),
            limit: 3,
            cursor: Query::Cursor(models_pagination::Cursor {
                id: cursor_decoded.id,
                limit: cursor_decoded.limit,
                val: models_pagination::CursorVal {
                    sort_type: cursor_decoded.val.sort_type.into(),
                    last_val: DynamicSortValue::Timestamp(cursor_decoded.val.last_val),
                },
                filter: filters_for_cursor,
            }),
            exclude_frecency: false,
        },
    )
    .await?;

    assert!(
        !second_page_items.is_empty(),
        "Second page should have items"
    );

    // Verify filter still applies on second page
    for item in &second_page_items {
        match item {
            SoupItem::Document(doc) => {
                assert_eq!(
                    doc.file_type.as_deref(),
                    Some("pdf"),
                    "All documents should be PDFs on second page"
                );
            }
            SoupItem::Chat(_) => {
                // Chats are fine
            }
            SoupItem::Project(_) => {
                // Projects are fine
            }
            _ => unimplemented!("encounted unexpected entity"),
        }
    }

    // Verify no duplicate items between pages
    let first_page_ids: HashSet<Uuid> = first_page_items.iter().map(|item| item.id()).collect();

    for item in &second_page_items {
        let id = item.id();
        assert!(
            !first_page_ids.contains(&id),
            "No item should appear on both pages"
        );
    }

    Ok(())
}

// Test cursor-based pagination with multiple entity filters
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_cursor_pagination_with_combined_filters(db: PgPool) -> anyhow::Result<()> {
    use item_filters::{ChatFilters, DocumentFilters, EntityFilters};

    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();

    // Filter for documents in specific projects AND specific chats
    let entity_filters = EntityFilters {
        document_filters: DocumentFilters {
            project_ids: vec![
                "11111111-1111-1111-1111-111111111111".to_string(),
                "22222222-2222-2222-2222-222222222222".to_string(),
            ],
            ..Default::default()
        },
        chat_filters: ChatFilters {
            chat_ids: vec!["a1a1a1a1-a1a1-a1a1-a1a1-a1a1a1a1a1a1".to_string()],
            ..Default::default()
        },
        ..Default::default()
    };

    let filters = EntityFilterAst::new_from_filters(entity_filters.clone())?.unwrap();

    // First page - get 2 items
    let result = expanded_dynamic_cursor_soup(
        &db,
        ExpandedDynamicCursorArgs {
            user_id: user_id.copied(),
            limit: 2,
            cursor: Query::Sort(SimpleSortMethod::CreatedAt.into(), filters.clone()),
            exclude_frecency: false,
        },
    )
    .await?
    .into_iter()
    .paginate_on(2, SimpleSortMethod::CreatedAt)
    .into_page();

    assert_eq!(result.items.len(), 2, "First page should have 2 items");

    // Get second page
    if let Some(next_cursor) = result.next_cursor {
        let cursor_decoded = next_cursor.decode_json()?;
        let filters_for_cursor = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

        let second_page_items = expanded_dynamic_cursor_soup(
            &db,
            ExpandedDynamicCursorArgs {
                user_id: user_id.copied(),
                limit: 2,
                cursor: Query::Cursor(models_pagination::Cursor {
                    id: cursor_decoded.id,
                    limit: cursor_decoded.limit,
                    val: models_pagination::CursorVal {
                        sort_type: cursor_decoded.val.sort_type.into(),
                        last_val: DynamicSortValue::Timestamp(cursor_decoded.val.last_val),
                    },
                    filter: filters_for_cursor,
                }),
                exclude_frecency: false,
            },
        )
        .await?;

        // Verify filters still apply on second page
        for item in &second_page_items {
            match item {
                SoupItem::Document(doc) => {
                    let project_id = doc.project_id.as_ref().unwrap();
                    let proj_a = Uuid::parse_str("11111111-1111-1111-1111-111111111111").unwrap();
                    let proj_b = Uuid::parse_str("22222222-2222-2222-2222-222222222222").unwrap();
                    assert!(
                        *project_id == proj_a || *project_id == proj_b,
                        "Documents should be in filtered projects"
                    );
                }
                SoupItem::Chat(chat) => {
                    assert_eq!(
                        chat.id,
                        Uuid::parse_str("a1a1a1a1-a1a1-a1a1-a1a1-a1a1a1a1a1a1").unwrap(),
                        "Only the filtered chat should appear"
                    );
                }
                SoupItem::Project(_) => {
                    // All projects should be included
                }
                _ => unimplemented!("encountered an unknown entity"),
            }
        }
    }

    Ok(())
}

// Test cursor pagination maintains filter consistency across pages
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_cursor_pagination_filter_consistency(db: PgPool) -> anyhow::Result<()> {
    use item_filters::{DocumentFilters, EntityFilters};

    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();

    // Filter for documents by specific IDs
    let entity_filters = EntityFilters {
        document_filters: DocumentFilters {
            document_ids: vec![
                "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa".to_string(),
                "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb".to_string(),
            ],
            ..Default::default()
        },
        ..Default::default()
    };

    let filters = EntityFilterAst::new_from_filters(entity_filters.clone())?.unwrap();

    // Collect all items across multiple pages with small page size
    let mut all_items = Vec::new();
    let mut current_query = Query::Sort(SimpleSortMethod::CreatedAt, filters.clone());
    let page_size: u16 = 2;

    loop {
        let result = expanded_dynamic_cursor_soup(
            &db,
            ExpandedDynamicCursorArgs {
                user_id: user_id.copied(),
                limit: page_size,
                cursor: DynamicSort::from_timestamp_query(current_query),
                exclude_frecency: false,
            },
        )
        .await?
        .into_iter()
        .paginate_on(page_size as usize, SimpleSortMethod::CreatedAt)
        .into_page();

        all_items.extend(result.items);

        match result.next_cursor {
            Some(cursor) => {
                let cursor_decoded = cursor.decode_json()?;
                let filters_for_cursor =
                    EntityFilterAst::new_from_filters(entity_filters.clone())?.unwrap();
                current_query = Query::Cursor(models_pagination::Cursor {
                    id: cursor_decoded.id,
                    limit: cursor_decoded.limit,
                    val: cursor_decoded.val,
                    filter: filters_for_cursor,
                });
            }
            None => break,
        }
    }

    // Count filtered documents across all pages
    let mut filtered_doc_count = 0;
    let expected_doc_ids: HashSet<Uuid> = [
        "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
        "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb",
    ]
    .iter()
    .map(|&s| Uuid::parse_str(s).unwrap())
    .collect();

    for item in &all_items {
        if let SoupItem::Document(doc) = item {
            assert!(
                expected_doc_ids.contains(&doc.id),
                "Document {} should be in the filtered set",
                doc.id
            );
            filtered_doc_count += 1;
        }
    }

    // Should get exactly the 2 filtered documents
    assert_eq!(
        filtered_doc_count, 2,
        "Should get exactly 2 filtered documents across all pages"
    );

    // Verify no duplicate items
    let all_ids: Vec<Uuid> = all_items.iter().map(|item| item.id()).collect();

    let unique_ids: HashSet<_> = all_ids.iter().collect();
    assert_eq!(
        all_ids.len(),
        unique_ids.len(),
        "Should have no duplicate items across pages"
    );

    Ok(())
}

// Test cursor pagination with empty filter results on subsequent pages
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_cursor_pagination_with_single_item_filter(db: PgPool) -> anyhow::Result<()> {
    use item_filters::{ChatFilters, EntityFilters};

    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();

    // Filter for a single chat ID
    let entity_filters = EntityFilters {
        chat_filters: ChatFilters {
            chat_ids: vec!["a1a1a1a1-a1a1-a1a1-a1a1-a1a1a1a1a1a1".to_string()],
            ..Default::default()
        },
        ..Default::default()
    };

    let filters = EntityFilterAst::new_from_filters(entity_filters.clone())?.unwrap();

    // Get first page with limit 5
    let result = expanded_dynamic_cursor_soup(
        &db,
        ExpandedDynamicCursorArgs {
            user_id: user_id.copied(),
            limit: 5,
            cursor: Query::Sort(SimpleSortMethod::CreatedAt.into(), filters.clone()),
            exclude_frecency: false,
        },
    )
    .await?
    .into_iter()
    .paginate_on(5, SimpleSortMethod::CreatedAt)
    .into_page();

    // Count the filtered chat in first page
    let expected_chat_id = Uuid::parse_str("a1a1a1a1-a1a1-a1a1-a1a1-a1a1a1a1a1a1").unwrap();
    let chat_count_page1 = result
        .items
        .iter()
        .filter(|item| matches!(item, SoupItem::Chat(c) if c.id == expected_chat_id))
        .count();

    // We should see the filtered chat (it might be on page 1 or later depending on sort order)
    if let Some(next_cursor) = result.next_cursor {
        let cursor_decoded = next_cursor.decode_json()?;
        let filters_for_cursor = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

        let second_page_items = expanded_dynamic_cursor_soup(
            &db,
            ExpandedDynamicCursorArgs {
                user_id: user_id.copied(),
                limit: 5,
                cursor: Query::Cursor(models_pagination::Cursor {
                    id: cursor_decoded.id,
                    limit: cursor_decoded.limit,
                    val: models_pagination::CursorVal {
                        sort_type: cursor_decoded.val.sort_type.into(),
                        last_val: DynamicSortValue::Timestamp(cursor_decoded.val.last_val),
                    },
                    filter: filters_for_cursor,
                }),
                exclude_frecency: false,
            },
        )
        .await?;

        // Count the filtered chat in second page
        let chat_count_page2 = second_page_items
            .iter()
            .filter(|item| matches!(item, SoupItem::Chat(c) if c.id == expected_chat_id))
            .count();

        // Across both pages, we should see exactly 1 instance of the filtered chat
        assert_eq!(
            chat_count_page1 + chat_count_page2,
            1,
            "Should see the filtered chat exactly once across pages"
        );
    } else {
        // If no second page, the chat should be on the first page
        assert_eq!(
            chat_count_page1, 1,
            "Should see the filtered chat on first page"
        );
    }

    Ok(())
}

// Test that exclude_frecency=true works together with AST filters
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("no_frecency_items")
    )
)]
async fn test_dynamic_query_with_ast_and_frecency_exclusion(
    pool: Pool<Postgres>,
) -> anyhow::Result<()> {
    use item_filters::{ChatFilters, DocumentFilters, EntityFilters, ProjectFilters};

    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();

    // Create an AST filter that only returns specific document IDs
    // We'll filter for two docs without frecency and one with frecency
    // We'll also filter out all chats and projects to isolate the document filtering
    let entity_filters = EntityFilters {
        document_filters: DocumentFilters {
            document_ids: vec![
                "44444444-4444-4444-4444-444444444444".to_string(), // doc-no-frecency-1
                "55555555-5555-5555-5555-555555555555".to_string(), // doc-no-frecency-2
                "44444444-ffff-ffff-ffff-ffffffffffff".to_string(), // doc-with-frecency-1
            ],
            ..Default::default()
        },
        // Filter out all chats by using a non-existent ID
        chat_filters: ChatFilters {
            chat_ids: vec!["00000000-0000-0000-0000-000000000000".to_string()],
            ..Default::default()
        },
        // Filter out all projects by using a non-existent ID
        project_filters: ProjectFilters {
            project_ids: vec!["00000000-0000-0000-0000-000000000000".to_string()],
            ..Default::default()
        },
        ..Default::default()
    };

    let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

    // Call with exclude_frecency=true
    let items = expanded_dynamic_cursor_soup(
        &pool,
        ExpandedDynamicCursorArgs {
            user_id: user_id.copied(),
            limit: 20,
            cursor: Query::Sort(SimpleSortMethod::UpdatedAt.into(), filters.clone()),
            exclude_frecency: true,
        },
    )
    .await?;

    // The no_frecency_items fixture has:
    // - 44444444-4444-4444-4444-444444444444 (no frecency) - should be included
    // - 55555555-5555-5555-5555-555555555555 (no frecency) - should be included
    // - 44444444-ffff-ffff-ffff-ffffffffffff (has frecency) - excluded by frecency filter
    // So we should only get 2 documents
    assert_eq!(
        items.len(),
        2,
        "Should return only documents without frecency that match the AST filter"
    );

    // Verify all returned items are documents
    for item in &items {
        assert!(
            matches!(item, SoupItem::Document(_)),
            "All returned items should be documents"
        );
    }

    // Verify the returned document IDs
    let returned_ids: HashSet<Uuid> = items
        .iter()
        .map(|item| match item {
            SoupItem::Document(d) => d.id,
            _ => unreachable!(),
        })
        .collect();

    let expected_ids: HashSet<Uuid> = [
        "44444444-4444-4444-4444-444444444444",
        "55555555-5555-5555-5555-555555555555",
    ]
    .iter()
    .map(|&s| Uuid::parse_str(s).unwrap())
    .collect();

    assert_eq!(
        returned_ids, expected_ids,
        "Should only get documents without frecency records that match AST filter"
    );

    // Now test with exclude_frecency=false to verify both filters work independently
    let items_with_frecency = expanded_dynamic_cursor_soup(
        &pool,
        ExpandedDynamicCursorArgs {
            user_id: user_id.copied(),
            limit: 20,
            cursor: Query::Sort(SimpleSortMethod::UpdatedAt.into(), filters),
            exclude_frecency: false,
        },
    )
    .await?;

    // Should get 3 documents (2 without frecency + 1 with frecency, all matching the AST filter)
    assert_eq!(
        items_with_frecency.len(),
        3,
        "Should return all documents matching AST filter when frecency is not excluded"
    );

    let returned_with_frecency_ids: HashSet<Uuid> = items_with_frecency
        .iter()
        .map(|item| match item {
            SoupItem::Document(d) => d.id,
            _ => unreachable!(),
        })
        .collect();

    let expected_with_frecency_ids: HashSet<Uuid> = [
        "44444444-4444-4444-4444-444444444444",
        "55555555-5555-5555-5555-555555555555",
        "44444444-ffff-ffff-ffff-ffffffffffff",
    ]
    .iter()
    .map(|&s| Uuid::parse_str(s).unwrap())
    .collect();

    assert_eq!(
        returned_with_frecency_ids, expected_with_frecency_ids,
        "Should get all documents matching AST filter regardless of frecency"
    );

    Ok(())
}

// Test filtering documents by importance
// The fixture has:
//   doc-in-A: task, assigned to user-1 (important)
//   doc-in-B: task, assigned to other-user (NOT important)
//   doc-in-C: task, assigned to user-1 (important)
//   doc-in-D: not a task (important - non-tasks are always important)
//   standalone doc: not a task (important)
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_filter_documents_by_importance(db: PgPool) -> anyhow::Result<()> {
    use item_filters::{DocumentFilters, EntityFilters};

    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();

    // importance=true: non-tasks + tasks assigned to user
    {
        let entity_filters = EntityFilters {
            document_filters: DocumentFilters {
                importance: Some(true),
                ..Default::default()
            },
            ..Default::default()
        };

        let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

        let items = expanded_dynamic_cursor_soup(
            &db,
            ExpandedDynamicCursorArgs {
                user_id: user_id.copied(),
                limit: 20,
                cursor: Query::Sort(SimpleSortMethod::CreatedAt.into(), filters),
                exclude_frecency: false,
            },
        )
        .await?;

        let mut doc_ids: HashSet<Uuid> = HashSet::new();
        let mut chat_count = 0;
        let mut project_count = 0;

        for item in &items {
            match item {
                SoupItem::Document(doc) => {
                    doc_ids.insert(doc.id);
                }
                SoupItem::Chat(_) => chat_count += 1,
                SoupItem::Project(_) => project_count += 1,
                _ => unimplemented!("encountered unexpected entity"),
            }
        }

        let expected_ids: HashSet<Uuid> = [
            "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", // doc-in-A (task, assigned to user)
            "cccccccc-cccc-cccc-cccc-cccccccccccc", // doc-in-C (task, assigned to user)
            "dddddddd-dddd-dddd-dddd-dddddddddddd", // doc-in-D (not a task)
            "eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee", // standalone doc (not a task)
        ]
        .iter()
        .map(|&s| Uuid::parse_str(s).unwrap())
        .collect();

        assert_eq!(
            doc_ids, expected_ids,
            "Should get the correct important documents"
        );
        assert_eq!(chat_count, 4, "Should get all chats");
        assert_eq!(project_count, 4, "Should get all projects");
    }

    // importance=false: only tasks NOT assigned to user
    {
        let entity_filters = EntityFilters {
            document_filters: DocumentFilters {
                importance: Some(false),
                ..Default::default()
            },
            ..Default::default()
        };

        let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

        let items = expanded_dynamic_cursor_soup(
            &db,
            ExpandedDynamicCursorArgs {
                user_id: user_id.copied(),
                limit: 20,
                cursor: Query::Sort(SimpleSortMethod::CreatedAt.into(), filters),
                exclude_frecency: false,
            },
        )
        .await?;

        let mut doc_ids: HashSet<Uuid> = HashSet::new();
        let mut chat_count = 0;
        let mut project_count = 0;

        for item in &items {
            match item {
                SoupItem::Document(doc) => {
                    doc_ids.insert(doc.id);
                }
                SoupItem::Chat(_) => chat_count += 1,
                SoupItem::Project(_) => project_count += 1,
                _ => unimplemented!("encountered unexpected entity"),
            }
        }

        let expected_ids: HashSet<Uuid> = [
            "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb", // doc-in-B (task, assigned to other user)
        ]
        .iter()
        .map(|&s| Uuid::parse_str(s).unwrap())
        .collect();

        assert_eq!(
            doc_ids, expected_ids,
            "Should get the correct unimportant documents"
        );
        assert_eq!(chat_count, 4, "Should get all chats");
        assert_eq!(project_count, 4, "Should get all projects");
    }

    Ok(())
}

// Test filtering chats by importance
// importance=true is a no-op (returns all chats)
// importance=false short-circuits to match nothing (1=0)
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_filter_chats_by_importance(db: PgPool) -> anyhow::Result<()> {
    use item_filters::{ChatFilters, EntityFilters};

    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();

    // importance=true: no-op, should return all chats
    {
        let entity_filters = EntityFilters {
            chat_filters: ChatFilters {
                importance: Some(true),
                ..Default::default()
            },
            ..Default::default()
        };

        let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

        let items = expanded_dynamic_cursor_soup(
            &db,
            ExpandedDynamicCursorArgs {
                user_id: user_id.copied(),
                limit: 20,
                cursor: Query::Sort(SimpleSortMethod::CreatedAt.into(), filters),
                exclude_frecency: false,
            },
        )
        .await?;

        let mut doc_count = 0;
        let mut chat_count = 0;
        let mut project_count = 0;

        for item in &items {
            match item {
                SoupItem::Document(_) => doc_count += 1,
                SoupItem::Chat(_) => chat_count += 1,
                SoupItem::Project(_) => project_count += 1,
                _ => unimplemented!("encountered unexpected entity"),
            }
        }

        assert_eq!(chat_count, 4, "importance=true should return all chats");
        assert_eq!(doc_count, 5, "Should get all documents");
        assert_eq!(project_count, 4, "Should get all projects");
    }

    // importance=false: 1=0, should return no chats
    {
        let entity_filters = EntityFilters {
            chat_filters: ChatFilters {
                importance: Some(false),
                ..Default::default()
            },
            ..Default::default()
        };

        let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

        let items = expanded_dynamic_cursor_soup(
            &db,
            ExpandedDynamicCursorArgs {
                user_id: user_id.copied(),
                limit: 20,
                cursor: Query::Sort(SimpleSortMethod::CreatedAt.into(), filters),
                exclude_frecency: false,
            },
        )
        .await?;

        let mut doc_count = 0;
        let mut chat_count = 0;
        let mut project_count = 0;

        for item in &items {
            match item {
                SoupItem::Document(_) => doc_count += 1,
                SoupItem::Chat(_) => chat_count += 1,
                SoupItem::Project(_) => project_count += 1,
                _ => unimplemented!("encountered unexpected entity"),
            }
        }

        assert_eq!(chat_count, 0, "importance=false should return no chats");
        assert_eq!(doc_count, 5, "Should get all documents");
        assert_eq!(project_count, 4, "Should get all projects");
    }

    Ok(())
}

// Test filtering projects by importance
// importance=true is a no-op (returns all projects)
// importance=false short-circuits to match nothing (1=0)
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_filter_projects_by_importance(db: PgPool) -> anyhow::Result<()> {
    use item_filters::{EntityFilters, ProjectFilters};

    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();

    // importance=true: no-op, should return all projects
    {
        let entity_filters = EntityFilters {
            project_filters: ProjectFilters {
                importance: Some(true),
                ..Default::default()
            },
            ..Default::default()
        };

        let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

        let items = expanded_dynamic_cursor_soup(
            &db,
            ExpandedDynamicCursorArgs {
                user_id: user_id.copied(),
                limit: 20,
                cursor: Query::Sort(SimpleSortMethod::CreatedAt.into(), filters),
                exclude_frecency: false,
            },
        )
        .await?;

        let mut doc_count = 0;
        let mut chat_count = 0;
        let mut project_count = 0;

        for item in &items {
            match item {
                SoupItem::Document(_) => doc_count += 1,
                SoupItem::Chat(_) => chat_count += 1,
                SoupItem::Project(_) => project_count += 1,
                _ => unimplemented!("encountered unexpected entity"),
            }
        }

        assert_eq!(
            project_count, 4,
            "importance=true should return all projects"
        );
        assert_eq!(doc_count, 5, "Should get all documents");
        assert_eq!(chat_count, 4, "Should get all chats");
    }

    // importance=false: 1=0, should return no projects
    {
        let entity_filters = EntityFilters {
            project_filters: ProjectFilters {
                importance: Some(false),
                ..Default::default()
            },
            ..Default::default()
        };

        let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

        let items = expanded_dynamic_cursor_soup(
            &db,
            ExpandedDynamicCursorArgs {
                user_id: user_id.copied(),
                limit: 20,
                cursor: Query::Sort(SimpleSortMethod::CreatedAt.into(), filters),
                exclude_frecency: false,
            },
        )
        .await?;

        let mut doc_count = 0;
        let mut chat_count = 0;
        let mut project_count = 0;

        for item in &items {
            match item {
                SoupItem::Document(_) => doc_count += 1,
                SoupItem::Chat(_) => chat_count += 1,
                SoupItem::Project(_) => project_count += 1,
                _ => unimplemented!("encountered unexpected entity"),
            }
        }

        assert_eq!(
            project_count, 0,
            "importance=false should return no projects"
        );
        assert_eq!(doc_count, 5, "Should get all documents");
        assert_eq!(chat_count, 4, "Should get all chats");
    }

    Ok(())
}

/// Test that system properties are populated on SoupItems (Documents and Projects)
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("soup_items_with_properties")
    )
)]
async fn test_bulk_populate_properties_enriches_raw_soup_items(
    pool: Pool<Postgres>,
) -> anyhow::Result<()> {
    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();

    let items = expanded_dynamic_cursor_soup(
        &pool,
        ExpandedDynamicCursorArgs {
            user_id: user_id.copied(),
            limit: 20,
            cursor: Query::Sort(
                SimpleSortMethod::UpdatedAt.into(),
                EntityFilterAst::mock_empty(),
            ),
            exclude_frecency: false,
        },
    )
    .await?;
    let items = populate_properties(&pool, user_id, items).await?;

    // Should get 2 documents and 2 projects (A and B)
    assert!(!items.is_empty(), "Should return some items");

    // Check that Document in A has properties populated
    let doc_a_uuid = Uuid::parse_str("11111111-aaaa-aaaa-aaaa-aaaaaaaaaaaa").unwrap();
    let doc_a = items
        .iter()
        .find(|item| item.id() == doc_a_uuid)
        .expect("doc a shoul exist");

    let doc = unwrap_enum!(doc_a, SoupItem::Document);
    assert_eq!(
        doc.extra.properties.len(),
        2,
        "Document in A should have 2 properties"
    );

    // Check that Document in B has properties populated
    let doc_b_uuid = Uuid::parse_str("11111111-bbbb-bbbb-bbbb-bbbbbbbbbbbb").unwrap();
    let doc_b = items.iter().find(|item| item.id() == doc_b_uuid);
    assert!(doc_b.is_some(), "Should find Document in B");

    let doc = unwrap_enum!(doc_b, Some => SoupItem::Document);
    // Document in B has Priority and Due Date properties
    assert_eq!(
        doc.extra.properties.len(),
        2,
        "Document in B should have 2 properties"
    );

    // Check that Project A has properties populated
    let proj_a_uuid = Uuid::parse_str("aaaaaaaa-ffff-ffff-ffff-ffffffffffff").unwrap();
    let proj_a = items.iter().find(|item| item.id() == proj_a_uuid);
    assert!(proj_a.is_some(), "Should find Project A");

    let proj = unwrap_enum!(proj_a, Some => SoupItem::Project);
    assert!(
        !proj.extra.properties.is_empty(),
        "Project A should have properties populated"
    );
    // Project A has Priority property
    assert_eq!(
        proj.extra.properties.len(),
        1,
        "Project A should have 1 property"
    );

    // Check that Project B has no properties (none were added in fixture)
    let proj_b_uuid = Uuid::parse_str("bbbbbbbb-ffff-ffff-ffff-ffffffffffff").unwrap();
    let proj_b = items.iter().find(|item| item.id() == proj_b_uuid);
    assert!(proj_b.is_some(), "Should find Project B");

    let proj = unwrap_enum!(proj_b, Some => SoupItem::Project);
    // Project B has no properties in the fixture, so it should be empty or None
    let props_count = proj.extra.properties.len();
    assert_eq!(props_count, 0, "Project B should have 0 properties");

    Ok(())
}

// ============================================================================
// Exhaustive expanded_generic_cursor_soup tests
// ============================================================================

// Fixture constants for expanded_cursor_soup_exhaustive
const EX_PROJECT_ROOT: &str = "aa000001-ffff-ffff-ffff-ffffffffffff";
const EX_PROJECT_MID: &str = "aa000002-ffff-ffff-ffff-ffffffffffff";
const EX_PROJECT_DEEP: &str = "aa000003-ffff-ffff-ffff-ffffffffffff";
const EX_PROJECT_ISOLATED: &str = "aa000004-ffff-ffff-ffff-ffffffffffff";
const EX_DOC_ROOT: &str = "bb000001-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
const EX_DOC_MID: &str = "bb000002-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
const EX_DOC_DEEP: &str = "bb000003-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
const EX_DOC_STANDALONE: &str = "bb000004-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
const EX_DOC_DELETED: &str = "bb000005-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
const EX_DOC_TASK_COMPLETED: &str = "bb000006-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
const EX_DOC_TASK_INCOMPLETE: &str = "bb000007-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
const EX_DOC_TASK_NO_STATUS: &str = "bb000008-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
const EX_DOC_ISOLATED: &str = "bb000009-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
const EX_CHAT_ROOT: &str = "cc000001-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
const EX_CHAT_STANDALONE: &str = "cc000002-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
const EX_CHAT_DELETED: &str = "cc000003-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
const EX_CHAT_ISOLATED: &str = "cc000004-aaaa-aaaa-aaaa-aaaaaaaaaaaa";

fn uuid(s: &str) -> Uuid {
    Uuid::parse_str(s).unwrap()
}

/// Deleted documents and chats must not appear in results
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("expanded_cursor_soup_exhaustive")
    )
)]
async fn test_exhaustive_deleted_items_excluded(pool: Pool<Postgres>) -> anyhow::Result<()> {
    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
    let items = expanded_generic_cursor_soup(
        &pool,
        user_id.copied(),
        50,
        Query::Sort(SimpleSortMethod::UpdatedAt, ()),
    )
    .await?;

    let ids: HashSet<Uuid> = items.iter().map(|i| i.id()).collect();

    assert!(
        !ids.contains(&uuid(EX_DOC_DELETED)),
        "Deleted document must not appear"
    );
    assert!(
        !ids.contains(&uuid(EX_CHAT_DELETED)),
        "Deleted chat must not appear"
    );

    Ok(())
}

/// Items in isolated project must not appear for user-1.
/// Isolated project, doc, and chat should all be excluded.
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("expanded_cursor_soup_exhaustive")
    )
)]
async fn test_exhaustive_access_control_excludes_isolated(
    pool: Pool<Postgres>,
) -> anyhow::Result<()> {
    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
    let items = expanded_generic_cursor_soup(
        &pool,
        user_id.copied(),
        50,
        Query::Sort(SimpleSortMethod::UpdatedAt, ()),
    )
    .await?;

    let ids: HashSet<Uuid> = items.iter().map(|i| i.id()).collect();

    assert!(
        !ids.contains(&uuid(EX_PROJECT_ISOLATED)),
        "Isolated project must not appear for user-1"
    );
    assert!(
        !ids.contains(&uuid(EX_DOC_ISOLATED)),
        "Isolated doc must not appear for user-1"
    );
    assert!(
        !ids.contains(&uuid(EX_CHAT_ISOLATED)),
        "Isolated chat must not appear for user-1"
    );

    Ok(())
}

/// User-2 only has access to the isolated project and its children.
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("expanded_cursor_soup_exhaustive")
    )
)]
async fn test_exhaustive_user_isolation(pool: Pool<Postgres>) -> anyhow::Result<()> {
    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
    let items = expanded_generic_cursor_soup(
        &pool,
        user_id.copied(),
        50,
        Query::Sort(SimpleSortMethod::UpdatedAt, ()),
    )
    .await?;

    let ids: HashSet<Uuid> = items.iter().map(|i| i.id()).collect();

    let expected: HashSet<Uuid> = [EX_PROJECT_ISOLATED, EX_DOC_ISOLATED, EX_CHAT_ISOLATED]
        .iter()
        .map(|s| uuid(s))
        .collect();

    assert_eq!(
        ids, expected,
        "User-2 should only see isolated project, its doc, and its chat"
    );

    Ok(())
}

/// Documents 3 levels deep in the hierarchy (root -> mid -> deep) are accessible
/// through inherited project permissions.
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("expanded_cursor_soup_exhaustive")
    )
)]
async fn test_exhaustive_deep_hierarchy_access(pool: Pool<Postgres>) -> anyhow::Result<()> {
    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
    let items = expanded_generic_cursor_soup(
        &pool,
        user_id.copied(),
        50,
        Query::Sort(SimpleSortMethod::UpdatedAt, ()),
    )
    .await?;

    let ids: HashSet<Uuid> = items.iter().map(|i| i.id()).collect();

    // All hierarchy items should be present
    assert!(ids.contains(&uuid(EX_PROJECT_ROOT)), "project-root");
    assert!(ids.contains(&uuid(EX_PROJECT_MID)), "project-mid");
    assert!(ids.contains(&uuid(EX_PROJECT_DEEP)), "project-deep");
    assert!(ids.contains(&uuid(EX_DOC_ROOT)), "doc-in-root");
    assert!(ids.contains(&uuid(EX_DOC_MID)), "doc-in-mid");
    assert!(ids.contains(&uuid(EX_DOC_DEEP)), "doc-in-deep (3 levels)");
    assert!(ids.contains(&uuid(EX_CHAT_ROOT)), "chat-in-root");

    // Standalone items with direct access
    assert!(ids.contains(&uuid(EX_DOC_STANDALONE)), "doc-standalone");
    assert!(ids.contains(&uuid(EX_CHAT_STANDALONE)), "chat-standalone");

    // Task documents in root
    assert!(
        ids.contains(&uuid(EX_DOC_TASK_COMPLETED)),
        "doc-task-completed"
    );
    assert!(
        ids.contains(&uuid(EX_DOC_TASK_INCOMPLETE)),
        "doc-task-incomplete"
    );
    assert!(
        ids.contains(&uuid(EX_DOC_TASK_NO_STATUS)),
        "doc-task-no-status"
    );

    // Total should be 12: 3 projects + 7 docs + 2 chats
    assert_eq!(items.len(), 12, "User-1 should see exactly 12 items");

    Ok(())
}

/// Task documents should have correct is_completed values:
/// - Completed task -> Some(Task { is_completed: true })
/// - Incomplete task -> Some(Task { is_completed: false })
/// - Task with no status property -> Some(Task { is_completed: false })
/// - Non-task documents -> sub_type is None
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("expanded_cursor_soup_exhaustive")
    )
)]
async fn test_exhaustive_task_completion_status(pool: Pool<Postgres>) -> anyhow::Result<()> {
    use models_soup::document::SoupDocumentSubType;

    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
    let items = expanded_generic_cursor_soup(
        &pool,
        user_id.copied(),
        50,
        Query::Sort(SimpleSortMethod::UpdatedAt, ()),
    )
    .await?;

    let items_map: std::collections::HashMap<Uuid, &SoupItem> =
        items.iter().map(|i| (i.id(), i)).collect();

    // Completed task
    let doc = unwrap_enum!(items_map[&uuid(EX_DOC_TASK_COMPLETED)], SoupItem::Document);
    match &doc.sub_type {
        Some(SoupDocumentSubType::Task { is_completed }) => {
            assert!(is_completed, "Completed task should have is_completed=true");
        }
        other => panic!("Expected Task sub_type, got {:?}", other),
    }

    // Incomplete task (status = In Progress)
    let doc = unwrap_enum!(items_map[&uuid(EX_DOC_TASK_INCOMPLETE)], SoupItem::Document);
    match &doc.sub_type {
        Some(SoupDocumentSubType::Task { is_completed }) => {
            assert!(
                !is_completed,
                "Incomplete task should have is_completed=false"
            );
        }
        other => panic!("Expected Task sub_type, got {:?}", other),
    }

    // Task with no status property
    let doc = unwrap_enum!(items_map[&uuid(EX_DOC_TASK_NO_STATUS)], SoupItem::Document);
    match &doc.sub_type {
        Some(SoupDocumentSubType::Task { is_completed }) => {
            assert!(
                !is_completed,
                "Task with no status should have is_completed=false"
            );
        }
        other => panic!("Expected Task sub_type, got {:?}", other),
    }

    // Non-task document
    let doc = unwrap_enum!(items_map[&uuid(EX_DOC_ROOT)], SoupItem::Document);
    assert!(
        doc.sub_type.is_none(),
        "Non-task document should have sub_type=None"
    );

    Ok(())
}

/// Test UpdatedAt sort ordering.
/// Items sorted by updatedAt DESC, tiebreaker by updatedAt DESC.
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("expanded_cursor_soup_exhaustive")
    )
)]
async fn test_exhaustive_sort_updated_at(pool: Pool<Postgres>) -> anyhow::Result<()> {
    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
    let items = expanded_generic_cursor_soup(
        &pool,
        user_id.copied(),
        50,
        Query::Sort(SimpleSortMethod::UpdatedAt, ()),
    )
    .await?;

    let ids: Vec<Uuid> = items.iter().map(|i| i.id()).collect();

    // updatedAt DESC:
    // chat-standalone    2024-03-11
    // doc-in-deep        2024-03-10
    // project-deep       2024-02-12
    // project-mid        2024-02-11
    // project-root       2024-02-10
    // chat-in-root       2024-02-08
    // doc-task-no-status 2024-02-07
    // doc-task-incomplete 2024-02-06
    // doc-task-completed 2024-02-05
    // doc-standalone     2024-02-04
    // doc-in-mid         2024-02-02
    // doc-in-root        2024-02-01
    let expected: Vec<Uuid> = [
        EX_CHAT_STANDALONE,
        EX_DOC_DEEP,
        EX_PROJECT_DEEP,
        EX_PROJECT_MID,
        EX_PROJECT_ROOT,
        EX_CHAT_ROOT,
        EX_DOC_TASK_NO_STATUS,
        EX_DOC_TASK_INCOMPLETE,
        EX_DOC_TASK_COMPLETED,
        EX_DOC_STANDALONE,
        EX_DOC_MID,
        EX_DOC_ROOT,
    ]
    .iter()
    .map(|s| uuid(s))
    .collect();

    assert_eq!(ids, expected, "UpdatedAt sort order is wrong");

    Ok(())
}

/// Test CreatedAt sort ordering.
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("expanded_cursor_soup_exhaustive")
    )
)]
async fn test_exhaustive_sort_created_at(pool: Pool<Postgres>) -> anyhow::Result<()> {
    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
    let items = expanded_generic_cursor_soup(
        &pool,
        user_id.copied(),
        50,
        Query::Sort(SimpleSortMethod::CreatedAt, ()),
    )
    .await?;

    let ids: Vec<Uuid> = items.iter().map(|i| i.id()).collect();

    // createdAt DESC:
    // project-deep       2024-01-12
    // project-mid        2024-01-11
    // project-root       2024-01-10
    // chat-standalone    2024-01-09
    // chat-in-root       2024-01-08
    // doc-task-no-status 2024-01-07
    // doc-task-incomplete 2024-01-06
    // doc-task-completed 2024-01-05
    // doc-standalone     2024-01-04
    // doc-in-deep        2024-01-03
    // doc-in-mid         2024-01-02
    // doc-in-root        2024-01-01
    let expected: Vec<Uuid> = [
        EX_PROJECT_DEEP,
        EX_PROJECT_MID,
        EX_PROJECT_ROOT,
        EX_CHAT_STANDALONE,
        EX_CHAT_ROOT,
        EX_DOC_TASK_NO_STATUS,
        EX_DOC_TASK_INCOMPLETE,
        EX_DOC_TASK_COMPLETED,
        EX_DOC_STANDALONE,
        EX_DOC_DEEP,
        EX_DOC_MID,
        EX_DOC_ROOT,
    ]
    .iter()
    .map(|s| uuid(s))
    .collect();

    assert_eq!(ids, expected, "CreatedAt sort order is wrong");

    Ok(())
}

/// Test ViewedAt sort ordering.
/// Items with history sorted by UserHistory.updatedAt DESC.
/// Items without history get sort_ts = epoch, tiebroken by id DESC.
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("expanded_cursor_soup_exhaustive")
    )
)]
async fn test_exhaustive_sort_viewed_at(pool: Pool<Postgres>) -> anyhow::Result<()> {
    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
    let items = expanded_generic_cursor_soup(
        &pool,
        user_id.copied(),
        50,
        Query::Sort(SimpleSortMethod::ViewedAt, ()),
    )
    .await?;

    let ids: Vec<Uuid> = items.iter().map(|i| i.id()).collect();

    // With history (by UserHistory.updatedAt DESC):
    //   doc-in-mid         2024-03-08
    //   doc-task-no-status 2024-03-07
    //   doc-standalone     2024-03-06
    //   doc-in-root        2024-03-05
    //   doc-task-completed 2024-03-04
    //   chat-in-root       2024-03-03
    //   project-root       2024-03-02
    //   project-deep       2024-03-01
    // Without history (sort_ts = epoch, tiebroken by id DESC):
    //   chat-standalone     (epoch, id cc000002)
    //   doc-task-incomplete (epoch, id bb000007)
    //   doc-in-deep         (epoch, id bb000003)
    //   project-mid         (epoch, id aa000002)
    let expected: Vec<Uuid> = [
        EX_DOC_MID,
        EX_DOC_TASK_NO_STATUS,
        EX_DOC_STANDALONE,
        EX_DOC_ROOT,
        EX_DOC_TASK_COMPLETED,
        EX_CHAT_ROOT,
        EX_PROJECT_ROOT,
        EX_PROJECT_DEEP,
        EX_CHAT_STANDALONE,
        EX_DOC_TASK_INCOMPLETE,
        EX_DOC_DEEP,
        EX_PROJECT_MID,
    ]
    .iter()
    .map(|s| uuid(s))
    .collect();

    assert_eq!(ids, expected, "ViewedAt sort order is wrong");

    Ok(())
}

/// Test ViewedUpdated sort ordering.
/// Uses COALESCE(uh.updatedAt, item.updatedAt) instead of falling back to epoch.
/// This means items without history use their updatedAt, which produces a
/// different ordering than ViewedAt for items with high updatedAt but no history.
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("expanded_cursor_soup_exhaustive")
    )
)]
async fn test_exhaustive_sort_viewed_updated(pool: Pool<Postgres>) -> anyhow::Result<()> {
    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
    let items = expanded_generic_cursor_soup(
        &pool,
        user_id.copied(),
        50,
        Query::Sort(SimpleSortMethod::ViewedUpdated, ()),
    )
    .await?;

    let ids: Vec<Uuid> = items.iter().map(|i| i.id()).collect();

    // COALESCE(uh.updatedAt, item.updatedAt) DESC:
    //   chat-standalone      no history -> updatedAt 2024-03-11
    //   doc-in-deep          no history -> updatedAt 2024-03-10
    //   doc-in-mid           history 2024-03-08
    //   doc-task-no-status   history 2024-03-07
    //   doc-standalone       history 2024-03-06
    //   doc-in-root          history 2024-03-05
    //   doc-task-completed   history 2024-03-04
    //   chat-in-root         history 2024-03-03
    //   project-root         history 2024-03-02
    //   project-deep         history 2024-03-01
    //   project-mid          no history -> updatedAt 2024-02-11
    //   doc-task-incomplete  no history -> updatedAt 2024-02-06
    let expected: Vec<Uuid> = [
        EX_CHAT_STANDALONE,
        EX_DOC_DEEP,
        EX_DOC_MID,
        EX_DOC_TASK_NO_STATUS,
        EX_DOC_STANDALONE,
        EX_DOC_ROOT,
        EX_DOC_TASK_COMPLETED,
        EX_CHAT_ROOT,
        EX_PROJECT_ROOT,
        EX_PROJECT_DEEP,
        EX_PROJECT_MID,
        EX_DOC_TASK_INCOMPLETE,
    ]
    .iter()
    .map(|s| uuid(s))
    .collect();

    assert_eq!(ids, expected, "ViewedUpdated sort order is wrong");

    // Verify viewed_updated produces a DIFFERENT order than viewed_at
    let viewed_at_items = expanded_generic_cursor_soup(
        &pool,
        user_id.copied(),
        50,
        Query::Sort(SimpleSortMethod::ViewedAt, ()),
    )
    .await?;
    let viewed_at_ids: Vec<Uuid> = viewed_at_items.iter().map(|i| i.id()).collect();

    assert_ne!(
        ids, viewed_at_ids,
        "ViewedUpdated and ViewedAt should produce different orderings"
    );

    Ok(())
}

/// Walking through all items one at a time with limit=1 should yield every item
/// exactly once, in the correct order, with no duplicates.
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("expanded_cursor_soup_exhaustive")
    )
)]
async fn test_exhaustive_paginate_one_at_a_time(pool: Pool<Postgres>) -> anyhow::Result<()> {
    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
    let sort = SimpleSortMethod::UpdatedAt;

    let mut all_ids: Vec<Uuid> = Vec::new();
    let mut current_query: Query<Uuid, SimpleSortMethod, ()> = Query::Sort(sort, ());

    loop {
        let result = expanded_generic_cursor_soup(&pool, user_id.copied(), 1, current_query)
            .await?
            .into_iter()
            .paginate_on(1, sort)
            .into_page();

        all_ids.extend(result.items.iter().map(|i| i.id()));

        match result.next_cursor {
            Some(cursor) => {
                let decoded = cursor.decode_json().unwrap();
                current_query = Query::new(
                    Some(models_pagination::Cursor {
                        id: decoded.id,
                        limit: decoded.limit,
                        val: decoded.val,
                        filter: decoded.filter,
                    }),
                    sort,
                    (),
                );
            }
            None => break,
        }
    }

    assert_eq!(all_ids.len(), 12, "Should walk through all 12 items");

    let unique: HashSet<Uuid> = all_ids.iter().copied().collect();
    assert_eq!(
        unique.len(),
        12,
        "No duplicates when paginating one at a time"
    );

    // Verify order matches a single large fetch
    let all_at_once =
        expanded_generic_cursor_soup(&pool, user_id.copied(), 50, Query::Sort(sort, ())).await?;
    let expected_ids: Vec<Uuid> = all_at_once.iter().map(|i| i.id()).collect();
    assert_eq!(
        all_ids, expected_ids,
        "Paginated order should match single-fetch order"
    );

    Ok(())
}

/// Full pagination walk with page size of 3. Verifies no duplicates
/// and correct total count across all pages for each sort method.
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("expanded_cursor_soup_exhaustive")
    )
)]
async fn test_exhaustive_paginate_all_sort_methods(pool: Pool<Postgres>) -> anyhow::Result<()> {
    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
    let page_size: u16 = 3;

    for sort in [
        SimpleSortMethod::ViewedAt,
        SimpleSortMethod::UpdatedAt,
        SimpleSortMethod::CreatedAt,
        SimpleSortMethod::ViewedUpdated,
    ] {
        let mut all_ids: Vec<Uuid> = Vec::new();
        let mut current_query: Query<Uuid, SimpleSortMethod, ()> = Query::Sort(sort, ());
        let mut _page_count = 0;

        loop {
            let result =
                expanded_generic_cursor_soup(&pool, user_id.copied(), page_size, current_query)
                    .await?
                    .into_iter()
                    .paginate_on(page_size as usize, sort)
                    .into_page();

            all_ids.extend(result.items.iter().map(|i| i.id()));
            _page_count += 1;

            match result.next_cursor {
                Some(cursor) => {
                    let decoded = cursor.decode_json().unwrap();
                    current_query = Query::new(
                        Some(models_pagination::Cursor {
                            id: decoded.id,
                            limit: decoded.limit,
                            val: decoded.val,
                            filter: decoded.filter,
                        }),
                        sort,
                        (),
                    );
                }
                None => break,
            }
        }

        assert_eq!(
            all_ids.len(),
            12,
            "Sort {:?}: should get all 12 items across pages",
            sort
        );

        let unique: HashSet<Uuid> = all_ids.iter().copied().collect();
        assert_eq!(
            unique.len(),
            12,
            "Sort {:?}: no duplicates across pages",
            sort
        );
    }

    Ok(())
}

/// Limit larger than total items returns everything in one page.
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("expanded_cursor_soup_exhaustive")
    )
)]
async fn test_exhaustive_limit_larger_than_total(pool: Pool<Postgres>) -> anyhow::Result<()> {
    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
    let items = expanded_generic_cursor_soup(
        &pool,
        user_id.copied(),
        1000,
        Query::Sort(SimpleSortMethod::UpdatedAt, ()),
    )
    .await?;

    assert_eq!(
        items.len(),
        12,
        "Should return all 12 items with large limit"
    );

    Ok(())
}

/// Standalone items (no project) are correctly returned with project_id = None.
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("expanded_cursor_soup_exhaustive")
    )
)]
async fn test_exhaustive_standalone_items_have_no_project(
    pool: Pool<Postgres>,
) -> anyhow::Result<()> {
    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
    let items = expanded_generic_cursor_soup(
        &pool,
        user_id.copied(),
        50,
        Query::Sort(SimpleSortMethod::UpdatedAt, ()),
    )
    .await?;

    let items_map: std::collections::HashMap<Uuid, &SoupItem> =
        items.iter().map(|i| (i.id(), i)).collect();

    let doc = unwrap_enum!(items_map[&uuid(EX_DOC_STANDALONE)], SoupItem::Document);
    assert_eq!(
        doc.project_id, None,
        "Standalone doc should have no project"
    );
    assert_eq!(doc.name, "Doc Standalone");

    let chat = unwrap_enum!(items_map[&uuid(EX_CHAT_STANDALONE)], SoupItem::Chat);
    assert_eq!(
        chat.project_id, None,
        "Standalone chat should have no project"
    );
    assert_eq!(chat.name, "Chat Standalone");

    Ok(())
}

/// Documents in projects should carry the correct project_id, and projects
/// in the hierarchy should carry the correct parent_id.
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("expanded_cursor_soup_exhaustive")
    )
)]
async fn test_exhaustive_hierarchy_project_ids(pool: Pool<Postgres>) -> anyhow::Result<()> {
    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
    let items = expanded_generic_cursor_soup(
        &pool,
        user_id.copied(),
        50,
        Query::Sort(SimpleSortMethod::UpdatedAt, ()),
    )
    .await?;

    let items_map: std::collections::HashMap<Uuid, &SoupItem> =
        items.iter().map(|i| (i.id(), i)).collect();

    // doc-in-deep should reference project-deep
    let doc = unwrap_enum!(items_map[&uuid(EX_DOC_DEEP)], SoupItem::Document);
    assert_eq!(doc.project_id, Some(uuid(EX_PROJECT_DEEP)));

    // doc-in-mid should reference project-mid
    let doc = unwrap_enum!(items_map[&uuid(EX_DOC_MID)], SoupItem::Document);
    assert_eq!(doc.project_id, Some(uuid(EX_PROJECT_MID)));

    // project-deep should have parent_id = project-mid
    let proj = unwrap_enum!(items_map[&uuid(EX_PROJECT_DEEP)], SoupItem::Project);
    assert_eq!(proj.parent_id, Some(uuid(EX_PROJECT_MID)));

    // project-mid should have parent_id = project-root
    let proj = unwrap_enum!(items_map[&uuid(EX_PROJECT_MID)], SoupItem::Project);
    assert_eq!(proj.parent_id, Some(uuid(EX_PROJECT_ROOT)));

    // project-root should have parent_id = None
    let proj = unwrap_enum!(items_map[&uuid(EX_PROJECT_ROOT)], SoupItem::Project);
    assert_eq!(proj.parent_id, None);

    Ok(())
}

/// Items correctly report their viewed_at from UserHistory.
/// Items without history should have viewed_at = None.
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("expanded_cursor_soup_exhaustive")
    )
)]
async fn test_exhaustive_viewed_at_values(pool: Pool<Postgres>) -> anyhow::Result<()> {
    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
    let items = expanded_generic_cursor_soup(
        &pool,
        user_id.copied(),
        50,
        Query::Sort(SimpleSortMethod::UpdatedAt, ()),
    )
    .await?;

    let items_map: std::collections::HashMap<Uuid, &SoupItem> =
        items.iter().map(|i| (i.id(), i)).collect();

    // Items WITH history should have Some(viewed_at)
    let doc = unwrap_enum!(items_map[&uuid(EX_DOC_MID)], SoupItem::Document);
    assert!(
        doc.viewed_at.is_some(),
        "doc-in-mid has history and should have viewed_at"
    );

    let doc = unwrap_enum!(items_map[&uuid(EX_DOC_ROOT)], SoupItem::Document);
    assert!(
        doc.viewed_at.is_some(),
        "doc-in-root has history and should have viewed_at"
    );

    // Items WITHOUT history should have None
    let doc = unwrap_enum!(items_map[&uuid(EX_DOC_DEEP)], SoupItem::Document);
    assert!(
        doc.viewed_at.is_none(),
        "doc-in-deep has no history and should have viewed_at=None"
    );

    let chat = unwrap_enum!(items_map[&uuid(EX_CHAT_STANDALONE)], SoupItem::Chat);
    assert!(
        chat.viewed_at.is_none(),
        "chat-standalone has no history and should have viewed_at=None"
    );

    let proj = unwrap_enum!(items_map[&uuid(EX_PROJECT_MID)], SoupItem::Project);
    assert!(
        proj.viewed_at.is_none(),
        "project-mid has no history and should have viewed_at=None"
    );

    Ok(())
}

/// Documents should carry their file_type and sha from the latest DocumentInstance.
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("expanded_cursor_soup_exhaustive")
    )
)]
async fn test_exhaustive_document_fields(pool: Pool<Postgres>) -> anyhow::Result<()> {
    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
    let items = expanded_generic_cursor_soup(
        &pool,
        user_id.copied(),
        50,
        Query::Sort(SimpleSortMethod::UpdatedAt, ()),
    )
    .await?;

    let items_map: std::collections::HashMap<Uuid, &SoupItem> =
        items.iter().map(|i| (i.id(), i)).collect();

    let doc = unwrap_enum!(items_map[&uuid(EX_DOC_ROOT)], SoupItem::Document);
    assert_eq!(doc.file_type.as_deref(), Some("pdf"));
    assert_eq!(doc.sha.as_deref(), Some("sha-root"));
    assert_eq!(doc.name, "Doc In Root");

    let doc = unwrap_enum!(items_map[&uuid(EX_DOC_MID)], SoupItem::Document);
    assert_eq!(doc.file_type.as_deref(), Some("docx"));
    assert_eq!(doc.sha.as_deref(), Some("sha-mid"));

    let doc = unwrap_enum!(items_map[&uuid(EX_DOC_STANDALONE)], SoupItem::Document);
    assert_eq!(doc.file_type.as_deref(), Some("txt"));
    assert_eq!(doc.sha.as_deref(), Some("sha-standalone"));

    Ok(())
}

/// All returned items should have the correct entity type variant.
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("expanded_cursor_soup_exhaustive")
    )
)]
async fn test_exhaustive_entity_type_counts(pool: Pool<Postgres>) -> anyhow::Result<()> {
    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
    let items = expanded_generic_cursor_soup(
        &pool,
        user_id.copied(),
        50,
        Query::Sort(SimpleSortMethod::UpdatedAt, ()),
    )
    .await?;

    let mut doc_count = 0;
    let mut chat_count = 0;
    let mut project_count = 0;

    for item in &items {
        match item {
            SoupItem::Document(_) => doc_count += 1,
            SoupItem::Chat(_) => chat_count += 1,
            SoupItem::Project(_) => project_count += 1,
            _ => panic!("Unexpected entity type"),
        }
    }

    assert_eq!(doc_count, 7, "Should have 7 documents");
    assert_eq!(chat_count, 2, "Should have 2 chats");
    assert_eq!(project_count, 3, "Should have 3 projects");

    Ok(())
}

// ============================================================================
// Exhaustive expanded_dynamic_cursor_soup tests
// Uses dynamic_query_exhaustive fixture.
// ============================================================================

// Fixture constants for dynamic_query_exhaustive
const DYN_PROJECT_ROOT: &str = "aa000001-ffff-ffff-ffff-ffffffffffff";
const DYN_PROJECT_MID: &str = "aa000002-ffff-ffff-ffff-ffffffffffff";
const DYN_PROJECT_DEEP: &str = "aa000003-ffff-ffff-ffff-ffffffffffff";
const DYN_PROJECT_ISOLATED: &str = "aa000004-ffff-ffff-ffff-ffffffffffff";
const DYN_DOC_ROOT_PDF: &str = "bb000001-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
const DYN_DOC_MID_DOCX: &str = "bb000002-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
const DYN_DOC_DEEP_PDF: &str = "bb000003-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
const DYN_DOC_STANDALONE_TXT: &str = "bb000004-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
const DYN_DOC_DELETED: &str = "bb000005-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
const DYN_DOC_TASK_COMPLETED: &str = "bb000006-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
const DYN_DOC_TASK_INCOMPLETE: &str = "bb000007-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
const DYN_DOC_TASK_NO_STATUS: &str = "bb000008-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
const DYN_DOC_ISOLATED: &str = "bb000009-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
const DYN_DOC_SHARED_MD: &str = "bb000010-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
const DYN_CHAT_ROOT: &str = "cc000001-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
const DYN_CHAT_STANDALONE: &str = "cc000002-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
const DYN_CHAT_DELETED: &str = "cc000003-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
const DYN_CHAT_ISOLATED: &str = "cc000004-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
const DYN_CHAT_SHARED: &str = "cc000005-aaaa-aaaa-aaaa-aaaaaaaaaaaa";

/// Helper to call expanded_dynamic_cursor_soup with common defaults
async fn dyn_fetch(
    db: &PgPool,
    user: &str,
    limit: u16,
    sort: SimpleSortMethod,
    filters: EntityFilterAst,
    exclude_frecency: bool,
) -> Result<Vec<SoupItem>, sqlx::Error> {
    let user_id = MacroUserIdStr::parse_from_str(user).unwrap();
    expanded_dynamic_cursor_soup(
        db,
        ExpandedDynamicCursorArgs {
            user_id: user_id.copied(),
            limit,
            cursor: Query::Sort(sort.into(), filters),
            exclude_frecency,
        },
    )
    .await
}

// ---- Sort methods (4 tests) ----

/// 1. Verify UpdatedAt sort order through the dynamic query.
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("dynamic_query_exhaustive")
    )
)]
async fn test_dyn_sort_updated_at(db: PgPool) -> anyhow::Result<()> {
    let items = dyn_fetch(
        &db,
        "macro|[email protected]",
        50,
        SimpleSortMethod::UpdatedAt,
        EntityFilterAst::mock_empty(),
        false,
    )
    .await?;

    let ids: Vec<Uuid> = items.iter().map(|i| i.id()).collect();

    // updatedAt DESC:
    // chat-standalone    2024-03-11
    // doc-deep-pdf       2024-03-10
    // chat-shared        2024-02-18
    // project-deep       2024-02-12
    // project-mid        2024-02-11
    // project-root       2024-02-10
    // chat-root          2024-02-09
    // doc-shared-md      2024-02-08
    // doc-task-no-status 2024-02-07
    // doc-task-incomplete 2024-02-06
    // doc-task-completed 2024-02-05
    // doc-standalone-txt 2024-02-04
    // doc-mid-docx       2024-02-02
    // doc-root-pdf       2024-02-01
    let expected: Vec<Uuid> = [
        DYN_CHAT_STANDALONE,
        DYN_DOC_DEEP_PDF,
        DYN_CHAT_SHARED,
        DYN_PROJECT_DEEP,
        DYN_PROJECT_MID,
        DYN_PROJECT_ROOT,
        DYN_CHAT_ROOT,
        DYN_DOC_SHARED_MD,
        DYN_DOC_TASK_NO_STATUS,
        DYN_DOC_TASK_INCOMPLETE,
        DYN_DOC_TASK_COMPLETED,
        DYN_DOC_STANDALONE_TXT,
        DYN_DOC_MID_DOCX,
        DYN_DOC_ROOT_PDF,
    ]
    .iter()
    .map(|s| uuid(s))
    .collect();

    assert_eq!(ids, expected, "UpdatedAt sort order is wrong");
    Ok(())
}

/// 2. Verify CreatedAt sort order through the dynamic query.
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("dynamic_query_exhaustive")
    )
)]
async fn test_dyn_sort_created_at(db: PgPool) -> anyhow::Result<()> {
    let items = dyn_fetch(
        &db,
        "macro|[email protected]",
        50,
        SimpleSortMethod::CreatedAt,
        EntityFilterAst::mock_empty(),
        false,
    )
    .await?;

    let ids: Vec<Uuid> = items.iter().map(|i| i.id()).collect();

    // createdAt DESC:
    // chat-shared        2024-01-18
    // project-deep       2024-01-12
    // project-mid        2024-01-11
    // project-root       2024-01-10
    // chat-standalone    2024-01-09
    // chat-root          2024-01-08 (id cc000001 > bb000010)
    // doc-shared-md      2024-01-08
    // doc-task-no-status 2024-01-07
    // doc-task-incomplete 2024-01-06
    // doc-task-completed 2024-01-05
    // doc-standalone-txt 2024-01-04
    // doc-deep-pdf       2024-01-03
    // doc-mid-docx       2024-01-02
    // doc-root-pdf       2024-01-01
    let expected: Vec<Uuid> = [
        DYN_CHAT_SHARED,
        DYN_PROJECT_DEEP,
        DYN_PROJECT_MID,
        DYN_PROJECT_ROOT,
        DYN_CHAT_STANDALONE,
        DYN_CHAT_ROOT,
        DYN_DOC_SHARED_MD,
        DYN_DOC_TASK_NO_STATUS,
        DYN_DOC_TASK_INCOMPLETE,
        DYN_DOC_TASK_COMPLETED,
        DYN_DOC_STANDALONE_TXT,
        DYN_DOC_DEEP_PDF,
        DYN_DOC_MID_DOCX,
        DYN_DOC_ROOT_PDF,
    ]
    .iter()
    .map(|s| uuid(s))
    .collect();

    assert_eq!(ids, expected, "CreatedAt sort order is wrong");
    Ok(())
}

/// 3. Verify ViewedAt sort order through the dynamic query.
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("dynamic_query_exhaustive")
    )
)]
async fn test_dyn_sort_viewed_at(db: PgPool) -> anyhow::Result<()> {
    let items = dyn_fetch(
        &db,
        "macro|[email protected]",
        50,
        SimpleSortMethod::ViewedAt,
        EntityFilterAst::mock_empty(),
        false,
    )
    .await?;

    let ids: Vec<Uuid> = items.iter().map(|i| i.id()).collect();

    // With history (UserHistory.updatedAt DESC):
    //   doc-mid-docx         2024-03-08
    //   doc-task-no-status   2024-03-07
    //   doc-standalone-txt   2024-03-06
    //   doc-root-pdf         2024-03-05
    //   doc-task-completed   2024-03-04
    //   chat-root            2024-03-03
    //   project-root         2024-03-02
    //   project-deep         2024-03-01
    // Without history (epoch, tie by id DESC):
    //   cc000005 chat-shared
    //   cc000002 chat-standalone
    //   bb000010 doc-shared-md
    //   bb000007 doc-task-incomplete
    //   bb000003 doc-deep-pdf
    //   aa000002 project-mid
    let expected: Vec<Uuid> = [
        DYN_DOC_MID_DOCX,
        DYN_DOC_TASK_NO_STATUS,
        DYN_DOC_STANDALONE_TXT,
        DYN_DOC_ROOT_PDF,
        DYN_DOC_TASK_COMPLETED,
        DYN_CHAT_ROOT,
        DYN_PROJECT_ROOT,
        DYN_PROJECT_DEEP,
        DYN_CHAT_SHARED,
        DYN_CHAT_STANDALONE,
        DYN_DOC_SHARED_MD,
        DYN_DOC_TASK_INCOMPLETE,
        DYN_DOC_DEEP_PDF,
        DYN_PROJECT_MID,
    ]
    .iter()
    .map(|s| uuid(s))
    .collect();

    assert_eq!(ids, expected, "ViewedAt sort order is wrong");
    Ok(())
}

/// 4. Verify ViewedUpdated sort order through the dynamic query.
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("dynamic_query_exhaustive")
    )
)]
async fn test_dyn_sort_viewed_updated(db: PgPool) -> anyhow::Result<()> {
    let items = dyn_fetch(
        &db,
        "macro|[email protected]",
        50,
        SimpleSortMethod::ViewedUpdated,
        EntityFilterAst::mock_empty(),
        false,
    )
    .await?;

    let ids: Vec<Uuid> = items.iter().map(|i| i.id()).collect();

    // COALESCE(uh.updatedAt, item.updatedAt) DESC:
    //   chat-standalone      no history -> updatedAt 2024-03-11
    //   doc-deep-pdf         no history -> updatedAt 2024-03-10
    //   doc-mid-docx         history 2024-03-08
    //   doc-task-no-status   history 2024-03-07
    //   doc-standalone-txt   history 2024-03-06
    //   doc-root-pdf         history 2024-03-05
    //   doc-task-completed   history 2024-03-04
    //   chat-root            history 2024-03-03
    //   project-root         history 2024-03-02
    //   project-deep         history 2024-03-01
    //   chat-shared          no history -> updatedAt 2024-02-18
    //   project-mid          no history -> updatedAt 2024-02-11
    //   doc-shared-md        no history -> updatedAt 2024-02-08
    //   doc-task-incomplete  no history -> updatedAt 2024-02-06
    let expected: Vec<Uuid> = [
        DYN_CHAT_STANDALONE,
        DYN_DOC_DEEP_PDF,
        DYN_DOC_MID_DOCX,
        DYN_DOC_TASK_NO_STATUS,
        DYN_DOC_STANDALONE_TXT,
        DYN_DOC_ROOT_PDF,
        DYN_DOC_TASK_COMPLETED,
        DYN_CHAT_ROOT,
        DYN_PROJECT_ROOT,
        DYN_PROJECT_DEEP,
        DYN_CHAT_SHARED,
        DYN_PROJECT_MID,
        DYN_DOC_SHARED_MD,
        DYN_DOC_TASK_INCOMPLETE,
    ]
    .iter()
    .map(|s| uuid(s))
    .collect();

    assert_eq!(ids, expected, "ViewedUpdated sort order is wrong");
    Ok(())
}

// ---- Access control & deleted items (3 tests) ----

/// 5. Deleted doc and chat never appear in dynamic query results.
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("dynamic_query_exhaustive")
    )
)]
async fn test_dyn_deleted_items_excluded(db: PgPool) -> anyhow::Result<()> {
    let items = dyn_fetch(
        &db,
        "macro|[email protected]",
        50,
        SimpleSortMethod::UpdatedAt,
        EntityFilterAst::mock_empty(),
        false,
    )
    .await?;

    let ids: HashSet<Uuid> = items.iter().map(|i| i.id()).collect();

    assert!(
        !ids.contains(&uuid(DYN_DOC_DELETED)),
        "Deleted document must not appear"
    );
    assert!(
        !ids.contains(&uuid(DYN_CHAT_DELETED)),
        "Deleted chat must not appear"
    );

    Ok(())
}

/// 6. Isolated project/doc/chat excluded for user-1 in dynamic query.
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("dynamic_query_exhaustive")
    )
)]
async fn test_dyn_access_control_excludes_isolated(db: PgPool) -> anyhow::Result<()> {
    let items = dyn_fetch(
        &db,
        "macro|[email protected]",
        50,
        SimpleSortMethod::UpdatedAt,
        EntityFilterAst::mock_empty(),
        false,
    )
    .await?;

    let ids: HashSet<Uuid> = items.iter().map(|i| i.id()).collect();

    assert!(
        !ids.contains(&uuid(DYN_PROJECT_ISOLATED)),
        "Isolated project must not appear for user-1"
    );
    assert!(
        !ids.contains(&uuid(DYN_DOC_ISOLATED)),
        "Isolated doc must not appear for user-1"
    );
    assert!(
        !ids.contains(&uuid(DYN_CHAT_ISOLATED)),
        "Isolated chat must not appear for user-1"
    );

    Ok(())
}

/// 7. User-2 only sees isolated items through the dynamic query.
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("dynamic_query_exhaustive")
    )
)]
async fn test_dyn_user_isolation(db: PgPool) -> anyhow::Result<()> {
    let items = dyn_fetch(
        &db,
        "macro|[email protected]",
        50,
        SimpleSortMethod::UpdatedAt,
        EntityFilterAst::mock_empty(),
        false,
    )
    .await?;

    let ids: HashSet<Uuid> = items.iter().map(|i| i.id()).collect();

    let expected: HashSet<Uuid> = [DYN_PROJECT_ISOLATED, DYN_DOC_ISOLATED, DYN_CHAT_ISOLATED]
        .iter()
        .map(|s| uuid(s))
        .collect();

    assert_eq!(
        ids, expected,
        "User-2 should only see isolated project, its doc, and its chat"
    );

    Ok(())
}

// ---- Filter types (3 tests) ----

/// 8. Filter documents by owner (user-2) returns only doc-shared-md.
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("dynamic_query_exhaustive")
    )
)]
async fn test_dyn_filter_doc_by_owner(db: PgPool) -> anyhow::Result<()> {
    use item_filters::{DocumentFilters, EntityFilters};

    let entity_filters = EntityFilters {
        document_filters: DocumentFilters {
            owners: vec!["macro|[email protected]".to_string()],
            ..Default::default()
        },
        ..Default::default()
    };

    let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

    let items = dyn_fetch(
        &db,
        "macro|[email protected]",
        50,
        SimpleSortMethod::UpdatedAt,
        filters,
        false,
    )
    .await?;

    let doc_ids: Vec<Uuid> = items
        .iter()
        .filter_map(|i| match i {
            SoupItem::Document(d) => Some(d.id),
            _ => None,
        })
        .collect();

    // Only doc-shared-md is owned by user-2 and accessible to user-1
    assert_eq!(doc_ids.len(), 1, "Should get exactly 1 document");
    assert_eq!(doc_ids[0], uuid(DYN_DOC_SHARED_MD));

    // Chats and projects should still be present (unfiltered)
    let chat_count = items
        .iter()
        .filter(|i| matches!(i, SoupItem::Chat(_)))
        .count();
    let project_count = items
        .iter()
        .filter(|i| matches!(i, SoupItem::Project(_)))
        .count();
    assert_eq!(chat_count, 3, "Should get all 3 chats");
    assert_eq!(project_count, 3, "Should get all 3 projects");

    Ok(())
}

/// 9. Filter documents by project ID returns only docs in that project.
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("dynamic_query_exhaustive")
    )
)]
async fn test_dyn_filter_doc_by_project(db: PgPool) -> anyhow::Result<()> {
    use item_filters::{DocumentFilters, EntityFilters};

    // Filter for documents in project-deep only
    let entity_filters = EntityFilters {
        document_filters: DocumentFilters {
            project_ids: vec![DYN_PROJECT_DEEP.to_string()],
            ..Default::default()
        },
        ..Default::default()
    };

    let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

    let items = dyn_fetch(
        &db,
        "macro|[email protected]",
        50,
        SimpleSortMethod::UpdatedAt,
        filters,
        false,
    )
    .await?;

    let doc_ids: Vec<Uuid> = items
        .iter()
        .filter_map(|i| match i {
            SoupItem::Document(d) => Some(d.id),
            _ => None,
        })
        .collect();

    assert_eq!(doc_ids.len(), 1, "Should get exactly 1 document in deep");
    assert_eq!(doc_ids[0], uuid(DYN_DOC_DEEP_PDF));

    Ok(())
}

/// 10. Filter documents by multiple file types (pdf + docx).
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("dynamic_query_exhaustive")
    )
)]
async fn test_dyn_filter_multiple_file_types(db: PgPool) -> anyhow::Result<()> {
    use item_filters::{DocumentFilters, EntityFilters};

    let entity_filters = EntityFilters {
        document_filters: DocumentFilters {
            file_types: vec!["pdf".to_string(), "docx".to_string()],
            ..Default::default()
        },
        ..Default::default()
    };

    let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

    let items = dyn_fetch(
        &db,
        "macro|[email protected]",
        50,
        SimpleSortMethod::UpdatedAt,
        filters,
        false,
    )
    .await?;

    let doc_ids: HashSet<Uuid> = items
        .iter()
        .filter_map(|i| match i {
            SoupItem::Document(d) => Some(d.id),
            _ => None,
        })
        .collect();

    // Accessible pdf docs: doc-root-pdf, doc-deep-pdf
    // Accessible docx docs: doc-mid-docx
    let expected_docs: HashSet<Uuid> = [DYN_DOC_ROOT_PDF, DYN_DOC_MID_DOCX, DYN_DOC_DEEP_PDF]
        .iter()
        .map(|s| uuid(s))
        .collect();

    assert_eq!(
        doc_ids, expected_docs,
        "Should get exactly the pdf and docx documents"
    );

    Ok(())
}

// ---- Notification + task include filters (3 tests) ----

#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("dynamic_query_exhaustive")
    )
)]
async fn test_dyn_filter_notification_done_false(db: PgPool) -> anyhow::Result<()> {
    use item_filters::{
        ChatFilters, DocumentFilters, EntityFilters, NotificationFilters, ProjectFilters,
    };

    let entity_filters = EntityFilters {
        document_filters: DocumentFilters {
            notification_filters: NotificationFilters {
                states: vec![
                    item_filters::NotificationState::Unseen,
                    item_filters::NotificationState::Seen,
                ],
            },
            ..Default::default()
        },
        chat_filters: ChatFilters {
            notification_filters: NotificationFilters {
                states: vec![
                    item_filters::NotificationState::Unseen,
                    item_filters::NotificationState::Seen,
                ],
            },
            ..Default::default()
        },
        project_filters: ProjectFilters {
            notification_filters: NotificationFilters {
                states: vec![
                    item_filters::NotificationState::Unseen,
                    item_filters::NotificationState::Seen,
                ],
            },
            ..Default::default()
        },
        ..Default::default()
    };

    let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

    let items = dyn_fetch(
        &db,
        "macro|[email protected]",
        50,
        SimpleSortMethod::UpdatedAt,
        filters,
        false,
    )
    .await?;

    let ids: HashSet<Uuid> = items.iter().map(|i| i.id()).collect();
    let expected: HashSet<Uuid> = [DYN_DOC_ROOT_PDF, DYN_CHAT_ROOT, DYN_PROJECT_ROOT]
        .iter()
        .map(|s| uuid(s))
        .collect();

    assert_eq!(
        ids, expected,
        "done=false should only return entities with matching not-done notifications"
    );

    Ok(())
}

#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("dynamic_query_exhaustive")
    )
)]
async fn test_dyn_filter_notification_done_and_seen_false(db: PgPool) -> anyhow::Result<()> {
    use item_filters::{
        ChatFilters, DocumentFilters, EntityFilters, NotificationFilters, ProjectFilters,
    };

    let notification_filters = NotificationFilters {
        states: vec![item_filters::NotificationState::Unseen],
    };
    let entity_filters = EntityFilters {
        document_filters: DocumentFilters {
            notification_filters: notification_filters.clone(),
            ..Default::default()
        },
        chat_filters: ChatFilters {
            notification_filters: notification_filters.clone(),
            ..Default::default()
        },
        project_filters: ProjectFilters {
            notification_filters,
            ..Default::default()
        },
        ..Default::default()
    };

    let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

    let items = dyn_fetch(
        &db,
        "macro|[email protected]",
        50,
        SimpleSortMethod::UpdatedAt,
        filters,
        false,
    )
    .await?;

    let ids: HashSet<Uuid> = items.iter().map(|i| i.id()).collect();
    let expected: HashSet<Uuid> = [DYN_DOC_ROOT_PDF, DYN_CHAT_ROOT, DYN_PROJECT_ROOT]
        .iter()
        .map(|s| uuid(s))
        .collect();

    assert_eq!(
        ids, expected,
        "done=false + seen=false should require both constraints on notifications"
    );

    Ok(())
}

#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("dynamic_query_exhaustive")
    )
)]
async fn test_dyn_task_include_cbm_atm_nc_bypasses_document_filters(
    db: PgPool,
) -> anyhow::Result<()> {
    use item_filters::{DocumentFilters, EntityFilters, TaskFilters};

    let entity_filters = EntityFilters {
        document_filters: DocumentFilters {
            file_types: vec!["pdf".to_string()],
            task_filters: TaskFilters {
                include_cbm_atm_nc: Some(true),
            },
            ..Default::default()
        },
        ..Default::default()
    };

    let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

    let items = dyn_fetch(
        &db,
        "macro|[email protected]",
        50,
        SimpleSortMethod::UpdatedAt,
        filters,
        false,
    )
    .await?;

    let doc_ids: HashSet<Uuid> = items
        .iter()
        .filter_map(|i| match i {
            SoupItem::Document(d) => Some(d.id),
            _ => None,
        })
        .collect();

    let expected_docs: HashSet<Uuid> = [DYN_DOC_ROOT_PDF, DYN_DOC_DEEP_PDF, DYN_DOC_TASK_NO_STATUS]
        .iter()
        .map(|s| uuid(s))
        .collect();

    assert_eq!(
        doc_ids, expected_docs,
        "include_cbm_atm_nc should OR in matching assigned not-completed tasks"
    );

    Ok(())
}

// ---- Task completion & document fields (2 tests) ----

/// 11. Task documents should have correct is_completed values.
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("dynamic_query_exhaustive")
    )
)]
async fn test_dyn_task_completion_status(db: PgPool) -> anyhow::Result<()> {
    use models_soup::document::SoupDocumentSubType;

    let items = dyn_fetch(
        &db,
        "macro|[email protected]",
        50,
        SimpleSortMethod::UpdatedAt,
        EntityFilterAst::mock_empty(),
        false,
    )
    .await?;

    let items_map: std::collections::HashMap<Uuid, &SoupItem> =
        items.iter().map(|i| (i.id(), i)).collect();

    // Completed task
    let doc = unwrap_enum!(items_map[&uuid(DYN_DOC_TASK_COMPLETED)], SoupItem::Document);
    match &doc.sub_type {
        Some(SoupDocumentSubType::Task { is_completed }) => {
            assert!(is_completed, "Completed task should have is_completed=true");
        }
        other => panic!("Expected Task sub_type, got {:?}", other),
    }

    // Incomplete task (status = In Progress)
    let doc = unwrap_enum!(
        items_map[&uuid(DYN_DOC_TASK_INCOMPLETE)],
        SoupItem::Document
    );
    match &doc.sub_type {
        Some(SoupDocumentSubType::Task { is_completed }) => {
            assert!(
                !is_completed,
                "Incomplete task should have is_completed=false"
            );
        }
        other => panic!("Expected Task sub_type, got {:?}", other),
    }

    // Task with no status property
    let doc = unwrap_enum!(items_map[&uuid(DYN_DOC_TASK_NO_STATUS)], SoupItem::Document);
    match &doc.sub_type {
        Some(SoupDocumentSubType::Task { is_completed }) => {
            assert!(
                !is_completed,
                "Task with no status should have is_completed=false"
            );
        }
        other => panic!("Expected Task sub_type, got {:?}", other),
    }

    // Non-task document
    let doc = unwrap_enum!(items_map[&uuid(DYN_DOC_ROOT_PDF)], SoupItem::Document);
    assert!(
        doc.sub_type.is_none(),
        "Non-task document should have sub_type=None"
    );

    Ok(())
}

/// 12. Document fields (sha, file_type, viewed_at, project_id) populated correctly.
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("dynamic_query_exhaustive")
    )
)]
async fn test_dyn_document_fields(db: PgPool) -> anyhow::Result<()> {
    let items = dyn_fetch(
        &db,
        "macro|[email protected]",
        50,
        SimpleSortMethod::UpdatedAt,
        EntityFilterAst::mock_empty(),
        false,
    )
    .await?;

    let items_map: std::collections::HashMap<Uuid, &SoupItem> =
        items.iter().map(|i| (i.id(), i)).collect();

    // doc-root-pdf: pdf, sha-root-pdf, has history, in project-root
    let doc = unwrap_enum!(items_map[&uuid(DYN_DOC_ROOT_PDF)], SoupItem::Document);
    assert_eq!(doc.file_type.as_deref(), Some("pdf"));
    assert_eq!(doc.sha.as_deref(), Some("sha-root-pdf"));
    assert!(doc.viewed_at.is_some(), "doc-root-pdf has history");
    assert_eq!(doc.project_id, Some(uuid(DYN_PROJECT_ROOT)));

    // doc-mid-docx: docx, sha-mid-docx, has history, in project-mid
    let doc = unwrap_enum!(items_map[&uuid(DYN_DOC_MID_DOCX)], SoupItem::Document);
    assert_eq!(doc.file_type.as_deref(), Some("docx"));
    assert_eq!(doc.sha.as_deref(), Some("sha-mid-docx"));
    assert!(doc.viewed_at.is_some(), "doc-mid-docx has history");
    assert_eq!(doc.project_id, Some(uuid(DYN_PROJECT_MID)));

    // doc-standalone-txt: txt, sha-standalone-txt, has history, no project
    let doc = unwrap_enum!(items_map[&uuid(DYN_DOC_STANDALONE_TXT)], SoupItem::Document);
    assert_eq!(doc.file_type.as_deref(), Some("txt"));
    assert_eq!(doc.sha.as_deref(), Some("sha-standalone-txt"));
    assert!(doc.viewed_at.is_some(), "doc-standalone-txt has history");
    assert_eq!(doc.project_id, None);

    // doc-deep-pdf: pdf, no history
    let doc = unwrap_enum!(items_map[&uuid(DYN_DOC_DEEP_PDF)], SoupItem::Document);
    assert_eq!(doc.file_type.as_deref(), Some("pdf"));
    assert!(doc.viewed_at.is_none(), "doc-deep-pdf has no history");
    assert_eq!(doc.project_id, Some(uuid(DYN_PROJECT_DEEP)));

    // doc-shared-md: md, owned by user-2
    let doc = unwrap_enum!(items_map[&uuid(DYN_DOC_SHARED_MD)], SoupItem::Document);
    assert_eq!(doc.file_type.as_deref(), Some("md"));
    assert_eq!(doc.sha.as_deref(), Some("sha-shared-md"));
    assert_eq!(doc.project_id, Some(uuid(DYN_PROJECT_ROOT)));

    Ok(())
}

// ---- Pagination (2 tests) ----

/// 13. Walk all items limit=1, verify no dupes, matches bulk fetch order.
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("dynamic_query_exhaustive")
    )
)]
async fn test_dyn_paginate_one_at_a_time(db: PgPool) -> anyhow::Result<()> {
    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
    let sort = SimpleSortMethod::UpdatedAt;
    let filters = EntityFilterAst::mock_empty();

    let mut all_ids: Vec<Uuid> = Vec::new();
    let mut current_query: Query<Uuid, SimpleSortMethod, EntityFilterAst> =
        Query::Sort(sort, filters.clone());

    loop {
        let result = expanded_dynamic_cursor_soup(
            &db,
            ExpandedDynamicCursorArgs {
                user_id: user_id.copied(),
                limit: 1,
                cursor: DynamicSort::from_timestamp_query(current_query),
                exclude_frecency: false,
            },
        )
        .await?
        .into_iter()
        .paginate_on(1, sort)
        .filter_on(filters.clone())
        .into_page();

        all_ids.extend(result.items.iter().map(|i| i.id()));

        match result.next_cursor {
            Some(cursor) => {
                let decoded = cursor.decode_json().unwrap();
                current_query = Query::Cursor(models_pagination::Cursor {
                    id: decoded.id,
                    limit: decoded.limit,
                    val: decoded.val,
                    filter: decoded.filter,
                });
            }
            None => break,
        }
    }

    assert_eq!(all_ids.len(), 14, "Should walk through all 14 items");

    let unique: HashSet<Uuid> = all_ids.iter().copied().collect();
    assert_eq!(
        unique.len(),
        14,
        "No duplicates when paginating one at a time"
    );

    // Verify order matches a single large fetch
    let all_at_once = dyn_fetch(
        &db,
        "macro|[email protected]",
        50,
        sort,
        EntityFilterAst::mock_empty(),
        false,
    )
    .await?;
    let expected_ids: Vec<Uuid> = all_at_once.iter().map(|i| i.id()).collect();
    assert_eq!(
        all_ids, expected_ids,
        "Paginated order should match single-fetch order"
    );

    Ok(())
}

/// 14. Paginate through filtered results, verify filter consistency and no dupes.
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("dynamic_query_exhaustive")
    )
)]
async fn test_dyn_paginate_with_filters(db: PgPool) -> anyhow::Result<()> {
    use item_filters::{DocumentFilters, EntityFilters};

    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
    let sort = SimpleSortMethod::CreatedAt;
    let page_size: u16 = 3;

    let entity_filters = EntityFilters {
        document_filters: DocumentFilters {
            file_types: vec!["pdf".to_string()],
            ..Default::default()
        },
        ..Default::default()
    };

    let make_filters = || {
        EntityFilterAst::new_from_filters(entity_filters.clone())
            .unwrap()
            .unwrap()
    };

    let mut all_items: Vec<SoupItem> = Vec::new();
    let mut current_query: Query<Uuid, SimpleSortMethod, EntityFilterAst> =
        Query::Sort(sort, make_filters());

    loop {
        let result = expanded_dynamic_cursor_soup(
            &db,
            ExpandedDynamicCursorArgs {
                user_id: user_id.copied(),
                limit: page_size,
                cursor: DynamicSort::from_timestamp_query(current_query),
                exclude_frecency: false,
            },
        )
        .await?
        .into_iter()
        .paginate_on(page_size as usize, sort)
        .filter_on(make_filters())
        .into_page();

        all_items.extend(result.items);

        match result.next_cursor {
            Some(cursor) => {
                let decoded = cursor.decode_json()?;
                current_query = Query::Cursor(models_pagination::Cursor {
                    id: decoded.id,
                    limit: decoded.limit,
                    val: decoded.val,
                    filter: decoded.filter,
                });
            }
            None => break,
        }
    }

    // Verify all documents are PDFs (filter consistency)
    for item in &all_items {
        if let SoupItem::Document(doc) = item {
            assert_eq!(
                doc.file_type.as_deref(),
                Some("pdf"),
                "All documents should be PDFs"
            );
        }
    }

    // Verify no duplicates
    let all_ids: Vec<Uuid> = all_items.iter().map(|i| i.id()).collect();
    let unique: HashSet<Uuid> = all_ids.iter().copied().collect();
    assert_eq!(
        all_ids.len(),
        unique.len(),
        "Should have no duplicate items across pages"
    );

    // Should get 2 pdf docs + 3 chats + 3 projects = 8 total
    let doc_count = all_items
        .iter()
        .filter(|i| matches!(i, SoupItem::Document(_)))
        .count();
    assert_eq!(doc_count, 2, "Should get exactly 2 PDF documents");

    Ok(())
}

// ---- Frecency (2 tests) ----

/// 15. exclude_frecency=true removes items with frecency records.
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("dynamic_query_exhaustive")
    )
)]
async fn test_dyn_exclude_frecency(db: PgPool) -> anyhow::Result<()> {
    let items = dyn_fetch(
        &db,
        "macro|[email protected]",
        50,
        SimpleSortMethod::UpdatedAt,
        EntityFilterAst::mock_empty(),
        true, // exclude_frecency
    )
    .await?;

    let ids: HashSet<Uuid> = items.iter().map(|i| i.id()).collect();

    // doc-root-pdf and chat-root have frecency records and should be excluded
    assert!(
        !ids.contains(&uuid(DYN_DOC_ROOT_PDF)),
        "doc-root-pdf should be excluded (has frecency)"
    );
    assert!(
        !ids.contains(&uuid(DYN_CHAT_ROOT)),
        "chat-root should be excluded (has frecency)"
    );

    // All other accessible items should still be present (14 - 2 = 12)
    assert_eq!(
        items.len(),
        12,
        "Should get 12 items after frecency exclusion"
    );

    // Verify a non-frecency item is still present
    assert!(
        ids.contains(&uuid(DYN_DOC_MID_DOCX)),
        "doc-mid-docx should still be present"
    );

    Ok(())
}

/// 16. exclude_frecency works with all 4 sort methods.
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("dynamic_query_exhaustive")
    )
)]
async fn test_dyn_exclude_frecency_all_sort_methods(db: PgPool) -> anyhow::Result<()> {
    let frecency_ids: HashSet<Uuid> = [DYN_DOC_ROOT_PDF, DYN_CHAT_ROOT]
        .iter()
        .map(|s| uuid(s))
        .collect();

    for sort in [
        SimpleSortMethod::UpdatedAt,
        SimpleSortMethod::CreatedAt,
        SimpleSortMethod::ViewedAt,
        SimpleSortMethod::ViewedUpdated,
    ] {
        let items = dyn_fetch(
            &db,
            "macro|[email protected]",
            50,
            sort,
            EntityFilterAst::mock_empty(),
            true,
        )
        .await?;

        let ids: HashSet<Uuid> = items.iter().map(|i| i.id()).collect();

        for fid in &frecency_ids {
            assert!(
                !ids.contains(fid),
                "Sort {:?}: item {} should be excluded by frecency filter",
                sort,
                fid
            );
        }

        assert_eq!(
            items.len(),
            12,
            "Sort {:?}: should get 12 items after frecency exclusion",
            sort
        );
    }

    Ok(())
}

// ---- Edge cases (1 test) ----

/// 17. All 3 entity types filtered to nothing returns empty vec.
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("dynamic_query_exhaustive")
    )
)]
async fn test_dyn_all_types_filtered_to_empty(db: PgPool) -> anyhow::Result<()> {
    use item_filters::{ChatFilters, DocumentFilters, EntityFilters, ProjectFilters};

    let entity_filters = EntityFilters {
        document_filters: DocumentFilters {
            document_ids: vec!["00000000-0000-0000-0000-000000000000".to_string()],
            ..Default::default()
        },
        chat_filters: ChatFilters {
            chat_ids: vec!["00000000-0000-0000-0000-000000000000".to_string()],
            ..Default::default()
        },
        project_filters: ProjectFilters {
            project_ids: vec!["00000000-0000-0000-0000-000000000000".to_string()],
            ..Default::default()
        },
        ..Default::default()
    };

    let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

    let items = dyn_fetch(
        &db,
        "macro|[email protected]",
        50,
        SimpleSortMethod::UpdatedAt,
        filters,
        false,
    )
    .await?;

    assert!(
        items.is_empty(),
        "All types filtered to nothing should return empty"
    );

    Ok(())
}

/// Reproduces a 500 error when all filter types are provided simultaneously.
/// The `chat_filters.owners` filter generates `c.owner` but the Chat table
/// uses `"userId"` as its owner column, not `owner`.
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("dynamic_query_exhaustive")
    )
)]
async fn test_all_filter_types_combined(db: PgPool) -> anyhow::Result<()> {
    use item_filters::{
        ChannelFilters, ChatFilters, DocumentFilters, EmailFilters, EntityFilters, ProjectFilters,
    };

    let nil = "00000000-0000-0000-0000-000000000000".to_string();

    let entity_filters = EntityFilters {
        channel_filters: ChannelFilters {
            channel_ids: vec![nil.clone()],
            ..Default::default()
        },
        document_filters: DocumentFilters {
            document_ids: vec![nil.clone()],
            ..Default::default()
        },
        email_filters: EmailFilters {
            recipients: vec![nil.clone()],
            ..Default::default()
        },
        project_filters: ProjectFilters {
            project_ids: vec![nil.clone()],
            ..Default::default()
        },
        chat_filters: ChatFilters {
            owners: vec!["macro|[email protected]".to_string()],
            ..Default::default()
        },
        ..Default::default()
    };

    let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

    let items = dyn_fetch(
        &db,
        "macro|[email protected]",
        100,
        SimpleSortMethod::UpdatedAt,
        filters,
        false,
    )
    .await?;

    // The query should execute without a SQL error.
    // With the nil UUID filters we expect no document/project matches,
    // but the chat owner filter should return chats owned by the user.
    assert!(
        items.iter().all(|i| matches!(i, SoupItem::Chat(_))),
        "Only chats should match (documents/projects filtered to nil UUID)"
    );

    Ok(())
}

// ---------- Property-based filtering tests ----------

// Test filtering documents by a single property select option (Priority = Low)
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("soup_items_with_properties")
    )
)]
async fn test_property_filter_by_select_option(db: Pool<Postgres>) -> anyhow::Result<()> {
    // Filter for Priority = Low (both doc A and doc B have Priority = Low)
    let entity_filters = item_filters::EntityFilters {
        property_filters: vec![PropertyFilter {
            property_definition_id: "00000001-0000-0000-0000-000000000003".to_string(), // Priority
            entity_type: Some("DOCUMENT".to_string()),
            option_ids: vec!["00000001-0000-0000-0003-000000000001".to_string()], // Low
            entity_ids: vec![],
            ..Default::default()
        }],
        ..Default::default()
    };

    let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

    let items = dyn_fetch(
        &db,
        "macro|[email protected]",
        100,
        SimpleSortMethod::UpdatedAt,
        filters,
        false,
    )
    .await?;

    // Both doc A and doc B have Priority = Low, plus projects/chats have no property filter
    // so they should all pass through. But only documents match the property filter on DOCUMENT entity_type.
    let doc_ids: HashSet<Uuid> = items
        .iter()
        .filter_map(|i| match i {
            SoupItem::Document(d) => Some(d.id),
            _ => None,
        })
        .collect();

    assert!(
        doc_ids.contains(&Uuid::parse_str("11111111-aaaa-aaaa-aaaa-aaaaaaaaaaaa").unwrap()),
        "Doc A should match Priority = Low"
    );
    assert!(
        doc_ids.contains(&Uuid::parse_str("11111111-bbbb-bbbb-bbbb-bbbbbbbbbbbb").unwrap()),
        "Doc B should match Priority = Low"
    );

    Ok(())
}

// Test filtering by a property value that only matches one document
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("soup_items_with_properties")
    )
)]
async fn test_property_filter_by_status_completed(db: Pool<Postgres>) -> anyhow::Result<()> {
    // Filter for Status = Completed (only doc A has this)
    let entity_filters = item_filters::EntityFilters {
        property_filters: vec![PropertyFilter {
            property_definition_id: "00000001-0000-0000-0000-000000000002".to_string(), // Status
            entity_type: Some("DOCUMENT".to_string()),
            option_ids: vec!["00000001-0000-0000-0002-000000000004".to_string()], // Completed
            entity_ids: vec![],
            ..Default::default()
        }],
        ..Default::default()
    };

    let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

    let items = dyn_fetch(
        &db,
        "macro|[email protected]",
        100,
        SimpleSortMethod::UpdatedAt,
        filters,
        false,
    )
    .await?;

    let doc_ids: Vec<Uuid> = items
        .iter()
        .filter_map(|i| match i {
            SoupItem::Document(d) => Some(d.id),
            _ => None,
        })
        .collect();

    assert_eq!(
        doc_ids.len(),
        1,
        "Only doc A should match Status = Completed"
    );
    assert_eq!(
        doc_ids[0],
        Uuid::parse_str("11111111-aaaa-aaaa-aaaa-aaaaaaaaaaaa").unwrap(),
        "Doc A should match Status = Completed"
    );

    Ok(())
}

// Test AND-ing multiple property filters (Priority = Low AND Status = Completed)
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("soup_items_with_properties")
    )
)]
async fn test_property_filter_multiple_and(db: Pool<Postgres>) -> anyhow::Result<()> {
    // Filter for Priority = Low AND Status = Completed (only doc A has both)
    let entity_filters = item_filters::EntityFilters {
        property_filters: vec![
            PropertyFilter {
                property_definition_id: "00000001-0000-0000-0000-000000000003".to_string(), // Priority
                entity_type: Some("DOCUMENT".to_string()),
                option_ids: vec!["00000001-0000-0000-0003-000000000001".to_string()], // Low
                entity_ids: vec![],
                ..Default::default()
            },
            PropertyFilter {
                property_definition_id: "00000001-0000-0000-0000-000000000002".to_string(), // Status
                entity_type: Some("DOCUMENT".to_string()),
                option_ids: vec!["00000001-0000-0000-0002-000000000004".to_string()], // Completed
                entity_ids: vec![],
                ..Default::default()
            },
        ],
        ..Default::default()
    };

    let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

    let items = dyn_fetch(
        &db,
        "macro|[email protected]",
        100,
        SimpleSortMethod::UpdatedAt,
        filters,
        false,
    )
    .await?;

    let doc_ids: Vec<Uuid> = items
        .iter()
        .filter_map(|i| match i {
            SoupItem::Document(d) => Some(d.id),
            _ => None,
        })
        .collect();

    assert_eq!(
        doc_ids.len(),
        1,
        "Only doc A should match both Priority = Low AND Status = Completed"
    );
    assert_eq!(
        doc_ids[0],
        Uuid::parse_str("11111111-aaaa-aaaa-aaaa-aaaaaaaaaaaa").unwrap()
    );

    Ok(())
}

// Test that a non-matching property filter returns no documents
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("soup_items_with_properties")
    )
)]
async fn test_property_filter_no_match(db: Pool<Postgres>) -> anyhow::Result<()> {
    // Filter for Priority = Urgent (no documents have this)
    let entity_filters = item_filters::EntityFilters {
        property_filters: vec![PropertyFilter {
            property_definition_id: "00000001-0000-0000-0000-000000000003".to_string(), // Priority
            entity_type: Some("DOCUMENT".to_string()),
            option_ids: vec!["00000001-0000-0000-0003-000000000004".to_string()], // Urgent
            entity_ids: vec![],
            ..Default::default()
        }],
        ..Default::default()
    };

    let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

    let items = dyn_fetch(
        &db,
        "macro|[email protected]",
        100,
        SimpleSortMethod::UpdatedAt,
        filters,
        false,
    )
    .await?;

    let doc_count = items
        .iter()
        .filter(|i| matches!(i, SoupItem::Document(_)))
        .count();

    assert_eq!(doc_count, 0, "No documents should match Priority = Urgent");

    Ok(())
}

// Test OR-ing multiple option values within a single property filter
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("soup_items_with_properties")
    )
)]
async fn test_property_filter_multiple_options_or(db: Pool<Postgres>) -> anyhow::Result<()> {
    // Filter for Priority = Low OR Priority = Medium
    // Doc A and Doc B have Priority = Low, Project A has Priority = Medium on PROJECT entity_type
    // Since we filter on DOCUMENT entity_type, only docs match
    let entity_filters = item_filters::EntityFilters {
        property_filters: vec![PropertyFilter {
            property_definition_id: "00000001-0000-0000-0000-000000000003".to_string(), // Priority
            entity_type: Some("DOCUMENT".to_string()),
            option_ids: vec![
                "00000001-0000-0000-0003-000000000001".to_string(), // Low
                "00000001-0000-0000-0003-000000000002".to_string(), // Medium
            ],
            entity_ids: vec![],
            ..Default::default()
        }],
        ..Default::default()
    };

    let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

    let items = dyn_fetch(
        &db,
        "macro|[email protected]",
        100,
        SimpleSortMethod::UpdatedAt,
        filters,
        false,
    )
    .await?;

    let doc_ids: HashSet<Uuid> = items
        .iter()
        .filter_map(|i| match i {
            SoupItem::Document(d) => Some(d.id),
            _ => None,
        })
        .collect();

    assert!(
        doc_ids.contains(&Uuid::parse_str("11111111-aaaa-aaaa-aaaa-aaaaaaaaaaaa").unwrap()),
        "Doc A should match Priority = Low"
    );
    assert!(
        doc_ids.contains(&Uuid::parse_str("11111111-bbbb-bbbb-bbbb-bbbbbbbbbbbb").unwrap()),
        "Doc B should match Priority = Low"
    );

    Ok(())
}

// Test filtering without entity_type matches across all entity types
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("soup_items_with_properties")
    )
)]
async fn test_property_filter_without_entity_type(db: Pool<Postgres>) -> anyhow::Result<()> {
    // Filter for Priority = Low OR Medium without specifying entity_type.
    // Doc A and Doc B have Priority = Low on DOCUMENT entity_type.
    // Project A has Priority = Medium on PROJECT entity_type.
    // With entity_type = None, the filter should match across both entity types.
    let entity_filters = item_filters::EntityFilters {
        property_filters: vec![PropertyFilter {
            property_definition_id: "00000001-0000-0000-0000-000000000003".to_string(), // Priority
            entity_type: None, // match across all entity types
            option_ids: vec![
                "00000001-0000-0000-0003-000000000001".to_string(), // Low
                "00000001-0000-0000-0003-000000000002".to_string(), // Medium
            ],
            entity_ids: vec![],
            ..Default::default()
        }],
        ..Default::default()
    };

    let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

    let items = dyn_fetch(
        &db,
        "macro|[email protected]",
        100,
        SimpleSortMethod::UpdatedAt,
        filters,
        false,
    )
    .await?;

    let item_ids: HashSet<Uuid> = items.iter().map(|i| i.id()).collect();

    // Documents with Priority = Low
    assert!(
        item_ids.contains(&Uuid::parse_str("11111111-aaaa-aaaa-aaaa-aaaaaaaaaaaa").unwrap()),
        "Doc A should match Priority = Low (DOCUMENT entity_type)"
    );
    assert!(
        item_ids.contains(&Uuid::parse_str("11111111-bbbb-bbbb-bbbb-bbbbbbbbbbbb").unwrap()),
        "Doc B should match Priority = Low (DOCUMENT entity_type)"
    );
    // Project with Priority = Medium
    assert!(
        item_ids.contains(&Uuid::parse_str("aaaaaaaa-ffff-ffff-ffff-ffffffffffff").unwrap()),
        "Project A should match Priority = Medium (PROJECT entity_type)"
    );

    Ok(())
}

// Test that specifying entity_type scopes the filter to only that type
#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("soup_items_with_properties")
    )
)]
async fn test_property_filter_scoped_entity_type(db: Pool<Postgres>) -> anyhow::Result<()> {
    // Filter for Priority = Medium on PROJECT entity_type only.
    // Project A has Priority = Medium on PROJECT. Docs do not.
    // With entity_type = Some("PROJECT"), only projects should match.
    let entity_filters = item_filters::EntityFilters {
        property_filters: vec![PropertyFilter {
            property_definition_id: "00000001-0000-0000-0000-000000000003".to_string(), // Priority
            entity_type: Some("PROJECT".to_string()),
            option_ids: vec!["00000001-0000-0000-0003-000000000002".to_string()], // Medium
            entity_ids: vec![],
            ..Default::default()
        }],
        ..Default::default()
    };

    let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

    let items = dyn_fetch(
        &db,
        "macro|[email protected]",
        100,
        SimpleSortMethod::UpdatedAt,
        filters,
        false,
    )
    .await?;

    let project_ids: Vec<Uuid> = items
        .iter()
        .filter_map(|i| match i {
            SoupItem::Project(p) => Some(p.id),
            _ => None,
        })
        .collect();

    assert_eq!(
        project_ids.len(),
        1,
        "Only Project A should match Priority = Medium on PROJECT"
    );
    assert_eq!(
        project_ids[0],
        Uuid::parse_str("aaaaaaaa-ffff-ffff-ffff-ffffffffffff").unwrap()
    );

    // No documents should match since they only have Priority on DOCUMENT entity_type
    let doc_count = items
        .iter()
        .filter(|i| matches!(i, SoupItem::Document(_)))
        .count();
    assert_eq!(
        doc_count, 0,
        "No documents should match Priority = Medium on PROJECT entity_type"
    );

    Ok(())
}

// Test filtering documents by sub_type = task
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_dyn_filter_by_document_sub_type_task(db: PgPool) -> anyhow::Result<()> {
    use item_filters::{DocumentFilters, EntityFilters};

    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();

    let entity_filters = EntityFilters {
        document_filters: DocumentFilters {
            sub_types: vec!["task".to_string()],
            ..Default::default()
        },
        ..Default::default()
    };

    let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

    let items = expanded_dynamic_cursor_soup(
        &db,
        ExpandedDynamicCursorArgs {
            user_id: user_id.copied(),
            limit: 20,
            cursor: Query::Sort(SimpleSortMethod::CreatedAt.into(), filters),
            exclude_frecency: false,
        },
    )
    .await?;

    let mut task_doc_count = 0;
    let mut non_task_doc_count = 0;
    let mut chat_count = 0;
    let mut project_count = 0;

    for item in &items {
        match item {
            SoupItem::Document(doc) => {
                if doc.sub_type.is_some() {
                    task_doc_count += 1;
                } else {
                    non_task_doc_count += 1;
                }
            }
            SoupItem::Chat(_) => chat_count += 1,
            SoupItem::Project(_) => project_count += 1,
            _ => unimplemented!("unexpected item type"),
        }
    }

    // 3 accessible task documents: doc-in-A, doc-in-B, doc-in-C
    assert_eq!(task_doc_count, 3, "Should get 3 task documents");
    // No non-task documents should be returned since sub_type filter is applied
    assert_eq!(non_task_doc_count, 0, "Should get 0 non-task documents");
    // Chats and projects are unaffected by document filters
    assert!(chat_count > 0, "Should still get chats");
    assert!(project_count > 0, "Should still get projects");

    Ok(())
}

// Test filtering documents by sub_type combined with file_type
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_dyn_filter_by_sub_type_and_file_type(db: PgPool) -> anyhow::Result<()> {
    use item_filters::{DocumentFilters, EntityFilters};

    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();

    // Filter for task documents that are also PDFs
    let entity_filters = EntityFilters {
        document_filters: DocumentFilters {
            sub_types: vec!["task".to_string()],
            file_types: vec!["pdf".to_string()],
            ..Default::default()
        },
        ..Default::default()
    };

    let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

    let items = expanded_dynamic_cursor_soup(
        &db,
        ExpandedDynamicCursorArgs {
            user_id: user_id.copied(),
            limit: 20,
            cursor: Query::Sort(SimpleSortMethod::CreatedAt.into(), filters),
            exclude_frecency: false,
        },
    )
    .await?;

    let doc_count = items
        .iter()
        .filter(|i| matches!(i, SoupItem::Document(_)))
        .count();

    // doc-in-A (task, pdf) and doc-in-B (task, pdf) match; doc-in-C is task but md
    assert_eq!(
        doc_count, 2,
        "Should get 2 documents that are both tasks and PDFs"
    );

    for item in &items {
        if let SoupItem::Document(doc) = item {
            assert!(doc.sub_type.is_some(), "All docs should be tasks");
            assert_eq!(
                doc.file_type.as_deref(),
                Some("pdf"),
                "All docs should be PDFs"
            );
        }
    }

    Ok(())
}

async fn document_ids_for_phase0_membership_filter(
    db: &PgPool,
    document_filter: serde_json::Value,
) -> anyhow::Result<HashSet<Uuid>> {
    let filters: EntityFilterAst = serde_json::from_value(serde_json::json!({
        "df": document_filter
    }))?;
    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
    let items = expanded_dynamic_cursor_soup(
        db,
        ExpandedDynamicCursorArgs {
            user_id,
            limit: 100,
            cursor: Query::Sort(SimpleSortMethod::UpdatedAt.into(), filters),
            exclude_frecency: false,
        },
    )
    .await?;

    Ok(items
        .into_iter()
        .filter_map(|item| match item {
            SoupItem::Document(document) => Some(document.id),
            _ => None,
        })
        .collect())
}

// Characterizes the authoritative PostgreSQL membership for every production
// Documents tab before soup-flat-v2 introduces equivalent local facts.
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests", "soup_projection_phase0")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn production_documents_presets_have_authoritative_membership(
    db: PgPool,
) -> anyhow::Result<()> {
    let owner = "macro|[email protected]";
    let not_task = serde_json::json!({ "!": { "l": { "dst": "task" } } });
    let not_task_or_snippet = serde_json::json!({
        "!": {
            "|": [
                { "l": { "dst": "task" } },
                { "l": { "dst": "snippet" } }
            ]
        }
    });
    let owned = |sub_type_filter: serde_json::Value| {
        serde_json::json!({
            "&": [
                sub_type_filter,
                {
                    "&": [
                        { "l": { "o": owner } },
                        { "l": { "iea": false } }
                    ]
                }
            ]
        })
    };
    let shared = |sub_type_filter: serde_json::Value| {
        serde_json::json!({
            "&": [
                sub_type_filter,
                {
                    "&": [
                        { "!": { "l": { "o": owner } } },
                        { "l": { "iea": false } }
                    ]
                }
            ]
        })
    };
    let ids = |values: &[&str]| {
        values
            .iter()
            .map(|value| Uuid::parse_str(value).unwrap())
            .collect::<HashSet<_>>()
    };

    let ordinary_owned = "ffffffff-ffff-ffff-ffff-ffffffffffff";
    let ordinary_shared = "dddddddd-dddd-dddd-dddd-dddddddddddd";
    let snippet = "eeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee";
    let attachment_task_a = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
    let attachment_task_b = "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb";

    let cases = [
        (
            "owned/snippets-on",
            owned(not_task.clone()),
            ids(&[ordinary_owned, snippet]),
        ),
        (
            "owned/snippets-off",
            owned(not_task_or_snippet.clone()),
            ids(&[ordinary_owned]),
        ),
        (
            "shared/snippets-on",
            shared(not_task.clone()),
            ids(&[ordinary_shared]),
        ),
        (
            "shared/snippets-off",
            shared(not_task_or_snippet.clone()),
            ids(&[ordinary_shared]),
        ),
        (
            "attachments",
            serde_json::json!({ "l": { "iea": true } }),
            ids(&[attachment_task_a, attachment_task_b]),
        ),
        (
            "all/snippets-on",
            not_task,
            ids(&[ordinary_owned, ordinary_shared, snippet]),
        ),
        (
            "all/snippets-off",
            not_task_or_snippet,
            ids(&[ordinary_owned, ordinary_shared]),
        ),
    ];

    for (name, filter, expected) in cases {
        assert_eq!(
            document_ids_for_phase0_membership_filter(&db, filter).await?,
            expected,
            "{name}"
        );
    }

    Ok(())
}

#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn projection_hydration_carries_viewer_relative_facts_from_flat_and_by_id_rows(
    db: PgPool,
) -> anyhow::Result<()> {
    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
    let flat = expanded_dynamic_cursor_soup_with_projection(
        &db,
        ExpandedDynamicCursorArgs {
            user_id: user_id.copied(),
            limit: 50,
            cursor: Query::Sort(
                SimpleSortMethod::CreatedAt.into(),
                EntityFilterAst::default(),
            ),
            exclude_frecency: false,
        },
    )
    .await?;

    let attachment_id = Uuid::parse_str("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa")?;
    let unimportant_task_id = Uuid::parse_str("bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb")?;
    let ordinary_id = Uuid::parse_str("dddddddd-dddd-dddd-dddd-dddddddddddd")?;
    let attachment_facts = flat
        .iter()
        .find(|hydration| hydration.item.id() == attachment_id)
        .and_then(|hydration| hydration.document_server_facts.clone())
        .expect("attachment document server facts are hydrated");
    let unimportant_task_facts = flat
        .iter()
        .find(|hydration| hydration.item.id() == unimportant_task_id)
        .and_then(|hydration| hydration.document_server_facts.clone())
        .expect("unimportant task server facts are hydrated");
    let ordinary_facts = flat
        .iter()
        .find(|hydration| hydration.item.id() == ordinary_id)
        .and_then(|hydration| hydration.document_server_facts.clone())
        .expect("ordinary document server facts are hydrated");
    assert_eq!(
        attachment_facts,
        SoupDocumentServerFacts {
            is_email_attachment: true,
            is_important: true,
            status_option_ids: vec![StatusOption::NOT_STARTED_UUID],
        }
    );
    assert_eq!(
        unimportant_task_facts,
        SoupDocumentServerFacts {
            is_email_attachment: true,
            is_important: false,
            status_option_ids: vec![StatusOption::IN_PROGRESS_UUID],
        }
    );
    assert_eq!(
        ordinary_facts,
        SoupDocumentServerFacts {
            is_email_attachment: false,
            is_important: true,
            status_option_ids: Vec::new(),
        }
    );

    let entities = [
        EntityType::Document.with_entity_string(attachment_id.to_string()),
        EntityType::Document.with_entity_string(unimportant_task_id.to_string()),
        EntityType::Document.with_entity_string(ordinary_id.to_string()),
    ];
    let by_id = expanded_soup_by_ids_with_projection(&db, user_id, &entities).await?;
    assert_eq!(by_id.len(), 3);
    assert!(by_id.iter().any(|hydration| {
        hydration.item.id() == attachment_id
            && hydration.document_server_facts
                == Some(SoupDocumentServerFacts {
                    is_email_attachment: true,
                    is_important: true,
                    status_option_ids: vec![StatusOption::NOT_STARTED_UUID],
                })
    }));
    assert!(by_id.iter().any(|hydration| {
        hydration.item.id() == unimportant_task_id
            && hydration.document_server_facts
                == Some(SoupDocumentServerFacts {
                    is_email_attachment: true,
                    is_important: false,
                    status_option_ids: vec![StatusOption::IN_PROGRESS_UUID],
                })
    }));
    assert!(by_id.iter().any(|hydration| {
        hydration.item.id() == ordinary_id
            && hydration.document_server_facts
                == Some(SoupDocumentServerFacts {
                    is_email_attachment: false,
                    is_important: true,
                    status_option_ids: Vec::new(),
                })
    }));

    // The relation probe is backed by the composite primary key's leading
    // document_id column. Disable sequential scans so the fixture's tiny table
    // still exposes the representative production access path.
    let mut tx = db.begin().await?;
    sqlx::query!("SET LOCAL enable_seqscan = off")
        .execute(&mut *tx)
        .await?;
    let plan = sqlx::query_scalar!(
        r#"EXPLAIN (FORMAT JSON)
           SELECT EXISTS (
               SELECT 1 FROM document_email de WHERE de.document_id = $1
           )"#,
        attachment_id.to_string(),
    )
    .fetch_one(&mut *tx)
    .await?
    .expect("EXPLAIN returns a JSON plan");
    assert!(
        plan.to_string().contains("document_email_pkey"),
        "attachment existence lookup must use document_email_pkey: {plan}"
    );

    Ok(())
}

#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn projection_status_extraction_treats_non_array_values_as_empty(
    db: PgPool,
) -> anyhow::Result<()> {
    let json_null_status_id = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
    let scalar_status_id = "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb";
    sqlx::query!(
        r#"UPDATE entity_properties
           SET values = CASE entity_id
               WHEN $1 THEN 'null'::jsonb
               ELSE '{"type":"String","value":"not-an-array"}'::jsonb
           END
           WHERE entity_id IN ($1, $2)
             AND entity_type = 'TASK'
             AND property_definition_id = $3"#,
        json_null_status_id,
        scalar_status_id,
        SystemPropertyKey::STATUS_UUID,
    )
    .execute(&db)
    .await?;

    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
    let flat = expanded_dynamic_cursor_soup_with_projection(
        &db,
        ExpandedDynamicCursorArgs {
            user_id: user_id.copied(),
            limit: 50,
            cursor: Query::Sort(
                SimpleSortMethod::CreatedAt.into(),
                EntityFilterAst::default(),
            ),
            exclude_frecency: false,
        },
    )
    .await?;

    for id in [json_null_status_id, scalar_status_id] {
        let id = Uuid::parse_str(id)?;
        let facts = flat
            .iter()
            .find(|hydration| hydration.item.id() == id)
            .and_then(|hydration| hydration.document_server_facts.as_ref())
            .expect("task document server facts are hydrated");
        assert!(facts.status_option_ids.is_empty());
    }

    let entities = [json_null_status_id, scalar_status_id]
        .map(|id| EntityType::Document.with_entity_string(id.to_string()));
    let by_id = expanded_soup_by_ids_with_projection(&db, user_id, &entities).await?;
    assert_eq!(by_id.len(), 2);
    assert!(by_id.iter().all(|hydration| {
        hydration
            .document_server_facts
            .as_ref()
            .is_some_and(|facts| facts.status_option_ids.is_empty())
    }));

    Ok(())
}

// Test filtering documents by is_email_attachment = true (only email attachments)
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_dyn_filter_is_email_attachment_true(db: PgPool) -> anyhow::Result<()> {
    use item_filters::{DocumentFilters, EntityFilters};

    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();

    let entity_filters = EntityFilters {
        document_filters: DocumentFilters {
            is_email_attachment: Some(true),
            ..Default::default()
        },
        ..Default::default()
    };

    let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

    let items = expanded_dynamic_cursor_soup(
        &db,
        ExpandedDynamicCursorArgs {
            user_id: user_id.copied(),
            limit: 20,
            cursor: Query::Sort(SimpleSortMethod::CreatedAt.into(), filters),
            exclude_frecency: false,
        },
    )
    .await?;

    let doc_ids: HashSet<Uuid> = items
        .iter()
        .filter_map(|i| match i {
            SoupItem::Document(doc) => Some(doc.id),
            _ => None,
        })
        .collect();

    let expected: HashSet<Uuid> = [
        "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
        "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb",
    ]
    .iter()
    .map(|s| Uuid::parse_str(s).unwrap())
    .collect();

    assert_eq!(
        doc_ids, expected,
        "is_email_attachment=true should only return documents linked via document_email"
    );

    // Chats and projects are unaffected by document filters
    let chat_count = items
        .iter()
        .filter(|i| matches!(i, SoupItem::Chat(_)))
        .count();
    let project_count = items
        .iter()
        .filter(|i| matches!(i, SoupItem::Project(_)))
        .count();
    assert!(chat_count > 0, "Should still get chats");
    assert!(project_count > 0, "Should still get projects");

    Ok(())
}

// Test filtering documents by is_email_attachment = false (only non-email attachments)
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_dyn_filter_is_email_attachment_false(db: PgPool) -> anyhow::Result<()> {
    use item_filters::{DocumentFilters, EntityFilters};

    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();

    let entity_filters = EntityFilters {
        document_filters: DocumentFilters {
            is_email_attachment: Some(false),
            ..Default::default()
        },
        ..Default::default()
    };

    let filters = EntityFilterAst::new_from_filters(entity_filters)?.unwrap();

    let items = expanded_dynamic_cursor_soup(
        &db,
        ExpandedDynamicCursorArgs {
            user_id: user_id.copied(),
            limit: 20,
            cursor: Query::Sort(SimpleSortMethod::CreatedAt.into(), filters),
            exclude_frecency: false,
        },
    )
    .await?;

    let doc_ids: HashSet<Uuid> = items
        .iter()
        .filter_map(|i| match i {
            SoupItem::Document(doc) => Some(doc.id),
            _ => None,
        })
        .collect();

    // doc-in-A and doc-in-B are email attachments, so they should be excluded
    let email_attachment_ids: HashSet<Uuid> = [
        "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
        "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb",
    ]
    .iter()
    .map(|s| Uuid::parse_str(s).unwrap())
    .collect();

    assert!(
        doc_ids.is_disjoint(&email_attachment_ids),
        "is_email_attachment=false should exclude documents linked via document_email"
    );
    assert!(
        !doc_ids.is_empty(),
        "Should still return non-email-attachment documents"
    );

    Ok(())
}

// Test filtering documents by NOT sub_type = task (excludes tasks, includes non-tasks with NULL sub_type)
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_dyn_filter_by_not_document_sub_type_task(db: PgPool) -> anyhow::Result<()> {
    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();

    // Construct an AST with NOT(sub_type = task) via JSON deserialization
    // This tests the fix for NULL handling: NOT (dt.sub_type IS NOT NULL AND dt.sub_type = 'task')
    // Which correctly expands to: dt.sub_type IS NULL OR dt.sub_type != 'task'
    let filters: EntityFilterAst = serde_json::from_value(serde_json::json!({
        "df": {
            "!": {
                "l": { "dst": "task" }
            }
        }
    }))?;

    let items = expanded_dynamic_cursor_soup(
        &db,
        ExpandedDynamicCursorArgs {
            user_id: user_id.copied(),
            limit: 50,
            cursor: Query::Sort(SimpleSortMethod::CreatedAt.into(), filters),
            exclude_frecency: false,
        },
    )
    .await?;

    let mut task_doc_count = 0;
    let mut non_task_doc_count = 0;

    for item in &items {
        if let SoupItem::Document(doc) = item {
            if doc.sub_type.is_some() {
                task_doc_count += 1;
            } else {
                non_task_doc_count += 1;
            }
        }
    }

    // The NOT filter should exclude all task documents
    assert_eq!(
        task_doc_count, 0,
        "Should get 0 task documents with NOT task filter"
    );
    // Should return non-task documents (those with NULL sub_type)
    assert!(
        non_task_doc_count > 0,
        "Should get non-task documents (NULL sub_type) with NOT task filter"
    );

    Ok(())
}

// ---- Date literal filter tests ----
// Uses `entity_filter_tests` fixture which has:
//   Docs (accessible): aaaa(10:00), bbbb(11:00), cccc(12:00), dddd(13:00), eeee(14:00) on 2023-01-05
//   Chats (accessible): a1a1(10:00), b2b2(11:00), c3c3(12:00), d4d4(13:00) on 2023-01-06
//   Projects (accessible): 1111(Jan-01 10:00), 2222(11:00), 3333(12:00), 4444(Jan-02 10:00)

fn mock_empty_ast() -> EntityFilterAst {
    EntityFilterAst {
        crm_association: None,
        document_filter: None,
        calendar_event_filter: None,
        project_filter: None,
        chat_filter: None,
        email_filter: item_filters::ast::EmailFilterAst::default(),
        channel_filter: None,
        channel_thread_filter: None,
        call_filter: None,
        crm_company_filter: None,
        crm_contact_filter: None,
        foreign_entity_filter: None,
        github_pull_request_filter: None,
        agent_session_filter: None,
        initiative_filter: None,
        database_row_filter: None,
        properties_filter: None,
        favorites_only: None,
    }
}

fn doc_ast(lit: DocumentLiteral) -> EntityFilterAst {
    EntityFilterAst {
        document_filter: Some(Arc::new(Expr::Literal(lit))),
        ..mock_empty_ast()
    }
}

fn chat_ast(lit: ChatLiteral) -> EntityFilterAst {
    EntityFilterAst {
        chat_filter: Some(Arc::new(Expr::Literal(lit))),
        ..mock_empty_ast()
    }
}

fn project_ast(lit: ProjectLiteral) -> EntityFilterAst {
    EntityFilterAst {
        project_filter: Some(Arc::new(Expr::Literal(lit))),
        ..mock_empty_ast()
    }
}

async fn run_and_count(
    db: &PgPool,
    ast: EntityFilterAst,
) -> anyhow::Result<(Vec<SoupItem>, usize, usize, usize)> {
    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
    let items = expanded_dynamic_cursor_soup(
        db,
        ExpandedDynamicCursorArgs {
            user_id: user_id.copied(),
            limit: 20,
            cursor: Query::Sort(SimpleSortMethod::CreatedAt.into(), ast),
            exclude_frecency: false,
        },
    )
    .await?;
    let mut doc_count = 0usize;
    let mut chat_count = 0usize;
    let mut project_count = 0usize;
    for item in &items {
        match item {
            SoupItem::Document(_) => doc_count += 1,
            SoupItem::Chat(_) => chat_count += 1,
            SoupItem::Project(_) => project_count += 1,
            _ => {}
        }
    }
    Ok((items, doc_count, chat_count, project_count))
}

#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_date_filter_document_created_at_gt(db: PgPool) -> anyhow::Result<()> {
    use chrono::DateTime;
    // cutoff between bbbb(11:00) and cccc(12:00) → expect cccc, dddd, eeee = 3 docs
    let cutoff: chrono::DateTime<chrono::Utc> =
        DateTime::parse_from_rfc3339("2023-01-05T11:30:00Z")?.into();
    let (items, doc_count, chat_count, project_count) = run_and_count(
        &db,
        doc_ast(DocumentLiteral::CreatedAt(DateLiteral::GreaterThan(cutoff))),
    )
    .await?;
    for item in &items {
        if let SoupItem::Document(d) = item {
            assert!(
                d.created_at > cutoff,
                "doc {} created_at {:?} should be > cutoff",
                d.id,
                d.created_at
            );
        }
    }
    assert_eq!(doc_count, 3, "expected 3 docs with createdAt after cutoff");
    assert_eq!(chat_count, 4, "chats unaffected by document date filter");
    assert_eq!(
        project_count, 4,
        "projects unaffected by document date filter"
    );
    Ok(())
}

#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_date_filter_document_created_at_lt(db: PgPool) -> anyhow::Result<()> {
    use chrono::DateTime;
    // cutoff between bbbb(11:00) and cccc(12:00) → expect aaaa, bbbb = 2 docs
    let cutoff: chrono::DateTime<chrono::Utc> =
        DateTime::parse_from_rfc3339("2023-01-05T11:30:00Z")?.into();
    let (items, doc_count, chat_count, project_count) = run_and_count(
        &db,
        doc_ast(DocumentLiteral::CreatedAt(DateLiteral::LessThan(cutoff))),
    )
    .await?;
    for item in &items {
        if let SoupItem::Document(d) = item {
            assert!(
                d.created_at < cutoff,
                "doc {} created_at {:?} should be < cutoff",
                d.id,
                d.created_at
            );
        }
    }
    assert_eq!(doc_count, 2, "expected 2 docs with createdAt before cutoff");
    assert_eq!(chat_count, 4, "chats unaffected by document date filter");
    assert_eq!(
        project_count, 4,
        "projects unaffected by document date filter"
    );
    Ok(())
}

#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_date_filter_chat_created_at_gt(db: PgPool) -> anyhow::Result<()> {
    use chrono::DateTime;
    // cutoff between b2b2(11:00) and c3c3(12:00) → expect c3c3, d4d4 = 2 chats
    let cutoff: chrono::DateTime<chrono::Utc> =
        DateTime::parse_from_rfc3339("2023-01-06T11:30:00Z")?.into();
    let (items, doc_count, chat_count, project_count) = run_and_count(
        &db,
        chat_ast(ChatLiteral::CreatedAt(DateLiteral::GreaterThan(cutoff))),
    )
    .await?;
    for item in &items {
        if let SoupItem::Chat(c) = item {
            assert!(
                c.created_at > cutoff,
                "chat {} created_at {:?} should be > cutoff",
                c.id,
                c.created_at
            );
        }
    }
    assert_eq!(doc_count, 5, "docs unaffected by chat date filter");
    assert_eq!(
        chat_count, 2,
        "expected 2 chats with createdAt after cutoff"
    );
    assert_eq!(project_count, 4, "projects unaffected by chat date filter");
    Ok(())
}

#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_date_filter_project_created_at_gt(db: PgPool) -> anyhow::Result<()> {
    use chrono::DateTime;
    // cutoff between 2222(Jan-01 11:00) and 3333(Jan-01 12:00) → expect 3333, 4444 = 2 projects
    let cutoff: chrono::DateTime<chrono::Utc> =
        DateTime::parse_from_rfc3339("2023-01-01T11:30:00Z")?.into();
    let (items, doc_count, chat_count, project_count) = run_and_count(
        &db,
        project_ast(ProjectLiteral::CreatedAt(DateLiteral::GreaterThan(cutoff))),
    )
    .await?;
    for item in &items {
        if let SoupItem::Project(p) = item {
            assert!(
                p.created_at > cutoff,
                "project {} created_at {:?} should be > cutoff",
                p.id,
                p.created_at
            );
        }
    }
    assert_eq!(doc_count, 5, "docs unaffected by project date filter");
    assert_eq!(chat_count, 4, "chats unaffected by project date filter");
    assert_eq!(
        project_count, 2,
        "expected 2 projects with createdAt after cutoff"
    );
    Ok(())
}

// document / createdAt / AND(gt, lt)
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_date_filter_document_created_at_range(db: PgPool) -> anyhow::Result<()> {
    use chrono::DateTime;
    // lower after aaaa(10:00), upper before eeee(14:00) → bbbb, cccc, dddd = 3 docs
    let lower: chrono::DateTime<chrono::Utc> =
        DateTime::parse_from_rfc3339("2023-01-05T10:30:00Z")?.into();
    let upper: chrono::DateTime<chrono::Utc> =
        DateTime::parse_from_rfc3339("2023-01-05T13:30:00Z")?.into();
    let ast = EntityFilterAst {
        document_filter: Some(Arc::new(Expr::And(
            Box::new(Expr::Literal(DocumentLiteral::CreatedAt(
                DateLiteral::GreaterThan(lower),
            ))),
            Box::new(Expr::Literal(DocumentLiteral::CreatedAt(
                DateLiteral::LessThan(upper),
            ))),
        ))),
        ..mock_empty_ast()
    };
    let (items, doc_count, chat_count, project_count) = run_and_count(&db, ast).await?;
    for item in &items {
        if let SoupItem::Document(d) = item {
            assert!(
                d.created_at > lower && d.created_at < upper,
                "doc {} created_at {:?} outside [{:?}, {:?}]",
                d.id,
                d.created_at,
                lower,
                upper
            );
        }
    }
    assert_eq!(doc_count, 3, "expected 3 docs in range");
    assert_eq!(chat_count, 4, "chats unaffected");
    assert_eq!(project_count, 4, "projects unaffected");
    Ok(())
}

// document / updatedAt / gt
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_date_filter_document_updated_at_gt(db: PgPool) -> anyhow::Result<()> {
    use chrono::DateTime;
    // fixture has updatedAt == createdAt; cutoff same as createdAt gt test → cccc, dddd, eeee = 3
    let cutoff: chrono::DateTime<chrono::Utc> =
        DateTime::parse_from_rfc3339("2023-01-05T11:30:00Z")?.into();
    let (items, doc_count, _, _) = run_and_count(
        &db,
        doc_ast(DocumentLiteral::UpdatedAt(DateLiteral::GreaterThan(cutoff))),
    )
    .await?;
    for item in &items {
        if let SoupItem::Document(d) = item {
            assert!(
                d.updated_at > cutoff,
                "doc {} updated_at {:?} should be > cutoff",
                d.id,
                d.updated_at
            );
        }
    }
    assert_eq!(doc_count, 3, "expected 3 docs with updatedAt after cutoff");
    Ok(())
}

// document / updatedAt / lt
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_date_filter_document_updated_at_lt(db: PgPool) -> anyhow::Result<()> {
    use chrono::DateTime;
    let cutoff: chrono::DateTime<chrono::Utc> =
        DateTime::parse_from_rfc3339("2023-01-05T11:30:00Z")?.into();
    let (items, doc_count, _, _) = run_and_count(
        &db,
        doc_ast(DocumentLiteral::UpdatedAt(DateLiteral::LessThan(cutoff))),
    )
    .await?;
    for item in &items {
        if let SoupItem::Document(d) = item {
            assert!(
                d.updated_at < cutoff,
                "doc {} updated_at {:?} should be < cutoff",
                d.id,
                d.updated_at
            );
        }
    }
    assert_eq!(doc_count, 2, "expected 2 docs with updatedAt before cutoff");
    Ok(())
}

// document / updatedAt / AND(gt, lt)
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_date_filter_document_updated_at_range(db: PgPool) -> anyhow::Result<()> {
    use chrono::DateTime;
    let lower: chrono::DateTime<chrono::Utc> =
        DateTime::parse_from_rfc3339("2023-01-05T10:30:00Z")?.into();
    let upper: chrono::DateTime<chrono::Utc> =
        DateTime::parse_from_rfc3339("2023-01-05T13:30:00Z")?.into();
    let ast = EntityFilterAst {
        document_filter: Some(Arc::new(Expr::And(
            Box::new(Expr::Literal(DocumentLiteral::UpdatedAt(
                DateLiteral::GreaterThan(lower),
            ))),
            Box::new(Expr::Literal(DocumentLiteral::UpdatedAt(
                DateLiteral::LessThan(upper),
            ))),
        ))),
        ..mock_empty_ast()
    };
    let (items, doc_count, _, _) = run_and_count(&db, ast).await?;
    for item in &items {
        if let SoupItem::Document(d) = item {
            assert!(
                d.updated_at > lower && d.updated_at < upper,
                "doc {} updated_at {:?} outside range",
                d.id,
                d.updated_at
            );
        }
    }
    assert_eq!(doc_count, 3, "expected 3 docs with updatedAt in range");
    Ok(())
}

// chat / createdAt / lt
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_date_filter_chat_created_at_lt(db: PgPool) -> anyhow::Result<()> {
    use chrono::DateTime;
    // cutoff between b2b2(11:00) and c3c3(12:00) → a1a1, b2b2 = 2 chats
    let cutoff: chrono::DateTime<chrono::Utc> =
        DateTime::parse_from_rfc3339("2023-01-06T11:30:00Z")?.into();
    let (items, _, chat_count, _) = run_and_count(
        &db,
        chat_ast(ChatLiteral::CreatedAt(DateLiteral::LessThan(cutoff))),
    )
    .await?;
    for item in &items {
        if let SoupItem::Chat(c) = item {
            assert!(
                c.created_at < cutoff,
                "chat {} created_at {:?} should be < cutoff",
                c.id,
                c.created_at
            );
        }
    }
    assert_eq!(
        chat_count, 2,
        "expected 2 chats with createdAt before cutoff"
    );
    Ok(())
}

// chat / createdAt / AND(gt, lt)
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_date_filter_chat_created_at_range(db: PgPool) -> anyhow::Result<()> {
    use chrono::DateTime;
    // lower after a1a1(10:00), upper before d4d4(13:00) → b2b2, c3c3 = 2 chats
    let lower: chrono::DateTime<chrono::Utc> =
        DateTime::parse_from_rfc3339("2023-01-06T10:30:00Z")?.into();
    let upper: chrono::DateTime<chrono::Utc> =
        DateTime::parse_from_rfc3339("2023-01-06T12:30:00Z")?.into();
    let ast = EntityFilterAst {
        chat_filter: Some(Arc::new(Expr::And(
            Box::new(Expr::Literal(ChatLiteral::CreatedAt(
                DateLiteral::GreaterThan(lower),
            ))),
            Box::new(Expr::Literal(ChatLiteral::CreatedAt(
                DateLiteral::LessThan(upper),
            ))),
        ))),
        ..mock_empty_ast()
    };
    let (items, _, chat_count, _) = run_and_count(&db, ast).await?;
    for item in &items {
        if let SoupItem::Chat(c) = item {
            assert!(
                c.created_at > lower && c.created_at < upper,
                "chat {} created_at {:?} outside range",
                c.id,
                c.created_at
            );
        }
    }
    assert_eq!(chat_count, 2, "expected 2 chats in createdAt range");
    Ok(())
}

// chat / updatedAt / gt
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_date_filter_chat_updated_at_gt(db: PgPool) -> anyhow::Result<()> {
    use chrono::DateTime;
    let cutoff: chrono::DateTime<chrono::Utc> =
        DateTime::parse_from_rfc3339("2023-01-06T11:30:00Z")?.into();
    let (items, _, chat_count, _) = run_and_count(
        &db,
        chat_ast(ChatLiteral::UpdatedAt(DateLiteral::GreaterThan(cutoff))),
    )
    .await?;
    for item in &items {
        if let SoupItem::Chat(c) = item {
            assert!(
                c.updated_at > cutoff,
                "chat {} updated_at {:?} should be > cutoff",
                c.id,
                c.updated_at
            );
        }
    }
    assert_eq!(
        chat_count, 2,
        "expected 2 chats with updatedAt after cutoff"
    );
    Ok(())
}

// chat / updatedAt / AND(gt, lt)
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_date_filter_chat_updated_at_range(db: PgPool) -> anyhow::Result<()> {
    use chrono::DateTime;
    let lower: chrono::DateTime<chrono::Utc> =
        DateTime::parse_from_rfc3339("2023-01-06T10:30:00Z")?.into();
    let upper: chrono::DateTime<chrono::Utc> =
        DateTime::parse_from_rfc3339("2023-01-06T12:30:00Z")?.into();
    let ast = EntityFilterAst {
        chat_filter: Some(Arc::new(Expr::And(
            Box::new(Expr::Literal(ChatLiteral::UpdatedAt(
                DateLiteral::GreaterThan(lower),
            ))),
            Box::new(Expr::Literal(ChatLiteral::UpdatedAt(
                DateLiteral::LessThan(upper),
            ))),
        ))),
        ..mock_empty_ast()
    };
    let (items, _, chat_count, _) = run_and_count(&db, ast).await?;
    for item in &items {
        if let SoupItem::Chat(c) = item {
            assert!(
                c.updated_at > lower && c.updated_at < upper,
                "chat {} updated_at {:?} outside range",
                c.id,
                c.updated_at
            );
        }
    }
    assert_eq!(chat_count, 2, "expected 2 chats with updatedAt in range");
    Ok(())
}

// project / createdAt / lt
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_date_filter_project_created_at_lt(db: PgPool) -> anyhow::Result<()> {
    use chrono::DateTime;
    // cutoff between 2222(Jan-01 11:00) and 3333(Jan-01 12:00) → 1111, 2222 = 2 projects
    let cutoff: chrono::DateTime<chrono::Utc> =
        DateTime::parse_from_rfc3339("2023-01-01T11:30:00Z")?.into();
    let (items, _, _, project_count) = run_and_count(
        &db,
        project_ast(ProjectLiteral::CreatedAt(DateLiteral::LessThan(cutoff))),
    )
    .await?;
    for item in &items {
        if let SoupItem::Project(p) = item {
            assert!(
                p.created_at < cutoff,
                "project {} created_at {:?} should be < cutoff",
                p.id,
                p.created_at
            );
        }
    }
    assert_eq!(
        project_count, 2,
        "expected 2 projects with createdAt before cutoff"
    );
    Ok(())
}

// project / createdAt / AND(gt, lt)
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_date_filter_project_created_at_range(db: PgPool) -> anyhow::Result<()> {
    use chrono::DateTime;
    // lower after 1111(Jan-01 10:00), upper before 4444(Jan-02 10:00) → 2222, 3333 = 2 projects
    let lower: chrono::DateTime<chrono::Utc> =
        DateTime::parse_from_rfc3339("2023-01-01T10:30:00Z")?.into();
    let upper: chrono::DateTime<chrono::Utc> =
        DateTime::parse_from_rfc3339("2023-01-01T12:30:00Z")?.into();
    let ast = EntityFilterAst {
        project_filter: Some(Arc::new(Expr::And(
            Box::new(Expr::Literal(ProjectLiteral::CreatedAt(
                DateLiteral::GreaterThan(lower),
            ))),
            Box::new(Expr::Literal(ProjectLiteral::CreatedAt(
                DateLiteral::LessThan(upper),
            ))),
        ))),
        ..mock_empty_ast()
    };
    let (items, _, _, project_count) = run_and_count(&db, ast).await?;
    for item in &items {
        if let SoupItem::Project(p) = item {
            assert!(
                p.created_at > lower && p.created_at < upper,
                "project {} created_at {:?} outside range",
                p.id,
                p.created_at
            );
        }
    }
    assert_eq!(
        project_count, 2,
        "expected 2 projects with createdAt in range"
    );
    Ok(())
}

// project / updatedAt / gt
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_date_filter_project_updated_at_gt(db: PgPool) -> anyhow::Result<()> {
    use chrono::DateTime;
    let cutoff: chrono::DateTime<chrono::Utc> =
        DateTime::parse_from_rfc3339("2023-01-01T11:30:00Z")?.into();
    let (items, _, _, project_count) = run_and_count(
        &db,
        project_ast(ProjectLiteral::UpdatedAt(DateLiteral::GreaterThan(cutoff))),
    )
    .await?;
    for item in &items {
        if let SoupItem::Project(p) = item {
            assert!(
                p.updated_at > cutoff,
                "project {} updated_at {:?} should be > cutoff",
                p.id,
                p.updated_at
            );
        }
    }
    assert_eq!(
        project_count, 2,
        "expected 2 projects with updatedAt after cutoff"
    );
    Ok(())
}

// project / updatedAt / AND(gt, lt)
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_date_filter_project_updated_at_range(db: PgPool) -> anyhow::Result<()> {
    use chrono::DateTime;
    let lower: chrono::DateTime<chrono::Utc> =
        DateTime::parse_from_rfc3339("2023-01-01T10:30:00Z")?.into();
    let upper: chrono::DateTime<chrono::Utc> =
        DateTime::parse_from_rfc3339("2023-01-01T12:30:00Z")?.into();
    let ast = EntityFilterAst {
        project_filter: Some(Arc::new(Expr::And(
            Box::new(Expr::Literal(ProjectLiteral::UpdatedAt(
                DateLiteral::GreaterThan(lower),
            ))),
            Box::new(Expr::Literal(ProjectLiteral::UpdatedAt(
                DateLiteral::LessThan(upper),
            ))),
        ))),
        ..mock_empty_ast()
    };
    let (items, _, _, project_count) = run_and_count(&db, ast).await?;
    for item in &items {
        if let SoupItem::Project(p) = item {
            assert!(
                p.updated_at > lower && p.updated_at < upper,
                "project {} updated_at {:?} outside range",
                p.id,
                p.updated_at
            );
        }
    }
    assert_eq!(
        project_count, 2,
        "expected 2 projects with updatedAt in range"
    );
    Ok(())
}

async fn insert_notification(
    db: &PgPool,
    notification_id: &str,
    user_id: &str,
    item_type: &str,
    item_id: &str,
    done: bool,
    seen: bool,
) -> anyhow::Result<()> {
    sqlx::query!(
        r#"
        INSERT INTO notification (
            id,
            notification_event_type,
            event_item_id,
            event_item_type,
            service_sender,
            created_at,
            metadata
        )
        VALUES ($1::uuid, 'test', $2, $3, 'test', NOW(), '{}')
        "#,
        Uuid::parse_str(notification_id)?,
        item_id,
        item_type,
    )
    .execute(db)
    .await?;

    sqlx::query!(
        r#"
        INSERT INTO user_notification (
            user_id,
            notification_id,
            state,
            seen_at,
            deleted_at
        )
        VALUES ($1, $2::uuid, CASE WHEN $3::bool THEN 'done'::notification_state
            WHEN $4::bool THEN 'seen'::notification_state ELSE 'unseen'::notification_state END,
            CASE WHEN $4 THEN NOW() ELSE NULL END, NULL)
        "#,
        user_id,
        Uuid::parse_str(notification_id)?,
        done,
        seen,
    )
    .execute(db)
    .await?;

    Ok(())
}

async fn run_notification_filter(
    db: &PgPool,
    ast: EntityFilterAst,
) -> anyhow::Result<HashSet<Uuid>> {
    let user_id = MacroUserIdStr::parse_from_str("macro|[email protected]").unwrap();
    let items = expanded_dynamic_cursor_soup(
        db,
        ExpandedDynamicCursorArgs {
            user_id: user_id.copied(),
            limit: 100,
            cursor: Query::Sort(SimpleSortMethod::UpdatedAt.into(), ast),
            exclude_frecency: false,
        },
    )
    .await?;

    Ok(items.into_iter().map(|item| item.id()).collect())
}

#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_notification_optimization_preserves_access_control(db: PgPool) -> anyhow::Result<()> {
    let user_id = "macro|[email protected]";

    // Accessible items from entity_filter_tests.
    let accessible_doc = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
    let accessible_chat = "a1a1a1a1-a1a1-a1a1-a1a1-a1a1a1a1a1a1";
    let accessible_project = "11111111-1111-1111-1111-111111111111";

    // Existing fixture rows intentionally not present in entity_access for user-1.
    let inaccessible_doc = "ffffffff-ffff-ffff-ffff-ffffffffffff";
    let inaccessible_chat = "e5e5e5e5-e5e5-e5e5-e5e5-e5e5e5e5e5e5";
    let inaccessible_project = "55555555-5555-5555-5555-555555555555";

    for (notification_id, item_type, item_id) in [
        (
            "10000000-0000-0000-0000-000000000001",
            "document",
            accessible_doc,
        ),
        (
            "10000000-0000-0000-0000-000000000002",
            "chat",
            accessible_chat,
        ),
        (
            "10000000-0000-0000-0000-000000000003",
            "project",
            accessible_project,
        ),
        (
            "10000000-0000-0000-0000-000000000004",
            "document",
            inaccessible_doc,
        ),
        (
            "10000000-0000-0000-0000-000000000005",
            "chat",
            inaccessible_chat,
        ),
        (
            "10000000-0000-0000-0000-000000000006",
            "project",
            inaccessible_project,
        ),
    ] {
        insert_notification(
            &db,
            notification_id,
            user_id,
            item_type,
            item_id,
            false,
            false,
        )
        .await?;
    }

    let ast = EntityFilterAst {
        document_filter: Some(Arc::new(Expr::or(
            filter_ast::Expr::val(DocumentLiteral::NotificationState(
                item_filters::NotificationState::Unseen,
            )),
            filter_ast::Expr::val(DocumentLiteral::NotificationState(
                item_filters::NotificationState::Seen,
            )),
        ))),
        chat_filter: Some(Arc::new(Expr::or(
            filter_ast::Expr::val(ChatLiteral::NotificationState(
                item_filters::NotificationState::Unseen,
            )),
            filter_ast::Expr::val(ChatLiteral::NotificationState(
                item_filters::NotificationState::Seen,
            )),
        ))),
        project_filter: Some(Arc::new(Expr::or(
            filter_ast::Expr::val(ProjectLiteral::NotificationState(
                item_filters::NotificationState::Unseen,
            )),
            filter_ast::Expr::val(ProjectLiteral::NotificationState(
                item_filters::NotificationState::Seen,
            )),
        ))),
        ..mock_empty_ast()
    };

    let ids = run_notification_filter(&db, ast).await?;

    assert!(ids.contains(&Uuid::parse_str(accessible_doc)?));
    assert!(ids.contains(&Uuid::parse_str(accessible_chat)?));
    assert!(ids.contains(&Uuid::parse_str(accessible_project)?));

    assert!(!ids.contains(&Uuid::parse_str(inaccessible_doc)?));
    assert!(!ids.contains(&Uuid::parse_str(inaccessible_chat)?));
    assert!(!ids.contains(&Uuid::parse_str(inaccessible_project)?));

    Ok(())
}

#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_optimized_notification_filter_matches_unoptimized_equivalent(
    db: PgPool,
) -> anyhow::Result<()> {
    let user_id = "macro|[email protected]";
    for (notification_id, item_type, item_id) in [
        (
            "20000000-0000-0000-0000-000000000001",
            "document",
            "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
        ),
        (
            "20000000-0000-0000-0000-000000000002",
            "document",
            "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb",
        ),
        (
            "20000000-0000-0000-0000-000000000003",
            "chat",
            "a1a1a1a1-a1a1-a1a1-a1a1-a1a1a1a1a1a1",
        ),
        (
            "20000000-0000-0000-0000-000000000004",
            "project",
            "11111111-1111-1111-1111-111111111111",
        ),
    ] {
        insert_notification(
            &db,
            notification_id,
            user_id,
            item_type,
            item_id,
            false,
            false,
        )
        .await?;
    }

    let optimized = EntityFilterAst {
        document_filter: Some(Arc::new(Expr::or(
            filter_ast::Expr::val(DocumentLiteral::NotificationState(
                item_filters::NotificationState::Unseen,
            )),
            filter_ast::Expr::val(DocumentLiteral::NotificationState(
                item_filters::NotificationState::Seen,
            )),
        ))),
        chat_filter: Some(Arc::new(Expr::or(
            filter_ast::Expr::val(ChatLiteral::NotificationState(
                item_filters::NotificationState::Unseen,
            )),
            filter_ast::Expr::val(ChatLiteral::NotificationState(
                item_filters::NotificationState::Seen,
            )),
        ))),
        project_filter: Some(Arc::new(Expr::or(
            filter_ast::Expr::val(ProjectLiteral::NotificationState(
                item_filters::NotificationState::Unseen,
            )),
            filter_ast::Expr::val(ProjectLiteral::NotificationState(
                item_filters::NotificationState::Seen,
            )),
        ))),
        ..mock_empty_ast()
    };

    // This is logically equivalent to Unseen OR Seen, but because the
    // notification predicate appears under OR it intentionally stays on the old
    // correlated-EXISTS path.
    let unoptimized_doc = Expr::Or(
        Box::new(Expr::or(
            filter_ast::Expr::val(DocumentLiteral::NotificationState(
                item_filters::NotificationState::Unseen,
            )),
            filter_ast::Expr::val(DocumentLiteral::NotificationState(
                item_filters::NotificationState::Seen,
            )),
        )),
        Box::new(Expr::And(
            Box::new(Expr::or(
                filter_ast::Expr::val(DocumentLiteral::NotificationState(
                    item_filters::NotificationState::Unseen,
                )),
                filter_ast::Expr::val(DocumentLiteral::NotificationState(
                    item_filters::NotificationState::Seen,
                )),
            )),
            Box::new(Expr::Literal(DocumentLiteral::UpdatedAt(
                DateLiteral::GreaterThan(
                    chrono::DateTime::parse_from_rfc3339("2000-01-01T00:00:00Z")?.into(),
                ),
            ))),
        )),
    );
    let unoptimized_chat = Expr::Or(
        Box::new(Expr::or(
            filter_ast::Expr::val(ChatLiteral::NotificationState(
                item_filters::NotificationState::Unseen,
            )),
            filter_ast::Expr::val(ChatLiteral::NotificationState(
                item_filters::NotificationState::Seen,
            )),
        )),
        Box::new(Expr::And(
            Box::new(Expr::or(
                filter_ast::Expr::val(ChatLiteral::NotificationState(
                    item_filters::NotificationState::Unseen,
                )),
                filter_ast::Expr::val(ChatLiteral::NotificationState(
                    item_filters::NotificationState::Seen,
                )),
            )),
            Box::new(Expr::Literal(ChatLiteral::UpdatedAt(
                DateLiteral::GreaterThan(
                    chrono::DateTime::parse_from_rfc3339("2000-01-01T00:00:00Z")?.into(),
                ),
            ))),
        )),
    );
    let unoptimized_project = Expr::Or(
        Box::new(Expr::or(
            filter_ast::Expr::val(ProjectLiteral::NotificationState(
                item_filters::NotificationState::Unseen,
            )),
            filter_ast::Expr::val(ProjectLiteral::NotificationState(
                item_filters::NotificationState::Seen,
            )),
        )),
        Box::new(Expr::And(
            Box::new(Expr::or(
                filter_ast::Expr::val(ProjectLiteral::NotificationState(
                    item_filters::NotificationState::Unseen,
                )),
                filter_ast::Expr::val(ProjectLiteral::NotificationState(
                    item_filters::NotificationState::Seen,
                )),
            )),
            Box::new(Expr::Literal(ProjectLiteral::UpdatedAt(
                DateLiteral::GreaterThan(
                    chrono::DateTime::parse_from_rfc3339("2000-01-01T00:00:00Z")?.into(),
                ),
            ))),
        )),
    );
    let unoptimized = EntityFilterAst {
        document_filter: Some(Arc::new(unoptimized_doc)),
        chat_filter: Some(Arc::new(unoptimized_chat)),
        project_filter: Some(Arc::new(unoptimized_project)),
        ..mock_empty_ast()
    };

    let optimized_ids = run_notification_filter(&db, optimized).await?;
    let unoptimized_ids = run_notification_filter(&db, unoptimized).await?;

    assert_eq!(optimized_ids, unoptimized_ids);

    Ok(())
}

const DOC_A: &str = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
const DOC_B: &str = "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb";
const DOC_D: &str = "dddddddd-dddd-dddd-dddd-dddddddddddd";

/// The fixture's attachments hang off messages that do not exist. Give doc A's
/// attachment a message from the user to [email protected] and doc B's a message
/// from [email protected], and point doc D's Companies property at `company`.
async fn seed_crm_document_associations(db: &PgPool, company: Uuid) -> anyhow::Result<()> {
    let mut tx = db.begin().await?;
    sqlx::raw_sql("SET LOCAL session_replication_role = 'replica'")
        .execute(&mut *tx)
        .await?;
    let link = Uuid::now_v7();
    let (me, vendor, other) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7());
    for (id, address) in [
        (me, "[email protected]"),
        (vendor, "[email protected]"),
        (other, "[email protected]"),
    ] {
        sqlx::query!(
            "INSERT INTO email_contacts (id, link_id, email_address) VALUES ($1, $2, $3)",
            id,
            link,
            address,
        )
        .execute(&mut *tx)
        .await?;
    }
    let to_acme = Uuid::parse_str("ea000001-0000-0000-0000-000000000099")?;
    let from_globex = Uuid::now_v7();
    for (message, sender) in [(to_acme, me), (from_globex, other)] {
        sqlx::query!(
            "INSERT INTO email_messages (id, thread_id, link_id, from_contact_id) VALUES ($1, $2, $3, $4)",
            message,
            Uuid::now_v7(),
            link,
            sender,
        )
        .execute(&mut *tx)
        .await?;
    }
    sqlx::query!(
        "INSERT INTO email_message_recipients (message_id, contact_id, recipient_type) VALUES ($1, $2, 'TO')",
        to_acme,
        vendor,
    )
    .execute(&mut *tx)
    .await?;
    sqlx::query!(
        "UPDATE email_attachments SET message_id = $1 WHERE id = 'ea000001-0000-0000-0000-000000000002'",
        from_globex,
    )
    .execute(&mut *tx)
    .await?;
    sqlx::query!(
        r#"INSERT INTO entity_properties (id, entity_id, entity_type, property_definition_id, values)
           VALUES ($1, $2, 'DOCUMENT', $3, $4)"#,
        Uuid::now_v7(),
        DOC_D,
        SystemPropertyKey::COMPANIES_UUID,
        serde_json::json!({
            "type": "EntityReference",
            "value": [{ "entity_type": "COMPANY", "entity_id": company.to_string() }]
        }),
    )
    .execute(&mut *tx)
    .await?;
    tx.commit().await?;
    Ok(())
}

async fn matching_document_ids(db: &PgPool, filter: Expr<DocumentLiteral>) -> HashSet<String> {
    let (items, ..) = run_and_count(
        db,
        EntityFilterAst {
            document_filter: Some(Arc::new(filter)),
            ..mock_empty_ast()
        },
    )
    .await
    .unwrap();
    items
        .into_iter()
        .filter_map(|item| match item {
            SoupItem::Document(doc) => Some(doc.id.to_string()),
            _ => None,
        })
        .collect()
}

fn ids(values: &[&str]) -> HashSet<String> {
    values.iter().map(ToString::to_string).collect()
}

#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn crm_document_literals_match_attachments_and_associations(
    db: PgPool,
) -> anyhow::Result<()> {
    use item_filters::ast::{
        email::Email,
        properties::{EntityRefId, PropertiesLiteral, PropertyMatchValue},
    };
    use macro_user_id::email::EmailStr;

    let company = Uuid::now_v7();
    seed_crm_document_associations(&db, company).await?;
    let participant =
        |email: Email| Expr::Literal(DocumentLiteral::EmailAttachmentParticipant(email));
    let associated = Expr::Literal(DocumentLiteral::Property(PropertiesLiteral {
        property_definition_id: SystemPropertyKey::COMPANIES_UUID,
        entity_type: None,
        value: PropertyMatchValue::EntityRef(EntityRefId::new(company.to_string())?),
    }));

    // Recipients count as participants, and domains match case-insensitively.
    assert_eq!(
        matching_document_ids(&db, participant(Email::Domain("ACME.com".to_string()))).await,
        ids(&[DOC_A])
    );
    // So do senders, by exact address.
    let globex = EmailStr::parse_from_str("[email protected]")?.into_owned();
    assert_eq!(
        matching_document_ids(&db, participant(Email::Complete(globex))).await,
        ids(&[DOC_B])
    );
    assert_eq!(
        matching_document_ids(&db, participant(Email::Partial("vendor@".to_string()))).await,
        ids(&[DOC_A])
    );
    // A company's files: attachments with its people OR documents associated with it.
    assert_eq!(
        matching_document_ids(
            &db,
            Expr::or(
                participant(Email::Domain("acme.com".to_string())),
                associated.clone()
            )
        )
        .await,
        ids(&[DOC_A, DOC_D])
    );
    // Negation keeps every other document.
    let not_acme = matching_document_ids(
        &db,
        Expr::is_not(participant(Email::Domain("acme.com".to_string()))),
    )
    .await;
    assert!(!not_acme.contains(DOC_A));
    assert!(not_acme.contains(DOC_B) && not_acme.contains(DOC_D));
    Ok(())
}

#[sqlx::test(
    migrator = "MACRO_DB_MIGRATIONS",
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("soup_items_with_properties")
    )
)]
async fn date_property_boundaries_presence_and_clearing(db: Pool<Postgres>) -> anyhow::Result<()> {
    use item_filters::ast::properties::DateRange;
    let at =
        chrono::DateTime::parse_from_rfc3339("2025-12-31T23:59:59Z")?.with_timezone(&chrono::Utc);
    let after = at + chrono::Duration::seconds(1);
    let before = at - chrono::Duration::seconds(1);
    let doc_a = Uuid::parse_str("11111111-aaaa-aaaa-aaaa-aaaaaaaaaaaa")?;
    let doc_b = Uuid::parse_str("11111111-bbbb-bbbb-bbbb-bbbbbbbbbbbb")?;
    let user_id = sqlx::query_scalar!(
        r#"SELECT owner FROM "Document" WHERE id = $1"#,
        doc_a.to_string(),
    )
    .fetch_one(&db)
    .await?;
    sqlx::query!(
        r#"INSERT INTO entity_properties (id, entity_id, entity_type, property_definition_id, values)
           VALUES ($1, $2, 'DOCUMENT', '00000001-0000-0000-0000-000000000004',
                   '{"type":"String","value":"not a timestamp"}'::jsonb)"#,
        Uuid::now_v7(),
        doc_a.to_string(),
    )
    .execute(&db)
    .await?;
    for (range, has_date, expected) in [
        (
            Some(DateRange {
                start: Some(at),
                end: Some(after),
            }),
            None,
            Some(doc_b),
        ),
        (
            Some(DateRange {
                start: Some(before),
                end: Some(at),
            }),
            None,
            None,
        ),
        (
            Some(DateRange {
                start: None,
                end: Some(after),
            }),
            None,
            Some(doc_b),
        ),
        (
            Some(DateRange {
                start: None,
                end: Some(at),
            }),
            None,
            None,
        ),
        (
            Some(DateRange {
                start: Some(at),
                end: None,
            }),
            None,
            Some(doc_b),
        ),
        (
            Some(DateRange {
                start: Some(after),
                end: None,
            }),
            None,
            None,
        ),
        (
            Some(DateRange {
                start: None,
                end: None,
            }),
            None,
            Some(doc_b),
        ),
        (None, Some(true), Some(doc_b)),
        (None, Some(false), Some(doc_a)),
    ] {
        let ast = EntityFilterAst::new_from_filters(item_filters::EntityFilters {
            property_filters: vec![PropertyFilter {
                property_definition_id: "00000001-0000-0000-0000-000000000004".into(),
                entity_type: Some("DOCUMENT".into()),
                date_range: range,
                has_date,
                ..Default::default()
            }],
            ..Default::default()
        })?
        .unwrap();
        let items = dyn_fetch(&db, &user_id, 1, SimpleSortMethod::UpdatedAt, ast, false).await?;
        let ids: Vec<_> = items
            .iter()
            .filter_map(|item| match item {
                SoupItem::Document(doc) => Some(doc.id),
                _ => None,
            })
            .collect();
        assert_eq!(ids.first().copied(), expected);
    }
    sqlx::query!("UPDATE entity_properties SET values = NULL WHERE entity_id = '11111111-bbbb-bbbb-bbbb-bbbbbbbbbbbb' AND property_definition_id = '00000001-0000-0000-0000-000000000004'")
        .execute(&db).await?;
    let ast = EntityFilterAst::new_from_filters(item_filters::EntityFilters {
        property_filters: vec![PropertyFilter {
            property_definition_id: "00000001-0000-0000-0000-000000000004".into(),
            entity_type: Some("DOCUMENT".into()),
            has_date: Some(false),
            ..Default::default()
        }],
        ..Default::default()
    })?
    .unwrap();
    let items = dyn_fetch(&db, &user_id, 100, SimpleSortMethod::UpdatedAt, ast, false).await?;
    let ids: HashSet<_> = items
        .iter()
        .filter_map(|item| match item {
            SoupItem::Document(doc) => Some(doc.id),
            _ => None,
        })
        .collect();
    assert!(ids.contains(&doc_a) && ids.contains(&doc_b));
    Ok(())
}

/// The Home unread feed sends `(unseen OR seen) AND unseen` for every entity
/// type. The notification join it now takes must agree with the per-row fold
/// of an equivalent tree the stripper leaves alone.
#[sqlx::test(
    fixtures(
        path = "../../../../../macro_db_client/fixtures",
        scripts("entity_filter_tests")
    ),
    migrator = "MACRO_DB_MIGRATIONS"
)]
async fn test_unread_conjunction_matches_unoptimized_equivalent(db: PgPool) -> anyhow::Result<()> {
    use item_filters::NotificationState::{Seen, Unseen};
    let unseen_doc = "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa";
    let seen_doc = "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb";
    let unseen_chat = "a1a1a1a1-a1a1-a1a1-a1a1-a1a1a1a1a1a1";
    let seen_project = "11111111-1111-1111-1111-111111111111";
    // The viewer `run_notification_filter` queries as owns this document.
    let user_id = sqlx::query_scalar!(
        r#"SELECT owner FROM "Document" WHERE id = 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa'"#
    )
    .fetch_one(&db)
    .await?;
    for (notification_id, item_type, item_id, seen) in [
        (
            "30000000-0000-0000-0000-000000000001",
            "document",
            unseen_doc,
            false,
        ),
        (
            "30000000-0000-0000-0000-000000000002",
            "document",
            seen_doc,
            true,
        ),
        (
            "30000000-0000-0000-0000-000000000003",
            "chat",
            unseen_chat,
            false,
        ),
        (
            "30000000-0000-0000-0000-000000000004",
            "project",
            seen_project,
            true,
        ),
    ] {
        insert_notification(
            &db,
            notification_id,
            &user_id,
            item_type,
            item_id,
            false,
            seen,
        )
        .await?;
    }

    let doc = |state| Expr::val(DocumentLiteral::NotificationState(state));
    let chat = |state| Expr::val(ChatLiteral::NotificationState(state));
    let project = |state| Expr::val(ProjectLiteral::NotificationState(state));
    let optimized = EntityFilterAst {
        document_filter: Some(Arc::new(Expr::and(
            Expr::or(doc(Unseen), doc(Seen)),
            doc(Unseen),
        ))),
        chat_filter: Some(Arc::new(Expr::and(
            Expr::or(chat(Unseen), chat(Seen)),
            chat(Unseen),
        ))),
        project_filter: Some(Arc::new(Expr::and(
            Expr::or(project(Unseen), project(Seen)),
            project(Unseen),
        ))),
        ..mock_empty_ast()
    };
    // Equivalent, but a notification literal under OR with another predicate
    // stays on the correlated-EXISTS path.
    let unoptimized = EntityFilterAst {
        document_filter: Some(Arc::new(Expr::or(
            doc(Unseen),
            Expr::and(doc(Unseen), Expr::val(DocumentLiteral::Importance(true))),
        ))),
        chat_filter: Some(Arc::new(Expr::or(
            chat(Unseen),
            Expr::and(chat(Unseen), Expr::val(ChatLiteral::Importance(true))),
        ))),
        project_filter: Some(Arc::new(Expr::or(
            project(Unseen),
            Expr::and(project(Unseen), Expr::val(ProjectLiteral::Importance(true))),
        ))),
        ..mock_empty_ast()
    };

    let optimized_ids = run_notification_filter(&db, optimized).await?;
    let unoptimized_ids = run_notification_filter(&db, unoptimized).await?;

    assert_eq!(optimized_ids, unoptimized_ids);
    assert!(optimized_ids.contains(&uuid(unseen_doc)));
    assert!(optimized_ids.contains(&uuid(unseen_chat)));
    assert!(!optimized_ids.contains(&uuid(seen_doc)));
    assert!(!optimized_ids.contains(&uuid(seen_project)));
    Ok(())
}