gradatum-server 0.4.1

Stateless HTTP/MCP façade :19090 — handles read/search + enqueues writes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
//! Helpers tests partagés — `vault_trace`, `vault_read`, `vault_context`.
//!
//! Pattern TDD : `#[path = "helpers/mod.rs"] mod helpers;` au début de chaque fichier
//! de test d'intégration correspondant.
//!
//! ## Conventions de typage
//!
//! - `vault_id` runtime = `&str` (généralement `"main"` dans les helpers)
//! - `tenant_id` DTO = `String` (sérialisation JSON inchangée)
//! - `seed_note_with_fts` hard-code `vault_id='main'` côté SQLite — paramètre `vault`
//!   du helper conservé pour signature lisible (ignoré côté SQL pour l'instant).
//!
//! ## API réelle vs spec rev2.1
//!
//! La spec rev2.1 a documenté des signatures indicatives. L'API réelle SqliteIndex
//! impose :
//! - `seed_note_with_fts(id, section, body)` — vault toujours "main"
//! - `upsert_note_title(NoteId, title)` — pas de vault arg
//! - `downgrade_note(NoteId, reason, replaced_by)` — pas de `set_status`
//!
//! Les helpers ci-dessous adaptent à cette API tout en gardant l'intention de la spec.

#![allow(dead_code)]

use std::sync::Arc;

use async_trait::async_trait;
use axum::body::Body;
use axum::http::{Request, StatusCode};
use gradatum_acl_policy::AclEngine;
use gradatum_auth::jwt::{JwtService, TokenScope};
use gradatum_core::identity::NoteId;
use gradatum_embed::error::EmbedError;
use gradatum_embed::{EmbedBackend, Embedder};
use gradatum_index::SqliteIndex;
use gradatum_server::state::AppState;
use http_body_util::BodyExt;
use tempfile::TempDir;
use tower::ServiceExt;
use ulid::Ulid;

/// Preset ACL minimal autorisant `read_patterns` larges sur le tenant `main`.
///
/// Les patterns autorisent à la fois `main/*` (locus avec section explicite),
/// `main/main` (locus tenant-only) et `*/reference` (cross-tenant section
/// `reference`) pour couvrir tous les chemins de section utilisés dans les
/// tests Tasks 14/15/16.
pub const TEST_ACL: &str = r#"
[[consumer]]
identity = "alpha13-tester"
read_patterns  = ["main/*", "main/main", "*/reference", "reference/*", "decisions/*", "main/decisions"]
write_patterns = []
"#;

/// Embedder Noop dimension 8 — utilisé pour tous les tests Tasks 14/15/16 qui ne
/// dépendent pas de la sémantique embedding (BM25 + title_lookup + FTS pur).
pub struct NoopBackend;

#[async_trait]
impl Embedder for NoopBackend {
    fn embedder_id(&self) -> &str {
        "noop-alpha13"
    }
    fn dim(&self) -> u16 {
        8
    }
    async fn embed(&self, _text: &str) -> Result<Vec<f32>, EmbedError> {
        Ok(vec![0.0f32; 8])
    }
    async fn embed_batch(&self, texts: &[&str]) -> Result<Vec<Vec<f32>>, EmbedError> {
        Ok(texts.iter().map(|_| vec![0.0f32; 8]).collect())
    }
    fn backend_kind(&self) -> EmbedBackend {
        EmbedBackend::Noop
    }
}

/// Bundle test : `(Router, AppState, TempDir)` — TempDir DOIT être conservé en vie
/// pour la durée du test (sinon le vault disque disparaît et `read_note_by_id` échoue).
///
/// Utiliser le pattern :
/// ```ignore
/// let env = build_app().await;
/// let token = sign_token(&env.state);
/// let resp = call_vault_read(env.app.clone(), &token, ...).await;
/// // env._tmp est drop à la fin du test → vault tempdir nettoyé
/// ```
pub struct TestEnv {
    pub app: axum::Router,
    pub state: AppState,
    /// Handle typé vers le vault concret — nécessaire pour `vault.write_note`
    /// (méthode inhérente, pas dans le trait Registry).
    pub _vault_typed: Arc<gradatum_vault::Vault>,
    /// Conservé pour empêcher le drop du vault disque pendant le test.
    pub _tmp: TempDir,
}

