mds-cli 0.4.0

MDS (Markdown Script) compiler — command-line interface
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
mod common;
use common::{fixture, mds_bin};
use std::collections::HashMap;

#[test]
fn undefined_variable_error() {
    let result = mds::compile(fixture("undefined_var.mds"), None);
    assert!(result.is_err());
    let err = format!("{}", result.unwrap_err());
    assert!(err.contains("username"));
}

#[test]
fn arity_mismatch_error() {
    let result = mds::compile(fixture("arity_error.mds"), None);
    assert!(result.is_err());
    let err = format!("{}", result.unwrap_err());
    assert!(err.contains("arity") || err.contains("expected 1"));
}

#[test]
fn file_not_found_error() {
    let result = mds::compile(std::path::PathBuf::from("nonexistent.mds"), None);
    assert!(result.is_err());
    let err = format!("{}", result.unwrap_err());
    assert!(
        err.contains("not found") || err.contains("nonexistent"),
        "expected file not found error, got: {err}"
    );
}

#[test]
fn not_mds_file_error() {
    // Try to compile a .md file without type: mds
    let path = fixture("not_mds.md");
    let result = mds::compile(&path, None);
    assert!(result.is_err());
    let err = format!("{}", result.unwrap_err());
    assert!(
        err.contains("not an MDS file"),
        "expected 'not an MDS file' in error, got: {err}"
    );
}

#[test]
fn undefined_function_error() {
    let source = "{{nonexistent(\"arg\")}}\n";
    let result = mds::compile_str_with(source, None, None);
    assert!(result.is_err());
    let err = format!("{}", result.unwrap_err());
    assert!(
        err.contains("undefined function") || err.contains("nonexistent"),
        "expected undefined function error, got: {err}"
    );
}

#[test]
fn undefined_namespace_in_qualified_call() {
    let source = "{{missing_ns.greet(\"Alice\")}}\n";
    let result = mds::compile_str_with(source, None, None);
    assert!(result.is_err());
    let err = format!("{}", result.unwrap_err());
    assert!(
        err.contains("undefined") || err.contains("missing_ns"),
        "expected undefined namespace error, got: {err}"
    );
}

#[test]
fn undefined_function_error_message_says_function() {
    let source = "{{nonexistent_fn(\"arg\")}}\n";
    let result = mds::compile_str_with(source, None, None);
    assert!(result.is_err());
    let err = format!("{}", result.unwrap_err());
    assert!(
        err.contains("undefined function") || err.contains("nonexistent_fn"),
        "expected 'undefined function' error, not 'undefined variable', got: {err}"
    );
    assert!(
        !err.contains("undefined variable"),
        "error should say 'function', not 'variable', got: {err}"
    );
}

#[test]
fn for_body_undefined_var_errors_at_validate_time() {
    let result = mds::compile(fixture("for_body_undef.mds"), None);
    assert!(
        result.is_err(),
        "expected error for undefined var in @for body"
    );
    let err = format!("{}", result.unwrap_err());
    assert!(
        err.contains("undefined") || err.contains("undefined_var_in_body"),
        "expected undefined variable error in for body, got: {err}"
    );
}

#[test]
fn for_iterate_non_array_error() {
    // Attempting to iterate over a non-array should produce a type error
    let mut vars = HashMap::new();
    vars.insert(
        "items".to_string(),
        mds::Value::String("not_an_array".to_string()),
    );
    let result = mds::compile(fixture("loop.mds"), Some(vars));
    assert!(result.is_err());
    let err = format!("{}", result.unwrap_err());
    assert!(err.contains("expected array") || err.contains("type error") || err.contains("string"));
}

