forked from tinode/tinode-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrafty.test.js
344 lines (336 loc) · 10.2 KB
/
drafty.test.js
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
const Drafty = require('./drafty');
// Drafty.parse test data.
const parse_this = [
[
'This is *bold*, `code` and _italic_, ~strike~',
{
"fmt": [
{"at": 8, "len": 4,"tp": "ST"},
{"at": 14,"len": 4,"tp": "CO"},
{"at": 23,"len": 6,"tp": "EM"},
{"at": 31,"len": 6,"tp": "DL"},
],
"txt": "This is bold, code and italic, strike",
}
],
[
'Это *жЫрный*, `код` и _наклонный_, ~зачеркнутый~',
{
"fmt": [
{"at": 4, "len": 6,"tp": "ST"},
{"at": 12,"len": 3,"tp": "CO"},
{"at": 18,"len": 9,"tp": "EM"},
{"at": 29,"len": 11,"tp": "DL"},
],
"txt": "Это жЫрный, код и наклонный, зачеркнутый",
}
],
[
'combined *bold and _italic_*',
{
"fmt": [
{"at": 18,"len": 6,"tp": "EM"},
{"at": 9,"len": 15,"tp": "ST"},
],
"txt": "combined bold and italic",
}
],
// FIXME: this test is inconsistent between golang, Android, and Javascript.
[
'This *text _has* staggered_ formats',
{
"fmt": [
{"at": 5,"len": 9,"tp": "ST"},
],
"txt": "This text _has staggered_ formats",
},
],
[
'an url: https://www.example.com/abc#fragment and another _www.tinode.co_',
{
"ent": [
{"data": {"url": "https://www.example.com/abc#fragment"},"tp": "LN"},
{"data": {"url": "http://www.tinode.co"}, "tp": "LN"},
],
"fmt": [
{"at": 57,"len": 13,"tp": "EM"},
{"at": 8,"len": 36,"key": 0},
{"at": 57,"len": 13,"key": 1},
],
"txt": "an url: https://www.example.com/abc#fragment and another www.tinode.co"
},
],
[
'this is a @mention and a #hashtag in a string',
{
"ent": [
{"data": {"val": "mention"}, "tp": "MN"},
{"data": {"val": "hashtag"}, "tp": "HT"},
],
"fmt": [
{"at": 10, "key": 0, "len": 8},
{"at": 25, "key": 1, "len": 8},
],
"txt": "this is a @mention and a #hashtag in a string"
},
],
[
'second #юникод',
{
"ent": [
{"data": {"val": "юникод"},"tp": "HT"},
],
"fmt": [
{"at": 7, "key": 0, "len": 7},
],
"txt": "second #юникод",
},
],
];
test.each(parse_this)('Drafty.parse %s', (src, exp) => {
expect(Drafty.parse(src)).toEqual(exp);
});
// Drafty docs for testing Drafty.preview and Drafty.normalize.
const shorten_this = [
[
"This is a plain text string.",
{"txt":"This is a plai…"},
],
[
{
"txt":"This is a string.", // Maybe remove extra space.
"fmt":[{"at":9,"tp":"BR"}]
},
{"txt":"This is a stri…","fmt":[{"at":9,len:0,"tp":"BR"}]},
],
[
{
"txt":"This is a string.",
"fmt":[{"at":true,"tp":"XX","len":{}}]
},
{"txt":"This is a stri…"},
],
[
{
"txt":"This is a string.",
"fmt":[{"at":{},"tp":123,"len":null}]
},
{"txt":"This is a stri…"},
],
[
{
"txt":"This is a string.",
"fmt":[{"test": 123},{"at":NaN,"tp":123,"len":-12}]
},
{"txt":"This is a stri…"},
],
[
{
"fmt":[{"at":-1}],
"ent":[{"data":{"mime":"image/jpeg","name":"hello.jpg","val":"<38992, bytes: 123456789012345678901234567890123456789012345678901234567890>","width":100, "height":80},"tp":"EX"}]
},
{
"txt": "",
"fmt":[{"at":-1,"key":0,"len":0}],
"ent":[{"tp":"EX","data":{"height":80,"mime":"image/jpeg","name":"hello.jpg","width":100}}]
},
],
[
{
"fmt":[{"at":-100,"len":99}],
"ent":[{"data":{"mime":"image/jpeg","name":"hello.jpg","val":"<38992, bytes: 123456789012345678901234567890123456789012345678901234567890>","width":100, "height":80},"tp":"EX"}]
},
{
"txt": "",
"fmt":[{"at":-1,"key":0,"len":0}],
"ent":[{"tp":"EX","data":{"height":80,"mime":"image/jpeg","name":"hello.jpg","width":100}}]
},
],
[
{
"fmt":[{"at":-1, "key": "fake"}],
"ent":[{"data":{"width":100},"tp":"EX"}]
},
{
"txt": ""
},
],
[
{
"txt": "Message with attachment",
"fmt":[{"at":-1,"len":0,"key":0},{"at":8,"len":4,"tp":"ST"}],
"ent":[{"data":{"mime":"image/jpeg","name":"hello.jpg","val":"<38992, bytes: 123456789012345678901234567890123456789012345678901234567890>","width":100, "height":80},"tp":"EX"}]
},
{
"txt": "Message with a…",
"fmt":[{"at":8,"len":4,"tp":"ST"},{"at":-1,"len":0,"key":0}],
"ent":[{"tp":"EX","data":{"height":80,"mime":"image/jpeg","name":"hello.jpg","width":100}}]
},
],
[
{
"txt":"https://api.tinode.co/",
"fmt":[{"len":22}],
"ent":[{"data":{"url":"https://www.youtube.com/watch?v=dQw4w9WgXcQ"},"tp":"LN"}]
},
{
"txt":"https://api.ti…",
"fmt":[{"at":0,"len":15,"key":0}],
"ent":[{"tp":"LN","data":{"url":"https://www.youtube.com/watch?v=dQw4w9WgXcQ"}}]
},
],
[
{
"txt":"https://api.tinode.co/",
"fmt":[{"len":22}],
"ent":[{"data":{"url":"https://api.tinode.co/"},"tp":"LN"}]
},
{
"txt":"https://api.ti…",
"fmt":[{"at":0,"len":15,"key":0}],
"ent":[{"tp":"LN","data":{"url":"https://api.tinode.co/"}}]
},
],
[
{
"txt":"Url one, two",
"fmt":[{"at":9,"len":3}, {"at":4,"len":3}],
"ent":[{"data":{"url":"http://tinode.co"},"tp":"LN"}]
},
{
"txt":"Url one, two",
"fmt":[{"at":4,"len":3,"key":0},{"at":9,"len":3,"key":0}],
"ent":[{"tp":"LN","data":{"url":"http://tinode.co"}}]
},
],
[
{
"txt":"Url one, two",
"fmt":[{"at":9,"len":3,"key":0},{"at":4,"len":3,"key":1}],
"ent":[{"data":{"url":"http://tinode.co"},"tp":"LN"}, {"data":{"url":"http://example.com"},"tp":"LN"}]
},
{
"txt":"Url one, two",
"fmt":[{"at":4,"len":3,"key":0},{"at":9,"len":3,"key":1}],
"ent":[{"data":{"url":"http://example.com"},"tp":"LN"}, {"data":{"url":"http://tinode.co"},"tp":"LN"}]
},
],
[
{
"txt":" ",
"fmt":[{"len":1}],
"ent":[{"data":{"height":213,"mime":"image/jpeg","name":"roses.jpg","val":"<38992, bytes: 123456789012345678901234567890123456789012345678901234567890>","width":638},"tp":"IM"}]
},
{
"txt":" ",
"fmt":[{"at":0,"len":1,"key":0}],
"ent":[{"tp":"IM","data":{"height":213,"mime":"image/jpeg","name":"roses.jpg","width":638}}]
},
],
[
{
"txt":"This text has staggered formats",
"fmt":[{"at":5,"len":8,"tp":"EM"},{"at":10,"len":13,"tp":"ST"}]
},
{
"txt":"This text has …",
"fmt":[{"tp":"EM","at":5,"len":8}]
},
],
[
{
"txt":"This text is formatted and deleted too",
"fmt":[{"at":5,"len":4,"tp":"ST"},{"at":13,"len":9,"tp":"EM"},{"at":35,"len":3,"tp":"ST"},{"at":27,"len":11,"tp":"DL"}]
},
{
"txt":"This text is f…",
"fmt":[{"tp":"ST","at":5,"len":4},{"tp":"EM","at":13,"len":2}]
},
],
[
{
"txt":"мультибайтовый юникод",
"fmt":[{"len":14,"tp":"ST"},{"at":15,"len":6,"tp":"EM"}]
},
{
"txt":"мультибайтовый…",
"fmt":[{"at":0,"tp":"ST","len":14}]
},
],
[
{
"txt":"Alice Johnson This is a test",
"fmt":[{"at":13,"len":1,"tp":"BR"},{"at":15,"len":1},{"len":13,"key":1},{"len":16,"tp":"QQ"},{"at":16,"len":1,"tp":"BR"}],
"ent":[{"tp":"IM","data":{"mime":"image/jpeg","val":"<1292, bytes: /9j/4AAQSkZJ123456789012345678901234567890123456789012345678901234567890rehH5o6D/9k=>","width":25,"height":14,"size":968}},{"tp":"MN","data":{"val":"usr123abcDE"}}]
},
{
"txt":"Alice Johnson …",
"fmt":[{"at":0,"len":13,"key":0},{"at":13,"len":1,"tp":"BR"},{"at":0,"len":15,"tp":"QQ"}],
"ent":[
{"tp":"MN","data":{"val":"usr123abcDE"}},
]
},
],
];
test.each(shorten_this)('Drafty.shorten %j', (src,exp) => {
expect(Drafty.shorten(src, 15, true)).toEqual(exp);
});
// Drafty docs for testing Drafty.forwardedContent.
const forward_this = [
[
{
"ent":[{"data":{"val":"usrCPvFc6lpAsw"},"tp":"MN"}],
"fmt":[{"len":13},{"at":13,"len":1,"tp":"BR"},{"len":38,"tp":"QQ"}],
"txt":"Alice Johnson This is a reply to replyThis is a Reply -> Forward -> Reply."
},
{
"ent":[{"data":{"val":"usrCPvFc6lpAsw"},"tp":"MN"}],
"fmt":[{"at":0,"len":13,"key":0},{"at":13,"len":1,"tp":"BR"},{"at":0,"len":38,"tp":"QQ"}],
"txt":"Alice Johnson This is a reply to replyThis is a Reply -> Forward -> Reply."
},
],
[
{
"ent":[{"data":{"val":"usrCPvFc6lpAsw"},"tp":"MN"},{"data":{"val":"usrCPvFc6lpAsw"},"tp":"MN"}],
"fmt":[{"len":15},{"at":15,"len":1,"tp":"BR"},{"at":16,"key":1,"len":13},{"at":29,"len":1,"tp":"BR"},{"at":16,"len":36,"tp":"QQ"}],
"txt":"➦ Alice Johnson Alice Johnson This is a simple replyThis is a reply to reply"
},
{
"ent":[{"data":{"val":"usrCPvFc6lpAsw"},"tp":"MN"}],
"fmt":[{"at":0,"key":0,"len":13},{"at":13,"len":1,"tp":"BR"},{"at":0,"len":36,"tp":"QQ"}],
"txt":"Alice Johnson This is a simple replyThis is a reply to reply"
}
],
];
test.each(forward_this)('Drafty.forwardedContent %j', (src,exp) => {
expect(Drafty.forwardedContent(src)).toEqual(exp);
});
// Drafty docs for testing Drafty.preview.
const preview_this = [
[
{
"ent":[{"data":{"val":"usrCPvFc6lpAsw"},"tp":"MN"}],
"fmt":[{"len":13},{"at":13,"len":1,"tp":"BR"},{"len":38,"tp":"QQ"}],
"txt":"Alice Johnson This is a reply to replyThis is a Reply -> Forward -> Reply."
},
{
"fmt":[{"at":0,"len":1,"tp":"QQ"}],
"txt":" This is a Reply -> Forw…"
},
],
[
{
"ent":[{"data":{"val":"usrCPvFc6lpAsw"},"tp":"MN"},{"data":{"val":"usrCPvFc6lpAsw"},"tp":"MN"}],
"fmt":[{"len":15},{"at":15,"len":1,"tp":"BR"},{"at":16,"key":1,"len":13},{"at":29,"len":1,"tp":"BR"},{"at":16,"len":36,"tp":"QQ"}],
"txt":"➦ Alice Johnson Alice Johnson This is a simple replyThis is a reply to reply"
},
{
"ent":[{"data":{"val":"usrCPvFc6lpAsw"},"tp":"MN"}],
"fmt":[{"at":0,"key":0,"len":1},{"at":2,"len":1,"tp":"QQ"}],
"txt":"➦ This is a reply to re…"
}
],
];
test.each(preview_this)('Drafty.preview %j', (src,exp) => {
expect(Drafty.preview(src, 25)).toEqual(exp);
});