// @ts-nocheckimport test from "node:test";import assert from "node:assert/strict";const { handleChatCore } = await import("../../open-sse/handlers/chatCore.ts");const originalFetch = globalThis.fetch;type ToolItem = { type: string; namespace?: string; name?: string };type SseFrame = { event: string | undefined; data: unknown };function requestBody(stream: boolean) { return { model: "gpt-4o-mini", stream, input: [ { type: "additional_tools", tools: [ { type: "namespace", name: "mcp__atlassian__cloud__tenant", tools: [{ name: "read_issue", parameters: { type: "object" } }], }, ], }, { type: "message", role: "user", content: [{ type: "input_text", text: "read it" }] }, ], };}function chatToolCall() { return { id: "chatcmpl_namespace_identity", object: "chat.completion", created: 1, model: "gpt-4o-mini", choices: [ { index: 0, message: { role: "assistant", tool_calls: [ { index: 0, id: "call_namespace_identity", type: "function", function: { name: "mcp__atlassian__cloud__tenant__read_issue", arguments: JSON.stringify({ key: "PROJ-1" }), }, }, ], }, finish_reason: "tool_calls", }, ], usage: { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 }, };}function streamingChatToolCall() { const toolCall = chatToolCall().choices[0].message.tool_calls[0]; return [ { id: "chatcmpl_namespace_identity", object: "chat.completion.chunk", created: 1, model: "gpt-4o-mini", choices: [ { index: 0, delta: { role: "assistant", tool_calls: [toolCall] }, finish_reason: "tool_calls", }, ], }, ];}function sse(events: unknown[]) { return ( events.map((event) => "data: " + JSON.stringify(event) + "\n\n").join("") + "data: [DONE]\n\n" );}async function invokeResponsesRequest(stream: boolean) { const calls: Array<{ body: Record }> = []; globalThis.fetch = async (_url, init = {}) => { const body = JSON.parse(String(init.body)); calls.push({ body }); const payload = stream ? sse(streamingChatToolCall()) : JSON.stringify(chatToolCall()); return new Response(payload, { status: 200, headers: { "Content-Type": stream ? "text/event-stream" : "application/json" }, }); }; try { const body = requestBody(stream); const result = await handleChatCore({ body: structuredClone(body), modelInfo: { provider: "openai", model: "gpt-4o-mini", extendedContext: false }, credentials: { apiKey: "sk-test", providerSpecificData: {} }, log: { debug() {}, info() {}, warn() {}, error() {} }, clientRawRequest: { endpoint: "/v1/responses", body: structuredClone(body), headers: new Headers({ accept: stream ? "text/event-stream" : "application/json" }), }, userAgent: "namespace-identity-integration-test", } as never); assert.equal(result.success, true); assert.equal(calls.length, 1); assert.equal( "_toolNameMap" in calls[0].body, false, "identity metadata must not reach upstream" ); return result.response; } finally { globalThis.fetch = originalFetch; }}function parseSseFrames(body: string): SseFrame[] { return body .trim() .split(/\r?\n\r?\n/) .map((frame) => { const fields = frame.split(/\r?\n/); const event = fields.find((line) => line.startsWith("event: "))?.slice("event: ".length); const data = fields.find((line) => line.startsWith("data: "))?.slice("data: ".length); return { event, data: data === "[DONE]" ? data : JSON.parse(data ?? "null") }; });}function assertNamespaceTuple(item: ToolItem | undefined) { assert.ok(item, "expected a function-call output item"); assert.equal(item.type, "function_call"); assert.deepEqual( { namespace: item.namespace, name: item.name }, { namespace: "mcp__atlassian__cloud__tenant", name: "read_issue" } );}test.afterEach(() => { globalThis.fetch = originalFetch;});test("returns a namespace tuple for a non-streaming Responses request translated through chatCore", async () => { const response = await invokeResponsesRequest(false); const payload = await response.json(); assertNamespaceTuple(payload.output.find((item: ToolItem) => item.type === "function_call"));});test("emits namespace tuples for every streaming Responses function-call event translated through chatCore", async () => { const response = await invokeResponsesRequest(true); const frames = parseSseFrames(await response.text()); const added = frames.find((frame) => frame.event === "response.output_item.added"); assertNamespaceTuple((added?.data as { item?: ToolItem })?.item); const done = frames.find((frame) => frame.event === "response.output_item.done"); assertNamespaceTuple((done?.data as { item?: ToolItem })?.item); const completed = frames.find((frame) => frame.event === "response.completed"); assertNamespaceTuple( (completed?.data as { response?: { output?: ToolItem[] } })?.response?.output?.find( (item) => item.type === "function_call" ) );});