open-standards/eoc/eoc-rs/crates/eoc-embeddings/tests/vendor_http.rs
Transaction Science 01d4ad9c7b Ingest batch 2: 9 new crates across openpay / smart-byte / eoc
openpay/ (+3 crates, ~8400 LOC, 162 tests)
  op-iso8583/         ISO 8583 messages for direct acquirer / card-network
                      connectivity. MTIs, primary+secondary bitmaps, full
                      DE catalogue, BCD/ASCII/EBCDIC/LLVAR/LLLVAR codecs,
                      five dialects (Visa Base I, Mastercard MDS, Amex GNS,
                      Discover, JCB), network-management messages
                      (sign-on/echo/cutover), DE 55 EMV TLV.
  op-mobile-wallets/  Apple Pay (ECDH-P256 + AES-256-GCM), Google Pay ECv2
                      (ECDH + HKDF + AES-256-CTR + HMAC), Samsung Pay. PAN
                      never leaves the decryption boundary as a String;
                      lands in VaultRef. No-PAN-leakage assertions per
                      wallet.
  op-routing/         Least-cost routing + MCC-aware routing + intelligent
                      retry / soft-decline recovery. 231-MCC catalogue.
                      Real ISO 8583 DE-39 soft/hard decline taxonomies.

smart-byte/smart-byte-rs/ (+3 crates, ~7100 LOC, 96 tests)
  smart-byte-vc/      W3C Verifiable Credentials 2.0: VCDM, presentations,
                      Data Integrity proofs (eddsa-jcs-2022), VC-JWT,
                      SD-JWT (selective disclosure), Bitstring Status
                      List 2021. Cargo-bridge maps VCs to Smart Byte
                      envelopes with SAID stability.
  smart-byte-did/     W3C DID resolution: did:key (Ed25519/P-256/secp256k1),
                      did:web (HTTPS), did:peer (numalgo 2+4), did:jwk,
                      did:ion (feature-gated stub). DID URL dereferencer.
  smart-byte-pq/      Post-quantum signatures: ML-DSA-44/65/87 (FIPS 204),
                      SLH-DSA across 12 parameter sets (FIPS 205), FN-DSA
                      (draft FIPS 206, feature-gated). Hybrid Ed25519 +
                      ML-DSA pattern for the transition. Secret-key
                      zeroize-on-drop.

eoc/eoc-rs/ (+3 crates, ~6450 LOC, 76 tests)
  eoc-vendor-api/     NeuralBackend implementations: Anthropic, OpenAI,
                      Google, Mistral, Cohere, Groq, Together, Fireworks.
                      SSE streaming. Prompt-caching where supported.
                      Joule estimates per published energy coefficient
                      (12-model curated table).
  eoc-embeddings/     21 embedding models behind one Embedder trait:
                      OpenAI v3 (3), Cohere v3 (3), Voyage (5), Mistral,
                      Jina, plus local ONNX (BGE x3, nomic, mxbai, GTE,
                      E5) behind 'local' feature. Matryoshka-style
                      dimension projection. Per-content-hash cache.
  eoc-route-learned/  RouteLLM-style matrix-factorisation router +
                      multinomial-logreg classifier + LinUCB / Thompson
                      bandits for online learning. LearnedCascade wraps
                      the rule-based cascade with a confidence-threshold
                      skip policy. Synthetic-eval accuracy: MF 0.935,
                      LogReg 0.980.

Quality:
- cargo check across all three workspaces: clean
- cargo clippy --all-targets -- -D warnings on every new crate: clean
- 334 new tests total; all passing
- #![forbid(unsafe_code)] across every new crate
- No unwrap/expect outside #[cfg(test)]

Rust 1.95.0 edition 2024 (inherited via root rust-toolchain.toml).
2026-05-23 18:12:24 -04:00

216 lines
7.5 KiB
Rust

