forked from Disciplr-Org/Disciplr-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstellarSdkRegression.test.ts
More file actions
447 lines (374 loc) · 16.6 KB
/
Copy pathstellarSdkRegression.test.ts
File metadata and controls
447 lines (374 loc) · 16.6 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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
import { describe, it, expect, beforeAll } from '@jest/globals'
// SDK exports loaded dynamically to capture import errors
let Keypair: unknown
let Contract: unknown
let TransactionBuilder: unknown
let Account: unknown
let nativeToScVal: unknown
let scValToNative: unknown
let xdr: unknown
let rpc: unknown
let Networks: unknown
let BASE_FEE: unknown
let StrKey: unknown
let Address: unknown
// Test constants (initialized after SDK import)
let VALID_STELLAR_PUBLIC_KEY: string
let VALID_SECRET_KEY: string
let VALID_CONTRACT_ID: string
const TEST_NETWORK = 'Test SDF Network ; September 2015'
beforeAll(async () => {
const sdk = await import('@stellar/stellar-sdk')
Keypair = sdk.Keypair
Contract = sdk.Contract
TransactionBuilder = sdk.TransactionBuilder
Account = sdk.Account
nativeToScVal = sdk.nativeToScVal
scValToNative = sdk.scValToNative
xdr = sdk.xdr
rpc = sdk.rpc
Networks = sdk.Networks
BASE_FEE = sdk.BASE_FEE
StrKey = sdk.StrKey
Address = sdk.Address
// Generate valid test addresses from keypairs
const testKeypair = (Keypair as any).random()
VALID_STELLAR_PUBLIC_KEY = testKeypair.publicKey()
VALID_SECRET_KEY = testKeypair.secret()
// Generate a valid contract ID using StrKey.encodeContract
const contractBuffer = Buffer.alloc(32, 1)
VALID_CONTRACT_ID = (StrKey as any).encodeContract(contractBuffer)
})
describe('Stellar SDK v14 regression suite', () => {
describe('Symbol availability', () => {
it('exports the symbols required by src/services/soroban.ts', () => {
expect(typeof Keypair).toBe('function')
expect(typeof Contract).toBe('function')
expect(typeof TransactionBuilder).toBe('function')
expect(typeof Account).toBe('function')
expect(typeof nativeToScVal).toBe('function')
expect(typeof rpc).toBe('object')
expect(rpc).not.toBeNull()
expect(typeof (rpc as any)?.Server).toBe('function')
expect(typeof Networks).toBe('object')
expect(Networks).not.toBeNull()
// BASE_FEE is a string in SDK v14
expect(typeof BASE_FEE).toBe('string')
expect(BASE_FEE).not.toBe('')
})
it('exports the XDR helpers required by src/services/eventParser.ts', () => {
expect(typeof xdr).toBe('object')
expect(xdr).not.toBeNull()
expect(typeof (xdr as any).ScVal).toBe('function')
expect(typeof (xdr as any).ScVal.fromXDR).toBe('function')
expect(typeof scValToNative).toBe('function')
})
it('Keypair has the expected methods', () => {
expect(typeof (Keypair as any).fromSecret).toBe('function')
expect(typeof (Keypair as any).random).toBe('function')
})
it('Account constructor and methods are available', () => {
const account = new (Account as any)(VALID_STELLAR_PUBLIC_KEY, '1')
expect(account).toBeDefined()
expect(typeof account.accountId).toBeDefined()
expect(typeof account.sequenceNumber).toBeDefined()
})
})
describe('Contract API shape', () => {
it('Contract constructor and call method match usage in soroban.ts', () => {
const contract = new (Contract as any)(VALID_CONTRACT_ID)
expect(contract).toBeDefined()
expect(typeof contract.call).toBe('function')
})
it('Contract.call returns an operation with toXDR method', () => {
const contract = new (Contract as any)(VALID_CONTRACT_ID)
const scVal = (nativeToScVal as any)('test', { type: 'string' })
const operation = contract.call('create_vault', scVal)
expect(operation).toBeDefined()
expect(typeof (operation as any).toXDR).toBe('function')
})
it('Contract.call supports multiple arguments', () => {
const contract = new (Contract as any)(VALID_CONTRACT_ID)
const arg1 = (nativeToScVal as any)('vault-123', { type: 'string' })
const arg2 = (nativeToScVal as any)('100', { type: 'string' })
const arg3 = (nativeToScVal as any)('verifier', { type: 'string' })
const operation = contract.call('create_vault', arg1, arg2, arg3)
expect(operation).toBeDefined()
expect(typeof (operation as any).toXDR).toBe('function')
})
})
describe('TransactionBuilder API shape', () => {
it('TransactionBuilder constructor and base methods exist', () => {
const account = new (Account as any)(VALID_STELLAR_PUBLIC_KEY, '1')
const builder = new (TransactionBuilder as any)(account, {
fee: (BASE_FEE as any) * 2,
networkPassphrase: TEST_NETWORK,
})
expect(builder).toBeDefined()
expect(typeof builder.addOperation).toBe('function')
expect(typeof builder.setTimeout).toBe('function')
expect(typeof builder.build).toBe('function')
})
it('TransactionBuilder.addOperation accepts contract operations', () => {
const account = new (Account as any)(VALID_STELLAR_PUBLIC_KEY, '1')
const contract = new (Contract as any)(VALID_CONTRACT_ID)
const scVal = (nativeToScVal as any)('test', { type: 'string' })
const operation = contract.call('test_method', scVal)
const builder = new (TransactionBuilder as any)(account, {
fee: (BASE_FEE as any) * 2,
networkPassphrase: TEST_NETWORK,
})
const result = builder.addOperation(operation)
expect(result).toBeDefined()
expect(typeof result.setTimeout).toBe('function')
})
it('TransactionBuilder.build() returns a Transaction with toXDR', () => {
const account = new (Account as any)(VALID_STELLAR_PUBLIC_KEY, '1')
const contract = new (Contract as any)(VALID_CONTRACT_ID)
const scVal = (nativeToScVal as any)('test', { type: 'string' })
const operation = contract.call('test_method', scVal)
const tx = new (TransactionBuilder as any)(account, {
fee: (BASE_FEE as any) * 2,
networkPassphrase: TEST_NETWORK,
})
.addOperation(operation)
.setTimeout(30)
.build()
expect(tx).toBeDefined()
expect(typeof tx.toXDR).toBe('function')
expect(typeof tx.sign).toBe('function')
expect(typeof (tx as any).operations).toBeDefined()
expect(Array.isArray((tx as any).operations)).toBe(true)
})
it('Transaction.sign() accepts Keypair', () => {
const account = new (Account as any)(VALID_STELLAR_PUBLIC_KEY, '1')
const contract = new (Contract as any)(VALID_CONTRACT_ID)
const scVal = (nativeToScVal as any)('test', { type: 'string' })
const operation = contract.call('test_method', scVal)
const tx = new (TransactionBuilder as any)(account, {
fee: (BASE_FEE as any) * 2,
networkPassphrase: TEST_NETWORK,
})
.addOperation(operation)
.setTimeout(30)
.build()
const keypair = (Keypair as any).random()
expect(typeof tx.sign).toBe('function')
tx.sign(keypair) // Should not throw
expect((tx as any).signatures.length).toBeGreaterThan(0)
})
})
describe('XDR conversion shape', () => {
it('nativeToScVal handles string type', () => {
const native = 'test-string'
const scVal = (nativeToScVal as any)(native, { type: 'string' })
expect(scVal).toBeDefined()
expect(typeof scVal.toXDR).toBe('function')
})
it('nativeToScVal handles i128 type', () => {
const scVal = (nativeToScVal as any)('100', { type: 'i128' })
expect(scVal).toBeDefined()
expect(typeof scVal.toXDR).toBe('function')
})
it('nativeToScVal handles u128 type', () => {
const scVal = (nativeToScVal as any)('200', { type: 'u128' })
expect(scVal).toBeDefined()
expect(typeof scVal.toXDR).toBe('function')
})
it('xdr.ScVal.fromXDR accepts base64-encoded XDR data', () => {
const native = 'test-data'
const scVal = (nativeToScVal as any)(native, { type: 'string' })
const encoded = scVal.toXDR('base64')
expect(typeof encoded).toBe('string')
expect(encoded.length).toBeGreaterThan(0)
const decoded = (xdr as any).ScVal.fromXDR(encoded, 'base64')
expect(decoded).toBeDefined()
})
it('scValToNative reverses nativeToScVal roundtrip for strings', () => {
const native = 'roundtrip-test'
const scVal = (nativeToScVal as any)(native, { type: 'string' })
const encoded = scVal.toXDR('base64')
const decoded = (xdr as any).ScVal.fromXDR(encoded, 'base64')
const roundtripped = (scValToNative as any)(decoded)
expect(roundtripped).toBe(native)
})
it('scValToNative roundtrip preserves numeric strings in i128', () => {
const native = '12345'
const scVal = (nativeToScVal as any)(native, { type: 'i128' })
const encoded = scVal.toXDR('base64')
const decoded = (xdr as any).ScVal.fromXDR(encoded, 'base64')
const roundtripped = (scValToNative as any)(decoded)
// i128 values roundtrip as BigInt or number, not as string
expect(roundtripped).toBeDefined()
expect(roundtripped.toString()).toBe(native)
})
it('scValToNative roundtrip preserves large numbers in u128', () => {
const native = '999999999999999999999'
const scVal = (nativeToScVal as any)(native, { type: 'u128' })
const encoded = scVal.toXDR('base64')
const decoded = (xdr as any).ScVal.fromXDR(encoded, 'base64')
const roundtripped = (scValToNative as any)(decoded)
// u128 values roundtrip as BigInt or number, not as string
expect(roundtripped).toBeDefined()
expect(roundtripped.toString()).toBe(native)
})
})
describe('rpc.Server API shape', () => {
it('rpc.Server constructor accepts a URL without making network calls', () => {
const server = new (rpc as any).Server('https://example.com')
expect(server).toBeDefined()
})
it('rpc.Server exposes network methods required by soroban.ts', () => {
const server = new (rpc as any).Server('https://example.com')
expect(typeof server.getAccount).toBe('function')
expect(typeof server.prepareTransaction).toBe('function')
expect(typeof server.sendTransaction).toBe('function')
})
it('rpc.Server.getAccount returns a Promise', () => {
const server = new (rpc as any).Server('https://example.com')
const result = server.getAccount(VALID_STELLAR_PUBLIC_KEY)
expect(result).toBeDefined()
expect(typeof result.then).toBe('function')
expect(typeof result.catch).toBe('function')
})
it('rpc.Server.prepareTransaction returns a Promise', () => {
const server = new (rpc as any).Server('https://example.com')
const account = new (Account as any)(VALID_STELLAR_PUBLIC_KEY, '1')
const contract = new (Contract as any)(VALID_CONTRACT_ID)
const scVal = (nativeToScVal as any)('test', { type: 'string' })
const operation = contract.call('test_method', scVal)
const tx = new (TransactionBuilder as any)(account, {
fee: (BASE_FEE as any) * 2,
networkPassphrase: TEST_NETWORK,
})
.addOperation(operation)
.setTimeout(30)
.build()
const result = server.prepareTransaction(tx)
expect(result).toBeDefined()
expect(typeof result.then).toBe('function')
})
it('rpc.Server.sendTransaction returns a Promise', () => {
const server = new (rpc as any).Server('https://example.com')
const account = new (Account as any)(VALID_STELLAR_PUBLIC_KEY, '1')
const tx = new (TransactionBuilder as any)(account, {
fee: (BASE_FEE as any) * 2,
networkPassphrase: TEST_NETWORK,
})
.setTimeout(30)
.build()
const result = server.sendTransaction(tx)
expect(result).toBeDefined()
expect(typeof result.then).toBe('function')
})
it('rpc.Server.getTransaction returns a Promise', () => {
const server = new (rpc as any).Server('https://example.com')
const result = server.getTransaction('test-hash')
expect(result).toBeDefined()
expect(typeof result.then).toBe('function')
})
})
describe('Networks constant', () => {
it('Networks object contains network passphrases as strings', () => {
expect((Networks as any).TESTNET).toBeDefined()
expect(typeof (Networks as any).TESTNET).toBe('string')
expect((Networks as any).PUBLIC).toBeDefined()
expect(typeof (Networks as any).PUBLIC).toBe('string')
})
it('Network passphrases are non-empty strings', () => {
expect((Networks as any).TESTNET.length).toBeGreaterThan(0)
expect((Networks as any).PUBLIC.length).toBeGreaterThan(0)
})
})
describe('Keypair operations', () => {
it('Keypair.random() generates a valid keypair', () => {
const keypair = (Keypair as any).random()
expect(keypair).toBeDefined()
expect(typeof keypair.publicKey).toBe('function')
expect(typeof keypair.secret).toBe('function')
})
it('Keypair.fromSecret() reconstructs from secret key', () => {
const original = (Keypair as any).random()
const secret = original.secret()
const reconstructed = (Keypair as any).fromSecret(secret)
expect(original.publicKey()).toBe(reconstructed.publicKey())
})
it('Keypair methods return consistent results', () => {
const keypair = (Keypair as any).random()
const pubkey1 = keypair.publicKey()
const pubkey2 = keypair.publicKey()
expect(pubkey1).toBe(pubkey2)
})
})
describe('Integration: full submit flow shape', () => {
it('models the complete submission flow from soroban.ts', () => {
// Simulate: const server = new SorobanRpc.Server(config.rpcUrl)
const server = new (rpc as any).Server('https://example.com')
expect(server).toBeDefined()
// Simulate: const account = await server.getAccount(sourceAccount)
const getAccountResult = server.getAccount(VALID_STELLAR_PUBLIC_KEY)
expect(typeof getAccountResult.then).toBe('function')
// Simulate: const contract = new Contract(contractId)
const contract = new (Contract as any)(VALID_CONTRACT_ID)
expect(typeof contract.call).toBe('function')
// Simulate: const callOp = contract.call(...)
const arg1 = (nativeToScVal as any)('vault-id', { type: 'string' })
const arg2 = (nativeToScVal as any)('100', { type: 'string' })
const operation = contract.call('create_vault', arg1, arg2)
expect(operation).toBeDefined()
// Simulate: const tx = new TransactionBuilder(account, { fee, networkPassphrase })
const mockAccount = new (Account as any)(VALID_STELLAR_PUBLIC_KEY, '1')
const txBuilder = new (TransactionBuilder as any)(mockAccount, {
fee: (BASE_FEE as any) * 2,
networkPassphrase: TEST_NETWORK,
})
expect(typeof txBuilder.addOperation).toBe('function')
// Simulate: .addOperation(callOp).setTimeout(30).build()
const tx = txBuilder.addOperation(operation).setTimeout(30).build()
expect(typeof tx.toXDR).toBe('function')
expect(typeof tx.sign).toBe('function')
// Simulate: prepared.sign(keypair)
const keypair = (Keypair as any).random()
expect(typeof tx.sign).toBe('function')
tx.sign(keypair)
// Simulate: await server.prepareTransaction(prepared)
const prepareResult = server.prepareTransaction(tx)
expect(typeof prepareResult.then).toBe('function')
})
})
describe('Integration: XDR parsing flow from eventParser.ts', () => {
it('models the complete XDR parsing flow', () => {
// Simulate: const scVal = xdr.ScVal.fromXDR(xdrData, 'base64')
const original = 'test-event-data'
const scVal = (nativeToScVal as any)(original, { type: 'string' })
const xdrData = scVal.toXDR('base64')
const decoded = (xdr as any).ScVal.fromXDR(xdrData, 'base64')
expect(decoded).toBeDefined()
// Simulate: nativeVal = scValToNative(scVal)
const nativeVal = (scValToNative as any)(decoded)
expect(nativeVal).toBe(original)
})
it('handles roundtrip with i128 amounts (vault amount field)', () => {
const vaultAmount = '50000000000'
const scVal = (nativeToScVal as any)(vaultAmount, { type: 'i128' })
const xdrData = scVal.toXDR('base64')
const decoded = (xdr as any).ScVal.fromXDR(xdrData, 'base64')
const parsed = (scValToNative as any)(decoded)
// i128 roundtrips as BigInt or number, not string, so compare as strings
expect(parsed.toString()).toBe(vaultAmount)
})
})
describe('Error resilience', () => {
it('gracefully handles invalid base64 in fromXDR', () => {
expect(() => {
(xdr as any).ScVal.fromXDR('not-valid-base64-!@#$', 'base64')
}).toThrow()
})
it('handles empty XDR data', () => {
// Verify fromXDR validates input
expect(() => {
(xdr as any).ScVal.fromXDR('', 'base64')
}).toThrow()
})
})
})