2024-09-27 14:02:29 +02:00
|
|
|
package enginetest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
|
2024-10-04 10:11:41 +02:00
|
|
|
"git.defalsify.org/vise.git/db"
|
2024-09-27 14:02:29 +02:00
|
|
|
"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"
|
|
|
|
"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 10:11:41 +02:00
|
|
|
func TestEngine(sessionId string) (engine.Engine, func(), *db.Db) {
|
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)
|
|
|
|
}
|
|
|
|
|
2024-10-01 01:18:54 +02:00
|
|
|
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)
|
2024-10-01 01:18:54 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
hl, err := lhs.GetHandler()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
en := lhs.GetEngine()
|
|
|
|
en = en.WithFirst(hl.Init)
|
|
|
|
|
2024-10-01 01:18:54 +02:00
|
|
|
cleanFn := func() {
|
2024-10-02 14:52:55 +02:00
|
|
|
err := en.Finish()
|
|
|
|
if err != nil {
|
|
|
|
logg.Errorf(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
err = menuStorageService.Close()
|
2024-10-01 01:18:54 +02:00
|
|
|
if err != nil {
|
|
|
|
logg.Errorf(err.Error())
|
|
|
|
}
|
|
|
|
logg.Infof("testengine storage closed")
|
|
|
|
}
|
2024-09-27 14:02:29 +02:00
|
|
|
|
2024-10-01 01:18:54 +02:00
|
|
|
//en = en.WithDebug(nil)
|
2024-10-04 10:11:41 +02:00
|
|
|
return en, cleanFn, lhs.UserdataStore
|
2024-09-27 14:02:29 +02:00
|
|
|
}
|