|
| 1 | +use std::time::Duration; |
| 2 | +use typesense::{Client, ExponentialBackoff, models::GetCollectionsParameters}; |
| 3 | + |
| 4 | +async fn clean_test_artifacts() { |
| 5 | + let client = Client::builder() |
| 6 | + .nodes(vec!["http://localhost:8108"]) |
| 7 | + .api_key("xyz") |
| 8 | + .healthcheck_interval(Duration::from_secs(5)) |
| 9 | + .retry_policy(ExponentialBackoff::builder().build_with_max_retries(3)) |
| 10 | + .connection_timeout(Duration::from_secs(3)) |
| 11 | + .build() |
| 12 | + .expect("Failed to create Typesense client"); |
| 13 | + |
| 14 | + let collections = client |
| 15 | + .collections() |
| 16 | + .retrieve(GetCollectionsParameters::new()) |
| 17 | + .await |
| 18 | + .expect("Get all collections failed!"); |
| 19 | + |
| 20 | + println!("Cleaning up test collections..."); |
| 21 | + |
| 22 | + let mut collection_count = 0; |
| 23 | + |
| 24 | + for collection in collections.iter() { |
| 25 | + if !collection.name.starts_with("test_") { |
| 26 | + continue; |
| 27 | + } |
| 28 | + |
| 29 | + if let Err(err) = client |
| 30 | + .collection_schemaless(&collection.name) |
| 31 | + .delete() |
| 32 | + .await |
| 33 | + { |
| 34 | + eprintln!("Failed to delete {}: {}", collection.name, err); |
| 35 | + } else { |
| 36 | + collection_count += 1; |
| 37 | + println!("Deleted {}", collection.name); |
| 38 | + } |
| 39 | + } |
| 40 | + println!("Deleted {} test collections.", collection_count); |
| 41 | + println!("✅ Cleanup complete."); |
| 42 | +} |
| 43 | + |
| 44 | +pub async fn test_clean(is_wasm: bool, args: Vec<String>) { |
| 45 | + let status = if is_wasm { |
| 46 | + println!("Running wasm-pack test..."); |
| 47 | + std::process::Command::new("wasm-pack") |
| 48 | + .arg("test") |
| 49 | + .arg("--headless") |
| 50 | + .arg("--chrome") |
| 51 | + .args(&args) |
| 52 | + .arg("typesense") |
| 53 | + .status() |
| 54 | + .expect("Failed to run wasm-pack test") |
| 55 | + } else { |
| 56 | + println!("Running cargo test with arguments: {}", args.join(" ")); |
| 57 | + std::process::Command::new("cargo") |
| 58 | + .arg("test") |
| 59 | + .args(&args) |
| 60 | + .status() |
| 61 | + .expect("Failed to run cargo test") |
| 62 | + }; |
| 63 | + |
| 64 | + clean_test_artifacts().await; |
| 65 | + |
| 66 | + // Propagate cargo test exit code |
| 67 | + std::process::exit(status.code().unwrap_or(1)); |
| 68 | +} |
0 commit comments