@@ -10,7 +10,7 @@ import {
1010 type ModelInfo ,
1111} from "../src/index.js" ;
1212import { CopilotSession } from "../src/session.js" ;
13- import { defaultJoinSessionPermissionHandler } from "../src/types.js" ;
13+ import { defaultJoinSessionPermissionHandler , type SessionHooks } from "../src/types.js" ;
1414
1515// This file is for unit tests. Where relevant, prefer to add e2e tests in e2e/*.test.ts instead
1616
@@ -2427,19 +2427,18 @@ describe("CopilotClient", () => {
24272427 // corresponding SessionHooks handler. These tests guard against
24282428 // regressions like the one fixed for postToolUseFailure (issue #1220).
24292429
2430- it ( "dispatches postToolUseFailure to onPostToolUseFailure handler" , async ( ) => {
2431- const client = new CopilotClient ( ) ;
2432- await client . start ( ) ;
2433- onTestFinished ( ( ) => client . forceStop ( ) ) ;
2430+ function createHookTestSession ( hooks : SessionHooks ) : CopilotSession {
2431+ const session = new CopilotSession ( "session-hooks-test" , { } as any ) ;
2432+ session . registerHooks ( hooks ) ;
2433+ return session ;
2434+ }
24342435
2436+ it ( "dispatches postToolUseFailure to onPostToolUseFailure handler" , async ( ) => {
24352437 const received : { input : any ; invocation : any } [ ] = [ ] ;
2436- const session = await client . createSession ( {
2437- onPermissionRequest : approveAll ,
2438- hooks : {
2439- onPostToolUseFailure : async ( input , invocation ) => {
2440- received . push ( { input, invocation } ) ;
2441- return { additionalContext : "failure observed" } ;
2442- } ,
2438+ const session = createHookTestSession ( {
2439+ onPostToolUseFailure : async ( input , invocation ) => {
2440+ received . push ( { input, invocation } ) ;
2441+ return { additionalContext : "failure observed" } ;
24432442 } ,
24442443 } ) ;
24452444
@@ -2469,19 +2468,12 @@ describe("CopilotClient", () => {
24692468 } ) ;
24702469
24712470 it ( "does not fall back to onPostToolUse for postToolUseFailure events" , async ( ) => {
2472- const client = new CopilotClient ( ) ;
2473- await client . start ( ) ;
2474- onTestFinished ( ( ) => client . forceStop ( ) ) ;
2475-
24762471 const postUseCalls : string [ ] = [ ] ;
2477- const session = await client . createSession ( {
2478- onPermissionRequest : approveAll ,
2479- hooks : {
2480- // Only onPostToolUse registered; postToolUseFailure events
2481- // must not be routed here.
2482- onPostToolUse : async ( input ) => {
2483- postUseCalls . push ( input . toolName ) ;
2484- } ,
2472+ const session = createHookTestSession ( {
2473+ // Only onPostToolUse registered; postToolUseFailure events
2474+ // must not be routed here.
2475+ onPostToolUse : async ( input ) => {
2476+ postUseCalls . push ( input . toolName ) ;
24852477 } ,
24862478 } ) ;
24872479
@@ -2498,21 +2490,14 @@ describe("CopilotClient", () => {
24982490 } ) ;
24992491
25002492 it ( "dispatches postToolUse and postToolUseFailure to their respective handlers" , async ( ) => {
2501- const client = new CopilotClient ( ) ;
2502- await client . start ( ) ;
2503- onTestFinished ( ( ) => client . forceStop ( ) ) ;
2504-
25052493 const postCalls : string [ ] = [ ] ;
25062494 const failureCalls : string [ ] = [ ] ;
2507- const session = await client . createSession ( {
2508- onPermissionRequest : approveAll ,
2509- hooks : {
2510- onPostToolUse : async ( input ) => {
2511- postCalls . push ( input . toolName ) ;
2512- } ,
2513- onPostToolUseFailure : async ( input ) => {
2514- failureCalls . push ( input . toolName ) ;
2515- } ,
2495+ const session = createHookTestSession ( {
2496+ onPostToolUse : async ( input ) => {
2497+ postCalls . push ( input . toolName ) ;
2498+ } ,
2499+ onPostToolUseFailure : async ( input ) => {
2500+ failureCalls . push ( input . toolName ) ;
25162501 } ,
25172502 } ) ;
25182503
@@ -2539,29 +2524,23 @@ describe("CopilotClient", () => {
25392524 } ) ;
25402525
25412526 it ( "routes hooks.invoke JSON-RPC requests to the SessionHooks handler" , async ( ) => {
2542- // Validates the full JSON-RPC entry point used by the CLI :
2527+ // Validates the full JSON-RPC entry point used by legacy runtimes :
25432528 // CopilotClient.handleHooksInvoke({sessionId, hookType, input})
25442529 // → CopilotSession._handleHooksInvoke(hookType, input)
25452530 // → SessionHooks.onPostToolUseFailure(normalizedInput, {sessionId})
25462531 //
2547- // This guards the wire-format contract that the bundled Copilot
2548- // CLI relies on: the hookType string "postToolUseFailure" and the
2532+ // This guards the wire-format contract: the hookType string "postToolUseFailure" and the
25492533 // input shape `{toolName, toolArgs, error, timestamp, cwd}`.
25502534 // The SDK maps that to public `{..., timestamp: Date, workingDirectory}`.
2551- const client = new CopilotClient ( ) ;
2552- await client . start ( ) ;
2553- onTestFinished ( ( ) => client . forceStop ( ) ) ;
2554-
25552535 const received : { input : any ; invocation : any } [ ] = [ ] ;
2556- const session = await client . createSession ( {
2557- onPermissionRequest : approveAll ,
2558- hooks : {
2559- onPostToolUseFailure : async ( input , invocation ) => {
2560- received . push ( { input, invocation } ) ;
2561- return { additionalContext : "context from failure hook" } ;
2562- } ,
2536+ const client = new CopilotClient ( ) ;
2537+ const session = createHookTestSession ( {
2538+ onPostToolUseFailure : async ( input , invocation ) => {
2539+ received . push ( { input, invocation } ) ;
2540+ return { additionalContext : "context from failure hook" } ;
25632541 } ,
25642542 } ) ;
2543+ ( client as any ) . sessions . set ( session . sessionId , session ) ;
25652544
25662545 const failureInput = {
25672546 toolName : "shell" ,
0 commit comments