|
1 | 1 | /* eslint-disable @typescript-eslint/no-explicit-any */ |
2 | 2 | import { describe, expect, it, onTestFinished, vi } from "vitest"; |
3 | 3 | import { approveAll, CopilotClient, type ModelInfo } from "../src/index.js"; |
| 4 | +import { defaultJoinSessionPermissionHandler } from "../src/types.js"; |
4 | 5 |
|
5 | 6 | // This file is for unit tests. Where relevant, prefer to add e2e tests in e2e/*.test.ts instead |
6 | 7 |
|
@@ -97,6 +98,60 @@ describe("CopilotClient", () => { |
97 | 98 | spy.mockRestore(); |
98 | 99 | }); |
99 | 100 |
|
| 101 | + it("does not request permissions on session.resume when using the default joinSession handler", async () => { |
| 102 | + const client = new CopilotClient(); |
| 103 | + await client.start(); |
| 104 | + onTestFinished(() => client.forceStop()); |
| 105 | + |
| 106 | + const session = await client.createSession({ onPermissionRequest: approveAll }); |
| 107 | + const spy = vi |
| 108 | + .spyOn((client as any).connection!, "sendRequest") |
| 109 | + .mockImplementation(async (method: string, params: any) => { |
| 110 | + if (method === "session.resume") return { sessionId: params.sessionId }; |
| 111 | + throw new Error(`Unexpected method: ${method}`); |
| 112 | + }); |
| 113 | + |
| 114 | + await client.resumeSession(session.sessionId, { |
| 115 | + onPermissionRequest: defaultJoinSessionPermissionHandler, |
| 116 | + }); |
| 117 | + |
| 118 | + expect(spy).toHaveBeenCalledWith( |
| 119 | + "session.resume", |
| 120 | + expect.objectContaining({ |
| 121 | + sessionId: session.sessionId, |
| 122 | + requestPermission: false, |
| 123 | + }) |
| 124 | + ); |
| 125 | + spy.mockRestore(); |
| 126 | + }); |
| 127 | + |
| 128 | + it("requests permissions on session.resume when using an explicit handler", async () => { |
| 129 | + const client = new CopilotClient(); |
| 130 | + await client.start(); |
| 131 | + onTestFinished(() => client.forceStop()); |
| 132 | + |
| 133 | + const session = await client.createSession({ onPermissionRequest: approveAll }); |
| 134 | + const spy = vi |
| 135 | + .spyOn((client as any).connection!, "sendRequest") |
| 136 | + .mockImplementation(async (method: string, params: any) => { |
| 137 | + if (method === "session.resume") return { sessionId: params.sessionId }; |
| 138 | + throw new Error(`Unexpected method: ${method}`); |
| 139 | + }); |
| 140 | + |
| 141 | + await client.resumeSession(session.sessionId, { |
| 142 | + onPermissionRequest: approveAll, |
| 143 | + }); |
| 144 | + |
| 145 | + expect(spy).toHaveBeenCalledWith( |
| 146 | + "session.resume", |
| 147 | + expect.objectContaining({ |
| 148 | + sessionId: session.sessionId, |
| 149 | + requestPermission: true, |
| 150 | + }) |
| 151 | + ); |
| 152 | + spy.mockRestore(); |
| 153 | + }); |
| 154 | + |
100 | 155 | it("sends session.model.switchTo RPC with correct params", async () => { |
101 | 156 | const client = new CopilotClient(); |
102 | 157 | await client.start(); |
|
0 commit comments