Try draft prompts on own charts before saving
CI / test (push) Successful in 46s
CI / build (push) Successful in 4m16s
CI / deploy (push) Skipped

Under each prompt on /admin/promptok, "Kipróbálás" writes the summary or
detailed analysis with the text in the editor (saved or not) on one of the
admin's own charts. Test runs live in prompt_previews: never shown with the
chart, not cached, no credits, one attempt, at most three open per admin,
removed after 30 days. The worker writes them when no analysis is due. The
result shows the model, tokens, cost and time, and the draft can be loaded back.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
feco
2026-11-17 14:41:12 +00:00
co-authored by Claude Opus 5.5
parent 86d5dc5503
commit b77dafdb26
13 changed files with 697 additions and 24 deletions
+76 -1
View File
@@ -175,7 +175,10 @@ async fn cleanup_removes_only_expired_data(db: PgPool) {
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)]);
assert_eq!(
deleted,
vec![("sessions", 1), ("auth_tokens", 0), ("emails", 1), ("invites", 0), ("prompt_previews", 0)]
);
let (left,): (Vec<Uuid>,) =
sqlx::query_as("SELECT array_agg(id ORDER BY id) FROM email_outbox").fetch_one(&db).await.unwrap();
assert_eq!(left, vec![recent, unsent]);
@@ -398,6 +401,78 @@ mod analyses {
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<String>, Option<String>, 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<String>, 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;