Tarot readings: AI-written, drawn on the server, sold as units
CI / test (push) Successful in 1m29s
CI / build (push) Successful in 5m52s
CI / deploy (push) Skipped

A new Kártyajóslás page: choose a spread (1, 3 or 5 cards), ask a
question, pick the cards from the fanned deck and watch them turn over;
the reading is written by the detailed-analysis model from the cards,
their positions and base meanings, the question and the reader's Sun,
Moon and Ascendant. The server shuffles and draws, so nobody can draw
again for free. A reading is a 'tarot' analysis (worker, retries,
fallback, refunds and admin regeneration as for analyses), uses one
'tarot' unit, and survives recalculations; deleting the account
deletes it.

Packages carry 2/5/10 readings, the new +5 kártyajóslás addon costs
1 000 Ft. The card artwork comes from the tarot design studio
(celestial astrolabe style); the deck JSON is shared by the frontend
and the prompt. A public example is at /kartyajoslas/pelda.

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-20 14:35:50 +00:00
co-authored by Claude Opus 5.5
parent 54af54cd39
commit b0fc547b73
36 changed files with 2957 additions and 46 deletions
+115 -13
View File
@@ -343,7 +343,7 @@ async fn register_login_verify_logout(pool: PgPool) {
let r = app.get("/v1/me", Some(&token)).await;
assert_eq!(r.status, StatusCode::OK);
assert_eq!(r.body["providers"], json!(["password"]));
assert_eq!(r.body["analyses_left"], json!({"natal": 0, "synastry": 0, "forecast": 0, "question": 0}));
assert_eq!(r.body["analyses_left"], json!({"natal": 0, "synastry": 0, "forecast": 0, "question": 0, "tarot": 0}));
// e-mail verification through the queued e-mail's link
let url = app.outbox_url("anna@example.com", "verify_email").await;
@@ -466,12 +466,12 @@ async fn roles_and_admin_guard(pool: PgPool) {
let r = app.post(&format!("/v1/admin/users/{anna}/credits"), Some(&admin), grant("natal", 5)).await;
assert_eq!(
(r.status, &r.body["analyses_left"]),
(StatusCode::CREATED, &json!({"natal": 5, "synastry": 0, "forecast": 0, "question": 0}))
(StatusCode::CREATED, &json!({"natal": 5, "synastry": 0, "forecast": 0, "question": 0, "tarot": 0}))
);
let r = app.post(&format!("/v1/admin/users/{anna}/credits"), Some(&admin), grant("chat", 5)).await;
assert_eq!(r.status, StatusCode::BAD_REQUEST);
let credits = app.get("/v1/credits", Some(&user)).await.body;
assert_eq!(credits["balance"], json!({"natal": 5, "synastry": 0, "forecast": 0, "question": 0}));
assert_eq!(credits["balance"], json!({"natal": 5, "synastry": 0, "forecast": 0, "question": 0, "tarot": 0}));
assert_eq!((credits["ledger"][0]["unit"].as_str(), &credits["ledger"][0]["delta"]), (Some("natal"), &json!(5)));
}
@@ -703,7 +703,7 @@ async fn analyses_spend_units_and_reuse_results(pool: PgPool) {
assert_eq!(model, "anthropic/claude-test");
assert_eq!(
app.get("/v1/credits", Some(&anna)).await.body["balance"],
json!({"natal": 1, "synastry": 0, "forecast": 0, "question": 0})
json!({"natal": 1, "synastry": 0, "forecast": 0, "question": 0, "tarot": 0})
);
// same request again: the same analysis, no charge
@@ -713,7 +713,7 @@ async fn analyses_spend_units_and_reuse_results(pool: PgPool) {
assert_eq!(r.status, StatusCode::ACCEPTED);
assert_eq!(
app.get("/v1/credits", Some(&anna)).await.body["balance"],
json!({"natal": 0, "synastry": 0, "forecast": 0, "question": 0})
json!({"natal": 0, "synastry": 0, "forecast": 0, "question": 0, "tarot": 0})
);
let r = app.post(&uri, Some(&anna), json!({"model": "anthropic/claude-other"})).await;
assert_eq!(r.status, StatusCode::BAD_REQUEST); // no model choice
@@ -985,7 +985,7 @@ async fn synastry_analyses_use_synastry_units(pool: PgPool) {
assert_eq!((r.status, r.body["kind"].as_str()), (StatusCode::ACCEPTED, Some("synastry")));
assert_eq!(
app.get("/v1/credits", Some(&anna)).await.body["balance"],
json!({"natal": 1, "synastry": 0, "forecast": 0, "question": 0})
json!({"natal": 1, "synastry": 0, "forecast": 0, "question": 0, "tarot": 0})
);
// questions can be asked about the relationship analysis too
@@ -1089,7 +1089,7 @@ async fn admins_use_units_too_and_mock_payments_add_units(pool: PgPool) {
let r = app.post("/v1/payments/mock", Some(&anna), json!({"unit": "natal"})).await;
assert_eq!(
(r.status, &r.body["analyses_left"]),
(StatusCode::OK, &json!({"natal": 1, "synastry": 0, "forecast": 0, "question": 3})),
(StatusCode::OK, &json!({"natal": 1, "synastry": 0, "forecast": 0, "question": 3, "tarot": 2})),
"{}",
r.body
);
@@ -1098,7 +1098,7 @@ async fn admins_use_units_too_and_mock_payments_add_units(pool: PgPool) {
assert_eq!((r.status, &r.body["credits_charged"]), (StatusCode::ACCEPTED, &json!(1)), "{}", r.body);
assert_eq!(
app.get("/v1/credits", Some(&anna)).await.body["balance"],
json!({"natal": 0, "synastry": 0, "forecast": 0, "question": 3})
json!({"natal": 0, "synastry": 0, "forecast": 0, "question": 3, "tarot": 2})
);
// without MOCK_PAYMENTS there is no such payment
@@ -1389,7 +1389,16 @@ async fn packages_grant_units(pool: PgPool) {
let ids: Vec<&str> = r.body["packages"].as_array().unwrap().iter().map(|p| p["id"].as_str().unwrap()).collect();
assert_eq!(
ids,
["single", "couple", "family", "addon_synastry", "addon_forecast", "addon_persons", "addon_questions"]
[
"single",
"couple",
"family",
"addon_synastry",
"addon_forecast",
"addon_persons",
"addon_questions",
"addon_tarot"
]
);
assert_eq!(
(&r.body["packages"][2]["badge"], &r.body["packages"][2]["per_analysis_huf"]),
@@ -1402,7 +1411,7 @@ async fn packages_grant_units(pool: PgPool) {
let r = grant_package(&app, &admin, &anna_id, "family").await;
assert_eq!(
(r.status, &r.body["analyses_left"]),
(StatusCode::CREATED, &json!({"natal": 6, "synastry": 5, "forecast": 6, "question": 3})),
(StatusCode::CREATED, &json!({"natal": 6, "synastry": 5, "forecast": 6, "question": 3, "tarot": 10})),
"{}",
r.body
);
@@ -1417,7 +1426,12 @@ async fn packages_grant_units(pool: PgPool) {
// a (mock) purchase adds to it and records the price
let r = app.post("/v1/payments/mock", Some(&anna), json!({"package": "couple"})).await;
assert_eq!(r.body["analyses_left"], json!({"natal": 8, "synastry": 6, "forecast": 8, "question": 6}), "{}", r.body);
assert_eq!(
r.body["analyses_left"],
json!({"natal": 8, "synastry": 6, "forecast": 8, "question": 6, "tarot": 15}),
"{}",
r.body
);
let (price,): (i32,) =
sqlx::query_as("SELECT price_huf FROM package_orders WHERE package = 'couple'").fetch_one(&pool).await.unwrap();
assert_eq!(price, 6000);
@@ -1658,14 +1672,19 @@ async fn forecasts_are_free_their_analysis_uses_a_forecast_unit(pool: PgPool) {
(StatusCode::PAYMENT_REQUIRED, Some("insufficient_credits"))
);
let r = app.post("/v1/payments/mock", Some(&anna), json!({"unit": "forecast"})).await;
assert_eq!(r.body["analyses_left"], json!({"natal": 1, "synastry": 0, "forecast": 1, "question": 0}), "{}", r.body);
assert_eq!(
r.body["analyses_left"],
json!({"natal": 1, "synastry": 0, "forecast": 1, "question": 0, "tarot": 0}),
"{}",
r.body
);
let r = app.call("POST", &analyses, Some(&anna), None).await;
assert_eq!(r.status, StatusCode::ACCEPTED, "{}", r.body);
assert_eq!((r.body["kind"].as_str(), r.body["forecast_id"].as_str()), (Some("forecast"), Some(id.as_str())));
let analysis = r.body["id"].as_str().unwrap().to_string();
assert_eq!(
app.get("/v1/credits", Some(&anna)).await.body["balance"],
json!({"natal": 1, "synastry": 0, "forecast": 0, "question": 0})
json!({"natal": 1, "synastry": 0, "forecast": 0, "question": 0, "tarot": 0})
);
assert_eq!(app.call("POST", &analyses, Some(&anna), None).await.status, StatusCode::OK); // the same one, no charge
assert_eq!(app.get(&analyses, Some(&anna)).await.body["analyses"].as_array().unwrap().len(), 1);
@@ -2568,3 +2587,86 @@ async fn stripe_purchases_wait_for_the_opening(pool: PgPool) {
assert_eq!(stripe_form(&id, "consent_collection[terms_of_service]"), None);
assert_eq!(stripe_form(&id, "custom_text[terms_of_service_acceptance][message]"), None);
}
#[sqlx::test(migrator = "db::MIGRATOR")]
async fn tarot_readings_draw_on_the_server_and_use_tarot_units(pool: PgPool) {
let app = TestApp::new(pool.clone()).await;
let anna = app.register("anna@example.com", "Anna").await;
app.chart(&anna, "Saját", true, "1990-07-01").await;
let body = json!({"spread": "trinity", "question": "Merre induljak tovább?"});
// No units: a package or the addon is needed.
let r = app.post("/v1/tarot/readings", Some(&anna), body.clone()).await;
assert_eq!(r.status, StatusCode::PAYMENT_REQUIRED, "{}", r.body);
assert_eq!(r.body["error"]["code"], "insufficient_credits");
// The packages carry readings, the addon five more.
let packages = app.get("/v1/packages", None).await.body["packages"].clone();
let tarot_of = |id: &str| packages.as_array().unwrap().iter().find(|p| p["id"] == id).unwrap()["tarot"].clone();
assert_eq!((tarot_of("single"), tarot_of("couple"), tarot_of("family")), (json!(2), json!(5), json!(10)));
assert_eq!(tarot_of("addon_tarot"), json!(5));
let r = app.post("/v1/payments/mock", Some(&anna), json!({"package": "addon_tarot"})).await;
assert_eq!(r.status, StatusCode::OK, "{}", r.body);
assert_eq!(app.get("/v1/me", Some(&anna)).await.body["analyses_left"]["tarot"], 5);
let r = app.post("/v1/tarot/readings", Some(&anna), json!({"spread": "nincs"})).await;
assert_eq!(r.status, StatusCode::BAD_REQUEST);
let long = "x".repeat(analysis_prompt::TAROT_QUESTION_MAX_CHARS + 1);
let r = app.post("/v1/tarot/readings", Some(&anna), json!({"spread": "daily", "question": long})).await;
assert_eq!(r.status, StatusCode::BAD_REQUEST);
let r = app.post("/v1/tarot/readings", Some(&anna), body.clone()).await;
assert_eq!(r.status, StatusCode::ACCEPTED, "{}", r.body);
assert_eq!(r.body["status"], "pending");
assert_eq!(r.body["spread_name"], "Sorsfonalak hármas");
assert_eq!(r.body["question"], "Merre induljak tovább?");
let cards = r.body["cards"].as_array().unwrap();
assert_eq!(cards.len(), 3);
assert_eq!(cards[1]["position"], "Jelenlegi fókusz");
let mut ids: Vec<&str> = cards.iter().map(|c| c["card"].as_str().unwrap()).collect();
assert!(ids.iter().all(|id| analysis_prompt::tarot::card(id).is_some()));
ids.dedup();
assert_eq!(ids.len(), 3);
assert!(r.body.get("sun").is_none() && r.body.get("model").is_none());
let id = r.body["id"].as_str().unwrap().to_string();
assert_eq!(app.get("/v1/me", Some(&anna)).await.body["analyses_left"]["tarot"], 4);
// Stored for the worker: the analysis model, the reader's signs.
let (model, tarot, chart): (String, Value, Option<uuid::Uuid>) =
sqlx::query_as("SELECT model, tarot, chart_id FROM analyses WHERE id = $1::uuid")
.bind(&id)
.fetch_one(&pool)
.await
.unwrap();
assert_eq!(model, app.state.config.analysis_model);
assert_eq!(
(&tarot["sun"], &tarot["moon"], &tarot["ascendant"]),
(&json!("cancer"), &json!("pisces"), &json!("aries"))
);
assert_eq!(chart, None);
// One at a time.
let r = app.post("/v1/tarot/readings", Some(&anna), json!({"spread": "daily"})).await;
assert_eq!(r.status, StatusCode::CONFLICT);
assert_eq!(r.body["error"]["code"], "tarot_in_progress");
let list = app.get("/v1/tarot/readings", Some(&anna)).await;
assert_eq!(list.body["readings"].as_array().unwrap().len(), 1);
assert_eq!(list.body["left"], 4);
assert_eq!(app.get(&format!("/v1/tarot/readings/{id}"), Some(&anna)).await.body["id"], id.as_str());
// Nobody else sees it, not even as an analysis.
let bela = app.register("bela@example.com", "Béla").await;
assert_eq!(app.get(&format!("/v1/tarot/readings/{id}"), Some(&bela)).await.status, StatusCode::NOT_FOUND);
assert_eq!(app.get(&format!("/v1/analyses/{id}"), Some(&anna)).await.status, StatusCode::NOT_FOUND);
assert_eq!(app.get("/v1/tarot/readings", Some(&bela)).await.body["readings"], json!([]));
// A recalculation keeps the readings; deleting the account removes them.
routes::recalculate(&app.state).await.unwrap();
let count = || async {
sqlx::query_scalar::<_, i64>("SELECT count(*) FROM analyses WHERE kind = 'tarot'")
.fetch_one(&pool)
.await
.unwrap()
};
assert_eq!(count().await, 1);
let r = app.call("DELETE", "/v1/me", Some(&anna), Some(json!({"confirm": "DELETE"}))).await;
assert_eq!(r.status, StatusCode::NO_CONTENT, "{}", r.body);
assert_eq!(count().await, 0);
}