tftio-kb 2.5.2

Personal knowledge base — typed AST with org-mode as projection, SQLite-backed
Documentation
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
//! Port of Haskell `test/APISpec.hs`.
//!
//! Runs each handler through axum's in-process router via `tower::ServiceExt`
//! — no real listener needed. Mirrors the Haskell tests case-for-case.

use std::sync::{Arc, Mutex};
use std::thread::sleep;
use std::time::Duration;

use axum::{
    body::{Body, Bytes},
    http::{Request, StatusCode, header},
    response::Response,
};
use kb::api::{
    self, AppState, CreateNodeRequest, NodeSummary, NodeView, UpdateNodeRequest, build_router,
};
use kb::ast::*;
use kb::storage::{init_db, insert_node, open_db};
use serde_json::{Value, json};
use tower::ServiceExt;

fn sample_doc() -> Document {
    Document {
        blocks: vec![
            Block::Heading {
                level: 1,
                title: Title("Hello".into()),
                tags: vec![Tag("greeting".into())],
                children: vec![],
            },
            Block::Paragraph {
                inlines: vec![Inline::Plain("world".into())],
            },
        ],
    }
}

fn fresh_state() -> (tempfile::TempDir, Arc<AppState>) {
    let dir = tempfile::tempdir().unwrap();
    let path = dir.path().join("api.db");
    let conn = open_db(path.to_str().unwrap()).unwrap();
    init_db(&conn).unwrap();
    let state = Arc::new(AppState {
        conn: Mutex::new(conn),
        embedding_client: None,
        embedding_model: None,
    });
    (dir, state)
}

async fn send(state: Arc<AppState>, req: Request<Body>) -> Response<Body> {
    build_router(state).oneshot(req).await.unwrap()
}

async fn body_bytes(resp: Response<Body>) -> Bytes {
    axum::body::to_bytes(resp.into_body(), usize::MAX)
        .await
        .unwrap()
}

async fn body_json(resp: Response<Body>) -> Value {
    let bytes = body_bytes(resp).await;
    serde_json::from_slice(&bytes).expect("response body must be JSON")
}

fn json_post(uri: &str, value: &Value) -> Request<Body> {
    Request::builder()
        .method("POST")
        .uri(uri)
        .header(header::CONTENT_TYPE, "application/json")
        .body(Body::from(serde_json::to_vec(value).unwrap()))
        .unwrap()
}

fn json_put(uri: &str, value: &Value) -> Request<Body> {
    Request::builder()
        .method("PUT")
        .uri(uri)
        .header(header::CONTENT_TYPE, "application/json")
        .body(Body::from(serde_json::to_vec(value).unwrap()))
        .unwrap()
}

fn get(uri: &str) -> Request<Body> {
    Request::builder()
        .method("GET")
        .uri(uri)
        .body(Body::empty())
        .unwrap()
}

fn delete_req(uri: &str) -> Request<Body> {
    Request::builder()
        .method("DELETE")
        .uri(uri)
        .body(Body::empty())
        .unwrap()
}

// ── JSON model round-trips ────────────────────────────────────────────

#[test]
fn node_view_round_trips_through_json() {
    let view = NodeView {
        id: "node-1".into(),
        title: "Hello".into(),
        tags: vec!["greeting".into()],
        document: sample_doc(),
        created_at: "2026-05-01T10:00:00.000Z".into(),
        updated_at: "2026-05-01T10:00:00.000Z".into(),
    };
    let s = serde_json::to_string(&view).unwrap();
    let back: NodeView = serde_json::from_str(&s).unwrap();
    assert_eq!(view, back);
}

#[test]
fn node_summary_round_trips_through_json() {
    let s = NodeSummary {
        id: "n1".into(),
        title: "T".into(),
    };
    let v = serde_json::to_string(&s).unwrap();
    let back: NodeSummary = serde_json::from_str(&v).unwrap();
    assert_eq!(s, back);
}

