wacore 0.6.0

Core WhatsApp protocol implementation without runtime dependencies
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
//! Group notification stanza types.
//!
//! Parses `<notification type="w:gp2">` stanzas for group updates.
//!
//! Reference: WhatsApp Web `WAWebHandleGroupNotification` (Ri7Gf1BxhsX.js:12556-12962)
//! Tag names: `WAWebHandleGroupNotificationConst.GROUP_NOTIFICATION_TAG` (hE1cdfp8vOc.js:2460-2506)
//!
//! Key behaviors:
//! - A single notification can contain MULTIPLE child actions (mapChildren pattern)
//! - Root `participant` attribute identifies the admin/author who triggered the change
//! - Participant lists are nested `<participant jid="..." />` children

use crate::WireEnum;
use serde::Serialize;
use wacore_binary::Jid;
use wacore_binary::{Node, NodeRef};

/// How a membership request was initiated.
///
/// Maps to `WAWebRequestMethodType` in WhatsApp Web JS.
#[derive(Debug, Clone, PartialEq, Eq, WireEnum)]
pub enum MembershipRequestMethod {
    #[wire_default]
    #[wire = "invite_link"]
    InviteLink,
    #[wire = "linked_group_join"]
    LinkedGroupJoin,
    #[wire = "non_admin_add"]
    NonAdminAdd,
}

/// Parsed group notification containing one or more actions.
#[derive(Debug, Clone)]
pub struct GroupNotification {
    /// Group JID (from `from` attribute)
    pub group_jid: Jid,
    /// Admin/user who triggered the notification (from `participant` attribute)
    pub participant: Option<Jid>,
    /// Phone number JID of the participant (from `participant_pn` attribute, for LID groups)
    pub participant_pn: Option<Jid>,
    /// Timestamp (from `t` attribute, unix seconds)
    pub timestamp: u64,
    /// Whether the group uses LID addressing mode (from `addressing_mode="lid"`)
    pub is_lid_addressing_mode: bool,
    /// One or more actions in this notification
    pub actions: Vec<GroupNotificationAction>,
}

/// Participant info extracted from `<participant>` child elements.
///
/// Wire format:
/// ```xml
/// <participant jid="1234567890@s.whatsapp.net" phone_number="1234567890@s.whatsapp.net"/>
/// ```
#[derive(Debug, Clone, Serialize)]
pub struct GroupParticipantInfo {
    pub jid: Jid,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub phone_number: Option<Jid>,
}

/// All possible group notification action types.
///
/// Maps 1:1 to `GROUP_NOTIFICATION_TAG` child element tags from WhatsApp Web.
///
/// The `#[wire = "..."]` attribute is the SINGLE source of truth for each
/// variant's wire tag: the JSON discriminator (via the auto-derived
/// `Serialize`), the parser dispatch (via the auto-generated sibling
/// `GroupNotificationActionTag` enum), and `wire_tag()` / `tag_name()` all
/// read from the same table.
#[derive(Debug, Clone, WireEnum)]
#[wire(tag = "type")]
pub enum GroupNotificationAction {
    // -- Participant management --
    /// `<add>` — Members added to group
    #[wire = "add"]
    Add {
        participants: Vec<GroupParticipantInfo>,
        reason: Option<String>,
    },
    /// `<remove>` — Members removed from group
    #[wire = "remove"]
    Remove {
        participants: Vec<GroupParticipantInfo>,
        reason: Option<String>,
    },
    /// `<promote>` — Members promoted to admin
    #[wire = "promote"]
    Promote {
        participants: Vec<GroupParticipantInfo>,
    },
    /// `<demote>` — Members demoted from admin
    #[wire = "demote"]
    Demote {
        participants: Vec<GroupParticipantInfo>,
    },
    /// `<modify>` — Member changed phone number
    #[wire = "modify"]
    Modify {
        participants: Vec<GroupParticipantInfo>,
    },

    // -- Metadata --
    /// `<subject subject="..." s_o="..." s_t="..."/>` — Group name changed
    #[wire = "subject"]
    Subject {
        subject: String,
        subject_owner: Option<Jid>,
        subject_time: Option<u64>,
    },
    /// `<description id="..."><body>text</body></description>` or `<description id="..."><delete/></description>`
    #[wire = "description"]
    Description {
        id: String,
        /// `Some(text)` = added/updated, `None` = deleted
        description: Option<String>,
    },