#[test]
fn invalid_identifier_in_for_var() {
    // @for x-y in items: — loop variable 'x-y' is not a valid identifier
    let source = "---\nitems: [a, b]\n---\n@for x-y in items:\n- {{item}}\n@end\n";
    let result = mds::compile_str_with(source, None, None);
    assert!(
        result.is_err(),
        "invalid loop variable name must be rejected"
    );
    let err = format!("{}", result.unwrap_err());
    assert!(
        err.contains("invalid") || err.contains("x-y"),
        "expected syntax error about invalid identifier, got: {err}"
    );
}

#[test]
fn invalid_identifier_in_define_name() {
    // @define my-func(): — function name 'my-func' is not a valid identifier
    let source = "@define my-func():\nhello\n@end\n";
    let result = mds::compile_str_with(source, None, None);
    assert!(result.is_err(), "invalid function name must be rejected");
    let err = format!("{}", result.unwrap_err());
    assert!(
        err.contains("invalid") || err.contains("my-func"),
        "expected syntax error about invalid function name, got: {err}"
    );
}

#[test]
fn duplicate_define_params_errors() {
    // @define test(a, a): — duplicate parameter 'a' must be a compile error
    let source = "@define test(a, a):\n{{a}}\n@end\n";
    let result = mds::compile_str_with(source, None, None);
    assert!(result.is_err(), "duplicate parameter name must be rejected");
    let err = format!("{}", result.unwrap_err());
    assert!(
        err.contains("duplicate") || err.contains("'a'"),
        "expected duplicate parameter error, got: {err}"
    );
}

#[test]
fn duplicate_define_errors() {
    // NOTE: This test documents expected behavior (Spec: no duplicate function names).
    // If the compiler does not yet enforce this, this test will fail until the fix lands.
    let result = mds::compile(fixture("duplicate_define.mds"), None);
    assert!(
        result.is_err(),
        "duplicate @define for same function name should be an error"
    );
    let err = format!("{}", result.unwrap_err());
    assert!(
        err.contains("collision") || err.contains("duplicate") || err.contains("already defined"),
        "expected collision/duplicate error, got: {err}"
    );
}

#[test]
fn error_output_shows_line_numbers() {
    // Compile a file with a known error and verify the miette output
    // includes source context with line numbers
    let source = "---\nname: Alice\n---\nHello {{username}}!\n";
    let result = mds::compile_str_with(source, None, None);
    assert!(result.is_err(), "should fail with undefined variable");

    let err = result.unwrap_err();
    // Format the error using miette's Debug impl (includes source context)
    let formatted = format!("{err:?}");
    assert!(
        formatted.contains("username"),
        "error should mention 'username', got: {formatted}"
    );
    // miette's fancy rendering includes line number context
    // The source has the error on line 4
    assert!(
        formatted.contains("4") || formatted.contains("username"),
        "error output should include line number context, got: {formatted}"
    );
}

#[test]
fn error_format_includes_file_line_col() {
    // Per spec: errors include file path, line number, column, contextual explanation.
    // Use the CLI to get the full diagnostic rendering.
    let dir = tempfile::tempdir().unwrap();
    let input = dir.path().join("err_test.mds");
    std::fs::write(&input, "---\nname: Alice\n---\nHello {{undefined_var}}!\n").unwrap();

    let output = mds_bin()
        .args(["build", input.to_str().unwrap()])
        .output()
        .unwrap();

    assert!(
        !output.status.success(),
        "should fail with undefined variable"
    );
    let stderr = String::from_utf8(output.stderr).unwrap();
    // miette renders: ╭─[path:line:col]
    assert!(
        stderr.contains("undefined_var"),
        "error should mention the undefined variable, got: {stderr}"
    );
    // Line number must be present (error is on line 4)
    assert!(
        stderr.contains(":4:") || stderr.contains("4 │"),
        "error should include line number 4, got: {stderr}"
    );
}

#[test]
fn circular_import_error_has_help_text() {
    let result = mds::compile(fixture("circular_a.mds"), None);
    assert!(result.is_err(), "circular import should fail");
    let err = result.unwrap_err();
    let formatted = format!("{err:?}");
    assert!(
        formatted.contains("import") || formatted.contains("cycle"),
        "circular import error should mention import/cycle, got: {formatted}"
    );
}