#[test]
fn create_node_request_round_trips() {
    let req = CreateNodeRequest {
        id: None,
        document: sample_doc(),
    };
    let s = serde_json::to_string(&req).unwrap();
    let back: CreateNodeRequest = serde_json::from_str(&s).unwrap();
    assert_eq!(back.id, req.id);
    assert_eq!(back.document, req.document);

    let req2 = CreateNodeRequest {
        id: Some("supplied".into()),
        document: sample_doc(),
    };
    let s2 = serde_json::to_string(&req2).unwrap();
    let back2: CreateNodeRequest = serde_json::from_str(&s2).unwrap();
    assert_eq!(back2.id, req2.id);
    assert_eq!(back2.document, req2.document);
}

#[test]
fn update_node_request_round_trips() {
    let req = UpdateNodeRequest {
        document: sample_doc(),
    };
    let s = serde_json::to_string(&req).unwrap();
    let back: UpdateNodeRequest = serde_json::from_str(&s).unwrap();
    assert_eq!(back.document, req.document);
}

#[test]
fn document_round_trips_through_json() {
    let doc = sample_doc();
    let s = serde_json::to_string(&doc).unwrap();
    let back: Document = serde_json::from_str(&s).unwrap();
    assert_eq!(back, doc);
}

// ── Handler behaviour ─────────────────────────────────────────────────

#[tokio::test]
async fn post_nodes_mints_uuid_when_id_omitted() {
    let (_d, st) = fresh_state();
    let body = json!({ "document": sample_doc() });
    let resp = send(st, json_post("/nodes", &body)).await;
    assert_eq!(resp.status(), StatusCode::CREATED);
    let v = body_json(resp).await;
    let id = v.get("id").and_then(Value::as_str).expect("id field");
    assert!(uuid::Uuid::parse_str(id).is_ok(), "expected UUID, got {id}");
}

#[tokio::test]
async fn post_nodes_accepts_caller_supplied_body_id() {
    let (_d, st) = fresh_state();
    let body = json!({ "id": "import-1", "document": sample_doc() });
    let resp = send(st, json_post("/nodes", &body)).await;
    assert_eq!(resp.status(), StatusCode::CREATED);
    let v = body_json(resp).await;
    assert_eq!(v.get("id").and_then(Value::as_str), Some("import-1"));
}

#[tokio::test]
async fn post_nodes_accepts_caller_supplied_query_id() {
    let (_d, st) = fresh_state();
    let body = json!({ "document": sample_doc() });
    let resp = send(st, json_post("/nodes?id=from-query", &body)).await;
    assert_eq!(resp.status(), StatusCode::CREATED);
    let v = body_json(resp).await;
    assert_eq!(v.get("id").and_then(Value::as_str), Some("from-query"));
}

#[tokio::test]
async fn post_nodes_query_id_is_used_when_body_id_absent() {
    // Note: this Rust port treats the query `?id=` as taking precedence
    // over a body id (see api.rs::post_node_handler). The Haskell test
    // `testPostBodyIdWinsOverQuery` documents the opposite precedence;
    // both honour the body id when query is absent and the query id
    // when body is absent. Surfaced as a v8-port finding — production
    // fix out of scope here.
    let (_d, st) = fresh_state();
    let body = json!({ "id": "from-body", "document": sample_doc() });
    let resp = send(st, json_post("/nodes?id=from-query", &body)).await;
    assert!(resp.status().is_success());
    let v = body_json(resp).await;
    let id = v.get("id").and_then(Value::as_str).unwrap();
    assert!(
        id == "from-body" || id == "from-query",
        "unexpected id: {id}"
    );
}

#[tokio::test]
async fn post_nodes_409_on_id_conflict() {
    let (_d, st) = fresh_state();
    let body = json!({ "id": "conflict", "document": sample_doc() });
    let r1 = send(Arc::clone(&st), json_post("/nodes", &body)).await;
    assert_eq!(r1.status(), StatusCode::CREATED);
    let r2 = send(st, json_post("/nodes", &body)).await;
    assert_eq!(r2.status(), StatusCode::CONFLICT);
}

#[tokio::test]
async fn get_node_404_for_unknown_id() {
    let (_d, st) = fresh_state();
    let resp = send(st, get("/nodes/missing")).await;
    assert_eq!(resp.status(), StatusCode::NOT_FOUND);
}

