Newer
Older
TelosDB / src-backend / tests / test_vector_dll.rs
/// vector.dll のロードテスト
/// 実行方法: cargo test --test test_vector_dll -- --nocapture
use rusqlite::{Connection, LoadExtensionGuard};
use std::path::Path;

#[test]
fn test_load_vector_dll() {
    // DLL の候補パスを探す
    let candidates = [
        "build_assets/vector.dll",
        "../build_assets/vector.dll", 
        "../resources/vector.dll",
        "../bin/vector.dll",
        "../node_modules/@sqliteai/sqlite-vector-win32-x86_64/vector.dll",
        "../src-backend/build_assets/vector.dll",
    ];

    let dll_path = candidates.iter()
        .find(|p| Path::new(p).exists())
        .expect("vector.dll が見つかりません");

    println!("Testing with DLL: {}", dll_path);
    println!("Absolute path: {:?}", std::fs::canonicalize(dll_path).unwrap());

    let conn = Connection::open_in_memory().expect("DB接続失敗");

    // load_extension を有効にする
    unsafe {
        let _guard: LoadExtensionGuard = conn.load_extension_enable().expect("load_extension有効化失敗");
        
        // 拡張子を除いたパスで試す
        let dll_no_ext = dll_path.trim_end_matches(".dll");
        println!("Trying load_extension('{}')...", dll_no_ext);
        
        match conn.load_extension(dll_no_ext, None::<&str>) {
            Ok(_) => println!("✅ load_extension('{}') 成功!", dll_no_ext),
            Err(e) => {
                println!("❌ load_extension('{}') 失敗: {}", dll_no_ext, e);
                
                // フルパスでも試す
                println!("Trying with full path...");
                match conn.load_extension(dll_path, None::<&str>) {
                    Ok(_) => println!("✅ load_extension('{}') 成功!", dll_path),
                    Err(e2) => {
                        println!("❌ load_extension('{}') 失敗: {}", dll_path, e2);
                        
                        // エントリポイントを明示して試す
                        println!("Trying with explicit entry point 'sqlite3_vector_init'...");
                        match conn.load_extension(dll_path, Some("sqlite3_vector_init")) {
                            Ok(_) => println!("✅ sqlite3_vector_init で成功!"),
                            Err(e3) => {
                                println!("❌ sqlite3_vector_init でも失敗: {}", e3);
                                panic!("すべてのロード方法が失敗しました");
                            }
                        }
                    }
                }
            }
        }
    }

    // ロードが成功した場合、vector_init のテスト
    conn.execute(
        "CREATE TABLE test_items (id INTEGER PRIMARY KEY, embedding BLOB);",
        [],
    ).expect("テーブル作成失敗");

    match conn.execute(
        "SELECT vector_init('test_items', 'embedding', 'type=FLOAT32,dimension=4');",
        [],
    ) {
        Ok(_) => println!("✅ vector_init 成功!"),
        Err(e) => println!("❌ vector_init 失敗: {}", e),
    }
}