    // -- Settings --
    /// `<locked threshold="..."/>` — Only admins can edit group info
    #[wire = "locked"]
    Locked { threshold: Option<String> },
    /// `<unlocked/>` — All members can edit group info
    #[wire = "unlocked"]
    Unlocked,
    /// `<announcement/>` — Only admins can send messages
    #[wire = "announcement"]
    Announce,
    /// `<not_announcement/>` — All members can send messages
    #[wire = "not_announcement"]
    NotAnnounce,
    /// `<ephemeral expiration="..." trigger="..."/>` — and the alias
    /// `<not_ephemeral/>` which parses into `Ephemeral { expiration: 0 }`,
    /// matching WA Web's collapsing of the two tags into one action type.
    #[wire = "ephemeral"]
    #[wire_alias = "not_ephemeral"]
    Ephemeral {
        expiration: u32,
        trigger: Option<u32>,
    },
    /// `<membership_approval_mode><group_join state="on|off"/></membership_approval_mode>`
    #[wire = "membership_approval_mode"]
    MembershipApprovalMode { enabled: bool },
    /// `<membership_approval_request request_method="..." parent_group_jid="..."/>`
    /// A user requested to join. Requester is on parent [`GroupNotification::participant`].
    #[wire = "membership_approval_request"]
    MembershipApprovalRequest {
        request_method: MembershipRequestMethod,
        parent_group_jid: Option<Jid>,
    },
    /// `<created_membership_requests request_method="..." parent_group_jid="...">` —
    /// admin-side notification: new join requests appeared.
    #[wire = "created_membership_requests"]
    CreatedMembershipRequests {
        request_method: MembershipRequestMethod,
        parent_group_jid: Option<Jid>,
        /// `<requested_user>` children (not `<participant>`).
        requests: Vec<GroupParticipantInfo>,
    },
    /// `<revoked_membership_requests>` — requests rejected by admin or cancelled by requester.
    #[wire = "revoked_membership_requests"]
    RevokedMembershipRequests { participants: Vec<Jid> },
    /// `<member_add_mode>admin_add|all_member_add</member_add_mode>`
    #[wire = "member_add_mode"]
    MemberAddMode { mode: String },
    /// `<no_frequently_forwarded/>` — Forwarding restricted
    #[wire = "no_frequently_forwarded"]
    NoFrequentlyForwarded,
    /// `<frequently_forwarded_ok/>` — Forwarding allowed
    #[wire = "frequently_forwarded_ok"]
    FrequentlyForwardedOk,

    // -- Invites --
    /// `<invite code="..."/>` — Joined via invite link
    #[wire = "invite"]
    Invite { code: String },
    /// `<revoke>` — Invite link revoked
    #[wire = "revoke"]
    RevokeInvite,
    /// `<growth_locked expiration="..." type="..."/>` — Invite links unavailable
    #[wire = "growth_locked"]
    GrowthLocked { expiration: u32, lock_type: String },
    /// `<growth_unlocked/>` — Invite links available again
    #[wire = "growth_unlocked"]
    GrowthUnlocked,

    // -- Group lifecycle --
    /// `<create>` — Group created (complex structure, raw node preserved)
    #[wire = "create"]
    Create {
        #[wire(skip)]
        raw: Node,
    },
    /// `<delete>` — Group deleted
    #[wire = "delete"]
    Delete { reason: Option<String> },

    // -- Community linking --
    /// `<link link_type="...">` — Subgroup linked
    #[wire = "link"]
    Link {
        link_type: String,
        #[wire(skip)]
        raw: Node,
    },
    /// `<unlink unlink_type="..." unlink_reason="...">` — Subgroup unlinked
    #[wire = "unlink"]
    Unlink {
        unlink_type: String,
        unlink_reason: Option<String>,
        #[wire(skip)]
        raw: Node,
    },

    // -- Catch-all --
    /// Unknown child tag — preserved for forward compatibility. The `tag`
    /// field is what `wire_tag()` returns, so roundtrips stay intact.
    #[wire_fallback]
    Unknown { tag: String },
}

