nativeview-cli 0.1.0-rc.1

Developer CLI (`nv`) for Native View Engine: dev hot-reload, builds, pairing, SDK management, app scaffolding, and docs.
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
//! Unit tests for `view_diff` covering direct-root text/style/prop patching, composite fallback,
//! control flow, non-`view!` Rust code rejection, and multiple/nested `view!` ordinals.

mod regressions;
mod roundtrip;

use nativeview_engine::HotPatchMsg;
use nativeview_store::{Length, Rgba, RowPatchView};
use nativeview_tables::tables::{
    AlignItems, CursorKind, FlexDirection, Interaction, JustifyContent, Layout, Overflow, TextAlign,
    Typography, Visual,
};
use nativeview_view_syntax::compute_anchor_key;

use super::{ViewDiffOutcome, diff_view_source};

pub(super) fn expect_tier1(outcome: ViewDiffOutcome) -> Vec<HotPatchMsg> {
    let ViewDiffOutcome::Tier1Patch { messages, .. } = outcome else {
        unreachable!("expected Tier1Patch, got {outcome:?}");
    };
    messages
}

pub(super) fn expect_tier2(outcome: ViewDiffOutcome) -> String {
    let ViewDiffOutcome::NeedsRebuild { reason } = outcome else {
        unreachable!("expected NeedsRebuild, got {outcome:?}");
    };
    reason
}

#[test]
fn direct_root_static_text_edits_emit_anchor_key_with_empty_node_path() {
    let base = r#"
fn panel() -> impl View {
    view! {
        Column(style: TypedStyle::col().gap(8.0)) {
            "Welcome"
            Text("Subtitle")
            Text(text: "Footer")
        }
    }
}
"#;
    let edited = base
        .replace("\"Welcome\"", "\"Hello World\"")
        .replace("\"Subtitle\"", "\"Updated Subtitle\"")
        .replace("\"Footer\"", "\"New Footer\"");

    let msgs = expect_tier1(diff_view_source("src/panel.rs", base, &edited, 7));
    assert_eq!(msgs.len(), 3);

    let expected = [
        (
            compute_anchor_key("src/panel.rs", 0, &[0, 0]).0,
            "Welcome",
            "Hello World",
        ),
        (
            compute_anchor_key("src/panel.rs", 0, &[0, 1]).0,
            "Subtitle",
            "Updated Subtitle",
        ),
        (
            compute_anchor_key("src/panel.rs", 0, &[0, 2]).0,
            "Footer",
            "New Footer",
        ),
    ];

    for (msg, (exp_key, exp_old, exp_new)) in msgs.iter().zip(expected) {
        let HotPatchMsg::TemplatePatch {
            generation,
            template_key,
            node_path,
            row_patch_words,
            text_updates,
            text_replacements,
            ..
        } = msg
        else {
            unreachable!("expected TemplatePatch, got {msg:?}");
        };
        assert_eq!(*generation, 7);
        assert_eq!(*template_key, exp_key);
        assert!(node_path.is_empty(), "view_diff must emit empty node_path");
        assert!(row_patch_words.is_empty());
        assert!(text_updates.is_empty());
        assert_eq!(
            text_replacements.as_slice(),
            &[(exp_old.to_owned(), exp_new.to_owned())]
        );
    }
}

