fix: separate playground values from object descriptions
This commit is contained in:
@@ -68,11 +68,7 @@ pub trait Document {
|
||||
/// Host-private implementation; no body lookup bypass is exposed to clients.
|
||||
pub enum SiteObject {
|
||||
Document(Arc<DocumentNode>, Option<Rc<Playground>>),
|
||||
Counter {
|
||||
name: String,
|
||||
description: String,
|
||||
state: Rc<Playground>,
|
||||
},
|
||||
Counter { name: String, state: Rc<Playground> },
|
||||
}
|
||||
|
||||
impl WipObject for SiteObject {
|
||||
@@ -86,7 +82,7 @@ impl WipObject for SiteObject {
|
||||
fn description(&self) -> Option<&str> {
|
||||
Some(match self {
|
||||
Self::Document(node, _) => &node.title,
|
||||
Self::Counter { description, .. } => description,
|
||||
Self::Counter { .. } => "An in-memory counter.",
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -130,7 +126,7 @@ fn protocol_error(code: ProtocolErrorCode, message: impl Into<String>) -> Protoc
|
||||
}
|
||||
|
||||
/// Immutable documents and isolated mutable playground state. This synchronous,
|
||||
/// single-threaded provider publishes a fresh counter snapshot on every lookup.
|
||||
/// single-threaded provider publishes stable counter metadata; values are read by calls.
|
||||
pub struct Worldspace {
|
||||
registry: InterfaceRegistry,
|
||||
objects: BTreeMap<String, PublishedObject<SiteObject>>,
|
||||
@@ -327,16 +323,14 @@ impl ObjectProvider for Worldspace {
|
||||
&self.counter,
|
||||
path.strip_prefix("/playground/"),
|
||||
) {
|
||||
let value = state.counters.borrow().get(name).copied();
|
||||
let Some(value) = value else {
|
||||
if !state.counters.borrow().contains_key(name) {
|
||||
return ProviderResult::NotFound;
|
||||
};
|
||||
// No shared mutable description: each callback receives its own current
|
||||
// snapshot. Rc/RefCell makes the synchronous lookup/dispatch boundary
|
||||
// single-threaded, with no external writer between resolution and call.
|
||||
}
|
||||
// Resolve existence only; counter values are exposed by typed operations.
|
||||
// Rc/RefCell keeps lookup/dispatch synchronous and single-threaded,
|
||||
// with no external writer between resolution and call.
|
||||
let publication = PublishedObject::new(SiteObject::Counter {
|
||||
name: name.into(),
|
||||
description: value.to_string(),
|
||||
state: Rc::clone(state),
|
||||
})
|
||||
.bind_scope(self.current_scope.clone())
|
||||
|
||||
@@ -65,6 +65,29 @@ fn increment(world: &Worldspace, amount: i64) -> Result<CallOperationResponse, P
|
||||
BTreeMap::from([("amount".into(), Value::Integer(amount))]),
|
||||
)
|
||||
}
|
||||
fn get(world: &Worldspace, name: &str) -> Value {
|
||||
call(
|
||||
world,
|
||||
&format!("/playground/{name}"),
|
||||
"counter",
|
||||
"get",
|
||||
BTreeMap::new(),
|
||||
)
|
||||
.unwrap()
|
||||
.result
|
||||
}
|
||||
|
||||
fn assert_initial_values(world: &Worldspace, observation: &ObjectObservation) {
|
||||
for child in observation.children.as_ref().unwrap() {
|
||||
let name = &child.object.name;
|
||||
assert_eq!(
|
||||
get(world, name),
|
||||
Value::Integer(if name == "apple" { 3 } else { 0 }),
|
||||
"counter {name} must remain unchanged"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn observe(world: &Worldspace, path: &str, depth: u32) -> ObjectObservation {
|
||||
world
|
||||
.observe(ObserveRequest {
|
||||
@@ -137,25 +160,18 @@ fn typed_descriptors_root_scope_and_initial_snapshots() {
|
||||
.iter()
|
||||
.map(|c| (c.object.name.as_str(), c.object.description.as_deref()))
|
||||
.collect::<Vec<_>>(),
|
||||
[("apple", Some("3")), ("orange", Some("0"))]
|
||||
[
|
||||
("apple", Some("An in-memory counter.")),
|
||||
("orange", Some("An in-memory counter.")),
|
||||
]
|
||||
);
|
||||
assert!(
|
||||
children
|
||||
.iter()
|
||||
.all(|c| c.object.interfaces == [reference("counter")] && c.children == Some(vec![]))
|
||||
);
|
||||
assert_eq!(
|
||||
call(
|
||||
&world,
|
||||
"/playground/apple",
|
||||
"counter",
|
||||
"get",
|
||||
BTreeMap::new()
|
||||
)
|
||||
.unwrap()
|
||||
.result,
|
||||
Value::Integer(3)
|
||||
);
|
||||
assert_eq!(get(&world, "apple"), Value::Integer(3));
|
||||
assert_eq!(get(&world, "orange"), Value::Integer(0));
|
||||
assert_eq!(
|
||||
call(
|
||||
&world,
|
||||
@@ -168,18 +184,21 @@ fn typed_descriptors_root_scope_and_initial_snapshots() {
|
||||
.code,
|
||||
ProtocolErrorCode::InterfaceMismatch
|
||||
);
|
||||
assert_eq!(get(&world, "apple"), Value::Integer(3));
|
||||
assert_eq!(
|
||||
call(&world, "/book", "collection", "create", BTreeMap::new())
|
||||
.unwrap_err()
|
||||
.code,
|
||||
ProtocolErrorCode::InterfaceMismatch
|
||||
);
|
||||
assert_eq!(get(&world, "apple"), Value::Integer(3));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mutations_publish_new_values_and_children_without_mutating_documents() {
|
||||
fn mutations_preserve_metadata_and_publish_values_and_children_without_mutating_documents() {
|
||||
let world = world();
|
||||
let before = observe(&world, "/playground/apple", 1);
|
||||
let parent_before = observe(&world, "/playground", 2);
|
||||
let documents: Vec<_> = ["/", "/book", "/playground"]
|
||||
.into_iter()
|
||||
.map(|path| {
|
||||
@@ -192,19 +211,11 @@ fn mutations_publish_new_values_and_children_without_mutating_documents() {
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(increment(&world, 7).unwrap().result, Value::Integer(10));
|
||||
assert_eq!(
|
||||
observe(&world, "/playground/apple", 1)
|
||||
.object
|
||||
.description
|
||||
.as_deref(),
|
||||
Some("10")
|
||||
);
|
||||
assert_eq!(
|
||||
before.object.description.as_deref(),
|
||||
Some("3"),
|
||||
"old snapshots stay immutable"
|
||||
);
|
||||
assert_eq!(get(&world, "apple"), Value::Integer(10));
|
||||
assert_eq!(observe(&world, "/playground/apple", 1), before);
|
||||
assert_eq!(observe(&world, "/playground", 2), parent_before);
|
||||
assert_eq!(increment(&world, -12).unwrap().result, Value::Integer(-2));
|
||||
assert_eq!(get(&world, "apple"), Value::Integer(-2));
|
||||
assert_eq!(
|
||||
call(
|
||||
&world,
|
||||
@@ -217,7 +228,9 @@ fn mutations_publish_new_values_and_children_without_mutating_documents() {
|
||||
.result,
|
||||
Value::Integer(0)
|
||||
);
|
||||
assert_eq!(observe(&world, "/playground/apple", 1).object.name, "apple");
|
||||
assert_eq!(get(&world, "apple"), Value::Integer(0));
|
||||
assert_eq!(observe(&world, "/playground/apple", 1), before);
|
||||
assert_eq!(observe(&world, "/playground", 2), parent_before);
|
||||
assert_eq!(
|
||||
collection(&world, "create", "New_counter-2")
|
||||
.unwrap()
|
||||
@@ -229,8 +242,9 @@ fn mutations_publish_new_values_and_children_without_mutating_documents() {
|
||||
.object
|
||||
.description
|
||||
.as_deref(),
|
||||
Some("0")
|
||||
Some("An in-memory counter.")
|
||||
);
|
||||
assert_eq!(get(&world, "New_counter-2"), Value::Integer(0));
|
||||
collection(&world, "remove", "apple").unwrap();
|
||||
assert_eq!(
|
||||
call(
|
||||
@@ -252,13 +266,8 @@ fn mutations_publish_new_values_and_children_without_mutating_documents() {
|
||||
.any(|child| child.object.name == "apple")
|
||||
);
|
||||
collection(&world, "create", "apple").unwrap();
|
||||
assert_eq!(
|
||||
observe(&world, "/playground/apple", 1)
|
||||
.object
|
||||
.description
|
||||
.as_deref(),
|
||||
Some("0")
|
||||
);
|
||||
assert_eq!(observe(&world, "/playground/apple", 1), before);
|
||||
assert_eq!(get(&world, "apple"), Value::Integer(0));
|
||||
for (path, expected) in documents {
|
||||
assert_eq!(
|
||||
call(&world, path, "document", "read", BTreeMap::new())
|
||||
@@ -273,6 +282,7 @@ fn mutations_publish_new_values_and_children_without_mutating_documents() {
|
||||
fn invalid_names_duplicates_missing_and_capacity_fail_without_mutation() {
|
||||
let world = world();
|
||||
let before = observe(&world, "/", 3);
|
||||
let counters_before = observe(&world, "/playground", 1);
|
||||
for name in [
|
||||
"",
|
||||
".",
|
||||
@@ -291,11 +301,15 @@ fn invalid_names_duplicates_missing_and_capacity_fail_without_mutation() {
|
||||
"{operation} {name:?}"
|
||||
);
|
||||
assert_eq!(observe(&world, "/", 3), before);
|
||||
assert_initial_values(&world, &counters_before);
|
||||
}
|
||||
}
|
||||
assert!(collection(&world, "create", "apple").is_err());
|
||||
assert_eq!(observe(&world, "/", 3), before);
|
||||
assert_initial_values(&world, &counters_before);
|
||||
assert!(collection(&world, "remove", "missing").is_err());
|
||||
assert_eq!(observe(&world, "/", 3), before);
|
||||
assert_initial_values(&world, &counters_before);
|
||||
collection(&world, "create", &"a".repeat(32)).unwrap();
|
||||
for index in 3..MAX_COUNTERS {
|
||||
collection(&world, "create", &format!("counter-{index}")).unwrap();
|
||||
@@ -307,6 +321,7 @@ fn invalid_names_duplicates_missing_and_capacity_fail_without_mutation() {
|
||||
ProtocolErrorCode::ResourceLimitExceeded
|
||||
);
|
||||
assert_eq!(observe(&world, "/playground", 1), full);
|
||||
assert_initial_values(&world, &full);
|
||||
collection(&world, "remove", "orange").unwrap();
|
||||
collection(&world, "create", "over-limit").unwrap();
|
||||
}
|
||||
@@ -318,22 +333,36 @@ fn integer_and_typed_argument_failures_are_atomic() {
|
||||
increment(&world, MAX_SAFE_INTEGER - 3).unwrap().result,
|
||||
Value::Integer(MAX_SAFE_INTEGER)
|
||||
);
|
||||
assert_eq!(get(&world, "apple"), Value::Integer(MAX_SAFE_INTEGER));
|
||||
let maximum = observe(&world, "/playground/apple", 1);
|
||||
for amount in [1, MAX_SAFE_INTEGER, i64::MAX, i64::MIN] {
|
||||
assert!(increment(&world, amount).is_err());
|
||||
assert_eq!(observe(&world, "/playground/apple", 1), maximum);
|
||||
assert_eq!(get(&world, "apple"), Value::Integer(MAX_SAFE_INTEGER));
|
||||
}
|
||||
call(
|
||||
&world,
|
||||
"/playground/apple",
|
||||
"counter",
|
||||
"reset",
|
||||
BTreeMap::new(),
|
||||
)
|
||||
.unwrap();
|
||||
increment(&world, MIN_SAFE_INTEGER).unwrap();
|
||||
assert_eq!(
|
||||
call(
|
||||
&world,
|
||||
"/playground/apple",
|
||||
"counter",
|
||||
"reset",
|
||||
BTreeMap::new(),
|
||||
)
|
||||
.unwrap()
|
||||
.result,
|
||||
Value::Integer(0)
|
||||
);
|
||||
assert_eq!(get(&world, "apple"), Value::Integer(0));
|
||||
assert_eq!(observe(&world, "/playground/apple", 1), maximum);
|
||||
assert_eq!(
|
||||
increment(&world, MIN_SAFE_INTEGER).unwrap().result,
|
||||
Value::Integer(MIN_SAFE_INTEGER)
|
||||
);
|
||||
assert_eq!(get(&world, "apple"), Value::Integer(MIN_SAFE_INTEGER));
|
||||
let minimum = observe(&world, "/playground/apple", 1);
|
||||
assert_eq!(minimum, maximum);
|
||||
assert!(increment(&world, -1).is_err());
|
||||
assert_eq!(get(&world, "apple"), Value::Integer(MIN_SAFE_INTEGER));
|
||||
for args in [
|
||||
BTreeMap::new(),
|
||||
BTreeMap::from([("amount".into(), Value::String("1".into()))]),
|
||||
@@ -349,23 +378,25 @@ fn integer_and_typed_argument_failures_are_atomic() {
|
||||
ProtocolErrorCode::InvalidArguments
|
||||
);
|
||||
assert_eq!(observe(&world, "/playground/apple", 1), minimum);
|
||||
assert_eq!(get(&world, "apple"), Value::Integer(MIN_SAFE_INTEGER));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_worldspace_resets_state_and_manifest_documents_cannot_be_shadowed() {
|
||||
let first = world();
|
||||
increment(&first, 12).unwrap();
|
||||
assert_eq!(increment(&first, 12).unwrap().result, Value::Integer(15));
|
||||
assert_eq!(get(&first, "apple"), Value::Integer(15));
|
||||
collection(&first, "remove", "orange").unwrap();
|
||||
collection(&first, "create", "pear").unwrap();
|
||||
let fresh = world();
|
||||
assert_eq!(
|
||||
observe(&fresh, "/playground/apple", 1)
|
||||
.object
|
||||
.description
|
||||
.as_deref(),
|
||||
Some("3")
|
||||
observe(&fresh, "/playground/apple", 1),
|
||||
observe(&first, "/playground/apple", 1)
|
||||
);
|
||||
assert_eq!(get(&fresh, "apple"), Value::Integer(3));
|
||||
assert_eq!(get(&fresh, "orange"), Value::Integer(0));
|
||||
assert_eq!(get(&first, "apple"), Value::Integer(15));
|
||||
assert_eq!(
|
||||
observe(&fresh, "/playground", 1)
|
||||
.children
|
||||
|
||||
Reference in New Issue
Block a user