impl GroupNotification {
    /// Parse from a `NodeRef`.
    ///
    /// Most fields are parsed zero-copy. Only `Create`/`Link`/`Unlink` actions
    /// call `.to_owned()` on their specific child node (structurally required to store `raw: Node`).
    pub fn try_from_node_ref(node: &NodeRef<'_>) -> Option<Self> {
        let mut attrs = node.attrs();
        let group_jid = attrs.optional_jid("from")?;
        let participant = attrs.optional_jid("participant");
        let participant_pn = attrs.optional_jid("participant_pn");
        let timestamp = attrs.optional_u64("t").unwrap_or(0);
        let is_lid_addressing_mode = node
            .get_attr("addressing_mode")
            .map(|v| v.as_str())
            .is_some_and(|s| s == "lid");

        let actions = node
            .children()
            .map(|children| children.iter().filter_map(parse_action).collect())
            .unwrap_or_default();

        Some(Self {
            group_jid,
            participant,
            participant_pn,
            timestamp,
            is_lid_addressing_mode,
            actions,
        })
    }
}

/// Parse a single child element into a GroupNotificationAction.
///
/// Dispatches via [`GroupNotificationActionTag`] (auto-generated by
/// `#[derive(WireEnum)]`) — no wire-tag string literal appears in this
/// function. If the `#[wire = "..."]` attribute on a variant changes, both
/// the serializer and this dispatcher track it automatically.
///
/// Only `Create`/`Link`/`Unlink` call `.to_owned()` because those variants store `raw: Node`.
fn parse_action(node: &NodeRef<'_>) -> Option<GroupNotificationAction> {
    use GroupNotificationActionTag as T;
    use wacore_binary::NodeContentRef;

    // WA Web drops this child entirely; mirror that behavior.
    if node.tag.as_ref() == "missing_participant_identification" {
        return None;
    }

    let tag = T::from(node.tag.as_ref());

    let action = match tag {
        T::Add => GroupNotificationAction::Add {
            participants: parse_participants(node),
            reason: node
                .attrs()
                .optional_string("reason")
                .map(|s| s.into_owned()),
        },
        T::Remove => GroupNotificationAction::Remove {
            participants: parse_participants(node),
            reason: node
                .attrs()
                .optional_string("reason")
                .map(|s| s.into_owned()),
        },
        T::Promote => GroupNotificationAction::Promote {
            participants: parse_participants(node),
        },
        T::Demote => GroupNotificationAction::Demote {
            participants: parse_participants(node),
        },
        T::Modify => GroupNotificationAction::Modify {
            participants: parse_participants(node),
        },
        T::Subject => GroupNotificationAction::Subject {
            subject: node
                .attrs()
                .optional_string("subject")
                .as_deref()
                .unwrap_or_default()
                .to_string(),
            subject_owner: node.attrs().optional_jid("s_o"),
            subject_time: node.attrs().optional_u64("s_t"),
        },
        T::Description => {
            let id = node
                .attrs()
                .optional_string("id")
                .as_deref()
                .unwrap_or_default()
                .to_string();
            let description = if node.get_optional_child("delete").is_some() {
                None
            } else {
                node.get_optional_child("body")
                    .and_then(|body| body.content_as_string())
                    .map(|s| s.to_string())
            };
            GroupNotificationAction::Description { id, description }
        }
        T::Locked => GroupNotificationAction::Locked {
            threshold: node
                .attrs()
                .optional_string("threshold")
                .map(|s| s.into_owned()),
        },
        T::Unlocked => GroupNotificationAction::Unlocked,
        T::Announce => GroupNotificationAction::Announce,
        T::NotAnnounce => GroupNotificationAction::NotAnnounce,
        // Both `<ephemeral .../>` and the alias `<not_ephemeral/>` land here —
        // the alias carries no `expiration`/`trigger` attrs, so the fallbacks
        // below produce `Ephemeral { expiration: 0, trigger: None }`, matching
        // WA Web's collapse of the two wire tags into one action.
        T::Ephemeral => GroupNotificationAction::Ephemeral {
            expiration: node.attrs().optional_u64("expiration").unwrap_or(0) as u32,
            trigger: node.attrs().optional_u64("trigger").map(|t| t as u32),
        },
        T::MembershipApprovalMode => {
            let enabled = node
                .get_optional_child("group_join")
                .and_then(|gj| gj.attrs().optional_string("state"))
                .is_some_and(|s| s == "on");
            GroupNotificationAction::MembershipApprovalMode { enabled }
        }
        T::MembershipApprovalRequest => {
            let request_method = parse_request_method(node);
            let parent_group_jid = node.attrs().optional_jid("parent_group_jid");
            GroupNotificationAction::MembershipApprovalRequest {
                request_method,
                parent_group_jid,
            }
        }
        T::CreatedMembershipRequests => {
            let request_method = parse_request_method(node);
            let parent_group_jid = node.attrs().optional_jid("parent_group_jid");
            let requests = parse_requested_users(node);
            GroupNotificationAction::CreatedMembershipRequests {
                request_method,
                parent_group_jid,
                requests,
            }
        }
        T::RevokedMembershipRequests => {
            let participants = parse_participant_jids(node);
            GroupNotificationAction::RevokedMembershipRequests { participants }
        }
        T::MemberAddMode => {
            let mode = match node.content.as_deref() {
                Some(NodeContentRef::String(s)) => s.to_string(),
                Some(NodeContentRef::Bytes(b)) => String::from_utf8_lossy(b.as_ref()).into_owned(),
                _ => String::new(),
            };
            GroupNotificationAction::MemberAddMode { mode }
        }
        T::NoFrequentlyForwarded => GroupNotificationAction::NoFrequentlyForwarded,
        T::FrequentlyForwardedOk => GroupNotificationAction::FrequentlyForwardedOk,
        T::Invite => GroupNotificationAction::Invite {
            code: node
                .attrs()
                .optional_string("code")
                .as_deref()
                .unwrap_or_default()
                .to_string(),
        },
        T::RevokeInvite => GroupNotificationAction::RevokeInvite,
        T::GrowthLocked => GroupNotificationAction::GrowthLocked {
            expiration: node.attrs().optional_u64("expiration").unwrap_or(0) as u32,
            lock_type: node
                .attrs()
                .optional_string("type")
                .as_deref()
                .unwrap_or_default()
                .to_string(),
        },
        T::GrowthUnlocked => GroupNotificationAction::GrowthUnlocked,
        T::Create => GroupNotificationAction::Create {
            raw: node.to_owned(),
        },
        T::Delete => GroupNotificationAction::Delete {
            reason: node
                .attrs()
                .optional_string("reason")
                .map(|s| s.into_owned()),
        },
        T::Link => GroupNotificationAction::Link {
            link_type: node
                .attrs()
                .optional_string("link_type")
                .as_deref()
                .unwrap_or_default()
                .to_string(),
            raw: node.to_owned(),
        },
        T::Unlink => GroupNotificationAction::Unlink {
            unlink_type: node
                .attrs()
                .optional_string("unlink_type")
                .as_deref()
                .unwrap_or_default()
                .to_string(),
            unlink_reason: node
                .attrs()
                .optional_string("unlink_reason")
                .map(|s| s.into_owned()),
            raw: node.to_owned(),
        },
        T::Unknown(_) => GroupNotificationAction::Unknown {
            tag: node.tag.to_string(),
        },
    };
    Some(action)
}

