noxid-cli 0.2.0

The Noxid compiler command line: check, build, test, adapt, and the agent surface
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
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
//! WO-55 — CSS accuracy.
//!
//! The view is the oracle: every refusal below is provable from a component's
//! `style` block plus its compiler-visible view tree. Each case asserts the
//! teaching text as well as the code, because the message is the prompt the
//! next model reads.

use noxid_compiler_core::compile;
use noxid_source::{Diagnostic, SourceFile, SourceId};

fn check(source: &str) -> Vec<Diagnostic> {
    let file = SourceFile::new(SourceId(0), "Wo55.nox", source);
    compile(&file).diagnostics
}

fn only(source: &str, code: &str) -> String {
    let diagnostics = check(source);
    let matching = diagnostics
        .iter()
        .filter(|diagnostic| diagnostic.code == code)
        .collect::<Vec<_>>();
    assert!(
        !matching.is_empty(),
        "expected {code}, saw {:?}",
        diagnostics
            .iter()
            .map(|diagnostic| (diagnostic.code, diagnostic.message.as_str()))
            .collect::<Vec<_>>()
    );
    matching[0].message.clone()
}

fn assert_clean(source: &str) {
    let diagnostics = check(source);
    let css = diagnostics
        .iter()
        .filter(|diagnostic| diagnostic.code.starts_with("CSS_"))
        .map(|diagnostic| format!("{}: {}", diagnostic.code, diagnostic.message))
        .collect::<Vec<_>>();
    assert!(css.is_empty(), "expected no CSS refusals, saw {css:#?}");
}

#[test]
fn unused_selector_names_the_nearest_rendered_element() {
    let message = only(
        r#"
component Panel {
    view { <article class="card"><span>ok</span></article> }
    style { .crd { color: red; } }
}
"#,
        "CSS_UNUSED_SELECTOR",
    );
    assert!(message.contains("`.crd`"), "{message}");
    assert!(
        message.contains("matches no element this component's view can render"),
        "{message}"
    );
    assert!(
        message.contains("the nearest element this view renders is `.card`"),
        "{message}"
    );
    assert!(message.contains("wrap it in `:global(...)`"), "{message}");
}

#[test]
fn global_selectors_are_exempt_from_the_view_oracle() {
    assert_clean(
        r#"
component Panel {
    view { <article class="card">ok</article> }
    style {
        :global(body) { margin: 0; }
        .card { color: red; }
    }
}
"#,
    );
}

#[test]
fn a_bound_class_is_never_guessed_at() {
    // `class` is bound, so the compiler cannot know which classes render;
    // unknown must never refuse.
    assert_clean(
        r#"
component Panel {
    state { tone: String = "warm" }
    view { <article class={tone}>ok</article> }
    style { .warm { color: red; } .cool { color: blue; } }
}
"#,
    );
}

#[test]
fn descendant_and_child_combinators_match_through_the_view() {
    assert_clean(
        r#"
component Panel {
    view {
        <main class="layout">
            <section class="cards"><article class="card"><span>ok</span></article></section>
        </main>
    }
    style {
        .layout .card { color: red; }
        .cards > .card { border-color: blue; }
        .card > span { display: block; }
    }
}
"#,
    );
    let message = only(
        r#"
component Panel {
    view {
        <main class="layout"><section class="cards">ok</section></main>
    }
    style { .cards > .layout { color: red; } }
}
"#,
        "CSS_UNUSED_SELECTOR",
    );
    assert!(message.contains("`.cards > .layout`"), "{message}");
}

#[test]
fn unknown_property_names_the_nearest_known_one() {
    let message = only(
        r#"
component Panel {
    view { <p class="note">ok</p> }
    style { .note { bacground: red; } }
}
"#,
        "CSS_UNKNOWN_PROPERTY",
    );
    assert!(
        message.contains("unknown CSS property `bacground`"),
        "{message}"
    );
    assert!(message.contains("Write `background` instead."), "{message}");
    assert!(
        message.contains("Custom properties (`--name`)"),
        "{message}"
    );
}

