stemedb/crates/stemedb-api/tests/common/mod.rs
jordan 55349845d0 refactor: Split all files to enforce 500-line max
Break monolith source files into focused modules:
- stemedb-core/types.rs → types/ directory (assertion, source, gold_standard, etc.)
- stemedb-storage: audit_store, quota_store, trust_rank_store, vector_index, vote_store → module directories
- stemedb-ingest/worker.rs → worker/ with separate test modules
- stemedb-query: engine, materializer, query → module directories
- stemedb-lens: epoch_aware, skeptic → module directories
- stemedb-sim/lib.rs → agent, arenas/, helpers, runner, strategy, types
- stemedb-api/tests: integration_tests → http_basic, http_validation, http_epoch, http_pipeline
- stemedb-api/tests: e2e_flow_test → e2e_full_pipeline, e2e_lens_resolution
- stemedb-query/tests: e2e_pipeline → e2e_pipeline + e2e_decay

Also adds new features: gold standard verification, escalation handlers,
admin endpoints, concept hierarchy spec, arena roadmap, and Go SDK.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-02 01:13:45 -07:00

133 lines
4.6 KiB
Rust

//! Shared test utilities for HTTP integration tests.
#![allow(clippy::expect_used)]
#![allow(dead_code)]
use ed25519_dalek::{Signer, SigningKey};
use rand::rngs::OsRng;
use serde_json::json;
use std::sync::Arc;
use stemedb_api::AppState;
use stemedb_ingest::Ingestor;
use stemedb_storage::{GenericEscalationStore, GenericQuotaStore, SledStore};
use stemedb_wal::Journal;
use tokio::sync::Mutex;
/// Test environment that keeps temp directories alive for the test duration.
pub struct TestEnvironment {
pub _temp_dir: tempfile::TempDir,
pub state: AppState,
}
/// Test environment with full ingestor for roundtrip tests.
pub struct TestEnvironmentWithIngestor {
pub _temp_dir: tempfile::TempDir,
pub state: AppState,
pub ingestor: Ingestor<SledStore>,
}
/// Helper to create a test environment with temporary directories.
pub async fn create_test_env() -> TestEnvironment {
let temp_dir = tempfile::tempdir().expect("failed to create temp dir");
let wal_dir = temp_dir.path().join("wal");
let db_dir = temp_dir.path().join("db");
std::fs::create_dir_all(&wal_dir).expect("failed to create wal dir");
std::fs::create_dir_all(&db_dir).expect("failed to create db dir");
let journal = Journal::open(&wal_dir).expect("failed to open journal");
let store = SledStore::open(&db_dir).expect("failed to open store");
let state = AppState::new(journal, store);
TestEnvironment { _temp_dir: temp_dir, state }
}
/// Helper to create a test environment with a running ingestor for roundtrip tests.
///
/// Note: We need to share the same store between AppState and Ingestor.
/// AppState::new() takes ownership, so we need a different approach:
/// we'll create the ingestor first, then construct AppState manually.
pub async fn create_test_env_with_ingestor() -> TestEnvironmentWithIngestor {
let temp_dir = tempfile::tempdir().expect("failed to create temp dir");
let wal_dir = temp_dir.path().join("wal");
let db_dir = temp_dir.path().join("db");
std::fs::create_dir_all(&wal_dir).expect("failed to create wal dir");
std::fs::create_dir_all(&db_dir).expect("failed to create db dir");
// Create shared store
let store = Arc::new(SledStore::open(&db_dir).expect("failed to open store"));
// Journal for API (writing)
let journal_for_api =
Arc::new(Mutex::new(Journal::open(&wal_dir).expect("failed to open journal for API")));
// Journal for ingestor (reading) - WAL allows multiple readers
let journal_for_ingestor =
Arc::new(Mutex::new(Journal::open(&wal_dir).expect("failed to open journal for ingestor")));
// Create ingestor with shared store
let ingestor = Ingestor::new(journal_for_ingestor, store.clone())
.await
.expect("failed to create ingestor");
// Create quota store for AppState
let quota_store = Arc::new(GenericQuotaStore::new(store.clone()));
// Create escalation store for AppState
let escalation_store = Arc::new(GenericEscalationStore::new(store.clone()));
// Construct AppState manually to share the store
let state = AppState { journal: journal_for_api, store, quota_store, escalation_store };
TestEnvironmentWithIngestor { _temp_dir: temp_dir, state, ingestor }
}
/// Helper to sign a message using Ed25519.
#[allow(dead_code)]
pub fn sign_message(message: &str) -> ([u8; 32], [u8; 64]) {
let mut csprng = OsRng;
let signing_key = SigningKey::generate(&mut csprng);
let verifying_key = signing_key.verifying_key();
let signature = signing_key.sign(message.as_bytes());
(verifying_key.to_bytes(), signature.to_bytes())
}
/// Create a properly signed assertion for testing.
pub fn create_signed_assertion_json(
subject: &str,
predicate: &str,
value: f64,
) -> serde_json::Value {
let mut csprng = OsRng;
let signing_key = SigningKey::generate(&mut csprng);
let verifying_key = signing_key.verifying_key();
let message = format!("{}:{}", subject, predicate);
let signature = signing_key.sign(message.as_bytes());
let timestamp = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0);
json!({
"subject": subject,
"predicate": predicate,
"object": {"type": "Number", "value": value},
"confidence": 0.95,
"source_class": "Expert",
"lifecycle": "Proposed",
"signatures": [{
"agent_id": hex::encode(verifying_key.to_bytes()),
"signature": hex::encode(signature.to_bytes()),
"timestamp": timestamp
}],
"source_hash": "0".repeat(64),
"timestamp": timestamp
})
}