/// Construit un environnement de test E2E avec un Vault réel sur TempDir.
///
/// - `state.search` = index `vault.index()` partagé avec le vault (cohérence write/read).
/// - `state.vault` = `Vault::create(TempDir/.gradatum/...)` (lecture par ULID OK).
/// - Embedder Noop dim=8.
/// - ACL `alpha13-tester` (read scope).
///
/// **Important** : `vault.write_note(frontmatter, body)` est requis pour que
/// `state.vault.read_note_by_id(ulid)` réussisse (fichier .md sur disque + index SQLite).
/// Les helpers `seed_note_with_h1` / `seed_note_downgraded` utilisent ce chemin.
pub async fn build_app() -> TestEnv {
    use axum::{middleware, Router};
    use gradatum_core::scope::VaultId;
    use gradatum_vault::Vault;

    let tmp = TempDir::new().expect("TempDir tests/helpers");
    let vault_path = tmp.path().join("vault");
    let vault = Arc::new(
        Vault::create(&vault_path, VaultId::new("main"))
            .await
            .expect("Vault::create test fixture"),
    );
    let vault_registry: Arc<dyn gradatum_vault::Registry> = vault.clone();
    let index = vault.index().clone();

    let jwt = JwtService::new_ephemeral();
    let acl = AclEngine::from_preset_str(TEST_ACL).expect("preset ACL alpha13 valide");
    let mut state = AppState::with_jwt_and_acl(jwt, acl)
        .with_embedder(Arc::new(NoopBackend))
        .with_vault_arc(vault_registry);
    // Aligner state.search sur le SqliteIndex utilisé par le vault — sinon
    // les tests qui font `state.search.title_lookup` seraient déconnectés du
    // vault-disk-write. Pas de `with_search_arc` public — assignation directe
    // (le field est `pub` dans AppState).
    state.search = index;

    let app = Router::new()
        .nest("/api/v1", gradatum_server::api_v1::router())
        .layer(middleware::from_fn_with_state(
            state.clone(),
            gradatum_server::middleware::auth_middleware,
        ))
        .with_state(state.clone());

    TestEnv {
        app,
        state,
        _vault_typed: vault,
        _tmp: tmp,
    }
}

/// Signe un JWT pour le consumer `alpha13-tester` (read scope, tenant `main`).
pub fn sign_token(state: &AppState) -> String {
    state
        .jwt
        .sign(
            "alpha13-tester",
            &["read".to_string()],
            TokenScope::Service,
            "main",
        )
        .expect("sign JWT alpha13-tester")
}

/// Seed une note via SQL only (`seed_note_with_fts` + `upsert_note_title`), SANS
/// passer par `Vault::write_note`.
///
/// **Limitation** : le fichier `.md` n'est PAS créé sur disque — `state.vault.read_note_by_id`
/// renverra une erreur Storage. Utilisable uniquement pour les tests qui consomment
/// l'index SQLite directement (ex. tests `state.search.title_lookup` ou tests qui
/// appellent les handlers utilisant le SQLite — Tasks 15/16 vault_trace/vault_context
/// utilisent `state.search.get_note()` directement).
pub async fn seed_note_sql_only(
    idx: &SqliteIndex,
    ulid: &str,
    section: &str,
    title: &str,
    body: &str,
) -> NoteId {
    let full = format!("# {title}\n{body}");
    idx.seed_note_with_fts(ulid, section, &full)
        .await
        .expect("seed_note_with_fts");
    let nid = NoteId(Ulid::from_string(ulid).expect("ULID parse seed_note_sql_only"));
    idx.upsert_note_title(&nid, title)
        .await
        .expect("upsert_note_title");
    nid
}

