Paid detailed analyses with per-kind units; example analysis page
- Credits become analysis units (migration 0006): a natal analysis uses one 'natal' unit, a synastry analysis one 'synastry' unit. No monthly free credits, no model tiers: ANALYSIS_MODEL (default Opus 5.5) writes every paid analysis. Admins grant units per kind; the worker refunds the right unit on failure. - Chart page: the detailed analysis card (offer with units left and a confirmation, progress while writing, the finished six sections). - Public /pelda page: a full Opus analysis of a made-up chart, with the free summary and a login call to action. - The worker sends the JSON schema with properties in 'required' order. serde_json sorted them, so the title came after ~9000 tokens of text and Opus sometimes left it empty (3 failed attempts in a live test); with the order fixed the same analysis succeeded at the first try. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -222,7 +222,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["credits"], 2);
|
||||
assert_eq!(r.body["analyses_left"], json!({"natal": 0, "synastry": 0}));
|
||||
|
||||
// e-mail verification through the queued e-mail's link
|
||||
let url = app.outbox_url("anna@example.com", "verify_email").await;
|
||||
@@ -341,10 +341,14 @@ async fn roles_and_admin_guard(pool: PgPool) {
|
||||
app.call("PATCH", &format!("/v1/admin/users/{admin_id}"), Some(&admin), Some(json!({"roles": ["user"]}))).await;
|
||||
assert_eq!(r.status, StatusCode::CONFLICT);
|
||||
|
||||
let r =
|
||||
app.post(&format!("/v1/admin/users/{anna}/credits"), Some(&admin), json!({"delta": 5, "note": "bonus"})).await;
|
||||
assert_eq!(r.status, StatusCode::CREATED);
|
||||
assert_eq!(app.get("/v1/credits", Some(&user)).await.body["balance"], 7); // 2 monthly + 5
|
||||
let grant = |unit: &str, delta: i32| json!({"unit": unit, "delta": delta, "note": "bonus"});
|
||||
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})));
|
||||
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}));
|
||||
assert_eq!((credits["ledger"][0]["unit"].as_str(), &credits["ledger"][0]["delta"]), (Some("natal"), &json!(5)));
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "db::MIGRATOR")]
|
||||
@@ -456,8 +460,22 @@ async fn invitation_links_two_users(pool: PgPool) {
|
||||
assert!(app.outbox_url("anna@example.com", "invite_accepted").await.ends_with(&synastry_id));
|
||||
}
|
||||
|
||||
/// Gives `token`'s user analysis units (the admin endpoint is tested in `roles_and_admin_guard`).
|
||||
async fn grant_units(app: &TestApp, token: &str, unit: &str, delta: i32) {
|
||||
let id = app.get("/v1/me", Some(token)).await.body["user"]["id"].as_str().unwrap().to_string();
|
||||
sqlx::query(
|
||||
"INSERT INTO credit_ledger (id, user_id, unit, delta, reason) VALUES (gen_random_uuid(), $1::uuid, $2, $3, 'purchase')",
|
||||
)
|
||||
.bind(id)
|
||||
.bind(unit)
|
||||
.bind(delta)
|
||||
.execute(&app.pool)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "db::MIGRATOR")]
|
||||
async fn analyses_spend_credits_and_reuse_results(pool: PgPool) {
|
||||
async fn analyses_spend_units_and_reuse_results(pool: PgPool) {
|
||||
let app = TestApp::new(pool.clone()).await;
|
||||
let anna = app.register("anna@example.com", "Anna").await;
|
||||
let chart = app.chart(&anna, "Saját", true, "1990-03-15").await;
|
||||
@@ -472,26 +490,32 @@ async fn analyses_spend_credits_and_reuse_results(pool: PgPool) {
|
||||
);
|
||||
let r = app.call("POST", &format!("/v1/charts/{}/summary", chart["id"].as_str().unwrap()), Some(&anna), None).await;
|
||||
assert_eq!((r.status, &r.body["id"]), (StatusCode::OK, &summary["id"]));
|
||||
assert_eq!(app.get("/v1/credits", Some(&anna)).await.body["balance"], 2);
|
||||
|
||||
// the detailed analysis is paid: no unit, no analysis
|
||||
let r = app.call("POST", &uri, Some(&anna), None).await;
|
||||
assert_eq!(
|
||||
(r.status, r.body["error"]["code"].as_str()),
|
||||
(StatusCode::PAYMENT_REQUIRED, Some("insufficient_credits"))
|
||||
);
|
||||
grant_units(&app, &anna, "natal", 2).await;
|
||||
|
||||
let r = app.call("POST", &uri, Some(&anna), None).await;
|
||||
assert_eq!(r.status, StatusCode::ACCEPTED, "{}", r.body);
|
||||
assert_eq!((r.body["status"].as_str(), r.body["language"].as_str()), (Some("pending"), Some("hu")));
|
||||
assert_eq!(
|
||||
(r.body["status"].as_str(), r.body["language"].as_str(), r.body["model"].as_str(), &r.body["credits_charged"]),
|
||||
(Some("pending"), Some("hu"), Some("anthropic/claude-test"), &json!(1))
|
||||
);
|
||||
let first = r.body["id"].as_str().unwrap().to_string();
|
||||
assert_eq!(app.get("/v1/credits", Some(&anna)).await.body["balance"], 1);
|
||||
assert_eq!(app.get("/v1/credits", Some(&anna)).await.body["balance"], json!({"natal": 1, "synastry": 0}));
|
||||
|
||||
// same request again: the same analysis, no charge
|
||||
let r = app.call("POST", &uri, Some(&anna), None).await;
|
||||
assert_eq!((r.status, r.body["id"].as_str()), (StatusCode::OK, Some(first.as_str())));
|
||||
let r = app.post(&uri, Some(&anna), json!({"language": "en"})).await;
|
||||
assert_eq!(r.status, StatusCode::ACCEPTED);
|
||||
let r = app.post(&uri, Some(&anna), json!({"model": "anthropic/claude-test"})).await;
|
||||
assert_eq!(
|
||||
(r.status, r.body["error"]["code"].as_str()),
|
||||
(StatusCode::PAYMENT_REQUIRED, Some("insufficient_credits"))
|
||||
);
|
||||
let r = app.post(&uri, Some(&anna), json!({"model": "unknown/model"})).await;
|
||||
assert_eq!(r.status, StatusCode::BAD_REQUEST);
|
||||
assert_eq!(app.get("/v1/credits", Some(&anna)).await.body["balance"], json!({"natal": 0, "synastry": 0}));
|
||||
let r = app.post(&uri, Some(&anna), json!({"model": "anthropic/claude-other"})).await;
|
||||
assert_eq!(r.status, StatusCode::BAD_REQUEST); // no model choice
|
||||
|
||||
// the worker finishes the first one; another user with the same birth data gets it for free
|
||||
sqlx::query("UPDATE analyses SET status = 'done', content = $1, completed_at = now() WHERE id = $2::uuid")
|
||||
@@ -506,7 +530,6 @@ async fn analyses_spend_credits_and_reuse_results(pool: PgPool) {
|
||||
let r = app.call("POST", &format!("/v1/charts/{}/analyses", same["id"].as_str().unwrap()), Some(&bela), None).await;
|
||||
assert_eq!(r.status, StatusCode::CREATED, "{}", r.body);
|
||||
assert_eq!((r.body["status"].as_str(), &r.body["credits_charged"]), (Some("done"), &json!(0)));
|
||||
assert_eq!(app.get("/v1/credits", Some(&bela)).await.body["balance"], 2);
|
||||
|
||||
assert_eq!(app.get(&format!("/v1/analyses/{first}"), Some(&bela)).await.status, StatusCode::NOT_FOUND);
|
||||
assert_eq!(app.get(&uri, Some(&anna)).await.body["analyses"].as_array().unwrap().len(), 3); // 2 + summary
|
||||
@@ -606,30 +629,26 @@ async fn public_config_and_health(pool: PgPool) {
|
||||
assert_eq!(r.body["google_login"]["enabled"], true);
|
||||
assert_eq!(r.body["google_login"]["client_id"], GOOGLE_CLIENT);
|
||||
assert_eq!(r.body["locales"], json!(["hu", "en"]));
|
||||
assert_eq!(r.body["default_model"], "openrouter/auto");
|
||||
assert_eq!(r.body["llm_models"][2], json!({"id": "anthropic/claude-vip", "credits": 2, "role": "vip"}));
|
||||
assert_eq!(r.body["analysis_model"], "anthropic/claude-test");
|
||||
assert_eq!(r.body["summary_model"], "anthropic/claude-summary");
|
||||
assert_eq!(app.get("/healthz", None).await.status, StatusCode::OK);
|
||||
}
|
||||
|
||||
#[sqlx::test(migrator = "db::MIGRATOR")]
|
||||
async fn tier_models_need_their_role(pool: PgPool) {
|
||||
async fn synastry_analyses_use_synastry_units(pool: PgPool) {
|
||||
let app = TestApp::new(pool).await;
|
||||
let anna = app.register("anna@example.com", "Anna").await;
|
||||
let admin = app.register("admin@example.com", "Admin").await;
|
||||
let chart = app.chart(&anna, "Saját", true, "1990-03-15").await;
|
||||
let uri = format!("/v1/charts/{}/analyses", chart["id"].as_str().unwrap());
|
||||
let vip = json!({"model": "anthropic/claude-vip"});
|
||||
let own = app.chart(&anna, "Saját", true, "1990-03-15").await;
|
||||
let other = app.chart(&anna, "Anya", false, "1950-03-01").await;
|
||||
let r = app.post("/v1/synastries", Some(&anna), json!({"chart1_id": own["id"], "chart2_id": other["id"]})).await;
|
||||
assert_eq!(r.status, StatusCode::CREATED, "{}", r.body);
|
||||
let uri = format!("/v1/synastries/{}/analyses", r.body["id"].as_str().unwrap());
|
||||
|
||||
let r = app.post(&uri, Some(&anna), vip.clone()).await;
|
||||
assert_eq!((r.status, r.body["error"]["code"].as_str()), (StatusCode::FORBIDDEN, Some("forbidden")));
|
||||
|
||||
let anna_id = app.get("/v1/me", Some(&anna)).await.body["user"]["id"].as_str().unwrap().to_string();
|
||||
let r = app
|
||||
.call("PATCH", &format!("/v1/admin/users/{anna_id}"), Some(&admin), Some(json!({"roles": ["user", "vip"]})))
|
||||
.await;
|
||||
assert_eq!(r.status, StatusCode::OK, "{}", r.body);
|
||||
let r = app.post(&uri, Some(&anna), vip).await;
|
||||
assert_eq!(r.status, StatusCode::ACCEPTED, "{}", r.body);
|
||||
assert_eq!((r.body["model"].as_str(), &r.body["credits_charged"]), (Some("anthropic/claude-vip"), &json!(2)));
|
||||
assert_eq!(app.get("/v1/credits", Some(&anna)).await.body["balance"], 0);
|
||||
grant_units(&app, &anna, "natal", 1).await;
|
||||
let r = app.call("POST", &uri, Some(&anna), None).await;
|
||||
assert_eq!(r.status, StatusCode::PAYMENT_REQUIRED); // a natal unit does not pay for a synastry
|
||||
grant_units(&app, &anna, "synastry", 1).await;
|
||||
let r = app.call("POST", &uri, Some(&anna), None).await;
|
||||
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}));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user