1- // Discord tests cover message handler.hydration plugin behavior.
21import { MessageReferenceType , MessageType } from "discord-api-types/v10" ;
32import { describe , expect , it , vi } from "vitest" ;
43import { Message } from "../internal/discord.js" ;
@@ -8,13 +7,65 @@ import {
87} from "../internal/test-builders.test-support.js" ;
98import { hydrateDiscordMessageIfNeeded } from "./message-handler.hydration.js" ;
109
10+ const TEST_TIMESTAMP = "2026-01-01T00:00:00.000Z" ;
11+
12+ function createMessagePayload ( overrides = { } ) {
13+ return {
14+ id : "m1" ,
15+ channel_id : "c1" ,
16+ content : "what did this mean?" ,
17+ attachments : [ ] ,
18+ embeds : [ ] ,
19+ mentions : [ ] ,
20+ mention_roles : [ ] ,
21+ mention_everyone : false ,
22+ timestamp : TEST_TIMESTAMP ,
23+ edited_timestamp : null ,
24+ author : {
25+ id : "u1" ,
26+ username : "alice" ,
27+ global_name : null ,
28+ discriminator : "0" ,
29+ avatar : null ,
30+ } ,
31+ type : MessageType . Default ,
32+ tts : false ,
33+ pinned : false ,
34+ flags : 0 ,
35+ ...overrides ,
36+ } ;
37+ }
38+
39+ function createDefaultReplyPayload ( overrides = { } ) {
40+ return createMessagePayload ( {
41+ message_reference : {
42+ type : MessageReferenceType . Default ,
43+ message_id : "m0" ,
44+ channel_id : "c1" ,
45+ } ,
46+ type : MessageType . Reply ,
47+ ...overrides ,
48+ } ) ;
49+ }
50+
51+ function createReferencedMessagePayload ( content : string ) {
52+ return createMessagePayload ( {
53+ id : "m0" ,
54+ content,
55+ author : {
56+ id : "u2" ,
57+ username : "bob" ,
58+ discriminator : "0" ,
59+ avatar : null ,
60+ } ,
61+ } ) ;
62+ }
63+
1164describe ( "hydrateDiscordMessageIfNeeded" , ( ) => {
1265 it ( "hydrates partial internal messages without assigning over getters" , async ( ) => {
1366 const client = createInternalTestClient ( ) ;
1467 const rest = createFakeRestClient ( [
15- {
16- id : "m1" ,
17- channel_id : "c1" ,
68+ createMessagePayload ( {
1869 content : "hello <@u2>" ,
1970 attachments : [ { id : "a1" , filename : "note.txt" } ] ,
2071 embeds : [ { title : "Embed" } ] ,
@@ -29,39 +80,17 @@ describe("hydrateDiscordMessageIfNeeded", () => {
2980 ] ,
3081 mention_roles : [ "role1" ] ,
3182 mention_everyone : false ,
32- timestamp : new Date ( ) . toISOString ( ) ,
33- author : {
34- id : "u1" ,
35- username : "alice" ,
36- discriminator : "0" ,
37- avatar : null ,
38- } ,
39- referenced_message : {
83+ referenced_message : createMessagePayload ( {
4084 id : "m0" ,
41- channel_id : "c1" ,
4285 content : "earlier" ,
43- attachments : [ ] ,
44- embeds : [ ] ,
45- mentions : [ ] ,
46- mention_roles : [ ] ,
47- mention_everyone : false ,
48- timestamp : new Date ( ) . toISOString ( ) ,
4986 author : {
5087 id : "u3" ,
5188 username : "carol" ,
5289 discriminator : "0" ,
5390 avatar : null ,
5491 } ,
55- type : 0 ,
56- tts : false ,
57- pinned : false ,
58- flags : 0 ,
59- } ,
60- type : 0 ,
61- tts : false ,
62- pinned : false ,
63- flags : 0 ,
64- } ,
92+ } ) ,
93+ } ) ,
6594 ] ) ;
6695 const message = new Message < true > ( client , { id : "m1" , channelId : "c1" } ) as unknown as Message ;
6796
@@ -83,80 +112,11 @@ describe("hydrateDiscordMessageIfNeeded", () => {
83112 it ( "hydrates reply references when Discord omits referenced_message" , async ( ) => {
84113 const client = createInternalTestClient ( ) ;
85114 const rest = createFakeRestClient ( [
86- {
87- id : "m1" ,
88- channel_id : "c1" ,
89- content : "what did this mean?" ,
90- attachments : [ ] ,
91- embeds : [ ] ,
92- mentions : [ ] ,
93- mention_roles : [ ] ,
94- mention_everyone : false ,
95- timestamp : new Date ( ) . toISOString ( ) ,
96- author : {
97- id : "u1" ,
98- username : "alice" ,
99- discriminator : "0" ,
100- avatar : null ,
101- } ,
102- message_reference : {
103- type : MessageReferenceType . Default ,
104- message_id : "m0" ,
105- channel_id : "c1" ,
106- } ,
107- referenced_message : {
108- id : "m0" ,
109- channel_id : "c1" ,
110- content : "the replied-to message" ,
111- attachments : [ ] ,
112- embeds : [ ] ,
113- mentions : [ ] ,
114- mention_roles : [ ] ,
115- mention_everyone : false ,
116- timestamp : new Date ( ) . toISOString ( ) ,
117- author : {
118- id : "u2" ,
119- username : "bob" ,
120- discriminator : "0" ,
121- avatar : null ,
122- } ,
123- type : MessageType . Default ,
124- tts : false ,
125- pinned : false ,
126- flags : 0 ,
127- } ,
128- type : MessageType . Reply ,
129- tts : false ,
130- pinned : false ,
131- flags : 0 ,
132- } ,
115+ createDefaultReplyPayload ( {
116+ referenced_message : createReferencedMessagePayload ( "the replied-to message" ) ,
117+ } ) ,
133118 ] ) ;
134- const message = new Message ( client , {
135- id : "m1" ,
136- channel_id : "c1" ,
137- content : "what did this mean?" ,
138- attachments : [ ] ,
139- embeds : [ ] ,
140- mentions : [ ] ,
141- mention_roles : [ ] ,
142- mention_everyone : false ,
143- timestamp : new Date ( ) . toISOString ( ) ,
144- author : {
145- id : "u1" ,
146- username : "alice" ,
147- global_name : null ,
148- discriminator : "0" ,
149- avatar : null ,
150- } ,
151- message_reference : {
152- type : MessageReferenceType . Default ,
153- message_id : "m0" ,
154- channel_id : "c1" ,
155- } ,
156- type : MessageType . Reply ,
157- tts : false ,
158- pinned : false ,
159- } ) ;
119+ const message = new Message ( client , createDefaultReplyPayload ( ) ) ;
160120
161121 const hydrated = await hydrateDiscordMessageIfNeeded ( {
162122 client : { rest } ,
@@ -175,32 +135,7 @@ describe("hydrateDiscordMessageIfNeeded", () => {
175135 throw Object . assign ( new Error ( "Missing Access" ) , { status : 403 } ) ;
176136 } ) ;
177137 rest . get = get ;
178- const message = new Message ( client , {
179- id : "m1" ,
180- channel_id : "c1" ,
181- content : "what did this mean?" ,
182- attachments : [ ] ,
183- embeds : [ ] ,
184- mentions : [ ] ,
185- mention_roles : [ ] ,
186- mention_everyone : false ,
187- timestamp : new Date ( ) . toISOString ( ) ,
188- author : {
189- id : "u1" ,
190- username : "alice" ,
191- global_name : null ,
192- discriminator : "0" ,
193- avatar : null ,
194- } ,
195- message_reference : {
196- type : MessageReferenceType . Default ,
197- message_id : "m0" ,
198- channel_id : "c1" ,
199- } ,
200- type : MessageType . Reply ,
201- tts : false ,
202- pinned : false ,
203- } ) ;
138+ const message = new Message ( client , createDefaultReplyPayload ( ) ) ;
204139
205140 const hydrated = await hydrateDiscordMessageIfNeeded ( {
206141 client : { rest } ,
@@ -216,46 +151,22 @@ describe("hydrateDiscordMessageIfNeeded", () => {
216151 it ( "does not hydrate known-deleted or forwarded references" , async ( ) => {
217152 const client = createInternalTestClient ( ) ;
218153 const rest = createFakeRestClient ( ) ;
219- const baseMessage = {
220- id : "m1" ,
221- channel_id : "c1" ,
222- content : "what did this mean?" ,
223- attachments : [ ] ,
224- embeds : [ ] ,
225- mentions : [ ] ,
226- mention_roles : [ ] ,
227- mention_everyone : false ,
228- timestamp : new Date ( ) . toISOString ( ) ,
229- author : {
230- id : "u1" ,
231- username : "alice" ,
232- global_name : null ,
233- discriminator : "0" ,
234- avatar : null ,
235- } ,
236- tts : false ,
237- pinned : false ,
238- } ;
239-
240- const deletedReply = new Message ( client , {
241- ...baseMessage ,
242- message_reference : {
243- type : MessageReferenceType . Default ,
244- message_id : "m0" ,
245- channel_id : "c1" ,
246- } ,
247- referenced_message : null ,
248- type : MessageType . Reply ,
249- } ) ;
250- const forwardedMessage = new Message ( client , {
251- ...baseMessage ,
252- message_reference : {
253- type : MessageReferenceType . Forward ,
254- message_id : "m0" ,
255- channel_id : "c1" ,
256- } ,
257- type : MessageType . Default ,
258- } ) ;
154+ const deletedReply = new Message (
155+ client ,
156+ createDefaultReplyPayload ( {
157+ referenced_message : null ,
158+ } ) ,
159+ ) ;
160+ const forwardedMessage = new Message (
161+ client ,
162+ createMessagePayload ( {
163+ message_reference : {
164+ type : MessageReferenceType . Forward ,
165+ message_id : "m0" ,
166+ channel_id : "c1" ,
167+ } ,
168+ } ) ,
169+ ) ;
259170
260171 await hydrateDiscordMessageIfNeeded ( {
261172 client : { rest } ,
0 commit comments