diff --git a/pkgs/id/examples/roc-counter/counter.wasm b/pkgs/id/examples/roc-counter/counter.wasm old mode 100755 new mode 100644 index 2ede55cd67..4ca1d8f238 Binary files a/pkgs/id/examples/roc-counter/counter.wasm and b/pkgs/id/examples/roc-counter/counter.wasm differ diff --git a/pkgs/id/examples/roc-counter/main.roc b/pkgs/id/examples/roc-counter/main.roc index ac9edcd6f6..e4120f3f8e 100644 --- a/pkgs/id/examples/roc-counter/main.roc +++ b/pkgs/id/examples/roc-counter/main.roc @@ -1,5 +1,8 @@ app [program] { pf: platform "../roc-world/platform.roc" } +import pf.Screen +import pf.RocText + Model := { count : I64 } program = { init, update, view, layout, records, wants, snapshot, restore } @@ -7,17 +10,31 @@ program = { init, update, view, layout, records, wants, snapshot, restore } init : U64 -> Model init = |_seed| { count: 0 } +## Events are matched after a RocText trim, so a journaled or typed +## " inc " counts the same as "inc". update : Model, U64, Str -> Model -update = |model, _pid, ev| if ev == "inc" { count: model.count + 1 } else model +update = |model, _pid, ev| if RocText.trim(ev) == "inc" { count: model.count + 1 } else model +## The counter renders through Screen, like roc-screen: a boxed 20x3 +## screen whose JSON the host draws, instead of a bare text view. view : Model -> Str -view = |model| Str.concat("count=", model.count.to_str()) +view = |model| render(model, 20, 3) -## Structured data the host stores and replicates: a JSON object of records. -## This program renders the same layout at any viewport. +# Reflow: a wide viewport gets a wider box, a short one keeps three rows. layout : Model, U64, U64 -> Str -layout = |model, _width, _height| view(model) +layout = |model, width, height| { + w = if width >= 30 30 else 20 + h = if height >= 4 3 else 3 + render(model, w, h) +} +render : Model, U64, U64 -> Str +render = |model, w, h| { + base = Screen.boxed(Screen.new(w, h), 0, 0, w, h, "counter") + Screen.render_json(Screen.put_text(base, 2, 1, "count=${model.count.to_str()}", { fg: 0, bg: 0, bold: Bool.False })) +} + +## Structured data the host stores and replicates: a JSON object of records. records : Model -> Str records = |model| "{\"count\":${model.count.to_str()}}" diff --git a/pkgs/id/examples/tic-tac-toe/main.roc b/pkgs/id/examples/tic-tac-toe/main.roc index 49aef58d3f..e331f55f0e 100644 --- a/pkgs/id/examples/tic-tac-toe/main.roc +++ b/pkgs/id/examples/tic-tac-toe/main.roc @@ -1,5 +1,7 @@ app [program] { pf: platform "../roc-world/platform.roc" } +import pf.RocJson + # A two-player tic-tac-toe world. # # State is a 9-character board string, the ply count, and the winner. The @@ -59,7 +61,13 @@ layout : Model, U64, U64 -> Str layout = |model, _width, _height| view(model) records : Model -> Str -records = |model| "{\"board\":\"${model.board}\",\"plays\":${model.plays.to_str()},\"winner\":\"${model.winner}\"}" +records = |model| encode_or_crash( + RocJson.Object([ + { key: "board", value: RocJson.String(model.board) }, + { key: "plays", value: RocJson.Number(model.plays.to_str()) }, + { key: "winner", value: RocJson.String(model.winner) }, + ]), +) ## The board is the whole state: the ply count is the number of marks on it ## and the winner follows from its lines, so nothing else is stored. @@ -79,6 +87,11 @@ restore = |board| { # --- helpers --------------------------------------------------------------- +encode_or_crash = |value| match RocJson.encode(value) { + Ok(text) => text + Err(_) => crash "tic-tac-toe attempted to encode an invalid or oversized JSON value" +} + empty_cell : U8 empty_cell = 45 # '-' diff --git a/pkgs/id/examples/tic-tac-toe/tic-tac-toe.wasm b/pkgs/id/examples/tic-tac-toe/tic-tac-toe.wasm old mode 100755 new mode 100644 index f7c320b5ff..90a00d97d9 Binary files a/pkgs/id/examples/tic-tac-toe/tic-tac-toe.wasm and b/pkgs/id/examples/tic-tac-toe/tic-tac-toe.wasm differ diff --git a/pkgs/id/src/sandbox.rs b/pkgs/id/src/sandbox.rs index be38578596..aac6fee22a 100644 --- a/pkgs/id/src/sandbox.rs +++ b/pkgs/id/src/sandbox.rs @@ -951,11 +951,23 @@ mod tests { let wasm = include_bytes!("../examples/roc-counter/counter.wasm"); let runner = Sandbox::compile(wasm, SandboxLimits::default()).unwrap(); let mut world = runner.instantiate(7).unwrap(); - assert_eq!(world.view().unwrap(), b"count=0"); + assert!( + String::from_utf8(world.view().unwrap()) + .unwrap() + .contains("count=0") + ); world.update(1, b"inc").unwrap(); - assert_eq!(world.view().unwrap(), b"count=1"); + assert!( + String::from_utf8(world.view().unwrap()) + .unwrap() + .contains("count=1") + ); world.update(1, b"inc").unwrap(); - assert_eq!(world.view().unwrap(), b"count=2"); + assert!( + String::from_utf8(world.view().unwrap()) + .unwrap() + .contains("count=2") + ); } #[tokio::test] @@ -971,25 +983,37 @@ mod tests { ); let (_, capability) = actor.issue("Ada", WorldScopes::GUEST).await.unwrap(); - assert_eq!( - actor.view(capability.clone()).await.unwrap().as_deref(), - Some("count=0") + assert!( + actor + .view(capability.clone()) + .await + .unwrap() + .as_deref() + .is_some_and(|view| view.contains("count=0")) ); actor .input(capability.clone(), b"inc".to_vec()) .await .unwrap(); - assert_eq!( - actor.view(capability.clone()).await.unwrap().as_deref(), - Some("count=1") + assert!( + actor + .view(capability.clone()) + .await + .unwrap() + .as_deref() + .is_some_and(|view| view.contains("count=1")) ); actor .chat(capability.clone(), "chat does not mutate the game") .await .unwrap(); - assert_eq!( - actor.view(capability).await.unwrap().as_deref(), - Some("count=1") + assert!( + actor + .view(capability) + .await + .unwrap() + .as_deref() + .is_some_and(|view| view.contains("count=1")) ); } @@ -1003,7 +1027,11 @@ mod tests { world.update(1, b"inc").unwrap(); assert_eq!(world.records().unwrap().unwrap(), br#"{"count":2}"#); // A view between records calls must not disturb either output. - assert_eq!(world.view().unwrap(), b"count=2"); + assert!( + String::from_utf8(world.view().unwrap()) + .unwrap() + .contains("count=2") + ); assert_eq!(world.records().unwrap().unwrap(), br#"{"count":2}"#); } @@ -1065,15 +1093,27 @@ mod tests { assert_eq!(live.snapshot().unwrap().unwrap(), b"3"); let mut restored = runner.instantiate(7).unwrap(); restored.restore(b"3").unwrap(); - assert_eq!(restored.view().unwrap(), b"count=3"); + assert!( + String::from_utf8(restored.view().unwrap()) + .unwrap() + .contains("count=3") + ); restored.update(1, b"inc").unwrap(); - assert_eq!(restored.view().unwrap(), b"count=4"); + assert!( + String::from_utf8(restored.view().unwrap()) + .unwrap() + .contains("count=4") + ); // The trait-level snapshot is the verified one the actor journals. assert_eq!(live.snapshot_via_trait(), Some("3".to_owned())); // Garbage restores to a defined state instead of trapping. let mut garbage = runner.instantiate(7).unwrap(); garbage.restore(b"not a number").unwrap(); - assert_eq!(garbage.view().unwrap(), b"count=0"); + assert!( + String::from_utf8(garbage.view().unwrap()) + .unwrap() + .contains("count=0") + ); // Tic-tac-toe: the board alone carries the ply count and the winner. let wasm = include_bytes!("../examples/tic-tac-toe/tic-tac-toe.wasm"); @@ -1465,9 +1505,8 @@ mod tests { ) .unwrap(); let _ = crate::world::WorldProgram::records(&mut program).unwrap(); - assert_eq!( - view(&mut program), - "count=1", + assert!( + view(&mut program).contains("count=1"), "wasm handles the actor order" ); } diff --git a/pkgs/id/src/world_compile.rs b/pkgs/id/src/world_compile.rs index 64758d0812..0860c06429 100644 --- a/pkgs/id/src/world_compile.rs +++ b/pkgs/id/src/world_compile.rs @@ -1087,7 +1087,7 @@ restore = |text| match U64.from_str(text) { native: false, }, &compiler, - &[], + &["Screen".to_owned(), "RocText".to_owned()], COMPILE_TIMEOUT, ) .await @@ -1115,7 +1115,11 @@ restore = |text| match U64.from_str(text) { }, ) .unwrap(); - assert_eq!(world.view().unwrap(), b"count=1"); + assert!( + String::from_utf8(world.view().unwrap()) + .unwrap() + .contains("count=1") + ); } #[tokio::test] @@ -1212,7 +1216,7 @@ restore = |text| match U64.from_str(text) { native: true, }, &compiler, - &[], + &["Screen".to_owned(), "RocText".to_owned()], COMPILE_TIMEOUT, ) .await @@ -1251,7 +1255,7 @@ restore = |text| match U64.from_str(text) { }, ) .unwrap(); - assert_eq!(view.as_deref(), Some("count=1")); + assert!(view.as_deref().is_some_and(|view| view.contains("count=1"))); } #[test] diff --git a/pkgs/id/src/world_net.rs b/pkgs/id/src/world_net.rs index 2015a4dfc0..4e55ede299 100644 --- a/pkgs/id/src/world_net.rs +++ b/pkgs/id/src/world_net.rs @@ -1484,7 +1484,11 @@ mod tests { assert_eq!(next(&mut client).await.unwrap()["type"], "snapshot"); let initial = next(&mut client).await.unwrap(); assert_eq!(initial["type"], "view"); - assert_eq!(initial["data"], "count=0"); + assert!( + initial["data"] + .as_str() + .is_some_and(|data| data.contains("count=0")) + ); client .send_json(&serde_json::json!({ "type": "input", "data_hex": "696e63" })) @@ -1493,7 +1497,11 @@ mod tests { let event = next(&mut client).await.unwrap(); assert_eq!(event["type"], "event"); let view = next(&mut client).await.unwrap(); - assert_eq!(view["data"], "count=1"); + assert!( + view["data"] + .as_str() + .is_some_and(|data| data.contains("count=1")) + ); client_ep.close().await; router.shutdown().await.unwrap(); diff --git a/pkgs/id/src/world_session/tests.rs b/pkgs/id/src/world_session/tests.rs index 5e1128a7f2..7a8968ffb4 100644 --- a/pkgs/id/src/world_session/tests.rs +++ b/pkgs/id/src/world_session/tests.rs @@ -1553,12 +1553,23 @@ mod tests { blobs.blobs().has(hash).await.unwrap(), "uploaded module is pinned in the blob store" ); - assert_eq!( - world.view(guest.clone()).await.unwrap().as_deref(), - Some("count=0") + assert!( + world + .view(guest.clone()) + .await + .unwrap() + .as_deref() + .is_some_and(|view| view.contains("count=0")) ); world.input(guest.clone(), b"inc".to_vec()).await.unwrap(); - assert_eq!(world.view(guest).await.unwrap().as_deref(), Some("count=1")); + assert!( + world + .view(guest) + .await + .unwrap() + .as_deref() + .is_some_and(|view| view.contains("count=1")) + ); } #[cfg(feature = "sandbox")] @@ -1685,9 +1696,13 @@ mod tests { let service = WorldService::new(world.clone(), Some("admin".to_owned())).with_blob_store(blobs); let (_, guest) = world.issue("guest", WorldScopes::GUEST).await.unwrap(); - assert_eq!( - world.view(guest.clone()).await.unwrap().as_deref(), - Some("count=0") + assert!( + world + .view(guest.clone()) + .await + .unwrap() + .as_deref() + .is_some_and(|view| view.contains("count=0")) ); let bad = b"not wasm".to_vec(); @@ -1699,12 +1714,23 @@ mod tests { .unwrap_err(), InstallError::InvalidModule ); - assert_eq!( - world.view(guest.clone()).await.unwrap().as_deref(), - Some("count=0") + assert!( + world + .view(guest.clone()) + .await + .unwrap() + .as_deref() + .is_some_and(|view| view.contains("count=0")) ); world.input(guest.clone(), b"inc".to_vec()).await.unwrap(); - assert_eq!(world.view(guest).await.unwrap().as_deref(), Some("count=1")); + assert!( + world + .view(guest) + .await + .unwrap() + .as_deref() + .is_some_and(|view| view.contains("count=1")) + ); } #[tokio::test] diff --git a/pkgs/id/src/world_store.rs b/pkgs/id/src/world_store.rs index 4918ab7dc6..53c66ef6f9 100644 --- a/pkgs/id/src/world_store.rs +++ b/pkgs/id/src/world_store.rs @@ -1174,7 +1174,7 @@ mod tests { .unwrap(); } svc.world().chat(ann.clone(), "hi").await.unwrap(); - assert_eq!(view(&svc, &ann).await, "count=3"); + assert!(view(&svc, &ann).await.contains("count=3")); svc.world().shutdown().await.unwrap(); let fresh_blobs: iroh_blobs::api::Store = iroh_blobs::store::mem::MemStore::new().into(); @@ -1190,7 +1190,10 @@ mod tests { assert_eq!(report.program_error, None); assert_eq!(report.replayed_inputs, 3); assert_eq!(report.module_hash.as_deref(), Some(hash.as_str())); - assert_eq!(view(&svc, &ann).await, "count=3", "state rebuilt by replay"); + assert!( + view(&svc, &ann).await.contains("count=3"), + "state rebuilt by replay" + ); assert_eq!( svc.active_module_hash().await.as_deref(), Some(hash.as_str()), @@ -1200,7 +1203,7 @@ mod tests { .input(ann.clone(), b"inc".to_vec()) .await .unwrap(); - assert_eq!(view(&svc, &ann).await, "count=4"); + assert!(view(&svc, &ann).await.contains("count=4")); } #[cfg(feature = "sandbox")] @@ -1242,7 +1245,7 @@ mod tests { .input(ann.clone(), b"inc".to_vec()) .await .unwrap(); - assert_eq!(view(&svc, &ann).await, "count=1"); + assert!(view(&svc, &ann).await.contains("count=1")); svc.world().shutdown().await.unwrap(); } @@ -1290,7 +1293,7 @@ mod tests { } svc.world().chat(ann.clone(), "hi").await.unwrap(); assert!(svc.world().revoke(bob_participant.id).await.unwrap()); - assert_eq!(view(&svc, &ann).await, "count=13"); + assert!(view(&svc, &ann).await.contains("count=13")); let sequence = svc .world() .snapshot(ann.clone()) @@ -1336,9 +1339,8 @@ mod tests { report.replayed_inputs < 5, "replay starts at the checkpoint" ); - assert_eq!( - view(&svc, &ann).await, - "count=13", + assert!( + view(&svc, &ann).await.contains("count=13"), "state from checkpoint + tail" ); assert_eq!( @@ -1353,7 +1355,7 @@ mod tests { .input(ann.clone(), b"inc".to_vec()) .await .unwrap(); - assert_eq!(view(&svc, &ann).await, "count=14"); + assert!(view(&svc, &ann).await.contains("count=14")); let page = svc .world() .events_after(ann.clone(), sequence)