//! Integration tests for vendor HTTP backends using `wiremock`.
//!
//! Covers happy path, 429-with-retry, and 401-no-retry for each vendor.
use eoc_embeddings::{
CohereEmbedder, Embedder, JinaEmbedder, MistralEmbedder, OpenAiEmbedder, VoyageEmbedder,
error::EmbeddingError,
};
use serde_json::json;
use wiremock::matchers::{header, method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};
fn openai_success_body(dim: usize) -> serde_json::Value {
let v: Vec<f32> = (0..dim).map(|i| (i as f32) * 0.001).collect();
json!({
"object": "list",
"data": [
{"object": "embedding", "index": 0, "embedding": v},
],
"model": "text-embedding-3-small",
"usage": {"prompt_tokens": 1, "total_tokens": 1},
})
}
fn cohere_success_body() -> serde_json::Value {
let v: Vec<f32> = (0..1024).map(|i| (i as f32) * 0.0001).collect();
json!({"id": "x", "embeddings": {"float": [v]}, "texts": ["hi"]})
}
fn voyage_success_body() -> serde_json::Value {
let v: Vec<f32> = (0..1024).map(|i| (i as f32) * 0.0001).collect();
json!({"object": "list", "data": [{"object": "embedding", "embedding": v, "index": 0}], "model": "voyage-3", "usage": {"total_tokens": 1}})
}
fn jina_success_body() -> serde_json::Value {
let v: Vec<f32> = (0..1024).map(|i| (i as f32) * 0.0001).collect();
json!({"data": [{"embedding": v, "index": 0, "object": "embedding"}], "model": "jina-embeddings-v3"})
}
fn mistral_success_body() -> serde_json::Value {
let v: Vec<f32> = (0..1024).map(|i| (i as f32) * 0.0001).collect();
json!({"id": "x", "object": "list", "data": [{"object": "embedding", "embedding": v, "index": 0}], "model": "mistral-embed", "usage": {"prompt_tokens": 1, "total_tokens": 1}})
}
#[tokio::test]
async fn openai_happy_path() {
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/v1/embeddings"))
.and(header("authorization", "Bearer sk-test"))
.respond_with(ResponseTemplate::new(200).set_body_json(openai_success_body(1536)))
.mount(&server)
.await;
let e = OpenAiEmbedder::new("sk-test", "text-embedding-3-small")
.expect("ok")
.with_endpoint(format!("{}/v1/embeddings", server.uri()));
let v = e.embed(&["hi"]).await.expect("embed ok");
assert_eq!(v.len(), 1);
assert_eq!(v[0].len(), 1536);
}
#[tokio::test]
async fn openai_401_no_retry() {
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/v1/embeddings"))
.respond_with(ResponseTemplate::new(401))
.expect(1)
.mount(&server)
.await;
let e = OpenAiEmbedder::new("sk-bad", "text-embedding-3-small")
.expect("ok")
.with_endpoint(format!("{}/v1/embeddings", server.uri()));
let r = e.embed(&["hi"]).await;
assert!(matches!(r, Err(EmbeddingError::InvalidApiKey)));
}
#[tokio::test]
async fn openai_429_retries_then_succeeds() {
let server = MockServer::start().await;
// First call: 429 (no retry-after to keep test fast).
Mock::given(method("POST"))
.and(path("/v1/embeddings"))
.respond_with(
ResponseTemplate::new(429).insert_header("retry-after", "0"),
)
.up_to_n_times(1)
.mount(&server)
.await;
// Subsequent calls: 200.
Mock::given(method("POST"))
.and(path("/v1/embeddings"))
.respond_with(ResponseTemplate::new(200).set_body_json(openai_success_body(1536)))
.mount(&server)
.await;
let e = OpenAiEmbedder::new("sk-test", "text-embedding-3-small")
.expect("ok")
.with_endpoint(format!("{}/v1/embeddings", server.uri()))
.with_max_retries(3);
let v = e.embed(&["hi"]).await.expect("eventually ok");
assert_eq!(v[0].len(), 1536);
}
#[tokio::test]
async fn cohere_happy_path() {
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/v2/embed"))
.respond_with(ResponseTemplate::new(200).set_body_json(cohere_success_body()))
.mount(&server)
.await;
let e = CohereEmbedder::new("k", "embed-english-v3.0")
.expect("ok")
.with_endpoint(format!("{}/v2/embed", server.uri()));
let v = e.embed(&["hi"]).await.expect("ok");
assert_eq!(v[0].len(), 1024);
}
#[tokio::test]
async fn cohere_401() {
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/v2/embed"))
.respond_with(ResponseTemplate::new(403))
.mount(&server)
.await;
let e = CohereEmbedder::new("k", "embed-english-v3.0")
.expect("ok")
.with_endpoint(format!("{}/v2/embed", server.uri()));
assert!(matches!(e.embed(&["hi"]).await, Err(EmbeddingError::InvalidApiKey)));
}
#[tokio::test]
async fn voyage_happy_path_and_429() {
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/v1/embeddings"))
.respond_with(ResponseTemplate::new(429).insert_header("retry-after", "0"))
.up_to_n_times(1)
.mount(&server)
.await;
Mock::given(method("POST"))
.and(path("/v1/embeddings"))
.respond_with(ResponseTemplate::new(200).set_body_json(voyage_success_body()))
.mount(&server)
.await;
let e = VoyageEmbedder::new("k", "voyage-3")
.expect("ok")
.with_endpoint(format!("{}/v1/embeddings", server.uri()))
.with_max_retries(3);
let v = e.embed(&["hi"]).await.expect("ok");
assert_eq!(v[0].len(), 1024);
}
#[tokio::test]
async fn jina_happy_path() {
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/v1/embeddings"))
.respond_with(ResponseTemplate::new(200).set_body_json(jina_success_body()))
.mount(&server)
.await;
let e = JinaEmbedder::new("k", "jina-embeddings-v3")
.expect("ok")
.with_endpoint(format!("{}/v1/embeddings", server.uri()));
let v = e.embed(&["hi"]).await.expect("ok");
assert_eq!(v[0].len(), 1024);
}
#[tokio::test]
async fn jina_401() {
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/v1/embeddings"))
.respond_with(ResponseTemplate::new(401))
.mount(&server)
.await;
let e = JinaEmbedder::new("k", "jina-embeddings-v3")
.expect("ok")
.with_endpoint(format!("{}/v1/embeddings", server.uri()));
assert!(matches!(e.embed(&["hi"]).await, Err(EmbeddingError::InvalidApiKey)));
}
#[tokio::test]
async fn mistral_happy_path() {
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/v1/embeddings"))
.respond_with(ResponseTemplate::new(200).set_body_json(mistral_success_body()))
.mount(&server)
.await;
let e = MistralEmbedder::new("k", "mistral-embed")
.expect("ok")
.with_endpoint(format!("{}/v1/embeddings", server.uri()));
let v = e.embed(&["hi"]).await.expect("ok");
assert_eq!(v[0].len(), 1024);
}
#[tokio::test]
async fn mistral_429_exhausts_budget() {
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path("/v1/embeddings"))
.respond_with(ResponseTemplate::new(429).insert_header("retry-after", "0"))
.mount(&server)
.await;
let e = MistralEmbedder::new("k", "mistral-embed")
.expect("ok")
.with_endpoint(format!("{}/v1/embeddings", server.uri()))
.with_max_retries(1);
let r = e.embed(&["hi"]).await;
assert!(matches!(r, Err(EmbeddingError::RateLimited { .. })));
}