ferricel 0.1.4

CLI tool to compile CEL expressions to Wasm
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
//! End-to-end tests for the ferricel CLI
//!
//! These tests focus on the CLI interface and integration between components:
//! - Building Wasm files from CEL expressions
//! - Running Wasm files produced by the build command
//! - Passing bindings via command line arguments and files
//!
//! Note: Unit tests for CEL compilation logic are in src/compiler.rs

use std::{fs, path::PathBuf};

use assert_cmd::Command;
use predicates::prelude::*;
use tempfile::NamedTempFile;

/// Helper function to get a Command for the ferricel binary
#[allow(deprecated)]
fn ferricel() -> Command {
    Command::cargo_bin("ferricel").expect("Failed to find ferricel binary")
}

/// Helper to create a temporary JSON file with given content
fn create_json_file(content: &str) -> NamedTempFile {
    let file = NamedTempFile::new().expect("Failed to create temp file");
    fs::write(file.path(), content).expect("Failed to write to temp file");
    file
}

/// Helper to create a temporary CEL file with given expression
fn create_cel_file(content: &str) -> NamedTempFile {
    let file = NamedTempFile::new().expect("Failed to create temp CEL file");
    fs::write(file.path(), content).expect("Failed to write CEL expression to file");
    file
}

// ============================================================================
// BUILD COMMAND TESTS
// ============================================================================

#[test]
fn test_build_creates_wasm_file() {
    let output_file = NamedTempFile::new().unwrap();
    let output_path = output_file.path();

    ferricel()
        .args(["build", "-e", "5 + 10", "-o"])
        .arg(output_path)
        .assert()
        .success()
        .stdout(predicate::str::contains("Successfully compiled"));

    // Verify the Wasm file was created and has content
    assert!(output_path.exists(), "Wasm file should exist");
    let metadata = fs::metadata(output_path).unwrap();
    assert!(metadata.len() > 0, "Wasm file should not be empty");
}

#[test]
fn test_build_with_default_output() {
    // Build without -o flag should create final_cel_program.wasm in current dir
    // We'll use a temp directory to avoid polluting the workspace
    let temp_dir = tempfile::tempdir().unwrap();

    ferricel()
        .current_dir(temp_dir.path())
        .args(["build", "-e", "10 * 2"])
        .assert()
        .success()
        .stdout(predicate::str::contains("final_cel_program.wasm"));

    let default_output = temp_dir.path().join("final_cel_program.wasm");
    assert!(default_output.exists(), "Default output file should exist");
}

