feat(ui): Verwaltung und Chat-Playground (#1014)
- Recht admin am Key (nexus key create --admin); ui bleibt lesend plus Chat - Sitzung liest die Rechte bei jedem Aufruf neu; gesperrte Keys verlieren sie - Ändernde Anfragen brauchen CSRF-Token, gleiche Herkunft und landen im Audit - Modelle: laden, entladen (nur unbenutzt), freigeben, sperren, Planung ändern - Warteschlange, Keys anlegen (einmalige Anzeige) und sperren - Playground: Chat mit Streaming über denselben Weg wie die API - Scheduler: Entladen von Hand Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01GmHhg3bfByVgQ9z7CGCSGc
This commit is contained in:
@ -154,6 +154,8 @@ struct Gateway {
|
||||
key_metrics: String,
|
||||
/// Key mit dem Recht `ui`.
|
||||
key_ui: String,
|
||||
/// Key mit dem Recht `admin`.
|
||||
key_admin: String,
|
||||
client: reqwest::Client,
|
||||
}
|
||||
|
||||
@ -221,6 +223,7 @@ async fn gateway_with(
|
||||
vram_mb: Option<i64>,
|
||||
options: RuntimeOptions,
|
||||
) -> Gateway {
|
||||
init_tracing();
|
||||
let backend_port = fake_backend().await;
|
||||
let engine = Arc::new(FixedEngine {
|
||||
port: backend_port,
|
||||
@ -263,6 +266,7 @@ async fn gateway_with(
|
||||
models: vec!["bonsai".to_string()],
|
||||
metrics: false,
|
||||
ui: false,
|
||||
admin: false,
|
||||
},
|
||||
Priority::Normal,
|
||||
Priority::Normal,
|
||||
@ -279,6 +283,7 @@ async fn gateway_with(
|
||||
models: vec!["*".to_string()],
|
||||
metrics: true,
|
||||
ui: false,
|
||||
admin: false,
|
||||
},
|
||||
Priority::Normal,
|
||||
Priority::Normal,
|
||||
@ -291,12 +296,27 @@ async fn gateway_with(
|
||||
models: vec![],
|
||||
metrics: false,
|
||||
ui: true,
|
||||
admin: false,
|
||||
},
|
||||
Priority::Normal,
|
||||
Priority::Normal,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let key_admin = store
|
||||
.create_api_key(
|
||||
"verwalter",
|
||||
&Rights {
|
||||
models: vec!["*".to_string()],
|
||||
metrics: false,
|
||||
ui: false,
|
||||
admin: true,
|
||||
},
|
||||
Priority::Normal,
|
||||
Priority::High,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let mut engines: HashMap<String, Arc<dyn Engine>> = HashMap::new();
|
||||
engines.insert("llama.cpp".to_string(), engine.clone());
|
||||
let state = AppState::new(store, Runtime::with_options(engines, options));
|
||||
@ -310,6 +330,7 @@ async fn gateway_with(
|
||||
key_vip,
|
||||
key_metrics,
|
||||
key_ui,
|
||||
key_admin,
|
||||
client: reqwest::Client::new(),
|
||||
}
|
||||
}
|
||||
@ -600,7 +621,24 @@ impl LogBuffer {
|
||||
/// Fängt die Logs des aktuellen Threads im Format von `nexus serve` ein. Gilt
|
||||
/// nur für Tests mit der Standard-Laufzeit `current_thread`, in der auch das
|
||||
/// Gateway läuft.
|
||||
/// Setzt einmal je Testprogramm einen globalen Empfänger, der alles annimmt
|
||||
/// und verwirft. Ohne ihn merkt sich `tracing` für Logstellen, die zuerst in
|
||||
/// einem Test ohne Empfänger erreicht werden, „niemand hört zu“; laufen die
|
||||
/// Tests parallel, fehlen dann im Test mit `capture_logs` einzelne Zeilen
|
||||
/// und Spans.
|
||||
fn init_tracing() {
|
||||
static ONCE: std::sync::Once = std::sync::Once::new();
|
||||
ONCE.call_once(|| {
|
||||
let sink = tracing_subscriber::fmt()
|
||||
.with_max_level(tracing::Level::DEBUG)
|
||||
.with_writer(std::io::sink)
|
||||
.finish();
|
||||
let _ = tracing::subscriber::set_global_default(sink);
|
||||
});
|
||||
}
|
||||
|
||||
fn capture_logs() -> (LogBuffer, tracing::subscriber::DefaultGuard) {
|
||||
init_tracing();
|
||||
let buffer = LogBuffer::default();
|
||||
let subscriber = tracing_subscriber::fmt()
|
||||
.json()
|
||||
@ -922,12 +960,22 @@ async fn abschlusszeile_im_log_nennt_tokens_und_dauer_aber_keinen_inhalt() {
|
||||
let id = resp.headers()["x-request-id"].to_str().unwrap().to_string();
|
||||
let _ = resp.text().await;
|
||||
wait_for_rows(&gw, 1).await;
|
||||
// Die Logzeile entsteht kurz nach der Zeile in der Datenbank.
|
||||
let deadline = Instant::now() + Duration::from_secs(3);
|
||||
while !logs
|
||||
.lines()
|
||||
.iter()
|
||||
.any(|l| message(l) == "Antwort abgeschlossen")
|
||||
&& Instant::now() < deadline
|
||||
{
|
||||
tokio::time::sleep(Duration::from_millis(10)).await;
|
||||
}
|
||||
|
||||
let lines = logs.lines();
|
||||
let zeile = lines
|
||||
.iter()
|
||||
.find(|l| message(l) == "Antwort abgeschlossen" && l["span"]["korrelation"] == id)
|
||||
.expect("Abschlusszeile mit der ID der Anfrage");
|
||||
.unwrap_or_else(|| panic!("Abschlusszeile mit der ID {id} fehlt: {lines:#?}"));
|
||||
assert_eq!(zeile["tokens_in"], 7);
|
||||
assert_eq!(zeile["tokens_out"], 9);
|
||||
assert_eq!(zeile["aborted"], false);
|
||||
@ -1076,7 +1124,7 @@ async fn seiten_zeigen_modelle_anfragen_audit_und_instanzen_ohne_inhalte() {
|
||||
|
||||
let (_, _, modelle) = ui_get(&gw, "/ui/modelle", Some(&cookie)).await;
|
||||
assert!(modelle.contains(">bonsai</td>") && modelle.contains("geladen"));
|
||||
assert!(modelle.contains(">gesperrt</span>") && modelle.contains("/m/zweit.gguf"));
|
||||
assert!(modelle.contains(">gesperrt</span>") && !modelle.contains("/m/zweit.gguf"));
|
||||
|
||||
let (_, _, anfragen) = ui_get(&gw, "/ui/anfragen", Some(&cookie)).await;
|
||||
assert!(anfragen.contains(&id), "Korrelations-ID der Anfrage fehlt");
|
||||
@ -1541,3 +1589,482 @@ async fn gemessener_vram_wird_gespeichert_und_begrenzt_die_gpu() {
|
||||
assert_eq!(view.instances.len(), 2, "{view:?}");
|
||||
assert_eq!(stops(&gw), 1);
|
||||
}
|
||||
|
||||
// --- Verwaltung und Playground (0.1.7) ---------------------------------------
|
||||
|
||||
/// Meldet an und liefert Cookie und CSRF-Token der Sitzung.
|
||||
async fn ui_session(gw: &Gateway, key: &str) -> (String, String) {
|
||||
let resp = ui_login(gw, key).await;
|
||||
assert_eq!(resp.status(), 303);
|
||||
let set = resp.headers()["set-cookie"].to_str().unwrap().to_string();
|
||||
let cookie = set.split(';').next().unwrap().to_string();
|
||||
let (_, _, page) = ui_get(gw, "/ui/chat", Some(&cookie)).await;
|
||||
let csrf = page
|
||||
.split("data-csrf=\"")
|
||||
.nth(1)
|
||||
.and_then(|rest| rest.split('"').next())
|
||||
.expect("CSRF-Token auf der Seite")
|
||||
.to_string();
|
||||
assert_eq!(csrf.len(), 64);
|
||||
(cookie, csrf)
|
||||
}
|
||||
|
||||
async fn ui_post(
|
||||
gw: &Gateway,
|
||||
path: &str,
|
||||
cookie: &str,
|
||||
body: &str,
|
||||
) -> (u16, Option<String>, String) {
|
||||
let resp = no_redirect()
|
||||
.post(format!("{}{path}", gw.base))
|
||||
.header("origin", &gw.base)
|
||||
.header("cookie", cookie)
|
||||
.header(CONTENT_TYPE, "application/x-www-form-urlencoded")
|
||||
.body(body.to_string())
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
let status = resp.status().as_u16();
|
||||
let location = resp
|
||||
.headers()
|
||||
.get("location")
|
||||
.map(|v| v.to_str().unwrap().to_string());
|
||||
(status, location, resp.text().await.unwrap())
|
||||
}
|
||||
|
||||
fn audit_actions(gw: &Gateway) -> Vec<(String, String, String)> {
|
||||
gw.state
|
||||
.store()
|
||||
.recent_audit(50)
|
||||
.unwrap()
|
||||
.into_iter()
|
||||
.map(|a| (a.actor, a.action, a.target.unwrap_or_default()))
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn verwaltung_braucht_admin_recht_csrf_token_und_gleiche_herkunft() {
|
||||
let gw = gateway(0, false).await;
|
||||
let (ui_cookie, ui_csrf) = ui_session(&gw, &gw.key_ui).await;
|
||||
let (admin_cookie, admin_csrf) = ui_session(&gw, &gw.key_admin).await;
|
||||
|
||||
// Ohne Recht admin: abgelehnt, nichts geschieht.
|
||||
let body = format!("csrf={ui_csrf}&name=bonsai");
|
||||
let (status, _, text) = ui_post(&gw, "/ui/modelle/laden", &ui_cookie, &body).await;
|
||||
assert_eq!(status, 403);
|
||||
assert!(text.contains("admin"), "{text}");
|
||||
let (status, _, _) = ui_get(&gw, "/ui/keys", Some(&ui_cookie)).await;
|
||||
assert_eq!(status, 403);
|
||||
let (_, _, seite) = ui_get(&gw, "/ui/modelle", Some(&ui_cookie)).await;
|
||||
assert!(!seite.contains("/ui/modelle/laden") && !seite.contains("href=\"/ui/keys\""));
|
||||
|
||||
// Admin ohne oder mit fremdem Token: abgelehnt.
|
||||
for body in [
|
||||
"name=bonsai".to_string(),
|
||||
format!("csrf={ui_csrf}&name=bonsai"),
|
||||
format!("csrf={}&name=bonsai", "0".repeat(64)),
|
||||
] {
|
||||
let (status, _, _) = ui_post(&gw, "/ui/modelle/laden", &admin_cookie, &body).await;
|
||||
assert_eq!(status, 403, "{body}");
|
||||
}
|
||||
// Fremde Herkunft trotz gültigem Token: abgelehnt.
|
||||
let resp = no_redirect()
|
||||
.post(format!("{}/ui/modelle/laden", gw.base))
|
||||
.header("origin", "http://boese.example")
|
||||
.header("cookie", &admin_cookie)
|
||||
.header(CONTENT_TYPE, "application/x-www-form-urlencoded")
|
||||
.body(format!("csrf={admin_csrf}&name=bonsai"))
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(resp.status(), 403);
|
||||
// Ohne Sitzung: zur Anmeldung.
|
||||
let (status, location, _) =
|
||||
ui_post(&gw, "/ui/modelle/laden", "nexus_ui=falsch", "name=bonsai").await;
|
||||
assert_eq!((status, location.as_deref()), (303, Some("/ui/login")));
|
||||
|
||||
assert_eq!(starts(&gw), 0, "nichts wurde geladen");
|
||||
assert!(
|
||||
!audit_actions(&gw).iter().any(|a| a.1.starts_with("model.")),
|
||||
"keine Verwaltungsaktion im Audit"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn admin_laedt_und_entlaedt_ein_modell_und_das_audit_nennt_ihn() {
|
||||
let gw = gateway(0, false).await;
|
||||
let (cookie, csrf) = ui_session(&gw, &gw.key_admin).await;
|
||||
|
||||
let (status, location, _) = ui_post(
|
||||
&gw,
|
||||
"/ui/modelle/laden",
|
||||
&cookie,
|
||||
&format!("csrf={csrf}&name=bonsai"),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(
|
||||
(status, location.as_deref()),
|
||||
(303, Some("/ui/modelle?m=laden"))
|
||||
);
|
||||
wait_until("Modell geladen und unbenutzt", || {
|
||||
let view = gw.state.runtime.scheduler_view();
|
||||
view.instances
|
||||
.iter()
|
||||
.any(|i| i.model == "bonsai" && i.state == "ready" && i.active == 0)
|
||||
})
|
||||
.await;
|
||||
assert_eq!(starts(&gw), 1);
|
||||
let (_, _, teil) = ui_get(&gw, "/ui/teil/modelle", Some(&cookie)).await;
|
||||
assert!(
|
||||
teil.contains("geladen") && teil.contains("/ui/modelle/entladen"),
|
||||
"{teil}"
|
||||
);
|
||||
let (_, _, seite) = ui_get(&gw, "/ui/modelle?m=laden", Some(&cookie)).await;
|
||||
assert!(seite.contains("Das Modell wird geladen") && seite.contains("/m/bonsai.gguf"));
|
||||
|
||||
let (_, location, _) = ui_post(
|
||||
&gw,
|
||||
"/ui/modelle/entladen",
|
||||
&cookie,
|
||||
&format!("csrf={csrf}&name=bonsai"),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(location.as_deref(), Some("/ui/modelle?m=entladen"));
|
||||
wait_until("Modell entladen", || stops(&gw) == 1).await;
|
||||
let (_, location, _) = ui_post(
|
||||
&gw,
|
||||
"/ui/modelle/entladen",
|
||||
&cookie,
|
||||
&format!("csrf={csrf}&name=bonsai"),
|
||||
)
|
||||
.await;
|
||||
assert!(
|
||||
matches!(
|
||||
location.as_deref(),
|
||||
Some("/ui/modelle?m=nicht_geladen" | "/ui/modelle?m=uebergang")
|
||||
),
|
||||
"{location:?}"
|
||||
);
|
||||
let (_, location, _) = ui_post(
|
||||
&gw,
|
||||
"/ui/modelle/laden",
|
||||
&cookie,
|
||||
&format!("csrf={csrf}&name=gibtsnicht"),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(location.as_deref(), Some("/ui/modelle?m=unbekannt"));
|
||||
|
||||
let audit = audit_actions(&gw);
|
||||
for action in ["model.load", "model.unload"] {
|
||||
assert!(
|
||||
audit.contains(&(
|
||||
"ui:verwalter".to_string(),
|
||||
action.to_string(),
|
||||
"bonsai".to_string()
|
||||
)),
|
||||
"{action} fehlt: {audit:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn benutztes_modell_wird_nicht_von_hand_entladen() {
|
||||
let gw = Arc::new(gateway(0, false).await);
|
||||
let (cookie, csrf) = ui_session(&gw, &gw.key_admin).await;
|
||||
let stream = {
|
||||
let gw = gw.clone();
|
||||
tokio::spawn(async move {
|
||||
gw.chat(Some(&gw.key), None, &chat_body("bonsai", true))
|
||||
.await
|
||||
.text()
|
||||
.await
|
||||
.unwrap()
|
||||
})
|
||||
};
|
||||
wait_until("Stream läuft", || {
|
||||
let view = gw.state.runtime.scheduler_view();
|
||||
view.instances.iter().any(|i| i.active == 1)
|
||||
})
|
||||
.await;
|
||||
let (_, location, _) = ui_post(
|
||||
&gw,
|
||||
"/ui/modelle/entladen",
|
||||
&cookie,
|
||||
&format!("csrf={csrf}&name=bonsai"),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(location.as_deref(), Some("/ui/modelle?m=benutzt"));
|
||||
assert!(stream.await.unwrap().contains("[DONE]"));
|
||||
assert_eq!(stops(&gw), 0);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn admin_sperrt_modelle_und_aendert_die_planung() {
|
||||
let gw = gateway(0, false).await;
|
||||
let (cookie, csrf) = ui_session(&gw, &gw.key_admin).await;
|
||||
|
||||
let (_, location, _) = ui_post(
|
||||
&gw,
|
||||
"/ui/modelle/freigabe",
|
||||
&cookie,
|
||||
&format!("csrf={csrf}&name=bonsai&an=0"),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(location.as_deref(), Some("/ui/modelle?m=gesperrt"));
|
||||
assert_eq!(
|
||||
gw.chat(Some(&gw.key), None, &chat_body("bonsai", false))
|
||||
.await
|
||||
.status(),
|
||||
404
|
||||
);
|
||||
ui_post(
|
||||
&gw,
|
||||
"/ui/modelle/freigabe",
|
||||
&cookie,
|
||||
&format!("csrf={csrf}&name=bonsai&an=1"),
|
||||
)
|
||||
.await;
|
||||
|
||||
let (_, location, _) = ui_post(
|
||||
&gw,
|
||||
"/ui/modelle/planung",
|
||||
&cookie,
|
||||
&format!("csrf={csrf}&name=bonsai&vram_mb=4321&idle_min=5"),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(location.as_deref(), Some("/ui/modelle?m=planung"));
|
||||
let plan = gw.state.store().start_plan("bonsai").unwrap().unwrap();
|
||||
assert_eq!((plan.vram_mb, plan.idle_secs), (Some(4321), Some(300)));
|
||||
// Dieselbe VRAM-Angabe noch einmal verwirft die Messung nicht; leerer Leerlauf heißt Standard.
|
||||
gw.state.store().set_measured_vram("bonsai", 777).unwrap();
|
||||
ui_post(
|
||||
&gw,
|
||||
"/ui/modelle/planung",
|
||||
&cookie,
|
||||
&format!("csrf={csrf}&name=bonsai&vram_mb=4321&idle_min="),
|
||||
)
|
||||
.await;
|
||||
let plan = gw.state.store().start_plan("bonsai").unwrap().unwrap();
|
||||
assert_eq!((plan.vram_mb, plan.idle_secs), (Some(777), None));
|
||||
for bad in ["vram_mb=abc", "vram_mb=0", "idle_min=-3"] {
|
||||
let (_, location, _) = ui_post(
|
||||
&gw,
|
||||
"/ui/modelle/planung",
|
||||
&cookie,
|
||||
&format!("csrf={csrf}&name=bonsai&{bad}"),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(location.as_deref(), Some("/ui/modelle?m=eingabe"), "{bad}");
|
||||
}
|
||||
let audit = audit_actions(&gw);
|
||||
for action in ["model.disable", "model.enable", "model.set"] {
|
||||
assert!(
|
||||
audit.iter().any(|a| a.0 == "ui:verwalter" && a.1 == action),
|
||||
"{action}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn admin_legt_keys_an_der_key_erscheint_einmal_und_gesperrte_verlieren_die_sitzung() {
|
||||
let gw = gateway(0, false).await;
|
||||
let (cookie, csrf) = ui_session(&gw, &gw.key_admin).await;
|
||||
|
||||
let (status, _, seite) = ui_post(
|
||||
&gw,
|
||||
"/ui/keys/anlegen",
|
||||
&cookie,
|
||||
&format!("csrf={csrf}&name=neu-1&models=bonsai%2C+zweit&default=low&max=normal&ui=1"),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(status, 200);
|
||||
let key = seite
|
||||
.split("id=\"neuer-key\">")
|
||||
.nth(1)
|
||||
.and_then(|rest| rest.split('<').next())
|
||||
.expect("Key auf der Seite")
|
||||
.to_string();
|
||||
assert!(key.starts_with("nx-"), "{key}");
|
||||
// Der neue Key gilt sofort, mit genau den gewählten Rechten.
|
||||
let record = gw.state.store().find_api_key(&key).unwrap().unwrap();
|
||||
assert_eq!(record.rights.models, ["bonsai", "zweit"]);
|
||||
assert!(record.rights.ui && !record.rights.admin && !record.rights.metrics);
|
||||
assert_eq!(
|
||||
(record.default_priority, record.max_priority),
|
||||
(Priority::Low, Priority::Normal)
|
||||
);
|
||||
// Danach steht er nirgends mehr: nicht auf der Seite, nicht im Audit.
|
||||
let (_, _, liste) = ui_get(&gw, "/ui/keys", Some(&cookie)).await;
|
||||
assert!(liste.contains("neu-1") && !liste.contains(&key));
|
||||
assert!(!format!("{:?}", audit_actions(&gw)).contains(&key));
|
||||
|
||||
for (body, code) in [
|
||||
("name=neu-1&models=*&default=normal&max=normal", "key_name"),
|
||||
(
|
||||
"name=mit+leer&models=*&default=normal&max=normal",
|
||||
"key_name",
|
||||
),
|
||||
("name=x&models=*&default=high&max=normal", "key_prio"),
|
||||
("name=x&models=&default=normal&max=normal", "eingabe"),
|
||||
("name=x&models=*&default=vip&max=normal", "eingabe"),
|
||||
] {
|
||||
let (_, location, _) = ui_post(
|
||||
&gw,
|
||||
"/ui/keys/anlegen",
|
||||
&cookie,
|
||||
&format!("csrf={csrf}&{body}"),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(location, Some(format!("/ui/keys?m={code}")), "{body}");
|
||||
}
|
||||
|
||||
// Der neue Key meldet sich an; nach dem Sperren endet seine Sitzung sofort.
|
||||
let (neu_cookie, _) = ui_session(&gw, &key).await;
|
||||
let (_, location, _) = ui_post(
|
||||
&gw,
|
||||
"/ui/keys/status",
|
||||
&cookie,
|
||||
&format!("csrf={csrf}&name=neu-1&an=0"),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(location.as_deref(), Some("/ui/keys?m=key_gesperrt"));
|
||||
let (status, _, _) = ui_get(&gw, "/ui", Some(&neu_cookie)).await;
|
||||
assert_eq!(status, 303, "gesperrter Key ist abgemeldet");
|
||||
assert_eq!(
|
||||
gw.chat(Some(&key), None, &chat_body("bonsai", false))
|
||||
.await
|
||||
.status(),
|
||||
401
|
||||
);
|
||||
|
||||
// Den eigenen Key sperrt man hier nicht.
|
||||
let (_, location, _) = ui_post(
|
||||
&gw,
|
||||
"/ui/keys/status",
|
||||
&cookie,
|
||||
&format!("csrf={csrf}&name=verwalter&an=0"),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(location.as_deref(), Some("/ui/keys?m=eigener_key"));
|
||||
let audit = audit_actions(&gw);
|
||||
for action in ["key.create", "key.disable"] {
|
||||
assert!(
|
||||
audit.contains(&(
|
||||
"ui:verwalter".to_string(),
|
||||
action.to_string(),
|
||||
"neu-1".to_string()
|
||||
)),
|
||||
"{action}: {audit:?}"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async fn playground(
|
||||
gw: &Gateway,
|
||||
cookie: &str,
|
||||
csrf: Option<&str>,
|
||||
body: Value,
|
||||
) -> reqwest::Response {
|
||||
let mut req = gw
|
||||
.client
|
||||
.post(format!("{}/ui/chat/senden", gw.base))
|
||||
.header("origin", &gw.base)
|
||||
.header("cookie", cookie)
|
||||
.header(CONTENT_TYPE, "application/json")
|
||||
.body(body.to_string());
|
||||
if let Some(csrf) = csrf {
|
||||
req = req.header("x-csrf-token", csrf);
|
||||
}
|
||||
req.send().await.unwrap()
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn playground_streamt_mit_den_rechten_des_keys_und_protokolliert_nur_metadaten() {
|
||||
let gw = gateway(0, false).await;
|
||||
let (cookie, csrf) = ui_session(&gw, &gw.key_ui).await;
|
||||
let (_, _, seite) = ui_get(&gw, "/ui/chat", Some(&cookie)).await;
|
||||
// Der UI-Key hat keine Modelle: Die Seite bietet nichts an.
|
||||
assert!(seite.contains("kein Modell freigegeben"), "{seite}");
|
||||
let frage =
|
||||
json!({"model": "bonsai", "messages": [{"role": "user", "content": "GEHEIM-PLAYGROUND"}]});
|
||||
let resp = playground(&gw, &cookie, Some(&csrf), frage.clone()).await;
|
||||
assert_eq!(
|
||||
resp.status(),
|
||||
404,
|
||||
"Modell für diesen Key nicht freigegeben"
|
||||
);
|
||||
|
||||
let (cookie, csrf) = ui_session(&gw, &gw.key_admin).await;
|
||||
let (_, _, seite) = ui_get(&gw, "/ui/chat", Some(&cookie)).await;
|
||||
assert!(seite.contains("<option value=\"bonsai\">") && !seite.contains("value=\"gesperrt\""));
|
||||
assert!(seite.contains("/ui/chat.js"));
|
||||
|
||||
// Ohne Token und ohne Sitzung: JSON-Fehler, kein Modellstart.
|
||||
assert_eq!(
|
||||
playground(&gw, &cookie, None, frage.clone()).await.status(),
|
||||
403
|
||||
);
|
||||
assert_eq!(
|
||||
playground(&gw, "nexus_ui=falsch", Some(&csrf), frage.clone())
|
||||
.await
|
||||
.status(),
|
||||
401
|
||||
);
|
||||
assert_eq!(
|
||||
playground(
|
||||
&gw,
|
||||
&cookie,
|
||||
Some(&csrf),
|
||||
json!({"model": "bonsai", "messages": []})
|
||||
)
|
||||
.await
|
||||
.status(),
|
||||
400
|
||||
);
|
||||
assert_eq!(starts(&gw), 0);
|
||||
|
||||
let resp = playground(&gw, &cookie, Some(&csrf), frage).await;
|
||||
assert_eq!(resp.status(), 200);
|
||||
assert_eq!(resp.headers()[CONTENT_TYPE], "text/event-stream");
|
||||
let text = resp.text().await.unwrap();
|
||||
assert!(
|
||||
text.contains("Hal") && text.contains("\"usage\"") && text.contains("[DONE]"),
|
||||
"{text}"
|
||||
);
|
||||
|
||||
let rows = wait_for_rows(&gw, 2).await;
|
||||
let row = rows.iter().find(|r| r.status == 200).expect("Zeile");
|
||||
assert!(
|
||||
row.model_id.is_some() && row.api_key_id.is_some(),
|
||||
"{row:?}"
|
||||
);
|
||||
assert_eq!((row.tokens_in, row.tokens_out), (Some(7), Some(9)));
|
||||
let (_, _, anfragen) = ui_get(&gw, "/ui/anfragen", Some(&cookie)).await;
|
||||
assert!(anfragen.contains("verwalter") && !anfragen.contains("GEHEIM-PLAYGROUND"));
|
||||
let script = gw
|
||||
.client
|
||||
.get(format!("{}/ui/chat.js", gw.base))
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(script.status(), 200);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn warteschlange_und_navigation_je_nach_recht() {
|
||||
let gw = gateway(0, false).await;
|
||||
let (ui_cookie, _) = ui_session(&gw, &gw.key_ui).await;
|
||||
let (admin_cookie, _) = ui_session(&gw, &gw.key_admin).await;
|
||||
let (status, _, seite) = ui_get(&gw, "/ui/warteschlange", Some(&ui_cookie)).await;
|
||||
assert_eq!(status, 200);
|
||||
assert!(seite.contains("Niemand wartet") && seite.contains("Kein Modell geladen"));
|
||||
assert!(seite.contains("lesend, mit Playground") && !seite.contains(">admin</span>"));
|
||||
let (_, _, seite) = ui_get(&gw, "/ui", Some(&admin_cookie)).await;
|
||||
assert!(seite.contains("href=\"/ui/keys\"") && seite.contains(">admin</span>"));
|
||||
gw.chat(Some(&gw.key), None, &chat_body("bonsai", false))
|
||||
.await;
|
||||
let (_, _, teil) = ui_get(&gw, "/ui/teil/warteschlange", Some(&admin_cookie)).await;
|
||||
assert!(teil.contains("bonsai") && teil.contains("bereit"), "{teil}");
|
||||
let (status, _, _) = ui_get(&gw, "/ui/teil/warteschlange", None).await;
|
||||
assert_eq!(status, 401);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user