#[test]
fn type_error_for_non_array_in_for_loop() {
    // Build a source that tries @for over a non-array variable
    let source = "---\ncount: 42\n---\n@for item in count:\n- {{item}}\n@end\n";
    let result = mds::compile_str(source);
    assert!(result.is_err(), "type error should be returned");
    let err = result.unwrap_err();
    // Use Display (not Debug) to get the human-readable error message
    let display = format!("{err}");
    assert!(
        display.contains("array") || display.contains("type error"),
        "type error Display should mention 'array' or 'type error', got: {display}"
    );
}

#[test]
fn if_negation_supported() {
    // `@if !premium:` — negation is now supported.
    // With premium=true, the negated condition is false → else branch.
    let source = "---\npremium: true\n---\n@if !premium:\nnegated_yes\n@else:\nnegated_no\n@end\n";
    let result = mds::compile_str(source);
    assert!(
        result.is_ok(),
        "@if with negation must succeed, got: {:?}",
        result
    );
    assert!(
        result
            .unwrap()
            .into_markdown()
            .unwrap()
            .contains("negated_no"),
        "negation of true must take else branch"
    );
}

#[test]
fn import_file_not_found_includes_source_span() {
    // When an @import directive references a non-existent file, the error must
    // include the source location (file:line:col) pointing at the @import line.
    let dir = tempfile::tempdir().unwrap();
    let consumer = dir.path().join("test.mds");
    std::fs::write(
        &consumer,
        "@import \"./nonexistent.mds\" as missing\n\nsome text\n",
    )
    .unwrap();

    let result = mds::compile(&consumer, None);
    assert!(result.is_err(), "import of non-existent file should error");

    let err = result.unwrap_err();
    // The Display format reports the path
    let display = format!("{err}");
    assert!(
        display.contains("nonexistent") || display.contains("not found"),
        "error message should mention the missing path, got: {display}"
    );
    // The Debug format (miette fancy rendering) includes source context with
    // line/column numbers when a span is attached.
    let debug = format!("{err:?}");
    assert!(
        debug.contains("test.mds") || debug.contains("1"),
        "error should include source context (file or line number), got: {debug}"
    );
    // Verify the @import line is referenced in the rendered output
    assert!(
        debug.contains("import") || debug.contains("nonexistent"),
        "error context should reference the @import line, got: {debug}"
    );
}

#[test]
fn import_file_not_found_span_for_alias_import() {
    // Alias-form @import should also include source span on file-not-found.
    let dir = tempfile::tempdir().unwrap();
    let consumer = dir.path().join("alias_test.mds");
    std::fs::write(&consumer, "@import \"./missing.mds\" as m\n\nsome text\n").unwrap();

    let result = mds::compile(&consumer, None);
    assert!(result.is_err());
    let err = result.unwrap_err();
    let debug = format!("{err:?}");
    assert!(
        debug.contains("alias_test.mds") || debug.contains("1"),
        "alias import error should include source context, got: {debug}"
    );
}

#[test]
fn import_file_not_found_span_for_merge_import() {
    // Merge-form @import (no alias) should also include source span on file-not-found.
    let dir = tempfile::tempdir().unwrap();
    let consumer = dir.path().join("merge_test.mds");
    std::fs::write(&consumer, "@import \"./missing.mds\"\n\nsome text\n").unwrap();

    let result = mds::compile(&consumer, None);
    assert!(result.is_err());
    let err = result.unwrap_err();
    let debug = format!("{err:?}");
    assert!(
        debug.contains("merge_test.mds") || debug.contains("1"),
        "merge import error should include source context, got: {debug}"
    );
}

// ── @if condition parse error tests ─────────────────────────────────────────

