-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmint-handler.test.ts
More file actions
412 lines (358 loc) · 11.8 KB
/
Copy pathmint-handler.test.ts
File metadata and controls
412 lines (358 loc) · 11.8 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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/* eslint-disable test/no-import-node-test */
import type { InitData } from '@telegram-apps/init-data-node'
import type { MintHandlerDependencies, MintUser } from '#root/backend/mint-handler'
import assert from 'node:assert/strict'
import test from 'node:test'
import fastify from 'fastify'
import { buildMintHandler } from '#root/backend/mint-handler'
const TRY_COST = 100n
interface PersistCall {
userId: number
image: string
description: string
}
interface MintTestContext {
app: ReturnType<typeof fastify>
persistCalls: PersistCall[]
generateImageCalls: MintUser[]
generateDescriptionCalls: MintUser[]
debitCalls: number[]
refundCalls: number[]
submitCalls: number[]
notifyCalls: MintUser[]
}
function baseUser(overrides: Partial<MintUser> = {}): MintUser {
return {
id: 1001,
votes: 500n,
state: 'WaitNothing',
minted: false,
avatar: '/data/alice/source.png',
wallet: 'EQalice',
name: 'alice',
...overrides,
}
}
async function createContext(
user: MintUser | null,
overrides: Partial<MintHandlerDependencies> = {},
): Promise<MintTestContext> {
const persistCalls: PersistCall[] = []
const generateImageCalls: MintUser[] = []
const generateDescriptionCalls: MintUser[] = []
const debitCalls: number[] = []
const refundCalls: number[] = []
const submitCalls: number[] = []
const notifyCalls: MintUser[] = []
const dependencies: MintHandlerDependencies = {
validateInitData: () => {},
parseInitData: () => ({ user: { id: 1001 } } as InitData),
findMintUser: async () => user,
tryCost: () => TRY_COST,
debitTry: async (userId) => {
debitCalls.push(userId)
const votes = user?.votes ?? 0n
return votes >= TRY_COST ? votes - TRY_COST : null
},
refundTry: async (userId) => {
refundCalls.push(userId)
},
generateImage: async (u) => {
generateImageCalls.push(u)
return '/data/alice/alice_0.png'
},
generateDescription: async (u) => {
generateDescriptionCalls.push(u)
return 'An inspiring journey begins.'
},
persistDraft: async (userId, image, description) => {
persistCalls.push({ userId, image, description })
},
submitDraft: async (userId) => {
submitCalls.push(userId)
},
notifyAdmins: async (u) => {
notifyCalls.push(u)
},
readImageDataUrl: async (path) => `data:image/png;base64,${path}`,
logError: () => {},
...overrides,
}
const app = fastify()
await app.register(buildMintHandler(dependencies), { prefix: '/api/mint' })
return {
app,
persistCalls,
generateImageCalls,
generateDescriptionCalls,
debitCalls,
refundCalls,
submitCalls,
notifyCalls,
}
}
// QUOTE — price + affordability
test('POST /quote returns the try cost and canAfford=true when covered', async (t) => {
const ctx = await createContext(baseUser({ votes: 100n }))
t.after(() => ctx.app.close())
const res = await ctx.app.inject({
method: 'POST',
url: '/api/mint/quote',
payload: { initData: 'signed' },
})
const body = res.json()
assert.equal(body.tryCost, '100')
assert.equal(body.yourVotes, '100')
assert.equal(body.canAfford, true)
assert.equal(body.minted, false)
})
test('POST /quote returns canAfford=false below the try cost', async (t) => {
const ctx = await createContext(baseUser({ votes: 99n }))
t.after(() => ctx.app.close())
const res = await ctx.app.inject({
method: 'POST',
url: '/api/mint/quote',
payload: { initData: 'signed' },
})
assert.equal(res.json().canAfford, false)
})
test('POST /quote returns User not found for an unknown user', async (t) => {
const ctx = await createContext(null)
t.after(() => ctx.app.close())
const res = await ctx.app.inject({
method: 'POST',
url: '/api/mint/quote',
payload: { initData: 'signed' },
})
assert.equal(res.json().error, 'User not found')
})
// GENERATE — paid try: debit → generate → persist (no state flip to review)
test('POST /generate debits one try, generates, persists, and returns the new balance', async (t) => {
const ctx = await createContext(baseUser({ votes: 500n }))
t.after(() => ctx.app.close())
const res = await ctx.app.inject({
method: 'POST',
url: '/api/mint/generate',
payload: { initData: 'signed' },
})
const body = res.json()
assert.deepEqual(ctx.debitCalls, [1001], 'debited exactly once')
assert.equal(body.image, 'data:image/png;base64,/data/alice/alice_0.png')
assert.equal(body.description, 'An inspiring journey begins.')
assert.equal(body.yourVotes, '400', 'balance after the paid try')
assert.equal(ctx.generateImageCalls.length, 1)
assert.equal(ctx.generateDescriptionCalls.length, 1)
assert.deepEqual(ctx.persistCalls, [
{
userId: 1001,
image: '/data/alice/alice_0.png',
description: 'An inspiring journey begins.',
},
])
assert.equal(ctx.refundCalls.length, 0, 'no refund on success')
})
test('POST /generate rejects when the balance does not cover a try', async (t) => {
const ctx = await createContext(baseUser({ votes: 99n }))
t.after(() => ctx.app.close())
const res = await ctx.app.inject({
method: 'POST',
url: '/api/mint/generate',
payload: { initData: 'signed' },
})
const body = res.json()
assert.equal(body.error, 'Not enough $CUBE')
assert.equal(body.tryCost, '100')
assert.equal(ctx.generateImageCalls.length, 0, 'no generation without funds')
assert.equal(ctx.persistCalls.length, 0)
})
test('POST /generate refunds the try when generation fails', async (t) => {
const ctx = await createContext(baseUser({ votes: 500n }), {
generateImage: async () => {
throw new Error('stability down')
},
})
t.after(() => ctx.app.close())
const res = await ctx.app.inject({
method: 'POST',
url: '/api/mint/generate',
payload: { initData: 'signed' },
})
assert.match(res.json().error, /refunded/i)
assert.deepEqual(ctx.debitCalls, [1001], 'debit happened')
assert.deepEqual(ctx.refundCalls, [1001], 'refund compensates the debit')
assert.equal(ctx.persistCalls.length, 0, 'nothing persisted on failure')
})
test('POST /generate refuses for an already-minted user without debiting', async (t) => {
const ctx = await createContext(baseUser({ minted: true }))
t.after(() => ctx.app.close())
const res = await ctx.app.inject({
method: 'POST',
url: '/api/mint/generate',
payload: { initData: 'signed' },
})
assert.equal(res.json().error, 'Already minted')
assert.equal(ctx.debitCalls.length, 0, 'no debit when minted')
})
test('POST /generate refuses while the draft is under review', async (t) => {
const ctx = await createContext(baseUser({ state: 'Submited' }))
t.after(() => ctx.app.close())
const res = await ctx.app.inject({
method: 'POST',
url: '/api/mint/generate',
payload: { initData: 'signed' },
})
assert.equal(res.json().error, 'Draft is under review')
assert.equal(ctx.debitCalls.length, 0)
})
test('POST /generate requires an avatar and does not debit without one', async (t) => {
const ctx = await createContext(baseUser({ avatar: undefined }))
t.after(() => ctx.app.close())
const res = await ctx.app.inject({
method: 'POST',
url: '/api/mint/generate',
payload: { initData: 'signed' },
})
assert.match(res.json().error, /avatar/i)
assert.equal(ctx.debitCalls.length, 0)
})
// SUBMIT — user liked the draft → Submited + admin push
test('POST /submit flips the draft to Submited and notifies admins', async (t) => {
const ctx = await createContext(
baseUser({ image: '/data/alice/alice_0.png', description: 'desc' }),
)
t.after(() => ctx.app.close())
const res = await ctx.app.inject({
method: 'POST',
url: '/api/mint/submit',
payload: { initData: 'signed' },
})
const body = res.json()
assert.equal(body.ok, true)
assert.equal(body.state, 'Submited')
assert.deepEqual(ctx.submitCalls, [1001])
assert.equal(ctx.notifyCalls.length, 1, 'admins notified once')
})
test('POST /submit succeeds even when the admin notification fails', async (t) => {
const ctx = await createContext(
baseUser({ image: '/data/alice/alice_0.png', description: 'desc' }),
{
notifyAdmins: async () => {
throw new Error('telegram down')
},
},
)
t.after(() => ctx.app.close())
const res = await ctx.app.inject({
method: 'POST',
url: '/api/mint/submit',
payload: { initData: 'signed' },
})
assert.equal(res.json().ok, true, 'submit is not failed by notify errors')
assert.deepEqual(ctx.submitCalls, [1001])
})
test('POST /submit requires a generated draft', async (t) => {
const ctx = await createContext(baseUser())
t.after(() => ctx.app.close())
const res = await ctx.app.inject({
method: 'POST',
url: '/api/mint/submit',
payload: { initData: 'signed' },
})
assert.match(res.json().error, /generate/i)
assert.equal(ctx.submitCalls.length, 0)
})
test('POST /submit requires a bound wallet', async (t) => {
const ctx = await createContext(
baseUser({
image: '/data/alice/alice_0.png',
description: 'desc',
wallet: undefined,
}),
)
t.after(() => ctx.app.close())
const res = await ctx.app.inject({
method: 'POST',
url: '/api/mint/submit',
payload: { initData: 'signed' },
})
assert.match(res.json().error, /wallet/i)
assert.equal(ctx.submitCalls.length, 0)
})
test('POST /submit rejects a repeat submission', async (t) => {
const ctx = await createContext(
baseUser({
state: 'Submited',
image: '/data/alice/alice_0.png',
description: 'desc',
}),
)
t.after(() => ctx.app.close())
const res = await ctx.app.inject({
method: 'POST',
url: '/api/mint/submit',
payload: { initData: 'signed' },
})
assert.equal(res.json().error, 'Already submitted')
assert.equal(ctx.submitCalls.length, 0)
})
// STATUS — flow snapshot for the webview
test('POST /status reports a declined (Rework) draft as regenerable', async (t) => {
const ctx = await createContext(
baseUser({
state: 'Rework',
image: '/data/alice/alice_0.png',
description: 'old',
}),
)
t.after(() => ctx.app.close())
const res = await ctx.app.inject({
method: 'POST',
url: '/api/mint/status',
payload: { initData: 'signed' },
})
const body = res.json()
assert.equal(body.state, 'Rework')
assert.equal(body.canGenerate, true, 'declined drafts can regenerate')
assert.equal(body.image, 'data:image/png;base64,/data/alice/alice_0.png')
assert.equal(body.description, 'old')
assert.equal(body.hasWallet, true)
})
test('POST /status blocks generation while under review and once minted', async (t) => {
const submitted = await createContext(baseUser({ state: 'Submited' }))
t.after(() => submitted.app.close())
const minted = await createContext(
baseUser({ minted: true, nftUrl: 'https://getgems.io/nft/abc' }),
)
t.after(() => minted.app.close())
const submittedBody = (
await submitted.app.inject({
method: 'POST',
url: '/api/mint/status',
payload: { initData: 'signed' },
})
).json()
assert.equal(submittedBody.canGenerate, false, 'review locks generation')
const mintedBody = (
await minted.app.inject({
method: 'POST',
url: '/api/mint/status',
payload: { initData: 'signed' },
})
).json()
assert.equal(mintedBody.canGenerate, false, 'minted locks generation')
assert.equal(mintedBody.nftUrl, 'https://getgems.io/nft/abc')
})
test('POST /status exposes cost, balance, and avatar preview', async (t) => {
const ctx = await createContext(baseUser({ votes: 250n }))
t.after(() => ctx.app.close())
const res = await ctx.app.inject({
method: 'POST',
url: '/api/mint/status',
payload: { initData: 'signed' },
})
const body = res.json()
assert.equal(body.tryCost, '100')
assert.equal(body.yourVotes, '250')
assert.equal(body.canAfford, true)
assert.equal(body.avatar, 'data:image/png;base64,/data/alice/source.png')
})