use liveactivity::{
AndroidContent, Error, LayoutStartInput, PresentationStyle, Timer, Validate, validate_request,
};
use serde_json::{Value, json};
const LARGEST_REVISION: u64 = 9_007_199_254_740_991;
const LARGEST_TIME: u64 = 8_640_000_000_000_000;
fn progress() -> Value {
json!({"activityKey":"job:1","schemaVersion":1,"template":"progress","fallback":"notification",
"snapshot":{"revision":0,"title":"Job","status":"Running","progress":0.3}})
}
fn timer() -> Value {
json!({"id":"opaque-handle-1","snapshot":{"revision":1,"title":"Timer","status":"Running",
"timer":{"startedAt":0,"endsAt":1}}})
}
fn end() -> Value {
json!({"id":"opaque-handle-1","policy":{"kind":"default"}})
}
fn with(mut request: Value, pointer: &str, value: Value) -> Value {
let (parent, member) = pointer.rsplit_once('/').unwrap();
request.pointer_mut(parent).unwrap()[member] = value;
request
}
fn without(mut request: Value, pointer: &str) -> Value {
let (parent, member) = pointer.rsplit_once('/').unwrap();
let parent = request
.pointer_mut(parent)
.unwrap()
.as_object_mut()
.unwrap();
parent.remove(member).unwrap();
request
}
fn refused() -> liveactivity::Result<()> {
Err(Error::invalid())
}
#[test]
fn req_c_008_the_fixtures_of_the_templates_get_their_verdict() {
let fixtures: Value = serde_json::from_str(include_str!("fixtures/requests.json")).unwrap();
let fixtures = fixtures.as_array().unwrap();
assert_eq!(fixtures.len(), 11);
for fixture in fixtures {
let request = &fixture["request"];
let verdict = validate_request(
request["command"].as_str().unwrap(),
request["input"].clone(),
);
let expected = if fixture["valid"] == true {
Ok(())
} else {
refused()
};
assert_eq!(verdict, expected, "{}", fixture["name"]);
}
}
#[test]
fn req_c_008_a_template_request_is_taken_at_each_limit_and_refused_past_it() {
type Request = fn() -> Value;
let requests: [(&str, Request); 3] = [("start", progress), ("update", timer), ("end", end)];
for (command, pointer, last, past) in [
(
0,
"/activityKey",
json!("a".repeat(128)),
json!("a".repeat(129)),
),
(
0,
"/snapshot/title",
json!("😀".repeat(120)),
json!("😀".repeat(121)),
),
(
0,
"/snapshot/status",
json!("한".repeat(240)),
json!("한".repeat(241)),
),
(0, "/snapshot/progress", json!(0), json!(-0.1)),
(0, "/snapshot/progress", json!(1), json!(1.1)),
(
0,
"/snapshot/revision",
json!(LARGEST_REVISION),
json!(LARGEST_REVISION + 1),
),
(
0,
"/snapshot/staleAt",
json!(LARGEST_TIME),
json!(LARGEST_TIME + 1),
),
(1, "/id", json!("한".repeat(256)), json!("한".repeat(257))),
(
1,
"/snapshot/timer/endsAt",
json!(LARGEST_TIME),
json!(LARGEST_TIME + 1),
),
(2, "/id", json!("한".repeat(256)), json!("한".repeat(257))),
(
2,
"/policy",
json!({"kind":"after","at":LARGEST_TIME}),
json!({"kind":"after","at":LARGEST_TIME + 1}),
),
] {
let (command, request) = requests[command];
let taken = validate_request(command, with(request(), pointer, last));
assert_eq!(taken, Ok(()), "{command} {pointer}");
let beyond = validate_request(command, with(request(), pointer, past));
assert_eq!(beyond, refused(), "{command} {pointer}");
}
let mut input = progress();
input["activityKey"] = json!("a".repeat(128));
input["snapshot"] = json!({"revision":LARGEST_REVISION,"title":"😀".repeat(120),
"status":"한".repeat(240),"progress":1,"staleAt":LARGEST_TIME});
assert_eq!(validate_request("start", input), Ok(()));
}
#[test]
fn req_c_008_a_template_request_of_a_wrong_shape_is_refused() {
for (command, request) in [("start", progress()), ("update", timer()), ("end", end())] {
assert_eq!(validate_request(command, request), Ok(()), "{command}");
}
for key in ["9a", "A.b_c:d-e"] {
let start = with(progress(), "/activityKey", json!(key));
assert_eq!(validate_request("start", start), Ok(()), "{key}");
}
for (pointer, value) in [
("/activityKey", json!("../secret")),
("/activityKey", json!(".hidden")),
("/activityKey", json!("a/b")),
("/activityKey", json!("key\n")),
("/activityKey", json!("é")),
("/activityKey", json!("a한")),
("/activityKey", json!("")),
("/schemaVersion", json!(2)),
("/fallback", json!(null)),
("/snapshot/title", json!(" \n\t")),
("/snapshot/status", json!(" \n\t")),
("/snapshot/status", json!("\u{feff}")),
("/snapshot/progress", json!(null)),
("/snapshot/progress", json!("private-input")),
("/snapshot/revision", json!(-1)),
("/snapshot/revision", json!(0.5)),
("/snapshot/staleAt", json!(null)),
("/snapshot/staleAt", json!(-1)),
("/snapshot/staleAt", json!(0.5)),
("/snapshot/token", json!("secret")),
("/unsafePath", json!("/tmp")),
("/snapshot", json!(null)),
] {
let verdict = validate_request("start", with(progress(), pointer, value));
assert_eq!(verdict, refused(), "{pointer}");
}
let status = with(progress(), "/snapshot/status", json!("\u{0085}"));
assert_eq!(validate_request("start", status), Ok(()));
assert_eq!(validate_request("start", json!([])), refused());
for (command, request) in [
("update", with(timer(), "/extra", json!(1))),
("update", with(timer(), "/snapshot/timer/extra", json!(1))),
("update", with(timer(), "/snapshot", json!(null))),
("update", with(timer(), "/id", json!(" "))),
("end", with(end(), "/extra", json!(1))),
("end", with(end(), "/policy/extra", json!(1))),
("end", with(end(), "/policy", json!(null))),
("end", with(end(), "/id", json!(" "))),
] {
let verdict = validate_request(command, request.clone());
assert_eq!(verdict, refused(), "{command} {request}");
}
}
#[test]
fn req_c_008_a_snapshot_has_the_one_presentation_of_its_template() {
let a_timer = json!({"startedAt":0,"endsAt":1});
let timed = with(
without(progress(), "/snapshot/progress"),
"/snapshot/timer",
a_timer.clone(),
);
let of_a_timer = with(timed.clone(), "/template", json!("timer"));
assert_eq!(validate_request("start", of_a_timer), Ok(()));
for (case, start) in [
("both", with(progress(), "/snapshot/timer", a_timer)),
("neither", without(progress(), "/snapshot/progress")),
("a timer for the progress template", timed),
(
"a progress for the timer template",
with(progress(), "/template", json!("timer")),
),
] {
assert_eq!(validate_request("start", start), refused(), "{case}");
}
let both = with(timer(), "/snapshot/progress", json!(0.5));
assert_eq!(validate_request("update", both), refused());
let neither = without(timer(), "/snapshot/timer");
assert_eq!(validate_request("update", neither), refused());
}
#[test]
fn req_c_008_a_timer_ends_after_its_start_and_an_end_says_when_only_with_after() {
for (started_at, ends_at, valid) in [(0, 1, true), (1, 1, false), (2, 1, false)] {
let times = json!({"startedAt":started_at,"endsAt":ends_at});
let verdict = validate_request("update", with(timer(), "/snapshot/timer", times));
assert_eq!(verdict.is_ok(), valid, "{started_at} to {ends_at}");
}
for (policy, valid) in [
(json!({"kind":"default"}), true),
(json!({"kind":"immediate"}), true),
(json!({"kind":"after","at":0}), true),
(json!({"kind":"after","at":-1}), false),
(json!({"kind":"after","at":0.5}), false),
(json!({"kind":"after"}), false),
(json!({"kind":"immediate","at":0}), false),
(json!({"kind":"default","at":0}), false),
(json!({"kind":"later","at":0}), false),
(json!({}), false),
] {
let verdict = validate_request("end", with(end(), "/policy", policy.clone()));
assert_eq!(verdict.is_ok(), valid, "{policy}");
}
}
#[test]
fn req_c_009_a_number_keeps_its_value_when_read_from_json_text() {
for text in [
"977.9461044818163",
"0.0018531278500143397",
"96.00000000000001",
"0.09999999999999999",
"3875413157790899.5",
"8640000000000000.6",
] {
let read: Value = serde_json::from_str(text).unwrap();
assert_eq!(read.as_f64(), text.parse().ok(), "{text}");
}
let start = |style: &str, values: &str| {
let text = format!(
r#"{{"activityKey":"job:1","schemaVersion":2,"fallback":"none","layout":{{"lockScreen":
{{"type":"vstack","children":[{{"type":"text","value":"a","style":"s"}},
{{"type":"timerText","start":"from","end":"to"}}]}}}},"styles":{{"s":{style}}},
"snapshot":{{"revision":0,"values":{values}}}}}"#
);
validate_request("start_layout", serde_json::from_str(&text).unwrap())
};
let times = r#"{"from":1,"to":2}"#;
assert_eq!(start(r#"{"size":12}"#, times), Ok(()));
assert_eq!(
start(r#"{"size":96,"minimumScaleFactor":0.1}"#, times),
Ok(())
);
for (style, values) in [
(r#"{"size":96.00000000000001}"#, times),
(r#"{"minimumScaleFactor":0.09999999999999999}"#, times),
(r#"{"size":12}"#, r#"{"from":0.9999999999999999,"to":2}"#),
(
r#"{"size":12}"#,
r#"{"from":3875413157790899.5,"to":3875413157790901}"#,
),
(r#"{"size":12}"#, r#"{"from":1,"to":8640000000000000.6}"#),
] {
assert_eq!(start(style, values), refused(), "{style} {values}");
}
}
#[test]
fn req_c_010_a_refusal_repeats_nothing_of_the_request() {
let secret = "private-input";
for request in [
with(progress(), "/snapshot/progress", json!(secret)),
with(progress(), "/activityKey", json!(format!("../{secret}"))),
with(progress(), "/snapshot/title", json!(secret.repeat(40))),
with(progress(), &format!("/{secret}"), json!(1)),
] {
let error = validate_request("start", request).unwrap_err();
assert_eq!(error, Error::invalid());
assert!(!format!("{error:?} {error}").contains(secret));
}
}
fn layout() -> Value {
json!({"activityKey":"demo:1","schemaVersion":2,"fallback":"none",
"layout":{"lockScreen":{"type":"text","value":"a"}},"snapshot":{"revision":0,"values":{}}})
}
#[test]
fn req_c_005_a_request_is_read_under_its_own_command_and_under_no_other_name() {
assert_eq!(validate_request("start", progress()), Ok(()));
assert_eq!(validate_request("start_layout", layout()), Ok(()));
assert_eq!(validate_request("start", layout()), refused());
assert_eq!(validate_request("start_layout", progress()), refused());
for command in [
"start ",
"Start",
"startLayout",
"start_layout ",
"list_layout",
"endAll",
"",
] {
for request in [json!({}), progress(), layout(), end()] {
assert_eq!(validate_request(command, request), refused(), "{command:?}");
}
}
}
fn started(member: &str, value: Value) -> liveactivity::Result<()> {
validate_request("start_layout", with(layout(), &format!("/{member}"), value))
}
fn updated(member: &str, value: Value) -> liveactivity::Result<()> {
let update = json!({"id":"opaque-handle-1","snapshot":{"revision":1,"values":{}}});
validate_request("update_layout", with(update, &format!("/{member}"), value))
}
fn ended(member: &str, value: Value) -> liveactivity::Result<()> {
validate_request("end_layout", with(end(), &format!("/{member}"), value))
}
fn alert() -> Value {
json!({"title":"Your ride","body":"It starts now"})
}
fn public_version() -> Value {
json!({"title":"Delivery","status":"On the way"})
}
fn with_a_public_content(visibility: Option<&str>) -> Value {
let content = json!({"title":"On time","status":"Gate 4",
"presentation":{"style":{"kind":"standard"},"publicVersion":public_version()}});
let start = with(layout(), "/snapshot/android", content);
match visibility {
Some(visibility) => with(start, "/android", json!({"visibility":visibility})),
None => start,
}
}
#[test]
fn req_c_011_the_sound_of_an_alert_is_the_name_of_a_file() {
assert_eq!(updated("alert", alert()), Ok(()));
for sound in ["chime.aiff", "a", "0", "A_b-c.d", &"a".repeat(64)] {
let alert = with(alert(), "/sound", json!(sound));
assert_eq!(updated("alert", alert), Ok(()), "{sound}");
}
for sound in [
json!(""),
json!(".aiff"),
json!("-a"),
json!("a b"),
json!("sounds/chime.aiff"),
json!("a".repeat(65)),
json!("é.aiff"),
json!("chime.aiff\n"),
json!(1),
json!(null),
json!(["a"]),
] {
let alert = with(alert(), "/sound", sound.clone());
assert_eq!(updated("alert", alert), refused(), "{sound}");
}
}
#[test]
fn req_c_011_the_options_of_a_start_have_their_members_and_their_json_types() {
for transient in [true, false] {
assert_eq!(started("ios", json!({"transient":transient})), Ok(()));
}
for ios in [
json!({}),
json!({"transient":"yes"}),
json!({"transient":1}),
json!({"transient":null}),
json!({"transient":true,"extra":1}),
json!(null),
json!([]),
json!(true),
] {
assert_eq!(started("ios", ios.clone()), refused(), "{ios}");
}
for at in [0, 1_900_000_000_000, LARGEST_TIME] {
let scheduled = json!({"at":at,"alert":alert()});
assert_eq!(started("scheduled", scheduled), Ok(()), "{at}");
}
let chime = with(alert(), "/sound", json!("chime.aiff"));
assert_eq!(started("scheduled", json!({"at":1,"alert":chime})), Ok(()));
for scheduled in [
json!({}),
json!({"at":1}),
json!({"alert":alert()}),
json!({"at":-1,"alert":alert()}),
json!({"at":0.5,"alert":alert()}),
json!({"at":LARGEST_TIME + 1,"alert":alert()}),
json!({"at":"1","alert":alert()}),
json!({"at":null,"alert":alert()}),
json!({"at":1,"alert":null}),
json!({"at":1,"alert":{"title":"a"}}),
json!({"at":1,"alert":with(alert(), "/title", json!(" "))}),
json!({"at":1,"alert":with(alert(), "/sound", json!("a b"))}),
json!({"at":1,"alert":with(alert(), "/extra", json!(1))}),
json!({"at":1,"alert":alert(),"extra":1}),
json!(null),
json!([]),
json!(1),
] {
assert_eq!(
started("scheduled", scheduled.clone()),
refused(),
"{scheduled}"
);
}
for android in [
json!({"visibility":"private"}),
json!({"visibility":"private","publicVersion":public_version(),"placement":"foreground"}),
json!({"visibility":"public","placement":"tagged"}),
json!({"visibility":"secret","placement":"foreground"}),
json!({"visibility":"private",
"publicVersion":{"title":"😀".repeat(120),"status":"한".repeat(240)}}),
] {
assert_eq!(started("android", android.clone()), Ok(()), "{android}");
}
for placement in [
json!("Tagged"),
json!("service"),
json!(""),
json!(null),
json!(1),
json!(["tagged"]),
] {
let android = json!({"visibility":"private","placement":placement});
assert_eq!(started("android", android), refused(), "{placement}");
}
for wrong in [
json!({}),
json!({"title":"a"}),
json!({"status":"b"}),
json!({"title":"","status":"b"}),
json!({"title":"a","status":" "}),
json!({"title":"a".repeat(121),"status":"b"}),
json!({"title":"a","status":"b".repeat(241)}),
with(public_version(), "/extra", json!(1)),
json!({"title":1,"status":"b"}),
json!(null),
json!([]),
json!("a"),
] {
let android = json!({"visibility":"private","publicVersion":wrong});
assert_eq!(started("android", android), refused(), "{wrong}");
}
for visibility in ["public", "secret"] {
let android = json!({"visibility":visibility,"publicVersion":public_version()});
assert_eq!(started("android", android), refused(), "{visibility}");
}
for android in [
json!({"visibility":"hidden"}),
json!({"placement":"tagged"}),
json!({"visibility":"private","lights":true}),
] {
assert_eq!(started("android", android.clone()), refused(), "{android}");
}
}
#[test]
fn req_c_011_a_public_version_in_a_content_is_of_a_private_start() {
for (visibility, verdict) in [
(None, Ok(())),
(Some("private"), Ok(())),
(Some("public"), refused()),
(Some("secret"), refused()),
] {
let start = with_a_public_content(visibility);
assert_eq!(
validate_request("start_layout", start),
verdict,
"{visibility:?}"
);
}
let snapshot = with_a_public_content(None)["snapshot"].clone();
assert_eq!(updated("snapshot", snapshot), Ok(()));
}
#[test]
fn req_c_011_each_member_belongs_to_its_command() {
assert_eq!(validate_request("start_layout", layout()), Ok(()));
assert_eq!(validate_request("end_layout", end()), Ok(()));
let options = [
("ios", json!({"transient":true})),
("scheduled", json!({"at":1,"alert":alert()})),
("android", json!({"visibility":"private"})),
];
for (member, value) in options {
assert_eq!(started(member, value.clone()), Ok(()), "{member}");
assert_eq!(updated(member, value.clone()), refused(), "{member}");
assert_eq!(ended(member, value), refused(), "{member}");
}
assert_eq!(updated("alert", alert()), Ok(()));
assert_eq!(started("alert", alert()), refused());
assert_eq!(ended("alert", alert()), refused());
}
#[test]
fn req_c_011_a_typed_start_writes_its_members_as_it_read_them() {
let full = with_a_public_content(Some("private"));
let full = with(full, "/android/publicVersion", public_version());
let full = with(full, "/android/placement", json!("foreground"));
let full = with(full, "/ios", json!({"transient":true}));
let chime = with(alert(), "/sound", json!("chime.aiff"));
let full = with(full, "/scheduled", json!({"at":1,"alert":chime}));
let plain = with(layout(), "/scheduled", json!({"at":1,"alert":alert()}));
let spelled = with(layout(), "/ios", json!({"transient":false}));
for start in [full, layout(), plain, spelled] {
let typed: LayoutStartInput = serde_json::from_value(start.clone()).unwrap();
assert_eq!(typed.validate(), Ok(()), "{start}");
assert_eq!(serde_json::to_value(&typed).unwrap(), start);
}
}
#[test]
fn req_c_006_a_timer_style_of_an_android_content_reads_from_json_and_is_checked_in_full() {
let content = |started_at: u64, ends_at: u64| {
json!({"title":"Mochi’s nap","status":"Dozing off","presentation":{"style":{"kind":"timer",
"value":{"timer":{"startedAt":started_at,"endsAt":ends_at},"countsDown":true}}}})
};
let typed: AndroidContent = serde_json::from_value(content(1_000, 2_000)).unwrap();
let PresentationStyle::Timer(style) = &typed.presentation.style else {
panic!("the style is a timer");
};
let read = (
style.timer.started_at,
style.timer.ends_at,
style.counts_down,
);
assert_eq!(read, (1_000, 2_000, true));
assert_eq!(serde_json::to_value(&typed).unwrap(), content(1_000, 2_000));
let template = Timer {
started_at: 1_000,
ends_at: 2_000,
};
assert_eq!(
serde_json::to_value(&template).unwrap(),
content(1_000, 2_000)["presentation"]["style"]["value"]["timer"]
);
let start = |android: Value| with(layout(), "/snapshot/android", android);
assert_eq!(
validate_request("start_layout", start(content(1_000, 2_000))),
Ok(())
);
let value = "/presentation/style/value";
for (case, broken) in [
("ends at its start", content(2_000, 2_000)),
("ends before its start", content(3_000, 2_000)),
(
"a null end",
with(
content(1_000, 2_000),
&format!("{value}/timer/endsAt"),
json!(null),
),
),
(
"a start of another type",
with(
content(1_000, 2_000),
&format!("{value}/timer/startedAt"),
json!("1000"),
),
),
(
"no start",
without(content(1_000, 2_000), &format!("{value}/timer/startedAt")),
),
(
"no timer",
without(content(1_000, 2_000), &format!("{value}/timer")),
),
(
"a member beside the timer's",
with(
content(1_000, 2_000),
&format!("{value}/timer/ends_at"),
json!(3_000),
),
),
(
"a member beside the style's",
with(content(1_000, 2_000), &format!("{value}/extra"), json!(1)),
),
(
"a direction of another type",
with(
content(1_000, 2_000),
&format!("{value}/countsDown"),
json!("yes"),
),
),
(
"no direction",
without(content(1_000, 2_000), &format!("{value}/countsDown")),
),
] {
assert_eq!(
validate_request("start_layout", start(broken)),
refused(),
"{case}"
);
}
}