utopia-ia/elffuss-braid public
Changesets · Branches · Tags · Compare
| Changeset | 379b0811aad9b2706cd370b29260bc1776abb3aeec24bd61bc136dae08aeeb9f |
|---|---|
| Author | KikoCis <KikoCis@users.noreply.github.com> · 2026-10-22 16:53 +02:00 |
| Committer | KikoCis <KikoCis@users.noreply.github.com> · 2026-10-22 16:53 +02:00 |
| Parents | 373d85f62fd5 |
6 files changed
modified crates/cli/src/hub/resilience_tests.rs +3 −0
| @@ -620,6 +620,7 @@ | ||
| 620 | 620 | change: None, |
| 621 | 621 | git_commit: None, |
| 622 | 622 | operation: Some(7), |
| 623 | + verify: None, | |
| 623 | 624 | }); |
| 624 | 625 | let before = Snapshot::of(&s.local.repo, &ws); |
| 625 | 626 | let err = format!("{:#}", s.land(&faulty, &ws).unwrap_err()); |
| @@ -669,6 +670,7 @@ | ||
| 669 | 670 | change: None, |
| 670 | 671 | git_commit: None, |
| 671 | 672 | operation: Some(7), |
| 673 | + verify: None, | |
| 672 | 674 | }); |
| 673 | 675 | let err = format!("{:#}", s.land(&faulty, &w2).unwrap_err()); |
| 674 | 676 | assert!(err.contains("does not hold"), "{err}"); |
| @@ -705,6 +707,7 @@ | ||
| 705 | 707 | change: None, |
| 706 | 708 | git_commit: None, |
| 707 | 709 | operation: None, |
| 710 | + verify: None, | |
| 708 | 711 | }); |
| 709 | 712 | let before = Snapshot::of(&s.local.repo, &ws); |
| 710 | 713 | let err = format!("{:#}", s.land(&faulty, &ws).unwrap_err()); |
modified crates/cli/src/hub/testing.rs +1 −0
| @@ -201,6 +201,7 @@ | ||
| 201 | 201 | change, |
| 202 | 202 | git_commit: None, |
| 203 | 203 | operation: report.operation, |
| 204 | + verify: None, | |
| 204 | 205 | } |
| 205 | 206 | } |
| 206 | 207 |
modified crates/hubproto/src/api.rs +124 −2
| @@ -12,6 +12,7 @@ | ||
| 12 | 12 | |
| 13 | 13 | use crate::cards::RepoRef; |
| 14 | 14 | use crate::hex; |
| 15 | +use crate::verify::LandVerify; | |
| 15 | 16 | |
| 16 | 17 | /// An error body: `{"error", "detail"}`, with `ids` for `missing`, |
| 17 | 18 | /// `retry_after` for `rate_limited`, and `head` for `not_on_branch`. |
| @@ -37,14 +38,28 @@ | ||
| 37 | 38 | pub head: Option<String>, |
| 38 | 39 | } |
| 39 | 40 | |
| 40 | -/// `GET /v1`: the protocol version the hub speaks, and the sizes a client | |
| 41 | -/// keeps its requests within. | |
| 41 | +/// `GET /v1`: the protocol version the hub speaks, the sizes a client | |
| 42 | +/// keeps its requests within, and the parts of the protocol the hub serves | |
| 43 | +/// beyond its core. | |
| 42 | 44 | #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] |
| 43 | 45 | pub struct Root { |
| 44 | 46 | /// The protocol version: 1. |
| 45 | 47 | pub version: u32, |
| 46 | 48 | /// What one request may carry or ask for. |
| 47 | 49 | pub limits: RootLimits, |
| 50 | + /// The parts of the protocol the hub serves beyond its core, such as | |
| 51 | + /// [`verify::FEATURE`](crate::verify::FEATURE). A client calls their | |
| 52 | + /// routes, and sends their fields, only when the hub lists them; empty | |
| 53 | + /// from a hub that lists none. | |
| 54 | + #[serde(default, skip_serializing_if = "Vec::is_empty")] | |
| 55 | + pub features: Vec<String>, | |
| 56 | +} | |
| 57 | + | |
| 58 | +impl Root { | |
| 59 | + /// Whether the hub lists `feature`. | |
| 60 | + pub fn serves(&self, feature: &str) -> bool { | |
| 61 | + self.features.iter().any(|listed| listed == feature) | |
| 62 | + } | |
| 48 | 63 | } |
| 49 | 64 | |
| 50 | 65 | /// The sizes of section 12 a hub enforces, as `GET /v1` gives them. |
| @@ -638,6 +653,10 @@ | ||
| 638 | 653 | /// move. |
| 639 | 654 | #[serde(default)] |
| 640 | 655 | pub operation: Option<u64>, |
| 656 | + /// How the default branch stands, for a landing on it in a repository | |
| 657 | + /// whose verification is on (section 25); `None` otherwise. | |
| 658 | + #[serde(default, skip_serializing_if = "Option::is_none")] | |
| 659 | + pub verify: Option<LandVerify>, | |
| 641 | 660 | } |
| 642 | 661 | |
| 643 | 662 | // ---------------------------------------------------------------- section 9 |
| @@ -764,8 +783,111 @@ | ||
| 764 | 783 | ) |
| 765 | 784 | .unwrap(); |
| 766 | 785 | assert_eq!(root.limits.social, None); |
| 786 | + // A hub that lists no feature serves none of them, and a root that | |
| 787 | + // lists none says nothing of them. | |
| 788 | + assert!(root.features.is_empty() && !root.serves(crate::verify::FEATURE)); | |
| 789 | + assert!(!serde_json::to_string(&root).unwrap().contains("features")); | |
| 767 | 790 | let me: Me = serde_json::from_str(r#"{"login":"ada"}"#).unwrap(); |
| 768 | 791 | assert_eq!((me.settings, me.github_scope), (None, None)); |
| 792 | + // A landing without `verify` is a landing on a branch the hub does | |
| 793 | + // not verify, and says nothing of it written again. | |
| 794 | + let id = ObjectId::hash(Kind::Changeset, b"z"); | |
| 795 | + let land: LandResponse = serde_json::from_str(&format!( | |
| 796 | + r#"{{"ref":"main","outcome":"fast-forward","head_before":null,"head":"{id}","landed":[]}}"# | |
| 797 | + )) | |
| 798 | + .unwrap(); | |
| 799 | + assert_eq!(land.verify, None); | |
| 800 | + assert!(!serde_json::to_string(&land).unwrap().contains("verify")); | |
| 801 | + } | |
| 802 | + | |
| 803 | + #[test] | |
| 804 | + fn what_a_newer_hub_sends_still_reads_in_an_older_client() { | |
| 805 | + // The answers as a client before verification read them: these | |
| 806 | + // structs hold every field they had, and none since. | |
| 807 | + #[derive(Debug, Deserialize)] | |
| 808 | + #[allow(dead_code)] | |
| 809 | + struct OldLanding { | |
| 810 | + #[serde(rename = "ref")] | |
| 811 | + reference: String, | |
| 812 | + outcome: String, | |
| 813 | + #[serde(with = "hex::opt_id")] | |
| 814 | + head_before: Option<ObjectId>, | |
| 815 | + #[serde(with = "hex::opt_id")] | |
| 816 | + head: Option<ObjectId>, | |
| 817 | + landed: Vec<Placed>, | |
| 818 | + #[serde(default)] | |
| 819 | + merged_paths: Vec<String>, | |
| 820 | + #[serde(default)] | |
| 821 | + conflicts: Vec<serde_json::Value>, | |
| 822 | + #[serde(default, with = "hex::opt_id")] | |
| 823 | + change: Option<ObjectId>, | |
| 824 | + #[serde(default)] | |
| 825 | + git_commit: Option<String>, | |
| 826 | + #[serde(default)] | |
| 827 | + operation: Option<u64>, | |
| 828 | + } | |
| 829 | + #[derive(Debug, Deserialize)] | |
| 830 | + #[allow(dead_code)] | |
| 831 | + struct OldRoot { | |
| 832 | + version: u32, | |
| 833 | + limits: RootLimits, | |
| 834 | + } | |
| 835 | + let (green, red) = ( | |
| 836 | + ObjectId::hash(Kind::Changeset, b"green"), | |
| 837 | + ObjectId::hash(Kind::Changeset, b"red"), | |
| 838 | + ); | |
| 839 | + let land = LandResponse { | |
| 840 | + reference: "main".into(), | |
| 841 | + outcome: "rebased".into(), | |
| 842 | + head_before: Some(green), | |
| 843 | + head: Some(red), | |
| 844 | + landed: vec![Placed { | |
| 845 | + from: green, | |
| 846 | + to: Some(red), | |
| 847 | + }], | |
| 848 | + merged_paths: Vec::new(), | |
| 849 | + conflicts: Vec::new(), | |
| 850 | + change: None, | |
| 851 | + git_commit: None, | |
| 852 | + operation: Some(7), | |
| 853 | + verify: Some(LandVerify { | |
| 854 | + state: crate::verify::STATE_RED.into(), | |
| 855 | + green: Some(green), | |
| 856 | + red: Some(red), | |
| 857 | + culprit: Some(red), | |
| 858 | + reverted: Vec::new(), | |
| 859 | + }), | |
| 860 | + }; | |
| 861 | + let text = serde_json::to_string(&land).unwrap(); | |
| 862 | + assert!(text.contains(r#""verify":{"state":"red""#), "{text}"); | |
| 863 | + assert_eq!(serde_json::from_str::<LandResponse>(&text).unwrap(), land); | |
| 864 | + let old: OldLanding = serde_json::from_str(&text).unwrap(); | |
| 865 | + assert_eq!((old.head, old.operation), (Some(red), Some(7))); | |
| 866 | + | |
| 867 | + let root = Root { | |
| 868 | + version: 1, | |
| 869 | + limits: RootLimits { | |
| 870 | + max_body: 1, | |
| 871 | + max_put: 1, | |
| 872 | + max_json: 1, | |
| 873 | + max_ids: 1, | |
| 874 | + max_paths: 1, | |
| 875 | + missing_page: 1, | |
| 876 | + missing_page_max: 1, | |
| 877 | + missing_objects: 1, | |
| 878 | + tree_entries: 1, | |
| 879 | + tree_depth: 1, | |
| 880 | + max_list: 1, | |
| 881 | + social: None, | |
| 882 | + }, | |
| 883 | + features: vec![crate::verify::FEATURE.into()], | |
| 884 | + }; | |
| 885 | + let text = serde_json::to_string(&root).unwrap(); | |
| 886 | + assert!(text.contains(r#""features":["verify"]"#), "{text}"); | |
| 887 | + assert_eq!(serde_json::from_str::<Root>(&text).unwrap(), root); | |
| 888 | + assert!(root.serves("verify") && !root.serves("verify.github")); | |
| 889 | + let old: OldRoot = serde_json::from_str(&text).unwrap(); | |
| 890 | + assert_eq!(old.version, 1); | |
| 769 | 891 | } |
| 770 | 892 | |
| 771 | 893 | #[test] |
modified crates/hubproto/src/lib.rs +2 −0
| @@ -14,6 +14,7 @@ | ||
| 14 | 14 | //! - [`browse`], [`history`], [`explore`], [`social`], [`issues`], |
| 15 | 15 | //! [`requests`], [`forks`], [`notifications`], [`moderation`] and |
| 16 | 16 | //! [`onboard`]: the bodies of sections 15 to 23. |
| 17 | +//! - [`verify`]: keeping a default branch green, section 25. | |
| 17 | 18 | //! - [`json`]: conflicts and changes as the CLI prints them, so a landing on |
| 18 | 19 | //! the hub reports exactly what a local landing does. |
| 19 | 20 | //! - [`limits`]: what one request may carry or ask for. |
| @@ -38,6 +39,7 @@ | ||
| 38 | 39 | pub mod social; |
| 39 | 40 | pub mod stream; |
| 40 | 41 | pub mod token; |
| 42 | +pub mod verify; | |
| 41 | 43 | |
| 42 | 44 | mod hex; |
| 43 | 45 |
modified crates/hubproto/src/notifications.rs +20 −2
| @@ -43,7 +43,15 @@ | ||
| 43 | 43 | pub const SUBSCRIBED: &str = "subscribed"; |
| 44 | 44 | |
| 45 | 45 | /// A notice of the hub about the account's own requests, such as a fork |
| 46 | -/// made or a grant to renew. | |
| 46 | +/// made or a grant to renew, or about the default branch of a repository | |
| 47 | +/// it administers or landed on: [`verify_red`], [`verify_fixed`] and | |
| 48 | +/// [`verify_stuck`] (section 25), whose `url` is the repository's | |
| 49 | +/// verification page. The reason of each notice is this one, whatever its | |
| 50 | +/// code. | |
| 51 | +/// | |
| 52 | +/// [`verify_red`]: crate::verify::NOTICE_RED | |
| 53 | +/// [`verify_fixed`]: crate::verify::NOTICE_FIXED | |
| 54 | +/// [`verify_stuck`]: crate::verify::NOTICE_STUCK | |
| 47 | 55 | pub const SYSTEM: &str = "system"; |
| 48 | 56 | |
| 49 | 57 | /// Why an account is told about a thread, strongest first: a notification |
| @@ -115,7 +123,8 @@ | ||
| 115 | 123 | /// What happened last: in a thread, `opened`, `commented`, `closed`, |
| 116 | 124 | /// `reopened`, `assigned`, `mentioned`, `review_requested`, `reviewed` |
| 117 | 125 | /// or `landed`; in a notice of the hub, what it says, such as |
| 118 | - /// `fork_done`. | |
| 126 | + /// `fork_done`, or `verify_red`, `verify_fixed` and `verify_stuck` for | |
| 127 | + /// a repository's default branch. | |
| 119 | 128 | #[serde(default)] |
| 120 | 129 | pub code: Option<String>, |
| 121 | 130 | /// When it last changed. |
| @@ -224,6 +233,15 @@ | ||
| 224 | 233 | assert!(notice["notifications"][1]["actor"].is_null()); |
| 225 | 234 | assert_eq!(REASONS[0], MENTION); |
| 226 | 235 | assert!(!REASONS.contains(&SYSTEM)); |
| 236 | + // A notice of a red branch names its repository, and leads to the | |
| 237 | + // page of its verification. | |
| 238 | + let red: Notification = serde_json::from_str( | |
| 239 | + r#"{"id":9,"repo":{"owner":"ada","name":"demo"},"thread":null,"reason":"system","actor":null,"code":"verify_red","updated":"2026-10-22T12:03:00Z","unread":true,"url":"https://hub.example/ada/demo/verify"}"#, | |
| 240 | + ) | |
| 241 | + .unwrap(); | |
| 242 | + assert_eq!(red.reason, SYSTEM); | |
| 243 | + assert_eq!(red.code.as_deref(), Some(crate::verify::NOTICE_RED)); | |
| 244 | + assert!(red.thread.is_none() && red.url.ends_with("/verify")); | |
| 227 | 245 | } |
| 228 | 246 | |
| 229 | 247 | #[test] |
added crates/hubproto/src/verify.rs +797 −0
| @@ -0,0 +1,797 @@ | ||
| 1 | +// SPDX-License-Identifier: Apache-2.0 | |
| 2 | +// Copyright (c) 2026 UtopiaIA | |
| 3 | + | |
| 4 | +//! Verification (section 25): keeping a repository's default branch green. | |
| 5 | +//! The bodies of the routes under `/v1/r/{owner}/{name}/verify`, the closed | |
| 6 | +//! sets of words they use, and the names of the hub's own refs that say | |
| 7 | +//! where the branch stands. | |
| 8 | +//! | |
| 9 | +//! A reporter, such as a CI job, tells the hub how a suite of tests went on | |
| 10 | +//! a changeset of the default branch ([`VerifyReport`]). The hub keeps | |
| 11 | +//! `braid/green/<branch>` at the newest changeset with a pass it accepts | |
| 12 | +//! and, while the branch is red, `braid/red/<branch>` at the changeset it | |
| 13 | +//! blames, or at the first fail it cannot explain. It names a culprit only | |
| 14 | +//! when a single changeset separates a pass from a fail. The hub runs no | |
| 15 | +//! code of a repository: every verdict comes from outside it. | |
| 16 | +//! | |
| 17 | +//! Every field of these bodies has a default, and none refuses a field it | |
| 18 | +//! does not know, so that a client reads what a newer hub answers. The | |
| 19 | +//! words of a closed set are strings: a client keeps one it does not know | |
| 20 | +//! as it came, and takes a state it does not know for one that is not | |
| 21 | +//! green. Ids are 64 lowercase hex digits; times are RFC 3339, in UTC; | |
| 22 | +//! logins are lowercase. | |
| 23 | + | |
| 24 | +use braid_format::ObjectId; | |
| 25 | +use serde::{Deserialize, Serialize}; | |
| 26 | + | |
| 27 | +use crate::hex; | |
| 28 | + | |
| 29 | +/// What `GET /v1` lists in `features` when the hub verifies branches: the | |
| 30 | +/// routes of section 25 answer, and landings say how the branch stands. | |
| 31 | +pub const FEATURE: &str = "verify"; | |
| 32 | + | |
| 33 | +/// The prefix of the green refs, `braid/green/<branch>`. | |
| 34 | +pub const GREEN_PREFIX: &str = "braid/green/"; | |
| 35 | + | |
| 36 | +/// The prefix of the red refs, `braid/red/<branch>`. | |
| 37 | +pub const RED_PREFIX: &str = "braid/red/"; | |
| 38 | + | |
| 39 | +/// The hub's green ref of `branch`: the newest changeset of the branch with | |
| 40 | +/// a pass the hub accepts. | |
| 41 | +pub fn green_ref(branch: &str) -> String { | |
| 42 | + format!("{GREEN_PREFIX}{branch}") | |
| 43 | +} | |
| 44 | + | |
| 45 | +/// The hub's red ref of `branch`: while the branch is red, the changeset | |
| 46 | +/// the hub blames, or the first fail it cannot explain. | |
| 47 | +pub fn red_ref(branch: &str) -> String { | |
| 48 | + format!("{RED_PREFIX}{branch}") | |
| 49 | +} | |
| 50 | + | |
| 51 | +/// Mode: the hub verifies nothing, and keeps neither ref. | |
| 52 | +pub const MODE_OFF: &str = "off"; | |
| 53 | + | |
| 54 | +/// Mode: the hub takes the verdicts of its reporters, and keeps the refs. | |
| 55 | +pub const MODE_REPORT: &str = "report"; | |
| 56 | + | |
| 57 | +/// The modes of a policy. | |
| 58 | +pub const MODES: [&str; 2] = [MODE_OFF, MODE_REPORT]; | |
| 59 | + | |
| 60 | +/// Source: the verdicts reporters post. | |
| 61 | +pub const SOURCE_REPORT: &str = "report"; | |
| 62 | + | |
| 63 | +/// The sources a policy's `green_from` may name. | |
| 64 | +pub const SOURCES: [&str; 1] = [SOURCE_REPORT]; | |
| 65 | + | |
| 66 | +/// Outcome: the suite passed. | |
| 67 | +pub const PASS: &str = "pass"; | |
| 68 | + | |
| 69 | +/// Outcome: the suite failed. | |
| 70 | +pub const FAIL: &str = "fail"; | |
| 71 | + | |
| 72 | +/// Outcome: the suite failed, then passed when run again. | |
| 73 | +pub const FLAKY: &str = "flaky"; | |
| 74 | + | |
| 75 | +/// The outcomes a report gives. | |
| 76 | +pub const OUTCOMES: [&str; 3] = [PASS, FAIL, FLAKY]; | |
| 77 | + | |
| 78 | +/// State: the policy's mode is `off`. | |
| 79 | +pub const STATE_OFF: &str = "off"; | |
| 80 | + | |
| 81 | +/// State: nothing is known yet, such as before the first verdict. | |
| 82 | +pub const STATE_UNKNOWN: &str = "unknown"; | |
| 83 | + | |
| 84 | +/// State: the head of the branch is green. | |
| 85 | +pub const STATE_GREEN: &str = "green"; | |
| 86 | + | |
| 87 | +/// State: no fail is left unexplained, and the changesets above green wait | |
| 88 | +/// for a verdict. | |
| 89 | +pub const STATE_PENDING: &str = "pending"; | |
| 90 | + | |
| 91 | +/// State: a fail is left unexplained; `braid/red/<branch>` names the | |
| 92 | +/// culprit, or the first such fail. | |
| 93 | +pub const STATE_RED: &str = "red"; | |
| 94 | + | |
| 95 | +/// State: a revert of the culprit is landing. | |
| 96 | +pub const STATE_REVERTING: &str = "reverting"; | |
| 97 | + | |
| 98 | +/// State: a revert of the culprit landed, and waits for a verdict. | |
| 99 | +pub const STATE_REVERTED: &str = "reverted"; | |
| 100 | + | |
| 101 | +/// State: red, and the hub cannot go further by itself; `code` says why. | |
| 102 | +pub const STATE_STUCK: &str = "stuck"; | |
| 103 | + | |
| 104 | +/// State: verification waits on something outside the branch. | |
| 105 | +pub const STATE_PAUSED: &str = "paused"; | |
| 106 | + | |
| 107 | +/// The states of a branch, as `VerifyStatus.state` gives them. | |
| 108 | +pub const STATES: [&str; 9] = [ | |
| 109 | + STATE_OFF, | |
| 110 | + STATE_UNKNOWN, | |
| 111 | + STATE_GREEN, | |
| 112 | + STATE_PENDING, | |
| 113 | + STATE_RED, | |
| 114 | + STATE_REVERTING, | |
| 115 | + STATE_REVERTED, | |
| 116 | + STATE_STUCK, | |
| 117 | + STATE_PAUSED, | |
| 118 | +]; | |
| 119 | + | |
| 120 | +/// Code: the branch is red, and has no green changeset to start from. | |
| 121 | +pub const CODE_NO_GREEN: &str = "no_green"; | |
| 122 | + | |
| 123 | +/// Code: more than one changeset separates the last pass from the first | |
| 124 | +/// fail, so none is named the culprit. | |
| 125 | +pub const CODE_CULPRIT_UNKNOWN: &str = "culprit_unknown"; | |
| 126 | + | |
| 127 | +/// Code: a revert fails itself, so it hides what it reverted. | |
| 128 | +pub const CODE_HIDDEN: &str = "hidden"; | |
| 129 | + | |
| 130 | +/// Code: more than 10,000 changesets are above green, too many to look at. | |
| 131 | +pub const CODE_RANGE_TOO_LONG: &str = "range_too_long"; | |
| 132 | + | |
| 133 | +/// Code: green was no longer on the branch's first-parent chain, as after | |
| 134 | +/// the branch was made again, so the hub started over. | |
| 135 | +pub const CODE_GREEN_OFF_CHAIN: &str = "green_off_chain"; | |
| 136 | + | |
| 137 | +/// Code: the newest verdict is flaky. | |
| 138 | +pub const CODE_FLAKY: &str = "flaky"; | |
| 139 | + | |
| 140 | +/// The codes that say why a branch is in its state. | |
| 141 | +pub const CODES: [&str; 6] = [ | |
| 142 | + CODE_NO_GREEN, | |
| 143 | + CODE_CULPRIT_UNKNOWN, | |
| 144 | + CODE_HIDDEN, | |
| 145 | + CODE_RANGE_TOO_LONG, | |
| 146 | + CODE_GREEN_OFF_CHAIN, | |
| 147 | + CODE_FLAKY, | |
| 148 | +]; | |
| 149 | + | |
| 150 | +/// A culprit named because one changeset separates a pass from a fail. | |
| 151 | +pub const DECIDED_SINGLE: &str = "single"; | |
| 152 | + | |
| 153 | +/// A culprit named by bisecting. | |
| 154 | +pub const DECIDED_BISECT: &str = "bisect"; | |
| 155 | + | |
| 156 | +/// How a culprit was named. | |
| 157 | +pub const DECIDED_BY: [&str; 2] = [DECIDED_SINGLE, DECIDED_BISECT]; | |
| 158 | + | |
| 159 | +/// The window starts at green. | |
| 160 | +pub const LOWER_GREEN: &str = "green"; | |
| 161 | + | |
| 162 | +/// The window starts at a changeset that passed. | |
| 163 | +pub const LOWER_PASS: &str = "pass"; | |
| 164 | + | |
| 165 | +/// The window starts at a revert. | |
| 166 | +pub const LOWER_REVERT: &str = "revert"; | |
| 167 | + | |
| 168 | +/// The window has no lower bound. | |
| 169 | +pub const LOWER_NONE: &str = "none"; | |
| 170 | + | |
| 171 | +/// What the lower bound of a window is. | |
| 172 | +pub const LOWER_KINDS: [&str; 4] = [LOWER_GREEN, LOWER_PASS, LOWER_REVERT, LOWER_NONE]; | |
| 173 | + | |
| 174 | +/// Effect: the report turned the branch red. | |
| 175 | +pub const EFFECT_RED_SET: &str = "red_set"; | |
| 176 | + | |
| 177 | +/// Effect: the report moved green forward. | |
| 178 | +pub const EFFECT_GREEN_MOVED: &str = "green_moved"; | |
| 179 | + | |
| 180 | +/// Effect: the report ended the branch's red. | |
| 181 | +pub const EFFECT_RED_CLEARED: &str = "red_cleared"; | |
| 182 | + | |
| 183 | +/// Effect: the report was recorded, and moved no ref. | |
| 184 | +pub const EFFECT_RECORDED: &str = "recorded"; | |
| 185 | + | |
| 186 | +/// Effect: the hub had this verdict from this reporter already. | |
| 187 | +pub const EFFECT_DUPLICATE: &str = "duplicate"; | |
| 188 | + | |
| 189 | +/// Effect: the changeset is at or below green: nothing was recorded. | |
| 190 | +pub const EFFECT_STALE: &str = "stale"; | |
| 191 | + | |
| 192 | +/// What a report did. | |
| 193 | +pub const EFFECTS: [&str; 6] = [ | |
| 194 | + EFFECT_RED_SET, | |
| 195 | + EFFECT_GREEN_MOVED, | |
| 196 | + EFFECT_RED_CLEARED, | |
| 197 | + EFFECT_RECORDED, | |
| 198 | + EFFECT_DUPLICATE, | |
| 199 | + EFFECT_STALE, | |
| 200 | +]; | |
| 201 | + | |
| 202 | +/// Event: an admin changed the policy. | |
| 203 | +pub const EVENT_POLICY: &str = "policy"; | |
| 204 | + | |
| 205 | +/// Event: green moved. | |
| 206 | +pub const EVENT_GREEN: &str = "green"; | |
| 207 | + | |
| 208 | +/// Event: the branch turned red. | |
| 209 | +pub const EVENT_RED: &str = "red"; | |
| 210 | + | |
| 211 | +/// Event: the hub named a culprit. | |
| 212 | +pub const EVENT_CULPRIT: &str = "culprit"; | |
| 213 | + | |
| 214 | +/// Event: the branch is no longer red. | |
| 215 | +pub const EVENT_FIXED: &str = "fixed"; | |
| 216 | + | |
| 217 | +/// Event: the branch is stuck. | |
| 218 | +pub const EVENT_STUCK: &str = "stuck"; | |
| 219 | + | |
| 220 | +/// Event: green or the fails were reset, by an admin or by the hub. | |
| 221 | +pub const EVENT_RESET: &str = "reset"; | |
| 222 | + | |
| 223 | +/// Event: a verdict was flaky. | |
| 224 | +pub const EVENT_FLAKY: &str = "flaky"; | |
| 225 | + | |
| 226 | +/// The events of the log. | |
| 227 | +pub const EVENTS: [&str; 8] = [ | |
| 228 | + EVENT_POLICY, | |
| 229 | + EVENT_GREEN, | |
| 230 | + EVENT_RED, | |
| 231 | + EVENT_CULPRIT, | |
| 232 | + EVENT_FIXED, | |
| 233 | + EVENT_STUCK, | |
| 234 | + EVENT_RESET, | |
| 235 | + EVENT_FLAKY, | |
| 236 | +]; | |
| 237 | + | |
| 238 | +/// Notice: the default branch turned red. | |
| 239 | +pub const NOTICE_RED: &str = "verify_red"; | |
| 240 | + | |
| 241 | +/// Notice: the default branch is no longer red. | |
| 242 | +pub const NOTICE_FIXED: &str = "verify_fixed"; | |
| 243 | + | |
| 244 | +/// Notice: the default branch is stuck. | |
| 245 | +pub const NOTICE_STUCK: &str = "verify_stuck"; | |
| 246 | + | |
| 247 | +/// The codes of the notices of verification in the inbox (section 21), | |
| 248 | +/// whose reason is `system`. | |
| 249 | +pub const NOTICES: [&str; 3] = [NOTICE_RED, NOTICE_FIXED, NOTICE_STUCK]; | |
| 250 | + | |
| 251 | +/// The longest suite label. | |
| 252 | +pub const MAX_SUITE: usize = 32; | |
| 253 | + | |
| 254 | +/// The most reporters a policy names. | |
| 255 | +pub const MAX_REPORTERS: usize = 10; | |
| 256 | + | |
| 257 | +/// The largest body of `PUT …/verify/policy`, in bytes. | |
| 258 | +pub const MAX_POLICY_BODY: u64 = 16 * 1024; | |
| 259 | + | |
| 260 | +/// The largest body of `POST …/verify/report`, in bytes. | |
| 261 | +pub const MAX_REPORT_BODY: u64 = 4 * 1024; | |
| 262 | + | |
| 263 | +/// Whether `text` is a suite label: 1 to 32 of `a-z 0-9 . _ -`, starting | |
| 264 | +/// with a letter or a digit. | |
| 265 | +pub fn is_suite(text: &str) -> bool { | |
| 266 | + let bytes = text.as_bytes(); | |
| 267 | + let Some(first) = bytes.first() else { | |
| 268 | + return false; | |
| 269 | + }; | |
| 270 | + bytes.len() <= MAX_SUITE | |
| 271 | + && (first.is_ascii_lowercase() || first.is_ascii_digit()) | |
| 272 | + && bytes | |
| 273 | + .iter() | |
| 274 | + .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b"._-".contains(b)) | |
| 275 | +} | |
| 276 | + | |
| 277 | +/// Whether `text` is a git commit as the protocol writes one: 40 lowercase | |
| 278 | +/// hex digits. | |
| 279 | +pub fn is_git_commit(text: &str) -> bool { | |
| 280 | + text.len() == 40 && text.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f')) | |
| 281 | +} | |
| 282 | + | |
| 283 | +/// `GET` and `PUT …/verify/policy`: how a repository's default branch is | |
| 284 | +/// verified. `PUT` replaces it whole. | |
| 285 | +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] | |
| 286 | +#[serde(default)] | |
| 287 | +pub struct VerifyPolicy { | |
| 288 | + /// [`MODE_OFF`] or [`MODE_REPORT`]. | |
| 289 | + pub mode: String, | |
| 290 | + /// The sources whose passes move green: [`SOURCE_REPORT`]. | |
| 291 | + pub green_from: Vec<String>, | |
| 292 | + /// Whether a flaky changeset moves green too. | |
| 293 | + pub flaky_moves_green: bool, | |
| 294 | + /// Who reports, and on which suite; needed with [`MODE_REPORT`]. | |
| 295 | + #[serde(skip_serializing_if = "Option::is_none")] | |
| 296 | + pub report: Option<ReportPolicy>, | |
| 297 | +} | |
| 298 | + | |
| 299 | +impl Default for VerifyPolicy { | |
| 300 | + /// The policy of a repository whose verification was never set: off. | |
| 301 | + fn default() -> Self { | |
| 302 | + VerifyPolicy { | |
| 303 | + mode: MODE_OFF.to_owned(), | |
| 304 | + green_from: Vec::new(), | |
| 305 | + flaky_moves_green: false, | |
| 306 | + report: None, | |
| 307 | + } | |
| 308 | + } | |
| 309 | +} | |
| 310 | + | |
| 311 | +/// The reporters of a policy, and the suite they report on. | |
| 312 | +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] | |
| 313 | +#[serde(default)] | |
| 314 | +pub struct ReportPolicy { | |
| 315 | + /// The suite's label (see [`is_suite`]). | |
| 316 | + pub suite: String, | |
| 317 | + /// The logins whose reports count, 1 to [`MAX_REPORTERS`], lowercase. | |
| 318 | + pub reporters: Vec<String>, | |
| 319 | +} | |
| 320 | + | |
| 321 | +/// `GET …/verify`: how the default branch stands. Also in the answers of | |
| 322 | +/// `POST …/verify/report` and `POST …/verify/reset`. | |
| 323 | +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] | |
| 324 | +#[serde(default)] | |
| 325 | +pub struct VerifyStatus { | |
| 326 | + /// The policy's mode. | |
| 327 | + pub mode: String, | |
| 328 | + /// The branch verified, the default branch; `None` with mode `off`. | |
| 329 | + pub branch: Option<String>, | |
| 330 | + /// One of [`STATES`]. | |
| 331 | + pub state: String, | |
| 332 | + /// Why, one of [`CODES`], when there is something to say. | |
| 333 | + pub code: Option<String>, | |
| 334 | + /// The head of the branch the hub last looked at. | |
| 335 | + #[serde(with = "hex::opt_id")] | |
| 336 | + pub head: Option<ObjectId>, | |
| 337 | + /// `braid/green/<branch>`. | |
| 338 | + #[serde(with = "hex::opt_id")] | |
| 339 | + pub green: Option<ObjectId>, | |
| 340 | + /// `braid/red/<branch>`. | |
| 341 | + #[serde(with = "hex::opt_id")] | |
| 342 | + pub red: Option<ObjectId>, | |
| 343 | + /// The changeset the hub blames, when it names one. | |
| 344 | + #[serde(with = "hex::opt_id")] | |
| 345 | + pub culprit: Option<ObjectId>, | |
| 346 | + /// How the culprit was named: one of [`DECIDED_BY`]. | |
| 347 | + pub decided_by: Option<String>, | |
| 348 | + /// Since when the branch is red. | |
| 349 | + pub since: Option<String>, | |
| 350 | + /// Where the culprit is looked for, while the branch is red. | |
| 351 | + pub window: Option<VerifyWindow>, | |
| 352 | + /// Verdicts the hub waits for; empty in this version of the protocol. | |
| 353 | + pub pending: Vec<PendingVerdict>, | |
| 354 | + /// Kept as the hub gave it: `None` in this version of the protocol. | |
| 355 | + pub round: Option<serde_json::Value>, | |
| 356 | + /// Kept as the hub gave it: `None` in this version of the protocol. | |
| 357 | + pub revert: Option<serde_json::Value>, | |
| 358 | + /// Kept as the hub gave it: `None` in this version of the protocol. | |
| 359 | + pub hold: Option<serde_json::Value>, | |
| 360 | + /// What only the repository's admins see. | |
| 361 | + pub admin: Option<AdminView>, | |
| 362 | +} | |
| 363 | + | |
| 364 | +impl Default for VerifyStatus { | |
| 365 | + /// The status of a repository whose verification is off. | |
| 366 | + fn default() -> Self { | |
| 367 | + VerifyStatus { | |
| 368 | + mode: MODE_OFF.to_owned(), | |
| 369 | + branch: None, | |
| 370 | + state: STATE_OFF.to_owned(), | |
| 371 | + code: None, | |
| 372 | + head: None, | |
| 373 | + green: None, | |
| 374 | + red: None, | |
| 375 | + culprit: None, | |
| 376 | + decided_by: None, | |
| 377 | + since: None, | |
| 378 | + window: None, | |
| 379 | + pending: Vec::new(), | |
| 380 | + round: None, | |
| 381 | + revert: None, | |
| 382 | + hold: None, | |
| 383 | + admin: None, | |
| 384 | + } | |
| 385 | + } | |
| 386 | +} | |
| 387 | + | |
| 388 | +/// The changesets a culprit is looked for among: above `lower`, up to and | |
| 389 | +/// including `upper`. | |
| 390 | +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] | |
| 391 | +#[serde(default)] | |
| 392 | +pub struct VerifyWindow { | |
| 393 | + /// The last changeset known to be good, or `None`. | |
| 394 | + #[serde(with = "hex::opt_id")] | |
| 395 | + pub lower: Option<ObjectId>, | |
| 396 | + /// What it is: one of [`LOWER_KINDS`]. | |
| 397 | + pub lower_kind: String, | |
| 398 | + /// The first fail the hub cannot explain. | |
| 399 | + #[serde(with = "hex::opt_id")] | |
| 400 | + pub upper: Option<ObjectId>, | |
| 401 | + /// How many changesets the window holds. | |
| 402 | + pub candidates: u64, | |
| 403 | +} | |
| 404 | + | |
| 405 | +impl Default for VerifyWindow { | |
| 406 | + fn default() -> Self { | |
| 407 | + VerifyWindow { | |
| 408 | + lower: None, | |
| 409 | + lower_kind: LOWER_NONE.to_owned(), | |
| 410 | + upper: None, | |
| 411 | + candidates: 0, | |
| 412 | + } | |
| 413 | + } | |
| 414 | +} | |
| 415 | + | |
| 416 | +/// A verdict the hub waits for. | |
| 417 | +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] | |
| 418 | +#[serde(default)] | |
| 419 | +pub struct PendingVerdict { | |
| 420 | + /// The changeset. | |
| 421 | + #[serde(with = "hex::opt_id")] | |
| 422 | + pub changeset: Option<ObjectId>, | |
| 423 | + /// Where the verdict will come from. | |
| 424 | + pub source: String, | |
| 425 | + /// Since when the hub waits. | |
| 426 | + pub since: String, | |
| 427 | +} | |
| 428 | + | |
| 429 | +/// What [`VerifyStatus`] shows the repository's admins only. | |
| 430 | +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] | |
| 431 | +#[serde(default)] | |
| 432 | +pub struct AdminView { | |
| 433 | + /// The account that landed the culprit, when the hub knows it. | |
| 434 | + pub lander: Option<String>, | |
| 435 | +} | |
| 436 | + | |
| 437 | +/// `POST …/verify/report`: how a suite went on a changeset of the default | |
| 438 | +/// branch, named by its git commit or by its id, exactly one of them. | |
| 439 | +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] | |
| 440 | +#[serde(default)] | |
| 441 | +pub struct VerifyReport { | |
| 442 | + /// The git commit the changeset exports to, 40 lowercase hex digits. | |
| 443 | + #[serde(skip_serializing_if = "Option::is_none")] | |
| 444 | + pub git_commit: Option<String>, | |
| 445 | + /// The changeset. | |
| 446 | + #[serde(with = "hex::opt_id", skip_serializing_if = "Option::is_none")] | |
| 447 | + pub changeset: Option<ObjectId>, | |
| 448 | + /// One of [`OUTCOMES`]. | |
| 449 | + pub outcome: String, | |
| 450 | + /// The suite, as the policy names it. | |
| 451 | + pub suite: String, | |
| 452 | +} | |
| 453 | + | |
| 454 | +/// The answer of `POST …/verify/report`. | |
| 455 | +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] | |
| 456 | +#[serde(default)] | |
| 457 | +pub struct VerifyReportAnswer { | |
| 458 | + /// What the report did: one of [`EFFECTS`]. | |
| 459 | + pub effect: String, | |
| 460 | + /// How the branch stands now. | |
| 461 | + pub status: VerifyStatus, | |
| 462 | +} | |
| 463 | + | |
| 464 | +/// One event of `GET …/verify/log`. | |
| 465 | +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] | |
| 466 | +#[serde(default)] | |
| 467 | +pub struct VerifyEvent { | |
| 468 | + /// Its id, which grows with each event. | |
| 469 | + pub id: u64, | |
| 470 | + /// When. | |
| 471 | + pub at: String, | |
| 472 | + /// What happened: one of [`EVENTS`]. | |
| 473 | + pub code: String, | |
| 474 | + /// The changeset it is about, if any. | |
| 475 | + #[serde(with = "hex::opt_id")] | |
| 476 | + pub changeset: Option<ObjectId>, | |
| 477 | + /// Another id, or a code, that goes with it. | |
| 478 | + pub other: Option<String>, | |
| 479 | + /// The login that did it; `None` for the hub, and for callers who are | |
| 480 | + /// not the repository's admins. | |
| 481 | + pub actor: Option<String>, | |
| 482 | +} | |
| 483 | + | |
| 484 | +/// `GET …/verify/log`: a page of events, newest first. | |
| 485 | +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] | |
| 486 | +#[serde(default)] | |
| 487 | +pub struct VerifyEvents { | |
| 488 | + /// The events. | |
| 489 | + pub events: Vec<VerifyEvent>, | |
| 490 | + /// The `after` of the next page, or `None` on the last one. | |
| 491 | + pub next: Option<u64>, | |
| 492 | +} | |
| 493 | + | |
| 494 | +/// `POST …/verify/reset`: move green, or dismiss the fails there are now. | |
| 495 | +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] | |
| 496 | +#[serde(default)] | |
| 497 | +pub struct VerifyReset { | |
| 498 | + /// Where green goes, on the head's first-parent chain, backward too; | |
| 499 | + /// `None` dismisses the fails there are now instead. | |
| 500 | + #[serde(with = "hex::opt_id")] | |
| 501 | + pub green: Option<ObjectId>, | |
| 502 | + /// What the caller saw: when the refs moved since, the hub answers 409 | |
| 503 | + /// `moved` and does nothing. | |
| 504 | + pub expect: Option<ResetExpect>, | |
| 505 | +} | |
| 506 | + | |
| 507 | +/// The refs a reset expects. | |
| 508 | +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] | |
| 509 | +#[serde(default)] | |
| 510 | +pub struct ResetExpect { | |
| 511 | + /// `braid/green/<branch>`, or `None` when there is none. | |
| 512 | + #[serde(with = "hex::opt_id")] | |
| 513 | + pub green: Option<ObjectId>, | |
| 514 | + /// `braid/red/<branch>`, or `None` when there is none. | |
| 515 | + #[serde(with = "hex::opt_id")] | |
| 516 | + pub red: Option<ObjectId>, | |
| 517 | +} | |
| 518 | + | |
| 519 | +/// `verify` in the answer of a landing on the default branch of a | |
| 520 | +/// repository whose verification is on: how the branch stands. | |
| 521 | +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] | |
| 522 | +#[serde(default)] | |
| 523 | +pub struct LandVerify { | |
| 524 | + /// One of [`STATES`]. | |
| 525 | + pub state: String, | |
| 526 | + /// `braid/green/<branch>`. | |
| 527 | + #[serde(with = "hex::opt_id")] | |
| 528 | + pub green: Option<ObjectId>, | |
| 529 | + /// `braid/red/<branch>`. | |
| 530 | + #[serde(with = "hex::opt_id")] | |
| 531 | + pub red: Option<ObjectId>, | |
| 532 | + /// The changeset the hub blames, when it names one. | |
| 533 | + #[serde(with = "hex::opt_id")] | |
| 534 | + pub culprit: Option<ObjectId>, | |
| 535 | + /// Reverts the hub landed that the landing was rebased over; empty in | |
| 536 | + /// this version of the protocol. | |
| 537 | + pub reverted: Vec<RevertedPair>, | |
| 538 | +} | |
| 539 | + | |
| 540 | +/// A changeset, and the revert of it the hub landed. | |
| 541 | +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] | |
| 542 | +#[serde(default)] | |
| 543 | +pub struct RevertedPair { | |
| 544 | + /// The changeset reverted. | |
| 545 | + #[serde(with = "hex::opt_id")] | |
| 546 | + pub culprit: Option<ObjectId>, | |
| 547 | + /// The revert. | |
| 548 | + #[serde(with = "hex::opt_id")] | |
| 549 | + pub revert: Option<ObjectId>, | |
| 550 | +} | |
| 551 | + | |
| 552 | +#[cfg(test)] | |
| 553 | +mod tests { | |
| 554 | + use super::*; | |
| 555 | + use braid_format::Kind; | |
| 556 | + use serde::de::DeserializeOwned; | |
| 557 | + use serde_json::{Value, json}; | |
| 558 | + | |
| 559 | + fn id(text: &[u8]) -> ObjectId { | |
| 560 | + ObjectId::hash(Kind::Changeset, text) | |
| 561 | + } | |
| 562 | + | |
| 563 | + /// Written and read again, `value` is itself. | |
| 564 | + fn round_trips<T: Serialize + DeserializeOwned + PartialEq + std::fmt::Debug>(value: &T) { | |
| 565 | + let text = serde_json::to_string(value).unwrap(); | |
| 566 | + assert_eq!(&serde_json::from_str::<T>(&text).unwrap(), value, "{text}"); | |
| 567 | + } | |
| 568 | + | |
| 569 | + #[test] | |
| 570 | + fn refs_and_closed_sets() { | |
| 571 | + assert_eq!(green_ref("main"), "braid/green/main"); | |
| 572 | + assert_eq!(red_ref("release/1"), "braid/red/release/1"); | |
| 573 | + assert!(green_ref("x").starts_with(GREEN_PREFIX)); | |
| 574 | + assert!(red_ref("x").starts_with(RED_PREFIX)); | |
| 575 | + // Each set holds each word once, and the words are what the wire | |
| 576 | + // says. | |
| 577 | + for set in [ | |
| 578 | + &MODES[..], | |
| 579 | + &SOURCES, | |
| 580 | + &OUTCOMES, | |
| 581 | + &STATES, | |
| 582 | + &CODES, | |
| 583 | + &DECIDED_BY, | |
| 584 | + &LOWER_KINDS, | |
| 585 | + &EFFECTS, | |
| 586 | + &EVENTS, | |
| 587 | + &NOTICES, | |
| 588 | + ] { | |
| 589 | + let mut words = set.to_vec(); | |
| 590 | + words.sort_unstable(); | |
| 591 | + words.dedup(); | |
| 592 | + assert_eq!(words.len(), set.len(), "{set:?}"); | |
| 593 | + for word in set { | |
| 594 | + assert!( | |
| 595 | + word.bytes().all(|b| b.is_ascii_lowercase() || b == b'_'), | |
| 596 | + "{word}" | |
| 597 | + ); | |
| 598 | + } | |
| 599 | + } | |
| 600 | + assert!(NOTICES.iter().all(|code| code.starts_with("verify_"))); | |
| 601 | + assert_eq!(FEATURE, "verify"); | |
| 602 | + } | |
| 603 | + | |
| 604 | + #[test] | |
| 605 | + fn suites_and_commits() { | |
| 606 | + for good in ["ci", "0", "unit-tests", "a.b_c-d", &"a".repeat(32)] { | |
| 607 | + assert!(is_suite(good), "{good:?}"); | |
| 608 | + } | |
| 609 | + for bad in [ | |
| 610 | + "", | |
| 611 | + "CI", | |
| 612 | + "-ci", | |
| 613 | + ".ci", | |
| 614 | + "_ci", | |
| 615 | + "c i", | |
| 616 | + "ci/x", | |
| 617 | + "é", | |
| 618 | + &"a".repeat(33), | |
| 619 | + ] { | |
| 620 | + assert!(!is_suite(bad), "{bad:?}"); | |
| 621 | + } | |
| 622 | + assert!(is_git_commit(&"0a".repeat(20))); | |
| 623 | + for bad in [ | |
| 624 | + "", | |
| 625 | + &"0A".repeat(20), | |
| 626 | + &"0".repeat(39), | |
| 627 | + &"0".repeat(64), | |
| 628 | + &"g".repeat(40), | |
| 629 | + ] { | |
| 630 | + assert!(!is_git_commit(bad), "{bad:?}"); | |
| 631 | + } | |
| 632 | + } | |
| 633 | + | |
| 634 | + #[test] | |
| 635 | + fn every_body_round_trips() { | |
| 636 | + let (a, b, c) = (id(b"a"), id(b"b"), id(b"c")); | |
| 637 | + round_trips(&VerifyPolicy { | |
| 638 | + mode: MODE_REPORT.into(), | |
| 639 | + green_from: vec![SOURCE_REPORT.into()], | |
| 640 | + flaky_moves_green: true, | |
| 641 | + report: Some(ReportPolicy { | |
| 642 | + suite: "ci".into(), | |
| 643 | + reporters: vec!["ci-bot".into()], | |
| 644 | + }), | |
| 645 | + }); | |
| 646 | + round_trips(&VerifyPolicy::default()); | |
| 647 | + let status = VerifyStatus { | |
| 648 | + mode: MODE_REPORT.into(), | |
| 649 | + branch: Some("main".into()), | |
| 650 | + state: STATE_RED.into(), | |
| 651 | + code: Some(CODE_CULPRIT_UNKNOWN.into()), | |
| 652 | + head: Some(c), | |
| 653 | + green: Some(a), | |
| 654 | + red: Some(c), | |
| 655 | + culprit: None, | |
| 656 | + decided_by: None, | |
| 657 | + since: Some("2026-10-22T12:03:00Z".into()), | |
| 658 | + window: Some(VerifyWindow { | |
| 659 | + lower: Some(a), | |
| 660 | + lower_kind: LOWER_GREEN.into(), | |
| 661 | + upper: Some(c), | |
| 662 | + candidates: 2, | |
| 663 | + }), | |
| 664 | + pending: vec![PendingVerdict { | |
| 665 | + changeset: Some(b), | |
| 666 | + source: SOURCE_REPORT.into(), | |
| 667 | + since: "2026-10-22T12:00:00Z".into(), | |
| 668 | + }], | |
| 669 | + round: Some(json!({"n": 4})), | |
| 670 | + revert: None, | |
| 671 | + hold: None, | |
| 672 | + admin: Some(AdminView { | |
| 673 | + lander: Some("ada".into()), | |
| 674 | + }), | |
| 675 | + }; | |
| 676 | + round_trips(&status); | |
| 677 | + round_trips(&VerifyStatus::default()); | |
| 678 | + round_trips(&VerifyReport { | |
| 679 | + git_commit: Some("0a".repeat(20)), | |
| 680 | + changeset: None, | |
| 681 | + outcome: FAIL.into(), | |
| 682 | + suite: "ci".into(), | |
| 683 | + }); | |
| 684 | + round_trips(&VerifyReport { | |
| 685 | + git_commit: None, | |
| 686 | + changeset: Some(b), | |
| 687 | + outcome: PASS.into(), | |
| 688 | + suite: "ci".into(), | |
| 689 | + }); | |
| 690 | + round_trips(&VerifyReportAnswer { | |
| 691 | + effect: EFFECT_RED_SET.into(), | |
| 692 | + status: status.clone(), | |
| 693 | + }); | |
| 694 | + round_trips(&VerifyEvents { | |
| 695 | + events: vec![VerifyEvent { | |
| 696 | + id: 17, | |
| 697 | + at: "2026-10-22T12:03:00Z".into(), | |
| 698 | + code: EVENT_RED.into(), | |
| 699 | + changeset: Some(c), | |
| 700 | + other: Some(CODE_CULPRIT_UNKNOWN.into()), | |
| 701 | + actor: None, | |
| 702 | + }], | |
| 703 | + next: Some(17), | |
| 704 | + }); | |
| 705 | + round_trips(&VerifyReset { | |
| 706 | + green: Some(a), | |
| 707 | + expect: Some(ResetExpect { | |
| 708 | + green: Some(b), | |
| 709 | + red: None, | |
| 710 | + }), | |
| 711 | + }); | |
| 712 | + round_trips(&VerifyReset::default()); | |
| 713 | + round_trips(&LandVerify { | |
| 714 | + state: STATE_RED.into(), | |
| 715 | + green: Some(a), | |
| 716 | + red: Some(c), | |
| 717 | + culprit: Some(c), | |
| 718 | + reverted: vec![RevertedPair { | |
| 719 | + culprit: Some(b), | |
| 720 | + revert: Some(c), | |
| 721 | + }], | |
| 722 | + }); | |
| 723 | + } | |
| 724 | + | |
| 725 | + #[test] | |
| 726 | + fn on_the_wire() { | |
| 727 | + let a = id(b"a"); | |
| 728 | + // A report names exactly what it was given. | |
| 729 | + let report = VerifyReport { | |
| 730 | + git_commit: Some("0a".repeat(20)), | |
| 731 | + changeset: None, | |
| 732 | + outcome: PASS.into(), | |
| 733 | + suite: "ci".into(), | |
| 734 | + }; | |
| 735 | + assert_eq!( | |
| 736 | + serde_json::to_value(&report).unwrap(), | |
| 737 | + json!({"git_commit": "0a".repeat(20), "outcome": "pass", "suite": "ci"}) | |
| 738 | + ); | |
| 739 | + // A policy never set says so, and leaves out the block it lacks. | |
| 740 | + assert_eq!( | |
| 741 | + serde_json::to_value(VerifyPolicy::default()).unwrap(), | |
| 742 | + json!({"mode": "off", "green_from": [], "flaky_moves_green": false}) | |
| 743 | + ); | |
| 744 | + // Ids the hub has none of are null, and so are the parts of the | |
| 745 | + // status this version of the protocol leaves empty. | |
| 746 | + let status = serde_json::to_value(VerifyStatus::default()).unwrap(); | |
| 747 | + for key in ["branch", "head", "green", "red", "culprit", "round", "hold"] { | |
| 748 | + assert_eq!(status[key], Value::Null, "{key}"); | |
| 749 | + } | |
| 750 | + assert_eq!(status["state"], "off"); | |
| 751 | + assert_eq!(status["pending"], json!([])); | |
| 752 | + // A reset that expects no green says so with null. | |
| 753 | + let reset = VerifyReset { | |
| 754 | + green: None, | |
| 755 | + expect: Some(ResetExpect { | |
| 756 | + green: None, | |
| 757 | + red: Some(a), | |
| 758 | + }), | |
| 759 | + }; | |
| 760 | + assert_eq!( | |
| 761 | + serde_json::to_value(&reset).unwrap(), | |
| 762 | + json!({"green": null, "expect": {"green": null, "red": a.to_hex()}}) | |
| 763 | + ); | |
| 764 | + // A bad id is refused. | |
| 765 | + assert!(serde_json::from_str::<LandVerify>(r#"{"state":"red","red":"xyz"}"#).is_err()); | |
| 766 | + } | |
| 767 | + | |
| 768 | + #[test] | |
| 769 | + fn what_a_hub_leaves_out_or_adds_still_reads() { | |
| 770 | + // An answer of a newer hub, with fields this crate does not know. | |
| 771 | + let status: VerifyStatus = serde_json::from_str( | |
| 772 | + r#"{"mode":"auto","state":"held","hold":{"until":"x"},"later":[1,2],"window":{"upper":null,"later":true}}"#, | |
| 773 | + ) | |
| 774 | + .unwrap(); | |
| 775 | + assert_eq!( | |
| 776 | + (status.mode.as_str(), status.state.as_str()), | |
| 777 | + ("auto", "held") | |
| 778 | + ); | |
| 779 | + assert_eq!(status.hold, Some(json!({"until": "x"}))); | |
| 780 | + assert_eq!(status.window.unwrap().lower_kind, LOWER_NONE); | |
| 781 | + // A body with nothing in it takes every default. | |
| 782 | + assert_eq!( | |
| 783 | + serde_json::from_str::<VerifyStatus>("{}").unwrap(), | |
| 784 | + VerifyStatus::default() | |
| 785 | + ); | |
| 786 | + assert_eq!( | |
| 787 | + serde_json::from_str::<VerifyPolicy>("{}").unwrap(), | |
| 788 | + VerifyPolicy::default() | |
| 789 | + ); | |
| 790 | + let answer: VerifyReportAnswer = serde_json::from_str(r#"{"effect":"stale"}"#).unwrap(); | |
| 791 | + assert_eq!(answer.status.state, STATE_OFF); | |
| 792 | + let land: LandVerify = serde_json::from_str(r#"{"state":"green"}"#).unwrap(); | |
| 793 | + assert!(land.reverted.is_empty() && land.culprit.is_none()); | |
| 794 | + let events: VerifyEvents = serde_json::from_str(r#"{"events":[{"id":3}]}"#).unwrap(); | |
| 795 | + assert_eq!((events.events[0].id, events.next), (3, None)); | |
| 796 | + } | |
| 797 | +} |