(t *testing.T)
| 554 | } |
| 555 | |
| 556 | func TestSession_CommandRouting(t *testing.T) { |
| 557 | t.Run("routes command.execute event to the correct handler", func(t *testing.T) { |
| 558 | session, cleanup := newTestSession() |
| 559 | defer cleanup() |
| 560 | |
| 561 | var receivedCtx CommandContext |
| 562 | session.registerCommands([]CommandDefinition{ |
| 563 | { |
| 564 | Name: "deploy", |
| 565 | Description: "Deploy the app", |
| 566 | Handler: func(ctx CommandContext) error { |
| 567 | receivedCtx = ctx |
| 568 | return nil |
| 569 | }, |
| 570 | }, |
| 571 | { |
| 572 | Name: "rollback", |
| 573 | Description: "Rollback", |
| 574 | Handler: func(ctx CommandContext) error { |
| 575 | return nil |
| 576 | }, |
| 577 | }, |
| 578 | }) |
| 579 | |
| 580 | // Simulate the dispatch — executeCommandAndRespond will fail on RPC (nil client) |
| 581 | // but the handler will still be invoked. We test routing only. |
| 582 | _, ok := session.getCommandHandler("deploy") |
| 583 | if !ok { |
| 584 | t.Fatal("Expected 'deploy' handler to be registered") |
| 585 | } |
| 586 | _, ok = session.getCommandHandler("rollback") |
| 587 | if !ok { |
| 588 | t.Fatal("Expected 'rollback' handler to be registered") |
| 589 | } |
| 590 | _, ok = session.getCommandHandler("nonexistent") |
| 591 | if ok { |
| 592 | t.Fatal("Expected 'nonexistent' handler to NOT be registered") |
| 593 | } |
| 594 | |
| 595 | // Directly invoke handler to verify context is correct |
| 596 | handler, _ := session.getCommandHandler("deploy") |
| 597 | err := handler(CommandContext{ |
| 598 | SessionID: "test-session", |
| 599 | Command: "/deploy production", |
| 600 | CommandName: "deploy", |
| 601 | Args: "production", |
| 602 | }) |
| 603 | if err != nil { |
| 604 | t.Fatalf("Handler returned error: %v", err) |
| 605 | } |
| 606 | if receivedCtx.SessionID != "test-session" { |
| 607 | t.Errorf("Expected sessionID 'test-session', got %q", receivedCtx.SessionID) |
| 608 | } |
| 609 | if receivedCtx.CommandName != "deploy" { |
| 610 | t.Errorf("Expected commandName 'deploy', got %q", receivedCtx.CommandName) |
| 611 | } |
| 612 | if receivedCtx.Command != "/deploy production" { |
| 613 | t.Errorf("Expected command '/deploy production', got %q", receivedCtx.Command) |
nothing calls this directly
no test coverage detected
searching dependent graphs…