alef 0.78.0

Opinionated polyglot binding generator for Rust libraries
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
//! Kotlin Android-style e2e codegen tests, split out of `tests.rs`.

use super::super::args::{KotlinArgsContext, build_args_and_setup};
use super::super::test_file::render_test_file_inner;
use crate::core::config::ResolvedCrateConfig;
use crate::e2e::config::ArgMapping;
use crate::e2e::config::E2eConfig;
use crate::e2e::fixture::Fixture;
use std::collections::{BTreeMap, HashMap};

/// Regression: kotlin_android test files that contain streaming fixtures must
/// emit `import kotlinx.coroutines.flow.toList`.  Non-android style files must
/// NOT emit it, because `Flow<T>.toList()` is not in scope on JVM targets.
#[test]
fn kotlin_android_streaming_fixture_emits_flow_to_list_import() {
    use crate::core::config::e2e::CallConfig;
    use crate::core::config::extras::{AdapterConfig, AdapterParam, AdapterPattern};
    use crate::e2e::fixture::MockResponse;

    // A fixture with a streaming mock response triggers is_streaming_mock().
    let streaming_fixture = Fixture {
        docs: None,
        requirements: Vec::new(),
        id: "smoke_stream".to_string(),
        category: None,
        description: "streaming test".to_string(),
        tags: vec![],
        skip: None,
        env: None,
        setup: Vec::new(),
        call: None,
        input: serde_json::json!({}),
        mock_response: Some(MockResponse {
            status: 200,
            body: None,
            stream_chunks: Some(vec![serde_json::json!({"delta": "hi"})]),
            headers: BTreeMap::new(),
        }),
        visitor: None,
        args: vec![],
        assertion_recipes: vec![],
        assertions: vec![],
        source: String::new(),
        http: None,
        asyncapi: None,
        websocket: None,
        preserve_input_urls: false,
    };

    let e2e_config = E2eConfig {
        call: CallConfig {
            function: "stream_items".to_string(),
            ..CallConfig::default()
        },
        ..E2eConfig::default()
    };
    // kotlin_android_style=true must emit the import.
    let config = crate::core::config::ResolvedCrateConfig {
        adapters: vec![AdapterConfig {
            name: "stream_items".to_string(),
            pattern: AdapterPattern::Streaming,
            core_path: "sample::Engine::stream_items".to_string(),
            params: vec![AdapterParam {
                name: "request".to_string(),
                ty: "sample::StreamRequest".to_string(),
                optional: false,
            }],
            returns: None,
            error_type: None,
            owner_type: Some("Engine".to_string()),
            item_type: Some("StreamItem".to_string()),
            gil_release: false,
            trait_name: None,
            trait_method: None,
            detect_async: false,
            request_type: None,
            skip_languages: Vec::new(),
        }],
        ..crate::core::config::ResolvedCrateConfig::default()
    };
    let type_defs: Vec<crate::core::ir::TypeDef> = Vec::new();
    let out_android = render_test_file_inner(
        "streaming",
        &[&streaming_fixture],
        "LlmClient",
        "chatStream",
        "dev.sample_crate.sampleapp.android",
        "result",
        &[],
        None,
        false,
        &e2e_config,
        &HashMap::new(),
        true,
        &config,
        &type_defs,
        &[],
        &[],
    )
    .expect("android streaming test file renders");
    assert!(
        out_android.contains("import kotlinx.coroutines.flow.toList"),
        "kotlin_android streaming file must import flow.toList, got:\n{out_android}"
    );
    assert!(
        out_android.contains("import dev.sample_crate.sampleapp.android.StreamRequest"),
        "streaming request DTO must be imported, got:\n{out_android}"
    );

    // kotlin_android_style=false must NOT emit the import.
    let out_jvm = render_test_file_inner(
        "streaming",
        &[&streaming_fixture],
        "LlmClient",
        "chatStream",
        "dev.sample_crate.sampleapp.android",
        "result",
        &[],
        None,
        false,
        &e2e_config,
        &HashMap::new(),
        false,
        &config,
        &type_defs,
        &[],
        &[],
    )
    .expect("jvm streaming test file renders");
    assert!(
        !out_jvm.contains("import kotlinx.coroutines.flow.toList"),
        "non-android streaming file must NOT import flow.toList, got:\n{out_jvm}"
    );
}

