Repository: meilisearch/meilisearch
File: crates/meilisearch/tests/index/get_index.rs (Rust)

Code before:
    assert_eq!(code, 200);
    assert!(response["results"].is_array());
    assert!(response["results"].as_array().unwrap().is_empty());
}

#[actix_rt::test]
async fn list_multiple_indexes() {
    let server = Server::new_shared();
    let index1 = server.unique_index();
    let index2 = server.unique_index();

    let (task1, _) = index1.create(None).await;
    let (_task2, _) = index2.create(Some("key")).await;

    index1.wait_task(task1.uid()).await.succeeded();

    let (response, code) = server.list_indexes(None, None).await;
    dbg!(response.clone());
    assert_eq!(code, 200);
    assert!(response["results"].is_array());
    let arr = response["results"].as_array().unwrap();
    assert!(arr.len() >= 2);
    assert!(arr
        .iter()
        .any(|entry| entry["uid"] == index1.uid && entry["primaryKey"] == Value::Null));
    assert!(arr.iter().any(|entry| entry["uid"] == index2.uid && entry["primaryKey"] == "key"));
}

#[actix_rt::test]
async fn get_and_paginate_indexes() {
    let server = Server::new().await;
    const NB_INDEXES: usize = 50;
    for i in 0..NB_INDEXES {
        server.index(&format!("test_{i:02}")).create(None).await;
        server.index(&format!("test_{i:02}")).wait_task(i as u64).await;
    }

    // basic
    let (response, code) = server.list_indexes(None, None).await;
    assert_eq!(code, 200);
    assert_eq!(response["limit"], json!(20));
    assert_eq!(response["offset"], json!(0));
    assert_eq!(response["total"], json!(NB_INDEXES));
    assert!(response["results"].is_array());
    let arr = response["results"].as_array().unwrap();
    assert_eq!(arr.len(), 20);
    // ensuring we get all the indexes in the alphabetical order
    assert!((0..20)
        .map(|idx| format!("test_{idx:02}"))
        .zip(arr)
        .all(|(expected, entry)| entry["uid"] == expected));

Review comment (suggestion):
I think you're right, for this test we should not use a shared index otherwise it's practically impossible to do these asserts

Code after:
    assert!(response["results"].is_array());
    assert!(response["results"].as_array().unwrap().is_empty());
}

#[actix_rt::test]
async fn list_multiple_indexes() {
    let server = Server::new().await;
    server.index("test").create(None).await;
    let (task, _status_code) = server.index("test1").create(Some("key")).await;

    server.index("test").wait_task(task.uid()).await.succeeded();

    let (response, code) = server.list_indexes(None, None).await;
    assert_eq!(code, 200);
    assert!(response["results"].is_array());
    let arr = response["results"].as_array().unwrap();
    assert_eq!(arr.len(), 2);
    assert!(arr.iter().any(|entry| entry["uid"] == "test" && entry["primaryKey"] == Value::Null));
    assert!(arr.iter().any(|entry| entry["uid"] == "test1" && entry["primaryKey"] == "key"));
}

#[actix_rt::test]
async fn get_and_paginate_indexes() {
    let server = Server::new().await;
    const NB_INDEXES: usize = 50;
    for i in 0..NB_INDEXES {
        server.index(format!("test_{i:02}")).create(None).await;
        server.index(format!("test_{i:02}")).wait_task(i as u64).await;
    }

    // basic
    let (response, code) = server.list_indexes(None, None).await;
    assert_eq!(code, 200);
    assert_eq!(response["limit"], json!(20));
    assert_eq!(response["offset"], json!(0));
    assert_eq!(response["total"], json!(NB_INDEXES));
    assert!(response["results"].is_array());
    let arr = response["results"].as_array().unwrap();
    assert_eq!(arr.len(), 20);
    // ensuring we get all the indexes in the alphabetical order
    assert!((0..20)
        .map(|idx| format!("test_{idx:02}"))
        .zip(arr)
        .all(|(expected, entry)| entry["uid"] == expected));

    // with an offset
    let (response, code) = server.list_indexes(Some(15), None).await;
    assert_eq!(code, 200);
    assert_eq!(response["limit"], json!(20));
    assert_eq!(response["offset"], json!(15));
    assert_eq!(response["total"], json!(NB_INDEXES));