feat(scum-companion): add bounded dispatcher runtime

This commit is contained in:
npc0-hue
2026-07-29 10:47:02 +08:00
parent d791b1de8e
commit 7155e755f8
3 changed files with 308 additions and 28 deletions
@@ -0,0 +1,80 @@
package companion
import (
"context"
"testing"
"time"
)
type dispatchFixture struct {
commands []ClaimedCommand
acks []string
completed []CommandResult
}
func (fixture *dispatchFixture) ClaimCommands(_ context.Context, _ int) ([]ClaimedCommand, error) {
return append([]ClaimedCommand(nil), fixture.commands...), nil
}
func (fixture *dispatchFixture) AckCommand(_ context.Context, id string, _ uint64) (CommandAck, error) {
fixture.acks = append(fixture.acks, id)
return CommandAck{CommandID: id, State: "claimed", FencingToken: 7}, nil
}
func (fixture *dispatchFixture) CompleteCommand(_ context.Context, _ string, _ uint64, result CommandResult) (CompletedCommand, error) {
fixture.completed = append(fixture.completed, result)
return CompletedCommand{State: "succeeded"}, nil
}
type adapterFixture struct{ reads int }
func (adapter *adapterFixture) ReadConfiguration(context.Context) (map[string]any, error) {
adapter.reads++
return map[string]any{"version": "0.9.700.90357", "hostPath": "C:/must-redact"}, nil
}
func (*adapterFixture) PatchConfiguration(context.Context, map[string]any) (map[string]any, error) {
return nil, nil
}
func (*adapterFixture) Diagnostics(context.Context) (map[string]any, error) {
return map[string]any{"status": "healthy"}, nil
}
func (*adapterFixture) PatchGameState(context.Context, map[string]any) (map[string]any, error) {
return nil, nil
}
func (*adapterFixture) DeliverReward(context.Context, map[string]any) (map[string]any, error) {
return nil, nil
}
func (*adapterFixture) NotifyPlayer(context.Context, map[string]any) (map[string]any, error) {
return nil, nil
}
func TestDispatcherAcknowledgesOnlyLiveValidatedTypedCommands(t *testing.T) {
stamp := time.Now().UTC()
adapter := &adapterFixture{}
registry := NewHandlerRegistry(HandlerAvailability{ServerVersion: "0.9.700.90357", Approved: true, Capabilities: map[string]bool{"config.read": true}}, adapter)
fixture := &dispatchFixture{commands: []ClaimedCommand{{ID: "read-1", ProfileKey: ProfileKey, CommandType: "config.read", Payload: map[string]any{}, FencingToken: 7, LeaseExpiresAt: stamp.Add(time.Minute), ExpiresAt: stamp.Add(time.Minute)}, {ID: "expired-1", ProfileKey: ProfileKey, CommandType: "config.read", Payload: map[string]any{}, FencingToken: 8, LeaseExpiresAt: stamp.Add(-time.Second), ExpiresAt: stamp.Add(-time.Second)}}}
dispatcher := Dispatcher{Client: fixture, Registry: registry, Now: func() time.Time { return stamp }}
if err := dispatcher.DispatchOnce(context.Background()); err != nil {
t.Fatalf("dispatch: %v", err)
}
if len(fixture.acks) != 1 || fixture.acks[0] != "read-1" || len(fixture.completed) != 2 || adapter.reads != 1 {
t.Fatalf("unexpected bounded dispatch: acks=%v completed=%+v reads=%d", fixture.acks, fixture.completed, adapter.reads)
}
if fixture.completed[0].Payload["hostPath"] != nil || fixture.completed[1].Payload["result"] != "validation-failed" {
t.Fatalf("unsafe or malformed result: %+v", fixture.completed)
}
}
func TestRegistryReturnsCachedResultForDuplicateDelivery(t *testing.T) {
stamp := time.Now().UTC()
adapter := &adapterFixture{}
registry := NewHandlerRegistry(HandlerAvailability{ServerVersion: "0.9.700.90357", Approved: true, Capabilities: map[string]bool{"config.read": true}}, adapter)
command := ClaimedCommand{ID: "duplicate-1", ProfileKey: ProfileKey, CommandType: "config.read", Payload: map[string]any{}, FencingToken: 7, LeaseExpiresAt: stamp.Add(time.Minute), ExpiresAt: stamp.Add(time.Minute)}
if _, err := registry.Execute(context.Background(), command); err != nil {
t.Fatalf("first execute: %v", err)
}
if _, err := registry.Execute(context.Background(), command); err != nil {
t.Fatalf("second execute: %v", err)
}
if adapter.reads != 1 {
t.Fatalf("duplicate delivery invoked adapter %d times", adapter.reads)
}
}