Skip to content

Commit 93e11a4

Browse files
committed
test(openai-chat): pin privacy-safe invalid field shapes
1 parent 55a3bcc commit 93e11a4

1 file changed

Lines changed: 65 additions & 11 deletions

File tree

Lines changed: 65 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { expect, test } from "bun:test";
1+
import { afterEach, expect, test } from "bun:test";
22
import { createOpenAIChatAdapter as createOpenAIChatAdapterProduction } from "../src/adapters/openai-chat";
3+
import { getDebugLogEntries, resetDebugLogBufferForTests } from "../src/lib/debug-log-buffer";
4+
import { resetDebugSettingsForTests } from "../src/lib/debug-settings";
35
import type { AdapterEvent, OcxProviderConfig } from "../src/types";
46
import { withTestTranslatorBudget } from "./helpers/translator-budget";
57

@@ -13,35 +15,46 @@ const provider: OcxProviderConfig = {
1315
authMode: "key",
1416
};
1517

18+
const previousDebug = process.env.OCX_DEBUG;
19+
20+
afterEach(() => {
21+
resetDebugSettingsForTests();
22+
resetDebugLogBufferForTests();
23+
if (previousDebug === undefined) delete process.env.OCX_DEBUG;
24+
else process.env.OCX_DEBUG = previousDebug;
25+
});
26+
1627
async function collect(stream: AsyncGenerator<AdapterEvent>): Promise<AdapterEvent[]> {
1728
const events: AdapterEvent[] = [];
1829
for await (const event of stream) events.push(event);
1930
return events;
2031
}
2132

22-
test("object-valued streamed function.name is a 502 with value-free compatibility detail", async () => {
23-
const privateNameValue = "must-not-reach-diagnostics";
24-
const privateArguments = "must-not-reach-diagnostics-either";
25-
const adapter = createOpenAIChatAdapter(provider);
26-
const response = new Response(
33+
function malformedNameResponse(name: unknown, args = "{}"): Response {
34+
return new Response(
2735
`data: ${JSON.stringify({
2836
choices: [{
2937
index: 0,
3038
delta: {
3139
tool_calls: [{
3240
index: 0,
3341
id: "call_1",
34-
function: {
35-
name: { privateNameValue },
36-
arguments: JSON.stringify({ value: privateArguments }),
37-
},
42+
function: { name, arguments: args },
3843
}],
3944
},
4045
}],
4146
})}\n\n`,
4247
);
48+
}
4349

44-
const events = await collect(adapter.parseStream(response));
50+
test("object-valued streamed function.name is a 502 with value-free compatibility detail", async () => {
51+
const privateNameValue = "must-not-reach-diagnostics";
52+
const privateArguments = "must-not-reach-diagnostics-either";
53+
const adapter = createOpenAIChatAdapter(provider);
54+
const events = await collect(adapter.parseStream(malformedNameResponse(
55+
{ privateNameValue },
56+
JSON.stringify({ value: privateArguments }),
57+
)));
4558

4659
expect(events).toEqual([{
4760
type: "error",
@@ -54,3 +67,44 @@ test("object-valued streamed function.name is a 502 with value-free compatibilit
5467
expect(serialized).not.toContain(privateArguments);
5568
expect(serialized).not.toContain("call_1");
5669
});
70+
71+
test("provider debug fingerprints only allowlisted object structure for an invalid field", async () => {
72+
process.env.OCX_DEBUG = "1";
73+
const privateNestedValue = "must-not-reach-provider-debug";
74+
const privateUnknownKey = "must-not-reach-provider-debug-as-a-key";
75+
const privateUnknownValue = "must-not-reach-provider-debug-as-a-value";
76+
const privateArguments = "must-not-reach-provider-debug-arguments";
77+
const adapter = createOpenAIChatAdapter(provider);
78+
79+
const events = await collect(adapter.parseStream(malformedNameResponse({
80+
name: privateNestedValue,
81+
[privateUnknownKey]: privateUnknownValue,
82+
}, JSON.stringify({ privateArguments }))));
83+
84+
expect(events[0]?.message).not.toContain("fieldShape");
85+
const lines = getDebugLogEntries().map(entry => entry.line).join("\n");
86+
expect(lines).toContain('[ocx:openai-chat:invalid-tool-calls]');
87+
expect(lines).toContain('"reason":"tool_call_function_name_invalid"');
88+
expect(lines).toContain('"fieldShape":{"kind":"object"');
89+
expect(lines).toContain('"knownKeys":["name"]');
90+
expect(lines).toContain('"knownFieldTypes":{"name":"string"}');
91+
expect(lines).toContain('"hasUnknownKeys":true');
92+
expect(lines).not.toContain(privateNestedValue);
93+
expect(lines).not.toContain(privateUnknownKey);
94+
expect(lines).not.toContain(privateUnknownValue);
95+
expect(lines).not.toContain(privateArguments);
96+
expect(lines).not.toContain("call_1");
97+
});
98+
99+
test("provider debug fingerprints invalid arrays by length without retaining elements", async () => {
100+
process.env.OCX_DEBUG = "1";
101+
const privateElement = "must-not-reach-array-fingerprint";
102+
const adapter = createOpenAIChatAdapter(provider);
103+
104+
const events = await collect(adapter.parseStream(malformedNameResponse([privateElement, { privateElement }])));
105+
106+
expect(events[0]?.message).not.toContain("fieldShape");
107+
const lines = getDebugLogEntries().map(entry => entry.line).join("\n");
108+
expect(lines).toContain('"fieldShape":{"kind":"array","length":2}');
109+
expect(lines).not.toContain(privateElement);
110+
});

0 commit comments

Comments
 (0)