fn parse_participants(node: &NodeRef<'_>) -> Vec<GroupParticipantInfo> {
    node.children()
        .map(|children| {
            children
                .iter()
                .filter(|c| c.tag == "participant")
                .filter_map(|c| {
                    let jid = c.attrs().optional_jid("jid")?;
                    let phone_number = c.attrs().optional_jid("phone_number");
                    Some(GroupParticipantInfo { jid, phone_number })
                })
                .collect()
        })
        .unwrap_or_default()
}

/// Parses `<requested_user>` children from `<created_membership_requests>`.
fn parse_requested_users(node: &NodeRef<'_>) -> Vec<GroupParticipantInfo> {
    node.children()
        .map(|children| {
            children
                .iter()
                .filter(|c| c.tag == "requested_user")
                .filter_map(|c| {
                    let jid = c.attrs().optional_jid("jid")?;
                    let phone_number = c.attrs().optional_jid("phone_number");
                    Some(GroupParticipantInfo { jid, phone_number })
                })
                .collect()
        })
        .unwrap_or_default()
}

/// Parses `<participant jid="..."/>` children into plain JIDs.
fn parse_participant_jids(node: &NodeRef<'_>) -> Vec<Jid> {
    node.children()
        .map(|children| {
            children
                .iter()
                .filter(|c| c.tag == "participant")
                .filter_map(|c| c.attrs().optional_jid("jid"))
                .collect()
        })
        .unwrap_or_default()
}

