feat: publish matchmaking state events

This commit is contained in:
Josh Creek
2026-08-31 23:12:20 +01:00
parent 161d2cdceb
commit 60fe2caf8f
4 changed files with 76 additions and 3 deletions
+46
View File
@@ -295,6 +295,52 @@ func TestEventHubRejectsEventsOutsideTheV1Vocabulary(t *testing.T) {
}
}
func TestStateChangingAPIActionsPublishTargetedEvents(t *testing.T) {
now := time.Unix(1000, 0).UTC()
backend := &queueBackendSpy{}
service := &Service{SessionBackend: &sessionBackendSpy{}, QueueBackend: backend, Now: func() time.Time { return now }, Proposals: make(map[string]*domain.Proposal)}
subscriber := service.getEventHub().subscribe("player-1")
defer service.getEventHub().unsubscribe(subscriber)
create := httptest.NewRequest(http.MethodPost, "/v1/queue", strings.NewReader(`{"ticket_id":"ticket-1234567890123456","playlist":"casual","client_build":"build-1","protocol_version":1}`))
create.Header.Set("Authorization", "Bearer session-1:token-1")
create.Header.Set("Idempotency-Key", "create-event-key-123456")
createRecorder := httptest.NewRecorder()
service.queueCreate(createRecorder, create)
if createRecorder.Code != http.StatusCreated {
t.Fatalf("create status = %d", createRecorder.Code)
}
var queueEvent ControlPlaneEvent
if err := json.Unmarshal(<-subscriber.queue, &queueEvent); err != nil {
t.Fatal(err)
}
if queueEvent.Event != "state_changed" || queueEvent.ResourceID != "ticket-1234567890123456" || queueEvent.PlayerID != "" {
t.Fatalf("queue event = %+v", queueEvent)
}
proposal, err := domain.NewProposal("proposal-1234567890123456", domain.Casual, []string{"player-1", "player-2"}, now)
if err != nil {
t.Fatal(err)
}
service.Proposals[proposal.ProposalID] = &proposal
respond := httptest.NewRequest(http.MethodPost, "/v1/proposals/"+proposal.ProposalID+"/accept", nil)
respond.Header.Set("Authorization", "Bearer session-1:token-1")
respond.Header.Set("Idempotency-Key", "proposal-event-key-123456")
respond.Header.Set("If-Match-Revision", "0")
respondRecorder := httptest.NewRecorder()
service.proposalMutation(respondRecorder, respond)
if respondRecorder.Code != http.StatusOK {
t.Fatalf("proposal status = %d", respondRecorder.Code)
}
var proposalEvent ControlPlaneEvent
if err := json.Unmarshal(<-subscriber.queue, &proposalEvent); err != nil {
t.Fatal(err)
}
if proposalEvent.Event != "proposal_changed" || proposalEvent.ResourceID != proposal.ProposalID || proposalEvent.State != "OPEN" {
t.Fatalf("proposal event = %+v", proposalEvent)
}
}
func TestQueueAPIRejectsUnauthenticatedUnknownAndOversizedInput(t *testing.T) {
service := &Service{Sessions: domain.NewSessionStore(), Queue: domain.NewQueue(), Candidate: func(string, string) (domain.Candidate, error) { return domain.Candidate{}, nil }}
server := httptest.NewServer(service.Handler())