Skip to content

refactor: use snapshot tests #475

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,341 changes: 695 additions & 646 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ serde_cbor = "0.11.2"
serde_json = "1.0.107"
reqwest = "0.12.2"
hyper = "1.4.1"
insta = { version = "1.42.0", features = ["json"] }

[build-dependencies]
tower = "0.4.13"
Expand Down
290 changes: 210 additions & 80 deletions src/eventsub/automod/message/hold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,8 @@ pub struct AutomodMessageHoldV1Payload {
#[cfg(test)]
#[test]
fn parse_payload_v1() {
use crate::eventsub::{Event, Message};

let payload = r##"
crate::eventsub::assert_eventsub_snapshot!(
r##"
{
"subscription": {
"id": "e523fda0-01b6-4b0e-9024-a5a80c5ad680",
Expand Down Expand Up @@ -115,22 +114,55 @@ fn parse_payload_v1() {
"held_at": "2024-10-19T20:11:16.799750627Z"
}
}
"##;

let val = Event::parse(payload).unwrap();
crate::tests::roundtrip(&val);

let Event::AutomodMessageHoldV1(val) = val else {
panic!("invalid event type");
};
let Message::Notification(notif) = val.message else {
panic!("invalid message type");
};

assert_eq!(notif.broadcaster_user_id.as_str(), "129546453");
assert_eq!(notif.category, AutomodCategory::Sexwords);
assert_eq!(notif.level, 4);
assert_eq!(notif.message.fragments.len(), 1);
"##,
@r#"
AutomodMessageHoldV1(
Payload {
subscription: EventSubscriptionInformation {
id: "e523fda0-01b6-4b0e-9024-a5a80c5ad680",
status: Enabled,
cost: 0,
condition: AutomodMessageHoldV1 {
broadcaster_user_id: "129546453",
moderator_user_id: "129546453",
},
created_at: "2024-10-19T20:11:13.917500523Z",
transport: Websocket(
WebsocketTransportResponse {
session_id: "AgoQRniKAQ1ITYSESh4ku8anEBIGY2VsbC1j",
connected_at: None,
disconnected_at: None,
},
),
type_: AutomodMessageHold,
version: "1",
},
message: Notification(
AutomodMessageHoldV1Payload {
broadcaster_user_id: "129546453",
broadcaster_user_login: "nerixyz",
broadcaster_user_name: "nerixyz",
user_id: "489584266",
user_login: "uint128",
user_name: "uint128",
message_id: "332e99ac-e19c-4368-a15b-793e8266b51f",
message: AutomodMessage {
text: "boobs",
fragments: [
Text {
text: "boobs",
},
],
},
category: Sexwords,
level: 4,
held_at: "2024-10-19T20:11:16.799750627Z",
},
),
},
)
"#
);
}

/// [`automod.message.hold`](https://dev.twitch.tv/docs/eventsub/eventsub-subscription-types/#automodmessagehold-v2): a message was caught by automod for review.
Expand Down Expand Up @@ -200,9 +232,8 @@ pub struct AutomodMessageHoldV2Payload {

#[test]
fn parse_payload_v2_automod() {
use crate::eventsub::{Event, Message};

let payload = r##"
crate::eventsub::assert_eventsub_snapshot!(
r##"
{
"subscription": {
"id": "85c8dcb0-7af4-4581-b684-32087d386384",
Expand Down Expand Up @@ -260,40 +291,74 @@ fn parse_payload_v2_automod() {
"held_at": "2024-11-18T16:59:46.323937273Z"
}
}
"##;

let val = Event::parse(payload).unwrap();
crate::tests::roundtrip(&val);

let Event::AutomodMessageHoldV2(val) = val else {
panic!("invalid event type");
};
let Message::Notification(notif) = val.message else {
panic!("invalid message type");
};

assert_eq!(notif.broadcaster_user_id.as_str(), "129546453");
assert_eq!(notif.message.fragments.len(), 2);

let AutomodHeldReason::Automod(automod) = &notif.reason else {
panic!("invalid held reason");
};
assert_eq!(automod.category, AutomodCategory::Swearing);
assert_eq!(automod.level, 4);
assert_eq!(
automod.boundaries,
&[AutomodMessageBoundary {
start_pos: 2,
end_pos: 4
}]
"##,
@r#"
AutomodMessageHoldV2(
Payload {
subscription: EventSubscriptionInformation {
id: "85c8dcb0-7af4-4581-b684-32087d386384",
status: Enabled,
cost: 0,
condition: AutomodMessageHoldV2 {
broadcaster_user_id: "129546453",
moderator_user_id: "129546453",
},
created_at: "2024-11-18T16:36:08.691979783Z",
transport: Websocket(
WebsocketTransportResponse {
session_id: "AgoQREw4FYBWQ5quz4J-S4VYkRIGY2VsbC1j",
connected_at: None,
disconnected_at: None,
},
),
type_: AutomodMessageHold,
version: "2",
},
message: Notification(
AutomodMessageHoldV2Payload {
broadcaster_user_id: "129546453",
broadcaster_user_login: "nerixyz",
broadcaster_user_name: "nerixyz",
user_id: "489584266",
user_login: "uint128",
user_name: "uint128",
message_id: "78ccd959-3e7e-4f8d-bd8b-f92c359b0a7d",
message: AutomodMessage {
text: "😂 ass",
fragments: [
Text {
text: "😂 ",
},
Text {
text: "ass",
},
],
},
reason: Automod(
AutomodMessageInfo {
category: Swearing,
level: 4,
boundaries: [
AutomodMessageBoundary {
start_pos: 2,
end_pos: 4,
},
],
},
),
held_at: "2024-11-18T16:59:46.323937273Z",
},
),
},
)
"#
);
}

#[test]
fn parse_payload_v2_blocked_term() {
use crate::eventsub::{Event, Message};

let payload = r##"
crate::eventsub::assert_eventsub_snapshot!(
r##"
{
"subscription": {
"id": "85c8dcb0-7af4-4581-b684-32087d386384",
Expand Down Expand Up @@ -398,34 +463,99 @@ fn parse_payload_v2_blocked_term() {
"held_at": "2024-11-18T16:58:41.476117057Z"
}
}
"##;

let val = Event::parse(payload).unwrap();
crate::tests::roundtrip(&val);

let Event::AutomodMessageHoldV2(val) = val else {
panic!("invalid event type");
};
let Message::Notification(notif) = val.message else {
panic!("invalid message type");
};

assert_eq!(notif.broadcaster_user_id.as_str(), "129546453");
assert_eq!(notif.message.fragments.len(), 7);

let AutomodHeldReason::BlockedTerm(blocked_term) = &notif.reason else {
panic!("invalid held reason");
};
assert_eq!(blocked_term.terms_found.len(), 2);
assert_eq!(
blocked_term.terms_found[0].term_id.as_str(),
"e4d4f1ba-99bf-4b19-9875-cd4eda98ead9"
);
assert_eq!(
blocked_term.terms_found[0].boundary,
AutomodMessageBoundary {
start_pos: 15,
end_pos: 17
}
"##,
@r#"
AutomodMessageHoldV2(
Payload {
subscription: EventSubscriptionInformation {
id: "85c8dcb0-7af4-4581-b684-32087d386384",
status: Enabled,
cost: 0,
condition: AutomodMessageHoldV2 {
broadcaster_user_id: "129546453",
moderator_user_id: "129546453",
},
created_at: "2024-11-18T16:36:08.691979783Z",
transport: Websocket(
WebsocketTransportResponse {
session_id: "AgoQREw4FYBWQ5quz4J-S4VYkRIGY2VsbC1j",
connected_at: None,
disconnected_at: None,
},
),
type_: AutomodMessageHold,
version: "2",
},
message: Notification(
AutomodMessageHoldV2Payload {
broadcaster_user_id: "129546453",
broadcaster_user_login: "nerixyz",
broadcaster_user_name: "nerixyz",
user_id: "489584266",
user_login: "uint128",
user_name: "uint128",
message_id: "dcfc6b48-0fd1-446c-8cf5-d1810bb55b73",
message: AutomodMessage {
text: "boobs Kappa 😂😂 foo private",
fragments: [
Text {
text: "boobs",
},
Text {
text: " ",
},
Emote {
text: "Kappa",
emote: AutomodMessageEmote {
id: "25",
emote_set_id: "0",
},
},
Text {
text: " 😂😂 ",
},
Text {
text: "foo",
},
Text {
text: " ",
},
Text {
text: "private",
},
],
},
reason: BlockedTerm(
AutomodBlockedTermInfo {
terms_found: [
AutomodBlockedTerm {
term_id: "e4d4f1ba-99bf-4b19-9875-cd4eda98ead9",
boundary: AutomodMessageBoundary {
start_pos: 15,
end_pos: 17,
},
owner_broadcaster_user_id: "129546453",
owner_broadcaster_user_login: "nerixyz",
owner_broadcaster_user_name: "nerixyz",
},
AutomodBlockedTerm {
term_id: "e60a94ea-e5d9-444e-a114-4cfd2f86c6ad",
boundary: AutomodMessageBoundary {
start_pos: 19,
end_pos: 25,
},
owner_broadcaster_user_id: "129546453",
owner_broadcaster_user_login: "nerixyz",
owner_broadcaster_user_name: "nerixyz",
},
],
},
),
held_at: "2024-11-18T16:58:41.476117057Z",
},
),
},
)
"#
);
}
Loading
Loading