#[test]
fn direct_root_static_style_color_layout_and_typography_edits_emit_row_patch() {
    let base = r#"
fn card() -> impl View {
    view! {
        Column(
            test_id: "root-col",
            style: Style::col()
                .padding(12.0)
                .gap(8.0)
                .bg(Rgba::rgb(15, 23, 42))
                .border(1.0, Rgba::rgb(51, 65, 85))
                .radius(8.0)
                .opacity(0.9)
                .align_items(AlignItems::FlexStart)
                .justify_content(JustifyContent::FlexStart)
                .overflow(Overflow::Hidden)
        ) {
            Text(
                "Heading",
                color: Rgba::WHITE,
                font_size: 16.0,
                font_weight: 500,
                text_align: TextAlign::Left,
            )
            Pressable(cursor: CursorKind::Default, width: 120.0, spacing: 4.0) {
                Row(flex_direction: FlexDirection::Row) {
                    LeafNode(opacity: 0.5)
                }
            }
            ScrollView(padding_horizontal: 10.0) {
                Node(width: Length::points(200.0))
            }
        }
    }
}
"#;
    let edited = base
        .replace(".padding(12.0)", ".padding(20.0)")
        .replace(".gap(8.0)", ".gap(16.0)")
        .replace("Rgba::rgb(15, 23, 42)", "Rgba::rgb(30, 41, 59)")
        .replace(
            ".border(1.0, Rgba::rgb(51, 65, 85))",
            ".border(2.0, Rgba::rgb(100, 116, 139))",
        )
        .replace(".radius(8.0)", ".radius(14.0)")
        .replace(".opacity(0.9)", ".opacity(1.0)")
        .replace("AlignItems::FlexStart", "AlignItems::Center")
        .replace("JustifyContent::FlexStart", "JustifyContent::SpaceBetween")
        .replace("Overflow::Hidden", "Overflow::Auto")
        .replace("\"Heading\"", "\"Dashboard\"")
        .replace("color: Rgba::WHITE", "color: Rgba::BLACK")
        .replace("font_size: 16.0", "font_size: 24.0")
        .replace("font_weight: 500", "font_weight: 700")
        .replace("TextAlign::Left", "TextAlign::Center")
        .replace("CursorKind::Default", "CursorKind::Pointer")
        .replace("width: 120.0", "width: 180.0")
        .replace("spacing: 4.0", "spacing: 12.0")
        .replace("FlexDirection::Row", "FlexDirection::RowReverse")
        .replace("opacity: 0.5", "opacity: 0.75")
        .replace("padding_horizontal: 10.0", "padding_horizontal: 24.0")
        .replace("Length::points(200.0)", "Length::points(320.0)");

    let outcome = diff_view_source("src/card.rs", base, &edited, 11);
    let ViewDiffOutcome::Tier1Patch { patch, messages: msgs } = outcome else {
        unreachable!("expected Tier1Patch, got {outcome:?}");
    };
    assert!(
        patch.log_lines().iter().any(|l| l.contains("test_id \"root-col\"")),
        "expected test_id in watch log lines: {:?}",
        patch.log_lines()
    );
    assert_eq!(msgs.len(), 7, "expected one TemplatePatch per edited direct-root element");

    let find_msg = |path: &[u16]| {
        let key = compute_anchor_key("src/card.rs", 0, path).0;
        msgs.iter()
            .find(|m| matches!(m, HotPatchMsg::TemplatePatch { template_key, .. } if *template_key == key))
            .expect("missing expected AnchorKey TemplatePatch")
    };

    // Text("Heading") -> atomic text + typography patch on [0, 0]
    let HotPatchMsg::TemplatePatch {
        node_path,
        row_patch_words,
        text_replacements,
        ..
    } = find_msg(&[0, 0])
    else {
        unreachable!();
    };
    assert!(node_path.is_empty());
    assert_eq!(
        text_replacements.as_slice(),
        &[("Heading".to_owned(), "Dashboard".to_owned())]
    );
    let tv = RowPatchView::from_words(row_patch_words).expect("valid Text RowPatch");
    assert_eq!(tv.get_prop(Typography::COLOR), Some(Rgba::BLACK));
    assert_eq!(tv.get_prop(Typography::FONT_SIZE), Some(24.0));
    assert_eq!(tv.get_prop(Typography::FONT_WEIGHT), Some(700));
    assert_eq!(tv.get_prop(Typography::TEXT_ALIGN), Some(TextAlign::Center));

    // Root Column [0]
    let HotPatchMsg::TemplatePatch {
        target_test_id,
        row_patch_words,
        ..
    } = find_msg(&[0])
    else {
        unreachable!();
    };
    assert!(
        target_test_id.is_none(),
        "keyed AnchorKey patches must not set target_test_id fallback"
    );
    let cv = RowPatchView::from_words(row_patch_words).expect("valid Column RowPatch");
    assert_eq!(cv.get_prop(Layout::PADDING_TOP), Some(Length::points(20.0)));
    assert_eq!(cv.get_prop(Layout::ROW_GAP), Some(Length::points(16.0)));
    assert_eq!(cv.get_prop(Layout::COLUMN_GAP), Some(Length::points(16.0)));
    assert_eq!(cv.get_prop(Visual::BACKGROUND), Some(Rgba::rgb(30, 41, 59)));
    assert_eq!(cv.get_prop(Visual::BORDER_WIDTH), Some(2.0));
    assert_eq!(
        cv.get_prop(Visual::BORDER_COLOR),
        Some(Rgba::rgb(100, 116, 139))
    );
    assert_eq!(cv.get_prop(Visual::BORDER_RADIUS), Some(14.0));
    assert_eq!(cv.get_prop(Visual::OPACITY), Some(1.0));
    assert_eq!(cv.get_prop(Layout::ALIGN_ITEMS), Some(AlignItems::Center));
    assert_eq!(
        cv.get_prop(Layout::JUSTIFY_CONTENT),
        Some(JustifyContent::SpaceBetween)
    );
    assert_eq!(cv.get_prop(Visual::OVERFLOW_X), Some(Overflow::Auto));
    assert_eq!(cv.get_prop(Visual::OVERFLOW_Y), Some(Overflow::Auto));

    // Pressable [0, 1], Row [0, 1, 0], LeafNode [0, 1, 0, 0]
    let HotPatchMsg::TemplatePatch { row_patch_words, .. } = find_msg(&[0, 1]) else {
        unreachable!();
    };
    let pv = RowPatchView::from_words(row_patch_words).expect("valid Pressable RowPatch");
    assert_eq!(pv.get_prop(Interaction::CURSOR), Some(CursorKind::Pointer));
    assert_eq!(pv.get_prop(Layout::WIDTH), Some(Length::points(180.0)));
    assert_eq!(pv.get_prop(Layout::ROW_GAP), Some(Length::points(12.0)));

    let HotPatchMsg::TemplatePatch { row_patch_words, .. } = find_msg(&[0, 1, 0]) else {
        unreachable!();
    };
    let rv = RowPatchView::from_words(row_patch_words).expect("valid Row RowPatch");
    assert_eq!(
        rv.get_prop(Layout::FLEX_DIRECTION),
        Some(FlexDirection::RowReverse)
    );

    let HotPatchMsg::TemplatePatch { row_patch_words, .. } = find_msg(&[0, 1, 0, 0]) else {
        unreachable!();
    };
    let lv = RowPatchView::from_words(row_patch_words).expect("valid LeafNode RowPatch");
    assert_eq!(lv.get_prop(Visual::OPACITY), Some(0.75));

    // ScrollView [0, 2] & Node [0, 2, 0]
    let HotPatchMsg::TemplatePatch { row_patch_words, .. } = find_msg(&[0, 2]) else {
        unreachable!();
    };
    let sv = RowPatchView::from_words(row_patch_words).expect("valid ScrollView RowPatch");
    assert_eq!(sv.get_prop(Layout::PADDING_LEFT), Some(Length::points(24.0)));
    assert_eq!(sv.get_prop(Layout::PADDING_RIGHT), Some(Length::points(24.0)));

    let HotPatchMsg::TemplatePatch { row_patch_words, .. } = find_msg(&[0, 2, 0]) else {
        unreachable!();
    };
    let nv = RowPatchView::from_words(row_patch_words).expect("valid Node RowPatch");
    assert_eq!(nv.get_prop(Layout::WIDTH), Some(Length::points(320.0)));
}