#[test]
fn custom_and_vendor_prefixed_properties_stay_legal() {
    assert_clean(
        r#"
component Panel {
    view { <p class="note">ok</p> }
    style { .note { --brand: #0ea5e9; -webkit-line-clamp: 3; color: var(--brand); } }
}
"#,
    );
}

#[test]
fn invalid_value_lists_the_legal_kinds() {
    let message = only(
        r#"
component Panel {
    view { <p class="note">ok</p> }
    style { .note { color: 12px; } }
}
"#,
        "CSS_INVALID_VALUE",
    );
    assert!(
        message.contains("`12px` is not a legal value for `color`"),
        "{message}"
    );
    assert!(message.contains("`color` accepts a color."), "{message}");

    let keyword = only(
        r#"
component Panel {
    view { <p class="note">ok</p> }
    style { .note { display: flexx; } }
}
"#,
        "CSS_INVALID_VALUE",
    );
    assert!(keyword.contains("`flexx`"), "{keyword}");
    assert!(keyword.contains("`flex`"), "{keyword}");
}

#[test]
fn unresolvable_values_are_never_refused() {
    assert_clean(
        r##"
design Product {
    tokens { primary: Color = "#0ea5e9" }
    components { Button }
}
component Panel {
    view { <p class="note">ok</p> }
    style {
        .note {
            color: token(primary);
            padding: calc(1rem + 2px);
            background: var(--anything, red);
            width: clamp(1rem, 5vw, 10rem);
        }
    }
}
"##,
    );
}

#[test]
fn shadowed_declaration_names_both_rules() {
    let message = only(
        r#"
component Panel {
    view { <article class="card">ok</article> }
    style {
        .card { color: red; padding: 1rem; }
        .card { color: blue; }
    }
}
"#,
        "CSS_SHADOWED_DECLARATION",
    );
    assert!(message.contains("`color` declared on `.card`"), "{message}");
    assert!(message.contains("fully overridden"), "{message}");
    assert!(message.contains("equal or higher specificity"), "{message}");
}

#[test]
fn pseudo_class_rules_do_not_shadow() {
    assert_clean(
        r#"
component Panel {
    view { <button class="cta">ok</button> }
    style {
        .cta { color: red; }
        .cta:hover { color: blue; }
    }
}
"#,
    );
}

#[test]
fn media_context_does_not_shadow_the_top_level() {
    assert_clean(
        r#"
component Panel {
    view { <article class="card">ok</article> }
    style {
        .card { padding: 1.25rem; }
        @media (min-width: 40rem) { .card { padding: 1.5rem; } }
    }
}
"#,
    );
}

#[test]
fn important_is_refused_in_a_component_block() {
    let message = only(
        r#"
component Panel {
    view { <p class="note">ok</p> }
    style { .note { color: red !important; } }
}
"#,
        "CSS_IMPORTANT_FORBIDDEN",
    );
    assert!(message.contains("`!important` is not allowed"), "{message}");
    assert!(message.contains("scoping already isolates"), "{message}");
    assert!(message.contains("Delete `!important`"), "{message}");
}

#[test]
fn inapplicable_nested_property_names_the_parent_display() {
    let message = only(
        r#"
component Panel {
    view { <div class="row"><span class="item">ok</span></div> }
    style {
        .row { display: block; }
        .item { align-self: center; }
    }
}
"#,
        "CSS_INVALID_NESTING",
    );
    assert!(
        message.contains("`align-self` only has an effect on a flex item"),
        "{message}"
    );
    assert!(
        message.contains("`display: block` on the parent `.row`"),
        "{message}"
    );
    assert!(
        message.contains("Declare `display: flex` on `.row`"),
        "{message}"
    );
}

#[test]
fn a_declared_flex_parent_makes_the_item_property_legal() {
    assert_clean(
        r#"
component Panel {
    view { <div class="row"><span class="item">ok</span></div> }
    style {
        .row { display: flex; }
        .item { align-self: center; }
    }
}
"#,
    );
}

#[test]
fn an_undeclared_display_is_never_inferred() {
    // Nothing in the block declares `.row`'s display, so nothing is provable.
    assert_clean(
        r#"
component Panel {
    view { <div class="row"><span class="item">ok</span></div> }
    style { .item { align-self: center; } }
}
"#,
    );
}

#[test]
fn container_properties_check_the_elements_own_display() {
    let message = only(
        r#"
component Panel {
    view { <div class="row">ok</div> }
    style { .row { display: block; flex-direction: column; } }
}
"#,
        "CSS_INVALID_NESTING",
    );
    assert!(
        message.contains("`flex-direction` only has an effect on a flex container"),
        "{message}"
    );
    assert!(message.contains("`display: block` on `.row`"), "{message}");
}

#[test]
fn every_shipped_example_and_the_website_stay_clean() {
    // The accuracy checks fail closed, so a false refusal would be a silent
    // regression for every author. The shipped corpus is the regression net.
    let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../..");
    let mut checked = 0usize;
    let mut refusals = Vec::new();
    let mut stack = vec![root.join("examples"), root.join("website")];
    while let Some(directory) = stack.pop() {
        let Ok(entries) = std::fs::read_dir(&directory) else {
            continue;
        };
        for entry in entries.flatten() {
            let path = entry.path();
            if path.is_dir() {
                stack.push(path);
                continue;
            }
            if path.extension().and_then(|value| value.to_str()) != Some("nox") {
                continue;
            }
            let Ok(text) = std::fs::read_to_string(&path) else {
                continue;
            };
            if !text.contains("style {") {
                continue;
            }
            let file = SourceFile::new(SourceId(0), &path, text);
            let diagnostics = compile(&file).diagnostics;
            // Project files compiled in isolation cannot resolve their
            // imported components, and the accuracy pass correctly defers
            // for them; the single-file corpus is what this asserts on.
            if diagnostics.iter().any(|diagnostic| {
                diagnostic.severity == noxid_source::Severity::Error
                    && !diagnostic.code.starts_with("CSS_")
            }) {
                continue;
            }
            checked += 1;
            for diagnostic in diagnostics {
                if diagnostic.code.starts_with("CSS_") {
                    refusals.push(format!(
                        "{}: {}: {}",
                        path.display(),
                        diagnostic.code,
                        diagnostic.message
                    ));
                }
            }
        }
    }
    assert!(
        checked > 20,
        "expected the shipped corpus, saw {checked} files"
    );
    assert!(refusals.is_empty(), "{refusals:#?}");
}

// ---------------------------------------------------------------------
// Stage (b): `strict` design mode.
// ---------------------------------------------------------------------

const STRICT_DESIGN: &str = r##"
design Product strict {
    tokens {
        ink: Color = "#0f172a"
        surface: Color = "#ffffff"
        faint: Color = "#a3b1c6"
        spaceMd: Spacing = "1rem"
        radiusMd: Radius = "8px"
        quick: Duration = "150ms"
        md: Breakpoint = "40rem"
    }
    components { Button }
}
"##;

fn strict(body: &str) -> String {
    format!("{STRICT_DESIGN}\n{body}")
}

#[test]
fn a_raw_color_under_strict_enumerates_the_declared_tokens() {
    let message = only(
        &strict(
            r#"
component Panel {
    view { <p class="note">x</p> }
    style { .note { color: #0f172a; } }
}
"#,
        ),
        "CSS_TOKEN_REQUIRED",
    );
    assert!(
        message.contains("`#0f172a` is a raw Color value"),
        "{message}"
    );
    assert!(message.contains("`design Product strict`"), "{message}");
    assert!(message.contains("`token(ink)`"), "{message}");
    assert!(message.contains("`token(surface)`"), "{message}");
}

#[test]
fn strict_refuses_raw_lengths_radii_and_durations_by_position() {
    let source = strict(
        r#"
component Panel {
    view { <p class="note">x</p> }
    style {
        .note {
            padding: 1.25rem;
            border-radius: 12px;
            transition: color;
            transition-duration: 200ms;
        }
    }
}
"#,
    );
    let kinds = check(&source)
        .into_iter()
        .filter(|diagnostic| diagnostic.code == "CSS_TOKEN_REQUIRED")
        .map(|diagnostic| diagnostic.message)
        .collect::<Vec<_>>();
    assert!(
        kinds
            .iter()
            .any(|message| message.contains("raw Spacing value")
                && message.contains("`token(spaceMd)`")),
        "{kinds:#?}"
    );
    assert!(
        kinds
            .iter()
            .any(|message| message.contains("raw Radius value")
                && message.contains("`token(radiusMd)`")),
        "{kinds:#?}"
    );
    assert!(
        kinds
            .iter()
            .any(|message| message.contains("raw Duration value")
                && message.contains("`token(quick)`")),
        "{kinds:#?}"
    );
}

#[test]
fn structural_literals_stay_legal_under_strict() {
    assert_clean(&strict(
        r#"
component Panel {
    view { <p class="note">x</p> }
    style {
        .note {
            margin: 0;
            width: 100%;
            max-width: none;
            height: auto;
            opacity: 1;
            color: token(ink);
            background: token(surface);
            padding: token(spaceMd);
        }
    }
}
"#,
    ));
}

#[test]
fn a_width_query_without_a_breakpoint_token_is_refused() {
    let message = only(
        &strict(
            r#"
component Panel {
    view { <p class="note">x</p> }
    style {
        .note { color: token(ink); }
        @media (min-width: 40rem) { .note { background: token(surface); } }
    }
}
"#,
        ),
        "CSS_TOKEN_REQUIRED",
    );
    assert!(message.contains("raw Breakpoint value"), "{message}");
    assert!(message.contains("`token(md)`"), "{message}");
}

#[test]
fn a_width_query_naming_a_breakpoint_token_is_legal() {
    assert_clean(&strict(
        r#"
component Panel {
    view { <p class="note">x</p> }
    style {
        .note { color: token(ink); }
        @media (min-width: token(md)) { .note { background: token(surface); } }
    }
}
"#,
    ));
}

#[test]
fn a_breakpoint_token_resolves_to_its_width_in_the_query_itself() {
    // A media condition is evaluated before custom properties resolve, so
    // emitting `var(--noxid-md)` here would be a query that never matches —
    // and strict mode makes `token(md)` the only legal spelling, so the
    // refusal would be pushing every author into the broken construct.
    let source = strict(
        r#"
component Panel {
    view { <p class="note">x</p> }
    style {
        .note { color: token(ink); }
        @media (min-width: token(md)) { .note { background: token(surface); } }
    }
}
"#,
    );
    let file = SourceFile::new(SourceId(0), "Wo55.nox", &source);
    let styles = compile(&file).styles.to_json();
    assert!(
        styles.contains("\"prelude\":\"(min-width: 40rem)\""),
        "breakpoint token did not resolve in the prelude: {styles}"
    );
    assert!(
        !styles.contains("\"prelude\":\"(min-width: var("),
        "a query prelude cannot use a custom property: {styles}"
    );
}

#[test]
fn a_failing_contrast_pair_reports_the_ratio() {
    let message = only(
        &strict(
            r#"
component Panel {
    view { <p class="note">x</p> }
    style { .note { color: token(faint); background: token(surface); } }
}
"#,
        ),
        "CSS_CONTRAST_INSUFFICIENT",
    );
    assert!(message.contains("token(faint)"), "{message}");
    assert!(message.contains("token(surface)"), "{message}");
    assert!(message.contains(":1"), "{message}");
    assert!(message.contains("WCAG AA minimum of 4.5:1"), "{message}");
}

#[test]
fn a_passing_contrast_pair_is_accepted_and_untokened_pairs_are_not_guessed() {
    assert_clean(&strict(
        r#"
component Panel {
    view { <p class="note">x</p> }
    style { .note { color: token(ink); background: token(surface); } }
}
"#,
    ));
    // Neither side is a token, so there is nothing to resolve and nothing
    // to refuse — the compiler does not guess at a runtime colour.
    assert_clean(
        r#"
component Panel {
    view { <p class="note">x</p> }
    style { .note { color: #a3b1c6; background: #ffffff; } }
}
"#,
    );
}

#[test]
fn a_non_strict_design_system_leaves_literals_alone() {
    assert_clean(
        r##"
design Product {
    tokens { ink: Color = "#0f172a" }
    components { Button }
}
component Panel {
    view { <p class="note">x</p> }
    style { .note { color: #123456; padding: 1rem; } }
}
"##,
    );
}