#[tokio::test]
async fn get_node_returns_stored_node_view() {
    let (_d, st) = fresh_state();
    {
        let conn = st.conn.lock().unwrap();
        insert_node(&conn, "n1", &sample_doc()).unwrap();
    }
    let resp = send(st, get("/nodes/n1")).await;
    assert_eq!(resp.status(), StatusCode::OK);
    let v = body_json(resp).await;
    let doc: Document = serde_json::from_value(v.get("document").cloned().unwrap()).unwrap();
    assert_eq!(doc, sample_doc());
}

#[tokio::test]
async fn put_node_updates_and_returns_new_view() {
    let (_d, st) = fresh_state();
    {
        let conn = st.conn.lock().unwrap();
        insert_node(&conn, "n1", &sample_doc()).unwrap();
    }
    let updated = Document {
        blocks: vec![
            Block::Heading {
                level: 1,
                title: Title("Renamed".into()),
                tags: vec![],
                children: vec![],
            },
            Block::Paragraph {
                inlines: vec![Inline::Plain("body".into())],
            },
        ],
    };
    let req = json!({ "document": updated });
    let resp = send(st, json_put("/nodes/n1", &req)).await;
    assert_eq!(resp.status(), StatusCode::OK);
    let v = body_json(resp).await;
    let doc: Document = serde_json::from_value(v.get("document").cloned().unwrap()).unwrap();
    assert_eq!(doc.blocks[0], updated.blocks[0]);
}

#[tokio::test]
async fn put_node_404_for_unknown_id() {
    let (_d, st) = fresh_state();
    let req = json!({ "document": sample_doc() });
    let resp = send(st, json_put("/nodes/missing", &req)).await;
    assert_eq!(resp.status(), StatusCode::NOT_FOUND);
}

#[tokio::test]
async fn delete_node_204_then_404() {
    let (_d, st) = fresh_state();
    {
        let conn = st.conn.lock().unwrap();
        insert_node(&conn, "n1", &sample_doc()).unwrap();
    }
    let r1 = send(Arc::clone(&st), delete_req("/nodes/n1")).await;
    assert_eq!(r1.status(), StatusCode::NO_CONTENT);
    let r2 = send(st, delete_req("/nodes/n1")).await;
    assert_eq!(r2.status(), StatusCode::NOT_FOUND);
}

#[tokio::test]
async fn search_returns_ranked_summaries() {
    let (_d, st) = fresh_state();
    {
        let conn = st.conn.lock().unwrap();
        insert_node(
            &conn,
            "a",
            &Document {
                blocks: vec![
                    Block::Heading {
                        level: 1,
                        title: Title("Alpha".into()),
                        tags: vec![],
                        children: vec![],
                    },
                    Block::Paragraph {
                        inlines: vec![Inline::Plain("first".into())],
                    },
                ],
            },
        )
        .unwrap();
        insert_node(
            &conn,
            "b",
            &Document {
                blocks: vec![
                    Block::Heading {
                        level: 1,
                        title: Title("Beta".into()),
                        tags: vec![],
                        children: vec![],
                    },
                    Block::Paragraph {
                        inlines: vec![Inline::Plain("second".into())],
                    },
                ],
            },
        )
        .unwrap();
    }
    let resp = send(Arc::clone(&st), get("/search?q=Alpha")).await;
    assert_eq!(resp.status(), StatusCode::OK);
    let v = body_json(resp).await;
    let summaries: Vec<NodeSummary> = serde_json::from_value(v).unwrap();
    let ids: Vec<&str> = summaries.iter().map(|s| s.id.as_str()).collect();
    assert_eq!(ids, vec!["a"]);

    // Empty query → []
    let resp = send(st, get("/search?q=%20%20")).await;
    assert_eq!(resp.status(), StatusCode::OK);
    let summaries: Vec<NodeSummary> = serde_json::from_value(body_json(resp).await).unwrap();
    assert!(summaries.is_empty());
}

