diff --git a/src/plugin-handlers/config-handler-formatter.test.ts b/src/plugin-handlers/config-handler-formatter.test.ts new file mode 100644 index 000000000..d8fb8494f --- /dev/null +++ b/src/plugin-handlers/config-handler-formatter.test.ts @@ -0,0 +1,120 @@ +import { afterEach, beforeEach, describe, expect, spyOn, test } from "bun:test" + +import type { OhMyOpenCodeConfig } from "../config" +import { createConfigHandler } from "./config-handler" +import * as agentConfigHandler from "./agent-config-handler" +import * as commandConfigHandler from "./command-config-handler" +import * as mcpConfigHandler from "./mcp-config-handler" +import * as pluginComponentsLoader from "./plugin-components-loader" +import * as providerConfigHandler from "./provider-config-handler" +import * as shared from "../shared" +import * as toolConfigHandler from "./tool-config-handler" + +let logSpy: ReturnType +let loadPluginComponentsSpy: ReturnType +let applyAgentConfigSpy: ReturnType +let applyToolConfigSpy: ReturnType +let applyMcpConfigSpy: ReturnType +let applyCommandConfigSpy: ReturnType +let applyProviderConfigSpy: ReturnType + +beforeEach(() => { + logSpy = spyOn(shared, "log").mockImplementation(() => {}) + loadPluginComponentsSpy = spyOn( + pluginComponentsLoader, + "loadPluginComponents", + ).mockResolvedValue({ + commands: {}, + skills: {}, + agents: {}, + mcpServers: {}, + hooksConfigs: [], + plugins: [], + errors: [], + }) + applyAgentConfigSpy = spyOn(agentConfigHandler, "applyAgentConfig").mockResolvedValue( + {}, + ) + applyToolConfigSpy = spyOn(toolConfigHandler, "applyToolConfig").mockImplementation( + () => {}, + ) + applyMcpConfigSpy = spyOn(mcpConfigHandler, "applyMcpConfig").mockResolvedValue() + applyCommandConfigSpy = spyOn( + commandConfigHandler, + "applyCommandConfig", + ).mockResolvedValue() + applyProviderConfigSpy = spyOn( + providerConfigHandler, + "applyProviderConfig", + ).mockImplementation(() => {}) +}) + +afterEach(() => { + logSpy.mockRestore() + loadPluginComponentsSpy.mockRestore() + applyAgentConfigSpy.mockRestore() + applyToolConfigSpy.mockRestore() + applyMcpConfigSpy.mockRestore() + applyCommandConfigSpy.mockRestore() + applyProviderConfigSpy.mockRestore() +}) + +describe("createConfigHandler formatter pass-through", () => { + test("preserves formatter object configured in opencode config", async () => { + // given + const pluginConfig: OhMyOpenCodeConfig = {} + const formatterConfig = { + prettier: { + command: ["prettier", "--write"], + extensions: [".ts", ".tsx"], + environment: { + PRETTIERD_DEFAULT_CONFIG: ".prettierrc", + }, + }, + eslint: { + disabled: false, + command: ["eslint", "--fix"], + extensions: [".js", ".ts"], + }, + } + const config: Record = { + formatter: formatterConfig, + } + const handler = createConfigHandler({ + ctx: { directory: "/tmp" }, + pluginConfig, + modelCacheState: { + anthropicContext1MEnabled: false, + modelContextLimitsCache: new Map(), + }, + }) + + // when + await handler(config) + + // then + expect(config.formatter).toEqual(formatterConfig) + }) + + test("preserves formatter=false configured in opencode config", async () => { + // given + const pluginConfig: OhMyOpenCodeConfig = {} + const config: Record = { + formatter: false, + } + const handler = createConfigHandler({ + ctx: { directory: "/tmp" }, + pluginConfig, + modelCacheState: { + anthropicContext1MEnabled: false, + modelContextLimitsCache: new Map(), + }, + }) + + // when + await handler(config) + + // then + expect(config.formatter).toBe(false) + }) +}) diff --git a/src/plugin-handlers/config-handler.ts b/src/plugin-handlers/config-handler.ts index e9b814a4f..47050300f 100644 --- a/src/plugin-handlers/config-handler.ts +++ b/src/plugin-handlers/config-handler.ts @@ -20,6 +20,8 @@ export function createConfigHandler(deps: ConfigHandlerDeps) { const { ctx, pluginConfig, modelCacheState } = deps; return async (config: Record) => { + const formatterConfig = config.formatter; + applyProviderConfig({ config, modelCacheState }); const pluginComponents = await loadPluginComponents({ pluginConfig }); @@ -35,6 +37,8 @@ export function createConfigHandler(deps: ConfigHandlerDeps) { await applyMcpConfig({ config, pluginConfig, pluginComponents }); await applyCommandConfig({ config, pluginConfig, ctx, pluginComponents }); + config.formatter = formatterConfig; + log("[config-handler] config handler applied", { agentCount: Object.keys(agentResult).length, commandCount: Object.keys((config.command as Record) ?? {})