forked from holochain/holochain-proto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsnucleus_test.go
196 lines (173 loc) · 5.96 KB
/
jsnucleus_test.go
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
package holochain
import (
"fmt"
"github.com/robertkrimen/otto"
. "github.com/smartystreets/goconvey/convey"
"testing"
"time"
)
func TestNewJSNucleus(t *testing.T) {
Convey("new should create a nucleus", t, func() {
v, err := NewJSNucleus(nil, `1 + 1`)
So(err, ShouldBeNil)
z := v.(*JSNucleus)
i, _ := z.lastResult.ToInteger()
So(i, ShouldEqual, 2)
})
Convey("new fail to create nucleus when code is bad", t, func() {
v, err := NewJSNucleus(nil, "1+ )")
So(v, ShouldBeNil)
So(err.Error(), ShouldEqual, "JS exec error: (anonymous): Line 1:45 Unexpected token )")
})
Convey("should have the built in functions:", t, func() {
d, _, h := prepareTestChain("test")
defer cleanupTestDir(d)
v, err := NewJSNucleus(h, "")
So(err, ShouldBeNil)
z := v.(*JSNucleus)
Convey("version", func() {
_, err = z.Run("version")
So(err, ShouldBeNil)
s, _ := z.lastResult.ToString()
So(s, ShouldEqual, "0.0.1")
})
Convey("property", func() {
_, err = z.Run(`property("description")`)
So(err, ShouldBeNil)
s, _ := z.lastResult.ToString()
So(s, ShouldEqual, "a bogus test holochain")
_, err = z.Run(`property("` + ID_PROPERTY + `")`)
So(err, ShouldBeNil)
id, _ := h.ID()
So(z.lastResult.String(), ShouldEqual, id.String())
_, err = z.Run(`property("` + AGENT_ID_PROPERTY + `")`)
So(err, ShouldBeNil)
aid := string(h.Agent().ID())
So(z.lastResult.String(), ShouldEqual, aid)
})
})
}
func TestJSInit(t *testing.T) {
Convey("it should fail if the init function returns false", t, func() {
z, _ := NewJSNucleus(nil, `function init() {return false}`)
err := z.InitChain()
So(err.Error(), ShouldEqual, "init failed")
})
Convey("it should work if the init function returns true", t, func() {
z, _ := NewJSNucleus(nil, `function init() {return true}`)
err := z.InitChain()
So(err, ShouldBeNil)
})
}
func TestJSValidateEntry(t *testing.T) {
Convey("should run an entry value against the defined validator for string data", t, func() {
v, err := NewJSNucleus(nil, `function validate(name,entry) { return (entry=="fish")};`)
So(err, ShouldBeNil)
d := EntryDef{Name: "myData", DataFormat: "string"}
err = v.ValidateEntry(&d, "cow")
So(err.Error(), ShouldEqual, "Invalid entry: cow")
err = v.ValidateEntry(&d, "fish")
So(err, ShouldBeNil)
})
Convey("should run an entry value against the defined validator for js data", t, func() {
v, err := NewJSNucleus(nil, `function validate(name,entry) { return (entry=="fish")};`)
d := EntryDef{Name: "myData", DataFormat: "js"}
err = v.ValidateEntry(&d, "\"cow\"")
So(err.Error(), ShouldEqual, "Invalid entry: \"cow\"")
err = v.ValidateEntry(&d, "\"fish\"")
So(err, ShouldBeNil)
})
Convey("should run an entry value against the defined validator for json data", t, func() {
v, err := NewJSNucleus(nil, `function validate(name,entry) { return (entry.data=="fish")};`)
d := EntryDef{Name: "myData", DataFormat: "JSON"}
err = v.ValidateEntry(&d, `{"data":"cow"}`)
So(err.Error(), ShouldEqual, `Invalid entry: {"data":"cow"}`)
err = v.ValidateEntry(&d, `{"data":"fish"}`)
So(err, ShouldBeNil)
})
}
func TestJSExposeCall(t *testing.T) {
var z *JSNucleus
Convey("should run", t, func() {
v, err := NewJSNucleus(nil, `
expose("cater",HC.STRING);
function cater(x) {return "result: "+x};
expose("adder",HC.STRING);
function adder(x){ return parseInt(x)+2};
expose("jtest",HC.JSON);
function jtest(x){ x.output = x.input*2; return x;};
`)
So(err, ShouldBeNil)
z = v.(*JSNucleus)
})
Convey("should build up interfaces list", t, func() {
i := z.Interfaces()
So(fmt.Sprintf("%v", i), ShouldEqual, "[{cater 0} {adder 0} {jtest 1}]")
})
Convey("should allow calling exposed STRING based functions", t, func() {
result, err := z.Call("cater", "fish \"zippy\"")
So(err, ShouldBeNil)
So(result.(string), ShouldEqual, "result: fish \"zippy\"")
result, err = z.Call("adder", "10")
So(err, ShouldBeNil)
So(result.(string), ShouldEqual, "12")
})
Convey("should allow calling exposed JSON based functions", t, func() {
result, err := z.Call("jtest", `{"input": 2}`)
So(err, ShouldBeNil)
So(result.(string), ShouldEqual, `{"input":2,"output":4}`)
})
}
func TestJSDHT(t *testing.T) {
d, _, h := prepareTestChain("test")
defer cleanupTestDir(d)
data := "7"
// add an entry onto the chain
now := time.Unix(1, 1) // pick a constant time so the test will always work
e := GobEntry{C: data}
_, hd, err := h.NewEntry(now, "jsData", &e)
if err != nil {
panic(err)
}
hash := hd.EntryLink
Convey("it should have a put function", t, func() {
v, err := NewJSNucleus(h, fmt.Sprintf(`put("%s");`, hash.String()))
So(err, ShouldBeNil)
z := v.(*JSNucleus)
So(err, ShouldBeNil)
So(z.lastResult.String(), ShouldEqual, otto.UndefinedValue().String())
})
Convey("it should have a get function", t, func() {
v, err := NewJSNucleus(h, fmt.Sprintf(`get ("%s");`, hash.String()))
So(err, ShouldBeNil)
z := v.(*JSNucleus)
So(z.lastResult.String(), ShouldEqual, "HolochainError: hash not found")
if err := h.dht.handlePutReqs(); err != nil {
panic(err)
}
v, err = NewJSNucleus(h, fmt.Sprintf(`get ("%s");`, hash.String()))
So(err, ShouldBeNil)
z = v.(*JSNucleus)
So(z.lastResult.String(), ShouldEqual, `"7"`)
})
e = GobEntry{C: "some meta data"}
_, mhd, _ := h.NewEntry(now, "myMetaData", &e)
metaHash := mhd.EntryLink
//b, _ := e.Marshal()
Convey("it should have a putmeta function", t, func() {
v, err := NewJSNucleus(h, fmt.Sprintf(`putmeta("%s","%s","myMetaType");`, hash.String(), metaHash.String()))
So(err, ShouldBeNil)
z := v.(*JSNucleus)
So(err, ShouldBeNil)
So(z.lastResult.String(), ShouldEqual, otto.UndefinedValue().String())
})
if err := h.dht.handlePutReqs(); err != nil {
panic(err)
}
Convey("it should have a getmeta function", t, func() {
v, err := NewJSNucleus(h, fmt.Sprintf(`getmeta("%s","myMetaType");`, hash.String()))
So(err, ShouldBeNil)
z := v.(*JSNucleus)
So(z.lastResult.String(), ShouldEqual, `[{"C":"some meta data"}]`)
})
}