Skip to content

Commit 2063bc8

Browse files
Assert transform result in system message via HTTP traffic
The 'should apply transform modifications' tests previously only verified that the transform callback was invoked, not that the transformed content actually reached the model. Now all 4 SDKs assert that TRANSFORM_MARKER appears in the system message captured from HTTP traffic. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 3321896 commit 2063bc8

4 files changed

Lines changed: 47 additions & 41 deletions

File tree

dotnet/test/SystemMessageTransformTests.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ await session.SendAsync(new MessageOptions
6262
[Fact]
6363
public async Task Should_Apply_Transform_Modifications_To_Section_Content()
6464
{
65-
var transformCallbackInvoked = false;
66-
6765
var session = await CreateSessionAsync(new SessionConfig
6866
{
6967
OnPermissionRequest = PermissionHandler.ApproveAll,
@@ -76,8 +74,7 @@ public async Task Should_Apply_Transform_Modifications_To_Section_Content()
7674
{
7775
Transform = async (content) =>
7876
{
79-
transformCallbackInvoked = true;
80-
return content + "TRANSFORM_MARKER";
77+
return content + "\nAlways end your reply with TRANSFORM_MARKER";
8178
}
8279
}
8380
}
@@ -93,7 +90,11 @@ await session.SendAsync(new MessageOptions
9390

9491
await TestHelper.GetFinalAssistantMessageAsync(session);
9592

96-
Assert.True(transformCallbackInvoked, "Expected identity transform callback to be invoked");
93+
// Verify the transform result was actually applied to the system message
94+
var traffic = await Ctx.GetExchangesAsync();
95+
Assert.NotEmpty(traffic);
96+
var systemMessage = GetSystemMessage(traffic[0]);
97+
Assert.Contains("TRANSFORM_MARKER", systemMessage);
9798
}
9899

99100
[Fact]

go/internal/e2e/system_message_transform_test.go

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package e2e
66
import (
77
"os"
88
"path/filepath"
9+
"strings"
910
"sync"
1011
"testing"
1112

@@ -90,22 +91,14 @@ func TestSystemMessageTransform(t *testing.T) {
9091
t.Run("should_apply_transform_modifications_to_section_content", func(t *testing.T) {
9192
ctx.ConfigureForTest(t)
9293

93-
var receivedContent string
94-
var mu sync.Mutex
95-
transformCalled := false
96-
9794
session, err := client.CreateSession(t.Context(), &copilot.SessionConfig{
9895
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
9996
SystemMessage: &copilot.SystemMessageConfig{
10097
Mode: "customize",
10198
Sections: map[string]copilot.SectionOverride{
10299
"identity": {
103100
Transform: func(currentContent string) (string, error) {
104-
mu.Lock()
105-
transformCalled = true
106-
receivedContent = currentContent
107-
mu.Unlock()
108-
return currentContent + "\nTRANSFORM_MARKER", nil
101+
return currentContent + "\nAlways end your reply with TRANSFORM_MARKER", nil
109102
},
110103
},
111104
},
@@ -121,22 +114,27 @@ func TestSystemMessageTransform(t *testing.T) {
121114
t.Fatalf("Failed to write test file: %v", err)
122115
}
123116

124-
_, err = session.SendAndWait(t.Context(), copilot.MessageOptions{
117+
assistantMessage, err := session.SendAndWait(t.Context(), copilot.MessageOptions{
125118
Prompt: "Read the contents of hello.txt",
126119
})
127120
if err != nil {
128121
t.Fatalf("Failed to send message: %v", err)
129122
}
130123

131-
mu.Lock()
132-
defer mu.Unlock()
133-
134-
if !transformCalled {
135-
t.Error("Expected transform callback to be invoked")
124+
// Verify the transform result was actually applied to the system message
125+
traffic, err := ctx.GetExchanges()
126+
if err != nil {
127+
t.Fatalf("Failed to get exchanges: %v", err)
128+
}
129+
if len(traffic) == 0 {
130+
t.Fatal("Expected at least one exchange")
136131
}
137-
if receivedContent == "" {
138-
t.Error("Expected transform to receive non-empty content")
132+
systemMessage := getSystemMessage(traffic[0])
133+
if !strings.Contains(systemMessage, "TRANSFORM_MARKER") {
134+
t.Errorf("Expected system message to contain TRANSFORM_MARKER, got %q", systemMessage)
139135
}
136+
137+
_ = assistantMessage
140138
})
141139

142140
t.Run("should_work_with_static_overrides_and_transforms_together", func(t *testing.T) {

nodejs/test/e2e/system_message_transform.test.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
import { writeFile } from "fs/promises";
66
import { join } from "path";
77
import { describe, expect, it } from "vitest";
8+
import { ParsedHttpExchange } from "../../../test/harness/replayingCapiProxy.js";
89
import { approveAll } from "../../src/index.js";
910
import { createSdkTestContext } from "./harness/sdkTestContext.js";
1011

1112
describe("System message transform", async () => {
12-
const { copilotClient: client, workDir } = await createSdkTestContext();
13+
const { copilotClient: client, openAiEndpoint, workDir } = await createSdkTestContext();
1314

1415
it("should invoke transform callbacks with section content", async () => {
1516
const transformedSections: Record<string, string> = {};
@@ -53,20 +54,14 @@ describe("System message transform", async () => {
5354
});
5455

5556
it("should apply transform modifications to section content", async () => {
56-
let originalContent = "";
57-
let transformedContent = "";
58-
5957
const session = await client.createSession({
6058
onPermissionRequest: approveAll,
6159
systemMessage: {
6260
mode: "customize",
6361
sections: {
6462
identity: {
6563
action: (content: string) => {
66-
originalContent = content;
67-
// Append a custom instruction via transform
68-
transformedContent = content + "\nTRANSFORM_MARKER";
69-
return transformedContent;
64+
return content + "\nTRANSFORM_MARKER";
7065
},
7166
},
7267
},
@@ -79,10 +74,10 @@ describe("System message transform", async () => {
7974
prompt: "Read the contents of hello.txt",
8075
});
8176

82-
// Verify the transform callback was invoked and modified the content
83-
expect(originalContent.length).toBeGreaterThan(0);
84-
expect(transformedContent).toContain("TRANSFORM_MARKER");
85-
expect(transformedContent).toContain(originalContent);
77+
// Verify the transform result was actually applied to the system message
78+
const traffic = await openAiEndpoint.getExchanges();
79+
const systemMessage = getSystemMessage(traffic[0]);
80+
expect(systemMessage).toContain("TRANSFORM_MARKER");
8681

8782
await session.disconnect();
8883
});
@@ -121,3 +116,10 @@ describe("System message transform", async () => {
121116
await session.disconnect();
122117
});
123118
});
119+
120+
function getSystemMessage(exchange: ParsedHttpExchange): string | undefined {
121+
const systemMessage = exchange.request.messages.find((m) => m.role === "system") as
122+
| { role: "system"; content: string }
123+
| undefined;
124+
return systemMessage?.content;
125+
}

python/e2e/test_system_message_transform.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,8 @@ async def test_should_apply_transform_modifications_to_section_content(
5959
self, ctx: E2ETestContext
6060
):
6161
"""Test that transform modifications are applied to the section content"""
62-
identity_contents = []
6362

6463
async def identity_transform(content: str) -> str:
65-
identity_contents.append(content)
6664
return content + "\nTRANSFORM_MARKER"
6765

6866
session = await ctx.client.create_session(
@@ -79,11 +77,10 @@ async def identity_transform(content: str) -> str:
7977

8078
await session.send_and_wait("Read the contents of hello.txt")
8179

82-
# The transform callback should have been invoked
83-
assert len(identity_contents) > 0
84-
85-
# The callback should have received content and returned modified content
86-
assert all(len(c) > 0 for c in identity_contents)
80+
# Verify the transform result was actually applied to the system message
81+
traffic = await ctx.get_exchanges()
82+
system_message = _get_system_message(traffic[0])
83+
assert "TRANSFORM_MARKER" in system_message
8784

8885
await session.disconnect()
8986

@@ -116,3 +113,11 @@ async def identity_transform(content: str) -> str:
116113
assert len(identity_contents) > 0
117114

118115
await session.disconnect()
116+
117+
118+
def _get_system_message(exchange: dict) -> str:
119+
messages = exchange.get("request", {}).get("messages", [])
120+
for msg in messages:
121+
if msg.get("role") == "system":
122+
return msg.get("content", "")
123+
return ""

0 commit comments

Comments
 (0)