|
1 | 1 | package copilot |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
4 | 5 | "fmt" |
5 | 6 | "strings" |
6 | 7 | "sync" |
@@ -530,6 +531,45 @@ func TestSession_ElicitationHandler(t *testing.T) { |
530 | 531 | }) |
531 | 532 | } |
532 | 533 |
|
| 534 | +func TestSession_HookForwardCompatibility(t *testing.T) { |
| 535 | + t.Run("unknown hook type returns nil without error when known hooks are registered", func(t *testing.T) { |
| 536 | + session, cleanup := newTestSession() |
| 537 | + defer cleanup() |
| 538 | + |
| 539 | + // Register known hook handlers to simulate a real session configuration. |
| 540 | + // The handler itself does nothing; it only exists to confirm that even |
| 541 | + // when other hooks are active, an unknown hook type is still ignored. |
| 542 | + session.registerHooks(&SessionHooks{ |
| 543 | + OnPostToolUse: func(input PostToolUseHookInput, invocation HookInvocation) (*PostToolUseHookOutput, error) { |
| 544 | + return nil, nil |
| 545 | + }, |
| 546 | + }) |
| 547 | + |
| 548 | + // "postToolUseFailure" is an example of a hook type introduced by a newer |
| 549 | + // CLI version that the SDK does not yet know about. |
| 550 | + output, err := session.handleHooksInvoke("postToolUseFailure", json.RawMessage(`{}`)) |
| 551 | + if err != nil { |
| 552 | + t.Errorf("Expected no error for unknown hook type, got: %v", err) |
| 553 | + } |
| 554 | + if output != nil { |
| 555 | + t.Errorf("Expected nil output for unknown hook type, got: %v", output) |
| 556 | + } |
| 557 | + }) |
| 558 | + |
| 559 | + t.Run("unknown hook type with no hooks registered returns nil without error", func(t *testing.T) { |
| 560 | + session, cleanup := newTestSession() |
| 561 | + defer cleanup() |
| 562 | + |
| 563 | + output, err := session.handleHooksInvoke("futureHookType", json.RawMessage(`{"someField":"value"}`)) |
| 564 | + if err != nil { |
| 565 | + t.Errorf("Expected no error for unknown hook type with no hooks, got: %v", err) |
| 566 | + } |
| 567 | + if output != nil { |
| 568 | + t.Errorf("Expected nil output for unknown hook type with no hooks, got: %v", output) |
| 569 | + } |
| 570 | + }) |
| 571 | +} |
| 572 | + |
533 | 573 | func TestSession_ElicitationRequestSchema(t *testing.T) { |
534 | 574 | t.Run("elicitation.requested passes full schema to handler", func(t *testing.T) { |
535 | 575 | // Verify the schema extraction logic from handleBroadcastEvent |
|
0 commit comments