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")
};
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));
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));
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));
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")
);
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())]
);
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)
);
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))
);
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());
}
}