forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpc.test.ts
More file actions
182 lines (142 loc) · 6.37 KB
/
rpc.test.ts
File metadata and controls
182 lines (142 loc) · 6.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { describe, expect, it, onTestFinished } from "vitest";
import { CopilotClient, approveAll } from "../../src/index.js";
import { createSdkTestContext } from "./harness/sdkTestContext.js";
function onTestFinishedForceStop(client: CopilotClient) {
onTestFinished(async () => {
try {
await client.forceStop();
} catch {
// Ignore cleanup errors - process may already be stopped
}
});
}
describe("RPC", () => {
it("should call rpc.ping with typed params and result", async () => {
const client = new CopilotClient({ useStdio: true });
onTestFinishedForceStop(client);
await client.start();
const result = await client.rpc.ping({ message: "typed rpc test" });
expect(result.message).toBe("pong: typed rpc test");
expect(typeof result.timestamp).toBe("number");
await client.stop();
});
it("should call rpc.models.list with typed result", async () => {
const client = new CopilotClient({ useStdio: true });
onTestFinishedForceStop(client);
await client.start();
const authStatus = await client.getAuthStatus();
if (!authStatus.isAuthenticated) {
await client.stop();
return;
}
const result = await client.rpc.models.list();
expect(result.models).toBeDefined();
expect(Array.isArray(result.models)).toBe(true);
await client.stop();
});
// account.getQuota is defined in schema but not yet implemented in CLI
it.skip("should call rpc.account.getQuota when authenticated", async () => {
const client = new CopilotClient({ useStdio: true });
onTestFinishedForceStop(client);
await client.start();
const authStatus = await client.getAuthStatus();
if (!authStatus.isAuthenticated) {
await client.stop();
return;
}
const result = await client.rpc.account.getQuota();
expect(result.quotaSnapshots).toBeDefined();
expect(typeof result.quotaSnapshots).toBe("object");
await client.stop();
});
});
describe("Session RPC", async () => {
const { copilotClient: client } = await createSdkTestContext();
// session.model.getCurrent is defined in schema but not yet implemented in CLI
it.skip("should call session.rpc.model.getCurrent", async () => {
const session = await client.createSession({
onPermissionRequest: approveAll,
model: "claude-sonnet-4.5",
});
const result = await session.rpc.model.getCurrent();
expect(result.modelId).toBeDefined();
expect(typeof result.modelId).toBe("string");
});
// session.model.switchTo is defined in schema but not yet implemented in CLI
it.skip("should call session.rpc.model.switchTo", async () => {
const session = await client.createSession({
onPermissionRequest: approveAll,
model: "claude-sonnet-4.5",
});
// Get initial model
const before = await session.rpc.model.getCurrent();
expect(before.modelId).toBeDefined();
// Switch to a different model with reasoning effort
const result = await session.rpc.model.switchTo({
modelId: "gpt-4.1",
reasoningEffort: "high",
});
expect(result.modelId).toBe("gpt-4.1");
// Verify the switch persisted
const after = await session.rpc.model.getCurrent();
expect(after.modelId).toBe("gpt-4.1");
});
it("should get and set session mode", async () => {
const session = await client.createSession({ onPermissionRequest: approveAll });
// Get initial mode (default should be interactive)
const initial = await session.rpc.mode.get();
expect(initial.mode).toBe("interactive");
// Switch to plan mode
const planResult = await session.rpc.mode.set({ mode: "plan" });
expect(planResult.mode).toBe("plan");
// Verify mode persisted
const afterPlan = await session.rpc.mode.get();
expect(afterPlan.mode).toBe("plan");
// Switch back to interactive
const interactiveResult = await session.rpc.mode.set({ mode: "interactive" });
expect(interactiveResult.mode).toBe("interactive");
});
it("should read, update, and delete plan", async () => {
const session = await client.createSession({ onPermissionRequest: approveAll });
// Initially plan should not exist
const initial = await session.rpc.plan.read();
expect(initial.exists).toBe(false);
expect(initial.content).toBeNull();
// Create/update plan
const planContent = "# Test Plan\n\n- Step 1\n- Step 2";
await session.rpc.plan.update({ content: planContent });
// Verify plan exists and has correct content
const afterUpdate = await session.rpc.plan.read();
expect(afterUpdate.exists).toBe(true);
expect(afterUpdate.content).toBe(planContent);
// Delete plan
await session.rpc.plan.delete();
// Verify plan is deleted
const afterDelete = await session.rpc.plan.read();
expect(afterDelete.exists).toBe(false);
expect(afterDelete.content).toBeNull();
});
it("should create, list, and read workspace files", async () => {
const session = await client.createSession({ onPermissionRequest: approveAll });
// Initially no files
const initialFiles = await session.rpc.workspace.listFiles();
expect(initialFiles.files).toEqual([]);
// Create a file
const fileContent = "Hello, workspace!";
await session.rpc.workspace.createFile({ path: "test.txt", content: fileContent });
// List files
const afterCreate = await session.rpc.workspace.listFiles();
expect(afterCreate.files).toContain("test.txt");
// Read file
const readResult = await session.rpc.workspace.readFile({ path: "test.txt" });
expect(readResult.content).toBe(fileContent);
// Create nested file
await session.rpc.workspace.createFile({
path: "subdir/nested.txt",
content: "Nested content",
});
const afterNested = await session.rpc.workspace.listFiles();
expect(afterNested.files).toContain("test.txt");
expect(afterNested.files.some((f) => f.includes("nested.txt"))).toBe(true);
});
});