Files
browser/plugins/examples/scum-server-plugin/companion/player_projection_test.go
T

47 lines
2.1 KiB
Go

package companion
import (
"context"
"encoding/json"
"io"
"net/http"
"strings"
"testing"
"time"
)
func TestSCUMPlayerLogProjectionCreatesTypedUserSnapshot(t *testing.T) {
stamp := time.Date(2026, 9, 2, 2, 0, 0, 0, time.UTC)
config := loadTestConfig(t)
var snapshot Snapshot
client := newTestClient(t, config, roundTripFunc(func(request *http.Request) (*http.Response, error) {
if request.URL.Path != snapshotPath {
t.Fatalf("unexpected projection request path: %s", request.URL.Path)
}
var body snapshotRequest
if err := json.NewDecoder(request.Body).Decode(&body); err != nil {
t.Fatalf("decode snapshot request: %v", err)
}
snapshot = Snapshot{Type: body.Type, SchemaVersion: body.SchemaVersion, StreamKey: body.StreamKey, Sequence: body.Sequence, ObservedAt: body.ObservedAt, Payload: body.Payload, KeepForSeconds: body.KeepForSeconds, MaxRecords: body.MaxRecords}
return &http.Response{StatusCode: http.StatusAccepted, Header: make(http.Header), Body: io.NopCloser(strings.NewReader(`{"snapshotId":"snapshot-1","profileKey":"scum-client-manager","type":"players","schemaVersion":"1","streamKey":"current","sequence":1}`))}, nil
}), stamp)
client.mu.Lock()
client.sessionToken = "component-session"
client.sessionExpiresAt = stamp.Add(time.Hour)
client.mu.Unlock()
projection := NewSCUMPlayerLogProjection(client, "server-example")
projection.Now = func() time.Time { return stamp }
if err := projection.Handle(context.Background(), SemanticEventBatch{ServerID: "server-example", Events: []SemanticEvent{{ServerID: "server-example", Sequence: 7, Type: "scum.login", PlayerID: "76561198000000001", DisplayName: "Ada", OccurredAt: stamp}}}); err != nil {
t.Fatalf("project login event: %v", err)
}
players, ok := snapshot.Payload["players"].([]any)
if !ok || len(players) != 1 {
t.Fatalf("expected one projected player, payload=%#v", snapshot.Payload)
}
player, ok := players[0].(map[string]any)
if !ok || player["playerId"] != "76561198000000001" || player["playerName"] != "Ada" || player["status"] != "online" {
t.Fatalf("unexpected projected player: %#v", players[0])
}
}