2024-01-31 23:29:31 -05:00
|
|
|
package player_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"gitlab.com/zephyrtronium/sq"
|
|
|
|
|
|
|
|
"git.sunturtle.xyz/studio/shotgun/player"
|
|
|
|
|
|
|
|
_ "modernc.org/sqlite" // sqlite driver
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSessions(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
db, err := sq.Open("sqlite", ":memory:")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
conn, err := db.Conn(ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if err := player.InitSessions(ctx, conn); err != nil {
|
|
|
|
t.Fatalf("couldn't init sessions: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-01-31 23:40:12 -05:00
|
|
|
p := player.ID{uuid.UUID{1}}
|
2024-01-31 23:29:31 -05:00
|
|
|
now := time.Unix(0, 0)
|
|
|
|
id, err := player.StartSession(ctx, conn, p, now)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("couldn't start session: %v", err)
|
|
|
|
}
|
2024-01-31 23:40:12 -05:00
|
|
|
if id.UUID == uuid.Nil {
|
2024-01-31 23:29:31 -05:00
|
|
|
t.Errorf("got zero session id at start")
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := player.FromSession(ctx, conn, id, now.Add(25*time.Hour))
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("no error on expired session")
|
|
|
|
}
|
2024-01-31 23:40:12 -05:00
|
|
|
if u.UUID != uuid.Nil {
|
2024-01-31 23:29:31 -05:00
|
|
|
t.Errorf("got nonzero player %v from expired session", u)
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err = player.FromSession(ctx, conn, id, now.Add(23*time.Hour))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("couldn't get player from session: %v", err)
|
|
|
|
}
|
2024-01-31 23:40:12 -05:00
|
|
|
if u.UUID == uuid.Nil {
|
2024-01-31 23:29:31 -05:00
|
|
|
t.Errorf("got zero player from session")
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err = player.FromSession(ctx, conn, id, now.Add(25*time.Hour))
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("couldn't get player from extended session: %v", err)
|
|
|
|
}
|
2024-01-31 23:40:12 -05:00
|
|
|
if u.UUID == uuid.Nil {
|
2024-01-31 23:29:31 -05:00
|
|
|
t.Errorf("got zero player from extended session")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := player.Logout(ctx, conn, id); err != nil {
|
|
|
|
t.Errorf("couldn't logout: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err = player.FromSession(ctx, conn, id, now.Add(25*time.Hour))
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("no error on logged out session")
|
|
|
|
}
|
2024-01-31 23:40:12 -05:00
|
|
|
if u.UUID != uuid.Nil {
|
2024-01-31 23:29:31 -05:00
|
|
|
t.Errorf("got nonzero player %v from logged out session", u)
|
|
|
|
}
|
|
|
|
}
|