@main

milvus #github.com/xulang-org/packages/milvus
#errors
#strings

fn verify_milvus_pool_and_entities() {
    println("--- 1. Testing Milvus Vector Protocol & Pool ---")
    $p := milvus.open_pool("http://127.0.0.1:19530", 5)

    // 1. Connection Reuse
    fd1 := p.get_conn()
    if fd1 < 0 {
        println("   [FAIL] Failed to connect to Milvus port 19530")
        <<
    }
    println("   Borrow 1 fd: " + str(fd1))
    p.put_conn(fd1)

    fd2 := p.get_conn()
    if fd2 < 0 {
        println("   [FAIL] Failed to re-borrow Milvus connection")
        <<
    }
    println("   Borrow 2 (after return) fd: " + str(fd2))
    if fd1 == fd2 {
        println("   [PASS] Connection reused successfully (fd " + str(fd1) + " == " + str(fd2) + ")")
    } else {
        println("   [WARN] Different fd: " + str(fd1) + " vs " + str(fd2))
    }

    // 2. Concurrency
    fd3 := p.get_conn()
    if fd3 < 0 {
        println("   [FAIL] Failed to borrow concurrent connection")
        <<
    }
    println("   Borrow 3 (concurrently) fd: " + str(fd3))
    if fd2 != fd3 {
        println("   [PASS] Concurrently allocated distinct fds (" + str(fd2) + " != " + str(fd3) + ")")
    } else {
        println("   [FAIL] Expected distinct fds for concurrent connections!")
    }
    p.put_conn(fd2)
    p.put_conn(fd3)

    // 2. Data model validation
    cfg := milvus.CollectionConfig{
        name: "test_articles",
        dimension: 4,
        metric_type: "COSINE"
    }
    if cfg.name == "test_articles" && cfg.dimension == 4 && cfg.metric_type == "COSINE" {
        println("   [PASS] Milvus CollectionConfig verified: 4-dim COSINE")
    }

    e1 := milvus.VectorEntity{
        id: 101,
        vector: ["0.15", "0.25", "0.35", "0.45"],
        text: "Cloud Native Vector Search"
    }
    if e1.id == 101 && e1.vector.len == 4 {
        println("   [PASS] Milvus VectorEntity serialized cleanly: id=101, dim=4")
    }

    p.close()
    println("   [PASS] Milvus Pool closed cleanly.")
}

fn main() {
    println("=== Xu Milvus Native Driver Verification ===\n")
    verify_milvus_pool_and_entities()
    println("\n[MILVUS VERIFICATION 100% COMPLETED]")
}
