weftgraph 0.1.0

Graph Storage gear: typed, multi-tenant knowledge graph with search and traversal over a pluggable store
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
#![allow(clippy::expect_used, clippy::unwrap_used)]

//! The REST surface, through the router the gear actually registers.
//!
//! The route test in `api::rest::routes` asserts that every operation
//! registers its schemas; nothing asserted that a request reaching one comes
//! back as the documented document. These cases send real HTTP through the
//! real router -- the handlers, the DTO conversions and the canonical error
//! mapping included -- with the platform's authentication layer stood in for
//! by an injected `SecurityContext`, which is what that layer produces.

#[allow(dead_code)]
mod conformance;
mod support;

use std::sync::Arc;

use axum::Router;
use axum::body::Body;
use axum::http::{Request, StatusCode};
use graph_storage::api::rest::routes::register_routes;
use support::Harness;
use tower::ServiceExt;

/// Registers nothing: what is under test here is the traffic, not the
/// schemas (`every_route_registers_its_schemas` covers those).
struct NoopRegistry;

impl toolkit::api::OpenApiRegistry for NoopRegistry {
    fn register_operation(&self, _spec: &toolkit::api::OperationSpec) {}

    fn ensure_schema_raw(
        &self,
        name: &str,
        _schemas: Vec<(
            String,
            utoipa::openapi::RefOr<utoipa::openapi::schema::Schema>,
        )>,
    ) -> String {
        name.to_owned()
    }

    fn as_any(&self) -> &dyn std::any::Any {
        self
    }
}

const BASE: &str = "/graph-storage/v1";

struct Stand {
    router: Router,
    harness: Harness,
}

impl Stand {
    fn new(harness: Harness) -> Self {
        let router = register_routes(Router::new(), &NoopRegistry, Arc::clone(&harness.services))
            // What the platform's authentication middleware would have put
            // there. Injected rather than mocked at the transport level: the
            // handlers take it as an extension, and that is the seam.
            .layer(axum::Extension(harness.ctx()));
        Self { router, harness }
    }

    async fn call(&self, request: Request<Body>) -> (StatusCode, serde_json::Value) {
        let response = self
            .router
            .clone()
            .oneshot(request)
            .await
            .expect("the router answers");
        let status = response.status();
        let bytes = axum::body::to_bytes(response.into_body(), 4 * 1024 * 1024)
            .await
            .expect("the body reads");
        let body = if bytes.is_empty() {
            serde_json::Value::Null
        } else {
            serde_json::from_slice(&bytes).unwrap_or(serde_json::Value::Null)
        };
        (status, body)
    }

    async fn get(&self, path: &str) -> (StatusCode, serde_json::Value) {
        self.call(
            Request::builder()
                .uri(format!("{BASE}{path}"))
                .body(Body::empty())
                .expect("a valid request"),
        )
        .await
    }

    async fn post(&self, path: &str, body: &serde_json::Value) -> (StatusCode, serde_json::Value) {
        self.call(
            Request::builder()
                .method("POST")
                .uri(format!("{BASE}{path}"))
                .header("content-type", "application/json")
                .body(Body::from(body.to_string()))
                .expect("a valid request"),
        )
        .await
    }

    async fn delete(&self, path: &str) -> (StatusCode, serde_json::Value) {
        self.call(
            Request::builder()
                .method("DELETE")
                .uri(format!("{BASE}{path}"))
                .body(Body::empty())
                .expect("a valid request"),
        )
        .await
    }
}

/// Register the ontology over HTTP, which is also the first assertion: the
/// gear's own base types go in through the same endpoint a producer uses.
async fn seed(stand: &Stand) {
    let types: Vec<serde_json::Value> = conformance::ontology_batch()
        .into_iter()
        .map(|registration| {
            serde_json::json!({
                "type_id": registration.type_id,
                "schema": registration.schema,
            })
        })
        .collect();
    let (status, body) = stand
        .post("/types", &serde_json::json!({ "types": types }))
        .await;
    assert_eq!(status, StatusCode::OK, "registration failed: {body}");
}

fn ingest_body() -> serde_json::Value {
    serde_json::json!({
        "nodes": [
            {"node_key": "rest-a", "type_id": conformance::OWNED, "name": "findable alpha"},
            {"node_key": "rest-b", "type_id": conformance::OWNED, "name": "findable beta"}
        ],
        "edges": [
            {"type_id": conformance::LINK, "src_node_key": "rest-a", "dst_node_key": "rest-b"}
        ]
    })
}

