diff --git a/crates/engine/src/cmd/layout.rs b/crates/engine/src/cmd/layout.rs index 2a230aa271..6e88e95455 100644 --- a/crates/engine/src/cmd/layout.rs +++ b/crates/engine/src/cmd/layout.rs @@ -444,10 +444,10 @@ pub fn commands() -> Vec { }).inputs(object_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .params(r#"{"layout"?, "element": id}"#), C::new("layout.frameZoom", "Zoom Frame", "Publish › Map Frame", frame_zoom).inputs(frame_zoom_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params( - r#"{"layout"?, "element"?: frame id, "frame"?: (as element), "to"?: "view" (the map view's extent, bearing and pitch; the default)|"full"|"selection"|"layer", "layer"?}"#, + r#"{"layout"?, "element"?: frame id, "frame"?: (as element), "to"?: "view" (the map view's extent, bearing and pitch; the default)|"full"|"selection"|"layer", "layer"?: name|id}"#, ), C::new("layout.series", "Map Series", "Publish › Map Frames", series).inputs(series_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params( - r#"{"layout"?, "layer": index layer, "nameField"?: string, "frame"?: id, "margin"?: 0..1, "off"?: bool}"#, + r#"{"layout"?, "layer": index layer name|id, "nameField"?: string, "frame"?: id, "margin"?: 0..1, "off"?: bool}"#, ), C::new("layout.seriesPage", "Map Series Page", "Publish › Map Frames", series_page).inputs(series_page_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params(r#"{"layout"?, "page": number|"next"|"previous"}"#), C::new("layout.seriesRefresh", "Refresh Map Series", "Publish › Map Frames", |s, p| series_page(s, &json!({"layout": p.get("layout"), "page": "current"}))).inputs(series_page_inputs).roles(&[]).effects(crate::policy::Effects::NONE) diff --git a/crates/engine/src/cmd/layout/frame.rs b/crates/engine/src/cmd/layout/frame.rs index 4581605756..05a21dd593 100644 --- a/crates/engine/src/cmd/layout/frame.rs +++ b/crates/engine/src/cmd/layout/frame.rs @@ -423,8 +423,7 @@ pub(in crate::cmd::layout) fn frame_zoom(s: &mut Session, p: &Value) -> CmdResul set_frame(s, li, frame, &e)?; } "layer" => { - let n = ps(p, "layer").ok_or("layer is required")?; - let e = m.layer_by_name(n).ok_or_else(|| format!("no layer '{n}'"))?.shown_extent(); + let e = crate::cmd::given_layer(m, p, "layer").ok_or("layer is required")??.shown_extent(); set_frame(s, li, frame, &e)?; } o => return Err(format!("unknown zoom '{o}'")), diff --git a/crates/engine/src/cmd/layout/inputs.rs b/crates/engine/src/cmd/layout/inputs.rs index d539f70459..4005e82001 100644 --- a/crates/engine/src/cmd/layout/inputs.rs +++ b/crates/engine/src/cmd/layout/inputs.rs @@ -44,7 +44,7 @@ pub(in crate::cmd::layout) fn frame_zoom_inputs(input: &crate::cmd::InputContext let frame = zoom_frame(layout, p)?; let map = frame_map(input, layout, frame)?; let mut inputs = match ps(p, "to").unwrap_or("view") { - "layer" => crate::cmd::Inputs::named(map, [ps(p, "layer").ok_or("layer is required")?])?, + "layer" => crate::cmd::Inputs::given(map, p, "layer")?.ok_or("layer is required")?, "full" | "selection" => crate::cmd::Inputs::layers(map.layers.iter().map(|l| (map, l))), _ => crate::cmd::Inputs::layers(std::iter::empty()), }; @@ -61,7 +61,7 @@ pub(in crate::cmd::layout) fn series_inputs(input: &crate::cmd::InputContext<'_> let layout = s.project.layouts.get(index(input, p)?).ok_or("no layout")?; let frame = frame_of(layout, p)?; let map = frame_map(input, layout, frame)?; - let mut inputs = crate::cmd::Inputs::named(map, [ps(p, "layer").ok_or("layer is required")?])?; + let mut inputs = crate::cmd::Inputs::given(map, p, "layer")?.ok_or("layer is required")?; inputs.maps.push(map.instance_id()); inputs.state = json!({"layout":index(input,p)?,"frame":frame}); Ok(inputs) diff --git a/crates/engine/src/cmd/layout/series.rs b/crates/engine/src/cmd/layout/series.rs index 92125d12ae..f8ffe9b4a2 100644 --- a/crates/engine/src/cmd/layout/series.rs +++ b/crates/engine/src/cmd/layout/series.rs @@ -16,9 +16,11 @@ pub(in crate::cmd::layout) fn series(s: &mut Session, p: &Value) -> CmdResult { Some(ElementKind::MapFrame { map, .. }) => map.clone(), _ => return Err("no such frame".into()), }; - let n = ps(p, "layer").ok_or("layer is required")?; let m = frame_map(s, l, frame)?; - let layer = m.layer_by_name(n).filter(|l| !l.is_raster()).ok_or_else(|| format!("no feature layer '{n}' in {map}"))?; + let layer = crate::cmd::given_layer(m, p, "layer").ok_or("layer is required")??; + if layer.is_raster() { + return Err(format!("no feature layer '{}' in {map}", layer.name)); + } rustgis_map::layout::require_series_index(layer)?; if layer.dataset.features.is_empty() { return Err("the index layer has no features".into()); diff --git a/crates/engine/src/cmd/mod.rs b/crates/engine/src/cmd/mod.rs index e4a32eee91..e2c6b2ccec 100644 --- a/crates/engine/src/cmd/mod.rs +++ b/crates/engine/src/cmd/mod.rs @@ -597,18 +597,9 @@ impl<'a> InputContext<'a> { /// Resolves numeric, name-before-id and default layer choices for both planner and executor. pub fn layer_id(&self, p: &Value) -> Result { let map = self.map(); - match p.get("layer") { - Some(Value::Number(n)) => n - .as_u64() - .and_then(|n| u32::try_from(n).ok()) - .filter(|id| map.layer(*id).is_some()) - .ok_or_else(|| format!("no layer with id {n}")), - Some(Value::String(s)) => map - .layer_by_name(s) - .map(|l| l.id) - .or_else(|| s.parse::().ok().filter(|id| map.layer(*id).is_some())) - .ok_or_else(|| format!("no layer named '{s}'")), - _ => { + match given_layer(map, p, "layer") { + Some(layer) => layer.map(|l| l.id), + None => { self.active_layer.filter(|id| map.layer(*id).is_some()).ok_or_else(|| "select a layer in the Layers pane first".to_string()) } } @@ -619,6 +610,20 @@ impl<'a> InputContext<'a> { self.map().layers.iter().map(|l| l.id).filter(|id| picked.contains(id) || self.active_layer == Some(*id)).collect() } } +/// The layer of `map` that `p[key]` gives by its id, its name, or its id as text, as +/// [`InputContext::layer_id`] reads `layer` (#3509); `None` when `key` gives neither. +pub(crate) fn given_layer<'a>(map: &'a rustgis_map::Map, p: &Value, key: &str) -> Option> { + Some(match p.get(key)? { + Value::Number(n) => { + n.as_u64().and_then(|n| u32::try_from(n).ok()).and_then(|id| map.layer(id)).ok_or_else(|| format!("no layer with id {n}")) + } + Value::String(s) => map + .layer_by_name(s) + .or_else(|| s.parse::().ok().and_then(|id| map.layer(id))) + .ok_or_else(|| format!("no layer named '{s}'")), + _ => return None, + }) +} /// Only scalar choices a pure owner transition can model without running a handler. #[derive(Clone, Copy)] pub(crate) struct Selection { @@ -684,6 +689,15 @@ impl Inputs { Ok(inputs) } + /// Binds the layer `p[key]` gives, as [`given_layer`] reads it: a name may also be a layer + /// a batch has yet to add, as [`Inputs::named`] binds it; an id names a layer already there. + pub fn given(map: &rustgis_map::Map, p: &serde_json::Value, key: &str) -> Result, String> { + match (given_layer(map, p, key), p.get(key)) { + (Some(Err(_)), Some(serde_json::Value::String(name))) => Self::named(map, [name.as_str()]).map(Some), + (found, _) => found.transpose().map(|l| l.map(|l| Self::layers([(map, l)]))), + } + } + /// The ordinary primary selector uses Session's exact numeric/name/default semantics. pub fn primary(s: &impl InputSource, p: &serde_json::Value) -> Result { let context = s.input_context(); diff --git a/crates/engine/src/cmd/report.rs b/crates/engine/src/cmd/report.rs index a3ec993d8f..b1b38bb8e4 100644 --- a/crates/engine/src/cmd/report.rs +++ b/crates/engine/src/cmd/report.rs @@ -36,10 +36,10 @@ fn need_report(s: &Session) -> Option<&'static str> { pub fn commands() -> Vec { vec![ C::new("report.new", "New Report", "Publish › Report", new_report).inputs(new_inputs).roles(&[]).effects(crate::policy::Effects::NONE).params( - r#"{"layer"?: name (default: the selected layer), "name"?, "title"?, "fields"?: [names] (default: all), "where"?: sql, "selectedOnly"?: bool, "groups"?: [fields], "sort"?: field, "descending"?: bool, "page"?: "letter"|"a4"|…, "orientation"?: "portrait"|"landscape"}"#, + r#"{"layer"?: name|id (default: the selected layer), "name"?, "title"?, "fields"?: [names] (default: all), "where"?: sql, "selectedOnly"?: bool, "groups"?: [fields], "sort"?: field, "descending"?: bool, "page"?: "letter"|"a4"|…, "orientation"?: "portrait"|"landscape"}"#, ), C::new("report.update", "Report Properties", "Report", update).inputs(update_inputs).roles(&[]).effects(crate::policy::Effects::NONE).when(need_report).params( - r#"{"report"?: name|index, "name"?, "title"?, "layer"?, "where"?: sql, "selectedOnly"?, "sort"?: field|null, "descending"?, "page"?, "orientation"?, "width"?, "height"?: mm}"#, + r#"{"report"?: name|index, "name"?, "title"?, "layer"?: name|id, "where"?: sql, "selectedOnly"?, "sort"?: field|null, "descending"?, "page"?, "orientation"?, "width"?, "height"?: mm}"#, ), C::new("report.activate", "Open Report", "Browser", |s, p| { let i = index(s, p)?; @@ -253,7 +253,7 @@ pub fn commands() -> Vec { .when(need_report) .params(r#"{"report"?, "path": "*.rgisrpttemplate"}"#), C::new("report.open", "Import Report", "Publish › Report", open).inputs(open_inputs).roles(&[]).effects(crate::policy::Effects::NONE.with(crate::policy::Effect::Files)).params( - r#"{"path"?: "*.rgisreport|*.rgisrpttemplate", "text"?: report JSON, "data"?: base64 file, "layer"?: the layer a template reports on (default: the selected layer)}"#, + r#"{"path"?: "*.rgisreport|*.rgisrpttemplate", "text"?: report JSON, "data"?: base64 file, "layer"?: name|id of the layer a template reports on (default: the selected layer)}"#, ), ] } @@ -648,8 +648,8 @@ fn update(s: &mut Session, p: &Value) -> CmdResult { let old_name = r.name.clone(); let mut renamed = None; let mut fitted = Fitted::default(); - if let Some(n) = ps(p, "layer") { - let l = find_layer(s, n).ok_or_else(|| format!("no layer named '{n}'"))?; + if let Some(found) = given_layer_map(s, p) { + let l = found?.1; if l.is_raster() { return Err("reports list a feature layer's records, not a raster's".into()); } @@ -1562,8 +1562,8 @@ fn say_report_clipped(s: &mut Session, clipped: &ClippedTexts) { fn new_layer<'a>(input: &'a impl super::InputSource, p: &Value) -> Result<&'a Layer, String> { let context = input.input_context(); let s = &context; - Ok(match ps(p, "layer") { - Some(n) => find_layer(input, n).ok_or_else(|| format!("no layer named '{n}'"))?, + Ok(match given_layer_map(input, p) { + Some(found) => found?.1, None => s.active_layer.and_then(|id| s.map().layer(id)).ok_or("select a layer, or pass layer")?, }) } @@ -1572,8 +1572,8 @@ fn new_layer<'a>(input: &'a impl super::InputSource, p: &Value) -> Result<&'a La fn import_layer(input: &impl super::InputSource, p: &Value, r: &mut Report) -> Result<(), String> { let context = input.input_context(); let s = &context; - if let Some(n) = ps(p, "layer") { - r.layer = find_layer(input, n).ok_or_else(|| format!("no layer named '{n}'"))?.name.clone(); + if let Some(found) = given_layer_map(input, p) { + r.layer = found?.1.name.clone(); } else if find_layer(input, &r.layer).is_none() { // A template (or a report on a layer this project lacks) reports on the selected layer. r.layer = s @@ -1597,6 +1597,20 @@ pub(super) fn named_elsewhere(s: &Session, name: &str) -> bool { s.project.maps.iter().filter(|m| !std::ptr::eq(*m, s.map())).any(|m| m.layer_by_name(name).is_some()) } +/// The layer `layer` gives: by name as [`find_layer_map`] finds it, else by its id (or its id as +/// text) in the active map, as `Session::layer_id` reads it (#3509); `None` when not given. +fn given_layer_map<'a>(input: &'a impl super::InputSource, p: &Value) -> Option> { + let context = input.input_context(); + let map = context.map(); + match p.get("layer")? { + Value::String(name) => { + find_layer_map(input, name).map(Ok).or_else(|| super::given_layer(map, p, "layer").map(|l| l.map(|l| (map, l)))) + } + Value::Number(_) => super::given_layer(map, p, "layer").map(|l| l.map(|l| (map, l))), + _ => None, + } +} + /// Finds a literal feature layer active-map first, then in project order. fn find_layer_map<'a>(input: &'a impl super::InputSource, name: &str) -> Option<(&'a rustgis_map::Map, &'a Layer)> { let context = input.input_context(); @@ -1691,8 +1705,8 @@ fn update_inputs(input: &super::InputContext<'_>, p: &Value) -> Result, p: &Value) -> Result, p: &Value) -> Result { - if let Some(name) = ps(p, "layer") { - return Ok(super::Inputs::layers([find_layer_map(input, name).ok_or_else(|| format!("no layer named '{name}'"))?])); + if let Some(found) = given_layer_map(input, p) { + return Ok(super::Inputs::layers([found?])); } let Some(text) = super::items::inline_text(p)? else { return Ok(super::Inputs::unresolved()); diff --git a/crates/engine/src/cmd/workspace.rs b/crates/engine/src/cmd/workspace.rs index e2d09e1386..754c896f22 100644 --- a/crates/engine/src/cmd/workspace.rs +++ b/crates/engine/src/cmd/workspace.rs @@ -186,7 +186,7 @@ pub fn commands() -> Vec { profile(s, p, &pts) }).roles(&[]).effects(crate::policy::Effects::NONE) .view() - .params(r#"{"layer": elevation raster, "points": [[lon, lat], …], "samples"?: number}"#), + .params(r#"{"layer": elevation raster name|id, "points": [[lon, lat], …], "samples"?: number}"#), C::new("profile.autoComplete", "Auto-complete Profile", "View › Profile Viewing", |s, p| { // Continue the line from its first point through its second to the raster's edge. let pts = points(p)?; @@ -204,7 +204,7 @@ pub fn commands() -> Vec { profile(s, p, &[a, Coord::new(a.x + dx * t, a.y + dy * t)]) }).roles(&[]).effects(crate::policy::Effects::NONE) .view() - .params(r#"{"layer": elevation raster, "samples"?: number (default 200), "points": [[lon, lat], [lon, lat]] (a start and a direction)}"#), + .params(r#"{"layer": elevation raster name|id, "samples"?: number (default 200), "points": [[lon, lat], [lon, lat]] (a start and a direction)}"#), C::new("profile.crossSection", "Cross Section", "View › Profile Viewing", |s, p| { // Across a line at a point: perpendicular, `width` metres long. let pts = points(p)?; @@ -221,7 +221,7 @@ pub fn commands() -> Vec { }).roles(&[]).effects(crate::policy::Effects::NONE) .view() .params( - r#"{"layer": elevation raster, "samples"?: number (default 200), "points": [[lon, lat], [lon, lat]] (the line crossed), "at"?: 0..1 along it, "width"?: metres}"#, + r#"{"layer": elevation raster name|id, "samples"?: number (default 200), "points": [[lon, lat], [lon, lat]] (the line crossed), "at"?: 0..1 along it, "width"?: metres}"#, ), C::new("profile.alongLine", "Profile Along a Line", "View › Profile Viewing", |s, p| { let (lines, _) = selected_lines(s, p, false)?; @@ -229,7 +229,7 @@ pub fn commands() -> Vec { profile(s, p, &first) }).inputs(profile_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .view() - .params(r#"{"layer": elevation raster, "samples"?: number (default 200), "lines": line layer, "row"?: number (the first selected when omitted)}"#), + .params(r#"{"layer": elevation raster name|id, "samples"?: number (default 200), "lines": line layer, "row"?: number (the first selected when omitted)}"#), C::new("profile.alongSelected", "Profile Along Selected Lines", "View › Profile Viewing", |s, p| { let (lines, unplaced) = selected_lines(s, p, true)?; // The lines past the first 50 get no profile, and that is said (#1896). @@ -248,11 +248,11 @@ pub fn commands() -> Vec { Ok(Value::Array(out)) }).inputs(profile_inputs).roles(&[]).effects(crate::policy::Effects::NONE) .view() - .params(r#"{"layer": elevation raster, "samples"?: number (default 200), "lines": line layer}"#), + .params(r#"{"layer": elevation raster name|id, "samples"?: number (default 200), "lines": line layer}"#), // Infographics and diagnostics. C::new("info.infographic", "Infographics", "Home › Inspect", infographic).roles(&[]).effects(crate::policy::Effects::NONE) .view() - .params(r#"{"layer"? (every visible feature layer when omitted)}"#), + .params(r#"{"layer"?: name|id (every visible feature layer when omitted)}"#), C::new("info.diagnostics", "Diagnostic Monitor", "Help › Performance", |s, _| Ok(diagnostics(s))).roles(&[]).effects(crate::policy::Effects::NONE).view(), // Offline areas. C::new("offline.download", "Download Map", "Data › Offline", offline_download).roles(&[]).effects(crate::policy::Effects::NONE) @@ -561,8 +561,8 @@ fn points(p: &Value) -> Result, String> { } fn dem(s: &Session, p: &Value) -> Result, String> { - let n = ps(p, "layer").ok_or("layer (an elevation raster) is required")?; - s.map().layer_by_name(n).and_then(|l| l.raster.as_ref()).and_then(|r| r.data.clone()).ok_or_else(|| format!("no raster layer '{n}'")) + let l = super::given_layer(s.map(), p, "layer").ok_or("layer (an elevation raster) is required")??; + l.raster.as_ref().and_then(|r| r.data.clone()).ok_or_else(|| format!("no raster layer '{}'", l.name)) } /// An elevation profile with its summary. @@ -714,7 +714,13 @@ pub fn diagnostics(s: &Session) -> Value { } fn infographic(s: &mut Session, p: &Value) -> CmdResult { - Ok(infographic_of(s, ps(p, "layer"))) + // A layer's id, or its id as text, reads as its name (#3509); a name the map lacks still + // gives no cards. + let only = match (super::given_layer(s.map(), p, "layer"), ps(p, "layer")) { + (Some(Err(_)), Some(name)) => Some(name.to_string()), + (found, _) => found.transpose()?.map(|l| l.name.clone()), + }; + Ok(infographic_of(s, only.as_deref())) } /// Cards summarising the features in view, for every visible feature layer or just `only`. @@ -1436,7 +1442,9 @@ pub(crate) fn area_map_index(project: &Project, ai: usize) -> Option { /// Binds the elevation raster and literal selected-line source. fn profile_inputs(input: &super::InputContext<'_>, p: &Value) -> Result { let s = input; - super::Inputs::named(s.map(), [super::imagery::raster_name(p)?, profile_lines(p)?]) + let mut inputs = super::Inputs::given(s.map(), p, "layer")?.ok_or("layer is required")?; + inputs.extend(super::Inputs::named(s.map(), [profile_lines(p)?])?); + Ok(inputs) } /// Parses the same required line-layer name used by profile extraction. diff --git a/crates/engine/src/policy/plan.rs b/crates/engine/src/policy/plan.rs index 9326f965cc..b8eddfa503 100644 --- a/crates/engine/src/policy/plan.rs +++ b/crates/engine/src/policy/plan.rs @@ -1113,10 +1113,10 @@ impl<'a> Builder<'a> { { Err(error) } - Err(_) => value - .as_str() - .map(|n| Target::MadeLayer { map: self.context().map().instance_id(), name: n.into() }) - .ok_or("unresolved layer target".into()), + // An id names a layer already there: its error is the resolver's (#3509). + Err(error) => { + value.as_str().map(|n| Target::MadeLayer { map: self.context().map().instance_id(), name: n.into() }).ok_or(error) + } } } /// An approved rename retains the original incarnation and adds its source when later read. diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 01f12864e8..a0ae39fc7a 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -185,6 +185,9 @@ mod layout_publication_crs; #[path = "tests/layout_frame_scale.rs"] mod layout_frame_scale; +#[path = "tests/layer_ids.rs"] +mod layer_ids; + #[path = "tests/layout_table.rs"] mod layout_table; diff --git a/crates/engine/src/tests/layer_ids.rs b/crates/engine/src/tests/layer_ids.rs new file mode 100644 index 0000000000..1ffb04de7e --- /dev/null +++ b/crates/engine/src/tests/layer_ids.rs @@ -0,0 +1,92 @@ +//! Commands that read `layer` as a map layer take the layer's id as well as its name, as +//! `Session::layer_id` reads it (#3509): `layer.addData` answers the id, and callers pass it on. + +use super::*; + +/// The sample world with the sample elevation raster added, and that raster's id as added. +fn with_dem() -> (Session, u32) { + let mut s = world(); + let added = s.run("layer.addDataBytes", &json!({"fileName": "dem.tif", "data": dem_b64()})).unwrap(); + let id = added["layer"].as_u64().unwrap() as u32; + (s, id) +} + +/// What `command` leaves (its answer and the project's layouts and reports) with `layer` given +/// first by `pick`'s name, then by its id, each in a new session. +fn by_name_and_id(pick: fn(&Session, u32) -> u32, setup: &[(&str, Value)], command: &str, params: Value) -> [Value; 2] { + [false, true].map(|by_id| { + let (mut s, dem) = with_dem(); + for (c, p) in setup { + s.run(c, p).unwrap(); + } + let id = pick(&s, dem); + let layer = if by_id { json!(id) } else { json!(s.map().layer(id).unwrap().name) }; + let mut p = params.clone(); + p["layer"] = layer; + let answer = s.run(command, &p).unwrap(); + json!({"answer": answer, "layouts": s.project.layouts, "reports": s.project.reports}) + }) +} + +fn dem(_: &Session, dem: u32) -> u32 { + dem +} + +fn countries(s: &Session, _: u32) -> u32 { + s.map().layer_by_name("Countries").unwrap().id +} + +#[test] +fn a_profile_takes_the_raster_id_layer_add_data_answered() { + let [named, by_id] = by_name_and_id(dem, &[], "profile.create", json!({"points": [[10.0015, 45.0195], [10.0385, 45.0195]]})); + assert!(named["answer"]["max"].as_f64().is_some(), "{named}"); + assert_eq!(named, by_id); +} + +#[test] +fn a_frame_zooms_to_a_layer_given_by_id() { + let layout = [("layout.new", json!({"page": "a4", "template": "map"}))]; + let [named, by_id] = by_name_and_id(countries, &layout, "layout.frameZoom", json!({"to": "layer"})); + assert_eq!(named, by_id); +} + +#[test] +fn a_map_series_takes_its_index_layer_by_id() { + let layout = [("layout.new", json!({"page": "a4", "template": "map"}))]; + let [named, by_id] = by_name_and_id(countries, &layout, "layout.series", json!({"nameField": "name"})); + assert_eq!(named, by_id); +} + +#[test] +fn a_report_takes_its_layer_by_id() { + let [named, by_id] = by_name_and_id(countries, &[], "report.new", json!({"name": "Atlas", "fields": ["name"]})); + assert_eq!(named["reports"][0]["layer"], "Countries"); + assert_eq!(named, by_id); +} + +#[test] +fn an_unknown_id_is_said_as_such() { + let (mut s, _) = with_dem(); + s.run("layout.new", &json!({"page": "a4", "template": "map"})).unwrap(); + for (command, params) in [ + ("profile.create", json!({"layer": 9999, "points": [[10.0, 45.0], [10.1, 45.0]]})), + ("layout.frameZoom", json!({"to": "layer", "layer": 9999})), + ("layout.series", json!({"layer": 9999})), + ("report.new", json!({"layer": 9999})), + ] { + let error = s.run(command, ¶ms).unwrap_err(); + assert!(error.contains("no layer with id 9999"), "{command}: {error}"); + } +} + +#[test] +fn an_infographic_takes_its_layer_by_id_or_id_as_text() { + let (mut s, _) = with_dem(); + let id = countries(&s, 0); + let named = s.run("info.infographic", &json!({"layer": "Countries"})).unwrap(); + assert_eq!(named["layers"].as_array().map(Vec::len), Some(1), "{named}"); + assert_eq!(s.run("info.infographic", &json!({"layer": id})).unwrap(), named); + assert_eq!(s.run("info.infographic", &json!({"layer": id.to_string()})).unwrap(), named); + // A name the map lacks still gives no cards. + assert_eq!(s.run("info.infographic", &json!({"layer": "Nowhere"})).unwrap()["layers"], json!([])); +}