2025-01-10 21:32:22 +01:00
|
|
|
package testutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.defalsify.org/vise.git/engine"
|
|
|
|
"git.defalsify.org/vise.git/resource"
|
2025-01-11 21:33:34 +01:00
|
|
|
"git.grassecon.net/grassrootseconomics/sarafu-api/remote"
|
|
|
|
httpremote "git.grassecon.net/grassrootseconomics/sarafu-api/remote/http"
|
2025-01-21 16:43:26 +01:00
|
|
|
"git.grassecon.net/grassrootseconomics/sarafu-api/testutil/testservice"
|
|
|
|
"git.grassecon.net/grassrootseconomics/sarafu-vise/config"
|
|
|
|
"git.grassecon.net/grassrootseconomics/sarafu-vise/handlers"
|
2025-01-11 21:33:34 +01:00
|
|
|
"git.grassecon.net/grassrootseconomics/sarafu-vise/testutil/testtag"
|
2025-01-21 16:43:26 +01:00
|
|
|
"git.grassecon.net/grassrootseconomics/visedriver/storage"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
2025-01-10 21:32:22 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// CleanDatabase removes all test data from the database
|
|
|
|
func CleanDatabase() {
|
2025-01-21 16:43:26 +01:00
|
|
|
for _, v := range []int8{
|
2025-01-20 11:28:42 +01:00
|
|
|
storage.STORETYPE_STATE,
|
|
|
|
storage.STORETYPE_USER,
|
2025-01-21 16:43:26 +01:00
|
|
|
} {
|
2025-01-20 11:28:42 +01:00
|
|
|
conn := conns[v]
|
|
|
|
logg.Infof("cleaning test database", "typ", v, "db", conn)
|
|
|
|
if conn.DbType() == storage.DBTYPE_POSTGRES {
|
|
|
|
ctx := context.Background()
|
|
|
|
// Update the connection string with the new search path
|
2025-01-22 17:40:32 +01:00
|
|
|
updatedConnStr := conn.Raw()
|
2025-01-20 11:28:42 +01:00
|
|
|
|
|
|
|
dbConn, err := pgxpool.New(ctx, updatedConnStr)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to connect to database for cleanup: %v", err)
|
|
|
|
}
|
|
|
|
defer dbConn.Close()
|
|
|
|
|
|
|
|
setDbSchema := conn.Domain()
|
|
|
|
|
|
|
|
query := fmt.Sprintf("DELETE FROM %s.kv_vise;", setDbSchema)
|
|
|
|
_, execErr := dbConn.Exec(ctx, query)
|
|
|
|
if execErr != nil {
|
|
|
|
log.Printf("Failed to cleanup table %s.kv_vise: %v", setDbSchema, execErr)
|
|
|
|
} else {
|
|
|
|
log.Printf("Successfully cleaned up table %s.kv_vise", setDbSchema)
|
|
|
|
}
|
|
|
|
} else if conn.DbType() == storage.DBTYPE_FS || conn.DbType() == storage.DBTYPE_GDBM {
|
|
|
|
connStr, _ := filepath.Abs(conn.Path())
|
|
|
|
if err := os.RemoveAll(connStr); err != nil {
|
|
|
|
log.Fatalf("Failed to delete state store %v: %v", conn, err)
|
|
|
|
}
|
2025-01-10 21:32:22 +01:00
|
|
|
} else {
|
2025-01-20 11:28:42 +01:00
|
|
|
logg.Errorf("store cleanup not handled")
|
2025-01-10 21:32:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-20 11:28:42 +01:00
|
|
|
func TestEngine(sessionId string) (engine.Engine, func(), chan bool) {
|
|
|
|
config.LoadConfig()
|
2025-01-22 17:40:32 +01:00
|
|
|
err := config.Apply(override)
|
2025-01-10 21:32:22 +01:00
|
|
|
if err != nil {
|
2025-01-20 11:28:42 +01:00
|
|
|
panic(fmt.Errorf("args override fail: %v\n", err))
|
|
|
|
}
|
|
|
|
conns, err = config.GetConns()
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Errorf("get conns fail: %v\n", err))
|
2025-01-10 21:32:22 +01:00
|
|
|
}
|
|
|
|
ctx := context.Background()
|
|
|
|
ctx = context.WithValue(ctx, "SessionId", sessionId)
|
2025-01-20 11:28:42 +01:00
|
|
|
logg.InfoCtxf(ctx, "loaded engine setup", "conns", conns)
|
2025-01-10 21:32:22 +01: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),
|
|
|
|
}
|
|
|
|
|
2025-01-19 17:32:55 +01:00
|
|
|
menuStorageService := storage.NewMenuStorageService(conns)
|
2025-01-20 11:28:42 +01:00
|
|
|
menuStorageService = menuStorageService.WithDb(resourceDb, storage.STORETYPE_RESOURCE)
|
2025-01-10 21:32:22 +01:00
|
|
|
|
|
|
|
rs, err := menuStorageService.GetResource(ctx)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "resource error: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
pe, err := menuStorageService.GetPersister(ctx)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "persister error: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
userDataStore, err := menuStorageService.GetUserdataDb(ctx)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "userdb error: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
dbResource, ok := rs.(*resource.DbResource)
|
|
|
|
if !ok {
|
|
|
|
fmt.Fprintf(os.Stderr, "dbresource cast error")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
lhs, err := handlers.NewLocalHandlerService(ctx, pfp, true, dbResource, cfg, rs)
|
|
|
|
lhs.SetDataStore(&userDataStore)
|
|
|
|
lhs.SetPersister(pe)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if testtag.AccountService == nil {
|
2025-01-11 21:33:34 +01:00
|
|
|
testtag.AccountService = &httpremote.HTTPAccountService{}
|
2025-01-10 21:32:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
switch testtag.AccountService.(type) {
|
|
|
|
case *testservice.TestAccountService:
|
|
|
|
go func() {
|
|
|
|
eventChannel <- false
|
|
|
|
}()
|
2025-01-11 21:33:34 +01:00
|
|
|
case remote.AccountService:
|
2025-01-10 21:32:22 +01:00
|
|
|
go func() {
|
|
|
|
time.Sleep(5 * time.Second) // Wait for 5 seconds
|
|
|
|
eventChannel <- true
|
|
|
|
}()
|
|
|
|
default:
|
|
|
|
panic("Unknown account service type")
|
|
|
|
}
|
|
|
|
|
|
|
|
hl, err := lhs.GetHandler(testtag.AccountService)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, err.Error())
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
en := lhs.GetEngine()
|
|
|
|
en = en.WithFirst(hl.Init)
|
|
|
|
cleanFn := func() {
|
2025-01-19 10:46:37 +01:00
|
|
|
err := en.Finish(ctx)
|
2025-01-10 21:32:22 +01:00
|
|
|
if err != nil {
|
|
|
|
logg.Errorf(err.Error())
|
|
|
|
}
|
|
|
|
|
2025-01-19 10:46:37 +01:00
|
|
|
err = menuStorageService.Close(ctx)
|
2025-01-10 21:32:22 +01:00
|
|
|
if err != nil {
|
|
|
|
logg.Errorf(err.Error())
|
|
|
|
}
|
|
|
|
logg.Infof("testengine storage closed")
|
|
|
|
}
|
|
|
|
return en, cleanFn, eventChannel
|
|
|
|
}
|