| 1 |
|
| 2 |
|
| 3 |
use crate::ids::{AppId, DeviceId, GroupId, UserId}; |
| 4 |
use chrono::{DateTime, Utc}; |
| 5 |
use serde::{Deserialize, Serialize}; |
| 6 |
use std::fmt; |
| 7 |
use uuid::Uuid; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)] |
| 17 |
#[serde(rename_all = "UPPERCASE")] |
| 18 |
pub enum ChangeOp { |
| 19 |
Insert, |
| 20 |
Update, |
| 21 |
Delete, |
| 22 |
} |
| 23 |
|
| 24 |
impl fmt::Display for ChangeOp { |
| 25 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 26 |
match self { |
| 27 |
ChangeOp::Insert => write!(f, "INSERT"), |
| 28 |
ChangeOp::Update => write!(f, "UPDATE"), |
| 29 |
ChangeOp::Delete => write!(f, "DELETE"), |
| 30 |
} |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
impl ChangeOp { |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
pub fn from_str_opt(s: &str) -> Option<Self> { |
| 40 |
match s { |
| 41 |
"INSERT" => Some(ChangeOp::Insert), |
| 42 |
"UPDATE" => Some(ChangeOp::Update), |
| 43 |
"DELETE" => Some(ChangeOp::Delete), |
| 44 |
_ => None, |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] |
| 80 |
pub struct Hlc { |
| 81 |
|
| 82 |
pub wall_ms: i64, |
| 83 |
|
| 84 |
pub counter: u32, |
| 85 |
|
| 86 |
|
| 87 |
pub node: DeviceId, |
| 88 |
} |
| 89 |
|
| 90 |
impl Hlc { |
| 91 |
|
| 92 |
|
| 93 |
pub fn zero(node: DeviceId) -> Self { |
| 94 |
Hlc { |
| 95 |
wall_ms: 0, |
| 96 |
counter: 0, |
| 97 |
node, |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
pub fn from_legacy(timestamp_ms: i64, node: DeviceId) -> Self { |
| 105 |
Hlc { |
| 106 |
wall_ms: timestamp_ms, |
| 107 |
counter: 0, |
| 108 |
node, |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
pub fn tick(prev: Hlc, now_ms: i64, node: DeviceId) -> Hlc { |
| 118 |
if now_ms > prev.wall_ms { |
| 119 |
Hlc { |
| 120 |
wall_ms: now_ms, |
| 121 |
counter: 0, |
| 122 |
node, |
| 123 |
} |
| 124 |
} else { |
| 125 |
bump_counter(prev.wall_ms, prev.counter, node) |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
pub fn observe(prev: Hlc, remote: Hlc, now_ms: i64, node: DeviceId) -> Hlc { |
| 138 |
let wall = prev.wall_ms.max(remote.wall_ms).max(now_ms); |
| 139 |
let base = if wall == prev.wall_ms && wall == remote.wall_ms { |
| 140 |
prev.counter.max(remote.counter) |
| 141 |
} else if wall == prev.wall_ms { |
| 142 |
prev.counter |
| 143 |
} else if wall == remote.wall_ms { |
| 144 |
remote.counter |
| 145 |
} else { |
| 146 |
|
| 147 |
return Hlc { |
| 148 |
wall_ms: wall, |
| 149 |
counter: 0, |
| 150 |
node, |
| 151 |
}; |
| 152 |
}; |
| 153 |
bump_counter(wall, base, node) |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
pub(crate) fn hlc_legacy_floor() -> Hlc { |
| 164 |
Hlc::zero(DeviceId::nil()) |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
fn bump_counter(wall_ms: i64, counter: u32, node: DeviceId) -> Hlc { |
| 172 |
match counter.checked_add(1) { |
| 173 |
Some(next) => Hlc { |
| 174 |
wall_ms, |
| 175 |
counter: next, |
| 176 |
node, |
| 177 |
}, |
| 178 |
None => match wall_ms.checked_add(1) { |
| 179 |
Some(wall_ms) => Hlc { |
| 180 |
wall_ms, |
| 181 |
counter: 0, |
| 182 |
node, |
| 183 |
}, |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
None => Hlc { |
| 190 |
wall_ms, |
| 191 |
counter: u32::MAX, |
| 192 |
node, |
| 193 |
}, |
| 194 |
}, |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
#[derive(Serialize)] |
| 201 |
pub(crate) struct AuthRequest<'a> { |
| 202 |
pub email: &'a str, |
| 203 |
pub password: &'a str, |
| 204 |
pub api_key: &'a str, |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
pub key: &'a str, |
| 209 |
} |
| 210 |
|
| 211 |
#[derive(Deserialize)] |
| 212 |
pub(crate) struct AuthResponse { |
| 213 |
pub token: String, |
| 214 |
pub user_id: UserId, |
| 215 |
pub app_id: AppId, |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
#[derive(Serialize)] |
| 221 |
pub(crate) struct RegisterDeviceRequest { |
| 222 |
pub device_name: String, |
| 223 |
pub platform: String, |
| 224 |
} |
| 225 |
|
| 226 |
#[derive(Debug, Clone, Deserialize, Serialize)] |
| 227 |
#[non_exhaustive] |
| 228 |
pub struct Device { |
| 229 |
|
| 230 |
pub id: DeviceId, |
| 231 |
|
| 232 |
pub app_id: AppId, |
| 233 |
|
| 234 |
pub user_id: UserId, |
| 235 |
|
| 236 |
pub device_name: String, |
| 237 |
|
| 238 |
pub platform: String, |
| 239 |
|
| 240 |
pub last_seen_at: DateTime<Utc>, |
| 241 |
|
| 242 |
pub created_at: DateTime<Utc>, |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
#[derive(Debug, Clone, Deserialize)] |
| 250 |
#[non_exhaustive] |
| 251 |
pub struct SyncGroup { |
| 252 |
|
| 253 |
pub id: GroupId, |
| 254 |
|
| 255 |
pub app_id: AppId, |
| 256 |
|
| 257 |
pub admin_user_id: UserId, |
| 258 |
|
| 259 |
pub name: String, |
| 260 |
|
| 261 |
|
| 262 |
pub gck_version: i32, |
| 263 |
|
| 264 |
pub created_at: DateTime<Utc>, |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
#[derive(Debug, Clone, Deserialize)] |
| 271 |
#[non_exhaustive] |
| 272 |
pub struct GroupMember { |
| 273 |
|
| 274 |
pub user_id: UserId, |
| 275 |
|
| 276 |
|
| 277 |
pub email: String, |
| 278 |
|
| 279 |
pub role: String, |
| 280 |
|
| 281 |
pub added_at: DateTime<Utc>, |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
#[derive(Debug, Clone, Deserialize)] |
| 288 |
#[non_exhaustive] |
| 289 |
pub struct GroupGrant { |
| 290 |
|
| 291 |
|
| 292 |
pub sealed_gck: String, |
| 293 |
|
| 294 |
pub gck_version: i32, |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
#[derive(Debug, Clone, Serialize, Deserialize)] |
| 302 |
pub struct ChangeEntry { |
| 303 |
|
| 304 |
pub table: String, |
| 305 |
|
| 306 |
pub op: ChangeOp, |
| 307 |
|
| 308 |
pub row_id: String, |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
pub timestamp: DateTime<Utc>, |
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
#[serde(default = "hlc_legacy_floor")] |
| 321 |
pub hlc: Hlc, |
| 322 |
|
| 323 |
|
| 324 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 325 |
pub data: Option<serde_json::Value>, |
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
#[serde(flatten)] |
| 341 |
pub extra: serde_json::Map<String, serde_json::Value>, |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
#[derive(Serialize)] |
| 346 |
pub(crate) struct WirePushRequest { |
| 347 |
|
| 348 |
pub device_id: DeviceId, |
| 349 |
|
| 350 |
|
| 351 |
pub batch_id: Uuid, |
| 352 |
|
| 353 |
pub changes: Vec<WireChangeEntry>, |
| 354 |
} |
| 355 |
|
| 356 |
#[derive(Debug, Serialize)] |
| 357 |
pub(crate) struct WireChangeEntry { |
| 358 |
pub table: String, |
| 359 |
pub op: ChangeOp, |
| 360 |
pub row_id: String, |
| 361 |
pub timestamp: DateTime<Utc>, |
| 362 |
pub data: Option<serde_json::Value>, |
| 363 |
} |
| 364 |
|
| 365 |
#[derive(Deserialize)] |
| 366 |
pub(crate) struct PushResponse { |
| 367 |
pub cursor: i64, |
| 368 |
} |
| 369 |
|
| 370 |
#[derive(Serialize)] |
| 371 |
pub(crate) struct PullRequest { |
| 372 |
pub device_id: DeviceId, |
| 373 |
pub cursor: i64, |
| 374 |
} |
| 375 |
|
| 376 |
#[derive(Deserialize)] |
| 377 |
pub(crate) struct PullResponse { |
| 378 |
pub changes: Vec<PullChangeEntry>, |
| 379 |
pub cursor: i64, |
| 380 |
pub has_more: bool, |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
#[derive(Deserialize, Clone)] |
| 389 |
pub(crate) struct PullChangeEntry { |
| 390 |
#[allow(dead_code)] |
| 391 |
pub seq: i64, |
| 392 |
#[allow(dead_code)] |
| 393 |
pub device_id: DeviceId, |
| 394 |
pub table: String, |
| 395 |
pub op: ChangeOp, |
| 396 |
pub row_id: String, |
| 397 |
pub timestamp: DateTime<Utc>, |
| 398 |
pub data: Option<serde_json::Value>, |
| 399 |
|
| 400 |
#[serde(default)] |
| 401 |
pub key_id: Option<i32>, |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
#[derive(Debug, Clone, Default, Serialize)] |
| 411 |
pub struct PullFilter { |
| 412 |
|
| 413 |
|
| 414 |
#[serde(skip_serializing_if = "PullFilter::tables_is_empty")] |
| 415 |
pub tables: Option<Vec<String>>, |
| 416 |
|
| 417 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 418 |
pub since: Option<DateTime<Utc>>, |
| 419 |
} |
| 420 |
|
| 421 |
impl PullFilter { |
| 422 |
#[allow( |
| 423 |
clippy::ref_option, |
| 424 |
reason = "serde skip_serializing_if requires a fn(&T) -> bool signature" |
| 425 |
)] |
| 426 |
fn tables_is_empty(tables: &Option<Vec<String>>) -> bool { |
| 427 |
match tables { |
| 428 |
None => true, |
| 429 |
Some(v) => v.is_empty(), |
| 430 |
} |
| 431 |
} |
| 432 |
} |
| 433 |
|
| 434 |
#[derive(Serialize)] |
| 435 |
pub(crate) struct FilteredPullRequest { |
| 436 |
pub device_id: DeviceId, |
| 437 |
pub cursor: i64, |
| 438 |
#[serde(skip_serializing_if = "PullFilter::tables_is_empty")] |
| 439 |
pub tables: Option<Vec<String>>, |
| 440 |
#[serde(skip_serializing_if = "Option::is_none")] |
| 441 |
pub since: Option<DateTime<Utc>>, |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
#[derive(Debug, Clone)] |
| 453 |
#[non_exhaustive] |
| 454 |
pub struct PulledChange { |
| 455 |
|
| 456 |
pub entry: ChangeEntry, |
| 457 |
|
| 458 |
pub device_id: DeviceId, |
| 459 |
|
| 460 |
pub seq: i64, |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
#[derive(Serialize)] |
| 466 |
pub(crate) struct PutKeyRequest { |
| 467 |
pub encrypted_key: String, |
| 468 |
|
| 469 |
pub expected_version: i32, |
| 470 |
} |
| 471 |
|
| 472 |
#[derive(Deserialize)] |
| 473 |
#[allow(dead_code)] |
| 474 |
pub(crate) struct GetKeyResponse { |
| 475 |
pub encrypted_key: String, |
| 476 |
|
| 477 |
#[serde(default)] |
| 478 |
pub key_version: Option<i32>, |
| 479 |
|
| 480 |
#[serde(default)] |
| 481 |
pub key_id: Option<i32>, |
| 482 |
|
| 483 |
#[serde(default)] |
| 484 |
pub pending_key: Option<PendingKeyInfo>, |
| 485 |
} |
| 486 |
|
| 487 |
#[derive(Debug, Clone, Deserialize)] |
| 488 |
#[allow(dead_code)] |
| 489 |
pub(crate) struct PendingKeyInfo { |
| 490 |
pub encrypted_key: String, |
| 491 |
pub key_id: i32, |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
#[derive(Serialize)] |
| 497 |
pub(crate) struct BeginRotationRequest { |
| 498 |
pub device_id: DeviceId, |
| 499 |
pub new_encrypted_key: String, |
| 500 |
pub expected_key_version: i32, |
| 501 |
} |
| 502 |
|
| 503 |
#[derive(Deserialize)] |
| 504 |
pub(crate) struct BeginRotationResponse { |
| 505 |
pub rotation_id: Uuid, |
| 506 |
pub target_seq: i64, |
| 507 |
pub new_key_id: i32, |
| 508 |
} |
| 509 |
|
| 510 |
#[derive(Serialize)] |
| 511 |
pub(crate) struct RotationEntriesRequest { |
| 512 |
pub rotation_id: Uuid, |
| 513 |
pub after_seq: i64, |
| 514 |
} |
| 515 |
|
| 516 |
#[derive(Deserialize)] |
| 517 |
pub(crate) struct RotationEntriesResponse { |
| 518 |
pub entries: Vec<RotationEntry>, |
| 519 |
pub has_more: bool, |
| 520 |
} |
| 521 |
|
| 522 |
#[derive(Deserialize)] |
| 523 |
pub(crate) struct RotationEntry { |
| 524 |
pub seq: i64, |
| 525 |
|
| 526 |
|
| 527 |
pub table: String, |
| 528 |
|
| 529 |
pub row_id: String, |
| 530 |
pub data: Option<serde_json::Value>, |
| 531 |
} |
| 532 |
|
| 533 |
#[derive(Serialize)] |
| 534 |
pub(crate) struct RotationBatchRequest { |
| 535 |
pub rotation_id: Uuid, |
| 536 |
pub entries: Vec<RotationBatchEntry>, |
| 537 |
} |
| 538 |
|
| 539 |
#[derive(Serialize)] |
| 540 |
pub(crate) struct RotationBatchEntry { |
| 541 |
pub seq: i64, |
| 542 |
pub data: Option<serde_json::Value>, |
| 543 |
} |
| 544 |
|
| 545 |
#[derive(Deserialize)] |
| 546 |
#[allow(dead_code)] |
| 547 |
pub(crate) struct RotationBatchResponse { |
| 548 |
pub updated_count: u64, |
| 549 |
} |
| 550 |
|
| 551 |
#[derive(Serialize)] |
| 552 |
pub(crate) struct CompleteRotationRequest { |
| 553 |
pub rotation_id: Uuid, |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
#[derive(Deserialize)] |
| 563 |
pub(crate) struct OAuthTokenResponse { |
| 564 |
pub access_token: String, |
| 565 |
#[allow(dead_code)] |
| 566 |
pub token_type: String, |
| 567 |
#[allow(dead_code)] |
| 568 |
pub expires_in: i64, |
| 569 |
pub user_id: UserId, |
| 570 |
pub app_id: AppId, |
| 571 |
} |
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
#[derive(Debug, Deserialize)] |
| 576 |
#[non_exhaustive] |
| 577 |
pub struct SyncStatus { |
| 578 |
|
| 579 |
pub total_changes: i64, |
| 580 |
|
| 581 |
pub latest_cursor: Option<i64>, |
| 582 |
} |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
#[derive(Serialize)] |
| 587 |
pub(crate) struct BlobUploadUrlRequest { |
| 588 |
pub hash: String, |
| 589 |
pub size_bytes: i64, |
| 590 |
} |
| 591 |
|
| 592 |
#[derive(Deserialize)] |
| 593 |
#[non_exhaustive] |
| 594 |
pub struct BlobUploadUrlResponse { |
| 595 |
|
| 596 |
pub upload_url: String, |
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
pub already_exists: bool, |
| 609 |
} |
| 610 |
|
| 611 |
#[derive(Serialize)] |
| 612 |
pub(crate) struct BlobConfirmRequest { |
| 613 |
pub hash: String, |
| 614 |
pub size_bytes: i64, |
| 615 |
} |
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
#[derive(Serialize)] |
| 624 |
pub(crate) struct BlobMultipartStartRequest { |
| 625 |
pub hash: String, |
| 626 |
pub size_bytes: i64, |
| 627 |
} |
| 628 |
|
| 629 |
#[derive(Deserialize)] |
| 630 |
pub(crate) struct BlobMultipartStartResponse { |
| 631 |
pub upload_id: String, |
| 632 |
|
| 633 |
pub part_size: u64, |
| 634 |
pub part_count: u32, |
| 635 |
|
| 636 |
|
| 637 |
pub already_exists: bool, |
| 638 |
} |
| 639 |
|
| 640 |
#[derive(Serialize)] |
| 641 |
pub(crate) struct BlobMultipartPartsRequest { |
| 642 |
pub hash: String, |
| 643 |
pub upload_id: String, |
| 644 |
pub size_bytes: i64, |
| 645 |
pub first_part: u32, |
| 646 |
pub count: u32, |
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
pub checksums: Vec<String>, |
| 652 |
} |
| 653 |
|
| 654 |
#[derive(Deserialize)] |
| 655 |
pub(crate) struct BlobMultipartPartsResponse { |
| 656 |
pub parts: Vec<BlobMultipartPartUrl>, |
| 657 |
} |
| 658 |
|
| 659 |
#[derive(Deserialize)] |
| 660 |
pub(crate) struct BlobMultipartPartUrl { |
| 661 |
pub part_number: i32, |
| 662 |
|
| 663 |
|
| 664 |
pub content_length: u64, |
| 665 |
pub url: String, |
| 666 |
} |
| 667 |
|
| 668 |
#[derive(Serialize)] |
| 669 |
pub(crate) struct BlobMultipartCompleteRequest { |
| 670 |
pub hash: String, |
| 671 |
pub upload_id: String, |
| 672 |
pub parts: Vec<BlobMultipartCompletedPart>, |
| 673 |
} |
| 674 |
|
| 675 |
#[derive(Serialize)] |
| 676 |
pub(crate) struct BlobMultipartCompletedPart { |
| 677 |
pub part_number: i32, |
| 678 |
pub etag: String, |
| 679 |
} |
| 680 |
|
| 681 |
#[derive(Serialize)] |
| 682 |
pub(crate) struct BlobMultipartAbortRequest { |
| 683 |
pub hash: String, |
| 684 |
pub upload_id: String, |
| 685 |
} |
| 686 |
|
| 687 |
#[derive(Serialize)] |
| 688 |
pub(crate) struct BlobDownloadUrlRequest { |
| 689 |
pub hash: String, |
| 690 |
} |
| 691 |
|
| 692 |
#[derive(Deserialize)] |
| 693 |
pub(crate) struct BlobDownloadUrlResponse { |
| 694 |
pub download_url: String, |
| 695 |
} |
| 696 |
|
| 697 |
#[cfg(test)] |
| 698 |
mod tests { |
| 699 |
use super::*; |
| 700 |
use serde_json::json; |
| 701 |
|
| 702 |
#[test] |
| 703 |
fn change_op_serde_roundtrip() { |
| 704 |
for (variant, expected_str) in [ |
| 705 |
(ChangeOp::Insert, "\"INSERT\""), |
| 706 |
(ChangeOp::Update, "\"UPDATE\""), |
| 707 |
(ChangeOp::Delete, "\"DELETE\""), |
| 708 |
] { |
| 709 |
let serialized = serde_json::to_string(&variant).unwrap(); |
| 710 |
assert_eq!(serialized, expected_str); |
| 711 |
let deserialized: ChangeOp = serde_json::from_str(&serialized).unwrap(); |
| 712 |
assert_eq!(deserialized, variant); |
| 713 |
} |
| 714 |
} |
| 715 |
|
| 716 |
#[test] |
| 717 |
fn change_op_display_matches_serde() { |
| 718 |
for variant in [ChangeOp::Insert, ChangeOp::Update, ChangeOp::Delete] { |
| 719 |
let display = variant.to_string(); |
| 720 |
let serde_str = serde_json::to_string(&variant).unwrap(); |
| 721 |
|
| 722 |
assert_eq!(format!("\"{display}\""), serde_str); |
| 723 |
} |
| 724 |
} |
| 725 |
|
| 726 |
#[test] |
| 727 |
fn change_op_from_str_opt_rejects_lowercase() { |
| 728 |
assert_eq!(ChangeOp::from_str_opt("insert"), None); |
| 729 |
assert_eq!(ChangeOp::from_str_opt("update"), None); |
| 730 |
assert_eq!(ChangeOp::from_str_opt("delete"), None); |
| 731 |
} |
| 732 |
|
| 733 |
#[test] |
| 734 |
fn change_op_from_str_opt_rejects_unknown() { |
| 735 |
assert_eq!(ChangeOp::from_str_opt("UPSERT"), None); |
| 736 |
assert_eq!(ChangeOp::from_str_opt(""), None); |
| 737 |
assert_eq!(ChangeOp::from_str_opt("MERGE"), None); |
| 738 |
} |
| 739 |
|
| 740 |
#[test] |
| 741 |
fn change_op_is_copy_and_eq() { |
| 742 |
let op = ChangeOp::Insert; |
| 743 |
let copied = op; |
| 744 |
assert_eq!(op, copied); |
| 745 |
} |
| 746 |
|
| 747 |
#[test] |
| 748 |
fn change_op_hash_works() { |
| 749 |
use std::collections::HashSet; |
| 750 |
let mut set = HashSet::new(); |
| 751 |
set.insert(ChangeOp::Insert); |
| 752 |
set.insert(ChangeOp::Update); |
| 753 |
set.insert(ChangeOp::Delete); |
| 754 |
set.insert(ChangeOp::Insert); |
| 755 |
assert_eq!(set.len(), 3); |
| 756 |
} |
| 757 |
|
| 758 |
#[test] |
| 759 |
fn change_entry_serialization_omits_none_data() { |
| 760 |
let entry = ChangeEntry { |
| 761 |
table: "t".into(), |
| 762 |
op: ChangeOp::Delete, |
| 763 |
row_id: "r".into(), |
| 764 |
timestamp: chrono::Utc::now(), |
| 765 |
hlc: Hlc::zero(DeviceId::nil()), |
| 766 |
data: None, |
| 767 |
extra: serde_json::Map::default(), |
| 768 |
}; |
| 769 |
let json = serde_json::to_string(&entry).unwrap(); |
| 770 |
assert!( |
| 771 |
!json.contains("\"data\""), |
| 772 |
"None data should be omitted: {json}" |
| 773 |
); |
| 774 |
} |
| 775 |
|
| 776 |
#[test] |
| 777 |
fn change_entry_serialization_includes_some_data() { |
| 778 |
let entry = ChangeEntry { |
| 779 |
table: "t".into(), |
| 780 |
op: ChangeOp::Insert, |
| 781 |
row_id: "r".into(), |
| 782 |
timestamp: chrono::Utc::now(), |
| 783 |
hlc: Hlc::zero(DeviceId::nil()), |
| 784 |
data: Some(json!({"k": "v"})), |
| 785 |
extra: serde_json::Map::default(), |
| 786 |
}; |
| 787 |
let json = serde_json::to_string(&entry).unwrap(); |
| 788 |
assert!( |
| 789 |
json.contains("\"data\""), |
| 790 |
"Some data should be present: {json}" |
| 791 |
); |
| 792 |
} |
| 793 |
|
| 794 |
#[test] |
| 795 |
fn change_entry_preserves_unknown_fields_across_roundtrip() { |
| 796 |
let json = r#"{ |
| 797 |
"table": "tasks", |
| 798 |
"op": "INSERT", |
| 799 |
"row_id": "r1", |
| 800 |
"timestamp": "2025-01-15T10:00:00Z", |
| 801 |
"data": {"title": "test"}, |
| 802 |
"future_marker": "should survive", |
| 803 |
"another_unknown": 42 |
| 804 |
}"#; |
| 805 |
let entry: ChangeEntry = serde_json::from_str(json).unwrap(); |
| 806 |
assert_eq!(entry.table, "tasks"); |
| 807 |
assert_eq!(entry.op, ChangeOp::Insert); |
| 808 |
assert_eq!(entry.data.as_ref().unwrap()["title"], "test"); |
| 809 |
|
| 810 |
assert_eq!(entry.extra["future_marker"], "should survive"); |
| 811 |
assert_eq!(entry.extra["another_unknown"], 42); |
| 812 |
|
| 813 |
|
| 814 |
let reserialized = serde_json::to_value(&entry).unwrap(); |
| 815 |
assert_eq!(reserialized["future_marker"], "should survive"); |
| 816 |
assert_eq!(reserialized["another_unknown"], 42); |
| 817 |
} |
| 818 |
|
| 819 |
#[test] |
| 820 |
fn device_deserialization_with_iso_timestamps() { |
| 821 |
let json = r#"{ |
| 822 |
"id": "550e8400-e29b-41d4-a716-446655440000", |
| 823 |
"app_id": "6ba7b810-9dad-11d1-80b4-00c04fd430c8", |
| 824 |
"user_id": "550e8400-e29b-41d4-a716-446655440001", |
| 825 |
"device_name": "MacBook Pro", |
| 826 |
"platform": "macos", |
| 827 |
"last_seen_at": "2025-06-15T14:30:00.123Z", |
| 828 |
"created_at": "2025-01-01T00:00:00Z" |
| 829 |
}"#; |
| 830 |
let device: Device = serde_json::from_str(json).unwrap(); |
| 831 |
assert_eq!(device.device_name, "MacBook Pro"); |
| 832 |
assert_eq!(device.platform, "macos"); |
| 833 |
assert_eq!( |
| 834 |
device.id.as_uuid(), |
| 835 |
Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000").unwrap() |
| 836 |
); |
| 837 |
} |
| 838 |
|
| 839 |
#[test] |
| 840 |
fn sync_status_with_zero_total_changes() { |
| 841 |
let json = r#"{"total_changes": 0, "latest_cursor": null}"#; |
| 842 |
let status: SyncStatus = serde_json::from_str(json).unwrap(); |
| 843 |
assert_eq!(status.total_changes, 0); |
| 844 |
assert!(status.latest_cursor.is_none()); |
| 845 |
} |
| 846 |
|
| 847 |
#[test] |
| 848 |
fn blob_upload_url_response_already_exists() { |
| 849 |
let json = r#"{"upload_url": "", "already_exists": true}"#; |
| 850 |
let resp: BlobUploadUrlResponse = serde_json::from_str(json).unwrap(); |
| 851 |
assert!(resp.already_exists); |
| 852 |
assert!(resp.upload_url.is_empty()); |
| 853 |
} |
| 854 |
|
| 855 |
#[test] |
| 856 |
fn change_op_debug_format() { |
| 857 |
assert_eq!(format!("{:?}", ChangeOp::Insert), "Insert"); |
| 858 |
assert_eq!(format!("{:?}", ChangeOp::Update), "Update"); |
| 859 |
assert_eq!(format!("{:?}", ChangeOp::Delete), "Delete"); |
| 860 |
} |
| 861 |
|
| 862 |
|
| 863 |
|
| 864 |
#[test] |
| 865 |
fn pull_filter_serialization_with_both_fields() { |
| 866 |
let filter = PullFilter { |
| 867 |
tables: Some(vec!["tasks".to_string(), "events".to_string()]), |
| 868 |
since: Some("2025-06-15T12:00:00Z".parse().unwrap()), |
| 869 |
}; |
| 870 |
let json = serde_json::to_string(&filter).unwrap(); |
| 871 |
let parsed: serde_json::Value = serde_json::from_str(&json).unwrap(); |
| 872 |
assert_eq!(parsed["tables"].as_array().unwrap().len(), 2); |
| 873 |
assert!(parsed["since"].is_string()); |
| 874 |
} |
| 875 |
|
| 876 |
#[test] |
| 877 |
fn pull_filter_serialization_with_none_fields() { |
| 878 |
let filter = PullFilter::default(); |
| 879 |
let json = serde_json::to_string(&filter).unwrap(); |
| 880 |
|
| 881 |
assert!(!json.contains("tables")); |
| 882 |
assert!(!json.contains("since")); |
| 883 |
assert_eq!(json, "{}"); |
| 884 |
} |
| 885 |
|
| 886 |
#[test] |
| 887 |
fn pull_filter_empty_tables_vec_omitted() { |
| 888 |
let filter = PullFilter { |
| 889 |
tables: Some(vec![]), |
| 890 |
since: None, |
| 891 |
}; |
| 892 |
let json = serde_json::to_string(&filter).unwrap(); |
| 893 |
assert!( |
| 894 |
!json.contains("tables"), |
| 895 |
"empty tables vec should be omitted: {json}" |
| 896 |
); |
| 897 |
assert_eq!(json, "{}"); |
| 898 |
} |
| 899 |
|
| 900 |
#[test] |
| 901 |
fn filtered_pull_request_includes_filter_fields() { |
| 902 |
let req = FilteredPullRequest { |
| 903 |
device_id: DeviceId::new( |
| 904 |
Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000").unwrap(), |
| 905 |
), |
| 906 |
cursor: 42, |
| 907 |
tables: Some(vec!["tasks".to_string()]), |
| 908 |
since: Some("2025-01-01T00:00:00Z".parse().unwrap()), |
| 909 |
}; |
| 910 |
let json = serde_json::to_string(&req).unwrap(); |
| 911 |
let parsed: serde_json::Value = serde_json::from_str(&json).unwrap(); |
| 912 |
assert_eq!(parsed["cursor"], 42); |
| 913 |
assert_eq!(parsed["tables"].as_array().unwrap().len(), 1); |
| 914 |
assert!(parsed["since"].is_string()); |
| 915 |
} |
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
#[test] |
| 920 |
fn hlc_orders_by_wall_then_counter_then_node() { |
| 921 |
let a = DeviceId::new(Uuid::from_u128(1)); |
| 922 |
let b = DeviceId::new(Uuid::from_u128(2)); |
| 923 |
|
| 924 |
assert!( |
| 925 |
Hlc { |
| 926 |
wall_ms: 1, |
| 927 |
counter: 9, |
| 928 |
node: b |
| 929 |
} < Hlc { |
| 930 |
wall_ms: 2, |
| 931 |
counter: 0, |
| 932 |
node: a |
| 933 |
} |
| 934 |
); |
| 935 |
|
| 936 |
assert!( |
| 937 |
Hlc { |
| 938 |
wall_ms: 5, |
| 939 |
counter: 0, |
| 940 |
node: b |
| 941 |
} < Hlc { |
| 942 |
wall_ms: 5, |
| 943 |
counter: 1, |
| 944 |
node: a |
| 945 |
} |
| 946 |
); |
| 947 |
|
| 948 |
assert!( |
| 949 |
Hlc { |
| 950 |
wall_ms: 5, |
| 951 |
counter: 1, |
| 952 |
node: a |
| 953 |
} < Hlc { |
| 954 |
wall_ms: 5, |
| 955 |
counter: 1, |
| 956 |
node: b |
| 957 |
} |
| 958 |
); |
| 959 |
} |
| 960 |
|
| 961 |
#[test] |
| 962 |
fn hlc_tick_adopts_physical_time_and_resets_counter() { |
| 963 |
let node = DeviceId::new(Uuid::from_u128(1)); |
| 964 |
let prev = Hlc { |
| 965 |
wall_ms: 1000, |
| 966 |
counter: 4, |
| 967 |
node, |
| 968 |
}; |
| 969 |
let next = Hlc::tick(prev, 2000, node); |
| 970 |
assert_eq!( |
| 971 |
next, |
| 972 |
Hlc { |
| 973 |
wall_ms: 2000, |
| 974 |
counter: 0, |
| 975 |
node |
| 976 |
} |
| 977 |
); |
| 978 |
} |
| 979 |
|
| 980 |
#[test] |
| 981 |
fn hlc_tick_bumps_counter_when_clock_has_not_advanced() { |
| 982 |
let node = DeviceId::new(Uuid::from_u128(1)); |
| 983 |
|
| 984 |
|
| 985 |
let prev = Hlc { |
| 986 |
wall_ms: 1000, |
| 987 |
counter: 4, |
| 988 |
node, |
| 989 |
}; |
| 990 |
assert_eq!( |
| 991 |
Hlc::tick(prev, 1000, node), |
| 992 |
Hlc { |
| 993 |
wall_ms: 1000, |
| 994 |
counter: 5, |
| 995 |
node |
| 996 |
} |
| 997 |
); |
| 998 |
assert_eq!( |
| 999 |
Hlc::tick(prev, 500, node), |
| 1000 |
Hlc { |
| 1001 |
wall_ms: 1000, |
| 1002 |
counter: 5, |
| 1003 |
node |
| 1004 |
} |
| 1005 |
); |
| 1006 |
|
| 1007 |
let mut h = Hlc::zero(node); |
| 1008 |
let mut last = h; |
| 1009 |
for _ in 0..100 { |
| 1010 |
h = Hlc::tick(h, 0, node); |
| 1011 |
assert!(h > last); |
| 1012 |
last = h; |
| 1013 |
} |
| 1014 |
} |
| 1015 |
|
| 1016 |
#[test] |
| 1017 |
fn hlc_observe_stays_ahead_of_a_skewed_fast_remote() { |
| 1018 |
let me = DeviceId::new(Uuid::from_u128(1)); |
| 1019 |
let them = DeviceId::new(Uuid::from_u128(2)); |
| 1020 |
|
| 1021 |
let local = Hlc { |
| 1022 |
wall_ms: 1000, |
| 1023 |
counter: 0, |
| 1024 |
node: me, |
| 1025 |
}; |
| 1026 |
let remote = Hlc { |
| 1027 |
wall_ms: 9000, |
| 1028 |
counter: 3, |
| 1029 |
node: them, |
| 1030 |
}; |
| 1031 |
let merged = Hlc::observe(local, remote, 1000, me); |
| 1032 |
|
| 1033 |
|
| 1034 |
assert_eq!(merged.wall_ms, 9000); |
| 1035 |
assert!(merged.counter > remote.counter); |
| 1036 |
assert_eq!(merged.node, me); |
| 1037 |
let next_local = Hlc::tick(merged, 1001, me); |
| 1038 |
assert!( |
| 1039 |
next_local > remote, |
| 1040 |
"a later local write must outrank the skewed remote" |
| 1041 |
); |
| 1042 |
} |
| 1043 |
|
| 1044 |
#[test] |
| 1045 |
fn hlc_observe_advances_to_physical_time_when_it_leads() { |
| 1046 |
let me = DeviceId::new(Uuid::from_u128(1)); |
| 1047 |
let them = DeviceId::new(Uuid::from_u128(2)); |
| 1048 |
let local = Hlc { |
| 1049 |
wall_ms: 1000, |
| 1050 |
counter: 2, |
| 1051 |
node: me, |
| 1052 |
}; |
| 1053 |
let remote = Hlc { |
| 1054 |
wall_ms: 1500, |
| 1055 |
counter: 0, |
| 1056 |
node: them, |
| 1057 |
}; |
| 1058 |
|
| 1059 |
let merged = Hlc::observe(local, remote, 3000, me); |
| 1060 |
assert_eq!( |
| 1061 |
merged, |
| 1062 |
Hlc { |
| 1063 |
wall_ms: 3000, |
| 1064 |
counter: 0, |
| 1065 |
node: me |
| 1066 |
} |
| 1067 |
); |
| 1068 |
} |
| 1069 |
|
| 1070 |
#[test] |
| 1071 |
fn hlc_counter_overflow_rolls_into_wall_not_backwards() { |
| 1072 |
let me = DeviceId::new(Uuid::from_u128(1)); |
| 1073 |
|
| 1074 |
|
| 1075 |
let prev = Hlc { |
| 1076 |
wall_ms: 1000, |
| 1077 |
counter: u32::MAX, |
| 1078 |
node: me, |
| 1079 |
}; |
| 1080 |
let next = Hlc::tick(prev, 1000, me); |
| 1081 |
assert!( |
| 1082 |
next > prev, |
| 1083 |
"tick at counter::MAX must stay strictly increasing" |
| 1084 |
); |
| 1085 |
assert_eq!( |
| 1086 |
next, |
| 1087 |
Hlc { |
| 1088 |
wall_ms: 1001, |
| 1089 |
counter: 0, |
| 1090 |
node: me |
| 1091 |
} |
| 1092 |
); |
| 1093 |
} |
| 1094 |
|
| 1095 |
#[test] |
| 1096 |
fn hlc_observe_hostile_max_counter_stays_monotonic() { |
| 1097 |
let me = DeviceId::new(Uuid::from_u128(1)); |
| 1098 |
let them = DeviceId::new(Uuid::from_u128(2)); |
| 1099 |
|
| 1100 |
let local = Hlc { |
| 1101 |
wall_ms: 5000, |
| 1102 |
counter: 3, |
| 1103 |
node: me, |
| 1104 |
}; |
| 1105 |
let remote = Hlc { |
| 1106 |
wall_ms: 5000, |
| 1107 |
counter: u32::MAX, |
| 1108 |
node: them, |
| 1109 |
}; |
| 1110 |
let merged = Hlc::observe(local, remote, 5000, me); |
| 1111 |
assert!( |
| 1112 |
merged > local, |
| 1113 |
"observe must not wrap backwards on a MAX remote counter" |
| 1114 |
); |
| 1115 |
assert!(merged > remote); |
| 1116 |
assert_eq!( |
| 1117 |
merged, |
| 1118 |
Hlc { |
| 1119 |
wall_ms: 5001, |
| 1120 |
counter: 0, |
| 1121 |
node: me |
| 1122 |
} |
| 1123 |
); |
| 1124 |
} |
| 1125 |
|
| 1126 |
#[test] |
| 1127 |
fn hlc_serde_roundtrip() { |
| 1128 |
let h = Hlc { |
| 1129 |
wall_ms: 123, |
| 1130 |
counter: 4, |
| 1131 |
node: DeviceId::new(Uuid::from_u128(7)), |
| 1132 |
}; |
| 1133 |
let j = serde_json::to_value(h).unwrap(); |
| 1134 |
assert_eq!(serde_json::from_value::<Hlc>(j).unwrap(), h); |
| 1135 |
} |
| 1136 |
} |
| 1137 |
|