- Clock API routes: start, pause, resume, advance, rewind, jump, get, warnings - Role-based access control (floor+ for mutations, any auth for reads) - Clock state persistence callback to DB on meaningful changes - Blind structure levels loaded from DB on clock start - Clock registry wired into HTTP server and cmd/leaf main - 25 tests covering: state machine, countdown, pause/resume, auto-advance, jump, rewind, hand-for-hand, warnings, overtime, crash recovery, snapshot - Fix missing crypto/rand import in auth/pin.go (Rule 3 auto-fix) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
81 lines
1.8 KiB
Go
81 lines
1.8 KiB
Go
package clock
|
|
|
|
import (
|
|
"testing"
|
|
)
|
|
|
|
func TestRegistryGetOrCreate(t *testing.T) {
|
|
registry := NewRegistry(nil)
|
|
|
|
// First call creates engine
|
|
e1 := registry.GetOrCreate("t1")
|
|
if e1 == nil {
|
|
t.Fatal("expected non-nil engine")
|
|
}
|
|
if e1.TournamentID() != "t1" {
|
|
t.Errorf("expected tournament t1, got %s", e1.TournamentID())
|
|
}
|
|
|
|
// Second call returns same engine
|
|
e2 := registry.GetOrCreate("t1")
|
|
if e1 != e2 {
|
|
t.Error("expected same engine instance on second GetOrCreate")
|
|
}
|
|
|
|
// Different tournament gets different engine
|
|
e3 := registry.GetOrCreate("t2")
|
|
if e1 == e3 {
|
|
t.Error("expected different engine for different tournament")
|
|
}
|
|
|
|
if registry.Count() != 2 {
|
|
t.Errorf("expected 2 engines, got %d", registry.Count())
|
|
}
|
|
}
|
|
|
|
func TestRegistryGet(t *testing.T) {
|
|
registry := NewRegistry(nil)
|
|
|
|
// Get non-existent returns nil
|
|
if e := registry.Get("nonexistent"); e != nil {
|
|
t.Error("expected nil for non-existent tournament")
|
|
}
|
|
|
|
// Create one
|
|
registry.GetOrCreate("t1")
|
|
|
|
// Now Get should return it
|
|
if e := registry.Get("t1"); e == nil {
|
|
t.Error("expected non-nil engine for existing tournament")
|
|
}
|
|
}
|
|
|
|
func TestRegistryRemove(t *testing.T) {
|
|
registry := NewRegistry(nil)
|
|
registry.GetOrCreate("t1")
|
|
registry.GetOrCreate("t2")
|
|
|
|
if registry.Count() != 2 {
|
|
t.Errorf("expected 2, got %d", registry.Count())
|
|
}
|
|
|
|
registry.Remove("t1")
|
|
if registry.Count() != 1 {
|
|
t.Errorf("expected 1 after remove, got %d", registry.Count())
|
|
}
|
|
if e := registry.Get("t1"); e != nil {
|
|
t.Error("expected nil after remove")
|
|
}
|
|
}
|
|
|
|
func TestRegistryShutdown(t *testing.T) {
|
|
registry := NewRegistry(nil)
|
|
registry.GetOrCreate("t1")
|
|
registry.GetOrCreate("t2")
|
|
registry.GetOrCreate("t3")
|
|
|
|
registry.Shutdown()
|
|
if registry.Count() != 0 {
|
|
t.Errorf("expected 0 after shutdown, got %d", registry.Count())
|
|
}
|
|
}
|