/// Variante SQL-only avec downgrade (status='downgraded').
pub async fn seed_note_downgraded_sql(idx: &SqliteIndex, ulid: &str, title: &str) -> NoteId {
    let nid = seed_note_sql_only(idx, ulid, "reference", title, "downgraded body").await;
    idx.downgrade_note(&nid, "test fixture downgrade", None)
        .await
        .expect("downgrade_note");
    nid
}

/// Seed une note dans une section spécifique (SQL only — pour Tasks 15/16 FTS).
pub async fn seed_note_in_section(
    idx: &SqliteIndex,
    ulid: &str,
    section: &str,
    title: &str,
    body: &str,
) -> NoteId {
    seed_note_sql_only(idx, ulid, section, title, body).await
}

// ── Variante TestEnv : seed via Vault::write_note (fichier .md + SQL) ──────────

impl TestEnv {
    /// Seed une note via `Vault::write_note` — fichier .md sur disque + index SQLite.
    ///
    /// Permet à `state.vault.read_note_by_id(ulid)` de réussir.
    /// Retourne le `NoteId` réel généré par `Vault::write_note` + applique
    /// `upsert_note_title` pour la résolution `title_lookup`.
    pub async fn write_note_with_h1(&self, title: &str, body: &str) -> NoteId {
        self.write_note_in_section("reference", title, body).await
    }

    /// Variante avec section explicite.
    pub async fn write_note_in_section(&self, section: &str, title: &str, body: &str) -> NoteId {
        use chrono::Utc;
        use gradatum_core::frontmatter::{ExtraFields, Frontmatter};
        use gradatum_core::scope::VaultId;
        use gradatum_core::section::Section;
        use gradatum_core::status::NoteStatus;

        let section_enum = match section {
            "decisions" => Section::Decisions,
            "experiments" => Section::Experiments,
            "debug" => Section::Debug,
            "architecture" => Section::Architecture,
            "retrospectives" => Section::Retrospectives,
            "reasoning" => Section::Reasoning,
            "feedback" => Section::Feedback,
            "lessons-learned" => Section::LessonsLearned,
            "agent-issues" => Section::AgentIssues,
            _ => Section::Reference,
        };
        let frontmatter = Frontmatter {
            schema_version: 1,
            vault_id: VaultId::new("main"),
            locus: None,
            section: section_enum,
            status: NoteStatus::Live,
            status_reason: None,
            status_changed: None,
            tags: Default::default(),
            author: None,
            created: Utc::now(),
            updated: None,
            extra: ExtraFields::empty(),
            provenance: None,
        };

        let body_full = format!("# {title}\n{body}");
        let note = self
            .vault_arc()
            .write_note(frontmatter, body_full)
            .await
            .expect("vault.write_note seed");

        // Upsert title (mimique du curator post-extract — alpha.10 migration 0005).
        self.state
            .search
            .upsert_note_title(&note.id, title)
            .await
            .expect("upsert_note_title seed");

        note.id
    }

    /// Seed une note + applique downgrade.
    pub async fn write_note_downgraded(&self, title: &str) -> NoteId {
        let nid = self.write_note_with_h1(title, "downgraded body").await;
        self.state
            .search
            .downgrade_note(&nid, "test fixture downgrade", None)
            .await
            .expect("downgrade_note");
        nid
    }

    /// Récupère le `Arc<Vault>` typé concret stocké dans le TestEnv.
    fn vault_arc(&self) -> Arc<gradatum_vault::Vault> {
        self._vault_typed.clone()
    }
}

/// Effectue une requête `POST /api/v1/vault_read` et retourne la `Response` complète.
///
/// Permet aux tests d'inspecter le `StatusCode` (404, 200) avant de décoder le body.
pub async fn call_vault_read_raw(
    app: axum::Router,
    token: &str,
    path: &str,
    tenant_id: &str,
) -> axum::http::Response<Body> {
    let body = serde_json::json!({
        "path": path,
        "tenant_id": tenant_id,
    });
    let req = Request::builder()
        .uri("/api/v1/vault_read")
        .method("POST")
        .header("content-type", "application/json")
        .header("authorization", format!("Bearer {token}"))
        .body(Body::from(serde_json::to_vec(&body).unwrap()))
        .unwrap();
    app.oneshot(req).await.expect("vault_read oneshot")
}

