From 5a289f5cad9aa291cad4e2bb687c4a2ef072862c Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 20 Apr 2026 21:28:14 +0100 Subject: [PATCH] test: share openai plugin fixtures --- extensions/openai/index.test.ts | 194 ++++++++++++-------------------- 1 file changed, 69 insertions(+), 125 deletions(-) diff --git a/extensions/openai/index.test.ts b/extensions/openai/index.test.ts index 92cf4f44ff9..5c36ddbbee1 100644 --- a/extensions/openai/index.test.ts +++ b/extensions/openai/index.test.ts @@ -71,6 +71,57 @@ async function registerOpenAIPluginWithHook(params?: { pluginConfig?: Record, +) { + expect( + provider.resolveSystemPromptContribution?.({ + config: undefined, + agentDir: undefined, + workspaceDir: undefined, + provider: "openai", + modelId: "gpt-5.4", + promptMode: "full", + runtimeChannel: undefined, + runtimeCapabilities: undefined, + agentId: undefined, + }), + ).toEqual({ + stablePrefix: [OPENAI_GPT5_OUTPUT_CONTRACT, OPENAI_GPT5_TOOL_CALL_STYLE].join("\n\n"), + sectionOverrides, + }); +} + +function mockOpenAIImageApiResponse(params: { + finalUrl: string; + imageData: string; + revisedPrompt?: string; +}) { + const resolveApiKeySpy = vi.spyOn(providerAuth, "resolveApiKeyForProvider").mockResolvedValue({ + apiKey: "sk-test", + source: "env", + mode: "api-key", + }); + const postJsonRequestSpy = vi.spyOn(providerHttp, "postJsonRequest").mockResolvedValue({ + finalUrl: params.finalUrl, + response: { + ok: true, + json: async () => ({ + data: [ + { + b64_json: Buffer.from(params.imageData).toString("base64"), + ...(params.revisedPrompt ? { revised_prompt: params.revisedPrompt } : {}), + }, + ], + }), + } as Response, + release: vi.fn(async () => {}), + }); + vi.spyOn(providerHttp, "assertOkOrThrowHttpError").mockResolvedValue(undefined); + return { resolveApiKeySpy, postJsonRequestSpy }; +} + describe("openai plugin", () => { beforeEach(() => { vi.clearAllMocks(); @@ -81,27 +132,11 @@ describe("openai plugin", () => { }); it("generates PNG buffers from the OpenAI Images API", async () => { - const resolveApiKeySpy = vi.spyOn(providerAuth, "resolveApiKeyForProvider").mockResolvedValue({ - apiKey: "sk-test", - source: "env", - mode: "api-key", - }); - const postJsonRequestSpy = vi.spyOn(providerHttp, "postJsonRequest").mockResolvedValue({ + const { resolveApiKeySpy, postJsonRequestSpy } = mockOpenAIImageApiResponse({ finalUrl: "https://api.openai.com/v1/images/generations", - response: { - ok: true, - json: async () => ({ - data: [ - { - b64_json: Buffer.from("png-data").toString("base64"), - revised_prompt: "revised", - }, - ], - }), - } as Response, - release: vi.fn(async () => {}), + imageData: "png-data", + revisedPrompt: "revised", }); - vi.spyOn(providerHttp, "assertOkOrThrowHttpError").mockResolvedValue(undefined); const provider = buildOpenAIImageGenerationProvider(); const authStore = { version: 1, profiles: {} }; @@ -149,26 +184,10 @@ describe("openai plugin", () => { }); it("submits reference-image edits to the OpenAI Images edits endpoint", async () => { - const resolveApiKeySpy = vi.spyOn(providerAuth, "resolveApiKeyForProvider").mockResolvedValue({ - apiKey: "sk-test", - source: "env", - mode: "api-key", - }); - const postJsonRequestSpy = vi.spyOn(providerHttp, "postJsonRequest").mockResolvedValue({ + const { resolveApiKeySpy, postJsonRequestSpy } = mockOpenAIImageApiResponse({ finalUrl: "https://api.openai.com/v1/images/edits", - response: { - ok: true, - json: async () => ({ - data: [ - { - b64_json: Buffer.from("edited-image").toString("base64"), - }, - ], - }), - } as Response, - release: vi.fn(async () => {}), + imageData: "edited-image", }); - vi.spyOn(providerHttp, "assertOkOrThrowHttpError").mockResolvedValue(undefined); const provider = buildOpenAIImageGenerationProvider(); const authStore = { version: 1, profiles: {} }; @@ -493,24 +512,9 @@ describe("openai plugin", () => { expect(on).not.toHaveBeenCalledWith("before_prompt_build", expect.any(Function)); const openaiProvider = requireRegisteredProvider(providers, "openai"); - expect( - openaiProvider.resolveSystemPromptContribution?.({ - config: undefined, - agentDir: undefined, - workspaceDir: undefined, - provider: "openai", - modelId: "gpt-5.4", - promptMode: "full", - runtimeChannel: undefined, - runtimeCapabilities: undefined, - agentId: undefined, - }), - ).toEqual({ - stablePrefix: [OPENAI_GPT5_OUTPUT_CONTRACT, OPENAI_GPT5_TOOL_CALL_STYLE].join("\n\n"), - sectionOverrides: { - interaction_style: OPENAI_FRIENDLY_PROMPT_OVERLAY, - execution_bias: OPENAI_GPT5_EXECUTION_BIAS, - }, + expectOpenAIPromptContribution(openaiProvider, { + interaction_style: OPENAI_FRIENDLY_PROMPT_OVERLAY, + execution_bias: OPENAI_GPT5_EXECUTION_BIAS, }); }); @@ -521,23 +525,8 @@ describe("openai plugin", () => { expect(on).not.toHaveBeenCalledWith("before_prompt_build", expect.any(Function)); const openaiProvider = requireRegisteredProvider(providers, "openai"); - expect( - openaiProvider.resolveSystemPromptContribution?.({ - config: undefined, - agentDir: undefined, - workspaceDir: undefined, - provider: "openai", - modelId: "gpt-5.4", - promptMode: "full", - runtimeChannel: undefined, - runtimeCapabilities: undefined, - agentId: undefined, - }), - ).toEqual({ - stablePrefix: [OPENAI_GPT5_OUTPUT_CONTRACT, OPENAI_GPT5_TOOL_CALL_STYLE].join("\n\n"), - sectionOverrides: { - execution_bias: OPENAI_GPT5_EXECUTION_BIAS, - }, + expectOpenAIPromptContribution(openaiProvider, { + execution_bias: OPENAI_GPT5_EXECUTION_BIAS, }); }); @@ -547,23 +536,8 @@ describe("openai plugin", () => { }); const openaiProvider = requireRegisteredProvider(providers, "openai"); - expect( - openaiProvider.resolveSystemPromptContribution?.({ - config: undefined, - agentDir: undefined, - workspaceDir: undefined, - provider: "openai", - modelId: "gpt-5.4", - promptMode: "full", - runtimeChannel: undefined, - runtimeCapabilities: undefined, - agentId: undefined, - }), - ).toEqual({ - stablePrefix: [OPENAI_GPT5_OUTPUT_CONTRACT, OPENAI_GPT5_TOOL_CALL_STYLE].join("\n\n"), - sectionOverrides: { - execution_bias: OPENAI_GPT5_EXECUTION_BIAS, - }, + expectOpenAIPromptContribution(openaiProvider, { + execution_bias: OPENAI_GPT5_EXECUTION_BIAS, }); }); @@ -574,24 +548,9 @@ describe("openai plugin", () => { expect(on).not.toHaveBeenCalledWith("before_prompt_build", expect.any(Function)); const openaiProvider = requireRegisteredProvider(providers, "openai"); - expect( - openaiProvider.resolveSystemPromptContribution?.({ - config: undefined, - agentDir: undefined, - workspaceDir: undefined, - provider: "openai", - modelId: "gpt-5.4", - promptMode: "full", - runtimeChannel: undefined, - runtimeCapabilities: undefined, - agentId: undefined, - }), - ).toEqual({ - stablePrefix: [OPENAI_GPT5_OUTPUT_CONTRACT, OPENAI_GPT5_TOOL_CALL_STYLE].join("\n\n"), - sectionOverrides: { - interaction_style: OPENAI_FRIENDLY_PROMPT_OVERLAY, - execution_bias: OPENAI_GPT5_EXECUTION_BIAS, - }, + expectOpenAIPromptContribution(openaiProvider, { + interaction_style: OPENAI_FRIENDLY_PROMPT_OVERLAY, + execution_bias: OPENAI_GPT5_EXECUTION_BIAS, }); }); @@ -601,24 +560,9 @@ describe("openai plugin", () => { }); const openaiProvider = requireRegisteredProvider(providers, "openai"); - expect( - openaiProvider.resolveSystemPromptContribution?.({ - config: undefined, - agentDir: undefined, - workspaceDir: undefined, - provider: "openai", - modelId: "gpt-5.4", - promptMode: "full", - runtimeChannel: undefined, - runtimeCapabilities: undefined, - agentId: undefined, - }), - ).toEqual({ - stablePrefix: [OPENAI_GPT5_OUTPUT_CONTRACT, OPENAI_GPT5_TOOL_CALL_STYLE].join("\n\n"), - sectionOverrides: { - interaction_style: OPENAI_FRIENDLY_PROMPT_OVERLAY, - execution_bias: OPENAI_GPT5_EXECUTION_BIAS, - }, + expectOpenAIPromptContribution(openaiProvider, { + interaction_style: OPENAI_FRIENDLY_PROMPT_OVERLAY, + execution_bias: OPENAI_GPT5_EXECUTION_BIAS, }); }); });