/// The byte bounds are the domain's, so REST answers them the same way the
/// in-process client does.
///
/// Worth asserting at this level rather than inferring it: the finding that
/// prompted these bounds pointed out that a gateway body cap, wherever one
/// exists, is not the gear's admission -- and a `ClientHub` caller has no
/// gateway in front of it at all. Both paths reach the same check because it
/// lives below both, and this is where that stops being a claim.
#[tokio::test]
async fn an_oversized_batch_is_refused_over_rest_with_the_bound_named() {
    let small = graph_storage::config::GraphStorageConfig {
        ingest_max_bytes: 16 * 1024,
        ..graph_storage::config::GraphStorageConfig::default()
    };
    let stand = Stand::new(Harness::configured(
        std::sync::Arc::new(support::AllowInOwnTenant),
        small,
    ));
    seed(&stand).await;

    let filler = "x".repeat(2 * 1024);
    let nodes: Vec<serde_json::Value> = (0..32)
        .map(|index| {
            serde_json::json!({
                "node_key": format!("rest-fat-{index}"),
                "type_id": conformance::OWNED,
                "payload": { "note": filler },
            })
        })
        .collect();

    let (status, body) = stand
        .post(
            "/ingest",
            &serde_json::json!({ "nodes": nodes, "edges": [] }),
        )
        .await;
    // `out_of_range`, not a server error and not a validation error: a value
    // outside a documented hard range is something backoff can never make
    // valid, which is the distinction the error model draws.
    assert_eq!(
        status,
        StatusCode::BAD_REQUEST,
        "an over-budget batch is a limit refusal: {body}"
    );
    assert_eq!(
        body["context"]["field_violations"][0]["reason"], "LIMIT_EXCEEDED",
        "{body}"
    );
    assert!(
        body.to_string().contains("ingest_max_bytes"),
        "the refusal names the bound it hit: {body}"
    );
}

#[tokio::test]
async fn the_write_and_read_endpoints_answer_the_documented_documents() {
    let stand = Stand::new(Harness::allowed());
    seed(&stand).await;

    let (status, body) = stand.post("/ingest", &ingest_body()).await;
    assert_eq!(status, StatusCode::OK, "{body}");
    assert_eq!(body["counts"]["nodes_inserted"], 2, "{body}");
    assert_eq!(body["counts"]["edges_inserted"], 1, "{body}");

    // Node read: payload, adjacency and the envelope, in one document.
    let (status, node) = stand.get("/nodes/rest-a").await;
    assert_eq!(status, StatusCode::OK, "{node}");
    assert_eq!(node["node_key"], "rest-a");
    assert_eq!(
        node["adjacency"].as_array().map(Vec::len),
        Some(1),
        "{node}"
    );
    assert_eq!(node["envelope"]["key"], "rest-a", "{node}");
    assert!(
        node["envelope"]["graph_revision"]["revision"].as_i64() > Some(0),
        "the envelope carries the observed revision: {node}"
    );

    // Edge read, addressed by the key the node read handed out.
    let edge_key = node["adjacency"][0]["edge_key"]
        .as_str()
        .expect("the adjacency entry names its edge")
        .to_owned();
    let (status, edge) = stand.get(&format!("/edges/{edge_key}")).await;
    assert_eq!(status, StatusCode::OK, "{edge}");
    assert_eq!(edge["src"], "rest-a");
    assert_eq!(edge["dst"], "rest-b");
    assert_eq!(edge["envelope"]["key"], edge_key, "{edge}");

    // Projection.
    let (status, page) = stand.get("/nodes?limit=10").await;
    assert_eq!(status, StatusCode::OK, "{page}");
    assert_eq!(page["items"].as_array().map(Vec::len), Some(2), "{page}");

    // Search.
    let (status, hits) = stand
        .post(
            "/search",
            &serde_json::json!({"mode": "lexical", "query": "findable", "limit": 10}),
        )
        .await;
    assert_eq!(status, StatusCode::OK, "{hits}");
    assert_eq!(hits["hits"].as_array().map(Vec::len), Some(2), "{hits}");

    // Traversal and neighborhood.
    let (status, walked) = stand
        .post(
            "/graph/traverse",
            &serde_json::json!({"seeds": ["rest-a"], "depth": 1}),
        )
        .await;
    assert_eq!(status, StatusCode::OK, "{walked}");
    assert_eq!(
        walked["nodes"].as_array().map(Vec::len),
        Some(2),
        "{walked}"
    );

    let (status, around) = stand
        .post(
            "/graph/neighborhood",
            &serde_json::json!({"root": "rest-a", "depth": 1}),
        )
        .await;
    assert_eq!(status, StatusCode::OK, "{around}");

    // Ontology reads.
    let (status, types) = stand.get("/types").await;
    assert_eq!(status, StatusCode::OK, "{types}");
    let (status, one) = stand.get(&format!("/types/{}", conformance::OWNED)).await;
    assert_eq!(status, StatusCode::OK, "{one}");
    assert_eq!(one["type_id"], conformance::OWNED);

    // Deletes, and the tombstone the next read observes.
    let (status, deleted) = stand.delete("/nodes/rest-a").await;
    assert_eq!(status, StatusCode::OK, "{deleted}");
    assert_eq!(deleted["tombstoned_nodes"], 1, "{deleted}");
    let (status, _) = stand.get("/nodes/rest-a").await;
    assert_eq!(status, StatusCode::NOT_FOUND, "a tombstoned node is gone");
}

