-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgunsafe_core.js
108 lines (102 loc) · 3.45 KB
/
gunsafe_core.js
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
import '../index.js'
import hardwareid from 'hardwareid'
import { expect } from 'chai'
import Gun from 'gun'
let id = await hardwareid()
const gun = new Gun()
gun.gunsafe()
describe('Test all gunsafe methods', async () => {
it('A named gunsafe vault has been created', async () => {
await gun.gunsafe.name(id, 'Alice Doe')
let pair = gun.gunsafe.key()
expect(Object.keys(pair).length === 4).to.equal(true)
})
it('A named gunsafe vault has logged into gun successfuly', () => {
let authenticated = gun.user().is
expect(Object.keys(authenticated).length === 3).to.equal(true)
})
it('A single line record has been put to gunsafe', (done) => {
gun.gunsafe.put('hi', 'hi?')
done()
})
it('A multiline record has been put to gunsafe', (done) => {
let doc = {
[+new Date()]: 'hi?',
[+new Date()+1]: 'hello?',
}
gun.gunsafe.put('hi', doc)
done()
})
it('Adds a javascript function to gunsafe', (done) => {
gun.gunsafe.put('fn', 'let a = 1; let b = 2; global.value = a+b;')
done()
})
it('Gunsafe can get the single line record', () => {
gun.gunsafe.get('hi', false, false, data => {
expect(data === 'hi?').to.equal(true)
})
})
it('Gunsafe can get the multiline record', () => {
gun.gunsafe.get('hi', false, false, data => {
expect(global.value).to.equal(3)
})
})
it('Gunsafe can get and execute the function in global scope', (done) => {
gun.gunsafe.get('fn', true, false, data => {
expect(data === 'hi').to.equal(true)
done()
})
})
it('Gunsafe can get and execute the function in global scope', (done) => {
gun.gunsafe.get('fn', true, false, data => {
expect(data === 'hi').to.equal(true)
done()
})
})
it('Gunsafe can list the available record names', () => {
gun.gunsafe.list(false, data => {
expect(data === 'hi' || data === 'hello').to.equal(true)
})
})
it('Gunsafe can delete a record by name', () => {
gun.gunsafe.delete('hi')
gun.gunsafe.list(true, data => {
expect(data === '[ deleted ] hi' || data === '[ deleted ] hello').to.equal(true)
})
})
it('Gunsafe can delete all records', () => {
gun.gunsafe.delete()
gun.gunsafe.list(true, data => {
expect(data === '[ deleted ] hi').to.equal(true)
})
})
it('Gunsafe can list the deleted record names', () => {
gun.gunsafe.list(true, data => {
expect(data === '[ deleted ] hi' || data === '[ deleted ] hello').to.equal(true)
})
})
it('Gunsafe returns its keypair', () => {
let pair = gun.gunsafe.key()
expect(typeof pair === 'object' && Object.keys(pair).length === 4).to.equal(true)
})
it('Gunsafe returns a pairing key', async () => {
let pair = await gun.gunsafe.pair()
expect(typeof pair === 'string').to.equal(true)
})
it('Adds peers to gunsafe for p2p storage', async () => {
let peerHosts = ['https://relay.peer.ooo/gun', 'https://gunjs.herokueapp.com']
await gun.gunsafe.peers(peerHosts)
let peers = gun.gunsafe.peers()
let p = Object.keys(peers)
let same = p.sort().toString() == peerHosts.sort().toString()
expect(same).to.equal(true)
})
it('Creates a 2nd gunsafe instance and pairs the 1st with the 2nd', async ()=>{
await gun.gunsafe.name(id, 'Bob Doe')
let aliceKey = await gun.gunsafe.key()
let pair = await gun.gunsafe.pair()
gun.gunsafe.pair(pair)
let bobKey = await gun.gunsafe.key()
expect(aliceKey === bobKey).to.equal(true)
})
})