| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
use crate::common::*; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
const SPEC_JSON: &str = include_str!("../openapi.json"); |
| 29 |
|
| 30 |
fn spec() -> serde_json::Value { |
| 31 |
serde_json::from_str(SPEC_JSON).expect("the vendored spec is valid JSON") |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
fn assert_matches_schema(schema_name: &str, body: &serde_json::Value) { |
| 40 |
let mut doc = spec(); |
| 41 |
doc.as_object_mut().expect("spec root is an object").insert( |
| 42 |
"$ref".to_string(), |
| 43 |
serde_json::Value::String(format!("#/components/schemas/{schema_name}")), |
| 44 |
); |
| 45 |
|
| 46 |
let validator = jsonschema::validator_for(&doc) |
| 47 |
.unwrap_or_else(|e| panic!("{schema_name} is not a usable schema: {e}")); |
| 48 |
|
| 49 |
let errors: Vec<String> = validator.iter_errors(body).map(|e| e.to_string()).collect(); |
| 50 |
assert!( |
| 51 |
errors.is_empty(), |
| 52 |
"fixture does not match the server's {schema_name} schema:\n {}\n\nfixture was:\n{}", |
| 53 |
errors.join("\n "), |
| 54 |
serde_json::to_string_pretty(body).unwrap_or_default(), |
| 55 |
); |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
#[test] |
| 61 |
fn auth_response_fixture_matches_the_spec() { |
| 62 |
assert_matches_schema("SyncAuthResponse", &auth_response_json()); |
| 63 |
} |
| 64 |
|
| 65 |
#[test] |
| 66 |
fn device_fixture_matches_the_spec() { |
| 67 |
assert_matches_schema("SyncDeviceResponse", &device_json()); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
#[test] |
| 78 |
fn push_response_fixture_matches_the_spec() { |
| 79 |
|
| 80 |
assert_matches_schema("PushResponse", &json!({"cursor": 1})); |
| 81 |
} |
| 82 |
|
| 83 |
#[test] |
| 84 |
fn pull_response_fixture_matches_the_spec() { |
| 85 |
|
| 86 |
assert_matches_schema( |
| 87 |
"PullResponse", |
| 88 |
&json!({"changes": [], "cursor": 50, "has_more": true}), |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
#[test] |
| 93 |
fn pull_change_entry_fixture_matches_the_spec() { |
| 94 |
|
| 95 |
let (_user_id, _app_id) = test_ids(); |
| 96 |
assert_matches_schema( |
| 97 |
"PullChangeEntry", |
| 98 |
&json!({ |
| 99 |
"seq": 1, |
| 100 |
"device_id": Uuid::new_v4(), |
| 101 |
"table": "tasks", |
| 102 |
"op": "INSERT", |
| 103 |
"row_id": "row-1", |
| 104 |
"timestamp": "2025-06-01T12:00:00Z", |
| 105 |
"data": "ciphertext", |
| 106 |
}), |
| 107 |
); |
| 108 |
} |
| 109 |
|
| 110 |
#[test] |
| 111 |
fn blob_upload_url_fixture_matches_the_spec() { |
| 112 |
|
| 113 |
assert_matches_schema( |
| 114 |
"BlobUploadUrlResponse", |
| 115 |
&json!({"upload_url": "https://s3.example.com/put", "already_exists": false}), |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
#[test] |
| 120 |
fn blob_download_url_fixture_matches_the_spec() { |
| 121 |
|
| 122 |
assert_matches_schema( |
| 123 |
"BlobDownloadUrlResponse", |
| 124 |
&json!({"download_url": "https://s3.example.com/get"}), |
| 125 |
); |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
const CLIENT_PATHS: &[&str] = &[ |
| 140 |
"/api/v1/sync/auth", |
| 141 |
"/api/v1/sync/validate-app", |
| 142 |
"/api/v1/sync/devices", |
| 143 |
"/api/v1/sync/push", |
| 144 |
"/api/v1/sync/pull", |
| 145 |
"/api/v1/sync/subscribe", |
| 146 |
"/api/v1/sync/status", |
| 147 |
"/api/v1/sync/keys", |
| 148 |
"/api/v1/sync/blobs/upload", |
| 149 |
"/api/v1/sync/blobs/confirm", |
| 150 |
"/api/v1/sync/blobs/download", |
| 151 |
"/api/v1/sync/blobs/multipart/start", |
| 152 |
"/api/v1/sync/blobs/multipart/parts", |
| 153 |
"/api/v1/sync/blobs/multipart/complete", |
| 154 |
"/api/v1/sync/blobs/multipart/abort", |
| 155 |
"/api/v1/sync/subscription", |
| 156 |
"/api/v1/sync/subscription/checkout", |
| 157 |
"/api/v1/sync/subscription/quote", |
| 158 |
"/api/v1/sync/subscription/storage-cap", |
| 159 |
"/api/v1/sync/app/pricing", |
| 160 |
"/api/v1/sync/account", |
| 161 |
"/api/v1/sync/ota", |
| 162 |
"/api/v1/sync/groups", |
| 163 |
"/api/v1/sync/invitations", |
| 164 |
]; |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
const UNDOCUMENTED: &[&str] = &[ |
| 188 |
"/api/v1/sync/subscribe", |
| 189 |
"/api/v1/sync/ota", |
| 190 |
"/api/v1/sync/groups", |
| 191 |
"/api/v1/sync/invitations", |
| 192 |
]; |
| 193 |
|
| 194 |
fn documented_paths() -> std::collections::BTreeSet<String> { |
| 195 |
spec()["paths"] |
| 196 |
.as_object() |
| 197 |
.expect("spec has a paths object") |
| 198 |
.keys() |
| 199 |
.cloned() |
| 200 |
.collect() |
| 201 |
} |
| 202 |
|
| 203 |
#[test] |
| 204 |
fn every_client_path_is_documented_or_pinned_as_a_known_gap() { |
| 205 |
let documented = documented_paths(); |
| 206 |
let missing: Vec<&str> = CLIENT_PATHS |
| 207 |
.iter() |
| 208 |
.copied() |
| 209 |
.filter(|p| !documented.contains(*p)) |
| 210 |
.collect(); |
| 211 |
|
| 212 |
assert_eq!( |
| 213 |
missing, UNDOCUMENTED, |
| 214 |
"the set of undocumented client endpoints changed.\n\ |
| 215 |
If it shrank, remove the fixed entries from UNDOCUMENTED.\n\ |
| 216 |
If it grew, the new endpoint needs a utoipa annotation on the server \ |
| 217 |
and a re-exported spec, not an entry here." |
| 218 |
); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
#[test] |
| 226 |
fn every_client_path_is_listed_here() { |
| 227 |
let src = include_str!("../../src/client/mod.rs"); |
| 228 |
let mut found: Vec<String> = Vec::new(); |
| 229 |
for (idx, _) in src.match_indices("/api/v1/sync") { |
| 230 |
let tail = &src[idx..]; |
| 231 |
let end = tail |
| 232 |
.find(|c: char| !(c.is_ascii_alphanumeric() || "/-_".contains(c))) |
| 233 |
.unwrap_or(tail.len()); |
| 234 |
let path = tail[..end].trim_end_matches('/').to_string(); |
| 235 |
if !found.contains(&path) { |
| 236 |
found.push(path); |
| 237 |
} |
| 238 |
} |
| 239 |
found.sort(); |
| 240 |
|
| 241 |
let mut listed: Vec<String> = CLIENT_PATHS.iter().map(|s| (*s).to_string()).collect(); |
| 242 |
listed.sort(); |
| 243 |
|
| 244 |
assert_eq!( |
| 245 |
found, listed, |
| 246 |
"CLIENT_PATHS has drifted from Endpoints::new in src/client/mod.rs" |
| 247 |
); |
| 248 |
} |
| 249 |
|