/// Effectue `POST /api/v1/vault_read` et décode le JSON si statut 200.
///
/// Renvoie `Err(StatusCode)` si non-200.
pub async fn call_vault_read(
    app: axum::Router,
    token: &str,
    path: &str,
    tenant_id: &str,
) -> Result<serde_json::Value, StatusCode> {
    let resp = call_vault_read_raw(app, token, path, tenant_id).await;
    let status = resp.status();
    if status != StatusCode::OK {
        return Err(status);
    }
    let bytes = resp.into_body().collect().await.unwrap().to_bytes();
    Ok(serde_json::from_slice(&bytes).expect("decode vault_read JSON"))
}

/// Effectue `POST /api/v1/vault_trace` et retourne la `Response`.
pub async fn call_vault_trace_raw(
    app: axum::Router,
    token: &str,
    query: &str,
    tenant_id: &str,
    limit: u32,
) -> axum::http::Response<Body> {
    let body = serde_json::json!({
        "query": query,
        "tenant_id": tenant_id,
        "limit": limit,
    });
    let req = Request::builder()
        .uri("/api/v1/vault_trace")
        .method("POST")
        .header("content-type", "application/json")
        .header("authorization", format!("Bearer {token}"))
        .body(Body::from(serde_json::to_vec(&body).unwrap()))
        .unwrap();
    app.oneshot(req).await.expect("vault_trace oneshot")
}

/// Effectue `POST /api/v1/vault_trace` et décode le JSON si 200.
pub async fn call_vault_trace(
    app: axum::Router,
    token: &str,
    query: &str,
    tenant_id: &str,
    limit: u32,
) -> Result<serde_json::Value, StatusCode> {
    let resp = call_vault_trace_raw(app, token, query, tenant_id, limit).await;
    let status = resp.status();
    if status != StatusCode::OK {
        return Err(status);
    }
    let bytes = resp.into_body().collect().await.unwrap().to_bytes();
    Ok(serde_json::from_slice(&bytes).expect("decode vault_trace JSON"))
}

/// Effectue `POST /api/v1/vault_context`.
pub async fn call_vault_context_raw(
    app: axum::Router,
    token: &str,
    query: &str,
    tenant_id: &str,
    max_tokens: Option<u32>,
    section: Option<&str>,
) -> axum::http::Response<Body> {
    let mut body = serde_json::Map::new();
    body.insert(
        "query".to_string(),
        serde_json::Value::String(query.to_string()),
    );
    body.insert(
        "tenant_id".to_string(),
        serde_json::Value::String(tenant_id.to_string()),
    );
    if let Some(mt) = max_tokens {
        body.insert(
            "max_tokens".to_string(),
            serde_json::Value::Number(mt.into()),
        );
    }
    if let Some(sec) = section {
        body.insert(
            "section".to_string(),
            serde_json::Value::String(sec.to_string()),
        );
    }
    let body = serde_json::Value::Object(body);
    let req = Request::builder()
        .uri("/api/v1/vault_context")
        .method("POST")
        .header("content-type", "application/json")
        .header("authorization", format!("Bearer {token}"))
        .body(Body::from(serde_json::to_vec(&body).unwrap()))
        .unwrap();
    app.oneshot(req).await.expect("vault_context oneshot")
}

/// Effectue `POST /api/v1/vault_context` et décode le JSON si 200.
pub async fn call_vault_context(
    app: axum::Router,
    token: &str,
    query: &str,
    tenant_id: &str,
    max_tokens: Option<u32>,
    section: Option<&str>,
) -> Result<serde_json::Value, StatusCode> {
    let resp = call_vault_context_raw(app, token, query, tenant_id, max_tokens, section).await;
    let status = resp.status();
    if status != StatusCode::OK {
        return Err(status);
    }
    let bytes = resp.into_body().collect().await.unwrap().to_bytes();
    Ok(serde_json::from_slice(&bytes).expect("decode vault_context JSON"))
}