diff --git a/pkgs/id/src/commands/world.rs b/pkgs/id/src/commands/world.rs index d78afe79ad..3425ed5d1a 100644 --- a/pkgs/id/src/commands/world.rs +++ b/pkgs/id/src/commands/world.rs @@ -1940,7 +1940,12 @@ mod tests { assert!(calls[kind]["fuel_max"].as_u64().unwrap() > 0, "{kind}"); } assert!(calls.get("restore").is_none(), "{report}"); - assert!(report["worst_call_percent_of_limit"].as_f64().unwrap() < 1.0); + // The worst call is layout, rendering the pf.Screen frame. Its + // ceiling is the largest per-call budget FUEL_BUDGETS allows this + // module (500_000 of the 10_000_000 limit), so the report must stay + // under 5 percent; before the Screen migration the bare-text view + // stayed under 1. + assert!(report["worst_call_percent_of_limit"].as_f64().unwrap() < 5.0); // Without a viewport there is no layout call; bad hex is refused // before the module runs. diff --git a/pkgs/id/src/sandbox.rs b/pkgs/id/src/sandbox.rs index aac6fee22a..86dd104836 100644 --- a/pkgs/id/src/sandbox.rs +++ b/pkgs/id/src/sandbox.rs @@ -2105,8 +2105,8 @@ mod tests { const FUEL_BUDGETS: &[(&str, GuestCall, u64)] = &[ ("roc-counter", GuestCall::Init, 100_000), // baseline 1215 ("roc-counter", GuestCall::Update, 100_000), // baseline 3759 - ("roc-counter", GuestCall::View, 100_000), // baseline 1327 - ("roc-counter", GuestCall::Layout, 100_000), // baseline 1337 + ("roc-counter", GuestCall::View, 400_000), // baseline 260269 + ("roc-counter", GuestCall::Layout, 500_000), // baseline 323425 ("roc-counter", GuestCall::Records, 100_000), // baseline 1866 ("roc-counter", GuestCall::Wants, 100_000), // baseline 733 ("roc-counter", GuestCall::Snapshot, 100_000), // baseline 874 diff --git a/pkgs/id/src/store_traits.rs b/pkgs/id/src/store_traits.rs index f39ae25db6..b7f512d05e 100644 --- a/pkgs/id/src/store_traits.rs +++ b/pkgs/id/src/store_traits.rs @@ -538,30 +538,180 @@ mod tests { use crate::chain::{ChainExt, sign}; use crate::waiting::WaitingLimits; - /// The build has no SQLite, with `LogSync` wired in. p2panda-sync's store dependency defaults to - /// SQLite, so the vendored copy turns that default off (`vendor/README.md`). This fails if the - /// lock names SQLite or sqlx, if p2panda-sync is missing, or if the vendored line loses the change. + /// One `[[package]]` entry of the lock: its name and the names it + /// depends on. Dependency entries are `"name"`, `"name version"` or + /// `"name version (source)"`; the name is the first word. + fn lock_packages(lock: &str) -> Vec<(String, Vec)> { + let mut packages = Vec::new(); + let mut current: Option<(String, Vec)> = None; + let mut in_deps = false; + for raw in lock.lines() { + let line = raw.trim(); + if line.starts_with("[[") { + in_deps = false; + if let Some(package) = current.take() { + packages.push(package); + } + } else if current.is_none() { + if let Some(rest) = line.strip_prefix("name = \"") { + current = Some((rest.trim_end_matches('"').to_owned(), Vec::new())); + } + } else if line == "dependencies = [" { + in_deps = true; + } else if in_deps && line == "]" { + in_deps = false; + } else if in_deps { + if let Some(entry) = line.strip_suffix(',') { + let entry = entry.trim_matches('"'); + if let Some(name) = entry.split(' ').next() { + current.as_mut().unwrap().1.push(name.to_owned()); + } + } + } + } + if let Some(package) = current.take() { + packages.push(package); + } + packages + } + + /// Whether a manifest section header opens a dev-dependency section + /// (`Some(true)`), a shipped one — normal or build (`Some(false)`) — or + /// neither. Plain, target-specific and per-crate subtable spellings all + /// count: `[dev-dependencies]`, `[target.'cfg(..)'.dependencies]`, + /// `[dependencies.rusqlite]`. + fn dep_section_dev(section: &str) -> Option { + let tail = section.rsplit('.').next().unwrap_or(section); + match tail { + "dev-dependencies" => Some(true), + "dependencies" | "build-dependencies" => Some(false), + _ if section.starts_with("dev-dependencies.") => Some(true), + _ if section.starts_with("dependencies.") + || section.starts_with("build-dependencies.") => + { + Some(false) + } + _ => None, + } + } + + /// The crate names a manifest declares in dependency sections of one + /// class (`dev` selects dev-dependencies, otherwise the shipped ones). + /// A `package = "..."` rename counts under the real name. + fn manifest_dep_names(manifest: &str, dev: bool) -> Vec { + let mut names = Vec::new(); + let mut in_class = false; + for raw in manifest.lines() { + let line = raw.trim(); + if line.starts_with('#') { + continue; + } + if line.starts_with('[') && line.ends_with(']') { + let section = line.trim_matches(|c| c == '[' || c == ']'); + in_class = dep_section_dev(section) == Some(dev); + if in_class { + // A per-crate subtable names the crate in the header: + // `[dependencies.rusqlite]`. + if let Some((_, name)) = section.rsplit_once('.') + && !matches!( + name, + "dependencies" | "build-dependencies" | "dev-dependencies" + ) + { + names.push(name.to_owned()); + } + } + continue; + } + if !in_class { + continue; + } + if let Some((key, _)) = line.split_once('=') { + let key = key.trim().trim_matches('"'); + if !key.is_empty() + && key + .chars() + .all(|c| c.is_ascii_alphanumeric() || matches!(c, '-' | '_')) + { + names.push(key.to_owned()); + } + } + if let Some((_, rest)) = line.split_once("package = \"") + && let Some(name) = rest.split('"').next() + { + names.push(name.to_owned()); + } + } + names + } + + /// The `[package]` name of a manifest. + fn manifest_package_name(manifest: &str) -> Option { + let mut in_package = false; + for raw in manifest.lines() { + let line = raw.trim(); + if line.starts_with('[') { + in_package = line == "[package]"; + continue; + } + if in_package && let Some(rest) = line.strip_prefix("name = \"") { + return Some(rest.trim_end_matches('"').to_owned()); + } + } + None + } + + /// The shipped build has no SQLite, with `LogSync` wired in. p2panda-sync's store dependency + /// defaults to SQLite, so the vendored copy turns that default off (`vendor/README.md`). + /// + /// The raw lock cannot carry this check on its own: it resolves dev-dependencies too, and + /// #2276 dev-depends on `rusqlite` for the bridge PoC's hosted store, which never ships. + /// So this reads the resolved graph instead. A SQLite-family package may appear only when + /// every package that depends on it is SQLite-family itself or this package — and this + /// package's manifest, checked section by section, must declare SQLite names as + /// dev-dependencies only. It fails if a shipped dependency pulls SQLite in, if the + /// manifest moves it out of dev-dependencies, if p2panda-sync is missing, or if the + /// vendored line loses the change. #[test] fn test_the_lock_has_no_sqlite_with_logsync_wired() { - let lock = - std::fs::read_to_string(concat!(env!("CARGO_MANIFEST_DIR"), "/Cargo.lock")).unwrap(); - for name in ["sqlx", "sqlite", "rusqlite", "libsqlite3-sys"] { - assert!( - !lock.contains(&format!("name = \"{name}")), - "Cargo.lock names {name}: SQLite is back in the build (see vendor/README.md)" - ); - } + const SQLITE: [&str; 4] = ["sqlx", "sqlite", "rusqlite", "libsqlite3-sys"]; + let manifest_dir = env!("CARGO_MANIFEST_DIR"); + let lock = std::fs::read_to_string(format!("{manifest_dir}/Cargo.lock")).unwrap(); + let packages = lock_packages(&lock); assert!( - lock.contains("name = \"p2panda-sync\""), + packages.iter().any(|(name, _)| name == "p2panda-sync"), "Cargo.lock does not name p2panda-sync: LogSync is not wired in" ); - let manifest = std::fs::read_to_string(concat!( - env!("CARGO_MANIFEST_DIR"), - "/vendor/p2panda-sync/Cargo.toml" - )) - .unwrap(); - let store_line = manifest + let manifest = std::fs::read_to_string(format!("{manifest_dir}/Cargo.toml")).unwrap(); + let root = manifest_package_name(&manifest).unwrap(); + // This package may declare SQLite names only as dev-dependencies. + let shipped = manifest_dep_names(&manifest, false); + for name in SQLITE { + assert!( + !shipped.iter().any(|declared| declared.as_str() == name), + "Cargo.toml declares {name} outside dev-dependencies: SQLite is back in the \ + shipped build (see vendor/README.md)" + ); + } + // Nothing outside the family itself and this package (dev-only, + // checked above) may depend on a SQLite-family package. + for (name, deps) in &packages { + for dep in deps { + if SQLITE.contains(&dep.as_str()) { + assert!( + SQLITE.contains(&name.as_str()) || name == &root, + "{name} depends on {dep}: SQLite is back in the shipped build (see \ + vendor/README.md)" + ); + } + } + } + + let vendored = + std::fs::read_to_string(format!("{manifest_dir}/vendor/p2panda-sync/Cargo.toml")) + .unwrap(); + let store_line = vendored .lines() .find(|line| line.starts_with("p2panda-store = {")) .unwrap(); diff --git a/pkgs/id/src/web/world_ws.rs b/pkgs/id/src/web/world_ws.rs index a8c48bc23a..70ebb3d4f5 100644 --- a/pkgs/id/src/web/world_ws.rs +++ b/pkgs/id/src/web/world_ws.rs @@ -1063,6 +1063,26 @@ mod tests { } } + /// Read one presentation frame and assert its data is a `pf.Screen` + /// frame whose rows show the counter at `count`. Since the pf.Screen + /// migration the roc-counter view returns screen JSON, not the bare + /// `count=N` text these tests originally asserted. + #[cfg(feature = "sandbox")] + async fn assert_screen_data(client: &mut Client, count: u8) { + let frame = next_frame(client).await.unwrap(); + let screen: serde_json::Value = + serde_json::from_str(frame["data"].as_str().unwrap()).unwrap(); + assert_eq!(screen["kind"], "screen", "{frame}"); + assert!( + screen["rows"].as_array().unwrap().iter().any(|row| { + row.as_str() + .unwrap_or_default() + .contains(&format!("count={count}")) + }), + "no screen row shows count={count}: {screen}" + ); + } + fn join(capability: &JoinCapability, after: Option) -> serde_json::Value { serde_json::json!({ "type": "join", @@ -1270,7 +1290,7 @@ mod tests { .unwrap(); let mut player = connect(addr, join(&capability, None)).await; assert_eq!(next_frame(&mut player).await.unwrap()["type"], "snapshot"); - assert_eq!(next_frame(&mut player).await.unwrap()["data"], "count=0"); + assert_screen_data(&mut player, 0).await; player .send(ClientMessage::Text( r#"{"type":"input","data_hex":"696e63"}"#.into(), @@ -1278,7 +1298,7 @@ mod tests { .await .unwrap(); assert_eq!(next_frame(&mut player).await.unwrap()["type"], "event"); - assert_eq!(next_frame(&mut player).await.unwrap()["data"], "count=1"); + assert_screen_data(&mut player, 1).await; } #[cfg(feature = "sandbox")] @@ -1314,6 +1334,13 @@ mod tests { )) .unwrap(); let world = WorldHandle::spawn(WorldCore::new("lobby", WorldLimits::default()).unwrap()); + // roc-counter's main.roc imports pf.Screen and pf.RocText, and the + // compile gate admits only capabilities the world has granted; + // grant them the way an admin would (`id world caps --grant`). + world + .update_caps(vec!["Screen".to_owned(), "RocText".to_owned()], Vec::new()) + .await + .unwrap(); let blobs: iroh_blobs::api::Store = MemStore::new().into(); let service = WorldService::new(world.clone(), Some("admin".to_owned())) .with_blob_store(blobs.clone()) @@ -1352,7 +1379,7 @@ mod tests { .unwrap(); let mut player = connect(addr, join(&capability, None)).await; assert_eq!(next_frame(&mut player).await.unwrap()["type"], "snapshot"); - assert_eq!(next_frame(&mut player).await.unwrap()["data"], "count=0"); + assert_screen_data(&mut player, 0).await; player .send(ClientMessage::Text( r#"{"type":"input","data_hex":"696e63"}"#.into(), @@ -1360,7 +1387,7 @@ mod tests { .await .unwrap(); assert_eq!(next_frame(&mut player).await.unwrap()["type"], "event"); - assert_eq!(next_frame(&mut player).await.unwrap()["data"], "count=1"); + assert_screen_data(&mut player, 1).await; } #[cfg(feature = "sandbox")]