-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
94 lines (75 loc) · 2.92 KB
/
test.js
File metadata and controls
94 lines (75 loc) · 2.92 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
const UsernameRegistry = require('./server')
const UsernameRegistryClient = require('./client')
const IdEnc = require('hypercore-id-encoding')
const HyperDHT = require('hyperdht')
const Corestore = require('corestore')
async function runTest() {
console.log('Starting Holepunch Username Registry Test...')
// Set up test DHT
const bootstrap = [{ host: '127.0.0.1', port: 30001 }]
let server = null
let client = null
try {
// Start server with unique storage path
console.log('\n1. Starting server...')
const serverStoragePath = `test-server-${Date.now()}`
server = new UsernameRegistry(serverStoragePath)
const { serverPublicKey, coreKey } = await server.initialize()
console.log(`Server public key: ${IdEnc.normalize(serverPublicKey)}`)
console.log(`Core key: ${IdEnc.normalize(coreKey)}`)
// Wait a bit for server to be ready
await new Promise(resolve => setTimeout(resolve, 2000))
// Start client with unique storage path
console.log('\n2. Starting client...')
const clientStoragePath = `test-client-${Date.now()}`
client = new UsernameRegistryClient(serverPublicKey, coreKey, clientStoragePath)
await client.initialize()
// Wait for connection
await new Promise(resolve => setTimeout(resolve, 2000))
// Test username registration
console.log('\n3. Testing username registration...')
const testUsername = 'alice'
const testPublicKey = 'alice-public-key-123'
const registerResult = await client.registerUsername(testUsername, testPublicKey)
console.log('Register result:', registerResult)
// Test username lookup
console.log('\n4. Testing username lookup...')
const lookupResult = await client.lookupUsername(testUsername)
console.log('Lookup result:', lookupResult)
// Test listing usernames
console.log('\n5. Testing username listing...')
const listResult = await client.listUsernames()
console.log('List result:', listResult)
// Test username deletion
console.log('\n6. Testing username deletion...')
const deleteResult = await client.deleteUsername(testUsername, testPublicKey)
console.log('Delete result:', deleteResult)
// Verify deletion
console.log('\n7. Verifying deletion...')
const verifyResult = await client.lookupUsername(testUsername)
console.log('Verify result:', verifyResult)
console.log('\n✅ Test completed successfully!')
} catch (error) {
console.error('❌ Test failed:', error)
} finally {
// Ensure cleanup happens even if there's an error
try {
if (client) {
await client.destroy()
}
} catch (cleanupError) {
console.error('Error cleaning up client:', cleanupError.message)
}
try {
if (server) {
await server.destroy()
}
} catch (cleanupError) {
console.error('Error cleaning up server:', cleanupError.message)
}
process.exit(0)
}
}
if (require.main === module) {
runTest()
}