cf-gears-toolkit 0.10.0

Core ToolKit library
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
551
552
#[cfg(test)]
#[cfg_attr(coverage_nightly, coverage(off))]
mod tests {
    use crate::api::odata::*;
    use axum::extract::FromRequestParts;
    use axum::http::Request;

    #[test]
    fn test_parse_orderby_simple() {
        let result = parse_orderby("created_at desc").unwrap();
        assert_eq!(result.0.len(), 1);
        assert_eq!(result.0[0].field, "created_at");
        assert_eq!(result.0[0].dir, SortDir::Desc);
    }

    #[test]
    fn test_parse_orderby_multiple_fields() {
        let result = parse_orderby("created_at desc, id asc, name").unwrap();
        assert_eq!(result.0.len(), 3);

        assert_eq!(result.0[0].field, "created_at");
        assert_eq!(result.0[0].dir, SortDir::Desc);

        assert_eq!(result.0[1].field, "id");
        assert_eq!(result.0[1].dir, SortDir::Asc);

        assert_eq!(result.0[2].field, "name");
        assert_eq!(result.0[2].dir, SortDir::Asc); // default
    }

    #[test]
    fn test_parse_orderby_whitespace_tolerance() {
        let result = parse_orderby("  created_at   desc  ,   id   asc  ").unwrap();
        assert_eq!(result.0.len(), 2);
        assert_eq!(result.0[0].field, "created_at");
        assert_eq!(result.0[0].dir, SortDir::Desc);
        assert_eq!(result.0[1].field, "id");
        assert_eq!(result.0[1].dir, SortDir::Asc);
    }

    #[test]
    fn test_parse_orderby_empty() {
        let result = parse_orderby("").unwrap();
        assert!(result.is_empty());

        let result = parse_orderby("   ").unwrap();
        assert!(result.is_empty());
    }

