feat: Add reanalyze case functionality
Introduce a new endpoint for reanalyzing cases. This feature allows administrators to rebuild analysis input files and enqueue new analysis jobs. The system will then generate a new document version, which will become the displayed document.
This commit is contained in:
@@ -35,7 +35,10 @@ fn make_user(slug: &str) -> User {
|
||||
}
|
||||
|
||||
fn config_with_llm(data_path: PathBuf, llm_url: String) -> Arc<Config> {
|
||||
let users = vec![make_user("dr_a")];
|
||||
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()))
|
||||
@@ -51,6 +54,16 @@ fn config_with_llm(data_path: PathBuf, llm_url: String) -> Arc<Config> {
|
||||
})
|
||||
}
|
||||
|
||||
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
|
||||
@@ -410,6 +423,206 @@ async fn document_view_picks_highest_version() {
|
||||
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/open/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());
|
||||
|
||||
Reference in New Issue
Block a user