/// Regression: kotlin_android test files that instantiate an ObjectMapper must
/// emit `import com.fasterxml.jackson.module.kotlin.registerKotlinModule` and
/// call `.registerKotlinModule()` on the mapper.  Non-android files use plain
/// Java records/builders and must NOT emit either.
#[test]
fn kotlin_android_object_mapper_emits_register_kotlin_module() {
    use crate::core::config::e2e::CallConfig;
    use crate::e2e::fixture::{HttpExpectedResponse, HttpFixture, HttpHandler, HttpRequest};

    // An HTTP fixture forces `needs_object_mapper = true` regardless of args.
    let http_fixture = Fixture {
        docs: None,
        requirements: Vec::new(),
        id: "http_test".to_string(),
        category: None,
        description: "http test".to_string(),
        tags: vec![],
        skip: None,
        env: None,
        setup: Vec::new(),
        call: None,
        input: serde_json::json!({}),
        mock_response: None,
        visitor: None,
        args: vec![],
        assertion_recipes: vec![],
        assertions: vec![],
        source: String::new(),
        http: Some(HttpFixture {
            handler: HttpHandler {
                route: "/v1/test".to_string(),
                method: "POST".to_string(),
                body_schema: None,
                parameters: BTreeMap::new(),
                middleware: None,
            },
            request: HttpRequest {
                method: "POST".to_string(),
                path: "/v1/test".to_string(),
                headers: BTreeMap::new(),
                query_params: BTreeMap::new(),
                cookies: BTreeMap::new(),
                body: None,
                form_data: None,
                content_type: None,
            },
            expected_response: HttpExpectedResponse {
                status_code: 200,
                body: None,
                body_partial: None,
                headers: BTreeMap::new(),
                validation_errors: None,
            },
        }),
        asyncapi: None,
        websocket: None,
        preserve_input_urls: false,
    };

    let e2e_config = E2eConfig {
        call: CallConfig::default(),
        ..E2eConfig::default()
    };
    // kotlin_android_style=true must emit registerKotlinModule import and call.
    let config = crate::core::config::ResolvedCrateConfig::default();
    let type_defs: Vec<crate::core::ir::TypeDef> = Vec::new();
    let out_android = render_test_file_inner(
        "configuration",
        &[&http_fixture],
        "",
        "",
        "dev.sample_crate.sampleapp.android",
        "result",
        &[],
        None,
        false,
        &e2e_config,
        &HashMap::new(),
        true,
        &config,
        &type_defs,
        &[],
        &[],
    )
    .expect("android configuration test file renders");
    assert!(
        out_android.contains("import com.fasterxml.jackson.module.kotlin.registerKotlinModule"),
        "kotlin_android with ObjectMapper must import registerKotlinModule, got:\n{out_android}"
    );
    assert!(
        out_android.contains(".registerKotlinModule()"),
        "kotlin_android MAPPER must call .registerKotlinModule(), got:\n{out_android}"
    );

    // kotlin_android_style=false must NOT emit registerKotlinModule.
    let out_jvm = render_test_file_inner(
        "configuration",
        &[&http_fixture],
        "",
        "",
        "dev.sample_crate.sampleapp.android",
        "result",
        &[],
        None,
        false,
        &e2e_config,
        &HashMap::new(),
        false,
        &config,
        &type_defs,
        &[],
        &[],
    )
    .expect("jvm configuration test file renders");
    assert!(
        !out_jvm.contains("registerKotlinModule"),
        "non-android MAPPER must NOT reference registerKotlinModule, got:\n{out_jvm}"
    );
}

