github.com/holochain/holochain-proto@v0.1.0-alpha-26.0.20200915073418-5c83169c9b5b/nucleus_test.go (about)

     1  package holochain
     2  
     3  import (
     4  	"fmt"
     5  	. "github.com/smartystreets/goconvey/convey"
     6  	"os"
     7  	"testing"
     8  )
     9  
    10  func TestNewNucleus(t *testing.T) {
    11  	d := SetupTestDir()
    12  	defer CleanupTestDir(d)
    13  	var h Holochain
    14  	h.rootPath = d
    15  	os.MkdirAll(h.DBPath(), os.ModePerm)
    16  
    17  	nucleus := NewNucleus(&h, &DNA{})
    18  	Convey("It should initialize the Nucleus struct", t, func() {
    19  		So(nucleus.h, ShouldEqual, &h)
    20  		So(nucleus.alog, ShouldEqual, &h.Config.Loggers.App)
    21  	})
    22  }
    23  
    24  func TestAppMessages(t *testing.T) {
    25  	d, _, h := PrepareTestChain("test")
    26  	defer CleanupTestChain(h, d)
    27  
    28  	// no need to activate DHT protocols for this test
    29  	h.Config.PeerModeDHTNode = false
    30  
    31  	if err := h.Activate(); err != nil {
    32  		panic(err)
    33  	}
    34  	Convey("it should fail on incorrect body types", t, func() {
    35  		msg := h.node.NewMessage(APP_MESSAGE, GetReq{})
    36  		_, err := h.Send(h.node.ctx, ActionProtocol, h.node.HashAddr, msg, 0)
    37  		So(err.Error(), ShouldEqual, "Unexpected request body type 'holochain.GetReq' in send request, expecting holochain.AppMsg")
    38  	})
    39  
    40  	Convey("it should fail on unknown zomes", t, func() {
    41  		msg := h.node.NewMessage(APP_MESSAGE, AppMsg{ZomeType: "foo"})
    42  		_, err := h.Send(h.node.ctx, ActionProtocol, h.node.HashAddr, msg, 0)
    43  		So(err.Error(), ShouldEqual, "unknown zome: foo")
    44  	})
    45  
    46  	Convey("it should send and receive app messages", t, func() {
    47  		msg := h.node.NewMessage(APP_MESSAGE, AppMsg{ZomeType: "jsSampleZome", Body: `{"ping":"foobar"}`})
    48  		r, err := h.Send(h.node.ctx, ActionProtocol, h.node.HashAddr, msg, 0)
    49  		So(err, ShouldBeNil)
    50  		So(fmt.Sprintf("%v", r), ShouldEqual, `{jsSampleZome {"pong":"foobar"}}`)
    51  	})
    52  }
    53  
    54  func TestNewUUID(t *testing.T) {
    55  	var dna DNA
    56  	Convey("It should initialize dna's UUID", t, func() {
    57  		So(fmt.Sprintf("%v", dna.UUID), ShouldEqual, "00000000-0000-0000-0000-000000000000")
    58  		err := dna.NewUUID()
    59  		So(err, ShouldBeNil)
    60  		So(fmt.Sprintf("%v", dna.UUID), ShouldNotEqual, "00000000-0000-0000-0000-000000000000")
    61  	})
    62  }