Compare commits
2 Commits
221db4e998
...
afe98b8695
Author | SHA1 | Date | |
---|---|---|---|
afe98b8695 | |||
ece80b31f6 |
88
enginetest/engine.go
Normal file
88
enginetest/engine.go
Normal file
@ -0,0 +1,88 @@
|
||||
package enginetest
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"git.defalsify.org/vise.git/engine"
|
||||
"git.defalsify.org/vise.git/logging"
|
||||
"git.defalsify.org/vise.git/persist"
|
||||
"git.defalsify.org/vise.git/resource"
|
||||
"git.grassecon.net/urdt/ussd/internal/handlers"
|
||||
"git.grassecon.net/urdt/ussd/internal/storage"
|
||||
)
|
||||
var (
|
||||
logg = logging.NewVanilla()
|
||||
scriptDir = path.Join("services", "registration")
|
||||
)
|
||||
|
||||
func TestEngine(sessionId string) (engine.Engine,*persist.Persister){
|
||||
ctx := context.Background()
|
||||
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
||||
pfp := path.Join(scriptDir, "pp.csv")
|
||||
|
||||
cfg := engine.Config{
|
||||
Root: "root",
|
||||
SessionId: sessionId,
|
||||
OutputSize: uint32(106),
|
||||
FlagCount: uint32(16),
|
||||
}
|
||||
|
||||
dbDir := ".test_state"
|
||||
resourceDir := scriptDir
|
||||
menuStorageService := storage.NewMenuStorageService(dbDir, resourceDir)
|
||||
|
||||
err := menuStorageService.EnsureDbDir()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
rs, err := menuStorageService.GetResource(ctx)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
pe, err := menuStorageService.GetPersister(ctx)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
userdatastore, err := menuStorageService.GetUserdataDb(ctx)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
dbResource, ok := rs.(*resource.DbResource)
|
||||
if !ok {
|
||||
fmt.Fprintf(os.Stderr, err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
lhs, err := handlers.NewLocalHandlerService(pfp, true, dbResource, cfg, rs)
|
||||
lhs.SetDataStore(&userdatastore)
|
||||
lhs.SetPersister(pe)
|
||||
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
hl, err := lhs.GetHandler()
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
en := lhs.GetEngine()
|
||||
en = en.WithFirst(hl.Init)
|
||||
|
||||
//en = en.WithDebug(nil)
|
||||
return en,pe
|
||||
|
||||
}
|
61
menu_traversal_test.go
Normal file
61
menu_traversal_test.go
Normal file
@ -0,0 +1,61 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"git.defalsify.org/vise.git/engine"
|
||||
|
||||
"git.grassecon.net/urdt/ussd/enginetest"
|
||||
)
|
||||
|
||||
type TestCase struct {
|
||||
Input []string `json:"input"`
|
||||
Expected string `json:"expected"`
|
||||
}
|
||||
|
||||
type UserRegistration struct {
|
||||
UserRegistration []TestCase `json:"user_registration"`
|
||||
}
|
||||
|
||||
type TestData struct {
|
||||
UserRegistration []TestCase `json:"user_registration"`
|
||||
PinCheck []TestCase `json:"pincheck"`
|
||||
}
|
||||
|
||||
func TestUserRegistration(t *testing.T) {
|
||||
en, pe := enginetest.TestEngine("session1234112")
|
||||
w := bytes.NewBuffer(nil)
|
||||
file, err := os.Open("test_data.json")
|
||||
if err != nil {
|
||||
fmt.Println("Error opening file:", err)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
var testData TestData
|
||||
decoder := json.NewDecoder(file)
|
||||
if err := decoder.Decode(&testData); err != nil {
|
||||
fmt.Println("Error decoding JSON:", err)
|
||||
return
|
||||
}
|
||||
|
||||
var inputBuilder strings.Builder
|
||||
for _, testCase := range testData.UserRegistration {
|
||||
inputBuilder.WriteString(strings.Join(testCase.Input, "\n") + "\n")
|
||||
}
|
||||
readers := bufio.NewReader(strings.NewReader(inputBuilder.String()))
|
||||
engine.Loop(context.Background(), en, readers, w, nil)
|
||||
st := pe.GetState()
|
||||
sym, _ := st.Where()
|
||||
b := w.Bytes()
|
||||
fmt.Println("Menu:",string(b))
|
||||
fmt.Println("Rendering symbol:", sym)
|
||||
|
||||
}
|
18
test_data.json
Normal file
18
test_data.json
Normal file
@ -0,0 +1,18 @@
|
||||
{
|
||||
"user_registration": [
|
||||
{
|
||||
"input": ["0", "0","1234","1234"],
|
||||
"expected": "Registration successful"
|
||||
}
|
||||
],
|
||||
"pincheck": [
|
||||
{
|
||||
"input": ["1234"],
|
||||
"expected": "PIN valid"
|
||||
},
|
||||
{
|
||||
"input": ["5678"],
|
||||
"expected": "PIN invalid"
|
||||
}
|
||||
]
|
||||
}
|
0
testdata/main.vis
vendored
Normal file
0
testdata/main.vis
vendored
Normal file
118
testdata/testdata.go
vendored
Normal file
118
testdata/testdata.go
vendored
Normal file
@ -0,0 +1,118 @@
|
||||
package testdata
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"git.defalsify.org/vise.git/db"
|
||||
fsdb "git.defalsify.org/vise.git/db/fs"
|
||||
"git.defalsify.org/vise.git/logging"
|
||||
"git.defalsify.org/vise.git/vm"
|
||||
testdataloader "github.com/peteole/testdata-loader"
|
||||
)
|
||||
|
||||
func outNew(sym string, b []byte, tpl string, data map[string]string) error {
|
||||
logg.Debugf("testdata out", "sym", sym)
|
||||
store.SetPrefix(db.DATATYPE_TEMPLATE)
|
||||
err := store.Put(ctx, []byte(sym), []byte(tpl))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
store.SetPrefix(db.DATATYPE_BIN)
|
||||
err = store.Put(ctx, []byte(sym), b)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
store.SetPrefix(db.DATATYPE_STATICLOAD)
|
||||
for k, v := range data {
|
||||
logg.Debugf("testdata out staticload", "sym", sym, "k", k, "v", v)
|
||||
err = store.Put(ctx, []byte(k), []byte(v))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var (
|
||||
ctx = context.Background()
|
||||
store = fsdb.NewFsDb()
|
||||
out = outNew
|
||||
logg = logging.NewVanilla().WithDomain("testdata")
|
||||
BaseDir = testdataloader.GetBasePath()
|
||||
DataDir = ""
|
||||
dirLock = false
|
||||
)
|
||||
|
||||
type genFunc func() error
|
||||
|
||||
func terms() error {
|
||||
b := []byte{}
|
||||
b = vm.NewLine(b, vm.MOUT, []string{"yes", "1"}, nil, nil)
|
||||
b = vm.NewLine(b, vm.MOUT, []string{"no", "2"}, nil, nil)
|
||||
b = vm.NewLine(b, vm.HALT, nil, nil, nil)
|
||||
b = vm.NewLine(b, vm.INCMP, []string{"create_pin", "1"}, nil, nil)
|
||||
b = vm.NewLine(b, vm.INCMP, []string{"quit", "2"}, nil, nil)
|
||||
tpl := "Do you agree to terms and conditions?"
|
||||
return out("terms", b, tpl, nil)
|
||||
}
|
||||
|
||||
func createPin() error {
|
||||
b := []byte{}
|
||||
b = vm.NewLine(b, vm.MOUT, []string{"exit", "0"}, nil, nil)
|
||||
b = vm.NewLine(b, vm.HALT, nil, nil, nil)
|
||||
b = vm.NewLine(b, vm.INCMP, []string{"0", "1"}, nil, nil)
|
||||
tpl := "create pin"
|
||||
return out("create_pin", b, tpl, nil)
|
||||
}
|
||||
|
||||
func quit() error {
|
||||
// b := []byte{}
|
||||
// b = vm.NewLine(b, vm.LOAD, []string{"quit"}, []byte{0x00}, nil)
|
||||
// //b = vm.NewLine(b, vm.RELOAD, []string{"quit"}, []byte{0x00}, nil)
|
||||
// b = vm.NewLine(b, vm.HALT, nil, nil, nil)
|
||||
|
||||
// return out("quit", b, "quit", nil)
|
||||
b := vm.NewLine(nil, vm.LOAD, []string{"quit"}, []byte{0x00}, nil)
|
||||
b = vm.NewLine(b, vm.RELOAD, []string{"quit"}, nil, nil)
|
||||
b = vm.NewLine(b, vm.HALT, nil, nil, nil)
|
||||
|
||||
fp := path.Join(DataDir, "nothing.bin")
|
||||
err := os.WriteFile(fp, b, 0600)
|
||||
return err
|
||||
}
|
||||
|
||||
func generate() error {
|
||||
err := os.MkdirAll(DataDir, 0755)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
store = fsdb.NewFsDb()
|
||||
store.Connect(ctx, DataDir)
|
||||
store.SetLock(db.DATATYPE_TEMPLATE, false)
|
||||
store.SetLock(db.DATATYPE_BIN, false)
|
||||
store.SetLock(db.DATATYPE_MENU, false)
|
||||
store.SetLock(db.DATATYPE_STATICLOAD, false)
|
||||
|
||||
fns := []genFunc{terms, createPin, quit}
|
||||
for _, fn := range fns {
|
||||
err = fn()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func Generate() (string, error) {
|
||||
dir, err := ioutil.TempDir("", "vise_testdata_")
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
DataDir = dir
|
||||
dirLock = true
|
||||
err = generate()
|
||||
return dir, err
|
||||
}
|
Loading…
Reference in New Issue
Block a user