#[test]
fn test_build_missing_expression_flag() {
    ferricel()
        .args(["build"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("required"));
}

#[test]
fn test_build_invalid_cel_expression() {
    let output_file = NamedTempFile::new().unwrap();

    ferricel()
        .args(["build", "-e", "this is not valid CEL $$$ @@@", "-o"])
        .arg(output_file.path())
        .assert()
        .failure()
        .stderr(predicate::str::contains("error").or(predicate::str::contains("Parse")));
}

// ============================================================================
// BUILD COMMAND TESTS - WITH FILE INPUT
// ============================================================================

#[test]
fn test_build_from_file_complex_expression() {
    // Create a CEL file with a more complex expression
    let cel_file = create_cel_file("x > 10 && y < 20");
    let output_file = NamedTempFile::new().unwrap();

    ferricel()
        .args(["build", "--expression-file"])
        .arg(cel_file.path())
        .arg("-o")
        .arg(output_file.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("Successfully compiled"));

    // Verify the Wasm file was created and has content
    let metadata = fs::metadata(output_file.path()).unwrap();
    assert!(metadata.len() > 0, "Wasm file should not be empty");
}

#[test]
fn test_build_from_file_with_custom_output() {
    let cel_file = create_cel_file("5 + 10");
    let output_file = NamedTempFile::new().unwrap();
    let output_path = output_file.path();

    ferricel()
        .args(["build", "--expression-file"])
        .arg(cel_file.path())
        .args(["-o"])
        .arg(output_path)
        .assert()
        .success()
        .stdout(predicate::str::contains(output_path.to_str().unwrap()));

    assert!(output_path.exists(), "Custom output path should be used");
}

#[test]
fn test_build_expression_file_not_found() {
    let output_file = NamedTempFile::new().unwrap();

    ferricel()
        .args(["build", "--expression-file", "nonexistent-file.cel", "-o"])
        .arg(output_file.path())
        .assert()
        .failure()
        .stderr(predicate::str::contains("Failed to read CEL file"));
}

#[test]
fn test_build_expression_file_invalid_content() {
    let cel_file = create_cel_file("this is invalid CEL syntax !@# $$$");
    let output_file = NamedTempFile::new().unwrap();

    ferricel()
        .args(["build", "--expression-file"])
        .arg(cel_file.path())
        .arg("-o")
        .arg(output_file.path())
        .assert()
        .failure()
        .stderr(predicate::str::contains("error").or(predicate::str::contains("Parse")));
}

#[test]
fn test_build_mutual_exclusivity_error() {
    let cel_file = create_cel_file("42");
    let output_file = NamedTempFile::new().unwrap();

    ferricel()
        .args(["build", "-e", "100", "--expression-file"])
        .arg(cel_file.path())
        .arg("-o")
        .arg(output_file.path())
        .assert()
        .failure()
        .stderr(predicate::str::contains("cannot be used with"));
}

#[test]
fn test_build_missing_both_flags() {
    let output_file = NamedTempFile::new().unwrap();

    ferricel()
        .args(["build", "-o"])
        .arg(output_file.path())
        .assert()
        .failure()
        .stderr(predicate::str::contains("required"));
}

// ============================================================================
// RUN COMMAND TESTS - WITHOUT VARIABLES
// ============================================================================

#[test]
fn test_run_simple_wasm_without_variables() {
    // Build a simple Wasm file
    let wasm_file = NamedTempFile::new().unwrap();
    ferricel()
        .args(["build", "-e", "42", "-o"])
        .arg(wasm_file.path())
        .assert()
        .success();

    // Run the Wasm file
    ferricel()
        .args(["run"])
        .arg(wasm_file.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("42"));
}

#[test]
fn test_run_nonexistent_wasm_file() {
    ferricel()
        .args(["run", "/tmp/nonexistent_file_xyz123.wasm"])
        .assert()
        .failure()
        .stderr(predicate::str::contains("not found"));
}

// ============================================================================
// RUN COMMAND TESTS - WITH BINDINGS FROM CLI
// ============================================================================

#[test]
fn test_run_with_bindings_json_inline() {
    // Build Wasm that uses age variable
    let wasm_file = NamedTempFile::new().unwrap();
    ferricel()
        .args(["build", "-e", "age + 10", "-o"])
        .arg(wasm_file.path())
        .assert()
        .success();

    // Run with inline JSON
    ferricel()
        .args(["run"])
        .arg(wasm_file.path())
        .args(["--bindings-json", r#"{"age": 25}"#])
        .assert()
        .success()
        .stdout(predicate::str::contains("35"));
}

#[test]
fn test_run_with_bindings_json_multiple_vars() {
    // Build Wasm that uses multiple variables
    let wasm_file = NamedTempFile::new().unwrap();
    ferricel()
        .args(["build", "-e", "x + y", "-o"])
        .arg(wasm_file.path())
        .assert()
        .success();

    // Run with both variables in bindings
    ferricel()
        .args(["run"])
        .arg(wasm_file.path())
        .args(["--bindings-json", r#"{"x": 100, "y": 200}"#])
        .assert()
        .success()
        .stdout(predicate::str::contains("300"));
}

// ============================================================================
// RUN COMMAND TESTS - WITH BINDINGS FROM FILES
// ============================================================================

#[test]
fn test_run_with_bindings_from_file() {
    // Create bindings JSON file
    let bindings_file = create_json_file(r#"{"age": 42}"#);

    // Build Wasm
    let wasm_file = NamedTempFile::new().unwrap();
    ferricel()
        .args(["build", "-e", "age", "-o"])
        .arg(wasm_file.path())
        .assert()
        .success();

    // Run with bindings file
    ferricel()
        .args(["run"])
        .arg(wasm_file.path())
        .arg("--bindings-file")
        .arg(bindings_file.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("42"));
}

#[test]
fn test_run_with_bindings_from_file_multiple_vars() {
    // Create bindings JSON file with multiple variables
    let bindings_file = create_json_file(r#"{"a": 100, "b": 50}"#);

    // Build Wasm
    let wasm_file = NamedTempFile::new().unwrap();
    ferricel()
        .args(["build", "-e", "a - b", "-o"])
        .arg(wasm_file.path())
        .assert()
        .success();

    // Run with bindings file
    ferricel()
        .args(["run"])
        .arg(wasm_file.path())
        .arg("--bindings-file")
        .arg(bindings_file.path())
        .assert()
        .success()
        .stdout(predicate::str::contains("50"));
}

// ============================================================================
// ERROR HANDLING TESTS
// ============================================================================

#[test]
fn test_run_bindings_json_and_file_are_mutually_exclusive() {
    let wasm_file = NamedTempFile::new().unwrap();
    let bindings_file = create_json_file(r#"{"age": 30}"#);

    ferricel()
        .args(["build", "-e", "42", "-o"])
        .arg(wasm_file.path())
        .assert()
        .success();

    ferricel()
        .args(["run"])
        .arg(wasm_file.path())
        .args(["--bindings-json", r#"{"age": 25}"#])
        .arg("--bindings-file")
        .arg(bindings_file.path())
        .assert()
        .failure()
        .stderr(predicate::str::contains("cannot be used with"));
}

#[test]
fn test_run_missing_bindings_file() {
    let wasm_file = NamedTempFile::new().unwrap();
    ferricel()
        .args(["build", "-e", "42", "-o"])
        .arg(wasm_file.path())
        .assert()
        .success();

    ferricel()
        .args(["run"])
        .arg(wasm_file.path())
        .arg("--bindings-file")
        .arg("/tmp/this_file_does_not_exist_xyz123.json")
        .assert()
        .failure()
        .stderr(predicate::str::contains("not found"));
}

#[test]
fn test_run_invalid_json_in_file() {
    // Create a file with invalid JSON
    let bad_json_file = create_json_file("this is not valid JSON { @@@ }");

    let wasm_file = NamedTempFile::new().unwrap();
    ferricel()
        .args(["build", "-e", "age", "-o"])
        .arg(wasm_file.path())
        .assert()
        .success();

    ferricel()
        .args(["run"])
        .arg(wasm_file.path())
        .arg("--bindings-file")
        .arg(bad_json_file.path())
        .assert()
        .failure();
    // The error might be in stderr or could cause a panic/error
    // Just verify it fails - the specific error message may vary
}

// ============================================================================
// EXTENSION DECLARATION TESTS
// ============================================================================
// Note: parsing and file-loading logic is unit-tested in src/cmd/extensions.rs.
// Only CLI-level concerns (clap mutual exclusivity, full build→run pipeline)
// belong here.

#[test]
fn test_build_extensions_and_extensions_file_are_mutually_exclusive() {
    let ext_file = create_json_file(r#"[]"#);
    let output_file = NamedTempFile::new().unwrap();

    ferricel()
        .args(["build", "-e", "abs(x)", "-o"])
        .arg(output_file.path())
        .args(["--extensions", "abs:global:1"])
        .arg("--extensions-file")
        .arg(ext_file.path())
        .assert()
        .failure()
        .stderr(predicate::str::contains("cannot be used with"));
}

#[test]
fn test_run_with_extension_produces_runtime_error_when_not_implemented() {
    // Build a Wasm that calls an extension, then run it without an implementation.
    // The runtime returns a CEL-level error encoded in the result JSON.
    let wasm_file = NamedTempFile::new().unwrap();
    ferricel()
        .args(["build", "-e", "abs(x)", "-o"])
        .arg(wasm_file.path())
        .args(["--extensions", "abs:global:1"])
        .assert()
        .success();

    ferricel()
        .args(["run"])
        .arg(wasm_file.path())
        .args(["--bindings-json", r#"{"x": -5}"#])
        .assert()
        .success()
        .stdout(predicate::str::contains("Extension not found"));
}

#[test]
fn test_build_no_warning_without_structs_no_proto() {
    // When expression doesn't use structs and no proto is provided,
    // there should be no warnings
    let output_file = NamedTempFile::new().unwrap();

    ferricel()
        .args(["build", "-e", "5 + 10", "-o"])
        .arg(output_file.path())
        .assert()
        .success()
        .stderr(predicate::str::contains("ERRO").not())
        .stderr(predicate::str::contains("schema").not());
}

#[test]
fn test_build_warning_struct_without_proto() {
    // When using a protobuf-looking struct without providing proto descriptor,
    // a warning should be shown
    let output_file = NamedTempFile::new().unwrap();

    ferricel()
        .args([
            "build",
            "-e",
            "google.protobuf.BoolValue{value: true}",
            "-o",
        ])
        .arg(output_file.path())
        .assert()
        .success()
        .stderr(predicate::str::contains("ERRO"))
        .stderr(predicate::str::contains("looks like a protobuf message"))
        .stderr(predicate::str::contains("no schema provided"))
        .stderr(predicate::str::contains("--proto-descriptor"));
}

#[test]
fn test_build_warning_proto_missing_type_definition() {
    // When proto descriptor is provided but doesn't contain the type being used,
    // a warning should be shown
    let output_file = NamedTempFile::new().unwrap();
    let proto_path =
        PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/empty_types.pb");

    ferricel()
        .args(["build", "-e", "test.missing.UnknownType{field: 42}", "-o"])
        .arg(output_file.path())
        .arg("--proto-descriptor")
        .arg(&proto_path)
        .assert()
        .success()
        .stderr(predicate::str::contains("ERRO"))
        .stderr(predicate::str::contains("looks like a protobuf message"))
        .stderr(predicate::str::contains(
            "not defined in the provided schema",
        ));
}

#[test]
fn test_build_no_warning_with_matching_proto() {
    // When struct type is properly defined in the provided proto,
    // no warning should be shown
    let output_file = NamedTempFile::new().unwrap();
    let proto_path =
        PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/simple_types.pb");

    ferricel()
        .args([
            "build",
            "-e",
            "test.fixtures.TestMessage{id: 123, name: 'test'}",
            "-o",
        ])
        .arg(output_file.path())
        .arg("--proto-descriptor")
        .arg(&proto_path)
        .assert()
        .success()
        .stderr(predicate::str::contains("ERRO").not());
}