forked from nbd-wtf/nostr-tools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nip75.test.ts
203 lines (184 loc) · 7.05 KB
/
nip75.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
import { describe, expect, it } from 'bun:test'
import { ZapGoal } from './kinds.ts'
import { Goal, generateGoalEventTemplate, validateZapGoalEvent } from './nip75.ts'
import { finalizeEvent, generateSecretKey } from './pure.ts'
describe('Goal Type', () => {
it('should create a proper Goal object', () => {
const goal: Goal = {
content: 'Fundraising for a new project',
amount: '100000000',
relays: ['wss://relay1.example.com', 'wss://relay2.example.com'],
closedAt: 1671150419,
image: 'https://example.com/goal-image.jpg',
summary: 'Help us reach our fundraising goal!',
r: 'https://example.com/additional-info',
a: 'fef2a50f7d9d3d5a5f38ee761bc087ec16198d3f0140df6d1e8193abf7c2b146',
zapTags: [
['zap', 'beneficiary1'],
['zap', 'beneficiary2'],
],
}
expect(goal.content).toBe('Fundraising for a new project')
expect(goal.amount).toBe('100000000')
expect(goal.relays).toEqual(['wss://relay1.example.com', 'wss://relay2.example.com'])
expect(goal.closedAt).toBe(1671150419)
expect(goal.image).toBe('https://example.com/goal-image.jpg')
expect(goal.summary).toBe('Help us reach our fundraising goal!')
expect(goal.r).toBe('https://example.com/additional-info')
expect(goal.a).toBe('fef2a50f7d9d3d5a5f38ee761bc087ec16198d3f0140df6d1e8193abf7c2b146')
expect(goal.zapTags).toEqual([
['zap', 'beneficiary1'],
['zap', 'beneficiary2'],
])
})
})
describe('generateGoalEventTemplate', () => {
it('should generate an EventTemplate for a fundraising goal', () => {
const goal: Goal = {
content: 'Fundraising for a new project',
amount: '100000000',
relays: ['wss://relay1.example.com', 'wss://relay2.example.com'],
closedAt: 1671150419,
image: 'https://example.com/goal-image.jpg',
summary: 'Help us reach our fundraising goal!',
r: 'https://example.com/additional-info',
zapTags: [
['zap', 'beneficiary1'],
['zap', 'beneficiary2'],
],
}
const eventTemplate = generateGoalEventTemplate(goal)
expect(eventTemplate.kind).toBe(ZapGoal)
expect(eventTemplate.content).toBe('Fundraising for a new project')
expect(eventTemplate.tags).toEqual([
['amount', '100000000'],
['relays', 'wss://relay1.example.com', 'wss://relay2.example.com'],
['closed_at', '1671150419'],
['image', 'https://example.com/goal-image.jpg'],
['summary', 'Help us reach our fundraising goal!'],
['r', 'https://example.com/additional-info'],
['zap', 'beneficiary1'],
['zap', 'beneficiary2'],
])
})
it('should generate an EventTemplate for a fundraising goal without optional properties', () => {
const goal: Goal = {
content: 'Fundraising for a new project',
amount: '100000000',
relays: ['wss://relay1.example.com', 'wss://relay2.example.com'],
}
const eventTemplate = generateGoalEventTemplate(goal)
expect(eventTemplate.kind).toBe(ZapGoal)
expect(eventTemplate.content).toBe('Fundraising for a new project')
expect(eventTemplate.tags).toEqual([
['amount', '100000000'],
['relays', 'wss://relay1.example.com', 'wss://relay2.example.com'],
])
})
it('should generate an EventTemplate that is valid', () => {
const sk = generateSecretKey()
const goal: Goal = {
content: 'Fundraising for a new project',
amount: '100000000',
relays: ['wss://relay1.example.com', 'wss://relay2.example.com'],
closedAt: 1671150419,
image: 'https://example.com/goal-image.jpg',
summary: 'Help us reach our fundraising goal!',
r: 'https://example.com/additional-info',
zapTags: [
['zap', 'beneficiary1'],
['zap', 'beneficiary2'],
],
}
const eventTemplate = generateGoalEventTemplate(goal)
const event = finalizeEvent(eventTemplate, sk)
const isValid = validateZapGoalEvent(event)
expect(isValid).toBe(true)
})
})
describe('validateZapGoalEvent', () => {
it('should validate a proper Goal event', () => {
const sk = generateSecretKey()
const eventTemplate = {
created_at: Math.floor(Date.now() / 1000),
kind: ZapGoal,
content: 'Fundraising for a new project',
tags: [
['amount', '100000000'],
['relays', 'wss://relay1.example.com', 'wss://relay2.example.com'],
['closed_at', '1671150419'],
['image', 'https://example.com/goal-image.jpg'],
['summary', 'Help us reach our fundraising goal!'],
['r', 'https://example.com/additional-info'],
['zap', 'beneficiary1'],
['zap', 'beneficiary2'],
],
}
const event = finalizeEvent(eventTemplate, sk)
const isValid = validateZapGoalEvent(event)
expect(isValid).toBe(true)
})
it('should not validate an event with an incorrect kind', () => {
const sk = generateSecretKey()
const eventTemplate = {
created_at: Math.floor(Date.now() / 1000),
kind: 0, // Incorrect kind
content: 'Fundraising for a new project',
tags: [
['amount', '100000000'],
['relays', 'wss://relay1.example.com', 'wss://relay2.example.com'],
['closed_at', '1671150419'],
['image', 'https://example.com/goal-image.jpg'],
['summary', 'Help us reach our fundraising goal!'],
['r', 'https://example.com/additional-info'],
['zap', 'beneficiary1'],
['zap', 'beneficiary2'],
],
}
const event = finalizeEvent(eventTemplate, sk)
const isValid = validateZapGoalEvent(event)
expect(isValid).toBe(false)
})
it('should not validate an event with missing required "amount" tag', () => {
const sk = generateSecretKey()
const eventTemplate = {
created_at: Math.floor(Date.now() / 1000),
kind: ZapGoal,
content: 'Fundraising for a new project',
tags: [
// Missing "amount" tag
['relays', 'wss://relay1.example.com', 'wss://relay2.example.com'],
['closed_at', '1671150419'],
['image', 'https://example.com/goal-image.jpg'],
['summary', 'Help us reach our fundraising goal!'],
['r', 'https://example.com/additional-info'],
['zap', 'beneficiary1'],
['zap', 'beneficiary2'],
],
}
const event = finalizeEvent(eventTemplate, sk)
const isValid = validateZapGoalEvent(event)
expect(isValid).toBe(false)
})
it('should not validate an event with missing required "relays" tag', () => {
const sk = generateSecretKey()
const eventTemplate = {
created_at: Math.floor(Date.now() / 1000),
kind: ZapGoal,
content: 'Fundraising for a new project',
tags: [
['amount', '100000000'],
// Missing "relays" tag
['closed_at', '1671150419'],
['image', 'https://example.com/goal-image.jpg'],
['summary', 'Help us reach our fundraising goal!'],
['r', 'https://example.com/additional-info'],
['zap', 'beneficiary1'],
['zap', 'beneficiary2'],
],
}
const event = finalizeEvent(eventTemplate, sk)
const isValid = validateZapGoalEvent(event)
expect(isValid).toBe(false)
})
})