feat(nakui): Fase 1 del ERP — FieldKind Select + AutoId, seed inyecta id

Primera fase del plan maestro. La metainterfaz gana dos tipos de campo:
Select (chips de un conjunto cerrado, con options validadas) y AutoId
(UUID autogenerado read-only). NakuiBackend::seed inyecta el id de la
entity = clave del store. El módulo CRM los adopta: etapa/canal son
selects, los ids de idempotencia se autogeneran, el form de cliente ya
no pide id. Ningún formulario pide un UUID a mano.

Tests en meta-schema, meta-runtime y nakui-ui verdes.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
sergio
2026-05-21 18:55:13 +00:00
parent 0d1e378e42
commit 86d06da020
12 changed files with 442 additions and 210 deletions
@@ -137,7 +137,10 @@ mod tests {
use serde_json::json;
fn map_of(items: &[(&str, Value)]) -> serde_json::Map<String, Value> {
items.iter().map(|(k, v)| (k.to_string(), v.clone())).collect()
items
.iter()
.map(|(k, v)| (k.to_string(), v.clone()))
.collect()
}
#[test]
@@ -180,7 +183,10 @@ mod tests {
fn update_with_set_changes_field() {
let mut b = MockBackend::new();
let id = b
.seed("Customer", map_of(&[("name", json!("Acme")), ("notes", json!("x"))]))
.seed(
"Customer",
map_of(&[("name", json!("Acme")), ("notes", json!("x"))]),
)
.unwrap()
.id
.unwrap();
@@ -205,7 +211,10 @@ mod tests {
fn update_with_clear_removes_key() {
let mut b = MockBackend::new();
let id = b
.seed("Customer", map_of(&[("name", json!("Acme")), ("notes", json!("x"))]))
.seed(
"Customer",
map_of(&[("name", json!("Acme")), ("notes", json!("x"))]),
)
.unwrap()
.id
.unwrap();
@@ -50,7 +50,10 @@ mod tests {
use serde_json::json;
fn map(items: &[(&str, Value)]) -> serde_json::Map<String, Value> {
items.iter().map(|(k, v)| (k.to_string(), v.clone())).collect()
items
.iter()
.map(|(k, v)| (k.to_string(), v.clone()))
.collect()
}
#[test]
@@ -15,15 +15,20 @@ use nahual_meta_schema::{FieldKind, FieldSpec};
/// - `Number` → i64 si parsea, sino f64.
pub fn parse_field_value(kind: FieldKind, raw: &str) -> Result<Value, String> {
match kind {
FieldKind::Text | FieldKind::Multiline | FieldKind::Date => Ok(json!(raw)),
// Select y AutoId guardan un string: el valor de la opción
// elegida y el UUID autogenerado, respectivamente.
FieldKind::Text
| FieldKind::Multiline
| FieldKind::Date
| FieldKind::Select
| FieldKind::AutoId => Ok(json!(raw)),
// EntityRef se almacena como string del UUID seleccionado.
// El selector clickable garantiza UUIDs válidos en happy
// path; este check protege paste manual o garbage tipeado.
FieldKind::EntityRef => {
let trimmed = raw.trim();
Uuid::parse_str(trimmed).map_err(|_| {
format!("'{raw}' no es UUID válido (usá el selector de records)")
})?;
Uuid::parse_str(trimmed)
.map_err(|_| format!("'{raw}' no es UUID válido (usá el selector de records)"))?;
Ok(json!(trimmed))
}
FieldKind::Boolean => match raw.to_ascii_lowercase().as_str() {
@@ -62,7 +67,11 @@ pub fn resolve_param_value(
return Ok(infer_param_value(raw));
};
let label = if s.label.is_empty() { field_name } else { &s.label };
let label = if s.label.is_empty() {
field_name
} else {
&s.label
};
if s.required && raw.trim().is_empty() {
return Err(format!("param '{label}' es obligatorio y está vacío"));
@@ -112,6 +121,7 @@ mod tests {
required,
help: None,
ref_entity: None,
options: Vec::new(),
}
}
@@ -119,7 +129,7 @@ mod tests {
fn infer_handles_basic_types() {
assert_eq!(infer_param_value(""), Value::Null);
assert_eq!(infer_param_value("42"), json!(42));
assert_eq!(infer_param_value("3.14"), json!(3.14));
assert_eq!(infer_param_value("2.5"), json!(2.5));
assert_eq!(infer_param_value("true"), json!(true));
assert_eq!(infer_param_value("false"), json!(false));
assert_eq!(infer_param_value("hola"), json!("hola"));
@@ -132,11 +142,29 @@ mod tests {
}
#[test]
fn parse_number_i64_or_f64() {
assert_eq!(parse_field_value(FieldKind::Number, "42").unwrap(), json!(42));
fn parse_select_and_auto_id_passthrough() {
// Select guarda el valor de la opción elegida.
assert_eq!(
parse_field_value(FieldKind::Number, "3.14").unwrap(),
json!(3.14)
parse_field_value(FieldKind::Select, "ganada").unwrap(),
json!("ganada")
);
// AutoId guarda el UUID autogenerado tal cual.
let id = Uuid::new_v4().to_string();
assert_eq!(
parse_field_value(FieldKind::AutoId, &id).unwrap(),
json!(id)
);
}
#[test]
fn parse_number_i64_or_f64() {
assert_eq!(
parse_field_value(FieldKind::Number, "42").unwrap(),
json!(42)
);
assert_eq!(
parse_field_value(FieldKind::Number, "2.5").unwrap(),
json!(2.5)
);
assert!(parse_field_value(FieldKind::Number, "abc").is_err());
}
@@ -144,7 +172,10 @@ mod tests {
#[test]
fn parse_boolean_recognizes_variants() {
for s in ["true", "yes", "1", "on", "y"] {
assert_eq!(parse_field_value(FieldKind::Boolean, s).unwrap(), json!(true));
assert_eq!(
parse_field_value(FieldKind::Boolean, s).unwrap(),
json!(true)
);
}
for s in ["false", "no", "0", "off", "n", ""] {
assert_eq!(
@@ -204,30 +204,24 @@ mod tests {
use serde_json::json;
fn map_of(items: &[(&str, Value)]) -> serde_json::Map<String, Value> {
items.iter().map(|(k, v)| (k.to_string(), v.clone())).collect()
items
.iter()
.map(|(k, v)| (k.to_string(), v.clone()))
.collect()
}
#[test]
fn with_records_populates_state() {
let id = Uuid::new_v4();
let b = MockBackend::with_records([(
"Customer".into(),
id,
json!({"name": "Acme"}),
)]);
let b = MockBackend::with_records([("Customer".into(), id, json!({"name": "Acme"}))]);
assert_eq!(b.total_records(), 1);
assert_eq!(
b.load_record("Customer", id),
Some(json!({"name": "Acme"}))
);
assert_eq!(b.load_record("Customer", id), Some(json!({"name": "Acme"})));
}
#[test]
fn seed_then_load_round_trip_via_trait() {
let mut b = MockBackend::new();
let out = b
.seed("X", map_of(&[("k", json!(1))]))
.unwrap();
let out = b.seed("X", map_of(&[("k", json!(1))])).unwrap();
let id = out.id.unwrap();
assert_eq!(out.changed, 1);
assert_eq!(b.load_record("X", id), Some(json!({"k": 1})));
@@ -236,25 +230,15 @@ mod tests {
#[test]
fn update_no_op_returns_no_change() {
let id = Uuid::new_v4();
let mut b = MockBackend::with_records([(
"X".into(),
id,
json!({"k": 1}),
)]);
let out = b
.update("X", id, serde_json::Map::new(), vec![])
.unwrap();
let mut b = MockBackend::with_records([("X".into(), id, json!({"k": 1}))]);
let out = b.update("X", id, serde_json::Map::new(), vec![]).unwrap();
assert_eq!(out, WriteOutcome::no_change(id));
}
#[test]
fn update_set_and_clear_aplica_ambos() {
let id = Uuid::new_v4();
let mut b = MockBackend::with_records([(
"X".into(),
id,
json!({"a": 1, "b": 2}),
)]);
let mut b = MockBackend::with_records([("X".into(), id, json!({"a": 1, "b": 2}))]);
let out = b
.update("X", id, map_of(&[("a", json!(10))]), vec!["b".into()])
.unwrap();
@@ -267,11 +251,7 @@ mod tests {
#[test]
fn delete_then_load_returns_none() {
let id = Uuid::new_v4();
let mut b = MockBackend::with_records([(
"X".into(),
id,
json!({"k": 1}),
)]);
let mut b = MockBackend::with_records([("X".into(), id, json!({"k": 1}))]);
b.delete("X", id).unwrap();
assert!(b.load_record("X", id).is_none());
}
@@ -287,17 +267,14 @@ mod tests {
#[test]
fn with_morphism_lets_caller_simulate_logic() {
let mut b = MockBackend::new().with_morphism(
"double_qty",
|inputs, params| {
assert!(inputs.is_empty());
let qty = params.get("qty").and_then(|v| v.as_i64()).unwrap_or(0);
if qty <= 0 {
return Err("qty must be positive".into());
}
Ok(qty as usize)
},
);
let mut b = MockBackend::new().with_morphism("double_qty", |inputs, params| {
assert!(inputs.is_empty());
let qty = params.get("qty").and_then(|v| v.as_i64()).unwrap_or(0);
if qty <= 0 {
return Err("qty must be positive".into());
}
Ok(qty as usize)
});
let out = b
.morphism("mod", "double_qty", BTreeMap::new(), json!({"qty": 7}))
.unwrap();
@@ -326,11 +303,7 @@ mod tests {
#[test]
fn records_for_returns_borrowed_view() {
let id = Uuid::new_v4();
let b = MockBackend::with_records([(
"X".into(),
id,
json!({"k": 1}),
)]);
let b = MockBackend::with_records([("X".into(), id, json!({"k": 1}))]);
let view = b.records_for("X");
assert_eq!(view.len(), 1);
assert_eq!(view[0].0, id);