A rule rides every event and can be rewritten
Browse files at this commit95bcb6d7707357426f396eebbb9d473dc18c8599Parent: 1f3547b- test
- a rule rides every event and can be rewritten
1 changed file +167 -0
- crates/izlek-web/tests/http.rs +167 -0
crates/izlek-web/tests/http.rsmodified+167 -0
| @@ -2817,3 +2817,170 @@ async fn the_settings_sidenav_offers_logs() { | |||
| 2817 | let html = String::from_utf8_lossy(&page.bytes); | 2817 | let html = String::from_utf8_lossy(&page.bytes); |
| 2818 | assert!(html.contains("href=\"/logs\""), "{html}"); | 2818 | assert!(html.contains("href=\"/logs\""), "{html}"); |
| 2819 | } | 2819 | } |
| 2820 | + | ||
| 2821 | +/// Polls the store until a `Rule` send for `rule_id` addressed to `recipient` | ||
| 2822 | +/// exists beyond the `already` count — the engine runs off the request in a | ||
| 2823 | +/// spawned task, so the row is not there yet when the triggering call | ||
| 2824 | +/// returns. Bounded so a send that never arrives fails the test instead of | ||
| 2825 | +/// hanging it. | ||
| 2826 | +async fn until_rule_send_to( | ||
| 2827 | + app: &App, | ||
| 2828 | + rule_id: &str, | ||
| 2829 | + recipient: &str, | ||
| 2830 | + already: usize, | ||
| 2831 | +) -> izlek_core::store::MailSend { | ||
| 2832 | + for _ in 0..500 { | ||
| 2833 | + let matching: Vec<_> = app | ||
| 2834 | + .store | ||
| 2835 | + .mail_queue(50) | ||
| 2836 | + .await | ||
| 2837 | + .unwrap() | ||
| 2838 | + .into_iter() | ||
| 2839 | + .filter(|send| { | ||
| 2840 | + send.kind == SendKind::Rule | ||
| 2841 | + && send.rule_id.as_deref() == Some(rule_id) | ||
| 2842 | + && send.recipient == recipient | ||
| 2843 | + }) | ||
| 2844 | + .collect(); | ||
| 2845 | + if matching.len() > already { | ||
| 2846 | + return matching.into_iter().next().unwrap(); | ||
| 2847 | + } | ||
| 2848 | + tokio::time::sleep(std::time::Duration::from_millis(10)).await; | ||
| 2849 | + } | ||
| 2850 | + panic!("no rule send to {recipient} for rule {rule_id} ever queued"); | ||
| 2851 | +} | ||
| 2852 | + | ||
| 2853 | +/// One rule rides two different events across its lifetime, and a rewrite in | ||
| 2854 | +/// place keeps riding the second without becoming a new rule: a comment mails | ||
| 2855 | +/// the task's creator, not the commenter, and after the rule is rewritten to | ||
| 2856 | +/// fire on a rename instead, a rename mails the assignee, not the renamer. | ||
| 2857 | +#[tokio::test] | ||
| 2858 | +async fn a_rule_rides_every_event_and_can_be_rewritten() { | ||
| 2859 | + let app = App::open_with_mail().await; | ||
| 2860 | + let admin_cookie = admin(&app).await; | ||
| 2861 | + let member = invited(&app, &admin_cookie, "deniz@izlek.sh", "Deniz", Role::Member).await; | ||
| 2862 | + let column = first_column(&app, &admin_cookie).await; | ||
| 2863 | + let task = a_task(&app, &admin_cookie, &column, "Ship the picker").await; | ||
| 2864 | + | ||
| 2865 | + let created = app | ||
| 2866 | + .post( | ||
| 2867 | + path::<izlek_web::rules::CreateRule>(), | ||
| 2868 | + Some(&admin_cookie), | ||
| 2869 | + &[ | ||
| 2870 | + ("trigger", "commented"), | ||
| 2871 | + ("column_id", ""), | ||
| 2872 | + ("subject", "Someone commented"), | ||
| 2873 | + ("audience", "creator"), | ||
| 2874 | + ], | ||
| 2875 | + ) | ||
| 2876 | + .await; | ||
| 2877 | + assert_eq!(created.body, "null", "{}", created.body); | ||
| 2878 | + let rule = only_rule(&app, &admin_cookie).await; | ||
| 2879 | + | ||
| 2880 | + // A second member comments; the task's creator is the admin, not them. | ||
| 2881 | + let commented = app | ||
| 2882 | + .post( | ||
| 2883 | + path::<izlek_web::detail::PostComment>(), | ||
| 2884 | + Some(&member), | ||
| 2885 | + &[("task_id", &task), ("body", "Looks good")], | ||
| 2886 | + ) | ||
| 2887 | + .await; | ||
| 2888 | + assert_eq!(commented.body, "null", "{}", commented.body); | ||
| 2889 | + | ||
| 2890 | + let send = until_rule_send_to(&app, &rule, "ada@izlek.sh", 0).await; | ||
| 2891 | + assert_eq!(send.recipient, "ada@izlek.sh"); | ||
| 2892 | + assert!( | ||
| 2893 | + app.store.mail_queue(50).await.unwrap().iter().all(|send| { | ||
| 2894 | + !(send.kind == SendKind::Rule | ||
| 2895 | + && send.rule_id.as_deref() == Some(rule.as_str()) | ||
| 2896 | + && send.recipient == "deniz@izlek.sh") | ||
| 2897 | + }), | ||
| 2898 | + "the commenter was mailed instead of the creator" | ||
| 2899 | + ); | ||
| 2900 | + let decisions = app.store.recent_mail_decisions(50).await.unwrap(); | ||
| 2901 | + assert!( | ||
| 2902 | + decisions.iter().any(|decision| { | ||
| 2903 | + decision.rule_id == rule | ||
| 2904 | + && matches!(decision.outcome, izlek_core::store::MailOutcome::Owed) | ||
| 2905 | + }), | ||
| 2906 | + "the decisions ledger has no matched decision for the rule" | ||
| 2907 | + ); | ||
| 2908 | + | ||
| 2909 | + // The admin is put on the task so the rewritten rule's assignees audience | ||
| 2910 | + // has someone to address once it fires on a rename. | ||
| 2911 | + let admin_id = person_id(&app, &admin_cookie, &task, "Ada Lovelace").await; | ||
| 2912 | + let assigned = app | ||
| 2913 | + .post( | ||
| 2914 | + path::<izlek_web::detail::Assign>(), | ||
| 2915 | + Some(&admin_cookie), | ||
| 2916 | + &[("task_id", &task), ("user_id", &admin_id)], | ||
| 2917 | + ) | ||
| 2918 | + .await; | ||
| 2919 | + assert_eq!(assigned.body, "null", "{}", assigned.body); | ||
| 2920 | + | ||
| 2921 | + // The rule is rewritten in place: same id, new trigger, subject and | ||
| 2922 | + // audience. | ||
| 2923 | + let updated = app | ||
| 2924 | + .post( | ||
| 2925 | + path::<izlek_web::rules::UpdateRule>(), | ||
| 2926 | + Some(&admin_cookie), | ||
| 2927 | + &[ | ||
| 2928 | + ("rule_id", &rule), | ||
| 2929 | + ("trigger", "retitled"), | ||
| 2930 | + ("column_id", ""), | ||
| 2931 | + ("subject", "Renamed"), | ||
| 2932 | + ("audience", "assignees"), | ||
| 2933 | + ], | ||
| 2934 | + ) | ||
| 2935 | + .await; | ||
| 2936 | + assert_eq!(updated.body, "null", "the rewrite was refused: {}", updated.body); | ||
| 2937 | + assert_eq!( | ||
| 2938 | + only_rule(&app, &admin_cookie).await, | ||
| 2939 | + rule, | ||
| 2940 | + "a new rule was made instead of the old one rewritten" | ||
| 2941 | + ); | ||
| 2942 | + | ||
| 2943 | + let seen = app | ||
| 2944 | + .post(path::<izlek_web::rules::CurrentRules>(), Some(&admin_cookie), &[]) | ||
| 2945 | + .await; | ||
| 2946 | + assert!(seen.body.contains(&format!("\"id\":\"{rule}\"")), "{}", seen.body); | ||
| 2947 | + assert!(seen.body.contains("\"enabled\":true"), "{}", seen.body); | ||
| 2948 | + assert!(seen.body.contains("\"trigger_kind\":\"retitled\""), "{}", seen.body); | ||
| 2949 | + assert!(seen.body.contains("\"subject\":\"Renamed\""), "{}", seen.body); | ||
| 2950 | + | ||
| 2951 | + // The second member renames the task; the rule now fires on retitle and | ||
| 2952 | + // addresses the assignee — the admin — not the member who renamed it. | ||
| 2953 | + let already = app | ||
| 2954 | + .store | ||
| 2955 | + .mail_queue(50) | ||
| 2956 | + .await | ||
| 2957 | + .unwrap() | ||
| 2958 | + .iter() | ||
| 2959 | + .filter(|send| { | ||
| 2960 | + send.kind == SendKind::Rule | ||
| 2961 | + && send.rule_id.as_deref() == Some(rule.as_str()) | ||
| 2962 | + && send.recipient == "ada@izlek.sh" | ||
| 2963 | + }) | ||
| 2964 | + .count(); | ||
| 2965 | + let renamed = app | ||
| 2966 | + .post( | ||
| 2967 | + path::<izlek_web::detail::SaveTask>(), | ||
| 2968 | + Some(&member), | ||
| 2969 | + &[("task_id", &task), ("title", "Ship the redesigned picker")], | ||
| 2970 | + ) | ||
| 2971 | + .await; | ||
| 2972 | + assert_eq!(renamed.body, "null", "{}", renamed.body); | ||
| 2973 | + | ||
| 2974 | + until_rule_send_to(&app, &rule, "ada@izlek.sh", already).await; | ||
| 2975 | + assert!( | ||
| 2976 | + app.store.mail_queue(50).await.unwrap().iter().all(|send| { | ||
| 2977 | + !(send.kind == SendKind::Rule | ||
| 2978 | + && send.rule_id.as_deref() == Some(rule.as_str()) | ||
| 2979 | + && send.recipient == "deniz@izlek.sh") | ||
| 2980 | + }), | ||
| 2981 | + "the renamer was mailed instead of being excluded as the actor" | ||
| 2982 | + ); | ||
| 2983 | + | ||
| 2984 | + let logs = until_logs_contains(&app, &admin_cookie, "\"subject\":\"Renamed\"").await; | ||
| 2985 | + assert!(logs.contains("\"recipient\":\"ada@izlek.sh\""), "{}", logs); | ||
| 2986 | +} | ||