Skip to content

Commit 38b2695

Browse files
committed
refactor: use snapshot tests
1 parent 1158b7d commit 38b2695

File tree

38 files changed

+909
-556
lines changed

38 files changed

+909
-556
lines changed

Cargo.lock

+55-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
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

+10-80
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+
)

0 commit comments

Comments
 (0)