forked from FlowwStar/FlowStar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstellar.test.ts
More file actions
166 lines (138 loc) · 5.74 KB
/
Copy pathstellar.test.ts
File metadata and controls
166 lines (138 loc) · 5.74 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
import { describe, it, expect } from 'vitest'
import { explorerUrl, isValidStellarAddress } from '@/lib/stellar'
describe('isValidStellarAddress', () => {
it('accepts a valid ed25519 public key', () => {
expect(isValidStellarAddress('GBZXN7PIRZGNMHGA7MUUUF4GWPY5AYPV6LY4UV2GL6VJGIQRXFDNMADI')).toBe(
true,
)
})
it('rejects a correctly-shaped address with a bad checksum', () => {
// Same length and prefix as a real key, but the checksum is wrong.
expect(isValidStellarAddress('G' + 'A'.repeat(55))).toBe(false)
})
it('rejects an address that is too short', () => {
expect(isValidStellarAddress('GBZXN7PIRZGNMHGA7MUUUF4GWPY5AYPV6LY4UV2GL6VJGIQRXFDNMAD')).toBe(
false,
)
})
it('rejects an address that is too long', () => {
expect(isValidStellarAddress('GBZXN7PIRZGNMHGA7MUUUF4GWPY5AYPV6LY4UV2GL6VJGIQRXFDNMADIX')).toBe(
false,
)
})
it('rejects an address with the wrong prefix', () => {
expect(isValidStellarAddress('CDLZFC3SYJYDZT7K67VZ75HPJVIEUVNIXF47ZG2FB2RMQQVU2HHGCYSC')).toBe(
false,
)
})
it('rejects an empty string', () => {
expect(isValidStellarAddress('')).toBe(false)
})
it('trims surrounding whitespace before validating', () => {
expect(
isValidStellarAddress(' GBZXN7PIRZGNMHGA7MUUUF4GWPY5AYPV6LY4UV2GL6VJGIQRXFDNMADI '),
).toBe(true)
})
})
describe('explorerUrl', () => {
describe('testnet URL generation', () => {
it('generates correct URL for testnet account', () => {
const url = explorerUrl('testnet', 'account', 'GABC123')
expect(url).toBe('https://stellar.expert/explorer/testnet/account/GABC123')
})
it('generates correct URL for testnet contract', () => {
const url = explorerUrl('testnet', 'contract', 'CABC123')
expect(url).toBe('https://stellar.expert/explorer/testnet/contract/CABC123')
})
it('generates correct URL for testnet transaction', () => {
const url = explorerUrl('testnet', 'tx', 'abc123txhash')
expect(url).toBe('https://stellar.expert/explorer/testnet/tx/abc123txhash')
})
})
describe('mainnet URL generation', () => {
it('generates correct URL for mainnet account', () => {
const url = explorerUrl('mainnet', 'account', 'GXYZ789')
expect(url).toBe('https://stellar.expert/explorer/public/account/GXYZ789')
})
it('generates correct URL for mainnet contract', () => {
const url = explorerUrl('mainnet', 'contract', 'CXYZ789')
expect(url).toBe('https://stellar.expert/explorer/public/contract/CXYZ789')
})
it('generates correct URL for mainnet transaction', () => {
const url = explorerUrl('mainnet', 'tx', 'xyz789txhash')
expect(url).toBe('https://stellar.expert/explorer/public/tx/xyz789txhash')
})
})
describe('URL structure', () => {
it('includes stellar.expert domain', () => {
const url = explorerUrl('testnet', 'account', 'GABC')
expect(url).toContain('stellar.expert')
})
it('includes explorer path segment', () => {
const url = explorerUrl('testnet', 'account', 'GABC')
expect(url).toContain('/explorer/')
})
it('includes network segment (testnet or public)', () => {
const testnetUrl = explorerUrl('testnet', 'account', 'GABC')
const mainnetUrl = explorerUrl('mainnet', 'account', 'GXYZ')
expect(testnetUrl).toContain('/testnet/')
expect(mainnetUrl).toContain('/public/')
})
it('includes type segment', () => {
const accountUrl = explorerUrl('testnet', 'account', 'GABC')
const contractUrl = explorerUrl('testnet', 'contract', 'CABC')
const txUrl = explorerUrl('testnet', 'tx', 'hash123')
expect(accountUrl).toContain('/account/')
expect(contractUrl).toContain('/contract/')
expect(txUrl).toContain('/tx/')
})
it('ends with the provided ID', () => {
const id = 'GABC123XYZ'
const url = explorerUrl('testnet', 'account', id)
expect(url.endsWith(id)).toBe(true)
})
})
describe('all type combinations', () => {
const networks = ['testnet', 'mainnet'] as const
const types = ['account', 'contract', 'tx'] as const
const testId = 'TEST123'
it('generates valid URLs for all network and type combinations', () => {
for (const network of networks) {
for (const type of types) {
const url = explorerUrl(network, type, testId)
expect(url).toContain('https://stellar.expert/explorer/')
expect(url).toContain(network === 'testnet' ? '/testnet/' : '/public/')
expect(url).toContain(`/${type}/`)
expect(url.endsWith(testId)).toBe(true)
}
}
})
})
describe('special characters in ID', () => {
it('handles IDs with numbers', () => {
const url = explorerUrl('testnet', 'account', 'GABC123456789')
expect(url).toBe('https://stellar.expert/explorer/testnet/account/GABC123456789')
})
it('handles long transaction hashes', () => {
const longHash = 'a'.repeat(64)
const url = explorerUrl('testnet', 'tx', longHash)
expect(url).toBe(`https://stellar.expert/explorer/testnet/tx/${longHash}`)
})
it('handles IDs with uppercase letters', () => {
const url = explorerUrl('mainnet', 'account', 'GABCDEFGHIJKLMNOP')
expect(url).toBe('https://stellar.expert/explorer/public/account/GABCDEFGHIJKLMNOP')
})
})
describe('network mapping', () => {
it('maps testnet to testnet explorer path', () => {
const url = explorerUrl('testnet', 'account', 'GABC')
expect(url).not.toContain('/public/')
expect(url).toContain('/testnet/')
})
it('maps mainnet to public explorer path', () => {
const url = explorerUrl('mainnet', 'account', 'GABC')
expect(url).not.toContain('/testnet/')
expect(url).toContain('/public/')
})
})
})