mirror of
https://github.com/Mezeporta/Erupe.git
synced 2026-03-21 23:22:34 +01:00
Add zero-dependency SQLite mode so users can run Erupe without
PostgreSQL. A transparent db.DB wrapper auto-translates PostgreSQL
SQL ($N placeholders, now(), ::casts, ILIKE, public. prefix,
TRUNCATE) for SQLite at runtime — all 28 repo files use the wrapper
with no per-query changes needed.
Setup wizard gains two new steps: quest file detection with download
link, and gameplay presets (solo/small/community/rebalanced). The API
server gets a /dashboard endpoint with auto-refreshing stats.
CI release workflow now builds and pushes Docker images to GHCR
alongside binary artifacts on tag push.
Key changes:
- common/db: DB/Tx wrapper with 6 SQL translation rules
- server/migrations/sqlite: full SQLite schema (0001-0005)
- config: Database.Driver field ("postgres" or "sqlite")
- main.go: SQLite connection with WAL mode, single writer
- server/setup: quest check + preset selection steps
- server/api: /dashboard with live stats
- .github/workflows: Docker in release, deduplicate docker.yml
143 lines
4.0 KiB
Go
143 lines
4.0 KiB
Go
package channelserver
|
|
|
|
import (
|
|
dbutil "erupe-ce/common/db"
|
|
"testing"
|
|
|
|
"github.com/jmoiron/sqlx"
|
|
)
|
|
|
|
func setupSessionRepo(t *testing.T) (*SessionRepository, *sqlx.DB, uint32, uint32, uint32, string) {
|
|
t.Helper()
|
|
db := SetupTestDB(t)
|
|
userID := CreateTestUser(t, db, "session_test_user")
|
|
charID := CreateTestCharacter(t, db, userID, "SessionChar")
|
|
token := "test_token_12345"
|
|
sessionID := CreateTestSignSession(t, db, userID, token)
|
|
repo := NewSessionRepository(dbutil.Wrap(db))
|
|
t.Cleanup(func() { TeardownTestDB(t, db) })
|
|
return repo, db, userID, charID, sessionID, token
|
|
}
|
|
|
|
func TestRepoSessionValidateLoginToken(t *testing.T) {
|
|
repo, _, _, charID, sessionID, token := setupSessionRepo(t)
|
|
|
|
err := repo.ValidateLoginToken(token, sessionID, charID)
|
|
if err != nil {
|
|
t.Fatalf("ValidateLoginToken failed: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestRepoSessionValidateLoginTokenInvalidToken(t *testing.T) {
|
|
repo, _, _, charID, sessionID, _ := setupSessionRepo(t)
|
|
|
|
err := repo.ValidateLoginToken("wrong_token", sessionID, charID)
|
|
if err == nil {
|
|
t.Fatal("Expected error for invalid token, got nil")
|
|
}
|
|
}
|
|
|
|
func TestRepoSessionValidateLoginTokenWrongChar(t *testing.T) {
|
|
repo, _, _, _, sessionID, token := setupSessionRepo(t)
|
|
|
|
err := repo.ValidateLoginToken(token, sessionID, 999999)
|
|
if err == nil {
|
|
t.Fatal("Expected error for wrong char ID, got nil")
|
|
}
|
|
}
|
|
|
|
func TestRepoSessionValidateLoginTokenWrongSession(t *testing.T) {
|
|
repo, _, _, charID, _, token := setupSessionRepo(t)
|
|
|
|
err := repo.ValidateLoginToken(token, 999999, charID)
|
|
if err == nil {
|
|
t.Fatal("Expected error for wrong session ID, got nil")
|
|
}
|
|
}
|
|
|
|
func TestRepoSessionBindSession(t *testing.T) {
|
|
repo, db, _, charID, _, token := setupSessionRepo(t)
|
|
|
|
CreateTestServer(t, db, 1)
|
|
|
|
if err := repo.BindSession(token, 1, charID); err != nil {
|
|
t.Fatalf("BindSession failed: %v", err)
|
|
}
|
|
|
|
var serverID *uint16
|
|
var boundCharID *uint32
|
|
if err := db.QueryRow("SELECT server_id, char_id FROM sign_sessions WHERE token=$1", token).Scan(&serverID, &boundCharID); err != nil {
|
|
t.Fatalf("Verification query failed: %v", err)
|
|
}
|
|
if serverID == nil || *serverID != 1 {
|
|
t.Errorf("Expected server_id=1, got: %v", serverID)
|
|
}
|
|
if boundCharID == nil || *boundCharID != charID {
|
|
t.Errorf("Expected char_id=%d, got: %v", charID, boundCharID)
|
|
}
|
|
}
|
|
|
|
func TestRepoSessionClearSession(t *testing.T) {
|
|
repo, db, _, charID, _, token := setupSessionRepo(t)
|
|
|
|
CreateTestServer(t, db, 1)
|
|
|
|
if err := repo.BindSession(token, 1, charID); err != nil {
|
|
t.Fatalf("BindSession failed: %v", err)
|
|
}
|
|
|
|
if err := repo.ClearSession(token); err != nil {
|
|
t.Fatalf("ClearSession failed: %v", err)
|
|
}
|
|
|
|
var serverID, boundCharID *int
|
|
if err := db.QueryRow("SELECT server_id, char_id FROM sign_sessions WHERE token=$1", token).Scan(&serverID, &boundCharID); err != nil {
|
|
t.Fatalf("Verification query failed: %v", err)
|
|
}
|
|
if serverID != nil {
|
|
t.Errorf("Expected server_id=NULL, got: %v", *serverID)
|
|
}
|
|
if boundCharID != nil {
|
|
t.Errorf("Expected char_id=NULL, got: %v", *boundCharID)
|
|
}
|
|
}
|
|
|
|
func TestRepoSessionUpdatePlayerCount(t *testing.T) {
|
|
repo, db, _, _, _, _ := setupSessionRepo(t)
|
|
|
|
CreateTestServer(t, db, 1)
|
|
|
|
if err := repo.UpdatePlayerCount(1, 42); err != nil {
|
|
t.Fatalf("UpdatePlayerCount failed: %v", err)
|
|
}
|
|
|
|
var count int
|
|
if err := db.QueryRow("SELECT current_players FROM servers WHERE server_id=1").Scan(&count); err != nil {
|
|
t.Fatalf("Verification query failed: %v", err)
|
|
}
|
|
if count != 42 {
|
|
t.Errorf("Expected current_players=42, got: %d", count)
|
|
}
|
|
}
|
|
|
|
func TestRepoSessionUpdatePlayerCountTwice(t *testing.T) {
|
|
repo, db, _, _, _, _ := setupSessionRepo(t)
|
|
|
|
CreateTestServer(t, db, 1)
|
|
|
|
if err := repo.UpdatePlayerCount(1, 10); err != nil {
|
|
t.Fatalf("First UpdatePlayerCount failed: %v", err)
|
|
}
|
|
if err := repo.UpdatePlayerCount(1, 25); err != nil {
|
|
t.Fatalf("Second UpdatePlayerCount failed: %v", err)
|
|
}
|
|
|
|
var count int
|
|
if err := db.QueryRow("SELECT current_players FROM servers WHERE server_id=1").Scan(&count); err != nil {
|
|
t.Fatalf("Verification query failed: %v", err)
|
|
}
|
|
if count != 25 {
|
|
t.Errorf("Expected current_players=25, got: %d", count)
|
|
}
|
|
}
|