From d243347fd08325847e84ff717c3eb346fc672108 Mon Sep 17 00:00:00 2001 From: B-uchi Date: Thu, 19 Nov 2026 01:54:56 +0100 Subject: [PATCH] M8: Code Connect keeps a connect file it can't read, and maps values as it checks them - Export refuses to write over a connect file it can't read (not JSON, not an object, or over 4 MB) instead of replacing it with this design's links alone. - Import skips an entry whose `values` is malformed, with a warning, instead of replacing the component's link with one that maps nothing. - `{Property ? "a" : "b"}` reads the design's value, so mapping Off to `inactive` doesn't make the condition true. - A `values` key that is only a parameter name (`state` for State) is reported as unknown, since the mapping is read by property name and it would do nothing. --- crates/engine/src/code_connect_tests.rs | 83 +++++++++++++++++++++++++ crates/engine/src/dev/code_connect.rs | 39 ++++++++---- docs/mcp.md | 9 +-- 3 files changed, 115 insertions(+), 16 deletions(-) diff --git a/crates/engine/src/code_connect_tests.rs b/crates/engine/src/code_connect_tests.rs index 9750db813..f28aa882c 100644 --- a/crates/engine/src/code_connect_tests.rs +++ b/crates/engine/src/code_connect_tests.rs @@ -419,3 +419,86 @@ fn listing_refuses_a_malformed_target_rather_than_reading_the_selection() { assert!(s.execute("codeConnect.list", &p).is_err(), "{p}"); } } + +/// A set called `name` with a variant `prop=value` for each of `values`. Returns (set, variants). +fn set_of(s: &mut Session, name: &str, prop: &str, values: &[&str]) -> (NodeId, Vec) { + let variants: Vec = values + .iter() + .enumerate() + .map(|(k, v)| { + let f = idv(&ex(s, "frame.create", json!({"x": k as f64 * 150.0, "y": 600, "width": 100, "height": 40, "name": format!("{prop}={v}")}))); + idv(&ex(s, "component.create", json!({"ids": [f]}))) + }) + .collect(); + let set = idv(&ex(s, "component.combineVariants", json!({"ids": variants.clone()}))); + ex(s, "node.set", json!({"id": set, "props": {"name": name}})); + (set, variants) +} + +#[test] +fn a_condition_reads_the_designs_value_not_its_name_in_code() { + let mut s = Session::new(); + ex(&mut s, "file.new", json!({})); + let (set, variants) = set_of(&mut s, "Toggle", "State", &["On", "Off"]); + ex(&mut s, "codeConnect.set", json!({"id": set, "name": "Toggle", "template": "{State ? \"enabled\" : \"disabled\"} {State}"})); + let off = idv(&ex(&mut s, "instance.create", json!({"component": variants[1]}))); + assert!(code(&mut s, off, "react").starts_with("disabled"), "{}", code(&mut s, off, "react")); + // `inactive` reads as true, but the condition is about the design's Off. + ex(&mut s, "codeConnect.set", json!({"id": set, "values": {"State": {"Off": "inactive"}}})); + let out = code(&mut s, off, "react"); + assert!(out.starts_with("disabled") && out.contains("inactive"), "{out}"); +} + +#[test] +fn a_mapping_keyed_by_a_name_it_cant_use_is_reported() { + let mut s = Session::new(); + ex(&mut s, "file.new", json!({})); + let (set, hover) = button(&mut s); + let r = ex( + &mut s, + "codeConnect.set", + json!({"id": set, "name": "Button", "template": "