forked from nbd-wtf/nostr-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nip58.test.ts
357 lines (324 loc) · 10.2 KB
/
nip58.test.ts
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
345
346
347
348
349
350
351
352
353
354
355
356
357
import { expect, test } from 'bun:test'
import { EventTemplate } from './core.ts'
import {
BadgeAward as BadgeAwardKind,
BadgeDefinition as BadgeDefinitionKind,
ProfileBadges as ProfileBadgesKind,
} from './kinds.ts'
import { finalizeEvent, generateSecretKey } from './pure.ts'
import {
BadgeAward,
BadgeDefinition,
ProfileBadges,
generateBadgeAwardEventTemplate,
generateBadgeDefinitionEventTemplate,
generateProfileBadgesEventTemplate,
validateBadgeAwardEvent,
validateBadgeDefinitionEvent,
validateProfileBadgesEvent,
} from './nip58.ts'
test('BadgeDefinition has required property "d"', () => {
const badge: BadgeDefinition = {
d: 'badge-id',
}
expect(badge.d).toEqual('badge-id')
})
test('BadgeDefinition has optional property "name"', () => {
const badge: BadgeDefinition = {
d: 'badge-id',
name: 'Badge Name',
}
expect(badge.name).toEqual('Badge Name')
})
test('BadgeDefinition has optional property "description"', () => {
const badge: BadgeDefinition = {
d: 'badge-id',
description: 'Badge Description',
}
expect(badge.description).toEqual('Badge Description')
})
test('BadgeDefinition has optional property "image"', () => {
const badge: BadgeDefinition = {
d: 'badge-id',
image: ['https://example.com/badge.png', '1024x1024'],
}
expect(badge.image).toEqual(['https://example.com/badge.png', '1024x1024'])
})
test('BadgeDefinition has optional property "thumbs"', () => {
const badge: BadgeDefinition = {
d: 'badge-id',
thumbs: [
['https://example.com/thumb.png', '100x100'],
['https://example.com/thumb2.png', '200x200'],
],
}
expect(badge.thumbs).toEqual([
['https://example.com/thumb.png', '100x100'],
['https://example.com/thumb2.png', '200x200'],
])
})
test('BadgeAward has required property "a"', () => {
const badgeAward: BadgeAward = {
a: 'badge-definition-address',
p: [
['pubkey1', 'relay1'],
['pubkey2', 'relay2'],
],
}
expect(badgeAward.a).toEqual('badge-definition-address')
})
test('BadgeAward has required property "p"', () => {
const badgeAward: BadgeAward = {
a: 'badge-definition-address',
p: [
['pubkey1', 'relay1'],
['pubkey2', 'relay2'],
],
}
expect(badgeAward.p).toEqual([
['pubkey1', 'relay1'],
['pubkey2', 'relay2'],
])
})
test('ProfileBadges has required property "d"', () => {
const profileBadges: ProfileBadges = {
d: 'profile_badges',
badges: [],
}
expect(profileBadges.d).toEqual('profile_badges')
})
test('ProfileBadges has required property "badges"', () => {
const profileBadges: ProfileBadges = {
d: 'profile_badges',
badges: [],
}
expect(profileBadges.badges).toEqual([])
})
test('ProfileBadges badges array contains objects with required properties "a" and "e"', () => {
const profileBadges: ProfileBadges = {
d: 'profile_badges',
badges: [
{
a: 'badge-definition-address',
e: ['badge-award-event-id'],
},
],
}
expect(profileBadges.badges[0].a).toEqual('badge-definition-address')
expect(profileBadges.badges[0].e).toEqual(['badge-award-event-id'])
})
test('generateBadgeDefinitionEventTemplate generates EventTemplate with mandatory tags', () => {
const badge: BadgeDefinition = {
d: 'badge-id',
}
const eventTemplate = generateBadgeDefinitionEventTemplate(badge)
expect(eventTemplate.tags).toEqual([['d', 'badge-id']])
})
test('generateBadgeDefinitionEventTemplate generates EventTemplate with optional tags', () => {
const badge: BadgeDefinition = {
d: 'badge-id',
name: 'Badge Name',
description: 'Badge Description',
image: ['https://example.com/badge.png', '1024x1024'],
thumbs: [
['https://example.com/thumb.png', '100x100'],
['https://example.com/thumb2.png', '200x200'],
],
}
const eventTemplate = generateBadgeDefinitionEventTemplate(badge)
expect(eventTemplate.tags).toEqual([
['d', 'badge-id'],
['name', 'Badge Name'],
['description', 'Badge Description'],
['image', 'https://example.com/badge.png', '1024x1024'],
['thumb', 'https://example.com/thumb.png', '100x100'],
['thumb', 'https://example.com/thumb2.png', '200x200'],
])
})
test('generateBadgeDefinitionEventTemplate generates EventTemplate without optional tags', () => {
const badge: BadgeDefinition = {
d: 'badge-id',
}
const eventTemplate = generateBadgeDefinitionEventTemplate(badge)
expect(eventTemplate.tags).toEqual([['d', 'badge-id']])
})
test('validateBadgeDefinitionEvent returns true for valid BadgeDefinition event', () => {
const sk = generateSecretKey()
const eventTemplate: EventTemplate = {
content: '',
created_at: Math.floor(Date.now() / 1000),
kind: BadgeDefinitionKind,
tags: [
['d', 'badge-id'],
['name', 'Badge Name'],
['description', 'Badge Description'],
['image', 'https://example.com/badge.png', '1024x1024'],
['thumb', 'https://example.com/thumb.png', '100x100'],
['thumb', 'https://example.com/thumb2.png', '200x200'],
],
}
const event = finalizeEvent(eventTemplate, sk)
const isValid = validateBadgeDefinitionEvent(event)
expect(isValid).toBe(true)
})
test('validateBadgeDefinitionEvent returns false for invalid BadgeDefinition event', () => {
const sk = generateSecretKey()
const eventTemplate: EventTemplate = {
content: '',
created_at: Math.floor(Date.now() / 1000),
kind: BadgeDefinitionKind,
tags: [],
}
const event = finalizeEvent(eventTemplate, sk)
const isValid = validateBadgeDefinitionEvent(event)
expect(isValid).toBe(false)
})
test('generateBadgeAwardEventTemplate generates EventTemplate with mandatory tags', () => {
const badgeAward: BadgeAward = {
a: 'badge-definition-address',
p: [
['pubkey1', 'relay1'],
['pubkey2', 'relay2'],
],
}
const eventTemplate = generateBadgeAwardEventTemplate(badgeAward)
expect(eventTemplate.tags).toEqual([
['a', 'badge-definition-address'],
['p', 'pubkey1', 'relay1'],
['p', 'pubkey2', 'relay2'],
])
})
test('generateBadgeAwardEventTemplate generates EventTemplate without optional tags', () => {
const badgeAward: BadgeAward = {
a: 'badge-definition-address',
p: [
['pubkey1', 'relay1'],
['pubkey2', 'relay2'],
],
}
const eventTemplate = generateBadgeAwardEventTemplate(badgeAward)
expect(eventTemplate.tags).toEqual([
['a', 'badge-definition-address'],
['p', 'pubkey1', 'relay1'],
['p', 'pubkey2', 'relay2'],
])
})
test('generateBadgeAwardEventTemplate generates EventTemplate with optional tags', () => {
const badgeAward: BadgeAward = {
a: 'badge-definition-address',
p: [
['pubkey1', 'relay1'],
['pubkey2', 'relay2'],
],
}
const eventTemplate = generateBadgeAwardEventTemplate(badgeAward)
expect(eventTemplate.tags).toEqual([
['a', 'badge-definition-address'],
['p', 'pubkey1', 'relay1'],
['p', 'pubkey2', 'relay2'],
])
})
test('validateBadgeAwardEvent returns true for valid BadgeAward event', () => {
const sk = generateSecretKey()
const eventTemplate: EventTemplate = {
content: '',
created_at: Math.floor(Date.now() / 1000),
kind: BadgeAwardKind,
tags: [
['a', 'badge-definition-address'],
['p', 'pubkey1', 'relay1'],
['p', 'pubkey2', 'relay2'],
],
}
const event = finalizeEvent(eventTemplate, sk)
const isValid = validateBadgeAwardEvent(event)
expect(isValid).toBe(true)
})
test('validateBadgeAwardEvent returns false for invalid BadgeAward event', () => {
const sk = generateSecretKey()
const eventTemplate: EventTemplate = {
content: '',
created_at: Math.floor(Date.now() / 1000),
kind: BadgeAwardKind,
tags: [],
}
const event = finalizeEvent(eventTemplate, sk)
const isValid = validateBadgeAwardEvent(event)
expect(isValid).toBe(false)
})
test('generateProfileBadgesEventTemplate generates EventTemplate with mandatory tags', () => {
const profileBadges: ProfileBadges = {
d: 'profile_badges',
badges: [],
}
const eventTemplate = generateProfileBadgesEventTemplate(profileBadges)
expect(eventTemplate.tags).toEqual([['d', 'profile_badges']])
})
test('generateProfileBadgesEventTemplate generates EventTemplate with optional tags', () => {
const profileBadges: ProfileBadges = {
d: 'profile_badges',
badges: [
{
a: 'badge-definition-address',
e: ['badge-award-event-id'],
},
],
}
const eventTemplate = generateProfileBadgesEventTemplate(profileBadges)
expect(eventTemplate.tags).toEqual([
['d', 'profile_badges'],
['a', 'badge-definition-address'],
['e', 'badge-award-event-id'],
])
})
test('generateProfileBadgesEventTemplate generates EventTemplate with multiple optional tags', () => {
const profileBadges: ProfileBadges = {
d: 'profile_badges',
badges: [
{
a: 'badge-definition-address1',
e: ['badge-award-event-id1', 'badge-award-event-id2'],
},
{
a: 'badge-definition-address2',
e: ['badge-award-event-id3'],
},
],
}
const eventTemplate = generateProfileBadgesEventTemplate(profileBadges)
expect(eventTemplate.tags).toEqual([
['d', 'profile_badges'],
['a', 'badge-definition-address1'],
['e', 'badge-award-event-id1', 'badge-award-event-id2'],
['a', 'badge-definition-address2'],
['e', 'badge-award-event-id3'],
])
})
test('validateProfileBadgesEvent returns true for valid ProfileBadges event', () => {
const sk = generateSecretKey()
const eventTemplate: EventTemplate = {
content: '',
created_at: Math.floor(Date.now() / 1000),
kind: ProfileBadgesKind,
tags: [
['d', 'profile_badges'],
['a', 'badge-definition-address'],
['e', 'badge-award-event-id'],
],
}
const event = finalizeEvent(eventTemplate, sk)
const isValid = validateProfileBadgesEvent(event)
expect(isValid).toBe(true)
})
test('validateProfileBadgesEvent returns false for invalid ProfileBadges event', () => {
const sk = generateSecretKey()
const eventTemplate: EventTemplate = {
content: '',
created_at: Math.floor(Date.now() / 1000),
kind: ProfileBadgesKind,
tags: [],
}
const event = finalizeEvent(eventTemplate, sk)
const isValid = validateProfileBadgesEvent(event)
expect(isValid).toBe(false)
})