-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathLinkGeneratorTests.fs
More file actions
248 lines (213 loc) · 6.85 KB
/
LinkGeneratorTests.fs
File metadata and controls
248 lines (213 loc) · 6.85 KB
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
// SPDX-FileCopyrightText: 2024 Emulsion contributors <https://github.com/codingteam/emulsion>
//
// SPDX-License-Identifier: MIT
module Emulsion.Tests.Telegram.LinkGeneratorTests
open System
open Funogram.Telegram.Types
open Serilog.Core
open Xunit
open Emulsion.ContentProxy
open Emulsion.Database
open Emulsion.Settings
open Emulsion.Telegram
open Emulsion.TestFramework
let private hostingSettings = {
ExternalUriBase = Uri "https://example.com"
BindUri = "http://localhost:5556"
HashIdSalt = "mySalt"
}
let private chatName = "test_chat"
let private fileId1 = "123456"
let private fileId2 = "654321"
let private photo: PhotoSize =
{
FileId = fileId1
FileUniqueId = fileId1
Width = 0
Height = 0
FileSize = None
}
let private messageTemplate =
Message.Create(
messageId = 0L,
date = DateTime.MinValue,
chat = Chat.Create(
id = 0L,
``type`` = ChatType.SuperGroup,
username = chatName
)
)
let private messageWithDocument =
{ messageTemplate with
Document = Some {
FileId = fileId1
FileUniqueId = fileId1
Thumbnail = None
FileName = None
MimeType = None
FileSize = None
}
}
let private messageWithAudio =
{ messageTemplate with
Audio = Some {
FileId = fileId1
FileUniqueId = fileId1
FileName = None
Duration = 0
Performer = None
Title = None
MimeType = None
FileSize = None
Thumbnail = None
}
}
let private messageWithAnimation =
{ messageTemplate with
Animation = Some <| Animation.Create(
fileId = fileId1,
fileUniqueId = fileId1,
width = 0L,
height = 0L,
duration = 0L
)
}
let private messageWithPhoto =
{ messageTemplate with
Photo = Some([| photo |])
}
let private messageWithMultiplePhotos =
{ messageWithPhoto with
Photo = Some(Array.append (Option.get messageWithPhoto.Photo) [|{
FileId = fileId2
FileUniqueId = fileId2
Width = 1000
Height = 2000
FileSize = None
}|])
}
let private messageWithSticker =
{ messageTemplate with
Sticker = Some <| Sticker.Create(
fileId = fileId1,
fileUniqueId = fileId1,
``type`` = "",
width = 0,
height = 0,
isAnimated = false,
isVideo = false
)
}
let private messageWithVideoSticker =
{ messageTemplate with
Sticker = Some <| Sticker.Create(
fileId = fileId1,
fileUniqueId = fileId1,
``type`` = "",
width = 0,
height = 0,
isAnimated = false,
isVideo = true
)
}
let private messageWithAnimatedSticker =
{ messageTemplate with
Sticker = Some <| Sticker.Create(
fileId = fileId2,
fileUniqueId = fileId2,
``type`` = "",
width = 0,
height = 0,
isAnimated = true,
isVideo = false,
thumbnail = photo
)
}
let private messageWithVideo =
{ messageTemplate with
Video = Some {
Cover = None
Duration = 0
FileId = fileId1
FileName = None
FileSize = None
FileUniqueId = fileId1
Height = 0
MimeType = None
StartTimestamp = None
Thumbnail = None
Width = 0
}
}
let private messageWithVoice =
{ messageTemplate with
Voice = Some {
FileId = fileId1
FileUniqueId = fileId1
Duration = 0
MimeType = None
FileSize = None
}
}
let private messageWithVideoNote =
{ messageTemplate with
VideoNote = Some {
FileId = fileId1
FileUniqueId = fileId1
Length = 0
Duration = 0
Thumbnail = None
FileSize = None
}
}
let private doBasicLinkTest message =
let links = Async.RunSynchronously(LinkGenerator.gatherLinks Logger.None None None message)
let expectedUri = Seq.singleton <| Uri $"https://t.me/{chatName}/{message.MessageId}"
Assert.Equal<Uri>(expectedUri, links.ContentLinks)
let private doDatabaseLinksTest (fileIds: string[]) message =
Async.RunSynchronously <| TestDataStorage.doWithDatabase(fun databaseSettings ->
async {
let! links = LinkGenerator.gatherLinks Logger.None (Some databaseSettings) (Some hostingSettings) message
let contentLinks = Seq.toArray links.ContentLinks
for fileId, link in Seq.zip fileIds contentLinks do
let link = link.ToString()
let baseUri = hostingSettings.ExternalUriBase.ToString()
Assert.StartsWith(baseUri, link)
let emptyLinkLength = (Proxy.getLink hostingSettings.ExternalUriBase "").ToString().Length
let id = link.Substring(emptyLinkLength)
let! content = DataStorage.transaction databaseSettings (fun context ->
ContentStorage.getById context (Proxy.decodeHashId hostingSettings.HashIdSalt id)
)
let content = Option.get content
Assert.Equal(message.MessageId, content.MessageId)
Assert.Equal(message.Chat.Username, Some content.ChatUserName)
Assert.Equal(fileId, content.FileId)
Assert.Equal(fileIds.Length, contentLinks.Length)
}
)
let private doDatabaseLinkTest fileId message =
doDatabaseLinksTest [|fileId|] message
[<Fact>]
let documentLinkTest(): unit = doBasicLinkTest messageWithDocument
[<Fact>]
let databaseDocumentTest(): unit = doDatabaseLinkTest fileId1 messageWithDocument
[<Fact>]
let databaseAudioTest(): unit = doDatabaseLinkTest fileId1 messageWithAudio
[<Fact>]
let databaseAnimationTest(): unit = doDatabaseLinkTest fileId1 messageWithAnimation
[<Fact>]
let databasePhotoTest(): unit = doDatabaseLinkTest fileId1 messageWithPhoto
[<Fact>]
let databaseStickerTest(): unit = doDatabaseLinkTest fileId1 messageWithSticker
[<Fact>]
let databaseVideoStickerTest(): unit = doDatabaseLinkTest fileId1 messageWithVideoSticker
[<Fact>]
let databaseAnimatedStickerTest(): unit = doDatabaseLinkTest fileId1 messageWithAnimatedSticker
[<Fact>]
let databaseVideoTest(): unit = doDatabaseLinkTest fileId1 messageWithVideo
[<Fact>]
let databaseVoiceTest(): unit = doDatabaseLinkTest fileId1 messageWithVoice
[<Fact>]
let databaseVideoNoteTest(): unit = doDatabaseLinkTest fileId1 messageWithVideoNote
[<Fact>]
let databaseMultiplePhotosTest(): unit = doDatabaseLinksTest [|fileId2|] messageWithMultiplePhotos