use serde_json::json; use sqlx::PgPool; use uuid::Uuid; use crate::email::{Delivery, Mailer, backoff}; use crate::templates::{TemplateError, render}; #[test] fn templates_in_both_languages() { let payload = json!({"url": "https://example.com/verify-email?token=abc", "display_name": "Anna"}); let hu = render("verify_email", "hu", &payload).unwrap(); assert_eq!(hu.subject, "Erősítsd meg az e-mail-címed"); assert!(hu.text.starts_with("Szia Anna!") && hu.text.contains("token=abc")); assert!( hu.html.contains(r#""#) && hu.html.contains(r#"href="https://example.com/verify-email?token=abc""#) ); let en = render("reset_password", "en", &payload).unwrap(); assert_eq!(en.subject, "Set a new password"); assert!(en.text.starts_with("Hi Anna,")); // Unknown locales fall back to English. assert_eq!(render("reset_password", "de", &payload).unwrap(), en); let accepted = render("invite_accepted", "hu", &json!({"url": "u", "invitee_name": "Béla"})).unwrap(); assert_eq!(accepted.subject, "Béla elfogadta a meghívásod"); let anonymous = render("invite_accepted", "en", &json!({"url": "u", "invitee_name": null})).unwrap(); assert_eq!(anonymous.subject, "Your invitation was accepted"); assert!(anonymous.text.contains("Your invitee accepted")); } #[test] fn template_input_is_escaped_and_checked() { let mail = render("verify_email", "hu", &json!({"url": "u", "display_name": "X\r\nBcc: y"})).unwrap(); assert!(mail.html.contains("Szia <b>X</b>Bcc: y!")); assert!(!mail.html.contains("X")); assert_eq!(render("verify_email", "hu", &json!({})), Err(TemplateError::MissingField("url"))); assert_eq!(render("nope", "hu", &json!({"url": "u"})), Err(TemplateError::UnknownTemplate("nope".into()))); } #[test] fn backoff_grows_and_is_capped() { assert_eq!(backoff(1).as_secs(), 60); assert_eq!(backoff(2).as_secs(), 120); assert_eq!(backoff(4).as_secs(), 480); assert_eq!(backoff(20).as_secs(), 6 * 3600); } fn delivery(db: PgPool, fail: Vec<(&str, bool)>) -> Delivery { Delivery { db, mailer: Mailer::Memory { sent: Default::default(), fail: fail.into_iter().map(|(a, p)| (a.to_owned(), p)).collect(), }, from: "Sorsfonalak ".parse().unwrap(), max_attempts: 3, } } fn sent(d: &Delivery) -> Vec<(String, String)> { let Mailer::Memory { sent, .. } = &d.mailer else { unreachable!() }; sent.lock().unwrap().iter().map(|(to, m)| (to.clone(), m.subject.clone())).collect() } async fn queue(db: &PgPool, to: &str, template: &str) -> Uuid { let id = Uuid::now_v7(); sqlx::query("INSERT INTO email_outbox (id, to_email, template, locale, payload) VALUES ($1, $2, $3, 'hu', $4)") .bind(id) .bind(to) .bind(template) .bind(json!({"url": "https://example.com/x"})) .execute(db) .await .unwrap(); id } type State = (i32, bool, bool, Option, bool); async fn state(db: &PgPool, id: Uuid) -> State { sqlx::query_as( "SELECT attempts, sent_at IS NOT NULL, failed_at IS NOT NULL, last_error, next_attempt_at > now() \ FROM email_outbox WHERE id = $1", ) .bind(id) .fetch_one(db) .await .unwrap() } #[sqlx::test(migrator = "db::MIGRATOR")] async fn delivers_retries_and_gives_up(db: PgPool) { let ok = queue(&db, "anna@example.com", "verify_email").await; let flaky = queue(&db, "flaky@example.com", "reset_password").await; let rejected = queue(&db, "rejected@example.com", "verify_email").await; let broken = queue(&db, "anna@example.com", "no_such_template").await; let d = delivery(db.clone(), vec![("flaky@example.com", false), ("rejected@example.com", true)]); assert_eq!(d.deliver_due().await.unwrap(), 4); assert_eq!(sent(&d), vec![("anna@example.com".into(), "Erősítsd meg az e-mail-címed".into())]); assert_eq!(state(&db, ok).await, (1, true, false, None, false)); // Temporary failure: retried later, not at once. assert_eq!(state(&db, flaky).await, (1, false, false, Some("test failure".into()), true)); // Permanent failures are not retried. assert_eq!(state(&db, rejected).await, (1, false, true, Some("test failure".into()), false)); let (_, _, failed, error, _) = state(&db, broken).await; assert!(failed && error.unwrap().contains("unknown e-mail template")); assert_eq!(d.deliver_due().await.unwrap(), 0); // Retries until max_attempts (3), then gives up. for expected in [2, 3] { sqlx::query("UPDATE email_outbox SET next_attempt_at = now() WHERE id = $1") .bind(flaky) .execute(&db) .await .unwrap(); assert_eq!(d.deliver_due().await.unwrap(), 1); assert_eq!(state(&db, flaky).await.0, expected); } let (_, sent_ok, failed, _, _) = state(&db, flaky).await; assert!(!sent_ok && failed); assert_eq!(sent(&d).len(), 1); } #[sqlx::test(migrator = "db::MIGRATOR")] async fn concurrent_workers_do_not_send_twice(db: PgPool) { for i in 0..15 { queue(&db, &format!("u{i}@example.com"), "verify_email").await; } let (a, b) = (delivery(db.clone(), vec![]), delivery(db.clone(), vec![])); let (na, nb) = tokio::join!(async { a.deliver_due().await.unwrap() }, async { b.deliver_due().await.unwrap() }); let (ra, rb) = (a.deliver_due().await.unwrap(), b.deliver_due().await.unwrap()); assert_eq!(na + nb + ra + rb, 15); let mut all: Vec = sent(&a).into_iter().chain(sent(&b)).map(|(to, _)| to).collect(); all.sort(); all.dedup(); assert_eq!(all.len(), 15); } #[sqlx::test(migrator = "db::MIGRATOR")] async fn insert_notifies_the_worker(db: PgPool) { let mut listener = sqlx::postgres::PgListener::connect_with(&db).await.unwrap(); listener.listen("email_outbox").await.unwrap(); queue(&db, "anna@example.com", "verify_email").await; let n = tokio::time::timeout(std::time::Duration::from_secs(5), listener.recv()).await.unwrap().unwrap(); assert_eq!(n.channel(), "email_outbox"); } #[sqlx::test(migrator = "db::MIGRATOR")] async fn cleanup_removes_only_expired_data(db: PgPool) { let user = Uuid::now_v7(); sqlx::query("INSERT INTO users (id, email) VALUES ($1, 'anna@example.com')").bind(user).execute(&db).await.unwrap(); for (hash, expires) in [(b"live".as_slice(), "now() + interval '1 day'"), (b"dead".as_slice(), "now() - interval '1 second'")] { sqlx::query(&format!( "INSERT INTO sessions (id, user_id, token_hash, expires_at) VALUES ($1, $2, $3, {expires})" )) .bind(Uuid::now_v7()) .bind(user) .bind(hash) .execute(&db) .await .unwrap(); } let old = queue(&db, "anna@example.com", "verify_email").await; let recent = queue(&db, "anna@example.com", "verify_email").await; let unsent = queue(&db, "anna@example.com", "verify_email").await; sqlx::query("UPDATE email_outbox SET sent_at = now() - interval '8 days' WHERE id = $1") .bind(old) .execute(&db) .await .unwrap(); sqlx::query("UPDATE email_outbox SET sent_at = now() WHERE id = $1").bind(recent).execute(&db).await.unwrap(); let deleted = crate::cleanup::run_once(&db).await.unwrap(); assert_eq!(deleted, vec![("sessions", 1), ("auth_tokens", 0), ("emails", 1), ("invites", 0)]); let (left,): (Vec,) = sqlx::query_as("SELECT array_agg(id ORDER BY id) FROM email_outbox").fetch_one(&db).await.unwrap(); assert_eq!(left, vec![recent, unsent]); } // --- analyses ---------------------------------------------------------------------------- mod analyses { use std::sync::{Arc, Mutex}; use axum::Json; use axum::extract::State; use axum::http::StatusCode; use serde_json::{Value, json}; use sqlx::PgPool; use uuid::Uuid; use crate::analysis::openrouter::OpenRouter; use crate::analysis::prompt::{Kind, schema, validate}; use crate::analysis::{Analyzer, backoff}; /// Scripted replies (then the last one forever) and the requests received. #[derive(Clone, Default)] struct Fake { replies: Arc>>, requests: Arc>>, } async fn completions(State(fake): State, Json(body): Json) -> (StatusCode, Json) { fake.requests.lock().unwrap().push(body); let mut replies = fake.replies.lock().unwrap(); let (status, reply) = if replies.len() > 1 { replies.remove(0) } else { replies[0].clone() }; (StatusCode::from_u16(status).unwrap(), Json(reply)) } async fn fake_openrouter(replies: Vec<(u16, Value)>) -> (Fake, String) { let fake = Fake { replies: Arc::new(Mutex::new(replies)), ..Default::default() }; let app = axum::Router::new().route("/chat/completions", axum::routing::post(completions)).with_state(fake.clone()); let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); let addr = listener.local_addr().unwrap(); tokio::spawn(async move { axum::serve(listener, app).await.unwrap() }); (fake, format!("http://{addr}")) } fn content(kind: Kind) -> Value { let schema = schema(kind); let keys: Vec<&str> = schema["properties"]["sections"]["required"] .as_array() .unwrap() .iter() .map(|k| k.as_str().unwrap()) .collect(); let sections: serde_json::Map = keys .iter() .map(|k| ((*k).to_owned(), json!({"title": format!("Cím {k}"), "body": "Első bekezdés.\n\nMásodik."}))) .collect(); json!({ "title": "Csendes erő", "summary": "Összefoglaló.", "sections": sections, "strengths": ["Kitartás.", "Empátia."], "challenges": ["Túlgondolás.", "Visszahúzódás."], "advice": "Bízz magadban." }) } fn reply(content: &Value) -> (u16, Value) { ( 200, json!({ "model": "test/model-2026", "choices": [{"finish_reason": "stop", "message": {"role": "assistant", "content": content.to_string()}}], "usage": {"prompt_tokens": 2100, "completion_tokens": 4800, "cost": 0.0123} }), ) } fn fixture(name: &str) -> Value { let path = format!("{}/testdata/{name}", env!("CARGO_MANIFEST_DIR")); serde_json::from_str(&std::fs::read_to_string(path).unwrap()).unwrap() } struct Setup { user: Uuid, chart: Uuid, } async fn setup(db: &PgPool) -> Setup { let (user, chart) = (Uuid::now_v7(), Uuid::now_v7()); sqlx::query("INSERT INTO users (id, email) VALUES ($1, 'anna@example.com')") .bind(user) .execute(db) .await .unwrap(); sqlx::query( "INSERT INTO charts (id, owner_id, label, is_primary, birth_data, result, chart_hash) \ VALUES ($1, $2, 'Saját', true, '{}', $3, 'hash1')", ) .bind(chart) .bind(user) .bind(fixture("natal-1990-03-15.json")) .execute(db) .await .unwrap(); Setup { user, chart } } async fn request_analysis(db: &PgPool, s: &Setup) -> Uuid { let id = Uuid::now_v7(); sqlx::query( "INSERT INTO analyses (id, kind, chart_id, requested_by, language, model, prompt_version, cache_key, credits_charged) \ VALUES ($1, 'natal', $2, $3, 'hu', 'test/model', 'v1', 'key', 1)", ) .bind(id) .bind(s.chart) .bind(s.user) .execute(db) .await .unwrap(); id } fn analyzer(db: &PgPool, base_url: &str) -> Analyzer { Analyzer::new(db.clone(), OpenRouter::new(base_url, "test-key", None), 3, 2) } async fn run_once(a: &Analyzer) -> bool { match a.claim().await.unwrap() { Some(job) => { a.handle(job).await; true } None => false, } } type Row = (String, i32, Option, Option, Option, Option, bool); async fn row(db: &PgPool, id: Uuid) -> Row { sqlx::query_as( "SELECT status, attempts, error, model_used, output_tokens, cost_usd, next_attempt_at > now() \ FROM analyses WHERE id = $1", ) .bind(id) .fetch_one(db) .await .unwrap() } async fn refunds(db: &PgPool, user: Uuid) -> Vec { sqlx::query_scalar( "SELECT delta FROM credit_ledger WHERE user_id = $1 AND reason = 'refund' AND unit = 'natal'", ) .bind(user) .fetch_all(db) .await .unwrap() } async fn make_due(db: &PgPool, id: Uuid) { sqlx::query("UPDATE analyses SET next_attempt_at = now() WHERE id = $1").bind(id).execute(db).await.unwrap(); } #[sqlx::test(migrator = "db::MIGRATOR")] async fn writes_the_analysis(db: PgPool) { let s = setup(&db).await; let id = request_analysis(&db, &s).await; let (fake, url) = fake_openrouter(vec![reply(&content(Kind::Natal))]).await; let a = analyzer(&db, &url); assert!(run_once(&a).await); assert!(!run_once(&a).await, "nothing else is due"); assert_eq!( row(&db, id).await, ("done".into(), 1, None, Some("test/model-2026".into()), Some(4800), Some(0.0123), false) ); let (stored,): (Value,) = sqlx::query_as("SELECT content FROM analyses WHERE id = $1").bind(id).fetch_one(&db).await.unwrap(); assert_eq!(stored["title"], "Csendes erő"); let requests = fake.requests.lock().unwrap(); let body = &requests[0]; assert_eq!(body["model"], "test/model"); assert_eq!(body["response_format"]["json_schema"]["strict"], true); assert_eq!(body["provider"]["require_parameters"], true); let system = body["messages"][0]["content"].as_str().unwrap(); let user = body["messages"][1]["content"].as_str().unwrap(); assert!(system.contains("Write in Hungarian.") && system.contains("Szaturnusz")); assert!(user.contains("- Sun: Pisces 24°42', house 8") && user.contains("- personality: ")); // No birth data in the prompt. for secret in ["1990", "14:30", "Szeged", "Hungary"] { assert!(!user.contains(secret) && !system.contains(secret), "{secret} leaked"); } } #[sqlx::test(migrator = "db::MIGRATOR")] async fn retries_bad_answers_then_fails_and_refunds(db: PgPool) { let s = setup(&db).await; let id = request_analysis(&db, &s).await; let broken = json!({"title": "Csak cím"}); let (_, url) = fake_openrouter(vec![reply(&broken)]).await; let a = analyzer(&db, &url); assert!(run_once(&a).await); let (status, attempts, error, _, _, _, later) = row(&db, id).await; assert_eq!((status.as_str(), attempts, later), ("pending", 1, true)); assert!(error.unwrap().contains("does not match the schema")); assert!(!run_once(&a).await, "the retry waits"); assert!(refunds(&db, s.user).await.is_empty()); for _ in 0..2 { make_due(&db, id).await; assert!(run_once(&a).await); } let (status, attempts, ..) = row(&db, id).await; assert_eq!((status.as_str(), attempts), ("failed", 3)); assert_eq!(refunds(&db, s.user).await, vec![1]); } #[sqlx::test(migrator = "db::MIGRATOR")] async fn permanent_errors_fail_at_once_and_retry_after_outage(db: PgPool) { let s = setup(&db).await; let bad_model = request_analysis(&db, &s).await; let (_, url) = fake_openrouter(vec![(400, json!({"error": {"message": "unknown model"}}))]).await; assert!(run_once(&analyzer(&db, &url)).await); let (status, attempts, error, ..) = row(&db, bad_model).await; assert_eq!((status.as_str(), attempts), ("failed", 1)); assert_eq!(error.as_deref(), Some("HTTP 400: unknown model")); assert_eq!(refunds(&db, s.user).await, vec![1]); // A provider outage (5xx) is retried and then succeeds. let outage = request_analysis(&db, &s).await; let (_, url) = fake_openrouter(vec![(503, json!({"error": {"message": "overloaded"}})), reply(&content(Kind::Natal))]) .await; let a = analyzer(&db, &url); assert!(run_once(&a).await); assert_eq!(row(&db, outage).await.0, "pending"); make_due(&db, outage).await; assert!(run_once(&a).await); assert_eq!(row(&db, outage).await.0, "done"); assert_eq!(refunds(&db, s.user).await, vec![1], "no refund for the successful one"); } #[sqlx::test(migrator = "db::MIGRATOR")] async fn synastry_prompt_and_stuck_jobs(db: PgPool) { let s = setup(&db).await; let response = fixture("synastry-response.json"); let (chart2, synastry) = (Uuid::now_v7(), Uuid::now_v7()); sqlx::query( "INSERT INTO charts (id, owner_id, label, is_primary, birth_data, result, chart_hash) \ VALUES ($1, $2, 'Partner', false, '{}', $3, 'hash2')", ) .bind(chart2) .bind(s.user) .bind(&response["person2"]) .execute(&db) .await .unwrap(); sqlx::query( "INSERT INTO synastries (id, owner_id, chart1_id, chart2_id, result, synastry_hash) VALUES ($1, $2, $3, $4, $5, 'sh')", ) .bind(synastry) .bind(s.user) .bind(s.chart) .bind(chart2) .bind(&response) .execute(&db) .await .unwrap(); let id = Uuid::now_v7(); sqlx::query( "INSERT INTO analyses (id, kind, synastry_id, requested_by, language, model, prompt_version, cache_key, \ status, started_at) VALUES ($1, 'synastry', $2, $3, 'en', 'test/model', 'v1', 'k2', 'running', now() - interval '20 minutes')", ) .bind(id) .bind(synastry) .bind(s.user) .execute(&db) .await .unwrap(); let (fake, url) = fake_openrouter(vec![reply(&content(Kind::Synastry))]).await; let a = analyzer(&db, &url); assert!(!run_once(&a).await, "running jobs are not claimed"); assert_eq!(a.requeue_stuck().await.unwrap(), 1); assert!(run_once(&a).await); assert_eq!(row(&db, id).await.0, "done"); let requests = fake.requests.lock().unwrap(); let user = requests[0]["messages"][1]["content"].as_str().unwrap(); assert!(user.starts_with("Chart data:\n\nSYNASTRY") && user.contains("- attraction: ")); assert!(requests[0]["messages"][0]["content"].as_str().unwrap().contains("Write in English.")); assert!(!user.contains("Szeged") && !user.contains("Pécs")); } #[test] fn schema_validation_and_backoff() { let schema = schema(Kind::Natal); assert_eq!(validate(&schema, &content(Kind::Natal)), Ok(())); let mut c = content(Kind::Natal); c["sections"]["love"]["body"] = json!(" "); assert_eq!(validate(&schema, &c), Err("$.sections.love.body: expected a non-empty string".into())); c["sections"]["love"]["body"] = json!("Szöveg."); c["sections"].as_object_mut().unwrap().remove("work"); assert!(validate(&schema, &c).unwrap_err().contains("work: missing")); assert_eq!([1, 2, 3, 9].map(|n| backoff(n).as_secs()), [30, 120, 480, 3600]); } }