#[test]
fn composite_components_and_qualified_paths_fall_back_to_tier2() {
    let base = r#"
fn form() -> impl View {
    view! {
        Column {
            Button(label: "Save", on_press: move |_| save())
            Card(padding: 12.0, style: Style::col().bg(Rgba::rgb(10, 10, 10))) {
                Text("Inside card")
            }
            ui::Column(style: Style::col().gap(4.0)) {
                "Qualified"
            }
        }
    }
}
"#;

    let edit_button = base.replace("\"Save\"", "\"Submit\"");
    assert!(expect_tier2(diff_view_source("src/form.rs", base, &edit_button, 1)).contains("Button"));

    let edit_card_prop = base.replace("padding: 12.0", "padding: 24.0");
    assert!(expect_tier2(diff_view_source("src/form.rs", base, &edit_card_prop, 1)).contains("Card"));

    let edit_card_style = base.replace("Rgba::rgb(10, 10, 10)", "Rgba::rgb(20, 20, 20)");
    assert!(
        expect_tier2(diff_view_source("src/form.rs", base, &edit_card_style, 1)).contains("Card")
    );

    let edit_qualified = base.replace(".gap(4.0)", ".gap(12.0)");
    assert!(
        expect_tier2(diff_view_source("src/form.rs", base, &edit_qualified, 1))
            .contains("ui::Column")
    );
}

