feat: add explicit protocol API and mutable playground host
This commit is contained in:
@@ -0,0 +1,383 @@
|
||||
use std::collections::BTreeMap;
|
||||
|
||||
use wip_site_worldspace::wip_protocol::{
|
||||
CallOperationRequest, CallOperationResponse, FetchInterfaceRequest, InterfaceReference,
|
||||
InterfaceTarget, MAX_SAFE_INTEGER, MIN_SAFE_INTEGER, ObjectObservation, ObserveRequest,
|
||||
ProtocolError, ProtocolErrorCode, Target, TypeExpr, Value,
|
||||
};
|
||||
use wip_site_worldspace::{MAX_COUNTERS, Worldspace};
|
||||
|
||||
fn world() -> Worldspace {
|
||||
Worldspace::from_json(r#"{"nodes":[
|
||||
{"path":"/","name":"","displayPath":"/","title":"Root","href":"/","html":"<p>root</p>"},
|
||||
{"path":"/book","name":"book","parent":"/","displayPath":"/book","title":"Book","href":"/book/","html":"<p>book</p>"},
|
||||
{"path":"/playground","name":"playground","parent":"/","displayPath":"/playground","title":"Playground","href":"/playground/","html":"<p>playground</p>"}
|
||||
]}"#).unwrap()
|
||||
}
|
||||
|
||||
fn reference(name: &str) -> InterfaceReference {
|
||||
InterfaceReference {
|
||||
scope: "/".into(),
|
||||
name: name.into(),
|
||||
}
|
||||
}
|
||||
|
||||
fn call(
|
||||
world: &Worldspace,
|
||||
path: &str,
|
||||
interface: &str,
|
||||
operation: &str,
|
||||
arguments: BTreeMap<String, Value>,
|
||||
) -> Result<CallOperationResponse, ProtocolError> {
|
||||
world.call_operation(CallOperationRequest {
|
||||
target: Target {
|
||||
path: path.into(),
|
||||
validator: None,
|
||||
},
|
||||
interface: InterfaceTarget {
|
||||
reference: reference(interface),
|
||||
scope_ref: None,
|
||||
validator: None,
|
||||
},
|
||||
operation: operation.into(),
|
||||
arguments,
|
||||
})
|
||||
}
|
||||
fn collection(
|
||||
world: &Worldspace,
|
||||
operation: &str,
|
||||
name: &str,
|
||||
) -> Result<CallOperationResponse, ProtocolError> {
|
||||
call(
|
||||
world,
|
||||
"/playground",
|
||||
"collection",
|
||||
operation,
|
||||
BTreeMap::from([("name".into(), Value::String(name.into()))]),
|
||||
)
|
||||
}
|
||||
fn increment(world: &Worldspace, amount: i64) -> Result<CallOperationResponse, ProtocolError> {
|
||||
call(
|
||||
world,
|
||||
"/playground/apple",
|
||||
"counter",
|
||||
"increment",
|
||||
BTreeMap::from([("amount".into(), Value::Integer(amount))]),
|
||||
)
|
||||
}
|
||||
fn observe(world: &Worldspace, path: &str, depth: u32) -> ObjectObservation {
|
||||
world
|
||||
.observe(ObserveRequest {
|
||||
path: path.into(),
|
||||
depth,
|
||||
})
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn typed_descriptors_root_scope_and_initial_snapshots() {
|
||||
let world = world();
|
||||
let collection = world
|
||||
.fetch_interface(FetchInterfaceRequest {
|
||||
interface: reference("collection"),
|
||||
})
|
||||
.unwrap();
|
||||
let counter = world
|
||||
.fetch_interface(FetchInterfaceRequest {
|
||||
interface: reference("counter"),
|
||||
})
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
collection
|
||||
.descriptor
|
||||
.operations
|
||||
.iter()
|
||||
.map(|op| op.name.as_str())
|
||||
.collect::<Vec<_>>(),
|
||||
["create", "remove"]
|
||||
);
|
||||
for op in &collection.descriptor.operations {
|
||||
assert_eq!(op.parameters.len(), 1);
|
||||
assert_eq!(op.parameters[0].name, "name");
|
||||
assert_eq!(op.parameters[0].r#type, TypeExpr::String);
|
||||
assert_eq!(op.returns.r#type, TypeExpr::Unit);
|
||||
}
|
||||
assert_eq!(
|
||||
counter
|
||||
.descriptor
|
||||
.operations
|
||||
.iter()
|
||||
.map(|op| op.name.as_str())
|
||||
.collect::<Vec<_>>(),
|
||||
["get", "increment", "reset"]
|
||||
);
|
||||
assert_eq!(
|
||||
counter.descriptor.operations[1].parameters[0].name,
|
||||
"amount"
|
||||
);
|
||||
assert_eq!(
|
||||
counter.descriptor.operations[1].parameters[0].r#type,
|
||||
TypeExpr::Integer
|
||||
);
|
||||
assert!(
|
||||
counter
|
||||
.descriptor
|
||||
.operations
|
||||
.iter()
|
||||
.all(|op| op.returns.r#type == TypeExpr::Integer)
|
||||
);
|
||||
let initial = observe(&world, "/playground", 2);
|
||||
assert_eq!(
|
||||
initial.object.interfaces,
|
||||
[reference("document"), reference("collection")]
|
||||
);
|
||||
let children = initial.children.unwrap();
|
||||
assert_eq!(
|
||||
children
|
||||
.iter()
|
||||
.map(|c| (c.object.name.as_str(), c.object.description.as_deref()))
|
||||
.collect::<Vec<_>>(),
|
||||
[("apple", Some("3")), ("orange", Some("0"))]
|
||||
);
|
||||
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!(
|
||||
call(
|
||||
&world,
|
||||
"/playground/apple",
|
||||
"document",
|
||||
"read",
|
||||
BTreeMap::new()
|
||||
)
|
||||
.unwrap_err()
|
||||
.code,
|
||||
ProtocolErrorCode::InterfaceMismatch
|
||||
);
|
||||
assert_eq!(
|
||||
call(&world, "/book", "collection", "create", BTreeMap::new())
|
||||
.unwrap_err()
|
||||
.code,
|
||||
ProtocolErrorCode::InterfaceMismatch
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mutations_publish_new_values_and_children_without_mutating_documents() {
|
||||
let world = world();
|
||||
let before = observe(&world, "/playground/apple", 1);
|
||||
let documents: Vec<_> = ["/", "/book", "/playground"]
|
||||
.into_iter()
|
||||
.map(|path| {
|
||||
(
|
||||
path,
|
||||
call(&world, path, "document", "read", BTreeMap::new())
|
||||
.unwrap()
|
||||
.result,
|
||||
)
|
||||
})
|
||||
.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!(increment(&world, -12).unwrap().result, Value::Integer(-2));
|
||||
assert_eq!(
|
||||
call(
|
||||
&world,
|
||||
"/playground/apple",
|
||||
"counter",
|
||||
"reset",
|
||||
BTreeMap::new()
|
||||
)
|
||||
.unwrap()
|
||||
.result,
|
||||
Value::Integer(0)
|
||||
);
|
||||
assert_eq!(observe(&world, "/playground/apple", 1).object.name, "apple");
|
||||
assert_eq!(
|
||||
collection(&world, "create", "New_counter-2")
|
||||
.unwrap()
|
||||
.result,
|
||||
Value::Unit
|
||||
);
|
||||
assert_eq!(
|
||||
observe(&world, "/playground/New_counter-2", 1)
|
||||
.object
|
||||
.description
|
||||
.as_deref(),
|
||||
Some("0")
|
||||
);
|
||||
collection(&world, "remove", "apple").unwrap();
|
||||
assert_eq!(
|
||||
call(
|
||||
&world,
|
||||
"/playground/apple",
|
||||
"counter",
|
||||
"get",
|
||||
BTreeMap::new()
|
||||
)
|
||||
.unwrap_err()
|
||||
.code,
|
||||
ProtocolErrorCode::NotFound
|
||||
);
|
||||
assert!(
|
||||
!observe(&world, "/playground", 1)
|
||||
.children
|
||||
.unwrap()
|
||||
.iter()
|
||||
.any(|child| child.object.name == "apple")
|
||||
);
|
||||
collection(&world, "create", "apple").unwrap();
|
||||
assert_eq!(
|
||||
observe(&world, "/playground/apple", 1)
|
||||
.object
|
||||
.description
|
||||
.as_deref(),
|
||||
Some("0")
|
||||
);
|
||||
for (path, expected) in documents {
|
||||
assert_eq!(
|
||||
call(&world, path, "document", "read", BTreeMap::new())
|
||||
.unwrap()
|
||||
.result,
|
||||
expected
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn invalid_names_duplicates_missing_and_capacity_fail_without_mutation() {
|
||||
let world = world();
|
||||
let before = observe(&world, "/", 3);
|
||||
for name in [
|
||||
"",
|
||||
".",
|
||||
"..",
|
||||
"a/b",
|
||||
"a b",
|
||||
"a\nb",
|
||||
"<script>",
|
||||
"é",
|
||||
"a.b",
|
||||
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
|
||||
] {
|
||||
for operation in ["create", "remove"] {
|
||||
assert!(
|
||||
collection(&world, operation, name).is_err(),
|
||||
"{operation} {name:?}"
|
||||
);
|
||||
assert_eq!(observe(&world, "/", 3), before);
|
||||
}
|
||||
}
|
||||
assert!(collection(&world, "create", "apple").is_err());
|
||||
assert!(collection(&world, "remove", "missing").is_err());
|
||||
assert_eq!(observe(&world, "/", 3), before);
|
||||
collection(&world, "create", &"a".repeat(32)).unwrap();
|
||||
for index in 3..MAX_COUNTERS {
|
||||
collection(&world, "create", &format!("counter-{index}")).unwrap();
|
||||
}
|
||||
let full = observe(&world, "/playground", 1);
|
||||
assert_eq!(full.children.as_ref().unwrap().len(), MAX_COUNTERS);
|
||||
assert_eq!(
|
||||
collection(&world, "create", "over-limit").unwrap_err().code,
|
||||
ProtocolErrorCode::ResourceLimitExceeded
|
||||
);
|
||||
assert_eq!(observe(&world, "/playground", 1), full);
|
||||
collection(&world, "remove", "orange").unwrap();
|
||||
collection(&world, "create", "over-limit").unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn integer_and_typed_argument_failures_are_atomic() {
|
||||
let world = world();
|
||||
assert_eq!(
|
||||
increment(&world, MAX_SAFE_INTEGER - 3).unwrap().result,
|
||||
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);
|
||||
}
|
||||
call(
|
||||
&world,
|
||||
"/playground/apple",
|
||||
"counter",
|
||||
"reset",
|
||||
BTreeMap::new(),
|
||||
)
|
||||
.unwrap();
|
||||
increment(&world, MIN_SAFE_INTEGER).unwrap();
|
||||
let minimum = observe(&world, "/playground/apple", 1);
|
||||
assert!(increment(&world, -1).is_err());
|
||||
for args in [
|
||||
BTreeMap::new(),
|
||||
BTreeMap::from([("amount".into(), Value::String("1".into()))]),
|
||||
BTreeMap::from([
|
||||
("amount".into(), Value::Integer(1)),
|
||||
("extra".into(), Value::Unit),
|
||||
]),
|
||||
] {
|
||||
assert_eq!(
|
||||
call(&world, "/playground/apple", "counter", "increment", args)
|
||||
.unwrap_err()
|
||||
.code,
|
||||
ProtocolErrorCode::InvalidArguments
|
||||
);
|
||||
assert_eq!(observe(&world, "/playground/apple", 1), minimum);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn new_worldspace_resets_state_and_manifest_documents_cannot_be_shadowed() {
|
||||
let first = world();
|
||||
increment(&first, 12).unwrap();
|
||||
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")
|
||||
);
|
||||
assert_eq!(
|
||||
observe(&fresh, "/playground", 1)
|
||||
.children
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|child| child.object.name.clone())
|
||||
.collect::<Vec<_>>(),
|
||||
["apple", "orange"]
|
||||
);
|
||||
assert!(Worldspace::from_json(r#"{"nodes":[
|
||||
{"path":"/","name":"","displayPath":"/","title":"Root","href":"/","html":""},
|
||||
{"path":"/playground","name":"playground","parent":"/","displayPath":"/playground","title":"Playground","href":"/playground/","html":""},
|
||||
{"path":"/playground/apple","name":"apple","parent":"/playground","displayPath":"/playground/apple","title":"Document","href":"/apple/","html":""}
|
||||
]}"#).is_err());
|
||||
}
|
||||
Reference in New Issue
Block a user