#[tokio::test]
async fn readiness_is_reachable_without_a_caller_and_names_its_components() {
    let stand = Stand::new(Harness::allowed());
    let (status, body) = stand.get("/health/ready").await;
    assert!(
        status == StatusCode::OK || status == StatusCode::SERVICE_UNAVAILABLE,
        "readiness answers one way or the other, got {status}: {body}"
    );
    assert!(
        body["components"]
            .as_array()
            .is_some_and(|rows| !rows.is_empty()),
        "the matrix's rows are on the answer: {body}"
    );
}

#[tokio::test]
async fn the_namespace_endpoints_list_and_transfer() {
    let stand = Stand::new(Harness::allowed());
    seed(&stand).await;

    let (status, listed) = stand.get("/source-namespaces").await;
    assert_eq!(status, StatusCode::OK, "{listed}");
    assert_eq!(listed["items"].as_array().map(Vec::len), Some(0));

    let (status, moved) = stand
        .post(
            "/source-namespaces/mirror/owner",
            &serde_json::json!({"owner_principal": "mirror-gear"}),
        )
        .await;
    assert_eq!(status, StatusCode::OK, "{moved}");
    assert_eq!(moved["owner_principal"], "mirror-gear");

    let (_, listed) = stand.get("/source-namespaces").await;
    assert_eq!(
        listed["items"].as_array().map(Vec::len),
        Some(1),
        "{listed}"
    );
}

#[tokio::test]
async fn a_dry_run_compatibility_check_answers_without_writing() {
    let stand = Stand::new(Harness::allowed());
    seed(&stand).await;

    let changed = serde_json::json!({
        "type_id": conformance::OWNED,
        "schema": {
            "$id": format!("gts://{}", conformance::OWNED),
            "$schema": "http://json-schema.org/draft-07/schema#",
            "x-gts-traits": {
                "full_text_search": ["/name"],
                "vector_search": ["/payload/summary"]
            },
            "type": "object",
            "allOf": [
                { "$ref": "gts://gts.cf.core.graph.node.v1~cf.core.graph.owned_node.v1~" },
                { "type": "object", "properties": {
                    "payload": { "type": "object", "properties": {
                        "summary": { "type": "string" }
                    }}
                }}
            ]
        }
    });
    let (status, verdicts) = stand
        .post(
            "/types/compatibility",
            &serde_json::json!({ "types": [changed] }),
        )
        .await;
    assert_eq!(status, StatusCode::OK, "{verdicts}");
    let verdict = verdicts["items"]
        .as_array()
        .and_then(|rows| rows.first().cloned())
        .expect("one verdict per submitted type");
    assert!(
        verdict["change"].is_object(),
        "the verdict says what the change is: {verdict}"
    );
}

#[tokio::test]
async fn a_refusal_comes_back_as_a_problem_document_with_its_reason() {
    let stand = Stand::new(Harness::allowed());
    seed(&stand).await;

    // A bound, refused rather than clamped: `out_of_range`.
    let over = stand.harness.services.config().node_read_max_adjacency + 1;
    let (status, problem) = stand
        .get(&format!("/nodes/rest-a?adjacency_limit={over}"))
        .await;
    assert_eq!(status, StatusCode::BAD_REQUEST, "{problem}");
    assert!(
        problem["detail"].is_string() || problem["title"].is_string(),
        "the refusal is an RFC-9457 problem document: {problem}"
    );

    // A search mode that needs text and was given none: an argument error,
    // which is a different reason from a breached bound.
    let (status, problem) = stand
        .post("/search", &serde_json::json!({"mode": "hybrid"}))
        .await;
    assert_eq!(status, StatusCode::BAD_REQUEST, "{problem}");

    // An unknown node: absent, never "denied", so an unauthorized key and a
    // nonexistent one are indistinguishable from outside.
    let (status, _) = stand.get("/nodes/never-existed").await;
    assert_eq!(status, StatusCode::NOT_FOUND);
}

/// A denied caller gets the *absent* answer, not a forbidden one.
///
/// That is the documented posture and not an accident of mapping: telling a
/// caller "you may not see this" confirms the thing exists, so denied and
/// nonexistent answer identically (DESIGN, anti-enumeration). The case pins
/// it because the obvious "fix" is to return 403 and it would be a leak.
#[tokio::test]
async fn a_denied_caller_cannot_tell_denial_from_absence() {
    let stand = Stand::new(Harness::denied());
    for (status, body) in [
        stand.post("/ingest", &ingest_body()).await,
        stand.get("/nodes/rest-a").await,
        stand.get("/nodes").await,
    ] {
        assert_eq!(status, StatusCode::NOT_FOUND, "{body}");
    }
}

