use crate::protocol::ContractViolation;
use crate::{ActionSpec, OperationKind, ResourceSpec};
#[must_use]
pub fn hidden_from_agents(resources: &[ResourceSpec]) -> Vec<ContractViolation> {
let mut hidden = Vec::new();
for resource in resources {
for child in &resource.children {
walk(child, &mut hidden);
}
}
hidden
}
fn walk(resource: &ResourceSpec, hidden: &mut Vec<ContractViolation>) {
if let Some(view) = disclosing_view(resource).filter(|view| is_private(view)) {
hide(resource, resource, view, hidden);
return;
}
for child in &resource.children {
walk(child, hidden);
}
}
fn disclosing_view(resource: &ResourceSpec) -> Option<&ActionSpec> {
let mut first: Option<&ActionSpec> = None;
for action in resource
.actions
.iter()
.filter(|action| action.kind == OperationKind::View)
{
if first.is_none_or(|first| {
action.contract.disclosure_rank() < first.contract.disclosure_rank()
}) {
first = Some(action);
}
}
first.or_else(|| resource.children.iter().find_map(disclosing_view))
}
fn is_private(view: &ActionSpec) -> bool {
!view.contract.discloses_resource()
}
fn hide(
owner: &ResourceSpec,
resource: &ResourceSpec,
view: &ActionSpec,
hidden: &mut Vec<ContractViolation>,
) {
for action in resource.actions.iter().filter(|action| match action.kind {
_ if crate::kind_is_a_drawn_control(&resource.kind) => false,
OperationKind::View => false,
OperationKind::Effect => !action.contract.only_shows() && action.contract.reaches_agents(),
OperationKind::Document => action.contract.reaches_agents(),
}) {
hidden.push(ContractViolation {
address: format!("{}\u{1f}{}", resource.id.as_str(), action.id),
reason: format!(
"an agent is never shown it: {} is disclosed only through the private view \
`{}`, which keeps the resource and everything on it from agents. Disclose it \
through a document read or a presence view, or move that view off it",
owner.id.as_str(),
view.id
),
});
}
for child in &resource.children {
hide(owner, child, view, hidden);
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{ActionContract, OperationKind};
fn action(id: &str, kind: OperationKind, contract: ActionContract) -> ActionSpec {
let mut spec = ActionSpec::typed_operation(id, id, "", kind, id, Vec::new(), |_| {
crate::EncodedAction {
type_id: "test".into(),
payload: Vec::new(),
}
});
spec.type_mut().contract = contract;
spec
}
fn view(id: &str, tags: &[&str]) -> ActionSpec {
let mut contract = ActionContract::view();
contract.tags = tags.iter().map(|tag| (*tag).to_owned()).collect();
action(id, OperationKind::View, contract)
}
fn effect(id: &str) -> ActionSpec {
action(id, OperationKind::Effect, ActionContract::default())
}
fn app(children: Vec<ResourceSpec>) -> Vec<ResourceSpec> {
let mut root = ResourceSpec::new("app", "app", "App");
root.actions.push(view("show", &[]));
root.children = children;
vec![root]
}
fn row(id: &str, actions: Vec<ActionSpec>) -> ResourceSpec {
let mut row = ResourceSpec::new(id, id, id);
row.actions = actions;
row
}
tilo_test_env::scenario! {
fn a_private_disclosure_hides_agent_actions_below_it;
phase a_private_view_hides_every_agent_action_on_its_resource_and_below;
{
let mut page = row(
"page",
vec![effect("read"), view("stop", &[]), effect("click")],
);
page.children.push(row("frame", vec![effect("frame-read")]));
let hidden = hidden_from_agents(&app(vec![page]));
let addresses: Vec<&str> = hidden.iter().map(|v| v.address.as_str()).collect();
assert_eq!(
addresses,
["page\u{1f}read", "page\u{1f}click", "frame\u{1f}frame-read"]
);
assert!(hidden[0].reason.contains("`stop`"), "{}", hidden[0].reason);
}
phase a_resource_without_a_private_disclosure_hides_nothing;
{
let resources = app(vec![
row("page", vec![effect("read"), effect("click")]),
row(
"document",
vec![
view("ui", &[]),
view("read-document", &[crate::DOCUMENT_READ_TAG]),
effect("edit"),
],
),
row(
"mine",
vec![view("look", &[crate::PRESENCE_VIEW_TAG]), effect("act")],
),
row("panel", vec![view("show-panel", &[])]),
row(
"session",
vec![
view("show-history", &[]),
view("review", &[crate::RESOURCE_READ_TAG]),
effect("run"),
],
),
]);
assert_eq!(hidden_from_agents(&resources), []);
let mut root = row("app", vec![view("show", &[]), effect("act")]);
root.children.clear();
assert_eq!(hidden_from_agents(&[root]), []);
}
}
}