11eb645f3f
The distinction between `open/` and `done/` directories for cases has been removed. All cases for a user now reside directly under the user's directory (e.g., `<data_path>/<slug>/<case_id>/`). This simplifies path management and eliminates redundant directory traversals. Key changes include: - Removed `open/` and `done/` subdirectories in path resolution. - Introduced a `paths::case_dir` function as a single source of truth for case directory layout. - Updated various modules (`recovery`, `auth`, `routes`, `transcribe`, `tests`) to use the new path structure. - Adjusted templates to reflect the simplified case status representation.
647 lines
23 KiB
Rust
647 lines
23 KiB
Rust
use std::collections::HashMap;
|
|
use std::path::{Path, PathBuf};
|
|
use std::sync::Arc;
|
|
use std::time::Duration;
|
|
|
|
use axum::body::Body;
|
|
use axum::http::{header, Request, StatusCode};
|
|
use doctate_server::analyze;
|
|
use doctate_server::config::{Config, User};
|
|
use serde_json::{json, Value};
|
|
use tower::util::ServiceExt;
|
|
use wiremock::matchers::{method, path};
|
|
use wiremock::{Mock, MockServer, ResponseTemplate};
|
|
|
|
// ---------------------------------------------------------------------
|
|
// Fixture helpers
|
|
// ---------------------------------------------------------------------
|
|
|
|
fn unique_tmp(label: &str) -> PathBuf {
|
|
std::env::temp_dir().join(format!(
|
|
"doctate-close-{label}-{}-{}",
|
|
std::process::id(),
|
|
uuid::Uuid::new_v4()
|
|
))
|
|
}
|
|
|
|
fn make_user(slug: &str) -> User {
|
|
User {
|
|
slug: slug.into(),
|
|
api_key: format!("key-{slug}"),
|
|
web_password: bcrypt::hash("s", 4).unwrap(),
|
|
role: "doctor".into(),
|
|
whisper: Default::default(),
|
|
}
|
|
}
|
|
|
|
fn config_with_llm(data_path: PathBuf, llm_url: String) -> Arc<Config> {
|
|
config_with_llm_users(data_path, llm_url, vec![make_user("dr_a")])
|
|
}
|
|
|
|
fn config_with_llm_users(data_path: PathBuf, llm_url: String, users: Vec<User>) -> Arc<Config> {
|
|
let api_keys: HashMap<String, String> = users
|
|
.iter()
|
|
.map(|u| (u.api_key.clone(), u.slug.clone()))
|
|
.collect();
|
|
Arc::new(Config {
|
|
data_path,
|
|
users,
|
|
api_keys,
|
|
llm_url,
|
|
llm_api_key: "test-key".into(),
|
|
llm_model: "test-model".into(),
|
|
..Config::test_default()
|
|
})
|
|
}
|
|
|
|
fn make_admin(slug: &str) -> User {
|
|
User {
|
|
slug: slug.into(),
|
|
api_key: format!("key-{slug}"),
|
|
web_password: bcrypt::hash("s", 4).unwrap(),
|
|
role: "admin".into(),
|
|
whisper: Default::default(),
|
|
}
|
|
}
|
|
|
|
fn config_without_llm(data_path: PathBuf) -> Arc<Config> {
|
|
let users = vec![make_user("dr_a")];
|
|
let api_keys: HashMap<String, String> = users
|
|
.iter()
|
|
.map(|u| (u.api_key.clone(), u.slug.clone()))
|
|
.collect();
|
|
Arc::new(Config {
|
|
data_path,
|
|
users,
|
|
api_keys,
|
|
..Config::test_default()
|
|
})
|
|
}
|
|
|
|
fn seed_case(data_path: &Path, slug: &str, case_id: &str) -> PathBuf {
|
|
let dir = data_path.join(slug).join(case_id);
|
|
std::fs::create_dir_all(&dir).unwrap();
|
|
dir
|
|
}
|
|
|
|
fn seed_recording(case_dir: &Path, ts_hms: &str, transcript: Option<&str>) {
|
|
let filename = format!("2026-04-15T{ts_hms}Z.m4a");
|
|
std::fs::write(case_dir.join(&filename), b"audio-bytes").unwrap();
|
|
if let Some(text) = transcript {
|
|
let tx_name = format!("2026-04-15T{ts_hms}Z.transcript.txt");
|
|
std::fs::write(case_dir.join(tx_name), text).unwrap();
|
|
}
|
|
}
|
|
|
|
async fn login(app: axum::Router, slug: &str) -> String {
|
|
let body = format!("slug={slug}&password=s");
|
|
let resp = app
|
|
.oneshot(
|
|
Request::builder()
|
|
.method("POST")
|
|
.uri("/web/login")
|
|
.header(header::CONTENT_TYPE, "application/x-www-form-urlencoded")
|
|
.body(Body::from(body))
|
|
.unwrap(),
|
|
)
|
|
.await
|
|
.unwrap();
|
|
for v in resp.headers().get_all(header::SET_COOKIE).iter() {
|
|
let s = v.to_str().unwrap();
|
|
if let Some(pair) = s.split(';').next()
|
|
&& pair.starts_with("session=")
|
|
{
|
|
return pair.to_string();
|
|
}
|
|
}
|
|
panic!("no session cookie after login");
|
|
}
|
|
|
|
fn close_request(case_id: &str, cookie: &str) -> Request<Body> {
|
|
Request::builder()
|
|
.method("POST")
|
|
.uri(format!("/web/cases/{case_id}/close"))
|
|
.header(header::COOKIE, cookie)
|
|
.body(Body::empty())
|
|
.unwrap()
|
|
}
|
|
|
|
// ---------------------------------------------------------------------
|
|
// HTTP-level precondition tests
|
|
// ---------------------------------------------------------------------
|
|
|
|
#[tokio::test]
|
|
async fn close_without_cookie_redirects_to_login() {
|
|
let config = config_with_llm(unique_tmp("a"), "http://unused".into());
|
|
let app = doctate_server::create_router(config);
|
|
let case_id = "11111111-1111-1111-1111-111111111111";
|
|
let resp = app
|
|
.oneshot(
|
|
Request::builder()
|
|
.method("POST")
|
|
.uri(format!("/web/cases/{case_id}/close"))
|
|
.body(Body::empty())
|
|
.unwrap(),
|
|
)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::FOUND);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn close_foreign_case_returns_404() {
|
|
let config = config_with_llm(unique_tmp("b"), "http://unused".into());
|
|
// Seed a case owned by someone else.
|
|
let foreign_case = "22222222-2222-2222-2222-222222222222";
|
|
let foreign_dir = config.data_path.join("dr_other/open").join(foreign_case);
|
|
std::fs::create_dir_all(&foreign_dir).unwrap();
|
|
seed_recording(&foreign_dir, "10-00-00", Some("text"));
|
|
|
|
let app = doctate_server::create_router(config);
|
|
let cookie = login(app.clone(), "dr_a").await;
|
|
|
|
let resp = app.oneshot(close_request(foreign_case, &cookie)).await.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn close_invalid_uuid_returns_400() {
|
|
let config = config_with_llm(unique_tmp("c"), "http://unused".into());
|
|
let app = doctate_server::create_router(config);
|
|
let cookie = login(app.clone(), "dr_a").await;
|
|
|
|
let resp = app.oneshot(close_request("not-a-uuid", &cookie)).await.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn close_case_without_recordings_returns_400() {
|
|
let config = config_with_llm(unique_tmp("d"), "http://unused".into());
|
|
let case_id = "11111111-1111-1111-1111-111111111111";
|
|
seed_case(&config.data_path, "dr_a", case_id);
|
|
// No .m4a files.
|
|
|
|
let app = doctate_server::create_router(config);
|
|
let cookie = login(app.clone(), "dr_a").await;
|
|
|
|
let resp = app.oneshot(close_request(case_id, &cookie)).await.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn close_case_with_missing_transcript_returns_400() {
|
|
let config = config_with_llm(unique_tmp("e"), "http://unused".into());
|
|
let case_id = "11111111-1111-1111-1111-111111111111";
|
|
let case_dir = seed_case(&config.data_path, "dr_a", case_id);
|
|
seed_recording(&case_dir, "10-00-00", Some("ok"));
|
|
seed_recording(&case_dir, "10-05-00", None); // still transcribing
|
|
|
|
let app = doctate_server::create_router(config);
|
|
let cookie = login(app.clone(), "dr_a").await;
|
|
|
|
let resp = app.oneshot(close_request(case_id, &cookie)).await.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn close_case_without_llm_returns_503() {
|
|
let config = config_without_llm(unique_tmp("f"));
|
|
let case_id = "11111111-1111-1111-1111-111111111111";
|
|
let case_dir = seed_case(&config.data_path, "dr_a", case_id);
|
|
seed_recording(&case_dir, "10-00-00", Some("ok"));
|
|
|
|
let app = doctate_server::create_router(config);
|
|
let cookie = login(app.clone(), "dr_a").await;
|
|
|
|
let resp = app.oneshot(close_request(case_id, &cookie)).await.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::SERVICE_UNAVAILABLE);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------
|
|
// HTTP-level happy path + race
|
|
// ---------------------------------------------------------------------
|
|
|
|
#[tokio::test]
|
|
async fn close_case_happy_path_writes_input_and_redirects() {
|
|
let config = config_with_llm(unique_tmp("g"), "http://unused".into());
|
|
let case_id = "11111111-1111-1111-1111-111111111111";
|
|
let case_dir = seed_case(&config.data_path, "dr_a", case_id);
|
|
seed_recording(&case_dir, "10-00-00", Some("Patient klagt über Knie."));
|
|
seed_recording(&case_dir, "10-05-00", Some("Korrektur: links, nicht rechts."));
|
|
|
|
let app = doctate_server::create_router(config);
|
|
let cookie = login(app.clone(), "dr_a").await;
|
|
|
|
let resp = app.oneshot(close_request(case_id, &cookie)).await.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::SEE_OTHER);
|
|
assert_eq!(
|
|
resp.headers().get(header::LOCATION).unwrap().to_str().unwrap(),
|
|
format!("/web/cases/{case_id}")
|
|
);
|
|
|
|
let input_path = case_dir.join("analysis_input_v1.json");
|
|
assert!(input_path.exists(), "analysis_input_v1.json missing");
|
|
|
|
let raw = std::fs::read_to_string(&input_path).unwrap();
|
|
let parsed: Value = serde_json::from_str(&raw).unwrap();
|
|
assert_eq!(parsed["version"], 1);
|
|
let recs = parsed["recordings"].as_array().unwrap();
|
|
assert_eq!(recs.len(), 2);
|
|
assert_eq!(recs[0]["recorded_at"], "2026-04-15T10:00:00Z");
|
|
assert_eq!(recs[0]["text"], "Patient klagt über Knie.");
|
|
assert_eq!(recs[1]["recorded_at"], "2026-04-15T10:05:00Z");
|
|
assert!(parsed["last_recording_mtime"].is_string());
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn close_case_second_time_returns_409() {
|
|
let config = config_with_llm(unique_tmp("h"), "http://unused".into());
|
|
let case_id = "11111111-1111-1111-1111-111111111111";
|
|
let case_dir = seed_case(&config.data_path, "dr_a", case_id);
|
|
seed_recording(&case_dir, "10-00-00", Some("text"));
|
|
|
|
let app = doctate_server::create_router(config);
|
|
let cookie = login(app.clone(), "dr_a").await;
|
|
|
|
let first = app.clone().oneshot(close_request(case_id, &cookie)).await.unwrap();
|
|
assert_eq!(first.status(), StatusCode::SEE_OTHER);
|
|
|
|
let second = app.oneshot(close_request(case_id, &cookie)).await.unwrap();
|
|
assert_eq!(second.status(), StatusCode::CONFLICT);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn close_case_skips_blank_transcript_from_recordings() {
|
|
let config = config_with_llm(unique_tmp("i"), "http://unused".into());
|
|
let case_id = "11111111-1111-1111-1111-111111111111";
|
|
let case_dir = seed_case(&config.data_path, "dr_a", case_id);
|
|
seed_recording(&case_dir, "10-00-00", Some("echt"));
|
|
seed_recording(&case_dir, "10-05-00", Some(" ")); // blank
|
|
seed_recording(&case_dir, "10-10-00", Some("auch echt"));
|
|
|
|
let app = doctate_server::create_router(config);
|
|
let cookie = login(app.clone(), "dr_a").await;
|
|
|
|
let resp = app.oneshot(close_request(case_id, &cookie)).await.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::SEE_OTHER);
|
|
|
|
let raw = std::fs::read_to_string(case_dir.join("analysis_input_v1.json")).unwrap();
|
|
let parsed: Value = serde_json::from_str(&raw).unwrap();
|
|
let recs = parsed["recordings"].as_array().unwrap();
|
|
assert_eq!(recs.len(), 2, "blank transcript must be filtered out");
|
|
}
|
|
|
|
// ---------------------------------------------------------------------
|
|
// Worker + wiremock integration
|
|
// ---------------------------------------------------------------------
|
|
|
|
#[tokio::test]
|
|
async fn analyze_worker_writes_document_via_wiremock() {
|
|
let tmp = unique_tmp("w");
|
|
let case_dir = tmp.join("dr_a/11111111-1111-1111-1111-111111111111");
|
|
std::fs::create_dir_all(&case_dir).unwrap();
|
|
|
|
let input = json!({
|
|
"version": 1,
|
|
"last_recording_mtime": "2026-04-15T10:00:00Z",
|
|
"recordings": [
|
|
{ "recorded_at": "2026-04-15T10:00:00Z", "text": "Patient mit Knie." }
|
|
]
|
|
});
|
|
std::fs::write(
|
|
case_dir.join("analysis_input_v1.json"),
|
|
serde_json::to_vec_pretty(&input).unwrap(),
|
|
)
|
|
.unwrap();
|
|
|
|
let mock = MockServer::start().await;
|
|
Mock::given(method("POST"))
|
|
.and(path("/v1/chat/completions"))
|
|
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
|
|
"choices": [{
|
|
"message": { "content": "Zusammenfassung: Knie-Beschwerden." }
|
|
}]
|
|
})))
|
|
.mount(&mock)
|
|
.await;
|
|
|
|
let config = config_with_llm(tmp.clone(), mock.uri());
|
|
let (tx, rx) = analyze::channel();
|
|
let client = reqwest::Client::new();
|
|
let handle = tokio::spawn(analyze::worker::run(rx, config, client));
|
|
|
|
tx.send(analyze::AnalyzeJob {
|
|
case_dir: case_dir.clone(),
|
|
version: 1,
|
|
})
|
|
.unwrap();
|
|
|
|
// Poll for the document (worker is in a separate task).
|
|
let document_path = case_dir.join("document_v1.md");
|
|
let deadline = std::time::Instant::now() + Duration::from_secs(5);
|
|
while !document_path.exists() {
|
|
if std::time::Instant::now() > deadline {
|
|
panic!("document_v1.md not written within 5s");
|
|
}
|
|
tokio::time::sleep(Duration::from_millis(20)).await;
|
|
}
|
|
|
|
let content = std::fs::read_to_string(&document_path).unwrap();
|
|
assert!(
|
|
content.contains("Knie-Beschwerden"),
|
|
"unexpected content: {content}"
|
|
);
|
|
|
|
drop(tx);
|
|
let _ = tokio::time::timeout(Duration::from_secs(2), handle).await;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------
|
|
// Recovery
|
|
// ---------------------------------------------------------------------
|
|
|
|
#[tokio::test]
|
|
async fn recovery_enqueues_pending_analysis() {
|
|
let tmp = unique_tmp("r1");
|
|
let case_dir = tmp.join("dr_a/11111111-1111-1111-1111-111111111111");
|
|
std::fs::create_dir_all(&case_dir).unwrap();
|
|
std::fs::write(case_dir.join("analysis_input_v1.json"), "{}").unwrap();
|
|
|
|
let (tx, mut rx) = analyze::channel();
|
|
analyze::recovery::scan_and_enqueue(&tmp, &tx).await;
|
|
|
|
let job = rx.try_recv().expect("expected one job");
|
|
assert_eq!(job.version, 1);
|
|
assert_eq!(job.case_dir, case_dir);
|
|
assert!(rx.try_recv().is_err(), "no further jobs expected");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn recovery_skips_completed_analysis() {
|
|
let tmp = unique_tmp("r2");
|
|
let case_dir = tmp.join("dr_a/11111111-1111-1111-1111-111111111111");
|
|
std::fs::create_dir_all(&case_dir).unwrap();
|
|
std::fs::write(case_dir.join("analysis_input_v1.json"), "{}").unwrap();
|
|
std::fs::write(case_dir.join("document_v1.md"), "done").unwrap();
|
|
|
|
let (tx, mut rx) = analyze::channel();
|
|
analyze::recovery::scan_and_enqueue(&tmp, &tx).await;
|
|
|
|
assert!(rx.try_recv().is_err(), "completed analysis must not re-enqueue");
|
|
}
|
|
|
|
// ---------------------------------------------------------------------
|
|
// Document view
|
|
// ---------------------------------------------------------------------
|
|
|
|
#[tokio::test]
|
|
async fn document_view_picks_highest_version() {
|
|
let config = config_with_llm(unique_tmp("dv"), "http://unused".into());
|
|
let case_id = "11111111-1111-1111-1111-111111111111";
|
|
let case_dir = seed_case(&config.data_path, "dr_a", case_id);
|
|
std::fs::write(case_dir.join("document_v1.md"), "alte Version").unwrap();
|
|
std::fs::write(case_dir.join("document_v2.md"), "neue Version v2").unwrap();
|
|
|
|
let app = doctate_server::create_router(config);
|
|
let cookie = login(app.clone(), "dr_a").await;
|
|
|
|
let resp = app
|
|
.oneshot(
|
|
Request::builder()
|
|
.uri(format!("/web/cases/{case_id}/document"))
|
|
.header(header::COOKIE, &cookie)
|
|
.body(Body::empty())
|
|
.unwrap(),
|
|
)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::OK);
|
|
let body = axum::body::to_bytes(resp.into_body(), usize::MAX).await.unwrap();
|
|
let body_str = String::from_utf8(body.to_vec()).unwrap();
|
|
assert!(body_str.contains("neue Version v2"), "expected v2 content");
|
|
assert!(!body_str.contains("alte Version"), "v1 must not leak");
|
|
assert!(body_str.contains("Dokument v2"));
|
|
}
|
|
|
|
// ---------------------------------------------------------------------
|
|
// Reanalyze (admin-only)
|
|
// ---------------------------------------------------------------------
|
|
|
|
fn reanalyze_request(case_id: &str, cookie: &str) -> Request<Body> {
|
|
Request::builder()
|
|
.method("POST")
|
|
.uri(format!("/web/cases/{case_id}/reanalyze"))
|
|
.header(header::COOKIE, cookie)
|
|
.body(Body::empty())
|
|
.unwrap()
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn reanalyze_by_non_admin_returns_403() {
|
|
let config = config_with_llm(unique_tmp("rn-1"), "http://unused".into());
|
|
let case_id = "11111111-1111-1111-1111-111111111111";
|
|
let case_dir = seed_case(&config.data_path, "dr_a", case_id);
|
|
std::fs::write(case_dir.join("document_v1.md"), "v1").unwrap();
|
|
seed_recording(&case_dir, "10-00-00", Some("ok"));
|
|
|
|
let app = doctate_server::create_router(config);
|
|
let cookie = login(app.clone(), "dr_a").await;
|
|
|
|
let resp = app.oneshot(reanalyze_request(case_id, &cookie)).await.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::FORBIDDEN);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn reanalyze_without_document_returns_400() {
|
|
let config = config_with_llm_users(
|
|
unique_tmp("rn-2"),
|
|
"http://unused".into(),
|
|
vec![make_admin("dr_a")],
|
|
);
|
|
let case_id = "11111111-1111-1111-1111-111111111111";
|
|
let case_dir = seed_case(&config.data_path, "dr_a", case_id);
|
|
seed_recording(&case_dir, "10-00-00", Some("ok"));
|
|
|
|
let app = doctate_server::create_router(config);
|
|
let cookie = login(app.clone(), "dr_a").await;
|
|
|
|
let resp = app.oneshot(reanalyze_request(case_id, &cookie)).await.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn reanalyze_foreign_case_returns_404() {
|
|
let config = config_with_llm_users(
|
|
unique_tmp("rn-3"),
|
|
"http://unused".into(),
|
|
vec![make_admin("dr_a")],
|
|
);
|
|
let foreign_case = "22222222-2222-2222-2222-222222222222";
|
|
let foreign_dir = config.data_path.join("dr_other/open").join(foreign_case);
|
|
std::fs::create_dir_all(&foreign_dir).unwrap();
|
|
std::fs::write(foreign_dir.join("document_v1.md"), "v1").unwrap();
|
|
|
|
let app = doctate_server::create_router(config);
|
|
let cookie = login(app.clone(), "dr_a").await;
|
|
|
|
let resp = app.oneshot(reanalyze_request(foreign_case, &cookie)).await.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn reanalyze_without_llm_returns_503() {
|
|
let users = vec![make_admin("dr_a")];
|
|
let api_keys: HashMap<String, String> = users
|
|
.iter()
|
|
.map(|u| (u.api_key.clone(), u.slug.clone()))
|
|
.collect();
|
|
let config = Arc::new(Config {
|
|
data_path: unique_tmp("rn-4"),
|
|
users,
|
|
api_keys,
|
|
..Config::test_default()
|
|
});
|
|
let case_id = "11111111-1111-1111-1111-111111111111";
|
|
let case_dir = seed_case(&config.data_path, "dr_a", case_id);
|
|
std::fs::write(case_dir.join("document_v1.md"), "v1").unwrap();
|
|
seed_recording(&case_dir, "10-00-00", Some("ok"));
|
|
|
|
let app = doctate_server::create_router(config);
|
|
let cookie = login(app.clone(), "dr_a").await;
|
|
|
|
let resp = app.oneshot(reanalyze_request(case_id, &cookie)).await.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::SERVICE_UNAVAILABLE);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn reanalyze_happy_path_writes_v2_input() {
|
|
let config = config_with_llm_users(
|
|
unique_tmp("rn-5"),
|
|
"http://unused".into(),
|
|
vec![make_admin("dr_a")],
|
|
);
|
|
let case_id = "11111111-1111-1111-1111-111111111111";
|
|
let case_dir = seed_case(&config.data_path, "dr_a", case_id);
|
|
std::fs::write(case_dir.join("document_v1.md"), "altes Dokument").unwrap();
|
|
seed_recording(&case_dir, "10-00-00", Some("erste Aufnahme"));
|
|
seed_recording(&case_dir, "10-05-00", Some("zweite Aufnahme"));
|
|
|
|
let app = doctate_server::create_router(config);
|
|
let cookie = login(app.clone(), "dr_a").await;
|
|
|
|
let resp = app.oneshot(reanalyze_request(case_id, &cookie)).await.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::SEE_OTHER);
|
|
|
|
let input_path = case_dir.join("analysis_input_v2.json");
|
|
assert!(input_path.exists(), "analysis_input_v2.json missing");
|
|
let parsed: Value = serde_json::from_str(&std::fs::read_to_string(&input_path).unwrap()).unwrap();
|
|
assert_eq!(parsed["version"], 2);
|
|
assert_eq!(parsed["recordings"].as_array().unwrap().len(), 2);
|
|
// v1 document is still there until worker writes v2.
|
|
assert!(case_dir.join("document_v1.md").exists());
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn reanalyze_second_time_returns_409() {
|
|
let config = config_with_llm_users(
|
|
unique_tmp("rn-6"),
|
|
"http://unused".into(),
|
|
vec![make_admin("dr_a")],
|
|
);
|
|
let case_id = "11111111-1111-1111-1111-111111111111";
|
|
let case_dir = seed_case(&config.data_path, "dr_a", case_id);
|
|
std::fs::write(case_dir.join("document_v1.md"), "v1").unwrap();
|
|
seed_recording(&case_dir, "10-00-00", Some("ok"));
|
|
|
|
let app = doctate_server::create_router(config);
|
|
let cookie = login(app.clone(), "dr_a").await;
|
|
|
|
let first = app.clone().oneshot(reanalyze_request(case_id, &cookie)).await.unwrap();
|
|
assert_eq!(first.status(), StatusCode::SEE_OTHER);
|
|
|
|
let second = app.oneshot(reanalyze_request(case_id, &cookie)).await.unwrap();
|
|
assert_eq!(second.status(), StatusCode::CONFLICT);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn reanalyze_worker_writes_document_v2_via_wiremock() {
|
|
let tmp = unique_tmp("rn-w");
|
|
let case_dir = tmp.join("dr_a/11111111-1111-1111-1111-111111111111");
|
|
std::fs::create_dir_all(&case_dir).unwrap();
|
|
std::fs::write(case_dir.join("document_v1.md"), "alte Version").unwrap();
|
|
|
|
let input = json!({
|
|
"version": 2,
|
|
"last_recording_mtime": "2026-04-15T10:00:00Z",
|
|
"recordings": [
|
|
{ "recorded_at": "2026-04-15T10:00:00Z", "text": "Patient." }
|
|
]
|
|
});
|
|
std::fs::write(
|
|
case_dir.join("analysis_input_v2.json"),
|
|
serde_json::to_vec_pretty(&input).unwrap(),
|
|
)
|
|
.unwrap();
|
|
|
|
let mock = MockServer::start().await;
|
|
Mock::given(method("POST"))
|
|
.and(path("/v1/chat/completions"))
|
|
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
|
|
"choices": [{ "message": { "content": "neue Fassung" } }]
|
|
})))
|
|
.mount(&mock)
|
|
.await;
|
|
|
|
let config = config_with_llm_users(tmp.clone(), mock.uri(), vec![make_admin("dr_a")]);
|
|
let (tx, rx) = analyze::channel();
|
|
let handle = tokio::spawn(analyze::worker::run(rx, config, reqwest::Client::new()));
|
|
|
|
tx.send(analyze::AnalyzeJob {
|
|
case_dir: case_dir.clone(),
|
|
version: 2,
|
|
})
|
|
.unwrap();
|
|
|
|
let doc_v2 = case_dir.join("document_v2.md");
|
|
let deadline = std::time::Instant::now() + Duration::from_secs(5);
|
|
while !doc_v2.exists() {
|
|
if std::time::Instant::now() > deadline {
|
|
panic!("document_v2.md not written within 5s");
|
|
}
|
|
tokio::time::sleep(Duration::from_millis(20)).await;
|
|
}
|
|
|
|
let content = std::fs::read_to_string(&doc_v2).unwrap();
|
|
assert!(content.contains("neue Fassung"));
|
|
// v1 untouched.
|
|
assert_eq!(
|
|
std::fs::read_to_string(case_dir.join("document_v1.md")).unwrap(),
|
|
"alte Version"
|
|
);
|
|
|
|
drop(tx);
|
|
let _ = tokio::time::timeout(Duration::from_secs(2), handle).await;
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn document_view_without_document_returns_404() {
|
|
let config = config_with_llm(unique_tmp("dv2"), "http://unused".into());
|
|
let case_id = "11111111-1111-1111-1111-111111111111";
|
|
seed_case(&config.data_path, "dr_a", case_id);
|
|
|
|
let app = doctate_server::create_router(config);
|
|
let cookie = login(app.clone(), "dr_a").await;
|
|
|
|
let resp = app
|
|
.oneshot(
|
|
Request::builder()
|
|
.uri(format!("/web/cases/{case_id}/document"))
|
|
.header(header::COOKIE, &cookie)
|
|
.body(Body::empty())
|
|
.unwrap(),
|
|
)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(resp.status(), StatusCode::NOT_FOUND);
|
|
}
|