package main

import (
	"testing"
)

func TestChatState(t *testing.T) {
	openID := "id"
	procedure := "getUserBasicProfile"
	s := createEmptyState(openID, procedure, 100)
	s.Name = "waiting for username"
	s.Data = map[string]string{
		"txt":  "What is your date of birth?",
		"icon": "/mnt/data/abc.jpg",
	}
	s.response = "somexml less than 2048 bytes"

	//save
	err := s.Save()
	AssertEqual(t, err, nil, "save state should be successful")

	//read out
	m := chatState{}
	m.Load(openID, procedure)

	//compare
	AssertEqual(t, m.Name, s.Name, "Name should be equal")
	AssertEqual(t, m.Expire, s.Expire, "Expire should be equal")
	AssertEqual(t, m.Data["txt"], s.Data["txt"], "Message[txt] should be equal")
	AssertEqual(t, m.Data["icon"], s.Data["icon"], "Message[icon] should be equal")
	AssertEqual(t, m.OpenID, openID, "openID should be "+openID)
	AssertEqual(t, m.response, "", "response should be empty")
	AssertEqual(t, m.Procedure, procedure, "procedure should be "+procedure)
	AssertEqual(t, m.Data["txt"], s.Data["txt"], "Message[txt] should be equal")
	AssertEqual(t, m.Data["icon"], s.Data["icon"], "Message[icon] should be equal")

	err = s.Delete()
	AssertEqual(t, err, nil, "delete chatState should be good")

}