    #[test]
    fn test_parse_orderby_too_long() {
        let long_orderby = "a".repeat(MAX_ORDERBY_LEN + 1);
        let result = parse_orderby(&long_orderby);
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            toolkit_odata::Error::InvalidOrderByField(_)
        ));
    }

    #[test]
    fn test_parse_orderby_too_many_fields() {
        let many_fields: Vec<String> = (0..=MAX_ORDER_FIELDS)
            .map(|i| format!("field{i}"))
            .collect();
        let orderby = many_fields.join(", ");

        let result = parse_orderby(&orderby);
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            toolkit_odata::Error::InvalidOrderByField(_)
        ));
    }

    #[test]
    fn test_parse_orderby_invalid_clause() {
        let result = parse_orderby("field invalid_direction");
        assert!(result.is_err());
        assert!(matches!(
            result.unwrap_err(),
            toolkit_odata::Error::InvalidOrderByField(_)
        ));
    }

    #[test]
    fn test_parse_orderby_empty_field() {
        let result = parse_orderby(", asc");
        // The new implementation is more lenient and skips empty segments
        // so this now succeeds with one field "asc"
        assert!(result.is_ok());
        let order = result.unwrap();
        assert_eq!(order.0.len(), 1);
        assert_eq!(order.0[0].field, "asc");
    }

    #[tokio::test]
    async fn test_extract_odata_query_full() {
        let uri = "/?%24filter=email%20eq%20%27test%40example.com%27&%24orderby=created_at%20desc&limit=25&cursor=eyJ2IjoxLCJrIjpbInRlc3QiXSwicyI6Ii1jcmVhdGVkX2F0Iiwib28oImFzYyJ9";

        let request = Request::builder().uri(uri).body(()).unwrap();

        let (mut parts, _body) = request.into_parts();

        let result = extract_odata_query(&mut parts, &()).await;
        assert!(result.is_err());
        let canonical = result.unwrap_err();
        // Canonical `InvalidArgument` maps to 400; replaces the legacy 422.
        assert_eq!(canonical.status_code(), 400);
        assert!(canonical.gts_type().contains("invalid_argument"));

        // Should have cursor (even if decode fails, it should try)
        // Note: The cursor in the test is not a valid base64url, but that's OK for this test
    }

    #[tokio::test]
    async fn test_extract_odata_query_filter_only() {
        let uri = "/?%24filter=email%20eq%20%27test%40example.com%27";

        let request = Request::builder().uri(uri).body(()).unwrap();

        let (mut parts, _body) = request.into_parts();

        let query = extract_odata_query(&mut parts, &()).await.unwrap();

        assert!(query.filter.is_some());
        assert!(query.order.is_empty());
        assert_eq!(query.limit, None);
        assert!(query.cursor.is_none());
    }

    #[tokio::test]
    async fn test_extract_odata_query_orderby_only() {
        let uri = "/?%24orderby=created_at%20desc%2C%20id%20asc";

        let request = Request::builder().uri(uri).body(()).unwrap();

        let (mut parts, _body) = request.into_parts();

        let query = extract_odata_query(&mut parts, &()).await.unwrap();

        assert!(query.filter.is_none());
        assert_eq!(query.order.0.len(), 2);
        assert_eq!(query.limit, None);
        assert!(query.cursor.is_none());
    }

    #[tokio::test]
    async fn test_extract_odata_query_empty() {
        let uri = "/";

        let request = Request::builder().uri(uri).body(()).unwrap();

        let (mut parts, _body) = request.into_parts();

        let query = extract_odata_query(&mut parts, &()).await.unwrap();

        assert!(query.filter.is_none());
        assert!(query.order.is_empty());
        assert_eq!(query.limit, None);
        assert!(query.cursor.is_none());
    }

    #[tokio::test]
    async fn test_extract_odata_query_limit_zero_error() {
        let uri = "/?limit=0";

        let request = Request::builder().uri(uri).body(()).unwrap();

        let (mut parts, _body) = request.into_parts();

        let result = extract_odata_query(&mut parts, &()).await;
        assert!(result.is_err());
        let _problem_response = result.unwrap_err();
    }

    #[tokio::test]
    async fn test_extract_odata_query_filter_too_long() {
        let long_filter = "email eq '".to_owned() + &"a".repeat(MAX_FILTER_LEN) + "'";
        let uri = format!("/?%24filter={}", urlencoding::encode(&long_filter));

        let request = Request::builder().uri(uri).body(()).unwrap();

        let (mut parts, _body) = request.into_parts();

        let result = extract_odata_query(&mut parts, &()).await;
        assert!(result.is_err());
        let _problem_response = result.unwrap_err();
    }

    #[tokio::test]
    async fn test_extract_odata_query_invalid_filter() {
        let uri = "/?%24filter=invalid%20syntax%20here";

        let request = Request::builder().uri(uri).body(()).unwrap();

        let (mut parts, _body) = request.into_parts();

        let result = extract_odata_query(&mut parts, &()).await;
        assert!(result.is_err());
        let _problem_response = result.unwrap_err();
    }

    #[tokio::test]
    async fn test_extract_odata_query_invalid_orderby() {
        let uri = "/?%24orderby=field%20invalid_direction";

        let request = Request::builder().uri(uri).body(()).unwrap();

        let (mut parts, _body) = request.into_parts();

        let result = extract_odata_query(&mut parts, &()).await;
        assert!(result.is_err());
        let _problem_response = result.unwrap_err();
    }

    #[tokio::test]
    async fn test_extract_odata_query_invalid_cursor() {
        let uri = "/?cursor=invalid_cursor";

        let request = Request::builder().uri(uri).body(()).unwrap();

        let (mut parts, _body) = request.into_parts();

        let result = extract_odata_query(&mut parts, &()).await;
        assert!(result.is_err());
        let _problem_response = result.unwrap_err();
    }

    #[tokio::test]
    async fn test_odata_extractor() {
        let uri = "/?%24filter=email%20eq%20%27test%40example.com%27&limit=10";

        let request = Request::builder().uri(uri).body(()).unwrap();

        let (mut parts, _body) = request.into_parts();

        let odata = OData::from_request_parts(&mut parts, &()).await.unwrap();

        assert!(odata.filter.is_some());
        assert_eq!(odata.limit, Some(10));
    }

    // ─── `$top` ⇄ `limit` alias ──────────────────────────────────────
    //
    // `$top` is the canonical OData page-size spelling (OASIS OData 4.01
    // Part 2: URL Conventions, §5.1.6). It is a serde alias of `limit`,
    // so both spellings fold onto the same `ODataQuery.limit` slot.

    #[tokio::test]
    async fn test_extract_odata_query_top_binds_limit() {
        let uri = "/?%24top=25";

        let request = Request::builder().uri(uri).body(()).unwrap();

        let (mut parts, _body) = request.into_parts();

        let query = extract_odata_query(&mut parts, &()).await.unwrap();

        assert_eq!(
            query.limit,
            Some(25),
            "`$top` must bind the same slot as `limit`"
        );
    }

    #[tokio::test]
    async fn test_extract_odata_query_top_zero_error() {
        // Same floor as `limit=0`: zero is not a usable page size.
        let uri = "/?%24top=0";

        let request = Request::builder().uri(uri).body(()).unwrap();

        let (mut parts, _body) = request.into_parts();

        let canonical = extract_odata_query(&mut parts, &())
            .await
            .expect_err("`$top=0` must be rejected");
        assert_eq!(canonical.status_code(), 400);

        // The violation names `$top` — a caller who sent `$top` must not be
        // told about a parameter they did not use.
        let problem = toolkit_canonical_errors::Problem::from(canonical);
        let violations = problem
            .context
            .get("field_violations")
            .and_then(|v| v.as_array())
            .expect("invalid_argument must carry field_violations[]");
        assert_eq!(
            violations[0].get("field").and_then(|f| f.as_str()),
            Some("$top"),
            "violation was {:?}",
            violations[0]
        );
    }

    #[tokio::test]
    async fn test_extract_odata_query_top_and_limit_conflict() {
        // Two spellings of one slot in one request is ambiguous: reject it
        // rather than silently picking a winner.
        let uri = "/?%24top=25&limit=10";

        let request = Request::builder().uri(uri).body(()).unwrap();

        let (mut parts, _body) = request.into_parts();

        let canonical = extract_odata_query(&mut parts, &())
            .await
            .expect_err("`$top` together with `limit` must be rejected");
        assert_eq!(canonical.status_code(), 400);

        // Surfaces through the axum-level deserialization path, so the
        // violation is keyed to `query` (gear OpenAPI documents cite this
        // reason for the two-spellings-in-one-request case).
        let problem = toolkit_canonical_errors::Problem::from(canonical);
        let violations = problem
            .context
            .get("field_violations")
            .and_then(|v| v.as_array())
            .expect("invalid_argument must carry field_violations[]");
        assert_eq!(
            violations[0].get("reason").and_then(|r| r.as_str()),
            Some("INVALID_QUERY_PARAMS"),
            "violation was {:?}",
            violations[0]
        );
    }

    #[tokio::test]
    async fn test_odata_extractor_top_alias() {
        let uri = "/?%24filter=email%20eq%20%27test%40example.com%27&%24top=10";

        let request = Request::builder().uri(uri).body(()).unwrap();

        let (mut parts, _body) = request.into_parts();

        let odata = OData::from_request_parts(&mut parts, &()).await.unwrap();

        assert!(odata.filter.is_some());
        assert_eq!(odata.limit, Some(10));
    }

    // ─── unsupported system query options ────────────────────────────
    //
    // OASIS OData 4.01 Part 1: Protocol, §6.1 "Query Option
    // Extensibility": a service "MUST fail any request that contains
    // unsupported OData query options defined in the version of this
    // specification supported by the service", and SHOULD fail options it
    // does not understand. Accepting-and-dropping `$skip` left a caller
    // re-reading page one while believing the offset advanced.

    /// Lift `field_violations[]` off a canonical error as
    /// `(field, reason, description)` triples in wire order.
    fn violations_of(canonical: CanonicalError) -> Vec<(String, String, String)> {
        let problem = toolkit_canonical_errors::Problem::from(canonical);
        problem
            .context
            .get("field_violations")
            .and_then(|v| v.as_array())
            .expect("invalid_argument must carry field_violations[]")
            .iter()
            .map(|v| {
                let get = |key: &str| {
                    v.get(key)
                        .and_then(|s| s.as_str())
                        .unwrap_or_default()
                        .to_owned()
                };
                (get("field"), get("reason"), get("description"))
            })
            .collect()
    }

    /// Run the extractor over a bare query string.
    async fn extract(query: &str) -> Result<ODataQuery, CanonicalError> {
        let request = Request::builder()
            .uri(format!("/?{query}"))
            .body(())
            .unwrap();
        let (mut parts, _body) = request.into_parts();
        extract_odata_query(&mut parts, &()).await
    }

    #[tokio::test]
    async fn test_extract_odata_query_skip_rejected() {
        // Offset paging is not the model: the platform pages by cursor, and
        // honoring `$top` while dropping `$skip` would serve a page of the
        // requested size at a fixed offset forever.
        let canonical = extract("%24skip=20")
            .await
            .expect_err("`$skip` must be rejected, not dropped");
        assert_eq!(canonical.status_code(), 400);

        let violations = violations_of(canonical);
        assert_eq!(violations.len(), 1, "violations were {violations:?}");
        let (field, reason, description) = &violations[0];
        assert_eq!(field, "$skip");
        assert_eq!(reason, "UNSUPPORTED_QUERY_PARAM");
        assert!(
            description.contains("$skiptoken"),
            "detail must name the cursor replacement: {description}"
        );
    }

    #[tokio::test]
    async fn test_extract_odata_query_count_rejected() {
        // `PageInfo` carries no total, so `$count` has no truthful answer.
        let canonical = extract("%24count=true")
            .await
            .expect_err("`$count` must be rejected, not dropped");
        assert_eq!(canonical.status_code(), 400);

        let violations = violations_of(canonical);
        assert_eq!(violations.len(), 1, "violations were {violations:?}");
        let (field, reason, description) = &violations[0];
        assert_eq!(field, "$count");
        assert_eq!(reason, "UNSUPPORTED_QUERY_PARAM");
        assert!(
            description.contains("total"),
            "detail must say totals are not part of the page contract: {description}"
        );
    }

    #[tokio::test]
    async fn test_extract_odata_query_misspelled_option_rejected() {
        // The §6.1 SHOULD case: an option the service does not understand.
        // A dropped `$filtre` returns the whole unfiltered collection with
        // a 200.
        let canonical = extract("%24filtre=email%20eq%20%27a%40b.c%27")
            .await
            .expect_err("`$filtre` must be rejected, not dropped");
        assert_eq!(canonical.status_code(), 400);

        let violations = violations_of(canonical);
        assert_eq!(violations.len(), 1, "violations were {violations:?}");
        let (field, _, description) = &violations[0];
        assert_eq!(field, "$filtre");
        // `$filtre` is not an OData-defined option, so it must not be
        // described as one: it is unknown, not unimplemented.
        assert!(
            description.contains("unknown"),
            "a typo must read as unknown, not as unsupported: {description}"
        );
    }

    #[tokio::test]
    async fn test_extract_odata_query_reports_every_unsupported_option() {
        // One round trip should name every offending option, in wire order,
        // rather than making the caller fix them one request at a time.
        let canonical = extract("%24skip=20&%24count=true&%24expand=tenant")
            .await
            .expect_err("unsupported options must be rejected");

        let fields: Vec<String> = violations_of(canonical)
            .into_iter()
            .map(|(field, _, _)| field)
            .collect();
        assert_eq!(fields, ["$skip", "$count", "$expand"]);
    }

    #[tokio::test]
    async fn test_extract_odata_query_repeated_unsupported_option_reported_once() {
        // One violation per offending key, not per occurrence: a repeated
        // option is one mistake to fix.
        let canonical = extract("%24skip=20&%24skip=40")
            .await
            .expect_err("`$skip` must be rejected");

        let fields: Vec<String> = violations_of(canonical)
            .into_iter()
            .map(|(field, _, _)| field)
            .collect();
        assert_eq!(fields, ["$skip"]);
    }

    #[tokio::test]
    async fn test_extract_odata_query_miscased_option_rejected() {
        // `ODataParams` binds `$top` case-sensitively, so `$Top` would
        // otherwise be dropped. Reject it and name the spelling that binds.
        // (OData 4.01 Part 2 §5.1 asks a 4.01 service to accept
        // case-insensitive option names; this platform does not, and says
        // so instead of ignoring the request.)
        let canonical = extract("%24Top=10")
            .await
            .expect_err("`$Top` must be rejected, not dropped");
        assert_eq!(canonical.status_code(), 400);

        let violations = violations_of(canonical);
        assert_eq!(violations.len(), 1, "violations were {violations:?}");
        let (field, _, description) = &violations[0];
        assert_eq!(field, "$Top");
        assert!(
            description.contains("$top"),
            "detail must name the spelling that binds: {description}"
        );
    }

    #[tokio::test]
    async fn test_extract_odata_query_skiptoken_binds_cursor() {
        // `$skiptoken` is the canonical OData spelling of what this
        // platform calls `cursor` — alias, same slot, same as `$top`.
        let token = CursorV1 {
            k: vec!["2026-08-10T00:00:00Z".to_owned()],
            o: SortDir::Asc,
            s: "created_at".to_owned(),
            f: None,
            d: "fwd".to_owned(),
        }
        .encode()
        .expect("cursor must encode");

        let query = extract(&format!("%24skiptoken={token}"))
            .await
            .expect("`$skiptoken` must be accepted");
        assert!(
            query.cursor.is_some(),
            "`$skiptoken` must bind the same slot as `cursor`"
        );
    }

    #[tokio::test]
    async fn test_extract_odata_query_non_odata_params_pass_through() {
        // Part 1 §6.1 reserves the `$` prefix for OData system query
        // options, so unprefixed keys belong to the handler's own params
        // struct — chat-engine's `q` / `context`, file-storage's `offset`.
        // The extractor must not police that namespace.
        let query = extract("q=needle&context=2&limit=5")
            .await
            .expect("non-OData params belong to the handler, not this extractor");
        assert_eq!(query.limit, Some(5));
    }

    #[test]
    fn test_odata_deref() {
        use toolkit_odata::ast::*;

        let expr = Expr::Identifier("test".to_owned());
        let query = ODataQuery::default().with_filter(expr);
        let odata = OData(query);

        // Test Deref
        assert!(odata.has_filter());

        // Test AsRef
        let query_ref: &ODataQuery = odata.as_ref();
        assert!(query_ref.has_filter());

        // Test Into
        let query_back: ODataQuery = odata.into();
        assert!(query_back.has_filter());
    }
}