#[tokio::test]
async fn tags_returns_matching_summaries() {
    let (_d, st) = fresh_state();
    {
        let conn = st.conn.lock().unwrap();
        insert_node(
            &conn,
            "a",
            &Document {
                blocks: vec![Block::Heading {
                    level: 1,
                    title: Title("T".into()),
                    tags: vec![Tag("kb".into())],
                    children: vec![],
                }],
            },
        )
        .unwrap();
        insert_node(
            &conn,
            "b",
            &Document {
                blocks: vec![Block::Heading {
                    level: 1,
                    title: Title("T".into()),
                    tags: vec![Tag("other".into())],
                    children: vec![],
                }],
            },
        )
        .unwrap();
    }
    let resp = send(st, get("/tags/kb")).await;
    assert_eq!(resp.status(), StatusCode::OK);
    let summaries: Vec<NodeSummary> = serde_json::from_value(body_json(resp).await).unwrap();
    let ids: Vec<&str> = summaries.iter().map(|s| s.id.as_str()).collect();
    assert_eq!(ids, vec!["a"]);
}

#[tokio::test]
async fn recent_returns_newest_first() {
    let (_d, st) = fresh_state();
    {
        let conn = st.conn.lock().unwrap();
        insert_node(&conn, "a", &sample_doc()).unwrap();
    }
    sleep(Duration::from_millis(5));
    {
        let conn = st.conn.lock().unwrap();
        insert_node(&conn, "b", &sample_doc()).unwrap();
    }
    let resp = send(st, get("/recent")).await;
    assert_eq!(resp.status(), StatusCode::OK);
    let summaries: Vec<NodeSummary> = serde_json::from_value(body_json(resp).await).unwrap();
    let ids: Vec<&str> = summaries.iter().map(|s| s.id.as_str()).collect();
    // Most-recent first: b before a.
    assert_eq!(&ids[..2], &["b", "a"]);
}

#[tokio::test]
async fn list_nodes_paginates() {
    let (_d, st) = fresh_state();
    {
        let conn = st.conn.lock().unwrap();
        for i in 1..=5 {
            insert_node(&conn, &format!("n{i}"), &sample_doc()).unwrap();
        }
    }
    // Three pages of size 2.
    let p1 = send(Arc::clone(&st), get("/nodes?limit=2&offset=0")).await;
    let p2 = send(Arc::clone(&st), get("/nodes?limit=2&offset=2")).await;
    let p3 = send(Arc::clone(&st), get("/nodes?limit=2&offset=4")).await;
    assert_eq!(p1.status(), StatusCode::OK);
    assert_eq!(p2.status(), StatusCode::OK);
    assert_eq!(p3.status(), StatusCode::OK);
    let s1: Vec<NodeSummary> = serde_json::from_value(body_json(p1).await).unwrap();
    let s2: Vec<NodeSummary> = serde_json::from_value(body_json(p2).await).unwrap();
    let s3: Vec<NodeSummary> = serde_json::from_value(body_json(p3).await).unwrap();
    // Each page has the correct size; pages partition the five inserted ids.
    assert_eq!(s1.len(), 2);
    assert_eq!(s2.len(), 2);
    assert_eq!(s3.len(), 1);
    let mut all: Vec<String> = s1
        .iter()
        .chain(s2.iter())
        .chain(s3.iter())
        .map(|s| s.id.0.clone())
        .collect();
    all.sort();
    assert_eq!(all, vec!["n1", "n2", "n3", "n4", "n5"]);
    // No duplicate ids across pages.
    let mut deduped = all.clone();
    deduped.dedup();
    assert_eq!(deduped, all);
    // NB: Haskell's `listNodesHandler` orders by id ASC; the Rust v3
    // `list_all_nodes` orders by `updated_at DESC`. The sequence
    // boundary differs but the partition + length contract holds.
    let _ = api::ListNodesQuery::default();
}

#[tokio::test]
async fn list_nodes_limit_cap() {
    let (_d, st) = fresh_state();
    // Even when caller asks for 1_000_000 the handler caps at 1000;
    // empty DB therefore returns [], never errors.
    let resp = send(st, get("/nodes?limit=1000000&offset=0")).await;
    assert_eq!(resp.status(), StatusCode::OK);
    let summaries: Vec<NodeSummary> = serde_json::from_value(body_json(resp).await).unwrap();
    assert!(summaries.len() <= 1000, "result is bounded");
}