Files
call-recorder/crates/core/src/testing.rs
T
haukeandClaude Opus 5.5 58e96a2954 feat #14740: admin backend client and transcript upload text
recorder-core side of linking the app to the euvino admin backend:
- backend: BackendClient (create a key with the admin's session cookie,
  check a key, upload a transcript; redirects never followed), German
  error messages, the login-window decision (LoginStep) and the upload
  flow with login, one retry after a refused key and no loop
- call_view: the upload text (header + merged, renamed lines) and the
  upload fields on the call
- store: schema v3 with crm_transcript_id and uploaded_at
- settings: backend_url (validated https), the app-managed login fields,
  kept on edit and cleared when the backend URL changes
- secrets: Service::Backend; http: per-request follow_redirects;
  FakeTransport can script transport errors

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-11-06 14:52:50 +01:00

110 lines
3.5 KiB
Rust

//! Test doubles shared across `recorder-core`'s unit tests, and usable from
//! other crates/integration tests via the `test-util` feature.
use std::sync::Mutex;
use async_trait::async_trait;
use crate::http::{HttpRequest, HttpResponse, HttpTransport, Method, TransportError};
struct Expectation {
method: Method,
url: String,
response: Result<HttpResponse, TransportError>,
}
/// Scripted [`HttpTransport`]: queue expected requests in order with
/// [`FakeTransport::expect`], run the code under test against it, then call
/// [`FakeTransport::assert_done`] to confirm every expectation was consumed.
///
/// Matching is exact (method and full URL, including query string) and
/// strictly FIFO. Uses interior mutability so `send` works through
/// `&self`, as required behind `Arc<dyn HttpTransport>`.
pub struct FakeTransport {
expectations: Mutex<Vec<Expectation>>,
requests: Mutex<Vec<HttpRequest>>,
}
impl FakeTransport {
pub fn new() -> Self {
FakeTransport {
expectations: Mutex::new(Vec::new()),
requests: Mutex::new(Vec::new()),
}
}
/// Queues the next expected request and the response to return for it.
pub fn expect(&self, method: Method, url: &str, status: u16, body: impl Into<Vec<u8>>) {
let response = HttpResponse {
status,
body: body.into(),
};
self.push(method, url, Ok(response));
}
/// Queues the next expected request and the transport error it fails
/// with (e.g. the server is unreachable).
pub fn fail(&self, method: Method, url: &str, error: TransportError) {
self.push(method, url, Err(error));
}
fn push(&self, method: Method, url: &str, response: Result<HttpResponse, TransportError>) {
self.expectations.lock().unwrap().push(Expectation {
method,
url: url.to_string(),
response,
});
}
/// Every request sent through this transport so far, in order.
pub fn requests(&self) -> Vec<HttpRequest> {
self.requests.lock().unwrap().clone()
}
/// Panics listing any scripted expectation that was never sent.
pub fn assert_done(&self) {
let remaining = self.expectations.lock().unwrap();
if !remaining.is_empty() {
let pending: Vec<String> = remaining
.iter()
.map(|e| format!("{:?} {}", e.method, e.url))
.collect();
panic!(
"FakeTransport: {} expectation(s) never called: {}",
pending.len(),
pending.join(", ")
);
}
}
}
impl Default for FakeTransport {
fn default() -> Self {
Self::new()
}
}
#[async_trait]
impl HttpTransport for FakeTransport {
async fn send(&self, req: HttpRequest) -> Result<HttpResponse, TransportError> {
let expectation = {
let mut expectations = self.expectations.lock().unwrap();
if expectations.is_empty() {
panic!(
"FakeTransport: unexpected {:?} {} (no expectations remain)",
req.method, req.url
);
}
expectations.remove(0)
};
if expectation.method != req.method || expectation.url != req.url {
panic!(
"FakeTransport: expected {:?} {}, got {:?} {}",
expectation.method, expectation.url, req.method, req.url
);
}
self.requests.lock().unwrap().push(req);
expectation.response
}
}