#[test]
fn rust_logic_structural_control_flow_and_dynamic_edits_fall_back_to_tier2() {
    let base = r#"
fn screen(count: Signal<i32>, ready: bool) -> impl View {
    let step = 1;
    view! {
        Column(gap: 8.0) {
            Text("Static")
            Text("Count: {count}"),
            { badge(step) }
            if ready {
                "Ready"
            } else {
                "Loading"
            }
        }
    }
}
"#;

    let edit_rust = base.replace("let step = 1;", "let step = 2;");
    assert!(
        expect_tier2(diff_view_source("src/screen.rs", base, &edit_rust, 1))
            .contains("outside patchable view!")
    );

    let add_child = base.replace("Text(\"Static\")", "Text(\"Static\")\n            \"Extra\"");
    assert!(
        expect_tier2(diff_view_source("src/screen.rs", base, &add_child, 1))
            .contains("child count")
    );

    let remove_child = base.replace("Text(\"Static\")\n", "");
    assert!(
        expect_tier2(diff_view_source("src/screen.rs", base, &remove_child, 1))
            .contains("child count")
    );

    let edit_expr = base.replace("badge(step)", "badge(step + 1)");
    assert!(
        expect_tier2(diff_view_source("src/screen.rs", base, &edit_expr, 1))
            .contains("dynamic expression")
    );

    let edit_interp = base.replace("\"Count: {count}\"", "\"Total: {count}\"");
    assert!(
        expect_tier2(diff_view_source("src/screen.rs", base, &edit_interp, 1))
            .contains("interpolated")
    );

    let edit_cond = base.replace("if ready {", "if !ready {");
    assert!(
        expect_tier2(diff_view_source("src/screen.rs", base, &edit_cond, 1))
            .contains("condition")
    );

    // Static edit inside an `else` branch Tier-1 patches with exact branch path [0, 3, 1, 0].
    let edit_branch_text = base.replace("\"Loading\"", "\"Fetching...\"");
    let msgs = expect_tier1(diff_view_source("src/screen.rs", base, &edit_branch_text, 2));
    assert_eq!(msgs.len(), 1);
    let HotPatchMsg::TemplatePatch {
        template_key,
        node_path,
        text_replacements,
        ..
    } = &msgs[0]
    else {
        unreachable!();
    };
    assert_eq!(
        *template_key,
        compute_anchor_key("src/screen.rs", 0, &[0, 3, 1, 0]).0
    );
    assert!(node_path.is_empty());
    assert_eq!(
        text_replacements.as_slice(),
        &[("Loading".to_owned(), "Fetching...".to_owned())]
    );

    // Comment / whitespace-only edits return NoOp.
    let edit_ws = base.replace("let step = 1;", "let   step = 1; // comment");
    assert_eq!(
        diff_view_source("src/screen.rs", base, &edit_ws, 3),
        ViewDiffOutcome::NoOp
    );
}

