ussd/internal/testutil/TestEngine.go

125 lines
2.7 KiB
Go
Raw Permalink Normal View History

2024-10-17 11:33:38 +02:00
package testutil
import (
"context"
"fmt"
"os"
"path"
"time"
"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-21 15:47:25 +02:00
"git.grassecon.net/urdt/ussd/internal/testutil/testservice"
"git.grassecon.net/urdt/ussd/internal/testutil/testtag"
2024-10-17 11:33:38 +02:00
testdataloader "github.com/peteole/testdata-loader"
2024-10-30 14:09:15 +01:00
"git.grassecon.net/urdt/ussd/remote"
2024-10-17 11:33:38 +02:00
)
var (
baseDir = testdataloader.GetBasePath()
logg = logging.NewVanilla()
scriptDir = path.Join(baseDir, "services", "registration")
)
func TestEngine(sessionId string) (engine.Engine, func(), chan bool) {
ctx := context.Background()
ctx = context.WithValue(ctx, "SessionId", sessionId)
2024-10-21 08:22:07 +02:00
ctx = context.WithValue(ctx, "Database", "gdbm")
2024-10-17 11:33:38 +02:00
pfp := path.Join(scriptDir, "pp.csv")
var eventChannel = make(chan bool)
cfg := engine.Config{
Root: "root",
SessionId: sessionId,
OutputSize: uint32(160),
FlagCount: uint32(128),
}
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)
}
2024-10-31 18:08:30 +01:00
lhs, err := handlers.NewLocalHandlerService(ctx, pfp, true, dbResource, cfg, rs)
2024-10-17 11:33:38 +02:00
lhs.SetDataStore(&userDataStore)
lhs.SetPersister(pe)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
2024-10-21 15:47:25 +02:00
if testtag.AccountService == nil {
2024-10-30 14:09:15 +01:00
testtag.AccountService = &remote.AccountService{}
2024-10-17 11:33:38 +02:00
}
2024-10-21 15:47:25 +02:00
switch testtag.AccountService.(type) {
case *testservice.TestAccountService:
2024-10-17 11:33:38 +02:00
go func() {
eventChannel <- false
}()
2024-10-30 14:09:15 +01:00
case *remote.AccountService:
2024-10-17 11:33:38 +02:00
go func() {
time.Sleep(5 * time.Second) // Wait for 5 seconds
eventChannel <- true
}()
default:
panic("Unknown account service type")
}
2024-10-21 15:47:25 +02:00
hl, err := lhs.GetHandler(testtag.AccountService)
2024-10-17 11:33:38 +02:00
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
en := lhs.GetEngine()
en = en.WithFirst(hl.Init)
cleanFn := func() {
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")
}
return en, cleanFn, eventChannel
}