Skip to content

Commit b586b32

Browse files
NerixyzEmilgardis
authored andcommitted
refactor: use snapshot tests
1 parent ba8e644 commit b586b32

File tree

38 files changed

+1549
-1200
lines changed

38 files changed

+1549
-1200
lines changed

Cargo.lock

Lines changed: 695 additions & 646 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ serde_cbor = "0.11.2"
167167
serde_json = "1.0.107"
168168
reqwest = "0.12.2"
169169
hyper = "1.4.1"
170+
insta = { version = "1.42.0", features = ["json"] }
170171

171172
[build-dependencies]
172173
tower = "0.4.13"

src/eventsub/automod/message/hold.rs

Lines changed: 10 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,8 @@ pub struct AutomodMessageHoldV1Payload {
7171
#[cfg(test)]
7272
#[test]
7373
fn parse_payload_v1() {
74-
use crate::eventsub::{Event, Message};
75-
76-
let payload = r##"
74+
crate::eventsub::assert_eventsub_snapshot!(
75+
r##"
7776
{
7877
"subscription": {
7978
"id": "e523fda0-01b6-4b0e-9024-a5a80c5ad680",
@@ -115,22 +114,8 @@ fn parse_payload_v1() {
115114
"held_at": "2024-10-19T20:11:16.799750627Z"
116115
}
117116
}
118-
"##;
119-
120-
let val = Event::parse(payload).unwrap();
121-
crate::tests::roundtrip(&val);
122-
123-
let Event::AutomodMessageHoldV1(val) = val else {
124-
panic!("invalid event type");
125-
};
126-
let Message::Notification(notif) = val.message else {
127-
panic!("invalid message type");
128-
};
129-
130-
assert_eq!(notif.broadcaster_user_id.as_str(), "129546453");
131-
assert_eq!(notif.category, AutomodCategory::Sexwords);
132-
assert_eq!(notif.level, 4);
133-
assert_eq!(notif.message.fragments.len(), 1);
117+
"##
118+
);
134119
}
135120

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

201186
#[test]
202187
fn parse_payload_v2_automod() {
203-
use crate::eventsub::{Event, Message};
204-
205-
let payload = r##"
188+
crate::eventsub::assert_eventsub_snapshot!(
189+
r##"
206190
{
207191
"subscription": {
208192
"id": "85c8dcb0-7af4-4581-b684-32087d386384",
@@ -260,40 +244,14 @@ fn parse_payload_v2_automod() {
260244
"held_at": "2024-11-18T16:59:46.323937273Z"
261245
}
262246
}
263-
"##;
264-
265-
let val = Event::parse(payload).unwrap();
266-
crate::tests::roundtrip(&val);
267-
268-
let Event::AutomodMessageHoldV2(val) = val else {
269-
panic!("invalid event type");
270-
};
271-
let Message::Notification(notif) = val.message else {
272-
panic!("invalid message type");
273-
};
274-
275-
assert_eq!(notif.broadcaster_user_id.as_str(), "129546453");
276-
assert_eq!(notif.message.fragments.len(), 2);
277-
278-
let AutomodHeldReason::Automod(automod) = &notif.reason else {
279-
panic!("invalid held reason");
280-
};
281-
assert_eq!(automod.category, AutomodCategory::Swearing);
282-
assert_eq!(automod.level, 4);
283-
assert_eq!(
284-
automod.boundaries,
285-
&[AutomodMessageBoundary {
286-
start_pos: 2,
287-
end_pos: 4
288-
}]
247+
"##
289248
);
290249
}
291250

292251
#[test]
293252
fn parse_payload_v2_blocked_term() {
294-
use crate::eventsub::{Event, Message};
295-
296-
let payload = r##"
253+
crate::eventsub::assert_eventsub_snapshot!(
254+
r##"
297255
{
298256
"subscription": {
299257
"id": "85c8dcb0-7af4-4581-b684-32087d386384",
@@ -398,34 +356,6 @@ fn parse_payload_v2_blocked_term() {
398356
"held_at": "2024-11-18T16:58:41.476117057Z"
399357
}
400358
}
401-
"##;
402-
403-
let val = Event::parse(payload).unwrap();
404-
crate::tests::roundtrip(&val);
405-
406-
let Event::AutomodMessageHoldV2(val) = val else {
407-
panic!("invalid event type");
408-
};
409-
let Message::Notification(notif) = val.message else {
410-
panic!("invalid message type");
411-
};
412-
413-
assert_eq!(notif.broadcaster_user_id.as_str(), "129546453");
414-
assert_eq!(notif.message.fragments.len(), 7);
415-
416-
let AutomodHeldReason::BlockedTerm(blocked_term) = &notif.reason else {
417-
panic!("invalid held reason");
418-
};
419-
assert_eq!(blocked_term.terms_found.len(), 2);
420-
assert_eq!(
421-
blocked_term.terms_found[0].term_id.as_str(),
422-
"e4d4f1ba-99bf-4b19-9875-cd4eda98ead9"
423-
);
424-
assert_eq!(
425-
blocked_term.terms_found[0].boundary,
426-
AutomodMessageBoundary {
427-
start_pos: 15,
428-
end_pos: 17
429-
}
359+
"##
430360
);
431361
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
source: src/eventsub/automod/message/hold.rs
3+
expression: parsed
4+
---
5+
AutomodMessageHoldV1(
6+
Payload {
7+
subscription: EventSubscriptionInformation {
8+
id: "e523fda0-01b6-4b0e-9024-a5a80c5ad680",
9+
status: Enabled,
10+
cost: 0,
11+
condition: AutomodMessageHoldV1 {
12+
broadcaster_user_id: "129546453",
13+
moderator_user_id: "129546453",
14+
},
15+
created_at: "2024-10-19T20:11:13.917500523Z",
16+
transport: Websocket(
17+
WebsocketTransportResponse {
18+
session_id: "AgoQRniKAQ1ITYSESh4ku8anEBIGY2VsbC1j",
19+
connected_at: None,
20+
disconnected_at: None,
21+
},
22+
),
23+
type_: AutomodMessageHold,
24+
version: "1",
25+
},
26+
message: Notification(
27+
AutomodMessageHoldV1Payload {
28+
broadcaster_user_id: "129546453",
29+
broadcaster_user_login: "nerixyz",
30+
broadcaster_user_name: "nerixyz",
31+
user_id: "489584266",
32+
user_login: "uint128",
33+
user_name: "uint128",
34+
message_id: "332e99ac-e19c-4368-a15b-793e8266b51f",
35+
message: AutomodMessage {
36+
text: "boobs",
37+
fragments: [
38+
Text {
39+
text: "boobs",
40+
},
41+
],
42+
},
43+
category: Sexwords,
44+
level: 4,
45+
held_at: "2024-10-19T20:11:16.799750627Z",
46+
},
47+
),
48+
},
49+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
source: src/eventsub/automod/message/hold.rs
3+
expression: parsed
4+
---
5+
AutomodMessageHoldV2(
6+
Payload {
7+
subscription: EventSubscriptionInformation {
8+
id: "85c8dcb0-7af4-4581-b684-32087d386384",
9+
status: Enabled,
10+
cost: 0,
11+
condition: AutomodMessageHoldV2 {
12+
broadcaster_user_id: "129546453",
13+
moderator_user_id: "129546453",
14+
},
15+
created_at: "2024-11-18T16:36:08.691979783Z",
16+
transport: Websocket(
17+
WebsocketTransportResponse {
18+
session_id: "AgoQREw4FYBWQ5quz4J-S4VYkRIGY2VsbC1j",
19+
connected_at: None,
20+
disconnected_at: None,
21+
},
22+
),
23+
type_: AutomodMessageHold,
24+
version: "2",
25+
},
26+
message: Notification(
27+
AutomodMessageHoldV2Payload {
28+
broadcaster_user_id: "129546453",
29+
broadcaster_user_login: "nerixyz",
30+
broadcaster_user_name: "nerixyz",
31+
user_id: "489584266",
32+
user_login: "uint128",
33+
user_name: "uint128",
34+
message_id: "78ccd959-3e7e-4f8d-bd8b-f92c359b0a7d",
35+
message: AutomodMessage {
36+
text: "😂 ass",
37+
fragments: [
38+
Text {
39+
text: "😂 ",
40+
},
41+
Text {
42+
text: "ass",
43+
},
44+
],
45+
},
46+
reason: Automod(
47+
AutomodMessageInfo {
48+
category: Swearing,
49+
level: 4,
50+
boundaries: [
51+
AutomodMessageBoundary {
52+
start_pos: 2,
53+
end_pos: 4,
54+
},
55+
],
56+
},
57+
),
58+
held_at: "2024-11-18T16:59:46.323937273Z",
59+
},
60+
),
61+
},
62+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
source: src/eventsub/automod/message/hold.rs
3+
expression: parsed
4+
---
5+
AutomodMessageHoldV2(
6+
Payload {
7+
subscription: EventSubscriptionInformation {
8+
id: "85c8dcb0-7af4-4581-b684-32087d386384",
9+
status: Enabled,
10+
cost: 0,
11+
condition: AutomodMessageHoldV2 {
12+
broadcaster_user_id: "129546453",
13+
moderator_user_id: "129546453",
14+
},
15+
created_at: "2024-11-18T16:36:08.691979783Z",
16+
transport: Websocket(
17+
WebsocketTransportResponse {
18+
session_id: "AgoQREw4FYBWQ5quz4J-S4VYkRIGY2VsbC1j",
19+
connected_at: None,
20+
disconnected_at: None,
21+
},
22+
),
23+
type_: AutomodMessageHold,
24+
version: "2",
25+
},
26+
message: Notification(
27+
AutomodMessageHoldV2Payload {
28+
broadcaster_user_id: "129546453",
29+
broadcaster_user_login: "nerixyz",
30+
broadcaster_user_name: "nerixyz",
31+
user_id: "489584266",
32+
user_login: "uint128",
33+
user_name: "uint128",
34+
message_id: "dcfc6b48-0fd1-446c-8cf5-d1810bb55b73",
35+
message: AutomodMessage {
36+
text: "boobs Kappa 😂😂 foo private",
37+
fragments: [
38+
Text {
39+
text: "boobs",
40+
},
41+
Text {
42+
text: " ",
43+
},
44+
Emote {
45+
text: "Kappa",
46+
emote: AutomodMessageEmote {
47+
id: "25",
48+
emote_set_id: "0",
49+
},
50+
},
51+
Text {
52+
text: " 😂😂 ",
53+
},
54+
Text {
55+
text: "foo",
56+
},
57+
Text {
58+
text: " ",
59+
},
60+
Text {
61+
text: "private",
62+
},
63+
],
64+
},
65+
reason: BlockedTerm(
66+
AutomodBlockedTermInfo {
67+
terms_found: [
68+
AutomodBlockedTerm {
69+
term_id: "e4d4f1ba-99bf-4b19-9875-cd4eda98ead9",
70+
boundary: AutomodMessageBoundary {
71+
start_pos: 15,
72+
end_pos: 17,
73+
},
74+
owner_broadcaster_user_id: "129546453",
75+
owner_broadcaster_user_login: "nerixyz",
76+
owner_broadcaster_user_name: "nerixyz",
77+
},
78+
AutomodBlockedTerm {
79+
term_id: "e60a94ea-e5d9-444e-a114-4cfd2f86c6ad",
80+
boundary: AutomodMessageBoundary {
81+
start_pos: 19,
82+
end_pos: 25,
83+
},
84+
owner_broadcaster_user_id: "129546453",
85+
owner_broadcaster_user_login: "nerixyz",
86+
owner_broadcaster_user_name: "nerixyz",
87+
},
88+
],
89+
},
90+
),
91+
held_at: "2024-11-18T16:58:41.476117057Z",
92+
},
93+
),
94+
},
95+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
source: src/eventsub/automod/message/update.rs
3+
expression: parsed
4+
---
5+
AutomodMessageUpdateV1(
6+
Payload {
7+
subscription: EventSubscriptionInformation {
8+
id: "79cc58a2-1c34-48e0-97fe-126d5d77bf10",
9+
status: Enabled,
10+
cost: 0,
11+
condition: AutomodMessageUpdateV1 {
12+
broadcaster_user_id: "129546453",
13+
moderator_user_id: "129546453",
14+
},
15+
created_at: "2024-11-03T11:52:04.695680375Z",
16+
transport: Websocket(
17+
WebsocketTransportResponse {
18+
session_id: "AgoQZ12VWLotRG6u3pudLlbhvhIGY2VsbC1j",
19+
connected_at: None,
20+
disconnected_at: None,
21+
},
22+
),
23+
type_: AutomodMessageUpdate,
24+
version: "1",
25+
},
26+
message: Notification(
27+
AutomodMessageUpdateV1Payload {
28+
broadcaster_user_id: "129546453",
29+
broadcaster_user_login: "nerixyz",
30+
broadcaster_user_name: "nerixyz",
31+
user_id: "489584266",
32+
user_login: "uint128",
33+
user_name: "uint128",
34+
moderator_user_id: "129546453",
35+
moderator_user_login: "nerixyz",
36+
moderator_user_name: "nerixyz",
37+
message_id: "8b722958-741f-4013-8a8b-c7793d3aef9f",
38+
message: AutomodMessage {
39+
text: "boobs",
40+
fragments: [
41+
Text {
42+
text: "boobs",
43+
},
44+
],
45+
},
46+
category: Sexwords,
47+
level: 4,
48+
status: Approved,
49+
held_at: "2024-11-03T11:53:45.331308397Z",
50+
},
51+
),
52+
},
53+
)

0 commit comments

Comments
 (0)