Skip to content

Commit b770937

Browse files
devm33Copilot
andcommitted
test: add clientName forwarding tests across all SDKs
Add tests verifying clientName is included in session.create and session.resume RPC payloads when set: - Node.js: spy on connection.sendRequest to verify payload - Python: mock _client.request to capture and assert payload - Go: JSON serialization tests for internal request structs - .NET: clone test for ClientName on SessionConfig Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent f764bf3 commit b770937

File tree

4 files changed

+129
-1
lines changed

4 files changed

+129
-1
lines changed

dotnet/test/CloneTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public void SessionConfig_Clone_CopiesAllProperties()
7878
var original = new SessionConfig
7979
{
8080
SessionId = "test-session",
81+
ClientName = "my-app",
8182
Model = "gpt-4",
8283
ReasoningEffort = "high",
8384
ConfigDir = "/config",
@@ -94,6 +95,7 @@ public void SessionConfig_Clone_CopiesAllProperties()
9495
var clone = original.Clone();
9596

9697
Assert.Equal(original.SessionId, clone.SessionId);
98+
Assert.Equal(original.ClientName, clone.ClientName);
9799
Assert.Equal(original.Model, clone.Model);
98100
Assert.Equal(original.ReasoningEffort, clone.ReasoningEffort);
99101
Assert.Equal(original.ConfigDir, clone.ConfigDir);

go/client_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package copilot
22

33
import (
4+
"encoding/json"
45
"os"
56
"path/filepath"
67
"reflect"
@@ -389,3 +390,57 @@ func fileExistsForTest(path string) bool {
389390
_, err := os.Stat(path)
390391
return err == nil
391392
}
393+
394+
func TestCreateSessionRequest_ClientName(t *testing.T) {
395+
t.Run("includes clientName in JSON when set", func(t *testing.T) {
396+
req := createSessionRequest{ClientName: "my-app"}
397+
data, err := json.Marshal(req)
398+
if err != nil {
399+
t.Fatalf("Failed to marshal: %v", err)
400+
}
401+
var m map[string]any
402+
if err := json.Unmarshal(data, &m); err != nil {
403+
t.Fatalf("Failed to unmarshal: %v", err)
404+
}
405+
if m["clientName"] != "my-app" {
406+
t.Errorf("Expected clientName to be 'my-app', got %v", m["clientName"])
407+
}
408+
})
409+
410+
t.Run("omits clientName from JSON when empty", func(t *testing.T) {
411+
req := createSessionRequest{}
412+
data, _ := json.Marshal(req)
413+
var m map[string]any
414+
json.Unmarshal(data, &m)
415+
if _, ok := m["clientName"]; ok {
416+
t.Error("Expected clientName to be omitted when empty")
417+
}
418+
})
419+
}
420+
421+
func TestResumeSessionRequest_ClientName(t *testing.T) {
422+
t.Run("includes clientName in JSON when set", func(t *testing.T) {
423+
req := resumeSessionRequest{SessionID: "s1", ClientName: "my-app"}
424+
data, err := json.Marshal(req)
425+
if err != nil {
426+
t.Fatalf("Failed to marshal: %v", err)
427+
}
428+
var m map[string]any
429+
if err := json.Unmarshal(data, &m); err != nil {
430+
t.Fatalf("Failed to unmarshal: %v", err)
431+
}
432+
if m["clientName"] != "my-app" {
433+
t.Errorf("Expected clientName to be 'my-app', got %v", m["clientName"])
434+
}
435+
})
436+
437+
t.Run("omits clientName from JSON when empty", func(t *testing.T) {
438+
req := resumeSessionRequest{SessionID: "s1"}
439+
data, _ := json.Marshal(req)
440+
var m map[string]any
441+
json.Unmarshal(data, &m)
442+
if _, ok := m["clientName"]; ok {
443+
t.Error("Expected clientName to be omitted when empty")
444+
}
445+
})
446+
}

nodejs/test/client.test.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
2-
import { describe, expect, it, onTestFinished } from "vitest";
2+
import { describe, expect, it, onTestFinished, vi } from "vitest";
33
import { CopilotClient } from "../src/index.js";
44

55
// This file is for unit tests. Where relevant, prefer to add e2e tests in e2e/*.test.ts instead
@@ -27,6 +27,35 @@ describe("CopilotClient", () => {
2727
});
2828
});
2929

30+
it("forwards clientName in session.create request", async () => {
31+
const client = new CopilotClient();
32+
await client.start();
33+
onTestFinished(() => client.forceStop());
34+
35+
const spy = vi.spyOn((client as any).connection!, "sendRequest");
36+
await client.createSession({ clientName: "my-app" });
37+
38+
expect(spy).toHaveBeenCalledWith(
39+
"session.create",
40+
expect.objectContaining({ clientName: "my-app" })
41+
);
42+
});
43+
44+
it("forwards clientName in session.resume request", async () => {
45+
const client = new CopilotClient();
46+
await client.start();
47+
onTestFinished(() => client.forceStop());
48+
49+
const session = await client.createSession();
50+
const spy = vi.spyOn((client as any).connection!, "sendRequest");
51+
await client.resumeSession(session.sessionId, { clientName: "my-app" });
52+
53+
expect(spy).toHaveBeenCalledWith(
54+
"session.resume",
55+
expect.objectContaining({ clientName: "my-app", sessionId: session.sessionId })
56+
);
57+
});
58+
3059
describe("URL parsing", () => {
3160
it("should parse port-only URL format", () => {
3261
const client = new CopilotClient({

python/test_client.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,45 @@ def test_use_logged_in_user_with_cli_url_raises(self):
147147
CopilotClient(
148148
{"cli_url": "localhost:8080", "use_logged_in_user": False, "log_level": "error"}
149149
)
150+
151+
152+
class TestSessionConfigForwarding:
153+
@pytest.mark.asyncio
154+
async def test_create_session_forwards_client_name(self):
155+
client = CopilotClient({"cli_path": CLI_PATH})
156+
await client.start()
157+
158+
try:
159+
captured = {}
160+
original_request = client._client.request
161+
162+
async def mock_request(method, params):
163+
captured[method] = params
164+
return await original_request(method, params)
165+
166+
client._client.request = mock_request
167+
await client.create_session({"client_name": "my-app"})
168+
assert captured["session.create"]["clientName"] == "my-app"
169+
finally:
170+
await client.force_stop()
171+
172+
@pytest.mark.asyncio
173+
async def test_resume_session_forwards_client_name(self):
174+
client = CopilotClient({"cli_path": CLI_PATH})
175+
await client.start()
176+
177+
try:
178+
session = await client.create_session()
179+
180+
captured = {}
181+
original_request = client._client.request
182+
183+
async def mock_request(method, params):
184+
captured[method] = params
185+
return await original_request(method, params)
186+
187+
client._client.request = mock_request
188+
await client.resume_session(session.session_id, {"client_name": "my-app"})
189+
assert captured["session.resume"]["clientName"] == "my-app"
190+
finally:
191+
await client.force_stop()

0 commit comments

Comments
 (0)