Files
VRCWT-OSC/internal/app/guest_status_test.go
2026-06-26 09:16:46 +09:00

59 lines
1.2 KiB
Go

package app
import (
"os"
"path/filepath"
"testing"
"time"
"vrc_osc_go/internal/common"
)
func TestGuestTrackerJoinLeave(t *testing.T) {
base := t.TempDir()
prev := common.SetRootDirForTest(func() string { return base })
defer prev()
tracker := NewGuestTracker([]string{"Alice", "Bob"})
now := time.Date(2026, 6, 25, 12, 0, 0, 0, time.Local)
tracker.MarkJoin("Alice", now)
tracker.MarkJoin("Bob", now.Add(10*time.Second))
got := tracker.Snapshot(now.Add(20 * time.Second))
if len(got) != 2 {
t.Fatalf("expected 2 guests, got %d", len(got))
}
present := 0
for _, g := range got {
if g.Present {
present++
}
}
if present != 2 {
t.Fatalf("expected 2 present guests, got %#v", got)
}
tracker.MarkLeave("Alice", now.Add(30*time.Second))
got = tracker.Snapshot(now.Add(40 * time.Second))
var alice GuestStatus
for _, g := range got {
if g.Name == "Alice" {
alice = g
}
}
if alice.Present {
t.Fatalf("expected Alice to be absent, got %#v", alice)
}
if alice.AbsentFor == "" {
t.Fatalf("expected Alice absent duration, got %#v", alice)
}
snapshotPath := filepath.Join(base, "runtime", "guest_snapshot.json")
if _, err := os.Stat(snapshotPath); err != nil {
t.Fatalf("expected snapshot file: %v", err)
}
}