Cotton code
176 lines · 6.0 KB · RustRaw
//! WHO a version row records — the honest reading of `actor_role`.//!//! The column is a provenance stamp, not an identity: it says which kind of//! hand wrote the edit, because that is the only attribution a trigger can//! make. The pure half here returns a TOKEN rather than a sentence, so the one//! place a reader's words are chosen stays the app's catalog.use super::*;#[test]fn reads_the_owners_own_hand_from_every_role_the_writers_stamp_for_it() {    for role in ["authenticated", "owner", "user"] {        assert_eq!(version_actor(role), VersionActor::You, "{role}");    }}#[test]fn reads_an_assistant_from_the_mcp_and_the_old_service_role_alike() {    for role in ["agent", "service_role"] {        assert_eq!(version_actor(role), VersionActor::Assistant, "{role}");    }}#[test]fn reads_cottons_own_workers_as_the_system() {    for role in ["system", "server"] {        assert_eq!(version_actor(role), VersionActor::System, "{role}");    }}#[test]fn reads_a_relayed_chat_write_as_a_peer() {    assert_eq!(version_actor("peer"), VersionActor::Peer);}#[test]fn reads_the_files_own_trigger_stamp_as_an_unattributed_edit() {    assert_eq!(version_actor("local"), VersionActor::Direct);}#[test]fn falls_back_to_an_unattributed_edit_for_a_role_it_has_never_seen() {    assert_eq!(version_actor("something_new"), VersionActor::Direct);    assert_eq!(version_actor(""), VersionActor::Direct);}#[test]fn ignores_case_and_surrounding_space_since_the_stamp_is_free_text() {    assert_eq!(version_actor("  Agent "), VersionActor::Assistant);}/// The TypeScript sorts `VERSION_ACTOR_ROLES` and compares; here the list is/// the enum, so the same claim is that every declared role is present and the/// declared ORDER has not been shuffled.#[test]fn names_every_role_a_writer_in_this_repo_actually_stamps() {    let mut sorted: Vec<&str> = VersionActorRole::ALL.iter().map(|r| r.as_str()).collect();    sorted.sort_unstable();    assert_eq!(        sorted,        [            "agent",            "authenticated",            "local",            "owner",            "peer",            "server",            "service_role",            "system",            "user",        ]    );    assert_eq!(        VersionActorRole::ALL.map(|r| r.as_str()),        [            "local",            "authenticated",            "owner",            "user",            "agent",            "service_role",            "system",            "server",            "peer",        ]    );}#[test]fn maps_every_named_role_to_a_token() {    for role in VersionActorRole::ALL {        assert!(            VersionActor::ALL.contains(&version_actor(role.as_str())),            "{role}"        );        // The enum's own mapping and the free-text reading agree.        assert_eq!(version_actor(role.as_str()), role.actor(), "{role}");    }}#[test]fn a_role_round_trips_through_parse_and_as_str() {    for role in VersionActorRole::ALL {        assert_eq!(VersionActorRole::parse(role.as_str()), Some(role));        assert_eq!(role.to_string(), role.as_str());    }    assert_eq!(VersionActorRole::parse("nope"), None);}// ── Node vectors ─────────────────────────────────────────────────────────────//// Every expected value below was produced by running `versionActor` in Node// (`packages/core`, `npx tsx`), never by reasoning. Twenty-nine of them, and// three are the whole reason this function does not use Rust's `trim`./// `\u{feff}system\u{feff}` reads as the system, because a byte-order mark IS/// whitespace to `String.prototype.trim`. Rust's `str::trim` leaves it, so a/// port that used it would have read `Direct` here.#[test]fn a_stamp_wrapped_in_byte_order_marks_still_names_its_role() {    assert_eq!(        version_actor("\u{feff}system\u{feff}"),        VersionActor::System    );}/// U+0085 (NEL) is the mirror image: Rust's `trim` takes it, ECMAScript's does/// not, so the stamp stays `\u{85}user` and reads as an unattributed edit.#[test]fn a_stamp_preceded_by_a_next_line_character_is_not_trimmed() {    assert_eq!(version_actor("\u{85}user"), VersionActor::Direct);}#[test]fn the_trimmed_class_is_the_ecmascript_one_end_to_end() {    // Read out of Node, one line per input.    let vectors: &[(&str, VersionActor)] = &[        ("authenticated", VersionActor::You),        ("owner", VersionActor::You),        ("user", VersionActor::You),        ("agent", VersionActor::Assistant),        ("service_role", VersionActor::Assistant),        ("system", VersionActor::System),        ("server", VersionActor::System),        ("peer", VersionActor::Peer),        ("local", VersionActor::Direct),        ("", VersionActor::Direct),        ("something_new", VersionActor::Direct),        ("  Agent ", VersionActor::Assistant),        ("AGENT", VersionActor::Assistant),        ("\tOwner\n", VersionActor::You),        // NBSP either side — whitespace in both languages.        ("\u{a0}peer\u{a0}", VersionActor::Peer),        ("\u{feff}system\u{feff}", VersionActor::System),        ("\u{85}user", VersionActor::Direct),        ("SERVICE_ROLE", VersionActor::Assistant),        ("Peer ", VersionActor::Peer),        ("  ", VersionActor::Direct),        ("user ", VersionActor::You),        (" local ", VersionActor::Direct),        // Turkish dotted capital I lower-cases to two code points, so it        // matches nothing.        ("\u{130}", VersionActor::Direct),        ("USER", VersionActor::You),        ("owner\u{3000}", VersionActor::You),        ("\u{b}agent\u{c}", VersionActor::Assistant),        ("\u{2028} owner \u{2029}", VersionActor::You),        // A zero-width space is NOT whitespace to `\s`, so the stamp is        // `agent\u{200b}` and matches nothing.        ("agent\u{200b}", VersionActor::Direct),        ("  \r\n system \r\n ", VersionActor::System),    ];    for (input, expected) in vectors {        assert_eq!(version_actor(input), *expected, "{input:?}");    }}