#[tokio::test]
async fn the_remaining_endpoints_answer_too() {
    let stand = Stand::new(Harness::allowed());
    seed(&stand).await;
    let (status, _) = stand.post("/ingest", &ingest_body()).await;
    assert_eq!(status, StatusCode::OK);

    // The revision surface.
    let (status, revision) = stand.get("/revision").await;
    assert_eq!(status, StatusCode::OK, "{revision}");
    assert!(revision["revision"].as_i64() > Some(0), "{revision}");

    // Delete one edge, leaving both its endpoints.
    let (_, node) = stand.get("/nodes/rest-a").await;
    let edge_key = node["adjacency"][0]["edge_key"]
        .as_str()
        .expect("the edge key")
        .to_owned();
    let (status, deleted) = stand.delete(&format!("/edges/{edge_key}")).await;
    assert_eq!(status, StatusCode::OK, "{deleted}");
    assert_eq!(deleted["tombstoned_edges"], 1, "{deleted}");
    let (status, _) = stand.get("/nodes/rest-b").await;
    assert_eq!(status, StatusCode::OK, "the endpoints outlive the edge");

    // Types, narrowed by kind, and one type by identifier.
    let (status, page) = stand.get("/types?kind=node").await;
    assert_eq!(status, StatusCode::OK, "{page}");
    assert!(
        page["items"]
            .as_array()
            .is_some_and(|rows| rows.iter().all(|row| row["kind"] == "node")),
        "the filter is applied, not ignored: {page}"
    );

    // A kind the server does not know is refused by name, never quietly
    // read as one it does: a catalog filtered by the wrong kind answers
    // confidently with the wrong rows, and the caller cannot tell.
    for unknown in ["nodes", "Node", "relation", ""] {
        let (status, refused) = stand.get(&format!("/types?kind={unknown}")).await;
        assert_eq!(
            status,
            StatusCode::BAD_REQUEST,
            "`{unknown}` is not a type kind: {refused}"
        );
        assert!(
            refused.to_string().contains(unknown) || unknown.is_empty(),
            "the refusal names the value it was given: {refused}"
        );
    }
}

/// Registering a changed schema under a registered identifier: refused by
/// default, admitted with `on_existing: update` when it is backward
/// compatible. The two calls differ only in the option, which is the point.
#[tokio::test]
async fn an_existing_identifier_is_a_conflict_by_default_and_updatable_on_request() {
    let stand = Stand::new(Harness::allowed());
    seed(&stand).await;

    let widened = serde_json::json!({
        "type_id": conformance::OWNED,
        "schema": {
            "$id": format!("gts://{}", conformance::OWNED),
            "$schema": "http://json-schema.org/draft-07/schema#",
            "x-gts-traits": {
                "full_text_search": ["/name"],
                "vector_search": ["/payload/summary"]
            },
            "type": "object",
            "allOf": [
                { "$ref": "gts://gts.cf.core.graph.node.v1~cf.core.graph.owned_node.v1~" },
                { "type": "object", "properties": {
                    "payload": { "type": "object", "properties": {
                        "summary": { "type": "string" }
                    }}
                }}
            ]
        }
    });

    let (status, refused) = stand
        .post("/types", &serde_json::json!({ "types": [widened] }))
        .await;
    assert_eq!(
        status,
        StatusCode::CONFLICT,
        "a different schema under a registered identifier is a conflict: {refused}"
    );

    // `update` alone is not enough either, and for a reason worth pinning:
    // the payload is an open model, so declaring `summary` *narrows* what it
    // accepts (gts section 4.4) and the schemas cannot prove the change safe.
    // What admits it is `revalidate` -- a claim about the stored rows, made
    // by reading them.
    let (status, still_refused) = stand
        .post(
            "/types",
            &serde_json::json!({
                "types": [widened],
                "options": { "on_existing": "update" }
            }),
        )
        .await;
    assert_eq!(
        status,
        StatusCode::CONFLICT,
        "an open model cannot prove this one: {still_refused}"
    );

    let (status, updated) = stand
        .post(
            "/types",
            &serde_json::json!({
                "types": [widened],
                "options": { "on_existing": "update", "revalidate": true }
            }),
        )
        .await;
    assert_eq!(status, StatusCode::OK, "{updated}");
    assert_eq!(updated[0]["outcome"], "updated", "{updated}");
    assert_eq!(
        updated[0]["admission_basis"], "data_backed",
        "the answer says on what ground it was admitted: {updated}"
    );
    assert_eq!(
        updated[0]["rows_validated"], 0,
        "and how many rows that ground rests on: {updated}"
    );
}