/// Maps the `request_method` attribute to [`MembershipRequestMethod`].
/// Defaults to `InviteLink` when absent or unknown — matches WA Web's fallback.
/// Wire strings come from the derived `TryFrom<&str>` impl; this function has
/// no hard-coded tags of its own.
fn parse_request_method(node: &NodeRef<'_>) -> MembershipRequestMethod {
    node.attrs()
        .optional_string("request_method")
        .as_deref()
        .and_then(|s| MembershipRequestMethod::try_from(s).ok())
        .unwrap_or_default()
}

#[cfg(test)]
mod tests {
    use super::*;
    use wacore_binary::Jid;
    use wacore_binary::builder::NodeBuilder;

    fn group_jid() -> Jid {
        "120363012345678901@g.us".parse().unwrap()
    }

    fn user_jid() -> Jid {
        "5511999999999@s.whatsapp.net".parse().unwrap()
    }

    fn admin_jid() -> Jid {
        "5511888888888@s.whatsapp.net".parse().unwrap()
    }

    fn make_notification(children: Vec<Node>) -> Node {
        NodeBuilder::new("notification")
            .attr("type", "w:gp2")
            .attr("from", group_jid())
            .attr("participant", admin_jid())
            .attr("t", "1704067200")
            .children(children)
            .build()
    }

    #[test]
    fn test_parse_add_notification() {
        let node = make_notification(vec![
            NodeBuilder::new("add")
                .children(vec![
                    NodeBuilder::new("participant")
                        .attr("jid", user_jid())
                        .build(),
                ])
                .build(),
        ]);

        let notif = GroupNotification::try_from_node_ref(&node.as_node_ref()).unwrap();
        assert_eq!(notif.group_jid, group_jid());
        assert_eq!(notif.participant, Some(admin_jid()));
        assert_eq!(notif.timestamp, 1704067200);
        assert_eq!(notif.actions.len(), 1);

        match &notif.actions[0] {
            GroupNotificationAction::Add {
                participants,
                reason,
            } => {
                assert_eq!(participants.len(), 1);
                assert_eq!(participants[0].jid, user_jid());
                assert!(reason.is_none());
            }
            other => panic!("expected Add, got {:?}", other),
        }
    }

    #[test]
    fn test_parse_subject_notification() {
        let node = make_notification(vec![
            NodeBuilder::new("subject")
                .attr("subject", "New Group Name")
                .attr("s_o", admin_jid())
                .attr("s_t", "1704067200")
                .build(),
        ]);

        let notif = GroupNotification::try_from_node_ref(&node.as_node_ref()).unwrap();
        assert_eq!(notif.actions.len(), 1);

        match &notif.actions[0] {
            GroupNotificationAction::Subject {
                subject,
                subject_owner,
                subject_time,
            } => {
                assert_eq!(subject, "New Group Name");
                assert_eq!(*subject_owner, Some(admin_jid()));
                assert_eq!(*subject_time, Some(1704067200));
            }
            other => panic!("expected Subject, got {:?}", other),
        }
    }

    #[test]
    fn test_parse_description_add() {
        let node = make_notification(vec![
            NodeBuilder::new("description")
                .attr("id", "desc123")
                .children(vec![
                    NodeBuilder::new("body")
                        .string_content("Group description text")
                        .build(),
                ])
                .build(),
        ]);

        let notif = GroupNotification::try_from_node_ref(&node.as_node_ref()).unwrap();
        match &notif.actions[0] {
            GroupNotificationAction::Description { id, description } => {
                assert_eq!(id, "desc123");
                assert_eq!(description.as_deref(), Some("Group description text"));
            }
            other => panic!("expected Description, got {:?}", other),
        }
    }

    #[test]
    fn test_parse_description_delete() {
        let node = make_notification(vec![
            NodeBuilder::new("description")
                .attr("id", "desc123")
                .children(vec![NodeBuilder::new("delete").build()])
                .build(),
        ]);

        let notif = GroupNotification::try_from_node_ref(&node.as_node_ref()).unwrap();
        match &notif.actions[0] {
            GroupNotificationAction::Description { id, description } => {
                assert_eq!(id, "desc123");
                assert!(description.is_none());
            }
            other => panic!("expected Description, got {:?}", other),
        }
    }