/// Regression: kotlin_android bytes args must be coerced to ByteArray by reading
/// the file path, not passed as plain String literals.
#[test]
fn kotlin_android_bytes_arg_emits_files_read_all_bytes() {
    let args = vec![ArgMapping {
        name: "content".to_string(),
        field: "input.path".to_string(),
        arg_type: "bytes".to_string(),
        optional: false,
        owned: false,
        element_type: None,
        go_type: None,
        vec_inner_is_ref: false,
        trait_name: None,
    }];
    let fixture = Fixture {
        docs: None,
        requirements: Vec::new(),
        id: "extract_bytes_fixture".to_string(),
        category: None,
        description: "test bytes extraction".to_string(),
        tags: vec![],
        skip: None,
        env: None,
        setup: Vec::new(),
        call: None,
        input: serde_json::json!({ "path": "pdf/test.pdf" }),
        mock_response: None,
        visitor: None,
        args: vec![],
        assertion_recipes: vec![],
        assertions: vec![],
        source: String::new(),
        http: None,
        asyncapi: None,
        websocket: None,
        preserve_input_urls: false,
    };

    // JVM style: should emit plain string
    let (_, args_jvm) = build_args_and_setup(
        &fixture.input,
        &args,
        KotlinArgsContext {
            fixture: &fixture,
            class_name: "SampleBinding",
            options_type: None,
            fixture_id: "extract_bytes_fixture",
            kotlin_android_style: false,
            config: &ResolvedCrateConfig::default(),
            type_defs: &[],
            owner_handle_is_receiver: false,
            enums: &[],
            target_params: crate::e2e::codegen::call_ir::TargetParams::IrAbsent,
        },
    )
    .expect("args build succeeds");
    assert!(
        args_jvm.contains("\"pdf/test.pdf\""),
        "JVM style must emit string literal, got: {args_jvm}"
    );

    // Android style: should emit Files.readAllBytes(Paths.get(...))
    let (_, args_android) = build_args_and_setup(
        &fixture.input,
        &args,
        KotlinArgsContext {
            fixture: &fixture,
            class_name: "SampleBinding",
            options_type: None,
            fixture_id: "extract_bytes_fixture",
            kotlin_android_style: true,
            config: &ResolvedCrateConfig::default(),
            type_defs: &[],
            owner_handle_is_receiver: false,
            enums: &[],
            target_params: crate::e2e::codegen::call_ir::TargetParams::IrAbsent,
        },
    )
    .expect("args build succeeds");
    assert!(
        args_android.contains("java.nio.file.Files.readAllBytes"),
        "kotlin_android bytes arg must use Files.readAllBytes, got: {args_android}"
    );
    assert!(
        args_android.contains("Paths.get("),
        "kotlin_android bytes arg must use Paths.get, got: {args_android}"
    );
}

/// Regression: kotlin_android batch bytes args must wrap each path string in
/// the configured item type with file contents as ByteArray.
#[test]
fn kotlin_android_batch_bytes_item_wraps_paths() {
    let args = vec![ArgMapping {
        name: "items".to_string(),
        field: "input.paths".to_string(),
        arg_type: "json_object".to_string(),
        optional: false,
        owned: false,
        element_type: Some("FileBytesItem".to_string()),
        go_type: None,
        vec_inner_is_ref: false,
        trait_name: None,
    }];
    let fixture = Fixture {
        docs: None,
        requirements: Vec::new(),
        id: "batch_extract_fixture".to_string(),
        category: None,
        description: "test batch extraction".to_string(),
        tags: vec![],
        skip: None,
        env: None,
        setup: Vec::new(),
        call: None,
        input: serde_json::json!({ "paths": ["pdf/test1.pdf", "pdf/test2.pdf"] }),
        mock_response: None,
        visitor: None,
        args: vec![],
        assertion_recipes: vec![],
        assertions: vec![],
        source: String::new(),
        http: None,
        asyncapi: None,
        websocket: None,
        preserve_input_urls: false,
    };

    let (_, args_android) = build_args_and_setup(
        &fixture.input,
        &args,
        KotlinArgsContext {
            fixture: &fixture,
            class_name: "SampleBinding",
            options_type: None,
            fixture_id: "batch_extract_fixture",
            kotlin_android_style: true,
            config: &ResolvedCrateConfig::default(),
            type_defs: &[],
            owner_handle_is_receiver: false,
            enums: &[],
            target_params: crate::e2e::codegen::call_ir::TargetParams::IrAbsent,
        },
    )
    .expect("args build succeeds");
    assert!(
        args_android.contains("FileBytesItem"),
        "kotlin_android batch must wrap items in the configured item type, got: {args_android}"
    );
    assert!(
        args_android.contains("java.nio.file.Files.readAllBytes"),
        "kotlin_android batch items must read file bytes, got: {args_android}"
    );
    assert!(
        args_android.contains("listOf("),
        "kotlin_android batch must emit listOf(...), got: {args_android}"
    );
}

