Questions about an analysis or forecast, answered by the model that wrote it
- 3 questions in every package, +5 questions add-on (1 000 Ft); question units, refund on failure - The answer sees the chart data, the analysis and earlier questions; at most 250 words - Editable question prompt (Promptok → Kérdés-válasz); admins regenerate answers - Package texts and FAQ updated Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -644,4 +644,96 @@ mod analyses {
|
||||
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<String>) =
|
||||
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)]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user