    #[test]
    fn test_parse_settings_notifications() {
        // Test multiple actions in one notification
        let node = make_notification(vec![
            NodeBuilder::new("locked").attr("threshold", "100").build(),
            NodeBuilder::new("announcement").build(),
            NodeBuilder::new("ephemeral")
                .attr("expiration", "604800")
                .build(),
        ]);

        let notif = GroupNotification::try_from_node_ref(&node.as_node_ref()).unwrap();
        assert_eq!(notif.actions.len(), 3);

        match &notif.actions[0] {
            GroupNotificationAction::Locked { threshold } => {
                assert_eq!(threshold.as_deref(), Some("100"));
            }
            other => panic!("expected Locked, got {:?}", other),
        }
        assert!(matches!(
            notif.actions[1],
            GroupNotificationAction::Announce
        ));
        match &notif.actions[2] {
            GroupNotificationAction::Ephemeral {
                expiration,
                trigger,
            } => {
                assert_eq!(*expiration, 604800);
                assert!(trigger.is_none());
            }
            other => panic!("expected Ephemeral, got {:?}", other),
        }
    }

    #[test]
    fn test_parse_not_ephemeral() {
        let node = make_notification(vec![NodeBuilder::new("not_ephemeral").build()]);

        let notif = GroupNotification::try_from_node_ref(&node.as_node_ref()).unwrap();
        match &notif.actions[0] {
            GroupNotificationAction::Ephemeral {
                expiration,
                trigger,
            } => {
                assert_eq!(*expiration, 0);
                assert!(trigger.is_none());
            }
            other => panic!("expected Ephemeral, got {:?}", other),
        }
    }

    #[test]
    fn test_parse_membership_approval_mode() {
        let node = make_notification(vec![
            NodeBuilder::new("membership_approval_mode")
                .children(vec![
                    NodeBuilder::new("group_join").attr("state", "on").build(),
                ])
                .build(),
        ]);

        let notif = GroupNotification::try_from_node_ref(&node.as_node_ref()).unwrap();
        match &notif.actions[0] {
            GroupNotificationAction::MembershipApprovalMode { enabled } => {
                assert!(*enabled);
            }
            other => panic!("expected MembershipApprovalMode, got {:?}", other),
        }
    }

    #[test]
    fn test_parse_membership_approval_request() {
        // User requested to join — flat node with attrs only, actor is the requester.
        let node = make_notification(vec![
            NodeBuilder::new("membership_approval_request")
                .attr("request_method", "invite_link")
                .build(),
        ]);

        let notif = GroupNotification::try_from_node_ref(&node.as_node_ref()).unwrap();
        assert_eq!(notif.participant, Some(admin_jid()));
        match &notif.actions[0] {
            GroupNotificationAction::MembershipApprovalRequest {
                request_method,
                parent_group_jid,
            } => {
                assert_eq!(*request_method, MembershipRequestMethod::InviteLink);
                assert!(parent_group_jid.is_none());
            }
            other => panic!("expected MembershipApprovalRequest, got {:?}", other),
        }
    }

    #[test]
    fn test_parse_created_membership_requests() {
        // Admin-side: new requests appeared — uses <requested_user> children.
        let node = make_notification(vec![
            NodeBuilder::new("created_membership_requests")
                .attr("request_method", "non_admin_add")
                .children(vec![
                    NodeBuilder::new("requested_user")
                        .attr("jid", user_jid())
                        .build(),
                ])
                .build(),
        ]);

        let notif = GroupNotification::try_from_node_ref(&node.as_node_ref()).unwrap();
        assert_eq!(notif.participant, Some(admin_jid()));
        match &notif.actions[0] {
            GroupNotificationAction::CreatedMembershipRequests {
                request_method,
                parent_group_jid,
                requests,
            } => {
                assert_eq!(*request_method, MembershipRequestMethod::NonAdminAdd);
                assert!(parent_group_jid.is_none());
                assert_eq!(requests.len(), 1);
                assert_eq!(requests[0].jid, user_jid());
            }
            other => panic!("expected CreatedMembershipRequests, got {:?}", other),
        }
    }