#[test]
fn if_empty_after_operator_is_parse_error() {
    // `@if var ==:` — missing RHS value
    let source = "---\nvar: x\n---\n@if var ==:\nyes\n@end\n";
    let result = mds::compile_str(source);
    assert!(result.is_err(), "@if var ==: must be a parse error");
    let err = format!("{}", result.unwrap_err());
    assert!(
        err.contains("expected value after '=='"),
        "error must say expected value after '==', got: {err}"
    );
}

#[test]
fn if_bare_equals_is_parse_error() {
    // `@if var = "a":` — single `=` must suggest `==`
    let source = "---\nvar: a\n---\n@if var = \"a\":\nyes\n@end\n";
    let result = mds::compile_str(source);
    assert!(result.is_err(), "@if var = 'a': must be a parse error");
    let err = format!("{}", result.unwrap_err());
    assert!(
        err.contains("'=='") || err.contains("use '=='"),
        "error must suggest '==', got: {err}"
    );
}

#[test]
fn if_negation_empty_variable_is_parse_error() {
    // `@if !:` — no variable name after `!`
    let source = "---\nvar: true\n---\n@if !:\nyes\n@end\n";
    let result = mds::compile_str(source);
    assert!(result.is_err(), "@if !: must be a parse error");
    let err = format!("{}", result.unwrap_err());
    assert!(
        err.contains("expected variable name after '!'"),
        "error must say expected variable name, got: {err}"
    );
}

#[test]
fn if_double_negation_is_parse_error() {
    // `@if !!var:` — double negation not supported
    let source = "---\nvar: true\n---\n@if !!var:\nyes\n@end\n";
    let result = mds::compile_str(source);
    assert!(result.is_err(), "@if !!var: must be a parse error");
    let err = format!("{}", result.unwrap_err());
    assert!(
        err.contains("double negation"),
        "error must mention double negation, got: {err}"
    );
}

#[test]
fn elseif_after_else_is_parse_error() {
    // `@elseif` appearing after `@else:` — not valid; the @else body only
    // accepts @end as a terminator so @elseif is an unknown directive there.
    let source = "---\nx: true\n---\n@if x:\nyes\n@else:\nno\n@elseif x:\nbad\n@end\n";
    let result = mds::compile_str(source);
    assert!(
        result.is_err(),
        "@elseif after @else: must be a parse error"
    );
    let err = format!("{}", result.unwrap_err());
    assert!(
        err.contains("@elseif") || err.contains("unknown directive"),
        "error must mention @elseif or unknown directive, got: {err}"
    );
}

#[test]
fn if_negation_combined_with_comparison_is_parse_error() {
    // `@if !var == "x":` — cannot combine negation with comparison
    let source = "---\nvar: x\n---\n@if !var == \"x\":\nyes\n@end\n";
    let result = mds::compile_str(source);
    assert!(result.is_err(), "@if !var == 'x': must be a parse error");
    let err = format!("{}", result.unwrap_err());
    assert!(
        err.contains("cannot combine negation") || err.contains("negation with comparison"),
        "error must explain cannot combine negation with comparison, got: {err}"
    );
}

#[test]
fn if_unterminated_string_in_condition_is_parse_error() {
    // `@if var == "unclosed:` — unterminated string literal
    let source = "---\nvar: x\n---\n@if var == \"unclosed:\nyes\n@end\n";
    let result = mds::compile_str(source);
    assert!(result.is_err(), "unterminated string must be a parse error");
    let err = format!("{}", result.unwrap_err());
    assert!(
        err.contains("unterminated string"),
        "error must mention unterminated string, got: {err}"
    );
}

#[test]
fn if_eq_undefined_variable_is_error() {
    // `@if missing == "x":` — undefined variable in equality
    let source = "---\nvar: x\n---\n@if missing == \"x\":\nyes\n@end\n";
    let result = mds::compile_str(source);
    assert!(
        result.is_err(),
        "undefined variable in equality must be an error"
    );
    let err = format!("{}", result.unwrap_err());
    assert!(
        err.contains("missing") || err.contains("undefined"),
        "error must mention the undefined variable, got: {err}"
    );
}

