package peer

import (
	"path/filepath"
	"reflect"
	"testing"
)

func TestFilePaths(t *testing.T) {
	confDir := configDir("netName")
	if filepath.Base(confDir) != "netName" {
		t.Fatal(confDir)
	}
	if filepath.Base(filepath.Dir(confDir)) != ".vppn" {
		t.Fatal(confDir)
	}

	path := peerConfigPath("netName")
	if path != filepath.Join(confDir, "config.json") {
		t.Fatal(path)
	}

	path = peerStatePath("netName")
	if path != filepath.Join(confDir, "state.json") {
		t.Fatal(path)
	}
}

func TestStoreLoadJson(t *testing.T) {
	type Object struct {
		Name  string
		Age   int
		Price float64
	}

	tmpDir := t.TempDir()
	outPath := filepath.Join(tmpDir, "object.json")

	obj := Object{
		Name:  "Jason",
		Age:   22,
		Price: 123.534,
	}

	if err := storeJson(obj, outPath); err != nil {
		t.Fatal(err)
	}

	obj2 := Object{}
	if err := loadJson(outPath, &obj2); err != nil {
		t.Fatal(err)
	}

	if !reflect.DeepEqual(obj, obj2) {
		t.Fatal(obj, obj2)
	}
}