    #[test]
    fn test_parse_revoked_membership_requests() {
        // Requests rejected by admin — uses <participant jid="..."/> children.
        let node = make_notification(vec![
            NodeBuilder::new("revoked_membership_requests")
                .children(vec![
                    NodeBuilder::new("participant")
                        .attr("jid", user_jid())
                        .build(),
                ])
                .build(),
        ]);

        let notif = GroupNotification::try_from_node_ref(&node.as_node_ref()).unwrap();
        assert_eq!(notif.participant, Some(admin_jid()));
        match &notif.actions[0] {
            GroupNotificationAction::RevokedMembershipRequests { participants } => {
                assert_eq!(participants.len(), 1);
                assert_eq!(participants[0], user_jid());
            }
            other => panic!("expected RevokedMembershipRequests, got {:?}", other),
        }
    }

    #[test]
    fn test_parse_membership_approval_request_default_method() {
        // No request_method attr → defaults to InviteLink (matches WA Web fallback).
        let node = make_notification(vec![
            NodeBuilder::new("membership_approval_request").build(),
        ]);

        let notif = GroupNotification::try_from_node_ref(&node.as_node_ref()).unwrap();
        assert_eq!(notif.participant, Some(admin_jid()));
        match &notif.actions[0] {
            GroupNotificationAction::MembershipApprovalRequest {
                request_method,
                parent_group_jid,
            } => {
                assert_eq!(*request_method, MembershipRequestMethod::InviteLink);
                assert!(parent_group_jid.is_none());
            }
            other => panic!("expected MembershipApprovalRequest, got {:?}", other),
        }
    }

    #[test]
    fn test_parse_membership_request_with_parent_group_jid() {
        // Community-linked join — both variants carry parent_group_jid.
        let parent_jid: Jid = "999999999999999999@g.us".parse().unwrap();

        let approval_node = make_notification(vec![
            NodeBuilder::new("membership_approval_request")
                .attr("request_method", "linked_group_join")
                .attr("parent_group_jid", parent_jid.clone())
                .build(),
        ]);
        let notif = GroupNotification::try_from_node_ref(&approval_node.as_node_ref()).unwrap();
        match &notif.actions[0] {
            GroupNotificationAction::MembershipApprovalRequest {
                request_method,
                parent_group_jid,
            } => {
                assert_eq!(*request_method, MembershipRequestMethod::LinkedGroupJoin);
                assert_eq!(*parent_group_jid, Some(parent_jid.clone()));
            }
            other => panic!("expected MembershipApprovalRequest, got {:?}", other),
        }

        let created_node = make_notification(vec![
            NodeBuilder::new("created_membership_requests")
                .attr("request_method", "linked_group_join")
                .attr("parent_group_jid", parent_jid.clone())
                .children(vec![
                    NodeBuilder::new("requested_user")
                        .attr("jid", user_jid())
                        .build(),
                ])
                .build(),
        ]);
        let notif2 = GroupNotification::try_from_node_ref(&created_node.as_node_ref()).unwrap();
        match &notif2.actions[0] {
            GroupNotificationAction::CreatedMembershipRequests {
                request_method,
                parent_group_jid,
                requests,
            } => {
                assert_eq!(*request_method, MembershipRequestMethod::LinkedGroupJoin);
                assert_eq!(*parent_group_jid, Some(parent_jid));
                assert_eq!(requests.len(), 1);
                assert_eq!(requests[0].jid, user_jid());
            }
            other => panic!("expected CreatedMembershipRequests, got {:?}", other),
        }
    }

    #[test]
    fn test_parse_unknown_tag() {
        let node = make_notification(vec![NodeBuilder::new("some_future_feature").build()]);

        let notif = GroupNotification::try_from_node_ref(&node.as_node_ref()).unwrap();
        match &notif.actions[0] {
            GroupNotificationAction::Unknown { tag } => {
                assert_eq!(tag, "some_future_feature");
            }
            other => panic!("expected Unknown, got {:?}", other),
        }
    }

    #[test]
    fn test_missing_from_returns_none() {
        let node = NodeBuilder::new("notification")
            .attr("type", "w:gp2")
            .attr("t", "1704067200")
            .build();

        assert!(GroupNotification::try_from_node_ref(&node.as_node_ref()).is_none());
    }

