use db::{Db, fields, seed, vocab};
use sqlx::PgPool;

// Note: `server::seed` opens its own DB connection by URL, but `#[sqlx::test]`
// provisions a temporary database whose URL is not directly exposed. This test
// exercises the building block the command composes — `db::seed::seed_spectrum_cataloguing`
// — against the test pool, run twice to prove the idempotency the command relies on.
#[sqlx::test(migrations = "../db/migrations")]
async fn seed_is_idempotent_via_building_block(pool: PgPool) {
    let db = Db::from_pool(pool);

    for _ in 0..2 {
        let mut tx = db.pool().begin().await.unwrap();
        seed::seed_spectrum_cataloguing(&mut tx).await.unwrap();
        tx.commit().await.unwrap();
    }

    // A representative seeded vocabulary and field definition are present after two runs.
    assert!(
        vocab::vocabulary_by_key(db.pool(), "material")
            .await
            .unwrap()
            .is_some(),
        "vocabulary 'material' should be seeded"
    );
    assert!(
        fields::field_definition_by_key(db.pool(), "title")
            .await
            .unwrap()
            .is_some(),
        "field definition 'title' should be seeded"
    );
}
