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 invitation_headers_for_spam_filters(db: PgPool) { let id = Uuid::now_v7(); sqlx::query( "INSERT INTO email_outbox (id, to_email, template, locale, payload) VALUES ($1, $2, 'invitation', 'hu', $3)", ) .bind(id) .bind("guest@example.com") .bind(json!({ "url": "https://example.com/meghivo/t", "inviter_name": "Kata", "message": null, "expires_on": "2026-12-17", "unsubscribe_url": "https://example.com/leiratkozas?token=t", "unsubscribe_post_url": "https://example.com/api/v1/public/invitations/unsubscribe?token=t", })) .execute(&db) .await .unwrap(); let d = delivery(db.clone(), vec![]); assert_eq!(d.deliver_due().await.unwrap(), 1); let Mailer::Memory { sent, .. } = &d.mailer else { unreachable!() }; let raw = sent.lock().unwrap()[0].2.clone(); assert!(raw.contains(&format!("Message-ID: <{id}@example.com>")), "{raw}"); assert!(raw.contains("List-Unsubscribe: ")); assert!(raw.contains("List-Unsubscribe-Post: List-Unsubscribe=One-Click")); assert!(raw.contains("multipart/alternative") && raw.contains("text/plain") && raw.contains("text/html")); } #[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), ("invitations", 0), ("prompt_previews", 0), ("generation_errors", 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, kind, birth_data, result, chart_hash) \ VALUES ($1, $2, 'Saját', true, 'own', '{}', $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("Magyarul írj") && system.contains("Szaturnusz")); assert!( user.contains("- Sun: Pisces 24°42', house 8") && user.contains("- personality: Az alapvető identitás") ); // 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 uses_the_admin_edited_system_prompt(db: PgPool) { let s = setup(&db).await; let (revision,): (i64,) = sqlx::query_as( "INSERT INTO prompt_revisions (kind, language, system) VALUES ('natal', 'hu', 'Saját szabályok.') RETURNING id", ) .fetch_one(&db) .await .unwrap(); // a newer revision is not used: the analysis records the one it was requested with sqlx::query("INSERT INTO prompt_revisions (kind, language, system) VALUES ('natal', 'hu', 'Újabb.')") .execute(&db) .await .unwrap(); let id = request_analysis(&db, &s).await; sqlx::query("UPDATE analyses SET prompt_version = $1 WHERE id = $2") .bind(format!("v1-r{revision}")) .bind(id) .execute(&db) .await .unwrap(); let (fake, url) = fake_openrouter(vec![reply(&content(Kind::Natal))]).await; assert!(run_once(&analyzer(&db, &url)).await); assert_eq!(row(&db, id).await.0, "done"); let requests = fake.requests.lock().unwrap(); assert_eq!(requests[0]["messages"][0]["content"], "Saját szabályok."); assert!(requests[0]["messages"][1]["content"].as_str().unwrap().contains("- personality: ")); } #[sqlx::test(migrator = "db::MIGRATOR")] async fn prompt_previews_run_after_analyses(db: PgPool) { let s = setup(&db).await; let preview = Uuid::now_v7(); sqlx::query( "INSERT INTO prompt_previews (id, kind, chart_id, language, model, system, created_by) \ VALUES ($1, 'summary', $2, 'hu', 'test/summary', 'Próba szabályok.', $3)", ) .bind(preview) .bind(s.chart) .bind(s.user) .execute(&db) .await .unwrap(); let analysis = request_analysis(&db, &s).await; let (fake, url) = fake_openrouter(vec![ reply(&content(Kind::Natal)), reply(&json!({"title": "Érzékeny mag", "summary": "Rövid összefoglaló szöveg."})), (500, json!({})), ]) .await; let a = analyzer(&db, &url); // the analysis goes first; the preview only when nothing else is due let job = a.claim().await.unwrap().expect("the analysis"); assert_eq!(job.id, analysis); a.handle(job).await; assert!(a.claim().await.unwrap().is_none()); let job = a.claim_preview().await.unwrap().expect("the preview"); a.handle_preview(job).await; let (status, title, model, credits): (String, Option, Option, i64) = sqlx::query_as( "SELECT status, content->>'title', model_used, \ (SELECT count(*) FROM credit_ledger WHERE user_id = p.created_by) \ FROM prompt_previews p WHERE id = $1", ) .bind(preview) .fetch_one(&db) .await .unwrap(); assert_eq!((status.as_str(), title.is_some(), model.as_deref()), ("done", true, Some("test/model-2026"))); let before = credits; { let requests = fake.requests.lock().unwrap(); assert_eq!(requests[1]["model"], "test/summary"); assert_eq!(requests[1]["messages"][0]["content"], "Próba szabályok."); } // one attempt only: an error fails the preview at once, without refunds let second = Uuid::now_v7(); sqlx::query( "INSERT INTO prompt_previews (id, kind, chart_id, language, model, system, created_by) \ VALUES ($1, 'natal', $2, 'hu', 'test/model', 'Másik.', $3)", ) .bind(second) .bind(s.chart) .bind(s.user) .execute(&db) .await .unwrap(); a.handle_preview(a.claim_preview().await.unwrap().unwrap()).await; let (status, error, credits): (String, Option, i64) = sqlx::query_as( "SELECT status, error, (SELECT count(*) FROM credit_ledger WHERE user_id = p.created_by) \ FROM prompt_previews p WHERE id = $1", ) .bind(second) .fetch_one(&db) .await .unwrap(); assert_eq!((status.as_str(), error.is_some(), credits), ("failed", true, before)); assert!(a.claim_preview().await.unwrap().is_none()); } #[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]); // every failed attempt is in the admin error log let log: Vec<(String, i32, String, String)> = sqlx::query_as( "SELECT source, attempt, outcome, model FROM generation_errors WHERE item_id = $1 ORDER BY id", ) .bind(id) .fetch_all(&db) .await .unwrap(); let expected = |attempt: i32, outcome: &str| { ("analysis".to_string(), attempt, outcome.to_string(), "test/model".to_string()) }; assert_eq!(log, vec![expected(1, "retry"), expected(2, "retry"), expected(3, "failed")]); } #[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, kind, birth_data, result, chart_hash) \ VALUES ($1, $2, 'Partner', false, 'person', '{}', $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")); } // a prompt test run on the comparison sqlx::query( "INSERT INTO prompt_previews (id, kind, synastry_id, language, model, system, created_by) \ VALUES ($1, 'synastry', $2, 'hu', 'test/model', 'Kapcsolati próba.', $3)", ) .bind(Uuid::now_v7()) .bind(synastry) .bind(s.user) .execute(&db) .await .unwrap(); a.handle_preview(a.claim_preview().await.unwrap().expect("the preview")).await; let (status,): (String,) = sqlx::query_as("SELECT status FROM prompt_previews").fetch_one(&db).await.unwrap(); assert_eq!(status, "done"); let requests = fake.requests.lock().unwrap(); assert_eq!(requests[1]["messages"][0]["content"], "Kapcsolati próba."); assert!(requests[1]["messages"][1]["content"].as_str().unwrap().contains("SYNASTRY")); } #[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]); } async fn ask(db: &PgPool, analysis: Uuid, user: Uuid, question: &str, status: &str, answer: Option<&str>) -> Uuid { let id = Uuid::now_v7(); sqlx::query( "INSERT INTO questions (id, analysis_id, asked_by, question, language, model, prompt_version, status, \ answer, credits_charged) \ VALUES ($1, $2, $3, $4, 'hu', 'test/model', 'v1', $5, $6, 1)", ) .bind(id) .bind(analysis) .bind(user) .bind(question) .bind(status) .bind(answer) .execute(db) .await .unwrap(); id } async fn finished_analysis(db: &PgPool, s: &Setup) -> Uuid { let id = request_analysis(db, s).await; sqlx::query("UPDATE analyses SET status = 'done', content = $2 WHERE id = $1") .bind(id) .bind(content(Kind::Natal)) .execute(db) .await .unwrap(); id } async fn answer_once(a: &Analyzer) -> bool { match a.claim_question().await.unwrap() { Some(job) => { a.handle_question(job).await; true } None => false, } } #[sqlx::test(migrator = "db::MIGRATOR")] async fn answers_a_question_with_the_analysis_and_earlier_answers(db: PgPool) { let s = setup(&db).await; let analysis = finished_analysis(&db, &s).await; ask(&db, analysis, s.user, "Mi az erősségem?", "done", Some("A kitartásod.")).await; let id = ask(&db, analysis, s.user, "És a munkában?", "pending", None).await; let (fake, url) = fake_openrouter(vec![reply(&json!({"answer": "A munkában a \"lassú víz\" a te utad."}))]).await; let a = analyzer(&db, &url); assert!(answer_once(&a).await); assert!(!answer_once(&a).await, "nothing else is due"); let (status, answer): (String, Option) = sqlx::query_as("SELECT status, answer FROM questions WHERE id = $1").bind(id).fetch_one(&db).await.unwrap(); assert_eq!((status.as_str(), answer.as_deref()), ("done", Some("A munkában a „lassú víz” a te utad."))); let requests = fake.requests.lock().unwrap(); let body = &requests[0]; assert_eq!(body["model"], "test/model", "the model of the analysis answers"); assert_eq!(body["response_format"]["json_schema"]["name"], "question_answer"); let user = body["messages"][1]["content"].as_str().unwrap(); assert!(user.contains("- Sun: Pisces") && user.contains("# Csendes erő"), "chart data and analysis"); assert!(user.contains("Kérdés: Mi az erősségem?\nVálasz: A kitartásod."), "earlier answers"); assert!(user.contains("És a munkában?") && user.contains("legfeljebb 250 szóban")); for secret in ["1990", "14:30", "Szeged"] { assert!(!user.contains(secret), "{secret} leaked"); } } #[sqlx::test(migrator = "db::MIGRATOR")] async fn a_failed_answer_gives_the_question_back(db: PgPool) { let s = setup(&db).await; let analysis = finished_analysis(&db, &s).await; let id = ask(&db, analysis, s.user, "Mikor váltsak?", "pending", None).await; let (_, url) = fake_openrouter(vec![reply(&json!({"answer": ""}))]).await; let a = analyzer(&db, &url); for _ in 0..3 { sqlx::query("UPDATE questions SET next_attempt_at = now()").execute(&db).await.unwrap(); assert!(answer_once(&a).await); } let status: String = sqlx::query_scalar("SELECT status FROM questions WHERE id = $1").bind(id).fetch_one(&db).await.unwrap(); assert_eq!(status, "failed"); let refunds: Vec<(String, i32)> = sqlx::query_as("SELECT unit, delta FROM credit_ledger WHERE question_id = $1 AND reason = 'refund'") .bind(id) .fetch_all(&db) .await .unwrap(); assert_eq!(refunds, vec![("question".to_string(), 1)]); } #[test] fn the_chosen_model_gets_two_attempts_then_the_default_takes_over() { use crate::analysis::openrouter::LlmError; use crate::analysis::{Next, next_step}; let t = || LlmError::Transient("overloaded".into()); let p = || LlmError::Permanent("no such model".into()); assert_eq!(next_step(&t(), 1, 3, true), Next::Retry(backoff(1))); assert_eq!(next_step(&t(), 2, 3, true), Next::Fallback); assert_eq!(next_step(&p(), 1, 3, true), Next::Fallback); // without a fallback (or already on it): retries, then failure assert_eq!(next_step(&t(), 2, 3, false), Next::Retry(backoff(2))); assert_eq!(next_step(&t(), 3, 3, false), Next::Fail); assert_eq!(next_step(&p(), 1, 3, false), Next::Fail); } #[sqlx::test(migrator = "db::MIGRATOR")] async fn the_default_model_writes_when_the_chosen_one_fails(db: PgPool) { let s = setup(&db).await; let id = request_analysis(&db, &s).await; sqlx::query("UPDATE analyses SET model = 'cheap/model', fallback_model = 'test/model' WHERE id = $1") .bind(id) .execute(&db) .await .unwrap(); let broken = json!({"title": "Csak cím"}); let (fake, url) = fake_openrouter(vec![reply(&broken), reply(&broken), reply(&content(Kind::Natal))]).await; let a = analyzer(&db, &url); assert!(run_once(&a).await); make_due(&db, id).await; assert!(run_once(&a).await); // second failure: the default model takes over at once assert!(run_once(&a).await); let (status, on_fallback, credits): (String, bool, i32) = sqlx::query_as("SELECT status, on_fallback, credits_charged FROM analyses WHERE id = $1") .bind(id) .fetch_one(&db) .await .unwrap(); assert_eq!((status.as_str(), on_fallback, credits), ("done", true, 1)); assert!(refunds(&db, s.user).await.is_empty()); let models: Vec = fake.requests.lock().unwrap().iter().map(|b| b["model"].as_str().unwrap().to_string()).collect(); assert_eq!(models, ["cheap/model", "cheap/model", "test/model"]); let outcomes: Vec<(String, String)> = sqlx::query_as("SELECT outcome, model FROM generation_errors WHERE item_id = $1 ORDER BY id") .bind(id) .fetch_all(&db) .await .unwrap(); assert_eq!( outcomes, [("retry".to_string(), "cheap/model".to_string()), ("fallback".to_string(), "cheap/model".to_string())] ); } #[sqlx::test(migrator = "db::MIGRATOR")] async fn a_restarted_attempt_does_not_write_its_result(db: PgPool) { let s = setup(&db).await; let id = request_analysis(&db, &s).await; let (_, url) = fake_openrouter(vec![reply(&content(Kind::Natal))]).await; let a = analyzer(&db, &url); let job = a.claim().await.unwrap().expect("a job"); // an admin restarts it while the worker is still writing it sqlx::query("UPDATE analyses SET status = 'pending', started_at = NULL, attempts = 0 WHERE id = $1") .bind(id) .execute(&db) .await .unwrap(); a.handle(job).await; let (status, content): (String, Option) = sqlx::query_as("SELECT status, content FROM analyses WHERE id = $1").bind(id).fetch_one(&db).await.unwrap(); assert_eq!((status.as_str(), content), ("pending", None)); // the new attempt writes it assert!(run_once(&a).await); let status: String = sqlx::query_scalar("SELECT status FROM analyses WHERE id = $1").bind(id).fetch_one(&db).await.unwrap(); assert_eq!(status, "done"); } #[sqlx::test(migrator = "db::MIGRATOR")] async fn answers_a_question_about_a_relationship_with_both_charts(db: PgPool) { let s = setup(&db).await; let response = fixture("synastry-response.json"); let (chart2, synastry, analysis) = (Uuid::now_v7(), Uuid::now_v7(), Uuid::now_v7()); sqlx::query( "INSERT INTO charts (id, owner_id, label, is_primary, kind, birth_data, result, chart_hash) \ VALUES ($1, $2, 'Partner', false, 'person', '{}', $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(); sqlx::query( "INSERT INTO analyses (id, kind, synastry_id, requested_by, language, model, prompt_version, cache_key, \ status, content) VALUES ($1, 'synastry', $2, $3, 'hu', 'test/model', 'v1', 'k', 'done', $4)", ) .bind(analysis) .bind(synastry) .bind(s.user) .bind(content(Kind::Synastry)) .execute(&db) .await .unwrap(); let id = ask(&db, analysis, s.user, "Mire figyeljünk egy vitában?", "pending", None).await; let (fake, url) = fake_openrouter(vec![reply(&json!({"answer": "Arra, hogy..."}))]).await; let a = analyzer(&db, &url); assert!(answer_once(&a).await); let status: String = sqlx::query_scalar("SELECT status FROM questions WHERE id = $1").bind(id).fetch_one(&db).await.unwrap(); assert_eq!(status, "done"); let requests = fake.requests.lock().unwrap(); let user = requests[0]["messages"][1]["content"].as_str().unwrap(); assert!(user.starts_with("Képletadatok:\n\nSYNASTRY") && user.contains("Person 2"), "both charts"); assert!(!user.contains("Szeged") && !user.contains("Pécs")); } }