mod common;
mod tests {
use crate::common::AnythingLLMFixture;
use ayda::anythingllm::client::AnythingLLMClient;
use ayda::app::commands::workspace::import::UpdateParameter;
use ayda::Config;
use std::path::PathBuf;
#[tokio::test]
#[tracing_test::traced_test]
async fn test_get_workspaces() {
let fixture = AnythingLLMFixture::new();
fixture
.with_fixture(|w, c| async move {
let workspaces = c.get_workspaces().await.unwrap();
let workspace_slug = &w.slug;
assert!(workspaces.len() > 0);
assert!(workspaces
.iter()
.any(|w| w.slug == workspace_slug.to_string()));
})
.await;
}
#[tokio::test]
#[tracing_test::traced_test]
async fn test_create_workspace() {
let config = Config::from_file().unwrap();
let client = AnythingLLMClient::new(
&config.anythingllm_ip,
&config.anythingllm_port,
&config.anythingllm_api_key,
);
let before_count = client.get_workspaces().await.unwrap().len();
let fixture = AnythingLLMFixture::new();
fixture
.with_fixture(|w, c| async move {
let after_count = c.get_workspaces().await.unwrap().len();
})
.await;
}
#[tokio::test]
#[tracing_test::traced_test]
async fn test_get_workspace_by_slug() {
let fixture = AnythingLLMFixture::new();
fixture
.with_fixture(|w, c| async move {
let test_workspace_slug = &w.slug;
let workspace = c.get_workspace_by_slug(&test_workspace_slug).await.unwrap();
assert_eq!(workspace.slug, test_workspace_slug.to_string());
})
.await;
}
#[tokio::test]
#[tracing_test::traced_test]
async fn test_get_workspace_by_invalid_slug() {
let fixture = AnythingLLMFixture::new();
fixture
.with_fixture(|w, c| async move {
let workspace = c.get_workspace_by_slug("invalid-workspace-slug").await;
assert!(workspace.is_err());
})
.await;
}
#[tokio::test]
#[tracing_test::traced_test]
async fn test_workspace_from_name_is_valid() {
let fixture = AnythingLLMFixture::new();
fixture
.with_fixture(|w, c| async move {
let workspace = c.get_workspace_by_name(&w.name).await.unwrap();
dbg!(&workspace);
assert_eq!(workspace.id, w.id);
})
.await;
}
#[tokio::test]
#[tracing_test::traced_test]
async fn test_workspace_from_name_is_invalid() {
let fixture = AnythingLLMFixture::new();
fixture
.with_fixture(|w, c| async move {
let workspace = c.get_workspace_by_name("Invalid workspace name").await;
assert!(workspace.is_err());
})
.await;
}
#[tokio::test]
#[tracing_test::traced_test]
#[ignore] async fn test_update_embeddings() {
let fixture = AnythingLLMFixture::new();
fixture
.with_fixture(|w, c| async move {
let test_doc_filepath =
PathBuf::from("tests/test_data/DELETE ME test document.pdf");
let doc_before_count = c.get_documents().await.unwrap().len();
let doc = c.post_document_upload(&test_doc_filepath).await.unwrap();
let doc_after_count = c.get_documents().await.unwrap().len();
assert_eq!(doc_after_count, doc_before_count + 1);
let test_workspace_slug = &w.slug;
let docs = vec![doc.clone().location.unwrap()];
let _ = c
.update_embeddings(&test_workspace_slug, docs, UpdateParameter::Adds)
.await
.unwrap();
let doc_after_count = c.get_documents().await.unwrap().len();
assert_eq!(doc_after_count, doc_before_count + 1);
let documents = c
.get_workspace_by_slug(&test_workspace_slug)
.await
.unwrap()
.documents
.unwrap();
assert_eq!(documents.len(), 1);
assert_eq!(doc.clone().location.unwrap(), documents[0].docpath);
let doc_vec = vec![doc.clone().location.unwrap()];
let _ = c.delete_api_system_remove_documents(doc_vec).await.unwrap();
})
.await;
}
#[tokio::test]
async fn test_delete_workspace_slug() {
let fixture = AnythingLLMFixture::new();
fixture
.with_fixture(|w, c| async move {
let test_doc_filepath =
PathBuf::from("tests/test_data/DELETE ME test document.pdf");
let doc_before_count = c.get_documents().await.unwrap().len();
let doc = c.post_document_upload(&test_doc_filepath).await.unwrap();
let test_workspace_slug = &w.slug;
let docs = vec![doc.clone().location.unwrap()];
let _ = c
.update_embeddings(&test_workspace_slug, docs, UpdateParameter::Adds)
.await
.unwrap();
let workspace_slug = &w.slug;
let _ = c.delete_workspace_slug(&workspace_slug).await.unwrap();
})
.await;
}
#[tokio::test]
async fn test_delete_workspace_with_invalid_slug() {
let fixture = AnythingLLMFixture::new();
fixture
.with_fixture(|w, c| async move {
let result = c.delete_workspace_slug("invalid slug").await;
assert!(result.is_err());
})
.await;
}
}