ussd/engine/engine.go

110 lines
2.3 KiB
Go
Raw Normal View History

package engine
2024-09-27 14:02:29 +02:00
import (
"context"
"fmt"
"os"
"path"
"git.defalsify.org/vise.git/engine"
"git.defalsify.org/vise.git/logging"
"git.defalsify.org/vise.git/resource"
"git.grassecon.net/urdt/ussd/internal/handlers"
2024-10-04 15:56:23 +02:00
"git.grassecon.net/urdt/ussd/internal/handlers/server"
2024-09-27 14:02:29 +02:00
"git.grassecon.net/urdt/ussd/internal/storage"
2024-10-03 10:45:37 +02:00
testdataloader "github.com/peteole/testdata-loader"
2024-09-27 14:02:29 +02:00
)
2024-09-30 12:25:38 +02:00
2024-09-27 14:02:29 +02:00
var (
2024-10-03 10:45:37 +02:00
baseDir = testdataloader.GetBasePath()
2024-09-27 14:02:29 +02:00
logg = logging.NewVanilla()
2024-10-03 10:45:37 +02:00
scriptDir = path.Join(baseDir, "services", "registration")
2024-09-27 14:02:29 +02:00
)
2024-10-04 15:56:23 +02:00
func TestEngine(sessionId string) (engine.Engine, func()) {
var accountService server.AccountServiceInterface
2024-09-27 14:02:29 +02:00
ctx := context.Background()
ctx = context.WithValue(ctx, "SessionId", sessionId)
pfp := path.Join(scriptDir, "pp.csv")
cfg := engine.Config{
Root: "root",
SessionId: sessionId,
2024-09-27 15:26:00 +02:00
OutputSize: uint32(160),
2024-09-30 12:21:17 +02:00
FlagCount: uint32(16),
2024-09-27 14:02:29 +02:00
}
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)
2024-09-27 14:02:29 +02:00
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)
2024-09-27 14:02:29 +02:00
lhs.SetPersister(pe)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
if OnlineTestEnabled {
accountService = &server.AccountService{}
} else {
accountService = &server.MockAccountService{}
}
hl, err := lhs.GetHandler(accountService)
2024-09-27 14:02:29 +02:00
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
en := lhs.GetEngine()
en = en.WithFirst(hl.Init)
cleanFn := func() {
2024-10-02 14:52:55 +02:00
err := en.Finish()
if err != nil {
logg.Errorf(err.Error())
}
err = menuStorageService.Close()
if err != nil {
logg.Errorf(err.Error())
}
logg.Infof("testengine storage closed")
}
2024-09-27 14:02:29 +02:00
//en = en.WithDebug(nil)
2024-10-04 15:56:23 +02:00
return en, cleanFn
2024-09-27 14:02:29 +02:00
}