fn node_with_style(chain: &str) -> String {
    format!(
        "fn panel() -> impl View {{\n    view! {{\n        Node(style: {chain}) {{\n            \"Body\"\n        }}\n    }}\n}}\n"
    )
}

fn style_patch_prop<T: nativeview_store::NvTable, V: nativeview_store::ScalarField>(
    old_chain: &str,
    new_chain: &str,
    prop: nativeview_store::Prop<T, V>,
) -> Option<V> {
    let msgs = expect_tier1(diff_view_source(
        "src/panel.rs",
        &node_with_style(old_chain),
        &node_with_style(new_chain),
        1,
    ));
    assert_eq!(msgs.len(), 1, "{msgs:?}");
    let HotPatchMsg::TemplatePatch { row_patch_words, .. } = &msgs[0] else {
        unreachable!("expected TemplatePatch");
    };
    RowPatchView::from_words(row_patch_words)
        .expect("valid binary RowPatch words")
        .get_prop(prop)
}

#[test]
fn position_fixed_and_newer_style_builder_methods_are_classified_correctly() {
    use nativeview_tables::tables::Position;

    assert_eq!(
        style_patch_prop(
            "TypedStyle::new().position(Position::Relative)",
            "TypedStyle::new().position(Position::Fixed)",
            Layout::POSITION,
        ),
        Some(Position::Fixed)
    );
    assert_eq!(
        style_patch_prop(
            "TypedStyle::new().position(PositionMode::Static)",
            "TypedStyle::new().position(PositionMode::Fixed)",
            Layout::POSITION,
        ),
        Some(Position::Fixed)
    );

    // Newer disjoint builder methods must not block unrelated Tier-1 edits in the same chain.
    let disjoint_old = "TypedStyle::new()\
        .user_select(UserSelect::None)\
        .overscroll_behavior(OverscrollBehavior::Contain)\
        .overscroll_behavior_x(OverscrollBehavior::None)\
        .overscroll_behavior_y(OverscrollBehavior::Auto)\
        .overflow_anchor(OverflowAnchor::None)\
        .container_inline_size()\
        .at_container(ContainerQuery::min_width(400.0), patch)\
        .capture_on_primary_down()\
        .pointer_events_none()\
        .on_drag_over(drag_patch)\
        .drag_over(drag_patch)\
        .padding(8.0)";
    let disjoint_new = disjoint_old.replace(".padding(8.0)", ".padding(16.0)");
    assert_eq!(
        style_patch_prop(disjoint_old, &disjoint_new, Layout::PADDING_TOP),
        Some(Length::points(16.0))
    );

    // `z_index_auto`, `position_static`, and `position_fixed` shadow earlier setters on their slots.
    for (old, new) in [
        (
            "TypedStyle::new().z_index(10).z_index_auto()",
            "TypedStyle::new().z_index(20).z_index_auto()",
        ),
        (
            "TypedStyle::new().position(Position::Relative).position_fixed()",
            "TypedStyle::new().position(Position::Absolute).position_fixed()",
        ),
        (
            "TypedStyle::new().position(Position::Relative).position_static()",
            "TypedStyle::new().position(Position::Absolute).position_static()",
        ),
    ] {
        let reason = expect_tier2(diff_view_source(
            "src/panel.rs",
            &node_with_style(old),
            &node_with_style(new),
            1,
        ));
        assert!(!reason.is_empty());
    }
}