#[test]
fn if_negation_undefined_variable_is_error() {
    // `@if !missing:` — undefined variable in negation
    let source = "---\nvar: x\n---\n@if !missing:\nyes\n@end\n";
    let result = mds::compile_str(source);
    assert!(
        result.is_err(),
        "undefined variable in negation must be an error"
    );
    let err = format!("{}", result.unwrap_err());
    assert!(
        err.contains("missing") || err.contains("undefined"),
        "error must mention the undefined variable, got: {err}"
    );
}

#[test]
fn type_mismatch_cli_shows_miette_code_frame() {
    // D2: a type mismatch in @if now carries a span, so the CLI renders a miette
    // code frame with ╭─[file:line:col] (or the equivalent inline code block).
    let dir = tempfile::tempdir().unwrap();
    let input = dir.path().join("mismatch.mds");
    // frontmatter: x is a YAML integer (number); condition compares to a string.
    std::fs::write(
        &input,
        "---\nx: 3\n---\n@if x == \"3\":\nyes\n@else:\nno\n@end\n",
    )
    .unwrap();

    let output = mds_bin()
        .args(["build", input.to_str().unwrap()])
        .output()
        .unwrap();

    assert!(!output.status.success(), "D2: type mismatch must fail");
    let stderr = String::from_utf8(output.stderr).unwrap();
    // miette renders a code frame with ╭─[file:line:col] when a span is present.
    assert!(
        stderr.contains("type mismatch") || stderr.contains("mds::type_mismatch"),
        "D2: stderr must mention type_mismatch; got: {stderr}"
    );
    // A span is present → miette shows the file path in the frame.
    assert!(
        stderr.contains("mismatch.mds") || stderr.contains(":4:") || stderr.contains("4 │"),
        "D2: miette code frame must reference file or line 4; got: {stderr}"
    );
}

// ── R3 / CWE-209: human error frame names the project-root-relative path ─────

#[test]
fn r3_error_frame_names_root_relative_path_for_subdir_file() {
    // A compile error in a subdirectory file must render its miette frame
    // header with the project-root-relative path ("sub/err_test.mds"), never
    // the canonicalized absolute path (R3 / CWE-209, construction-time
    // relativization in mds-core).
    let dir = tempfile::tempdir().unwrap();
    // Canonicalize: macOS resolves /var → /private/var; the canonical form is
    // what a leaked absolute key would carry.
    let root = dir.path().canonicalize().unwrap();
    // Deterministic project-root walk-up anchor.
    std::fs::write(root.join(".mdsroot"), "").unwrap();
    let sub = root.join("sub");
    std::fs::create_dir_all(&sub).unwrap();
    let input = sub.join("err_test.mds");
    std::fs::write(&input, "Hello {{undefined_var}}!\n").unwrap();

    let output = mds_bin()
        .args(["build", input.to_str().unwrap()])
        .output()
        .unwrap();

    assert!(
        !output.status.success(),
        "undefined variable must fail the build"
    );
    let stderr = String::from_utf8(output.stderr).unwrap();
    let root_str = root.to_str().unwrap();
    // Positive control (PF-013): a frame naming the absolute path WOULD be
    // caught by the absence assertion below.
    let planted = format!("╭─[{}:1:10]", input.display());
    assert!(
        planted.contains(root_str),
        "positive control: planted absolute frame must match the absence predicate"
    );
    assert!(
        stderr.contains("sub/err_test.mds"),
        "frame header must name the root-relative path; got: {stderr}"
    );
    assert!(
        !stderr.contains(root_str),
        "stderr must not leak the absolute path prefix; got: {stderr}"
    );
}