Make SCUM logs live relay only

This commit is contained in:
npc0-hue
2026-09-01 14:26:00 +08:00
parent 3f348401a9
commit 7d9c1b7e9e
28 changed files with 1011 additions and 175 deletions
@@ -0,0 +1,62 @@
package companion
import (
"context"
"testing"
"time"
)
type recordingConsoleLogStore struct {
ensureCalls int
records []ConsoleRecord
batches []SemanticEventBatch
}
func (store *recordingConsoleLogStore) EnsureSchema(context.Context) error {
store.ensureCalls++
return nil
}
func (store *recordingConsoleLogStore) StoreConsoleRecords(_ context.Context, records []ConsoleRecord) (int, error) {
store.records = append(store.records, records...)
return len(records), nil
}
func (store *recordingConsoleLogStore) StoreSemanticEventBatch(_ context.Context, batch SemanticEventBatch) (int, error) {
store.batches = append(store.batches, batch)
return len(batch.Events), nil
}
func TestConsoleLogCollectorStoresLiveConsoleEventAndSemanticBatch(t *testing.T) {
stamp := time.Date(2026, 8, 31, 4, 0, 0, 0, time.UTC)
store := &recordingConsoleLogStore{}
collector := NewConsoleLogCollector(nil, store, "server-1", "correlation-secret")
handle := collector.handleEvent(context.Background())
if err := handle(LogStreamEvent{ServerInstanceID: "server-1", StreamID: "stream-1", Source: "process", StreamKey: "stdout", Entry: LogEntry{Seq: 11, Timestamp: stamp, Line: "SCUM LOGIN 76561198000000001 10.0.0.1"}}); err != nil {
t.Fatalf("handle log event: %v", err)
}
if len(store.records) != 1 || store.records[0].ServerID != "server-1" || store.records[0].Stream != "stdout" || store.records[0].Sequence != 11 {
t.Fatalf("collector did not store raw console record: %#v", store.records)
}
if len(store.batches) != 1 || len(store.batches[0].Events) != 1 {
t.Fatalf("collector did not store semantic event batch: %#v", store.batches)
}
event := store.batches[0].Events[0]
if event.Type != "scum.login" || event.PlayerID != "76561198000000001" || event.NetworkCorrelation == "" || event.NetworkCorrelation == "10.0.0.1" {
t.Fatalf("unexpected semantic event: %#v", event)
}
}
func TestConsoleLogCollectorIgnoresNonConsoleLiveLogEvents(t *testing.T) {
store := &recordingConsoleLogStore{}
collector := NewConsoleLogCollector(nil, store, "server-1", "correlation-secret")
handle := collector.handleEvent(context.Background())
if err := handle(LogStreamEvent{ServerInstanceID: "server-1", StreamID: "stream-1", Source: "process", StreamKey: "scum.file", Entry: LogEntry{Seq: 12, Timestamp: time.Now().UTC(), Line: "not console"}}); err != nil {
t.Fatalf("handle non-console event: %v", err)
}
if len(store.records) != 0 || len(store.batches) != 0 {
t.Fatalf("non-console event was stored: records=%#v batches=%#v", store.records, store.batches)
}
}