    /// Every variant serializes its JSON `"type"` discriminator using the
    /// exact wire tag the parser dispatches on. This is the regression guard
    /// for the PascalCase discriminator leak that used to ship
    /// `{"type":"Demote", ...}` instead of `{"type":"demote", ...}`.
    #[test]
    fn serialize_discriminator_matches_wire_tag() {
        let dummy_node = NodeBuilder::new("placeholder").build();
        let samples: Vec<GroupNotificationAction> = vec![
            GroupNotificationAction::Add {
                participants: vec![],
                reason: None,
            },
            GroupNotificationAction::Remove {
                participants: vec![],
                reason: Some("r".into()),
            },
            GroupNotificationAction::Promote {
                participants: vec![],
            },
            GroupNotificationAction::Demote {
                participants: vec![],
            },
            GroupNotificationAction::Modify {
                participants: vec![],
            },
            GroupNotificationAction::Subject {
                subject: "s".into(),
                subject_owner: None,
                subject_time: None,
            },
            GroupNotificationAction::Description {
                id: "i".into(),
                description: None,
            },
            GroupNotificationAction::Locked { threshold: None },
            GroupNotificationAction::Unlocked,
            GroupNotificationAction::Announce,
            GroupNotificationAction::NotAnnounce,
            GroupNotificationAction::Ephemeral {
                expiration: 0,
                trigger: None,
            },
            GroupNotificationAction::MembershipApprovalMode { enabled: true },
            GroupNotificationAction::MembershipApprovalRequest {
                request_method: MembershipRequestMethod::InviteLink,
                parent_group_jid: None,
            },
            GroupNotificationAction::CreatedMembershipRequests {
                request_method: MembershipRequestMethod::InviteLink,
                parent_group_jid: None,
                requests: vec![],
            },
            GroupNotificationAction::RevokedMembershipRequests {
                participants: vec![],
            },
            GroupNotificationAction::MemberAddMode { mode: "x".into() },
            GroupNotificationAction::NoFrequentlyForwarded,
            GroupNotificationAction::FrequentlyForwardedOk,
            GroupNotificationAction::Invite { code: "c".into() },
            GroupNotificationAction::RevokeInvite,
            GroupNotificationAction::GrowthLocked {
                expiration: 0,
                lock_type: "x".into(),
            },
            GroupNotificationAction::GrowthUnlocked,
            GroupNotificationAction::Create {
                raw: dummy_node.clone(),
            },
            GroupNotificationAction::Delete { reason: None },
            GroupNotificationAction::Link {
                link_type: "x".into(),
                raw: dummy_node.clone(),
            },
            GroupNotificationAction::Unlink {
                unlink_type: "x".into(),
                unlink_reason: None,
                raw: dummy_node,
            },
            GroupNotificationAction::Unknown {
                tag: "future_tag".into(),
            },
        ];

        for action in &samples {
            let value = serde_json::to_value(action).expect("serialize");
            let ty = value
                .get("type")
                .and_then(|v| v.as_str())
                .unwrap_or_else(|| panic!("missing type in {value}"));
            assert_eq!(
                ty,
                action.tag_name(),
                "serialized discriminator diverged from wire tag for {action:?}"
            );
        }
    }

    /// Lowercase wire strings round-trip through the parser, matching the
    /// exact JSON discriminators we now emit. If someone renames a variant
    /// and forgets to keep `tag_name()` aligned with the parser's dispatch
    /// table, this test fails.
    #[test]
    fn wire_tags_round_trip_through_parser() {
        type Check = fn(&GroupNotificationAction) -> bool;
        let cases: &[(&str, Check)] = &[
            ("add", |a| matches!(a, GroupNotificationAction::Add { .. })),
            ("demote", |a| {
                matches!(a, GroupNotificationAction::Demote { .. })
            }),
            ("promote", |a| {
                matches!(a, GroupNotificationAction::Promote { .. })
            }),
            ("revoke", |a| {
                matches!(a, GroupNotificationAction::RevokeInvite)
            }),
            ("not_announcement", |a| {
                matches!(a, GroupNotificationAction::NotAnnounce)
            }),
            ("announcement", |a| {
                matches!(a, GroupNotificationAction::Announce)
            }),
        ];

        for (tag, check) in cases {
            let node = make_notification(vec![NodeBuilder::new(tag).build()]);
            let notif = GroupNotification::try_from_node_ref(&node.as_node_ref())
                .unwrap_or_else(|| panic!("parse failed for <{tag}>"));
            let action = &notif.actions[0];
            assert!(
                check(action),
                "tag <{tag}> did not produce expected variant (got {action:?})"
            );
            assert_eq!(action.tag_name(), *tag);
        }
    }
}