/// Regression: emitted `System.loadLibrary(...)` must use the resolved
/// `jni_lib_name()` (`{ffi_prefix}_jni`) so it stays in sync with the cdylib
/// emitted by the generated JNI `Cargo.toml` `[lib] name`. Hard-coding
/// `{crate_name}_jni` breaks for crates that override `[crates.ffi] prefix`
/// (for example: name `custom-runtime-crate`, prefix `custom_runtime`, cdylib
/// `custom_runtime_jni`).
#[test]
fn kotlin_android_test_file_loads_resolved_jni_lib_name_not_crate_name() {
    use crate::core::config::e2e::CallConfig;
    use crate::core::config::{FfiConfig, ResolvedCrateConfig};
    use crate::e2e::fixture::MockResponse;

    let fixture = Fixture {
        docs: None,
        requirements: Vec::new(),
        id: "smoke_one".to_string(),
        category: None,
        description: "smoke".to_string(),
        tags: vec![],
        skip: None,
        env: None,
        setup: Vec::new(),
        call: None,
        input: serde_json::json!({}),
        mock_response: Some(MockResponse {
            status: 200,
            body: None,
            stream_chunks: None,
            headers: BTreeMap::new(),
        }),
        visitor: None,
        args: vec![],
        assertion_recipes: vec![],
        assertions: vec![],
        source: String::new(),
        http: None,
        asyncapi: None,
        websocket: None,
        preserve_input_urls: false,
    };
    let e2e_config = E2eConfig {
        call: CallConfig::default(),
        ..E2eConfig::default()
    };
    let mut config = ResolvedCrateConfig {
        name: "custom-runtime-crate".to_string(),
        ..ResolvedCrateConfig::default()
    };
    config.ffi = Some(FfiConfig {
        prefix: Some("custom_runtime".to_string()),
        error_style: "last_error".to_string(),
        header_name: None,
        lib_name: None,
        visitor_callbacks: false,
        features: None,
        extra_features: Vec::new(),
        serde_rename_all: None,
        exclude_functions: Vec::new(),
        exclude_types: Vec::new(),
        capsule_types: HashMap::new(),
        rename_fields: HashMap::new(),
        plugin_error_constructor: None,
        target_dep_overrides: Vec::new(),
        excluded_default_features: Vec::new(),
    });
    let type_defs: Vec<crate::core::ir::TypeDef> = Vec::new();
    let out = render_test_file_inner(
        "smoke",
        &[&fixture],
        "Bridge",
        "doThing",
        "dev.sample_crate.sampleapp.android",
        "result",
        &[],
        None,
        false,
        &e2e_config,
        &HashMap::new(),
        true,
        &config,
        &type_defs,
        &[],
        &[],
    )
    .expect("kotlin_android test file renders");
    assert!(
        out.contains("System.loadLibrary(\"custom_runtime_jni\")"),
        "kotlin_android test must loadLibrary the resolved jni_lib_name (`custom_runtime_jni`), got:\n{out}"
    );
    assert!(
        !out.contains("custom-runtime-crate_jni"),
        "kotlin_android test must NOT loadLibrary the raw crate name, got:\n{out}"
    );
}